From 9539f662acfc32c57ff053fdbd212efaecf55b24 Mon Sep 17 00:00:00 2001 From: Sohail Hussain Date: Tue, 28 Aug 2018 15:19:41 -0700 Subject: [PATCH 1/6] fix(datafile-parsing): Prevent SDK from initializing if the datafile has version other than supported versions --- .../Exceptions/OptimizelyException.cs | 9 +++++++ OptimizelySDK/Optimizely.cs | 6 +++++ OptimizelySDK/ProjectConfig.cs | 27 +++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/OptimizelySDK/Exceptions/OptimizelyException.cs b/OptimizelySDK/Exceptions/OptimizelyException.cs index a3770aaf..56c7b3ed 100644 --- a/OptimizelySDK/Exceptions/OptimizelyException.cs +++ b/OptimizelySDK/Exceptions/OptimizelyException.cs @@ -105,4 +105,13 @@ public InvalidRolloutException(string message) { } } + + public class ConfigParseException : OptimizelyException + { + public ConfigParseException(string message) + : base(message) + { + + } + } } \ No newline at end of file diff --git a/OptimizelySDK/Optimizely.cs b/OptimizelySDK/Optimizely.cs index 62df0dd3..ccaa0f06 100644 --- a/OptimizelySDK/Optimizely.cs +++ b/OptimizelySDK/Optimizely.cs @@ -111,9 +111,15 @@ public Optimizely(string datafile, IsValid = true; DecisionService = new DecisionService(Bucketer, ErrorHandler, Config, userProfileService, Logger); } + catch (OptimizelySDK.Exceptions.ConfigParseException parseException) + { + Logger.Log(LogLevel.ERROR, parseException.Message); + ErrorHandler.HandleError(parseException); + } catch (Exception ex) { Logger.Log(LogLevel.ERROR, "Provided 'datafile' is in an invalid format. " + ex.Message); + ErrorHandler.HandleError(ex); } } diff --git a/OptimizelySDK/ProjectConfig.cs b/OptimizelySDK/ProjectConfig.cs index 54f5c45a..fd74b986 100644 --- a/OptimizelySDK/ProjectConfig.cs +++ b/OptimizelySDK/ProjectConfig.cs @@ -25,6 +25,11 @@ namespace OptimizelySDK { public class ProjectConfig { + public enum OPTLYSDKVersion { + V2 = 2, + V3 = 3, + V4 = 4 + } public const string RESERVED_ATTRIBUTE_PREFIX = "$opt_"; /// @@ -60,6 +65,14 @@ public class ProjectConfig /// public bool? BotFiltering { get; set; } + + private static List SupportedVersions = new List { + OPTLYSDKVersion.V2, + OPTLYSDKVersion.V3, + OPTLYSDKVersion.V4 + }; + + //========================= Mappings =========================== /// @@ -272,14 +285,28 @@ public static ProjectConfig Create(string content, ILogger logger, IErrorHandler { ProjectConfig config = JsonConvert.DeserializeObject(content); + ValidateSupport(config); + config.Logger = logger; config.ErrorHandler = errorHandler; + + config.Initialize(); return config; } + public static void ValidateSupport(ProjectConfig config) + { + if(SupportedVersions.TrueForAll((obj) => !(obj.ToString() == config.Version))) + { + throw new Exceptions.ConfigParseException(string.Format(@"This version of the C# SDK does not support the given datafile version: {0}", config.Version)); + } + } + + + //========================= Getters =========================== From 914ac4119ad4d80db96106d3956e97ccca5c6daa Mon Sep 17 00:00:00 2001 From: mfahadahmed Date: Wed, 29 Aug 2018 20:00:33 +0500 Subject: [PATCH 2/6] Add unit tests for unsupported config version. --- OptimizelySDK.Tests/OptimizelyTest.cs | 2 +- OptimizelySDK.Tests/ProjectConfigTest.cs | 31 ++++++++++++++++++++++++ OptimizelySDK/Optimizely.cs | 7 +++--- OptimizelySDK/ProjectConfig.cs | 31 +++++++++++++++--------- 4 files changed, 55 insertions(+), 16 deletions(-) diff --git a/OptimizelySDK.Tests/OptimizelyTest.cs b/OptimizelySDK.Tests/OptimizelyTest.cs index 490ef7e7..4949cfda 100644 --- a/OptimizelySDK.Tests/OptimizelyTest.cs +++ b/OptimizelySDK.Tests/OptimizelyTest.cs @@ -185,7 +185,7 @@ public void TestValidateInputsInvalidFileJsonValidationSkipped() { string datafile = "{\"name\":\"optimizely\"}"; Optimizely optimizely = new Optimizely(datafile, null, null, null, skipJsonValidation: true); - Assert.IsTrue(optimizely.IsValid); + Assert.IsFalse(optimizely.IsValid); } [Test] diff --git a/OptimizelySDK.Tests/ProjectConfigTest.cs b/OptimizelySDK.Tests/ProjectConfigTest.cs index 8dece0f8..4f3769aa 100644 --- a/OptimizelySDK.Tests/ProjectConfigTest.cs +++ b/OptimizelySDK.Tests/ProjectConfigTest.cs @@ -863,5 +863,36 @@ public void TestGetAttributeIdWithInvalidAttributeKey() Assert.Null(Config.GetAttributeId("invalid_attribute")); LoggerMock.Verify(l => l.Log(LogLevel.ERROR, @"Attribute key ""invalid_attribute"" is not in datafile.")); } + + [Test] + public void TestCreateThrowsWithNullDatafile() + { + var exception = Assert.Throws(() => ProjectConfig.Create(null, null, null)); + Assert.AreEqual("Unable to parse null datafile.", exception.Message); + } + + [Test] + public void TestCreateThrowsWithEmptyDatafile() + { + var exception = Assert.Throws(() => ProjectConfig.Create("", null, null)); + Assert.AreEqual("Unable to parse empty datafile.", exception.Message); + } + + [Test] + public void TestCreateThrowsWithUnsopportedDatafileVersion() + { + var unsupportedConfig = ProjectConfig.Create(TestData.Datafile, null, null); + unsupportedConfig.Version = "5"; + var unsupportedConfigData = JsonConvert.SerializeObject(unsupportedConfig); + + var exception = Assert.Throws(() => ProjectConfig.Create(unsupportedConfigData, null, null)); + Assert.AreEqual("This version of the C# SDK does not support the given datafile version: 5", exception.Message); + } + + [Test] + public void TestCreateDoesNotThrowWithValidDatafile() + { + Assert.DoesNotThrow(() => ProjectConfig.Create(TestData.Datafile, null, null)); + } } } diff --git a/OptimizelySDK/Optimizely.cs b/OptimizelySDK/Optimizely.cs index ccaa0f06..5f028ae1 100644 --- a/OptimizelySDK/Optimizely.cs +++ b/OptimizelySDK/Optimizely.cs @@ -18,6 +18,7 @@ using OptimizelySDK.ErrorHandler; using OptimizelySDK.Event.Builder; using OptimizelySDK.Event.Dispatcher; +using OptimizelySDK.Exceptions; using OptimizelySDK.Logger; using OptimizelySDK.Utils; using OptimizelySDK.Notifications; @@ -111,10 +112,10 @@ public Optimizely(string datafile, IsValid = true; DecisionService = new DecisionService(Bucketer, ErrorHandler, Config, userProfileService, Logger); } - catch (OptimizelySDK.Exceptions.ConfigParseException parseException) + catch (ConfigParseException configException) { - Logger.Log(LogLevel.ERROR, parseException.Message); - ErrorHandler.HandleError(parseException); + Logger.Log(LogLevel.ERROR, configException.Message); + ErrorHandler.HandleError(configException); } catch (Exception ex) { diff --git a/OptimizelySDK/ProjectConfig.cs b/OptimizelySDK/ProjectConfig.cs index fd74b986..aab0367f 100644 --- a/OptimizelySDK/ProjectConfig.cs +++ b/OptimizelySDK/ProjectConfig.cs @@ -16,6 +16,7 @@ using Newtonsoft.Json; using OptimizelySDK.Entity; using OptimizelySDK.ErrorHandler; +using OptimizelySDK.Exceptions; using OptimizelySDK.Logger; using OptimizelySDK.Utils; using System.Collections.Generic; @@ -25,11 +26,13 @@ namespace OptimizelySDK { public class ProjectConfig { - public enum OPTLYSDKVersion { + public enum OPTLYSDKVersion + { V2 = 2, V3 = 3, V4 = 4 } + public const string RESERVED_ATTRIBUTE_PREFIX = "$opt_"; /// @@ -283,26 +286,30 @@ private void Initialize() public static ProjectConfig Create(string content, ILogger logger, IErrorHandler errorHandler) { - ProjectConfig config = JsonConvert.DeserializeObject(content); - - ValidateSupport(config); + ProjectConfig config = GetConfig(content); config.Logger = logger; config.ErrorHandler = errorHandler; - - config.Initialize(); return config; } - public static void ValidateSupport(ProjectConfig config) - { - if(SupportedVersions.TrueForAll((obj) => !(obj.ToString() == config.Version))) - { - throw new Exceptions.ConfigParseException(string.Format(@"This version of the C# SDK does not support the given datafile version: {0}", config.Version)); - } + private static ProjectConfig GetConfig(string configData) + { + if (configData == null) + throw new ConfigParseException("Unable to parse null datafile."); + + if (configData.Length == 0) + throw new ConfigParseException("Unable to parse empty datafile."); + + var config = JsonConvert.DeserializeObject(configData); + + if (SupportedVersions.TrueForAll((obj) => !(((int)obj).ToString() == config.Version))) + throw new ConfigParseException(string.Format(@"This version of the C# SDK does not support the given datafile version: {0}", config.Version)); + + return config; } From 5d73bda53804d42cc49ca2a80eb0b1c05d1b79f8 Mon Sep 17 00:00:00 2001 From: mfahadahmed Date: Thu, 30 Aug 2018 15:54:12 +0500 Subject: [PATCH 3/6] Added more unit tests for unsupported config version. --- OptimizelySDK.Tests/OptimizelyTest.cs | 21 +++++++++++++++++++++ OptimizelySDK.Tests/ProjectConfigTest.cs | 6 +++--- OptimizelySDK/ProjectConfig.cs | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/OptimizelySDK.Tests/OptimizelyTest.cs b/OptimizelySDK.Tests/OptimizelyTest.cs index 4949cfda..d51706ae 100644 --- a/OptimizelySDK.Tests/OptimizelyTest.cs +++ b/OptimizelySDK.Tests/OptimizelyTest.cs @@ -29,6 +29,7 @@ using OptimizelySDK.Notifications; using OptimizelySDK.Tests.NotificationTests; using OptimizelySDK.Utils; +using Newtonsoft.Json; namespace OptimizelySDK.Tests { @@ -188,6 +189,26 @@ public void TestValidateInputsInvalidFileJsonValidationSkipped() Assert.IsFalse(optimizely.IsValid); } + [Test] + public void TestErrorHandlingWithInvalidConfigVersion() + { + var unsupportedConfig = JsonConvert.DeserializeObject(TestData.Datafile); + unsupportedConfig.Version = "5"; + var unsupportedConfigData = JsonConvert.SerializeObject(unsupportedConfig); + + var optimizelyNullDatafile = new Optimizely(null, null, LoggerMock.Object, ErrorHandlerMock.Object, null, true); + var optimizelyEmptyDatafile = new Optimizely("", null, LoggerMock.Object, ErrorHandlerMock.Object, null, true); + var optimizelyUnsupportedVersion = new Optimizely(unsupportedConfigData, null, LoggerMock.Object, ErrorHandlerMock.Object, null, true); + + LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Unable to parse null datafile."), Times.Once); + LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Unable to parse empty datafile."), Times.Once); + LoggerMock.Verify(l => l.Log(LogLevel.ERROR, $"This version of the C# SDK does not support the given datafile version: {unsupportedConfig.Version}"), Times.Once); + + ErrorHandlerMock.Verify(e => e.HandleError(It.Is(ex => ex.Message == "Unable to parse null datafile.")), Times.Once); + ErrorHandlerMock.Verify(e => e.HandleError(It.Is(ex => ex.Message == "Unable to parse null datafile.")), Times.Once); + ErrorHandlerMock.Verify(e => e.HandleError(It.Is(ex => ex.Message == $"This version of the C# SDK does not support the given datafile version: {unsupportedConfig.Version}")), Times.Once); + } + [Test] public void TestValidatePreconditionsExperimentNotRunning() { diff --git a/OptimizelySDK.Tests/ProjectConfigTest.cs b/OptimizelySDK.Tests/ProjectConfigTest.cs index 4f3769aa..87370b27 100644 --- a/OptimizelySDK.Tests/ProjectConfigTest.cs +++ b/OptimizelySDK.Tests/ProjectConfigTest.cs @@ -879,14 +879,14 @@ public void TestCreateThrowsWithEmptyDatafile() } [Test] - public void TestCreateThrowsWithUnsopportedDatafileVersion() + public void TestCreateThrowsWithUnsupportedDatafileVersion() { - var unsupportedConfig = ProjectConfig.Create(TestData.Datafile, null, null); + var unsupportedConfig = JsonConvert.DeserializeObject(TestData.Datafile); unsupportedConfig.Version = "5"; var unsupportedConfigData = JsonConvert.SerializeObject(unsupportedConfig); var exception = Assert.Throws(() => ProjectConfig.Create(unsupportedConfigData, null, null)); - Assert.AreEqual("This version of the C# SDK does not support the given datafile version: 5", exception.Message); + Assert.AreEqual($"This version of the C# SDK does not support the given datafile version: {unsupportedConfig.Version}", exception.Message); } [Test] diff --git a/OptimizelySDK/ProjectConfig.cs b/OptimizelySDK/ProjectConfig.cs index aab0367f..ff6ec53d 100644 --- a/OptimizelySDK/ProjectConfig.cs +++ b/OptimizelySDK/ProjectConfig.cs @@ -301,7 +301,7 @@ private static ProjectConfig GetConfig(string configData) if (configData == null) throw new ConfigParseException("Unable to parse null datafile."); - if (configData.Length == 0) + if (string.IsNullOrEmpty(configData)) throw new ConfigParseException("Unable to parse empty datafile."); var config = JsonConvert.DeserializeObject(configData); From 5e619963a775d99181b19c832d961fbb16f2eab4 Mon Sep 17 00:00:00 2001 From: mfahadahmed Date: Wed, 5 Sep 2018 17:31:32 +0500 Subject: [PATCH 4/6] Fixed code review defects. --- OptimizelySDK.Tests/OptimizelySDK.Tests.csproj | 1 + OptimizelySDK.Tests/OptimizelyTest.cs | 10 +++------- OptimizelySDK.Tests/ProjectConfigTest.cs | 8 ++------ OptimizelySDK.Tests/TestData.cs | 13 +++++++++++-- OptimizelySDK/Optimizely.cs | 13 +++++++------ OptimizelySDK/ProjectConfig.cs | 5 +---- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/OptimizelySDK.Tests/OptimizelySDK.Tests.csproj b/OptimizelySDK.Tests/OptimizelySDK.Tests.csproj index e63b3923..dce9010a 100644 --- a/OptimizelySDK.Tests/OptimizelySDK.Tests.csproj +++ b/OptimizelySDK.Tests/OptimizelySDK.Tests.csproj @@ -104,6 +104,7 @@ + diff --git a/OptimizelySDK.Tests/OptimizelyTest.cs b/OptimizelySDK.Tests/OptimizelyTest.cs index d51706ae..b1ad6cff 100644 --- a/OptimizelySDK.Tests/OptimizelyTest.cs +++ b/OptimizelySDK.Tests/OptimizelyTest.cs @@ -192,21 +192,17 @@ public void TestValidateInputsInvalidFileJsonValidationSkipped() [Test] public void TestErrorHandlingWithInvalidConfigVersion() { - var unsupportedConfig = JsonConvert.DeserializeObject(TestData.Datafile); - unsupportedConfig.Version = "5"; - var unsupportedConfigData = JsonConvert.SerializeObject(unsupportedConfig); - var optimizelyNullDatafile = new Optimizely(null, null, LoggerMock.Object, ErrorHandlerMock.Object, null, true); var optimizelyEmptyDatafile = new Optimizely("", null, LoggerMock.Object, ErrorHandlerMock.Object, null, true); - var optimizelyUnsupportedVersion = new Optimizely(unsupportedConfigData, null, LoggerMock.Object, ErrorHandlerMock.Object, null, true); + var optimizelyUnsupportedVersion = new Optimizely(TestData.UnsupportedVersionDatafile, null, LoggerMock.Object, ErrorHandlerMock.Object, null, true); LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Unable to parse null datafile."), Times.Once); LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Unable to parse empty datafile."), Times.Once); - LoggerMock.Verify(l => l.Log(LogLevel.ERROR, $"This version of the C# SDK does not support the given datafile version: {unsupportedConfig.Version}"), Times.Once); + LoggerMock.Verify(l => l.Log(LogLevel.ERROR, $"This version of the C# SDK does not support the given datafile version: 5"), Times.Once); ErrorHandlerMock.Verify(e => e.HandleError(It.Is(ex => ex.Message == "Unable to parse null datafile.")), Times.Once); ErrorHandlerMock.Verify(e => e.HandleError(It.Is(ex => ex.Message == "Unable to parse null datafile.")), Times.Once); - ErrorHandlerMock.Verify(e => e.HandleError(It.Is(ex => ex.Message == $"This version of the C# SDK does not support the given datafile version: {unsupportedConfig.Version}")), Times.Once); + ErrorHandlerMock.Verify(e => e.HandleError(It.Is(ex => ex.Message == $"This version of the C# SDK does not support the given datafile version: 5")), Times.Once); } [Test] diff --git a/OptimizelySDK.Tests/ProjectConfigTest.cs b/OptimizelySDK.Tests/ProjectConfigTest.cs index 87370b27..f2b4381e 100644 --- a/OptimizelySDK.Tests/ProjectConfigTest.cs +++ b/OptimizelySDK.Tests/ProjectConfigTest.cs @@ -881,12 +881,8 @@ public void TestCreateThrowsWithEmptyDatafile() [Test] public void TestCreateThrowsWithUnsupportedDatafileVersion() { - var unsupportedConfig = JsonConvert.DeserializeObject(TestData.Datafile); - unsupportedConfig.Version = "5"; - var unsupportedConfigData = JsonConvert.SerializeObject(unsupportedConfig); - - var exception = Assert.Throws(() => ProjectConfig.Create(unsupportedConfigData, null, null)); - Assert.AreEqual($"This version of the C# SDK does not support the given datafile version: {unsupportedConfig.Version}", exception.Message); + var exception = Assert.Throws(() => ProjectConfig.Create(TestData.UnsupportedVersionDatafile, null, null)); + Assert.AreEqual($"This version of the C# SDK does not support the given datafile version: 5", exception.Message); } [Test] diff --git a/OptimizelySDK.Tests/TestData.cs b/OptimizelySDK.Tests/TestData.cs index ab4e6302..0bee0dbf 100644 --- a/OptimizelySDK.Tests/TestData.cs +++ b/OptimizelySDK.Tests/TestData.cs @@ -1,5 +1,5 @@ /* - * Copyright 2017, Optimizely + * Copyright 2017-2018, Optimizely * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ public class TestData { private static string cachedDataFile = null; private static string simpleABExperimentsDatafile = null; + private static string unsupportedVersionDatafile = null; public static string Datafile { @@ -41,7 +42,15 @@ public static string SimpleABExperimentsDatafile return simpleABExperimentsDatafile ?? (simpleABExperimentsDatafile = LoadJsonData("simple_ab_experiments.json")); } } - + + public static string UnsupportedVersionDatafile + { + get + { + return unsupportedVersionDatafile ?? (unsupportedVersionDatafile = LoadJsonData("unsupported_version_datafile.json")); + } + } + private static string LoadJsonData(string fileName = "TestData.json") { var assembly = Assembly.GetExecutingAssembly(); diff --git a/OptimizelySDK/Optimizely.cs b/OptimizelySDK/Optimizely.cs index 5f028ae1..182cf9e3 100644 --- a/OptimizelySDK/Optimizely.cs +++ b/OptimizelySDK/Optimizely.cs @@ -112,14 +112,15 @@ public Optimizely(string datafile, IsValid = true; DecisionService = new DecisionService(Bucketer, ErrorHandler, Config, userProfileService, Logger); } - catch (ConfigParseException configException) - { - Logger.Log(LogLevel.ERROR, configException.Message); - ErrorHandler.HandleError(configException); - } catch (Exception ex) { - Logger.Log(LogLevel.ERROR, "Provided 'datafile' is in an invalid format. " + ex.Message); + string error = String.Empty; + if (ex.GetType() == typeof(ConfigParseException)) + error = ex.Message; + else + error = "Provided 'datafile' is in an invalid format. " + ex.Message; + + Logger.Log(LogLevel.ERROR, error); ErrorHandler.HandleError(ex); } } diff --git a/OptimizelySDK/ProjectConfig.cs b/OptimizelySDK/ProjectConfig.cs index ff6ec53d..c6a5c0db 100644 --- a/OptimizelySDK/ProjectConfig.cs +++ b/OptimizelySDK/ProjectConfig.cs @@ -306,15 +306,12 @@ private static ProjectConfig GetConfig(string configData) var config = JsonConvert.DeserializeObject(configData); - if (SupportedVersions.TrueForAll((obj) => !(((int)obj).ToString() == config.Version))) + if (SupportedVersions.TrueForAll((supportedVersion) => !(((int)supportedVersion).ToString() == config.Version))) throw new ConfigParseException(string.Format(@"This version of the C# SDK does not support the given datafile version: {0}", config.Version)); return config; } - - - //========================= Getters =========================== /// From e6fa4954ed430ce02305dd0be951bd2292d4e7f4 Mon Sep 17 00:00:00 2001 From: mfahadahmed Date: Wed, 5 Sep 2018 17:48:17 +0500 Subject: [PATCH 5/6] Added unsupported version datafile. --- .../unsupported_version_datafile.json | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 OptimizelySDK.Tests/unsupported_version_datafile.json diff --git a/OptimizelySDK.Tests/unsupported_version_datafile.json b/OptimizelySDK.Tests/unsupported_version_datafile.json new file mode 100644 index 00000000..4830b21f --- /dev/null +++ b/OptimizelySDK.Tests/unsupported_version_datafile.json @@ -0,0 +1,42 @@ +{ + "version": "5", + "rollouts": [], + "projectId": "10431130345", + "variables": [], + "featureFlags": [], + "experiments": [{ + "status": "Running", + "key": "ab_running_exp_untargeted", + "layerId": "10417730432", + "trafficAllocation": [{ + "entityId": "10418551353", + "endOfRange": 10000 + }], + "audienceIds": [], + "variations": [{ + "variables": [], + "id": "10418551353", + "key": "all_traffic_variation" + }, + { + "variables": [], + "id": "10418510624", + "key": "no_traffic_variation" + } + ], + "forcedVariations": {}, + "id": "10420810910" + }], + "audiences": [], + "groups": [], + "attributes": [], + "accountId": "10367498574", + "events": [{ + "experimentIds": [ + "10420810910" + ], + "id": "10404198134", + "key": "winning" + }], + "revision": "1337" +} \ No newline at end of file From bd6c391e34263734fa390c59bfb2e1e91ca562dd Mon Sep 17 00:00:00 2001 From: mfahadahmed Date: Thu, 6 Sep 2018 11:53:57 +0500 Subject: [PATCH 6/6] Split unit tests. --- OptimizelySDK.Tests/OptimizelyTest.cs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/OptimizelySDK.Tests/OptimizelyTest.cs b/OptimizelySDK.Tests/OptimizelyTest.cs index b1ad6cff..bab40b22 100644 --- a/OptimizelySDK.Tests/OptimizelyTest.cs +++ b/OptimizelySDK.Tests/OptimizelyTest.cs @@ -190,18 +190,26 @@ public void TestValidateInputsInvalidFileJsonValidationSkipped() } [Test] - public void TestErrorHandlingWithInvalidConfigVersion() + public void TestErrorHandlingWithNullDatafile() { var optimizelyNullDatafile = new Optimizely(null, null, LoggerMock.Object, ErrorHandlerMock.Object, null, true); - var optimizelyEmptyDatafile = new Optimizely("", null, LoggerMock.Object, ErrorHandlerMock.Object, null, true); - var optimizelyUnsupportedVersion = new Optimizely(TestData.UnsupportedVersionDatafile, null, LoggerMock.Object, ErrorHandlerMock.Object, null, true); - LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Unable to parse null datafile."), Times.Once); + ErrorHandlerMock.Verify(e => e.HandleError(It.Is(ex => ex.Message == "Unable to parse null datafile.")), Times.Once); + } + + [Test] + public void TestErrorHandlingWithEmptyDatafile() + { + var optimizelyEmptyDatafile = new Optimizely("", null, LoggerMock.Object, ErrorHandlerMock.Object, null, true); LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Unable to parse empty datafile."), Times.Once); - LoggerMock.Verify(l => l.Log(LogLevel.ERROR, $"This version of the C# SDK does not support the given datafile version: 5"), Times.Once); + ErrorHandlerMock.Verify(e => e.HandleError(It.Is(ex => ex.Message == "Unable to parse empty datafile.")), Times.Once); + } - ErrorHandlerMock.Verify(e => e.HandleError(It.Is(ex => ex.Message == "Unable to parse null datafile.")), Times.Once); - ErrorHandlerMock.Verify(e => e.HandleError(It.Is(ex => ex.Message == "Unable to parse null datafile.")), Times.Once); + [Test] + public void TestErrorHandlingWithUnsupportedConfigVersion() + { + var optimizelyUnsupportedVersion = new Optimizely(TestData.UnsupportedVersionDatafile, null, LoggerMock.Object, ErrorHandlerMock.Object, null, true); + LoggerMock.Verify(l => l.Log(LogLevel.ERROR, $"This version of the C# SDK does not support the given datafile version: 5"), Times.Once); ErrorHandlerMock.Verify(e => e.HandleError(It.Is(ex => ex.Message == $"This version of the C# SDK does not support the given datafile version: 5")), Times.Once); }