|
| 1 | +/* |
| 2 | + * Copyright 2020, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +using Moq; |
| 18 | +using NUnit.Framework; |
| 19 | +using OptimizelySDK.ErrorHandler; |
| 20 | +using OptimizelySDK.Logger; |
| 21 | +using OptimizelySDK.OptimizelyDecisions; |
| 22 | +using System; |
| 23 | +using System.Collections.Generic; |
| 24 | + |
| 25 | +namespace OptimizelySDK.Tests.OptimizelyDecisions |
| 26 | +{ |
| 27 | + [TestFixture] |
| 28 | + public class OptimizelyDecisionTest |
| 29 | + { |
| 30 | + private Mock<ILogger> LoggerMock; |
| 31 | + private Mock<IErrorHandler> ErrorHandlerMock; |
| 32 | + |
| 33 | + [SetUp] |
| 34 | + public void Initialize() |
| 35 | + { |
| 36 | + ErrorHandlerMock = new Mock<IErrorHandler>(); |
| 37 | + ErrorHandlerMock.Setup(e => e.HandleError(It.IsAny<Exception>())); |
| 38 | + |
| 39 | + LoggerMock = new Mock<ILogger>(); |
| 40 | + LoggerMock.Setup(i => i.Log(It.IsAny<LogLevel>(), It.IsAny<string>())); |
| 41 | + } |
| 42 | + |
| 43 | + [Test] |
| 44 | + public void TestNewErrorDecision() |
| 45 | + { |
| 46 | + var optimizelyDecision = OptimizelyDecision.NewErrorDecision("var_key", null, "some error message", ErrorHandlerMock.Object, LoggerMock.Object); |
| 47 | + Assert.IsNull(optimizelyDecision.VariationKey); |
| 48 | + Assert.AreEqual(optimizelyDecision.FlagKey, "var_key"); |
| 49 | + Assert.AreEqual(optimizelyDecision.Variables.ToDictionary(), new Dictionary<string, object>()); |
| 50 | + Assert.AreEqual(optimizelyDecision.Reasons, new List<string>() { "some error message" }); |
| 51 | + Assert.IsNull(optimizelyDecision.RuleKey); |
| 52 | + Assert.False(optimizelyDecision.Enabled); |
| 53 | + } |
| 54 | + |
| 55 | + [Test] |
| 56 | + public void TestNewDecision() |
| 57 | + { |
| 58 | + var variableMap = new Dictionary<string, object>() { |
| 59 | + { "strField", "john doe" }, |
| 60 | + { "intField", 12 }, |
| 61 | + { "objectField", new Dictionary<string, object> () { |
| 62 | + { "inner_field_int", 3 } |
| 63 | + } |
| 64 | + } |
| 65 | + }; |
| 66 | + var optimizelyJSONUsingMap = new OptimizelyJSON(variableMap, ErrorHandlerMock.Object, LoggerMock.Object); |
| 67 | + string expectedStringObj = "{\"strField\":\"john doe\",\"intField\":12,\"objectField\":{\"inner_field_int\":3}}"; |
| 68 | + |
| 69 | + var optimizelyDecision = new OptimizelyDecision("var_key", |
| 70 | + true, |
| 71 | + optimizelyJSONUsingMap, |
| 72 | + "experiment", |
| 73 | + "feature_key", |
| 74 | + null, |
| 75 | + new string[0]); |
| 76 | + Assert.AreEqual(optimizelyDecision.VariationKey, "var_key"); |
| 77 | + Assert.AreEqual(optimizelyDecision.FlagKey, "feature_key"); |
| 78 | + Assert.AreEqual(optimizelyDecision.Variables.ToString(), expectedStringObj); |
| 79 | + Assert.AreEqual(optimizelyDecision.Reasons, new List<string>()); |
| 80 | + Assert.AreEqual(optimizelyDecision.RuleKey, "experiment"); |
| 81 | + Assert.True(optimizelyDecision.Enabled); |
| 82 | + } |
| 83 | + |
| 84 | + [Test] |
| 85 | + public void TestNewDecisionReasonWithIncludeReasons() |
| 86 | + { |
| 87 | + var decisionReasons = DefaultDecisionReasons.NewInstance(new List<OptimizelyDecideOption>() { OptimizelyDecideOption.INCLUDE_REASONS }); |
| 88 | + decisionReasons.AddError(DecisionMessage.Reason(DecisionMessage.FLAG_KEY_INVALID, "invalid_key")); |
| 89 | + |
| 90 | + Assert.AreEqual(decisionReasons.ToReport()[0], "No flag was found for key \"invalid_key\"."); |
| 91 | + decisionReasons.AddError(DecisionMessage.Reason(DecisionMessage.VARIABLE_VALUE_INVALID, "invalid_key")); |
| 92 | + Assert.AreEqual(decisionReasons.ToReport()[1], "Variable value for key \"invalid_key\" is invalid or wrong type."); |
| 93 | + decisionReasons.AddInfo("Some info message."); |
| 94 | + Assert.AreEqual(decisionReasons.ToReport()[2], "Some info message."); |
| 95 | + } |
| 96 | + |
| 97 | + [Test] |
| 98 | + public void TestNewDecisionReasonWithoutIncludeReasons() |
| 99 | + { |
| 100 | + var decisionReasons = DefaultDecisionReasons.NewInstance(); |
| 101 | + decisionReasons.AddError(DecisionMessage.Reason(DecisionMessage.FLAG_KEY_INVALID, "invalid_key")); |
| 102 | + |
| 103 | + Assert.AreEqual(decisionReasons.ToReport()[0], "No flag was found for key \"invalid_key\"."); |
| 104 | + decisionReasons.AddError(DecisionMessage.Reason(DecisionMessage.VARIABLE_VALUE_INVALID, "invalid_key")); |
| 105 | + Assert.AreEqual(decisionReasons.ToReport()[1], "Variable value for key \"invalid_key\" is invalid or wrong type."); |
| 106 | + decisionReasons.AddInfo("Some info message."); |
| 107 | + Assert.AreEqual(decisionReasons.ToReport().Count, 2); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments