Search in sources :

Example 11 with OptimizelyDecision

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

the class OptimizelyUserContextTest method setForcedDecisionsAndCallDecideExperimentRuleToDecision.

@Test
public void setForcedDecisionsAndCallDecideExperimentRuleToDecision() {
    String flagKey = "feature_1";
    String ruleKey = "exp_with_audience";
    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, 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 = "10389729780";
    String experimentId = "10390977673";
    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 12 with OptimizelyDecision

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

the class OptimizelyUserContextTest method decideReasons_userNotBucketedIntoTargetingRule.

@Test
public void decideReasons_userNotBucketedIntoTargetingRule() {
    String flagKey = "feature_1";
    // experimentKey of rollout[2]
    String experimentKey = "3332020494";
    OptimizelyUserContext user = optimizely.createUserContext(userId);
    user.setAttribute("browser", "safari");
    OptimizelyDecision decision = user.decide(flagKey, Arrays.asList(OptimizelyDecideOption.INCLUDE_REASONS));
    assertTrue(decision.getReasons().contains(String.format("User with bucketingId \"%s\" is not in any variation of experiment \"%s\".", userId, experimentKey)));
}
Also used : OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) Test(org.junit.Test)

Example 13 with OptimizelyDecision

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

the class OptimizelyUserContextTest method decide_sdkNotReady.

// errors
@Test
public void decide_sdkNotReady() {
    String flagKey = "feature_1";
    Optimizely optimizely = new Optimizely.Builder().build();
    OptimizelyUserContext user = optimizely.createUserContext(userId);
    OptimizelyDecision decision = user.decide(flagKey);
    assertNull(decision.getVariationKey());
    assertFalse(decision.getEnabled());
    assertTrue(decision.getVariables().isEmpty());
    assertEquals(decision.getFlagKey(), flagKey);
    assertEquals(decision.getUserContext(), user);
    assertEquals(decision.getReasons().size(), 1);
    assertEquals(decision.getReasons().get(0), DecisionMessage.SDK_NOT_READY.reason());
}
Also used : OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) Test(org.junit.Test)

Example 14 with OptimizelyDecision

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

the class OptimizelyUserContextTest method decideReasons_userBucketedIntoTargetingRule.

@Test
public void decideReasons_userBucketedIntoTargetingRule() {
    String flagKey = "feature_1";
    OptimizelyUserContext user = optimizely.createUserContext(userId);
    user.setAttribute("country", "US");
    OptimizelyDecision decision = user.decide(flagKey, Arrays.asList(OptimizelyDecideOption.INCLUDE_REASONS));
    assertTrue(decision.getReasons().contains(String.format("The user \"%s\" was bucketed into a rollout for feature flag \"%s\".", userId, flagKey)));
}
Also used : OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) Test(org.junit.Test)

Example 15 with OptimizelyDecision

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

the class OptimizelyUserContextTest method decide_sendEvent.

// send events
@Test
public void decide_sendEvent() {
    optimizely = new Optimizely.Builder().withDatafile(datafile).withEventProcessor(new ForwardingEventProcessor(eventHandler, null)).build();
    String flagKey = "feature_2";
    String variationKey = "variation_with_traffic";
    String experimentId = "10420810910";
    String variationId = "10418551353";
    OptimizelyUserContext user = optimizely.createUserContext(userId);
    OptimizelyDecision decision = user.decide(flagKey);
    assertEquals(decision.getVariationKey(), variationKey);
    eventHandler.expectImpression(experimentId, variationId, userId, Collections.emptyMap());
}
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)

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