Search in sources :

Example 36 with OptimizelyDecision

use of com.optimizely.ab.optimizelydecision.OptimizelyDecision in project java-sdk by optimizely.

the class OptimizelyUserContextTest method decideReasons_userBucketedIntoEveryoneTargetingRule.

@Test
public void decideReasons_userBucketedIntoEveryoneTargetingRule() {
    String flagKey = "feature_1";
    OptimizelyUserContext user = optimizely.createUserContext(userId);
    user.setAttribute("country", "KO");
    OptimizelyDecision decision = user.decide(flagKey, Arrays.asList(OptimizelyDecideOption.INCLUDE_REASONS));
    assertTrue(decision.getReasons().contains(String.format("User \"%s\" meets conditions for targeting rule \"Everyone Else\".", userId)));
}
Also used : OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) Test(org.junit.Test)

Example 37 with OptimizelyDecision

use of com.optimizely.ab.optimizelydecision.OptimizelyDecision in project java-sdk by optimizely.

the class OptimizelyUserContextTest method setForcedDecisionsAndCallDecideFlagToDecision.

/**
 ****************************************[START DECIDE TESTS WITH FDs]*****************************************
 */
@Test
public void setForcedDecisionsAndCallDecideFlagToDecision() {
    String flagKey = "feature_1";
    String variationKey = "a";
    optimizely = new Optimizely.Builder().withDatafile(datafile).withEventProcessor(new ForwardingEventProcessor(eventHandler, null)).build();
    OptimizelyUserContext optimizelyUserContext = new OptimizelyUserContext(optimizely, userId, Collections.emptyMap());
    OptimizelyDecisionContext optimizelyDecisionContext = new OptimizelyDecisionContext(flagKey, null);
    OptimizelyForcedDecision optimizelyForcedDecision = new OptimizelyForcedDecision(variationKey);
    optimizelyUserContext.setForcedDecision(optimizelyDecisionContext, optimizelyForcedDecision);
    assertEquals(variationKey, optimizelyUserContext.getForcedDecision(optimizelyDecisionContext).getVariationKey());
    optimizely.addDecisionNotificationHandler(decisionNotification -> {
        Assert.assertEquals(decisionNotification.getDecisionInfo().get(DECISION_EVENT_DISPATCHED), true);
        isListenerCalled = true;
    });
    isListenerCalled = false;
    // Test to confirm decide uses proper FD
    OptimizelyDecision decision = optimizelyUserContext.decide(flagKey, Arrays.asList(OptimizelyDecideOption.INCLUDE_REASONS));
    assertTrue(isListenerCalled);
    String variationId = "10389729780";
    String experimentId = "";
    DecisionMetadata metadata = new DecisionMetadata.Builder().setFlagKey(flagKey).setRuleKey("").setRuleType("feature-test").setVariationKey(variationKey).setEnabled(true).build();
    eventHandler.expectImpression(experimentId, variationId, userId, Collections.emptyMap(), metadata);
    assertNotNull(decision);
    assertTrue(decision.getReasons().contains(String.format("Variation (%s) is mapped to flag (%s) and user (%s) in the forced decision map.", variationKey, flagKey, userId)));
}
Also used : ForwardingEventProcessor(com.optimizely.ab.event.ForwardingEventProcessor) OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) FlagDecisionNotificationBuilder(com.optimizely.ab.notification.DecisionNotification.FlagDecisionNotificationBuilder) DecisionMetadata(com.optimizely.ab.event.internal.payload.DecisionMetadata) Test(org.junit.Test)

Example 38 with OptimizelyDecision

use of com.optimizely.ab.optimizelydecision.OptimizelyDecision in project java-sdk by optimizely.

the class OptimizelyUserContextTest method setForcedDecisionsAndCallDecideDeliveryRuleToDecision.

@Test
public void setForcedDecisionsAndCallDecideDeliveryRuleToDecision() {
    String flagKey = "feature_1";
    String ruleKey = "3332020515";
    String variationKey = "3324490633";
    optimizely = new Optimizely.Builder().withDatafile(datafile).withEventProcessor(new ForwardingEventProcessor(eventHandler, null)).build();
    OptimizelyUserContext optimizelyUserContext = new OptimizelyUserContext(optimizely, userId, Collections.emptyMap());
    OptimizelyDecisionContext optimizelyDecisionContext = new OptimizelyDecisionContext(flagKey, ruleKey);
    OptimizelyForcedDecision optimizelyForcedDecision = new OptimizelyForcedDecision(variationKey);
    optimizelyUserContext.setForcedDecision(optimizelyDecisionContext, optimizelyForcedDecision);
    assertEquals(variationKey, optimizelyUserContext.getForcedDecision(optimizelyDecisionContext).getVariationKey());
    optimizely.addDecisionNotificationHandler(decisionNotification -> {
        Assert.assertEquals(decisionNotification.getDecisionInfo().get(DECISION_EVENT_DISPATCHED), true);
        isListenerCalled = true;
    });
    isListenerCalled = false;
    // Test to confirm decide uses proper FD
    OptimizelyDecision decision = optimizelyUserContext.decide(flagKey, Arrays.asList(OptimizelyDecideOption.INCLUDE_REASONS));
    assertTrue(isListenerCalled);
    String variationId = "3324490633";
    String experimentId = "3332020515";
    eventHandler.expectImpression(experimentId, variationId, userId, Collections.emptyMap());
    assertNotNull(decision);
    assertTrue(decision.getReasons().contains(String.format("Variation (%s) is mapped to flag (%s), rule (%s) and user (%s) in the forced decision map.", variationKey, flagKey, ruleKey, userId)));
}
Also used : ForwardingEventProcessor(com.optimizely.ab.event.ForwardingEventProcessor) OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) FlagDecisionNotificationBuilder(com.optimizely.ab.notification.DecisionNotification.FlagDecisionNotificationBuilder) Test(org.junit.Test)

Example 39 with OptimizelyDecision

use of com.optimizely.ab.optimizelydecision.OptimizelyDecision in project java-sdk by optimizely.

the class OptimizelyUserContextTest method decideOptions_includeReasons.

@Test
public void decideOptions_includeReasons() {
    OptimizelyUserContext user = optimizely.createUserContext(userId);
    String flagKey = "invalid_key";
    OptimizelyDecision decision = user.decide(flagKey);
    assertEquals(decision.getReasons().size(), 1);
    TestCase.assertEquals(decision.getReasons().get(0), DecisionMessage.FLAG_KEY_INVALID.reason(flagKey));
    decision = user.decide(flagKey, Arrays.asList(OptimizelyDecideOption.INCLUDE_REASONS));
    assertEquals(decision.getReasons().size(), 1);
    assertEquals(decision.getReasons().get(0), DecisionMessage.FLAG_KEY_INVALID.reason(flagKey));
    flagKey = "feature_1";
    decision = user.decide(flagKey);
    assertEquals(decision.getReasons().size(), 0);
    decision = user.decide(flagKey, Arrays.asList(OptimizelyDecideOption.INCLUDE_REASONS));
    assertTrue(decision.getReasons().size() > 0);
}
Also used : OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) Test(org.junit.Test)

Example 40 with OptimizelyDecision

use of com.optimizely.ab.optimizelydecision.OptimizelyDecision in project java-sdk by optimizely.

the class OptimizelyUserContextTest method decideAll_twoFlags.

@Test
public void decideAll_twoFlags() {
    String flagKey1 = "feature_1";
    String flagKey2 = "feature_2";
    List<String> flagKeys = Arrays.asList(flagKey1, flagKey2);
    OptimizelyJSON variablesExpected1 = optimizely.getAllFeatureVariables(flagKey1, userId);
    OptimizelyJSON variablesExpected2 = optimizely.getAllFeatureVariables(flagKey2, userId);
    OptimizelyUserContext user = optimizely.createUserContext(userId, Collections.singletonMap("gender", "f"));
    Map<String, OptimizelyDecision> decisions = user.decideForKeys(flagKeys);
    assertTrue(decisions.size() == 2);
    assertEquals(decisions.get(flagKey1), new OptimizelyDecision("a", true, variablesExpected1, "exp_with_audience", flagKey1, user, Collections.emptyList()));
    assertEquals(decisions.get(flagKey2), new OptimizelyDecision("variation_with_traffic", true, variablesExpected2, "exp_no_audience", flagKey2, user, Collections.emptyList()));
}
Also used : OptimizelyJSON(com.optimizely.ab.optimizelyjson.OptimizelyJSON) OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) Test(org.junit.Test)

Aggregations

OptimizelyDecision (com.optimizely.ab.optimizelydecision.OptimizelyDecision)47 Test (org.junit.Test)46 FlagDecisionNotificationBuilder (com.optimizely.ab.notification.DecisionNotification.FlagDecisionNotificationBuilder)12 ForwardingEventProcessor (com.optimizely.ab.event.ForwardingEventProcessor)10 OptimizelyJSON (com.optimizely.ab.optimizelyjson.OptimizelyJSON)9 DecisionMetadata (com.optimizely.ab.event.internal.payload.DecisionMetadata)5 UserProfileService (com.optimizely.ab.bucketing.UserProfileService)2 OptimizelyUserContext (com.optimizely.ab.OptimizelyUserContext)1 OptimizelyDecideOption (com.optimizely.ab.optimizelydecision.OptimizelyDecideOption)1