예제 #1
0
        public void TestGettingErrorUponInvalidJsonString()
        {
            var optimizelyJSONUsingString = new OptimizelyJSON("{\"invalid\":}", ErrorHandlerMock.Object, LoggerMock.Object);

            LoggerMock.Verify(log => log.Log(LogLevel.ERROR, "Provided string could not be converted to map."), Times.Once);
            ErrorHandlerMock.Verify(er => er.HandleError(It.IsAny <InvalidJsonException>()), Times.Once);
        }
예제 #2
0
        public void TestGetValueReturnsNullWhenJsonPathIsEmptyAndTypeIsOfObject()
        {
            var optimizelyJSONUsingString = new OptimizelyJSON(Payload, ErrorHandlerMock.Object, LoggerMock.Object);
            var expectedValue             = optimizelyJSONUsingString.GetValue <object>("");

            Assert.NotNull(expectedValue);
        }
        public void TestNewDecision()
        {
            var variableMap = new Dictionary <string, object>()
            {
                { "strField", "john doe" },
                { "intField", 12 },
                { "objectField", new Dictionary <string, object> ()
                  {
                      { "inner_field_int", 3 }
                  } }
            };
            var    optimizelyJSONUsingMap = new OptimizelyJSON(variableMap, ErrorHandlerMock.Object, LoggerMock.Object);
            string expectedStringObj      = "{\"strField\":\"john doe\",\"intField\":12,\"objectField\":{\"inner_field_int\":3}}";

            var optimizelyDecision = new OptimizelyDecision("var_key",
                                                            true,
                                                            optimizelyJSONUsingMap,
                                                            "experiment",
                                                            "feature_key",
                                                            null,
                                                            new string[0]);

            Assert.AreEqual(optimizelyDecision.VariationKey, "var_key");
            Assert.AreEqual(optimizelyDecision.FlagKey, "feature_key");
            Assert.AreEqual(optimizelyDecision.Variables.ToString(), expectedStringObj);
            Assert.AreEqual(optimizelyDecision.Reasons, new List <string>());
            Assert.AreEqual(optimizelyDecision.RuleKey, "experiment");
            Assert.True(optimizelyDecision.Enabled);
        }
예제 #4
0
        public void TestOptimizelyJsonObjectIsValid()
        {
            var optimizelyJSONUsingMap    = new OptimizelyJSON(Map, ErrorHandlerMock.Object, LoggerMock.Object);
            var optimizelyJSONUsingString = new OptimizelyJSON(Payload, ErrorHandlerMock.Object, LoggerMock.Object);

            Assert.IsNotNull(optimizelyJSONUsingMap);
            Assert.IsNotNull(optimizelyJSONUsingString);
        }
예제 #5
0
        public void TestGetValueReturnsDefaultValueWhenTypeIsInvalid()
        {
            var payload = "{ \"field1\" : {\"1\":\"Csharp\",\"2\":\"Java\"} }";
            var optimizelyJSONUsingString = new OptimizelyJSON(payload, ErrorHandlerMock.Object, LoggerMock.Object);
            var expectedValue             = optimizelyJSONUsingString.GetValue <Dictionary <float, string> >("field1");

            Assert.IsNotNull(expectedValue);
        }
예제 #6
0
        public void TestGetValueReturnsCastedObject()
        {
            var optimizelyJson = new OptimizelyJSON(Map, ErrorHandlerMock.Object, LoggerMock.Object);
            var expectedValue  = optimizelyJson.ToDictionary();
            var actualValue    = optimizelyJson.GetValue <ParentJson>(null);

            Assert.IsTrue(TestData.CompareObjects(actualValue, expectedValue));
        }
예제 #7
0
        public void TestGetValueObjectNotModifiedIfCalledTwice()
        {
            var optimizelyJSONUsingString = new OptimizelyJSON(Payload, ErrorHandlerMock.Object, LoggerMock.Object);
            var expectedValue             = optimizelyJSONUsingString.GetValue <string>("field4.inner_field1");
            var expectedValue2            = optimizelyJSONUsingString.GetValue <string>("field4.inner_field1");

            Assert.AreEqual(expectedValue, expectedValue2);
        }
예제 #8
0
        public void TestGetValueReturnsDefaultValueWhenJsonPath2IsInvalid()
        {
            var optimizelyJSONUsingString = new OptimizelyJSON(Payload, ErrorHandlerMock.Object, LoggerMock.Object);
            var expectedValue             = optimizelyJSONUsingString.GetValue <string>("field4..inner_field1");

            Assert.IsNull(expectedValue);
            LoggerMock.Verify(log => log.Log(LogLevel.ERROR, "Value for JSON key not found."), Times.Once);
            ErrorHandlerMock.Verify(er => er.HandleError(It.IsAny <OptimizelyRuntimeException>()), Times.Once);
        }
예제 #9
0
        public void TestGetValueReturnsDefaultValueWhenJsonPathIsEmptyAndTypeIsNotValid()
        {
            var optimizelyJSONUsingString = new OptimizelyJSON(Payload, ErrorHandlerMock.Object, LoggerMock.Object);
            var expectedValue             = optimizelyJSONUsingString.GetValue <string>("");

            Assert.IsNull(expectedValue);
            LoggerMock.Verify(log => log.Log(LogLevel.ERROR, "Value for path could not be assigned to provided type."), Times.Once);
            ErrorHandlerMock.Verify(er => er.HandleError(It.IsAny <OptimizelyRuntimeException>()), Times.Once);
        }
예제 #10
0
        public void TestGetValueReturnsDefaultValueWhenJsonIsInvalid()
        {
            var payload = "{ \"field1\" : {1:\"Csharp\", 2:\"Java\"} }";
            var optimizelyJSONUsingString = new OptimizelyJSON(payload, ErrorHandlerMock.Object, LoggerMock.Object);
            var expectedValue             = optimizelyJSONUsingString.GetValue <Dictionary <float, string> >("field1");

            // Even though above given JSON is not valid, newtonsoft is parsing it so
            Assert.IsNotNull(expectedValue);
        }
예제 #11
0
        public void TestGetValueReturnsEntireDictWhenJsonPathIsEmptyAndTypeIsValid()
        {
            var optimizelyJSONUsingString = new OptimizelyJSON(Payload, ErrorHandlerMock.Object, LoggerMock.Object);
            var actualDict    = optimizelyJSONUsingString.ToDictionary();
            var expectedValue = optimizelyJSONUsingString.GetValue <Dictionary <string, object> >("");

            Assert.NotNull(expectedValue);
            Assert.True(TestData.CompareObjects(expectedValue, actualDict));
        }
예제 #12
0
        public void TestGetValueReturnsUsingGivenClassType()
        {
            var optimizelyJSONUsingString = new OptimizelyJSON(Payload, ErrorHandlerMock.Object, LoggerMock.Object);
            var expectedValue             = optimizelyJSONUsingString.GetValue <Field4>("field4");

            Assert.AreEqual(expectedValue.inner_field1, 3);
            Assert.AreEqual(expectedValue.inner_field2, new List <object>()
            {
                "1", "2", 3, 4.23, true
            });
        }
예제 #13
0
        public void TestOptimizelyJsonGetVariablesWhenSetUsingString()
        {
            var optimizelyJSONUsingString = new OptimizelyJSON(Payload, ErrorHandlerMock.Object, LoggerMock.Object);

            Assert.AreEqual(optimizelyJSONUsingString.GetValue <long>("field1"), 1);
            Assert.AreEqual(optimizelyJSONUsingString.GetValue <double>("field2"), 2.5);
            Assert.AreEqual(optimizelyJSONUsingString.GetValue <string>("field3"), "three");
            Assert.AreEqual(optimizelyJSONUsingString.GetValue <long>("field4.inner_field1"), 3);
            Assert.True(TestData.CompareObjects(optimizelyJSONUsingString.GetValue <List <object> >("field4.inner_field2"), new List <object>()
            {
                "1", "2", 3, 4.23, true
            }));
        }
예제 #14
0
        public void TestOptimizelyJsonGetVariablesWhenSetUsingMap()
        {
            var optimizelyJSONUsingMap = new OptimizelyJSON(Map, ErrorHandlerMock.Object, LoggerMock.Object);

            Assert.AreEqual(optimizelyJSONUsingMap.GetValue <string>("strField"), "john doe");
            Assert.AreEqual(optimizelyJSONUsingMap.GetValue <int>("intField"), 12);
            Assert.AreEqual(optimizelyJSONUsingMap.GetValue <double>("doubleField"), 2.23);
            Assert.AreEqual(optimizelyJSONUsingMap.GetValue <bool>("boolField"), true);
            Assert.AreEqual(optimizelyJSONUsingMap.GetValue <int>("objectField.inner_field_int"), 3);
            Assert.AreEqual(optimizelyJSONUsingMap.GetValue <double>("objectField.inner_field_double"), 13.21);
            Assert.AreEqual(optimizelyJSONUsingMap.GetValue <string>("objectField.inner_field_string"), "john");
            Assert.AreEqual(optimizelyJSONUsingMap.GetValue <bool>("objectField.inner_field_boolean"), true);
            Assert.IsTrue(optimizelyJSONUsingMap.GetValue <Dictionary <string, object> >("objectField") is Dictionary <string, object>);
        }
예제 #15
0
 public OptimizelyDecision(string variationKey,
                           bool enabled,
                           OptimizelyJSON variables,
                           string ruleKey,
                           string flagKey,
                           OptimizelyUserContext userContext,
                           string[] reasons)
 {
     VariationKey = variationKey;
     Enabled      = enabled;
     Variables    = variables;
     RuleKey      = ruleKey;
     FlagKey      = flagKey;
     UserContext  = userContext;
     Reasons      = reasons;
 }
예제 #16
0
        public void TestToStringReturnValidString()
        {
            var map = new Dictionary <string, object>()
            {
                { "strField", "john doe" },
                { "intField", 12 },
                { "objectField", new Dictionary <string, object> ()
                  {
                      { "inner_field_int", 3 }
                  } }
            };
            var    optimizelyJSONUsingMap = new OptimizelyJSON(map, ErrorHandlerMock.Object, LoggerMock.Object);
            string str = optimizelyJSONUsingMap.ToString();
            string expectedStringObj = "{\"strField\":\"john doe\",\"intField\":12,\"objectField\":{\"inner_field_int\":3}}";

            Assert.AreEqual(expectedStringObj, str);
        }
예제 #17
0
 public static void AreEqual(OptimizelyJSON expected, OptimizelyJSON actual)
 {
     Assert.AreEqual(expected.ToString(), actual.ToString());
 }