public void TestDefaultDecideOptions()
        {
            var flagKey           = "multi_variate_feature";
            var variablesExpected = Optimizely.GetAllFeatureVariables(flagKey, UserID);
            var decideOptions     = new OptimizelyDecideOption[] { OptimizelyDecideOption.DISABLE_DECISION_EVENT };

            var optimizely = new Optimizely(TestData.Datafile,
                                            EventDispatcherMock.Object,
                                            LoggerMock.Object,
                                            ErrorHandlerMock.Object,
                                            defaultDecideOptions: decideOptions);

            var user = optimizely.CreateUserContext(UserID);

            user.SetAttribute("browser_type", "chrome");

            var decision = user.Decide(flagKey);

            EventDispatcherMock.Verify(dispatcher => dispatcher.DispatchEvent(It.IsAny <LogEvent>()), Times.Never);

            Assert.AreEqual(decision.VariationKey, "Gred");
            Assert.False(decision.Enabled);
            Assert.AreEqual(decision.Variables.ToDictionary(), variablesExpected.ToDictionary());
            Assert.AreEqual(decision.RuleKey, "test_experiment_multivariate");
            Assert.AreEqual(decision.FlagKey, flagKey);
            Assert.AreNotEqual(decision.UserContext, user);
            Assert.IsTrue(TestData.CompareObjects(decision.UserContext, user));
        }
        public void TestDecideOptionsByPassUPS()
        {
            var userProfileServiceMock = new Mock <UserProfileService>();
            var flagKey = "string_single_variable_feature";

            var experimentId   = "122235";
            var userId         = "testUser3";
            var variationKey   = "control";
            var fbVariationId  = "122237";
            var fbVariationKey = "variation";


            var userProfile = new UserProfile(userId, new Dictionary <string, Decision>
            {
                { experimentId, new Decision(fbVariationId) }
            });

            userProfileServiceMock.Setup(_ => _.Lookup(userId)).Returns(userProfile.ToMap());

            var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object, LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);

            var user = optimizely.CreateUserContext(userId);

            var projectConfig = DatafileProjectConfig.Create(TestData.Datafile, LoggerMock.Object, ErrorHandlerMock.Object);

            var variationUserProfile = user.Decide(flagKey);

            Assert.AreEqual(fbVariationKey, variationUserProfile.VariationKey);

            var decideOptions = new OptimizelyDecideOption[] { OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICE };

            variationUserProfile = user.Decide(flagKey, decideOptions);
            Assert.AreEqual(variationKey, variationUserProfile.VariationKey);
        }
        public void DecideAllEnabledFlagsDefaultDecideOptionsPlusApiOptions()
        {
            var flagKey1      = "string_single_variable_feature";
            var decideOptions = new OptimizelyDecideOption[] { OptimizelyDecideOption.ENABLED_FLAGS_ONLY };

            var optimizely = new Optimizely(TestData.Datafile,
                                            EventDispatcherMock.Object,
                                            LoggerMock.Object,
                                            ErrorHandlerMock.Object,
                                            defaultDecideOptions: decideOptions);

            var user = optimizely.CreateUserContext(UserID);

            user.SetAttribute("browser_type", "chrome");
            decideOptions = new OptimizelyDecideOption[] { OptimizelyDecideOption.EXCLUDE_VARIABLES };

            var decisions = user.DecideAll(decideOptions);

            Assert.True(decisions.Count == 1);
            var expectedOptlyJson           = new Dictionary <string, object>();
            OptimizelyDecision expDecision1 = new OptimizelyDecision(
                "control",
                true,
                new OptimizelyJSON(expectedOptlyJson, ErrorHandlerMock.Object, LoggerMock.Object),
                "test_experiment_with_feature_rollout",
                flagKey1,
                user,
                new string[] { });

            Assert.IsTrue(TestData.CompareObjects(decisions[flagKey1], expDecision1));
        }
        public void DecideAllEnabledFlagsDefaultDecideOptions()
        {
            var flagKey1      = "string_single_variable_feature";
            var decideOptions = new OptimizelyDecideOption[] { OptimizelyDecideOption.ENABLED_FLAGS_ONLY };

            var optimizely = new Optimizely(TestData.Datafile,
                                            EventDispatcherMock.Object,
                                            LoggerMock.Object,
                                            ErrorHandlerMock.Object,
                                            defaultDecideOptions: decideOptions);

            var variablesExpected1 = Optimizely.GetAllFeatureVariables(flagKey1, UserID);

            var user = optimizely.CreateUserContext(UserID);

            user.SetAttribute("browser_type", "chrome");

            var decisions = user.DecideAll();

            Assert.True(decisions.Count == 1);

            OptimizelyDecision expDecision1 = new OptimizelyDecision(
                "control",
                true,
                variablesExpected1,
                "test_experiment_with_feature_rollout",
                flagKey1,
                user,
                new string[0]);

            Assert.IsTrue(TestData.CompareObjects(decisions[flagKey1], expDecision1));
        }
        public void TestGetForcedDecisionReturnsNullIfInvalidConfig()
        {
            var optly = new Optimizely(new FallbackProjectConfigManager(null));

            var user = optly.CreateUserContext(UserID);

            var context  = new OptimizelyDecisionContext("flag", null);
            var decision = new OptimizelyForcedDecision("variationKey");
            var result   = user.GetForcedDecision(context);

            Assert.IsNull(result);
        }
        public void TestDecide()
        {
            var flagKey           = "multi_variate_feature";
            var variablesExpected = Optimizely.GetAllFeatureVariables(flagKey, UserID);

            var user = Optimizely.CreateUserContext(UserID);

            user.SetAttribute("browser_type", "chrome");
            var decision = user.Decide(flagKey);

            Assert.AreEqual(decision.VariationKey, "Gred");
            Assert.False(decision.Enabled);
            Assert.AreEqual(decision.Variables.ToDictionary(), variablesExpected.ToDictionary());
            Assert.AreEqual(decision.RuleKey, "test_experiment_multivariate");
            Assert.AreEqual(decision.FlagKey, flagKey);
            Assert.AreNotEqual(decision.UserContext, user);
            Assert.IsTrue(TestData.CompareObjects(decision.UserContext, user));
            Assert.AreEqual(decision.Reasons.Length, 0);
        }
        public void TrackEventEmptyAttributesWithEventTags()
        {
            var OptimizelyWithTypedAudiences = new Optimizely(TestData.TypedAudienceDatafile, EventDispatcherMock.Object, LoggerMock.Object, ErrorHandlerMock.Object);

            var user = OptimizelyWithTypedAudiences.CreateUserContext(UserID);

            // Should be excluded as exact match boolean audience with id '3468206643' does not match so the overall conditions fail.
            user.TrackEvent("user_signed_up", new EventTags
            {
                { "revenue", 42 },
                { "wont_send_null", null }
            });

            EventDispatcherMock.Verify(dispatcher => dispatcher.DispatchEvent(It.IsAny <LogEvent>()), Times.Once);
        }
        public void TestTrackEventWithAudienceConditions()
        {
            var OptimizelyWithTypedAudiences = new Optimizely(TestData.TypedAudienceDatafile, EventDispatcherMock.Object, LoggerMock.Object, ErrorHandlerMock.Object);
            var userAttributes = new UserAttributes
            {
                { "house", "Gryffindor" },
                { "should_do_it", false }
            };

            var user = OptimizelyWithTypedAudiences.CreateUserContext(UserID, userAttributes);

            // Should be excluded as exact match boolean audience with id '3468206643' does not match so the overall conditions fail.
            user.TrackEvent("user_signed_up");

            EventDispatcherMock.Verify(dispatcher => dispatcher.DispatchEvent(It.IsAny <LogEvent>()), Times.Once);
        }
        public void DecideWhenConfigIsNull()
        {
            Optimizely optimizely = new Optimizely(TestData.UnsupportedVersionDatafile, EventDispatcherMock.Object, LoggerMock.Object, ErrorHandlerMock.Object);

            var flagKey          = "multi_variate_feature";
            var decisionExpected = OptimizelyDecision.NewErrorDecision(
                flagKey,
                new OptimizelyUserContext(optimizely, UserID, new UserAttributes(), ErrorHandlerMock.Object, LoggerMock.Object),
                DecisionMessage.SDK_NOT_READY,
                ErrorHandlerMock.Object,
                LoggerMock.Object);
            var user     = optimizely.CreateUserContext(UserID);
            var decision = user.Decide(flagKey);

            Assert.IsTrue(TestData.CompareObjects(decision, decisionExpected));
        }
        public void TestDecideOptionsByPassUPSNeverCallsSaveVariation()
        {
            var userProfileServiceMock = new Mock <UserProfileService>();
            var flagKey = "string_single_variable_feature";

            var userId       = "testUser3";
            var variationKey = "control";

            var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object, LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);
            var user       = optimizely.CreateUserContext(userId);

            var decideOptions        = new OptimizelyDecideOption[] { OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICE };
            var variationUserProfile = user.Decide(flagKey, decideOptions);

            userProfileServiceMock.Verify(l => l.Save(It.IsAny <Dictionary <string, object> >()), Times.Never);

            Assert.AreEqual(variationKey, variationUserProfile.VariationKey);
        }