Search in sources :

Example 41 with OptimizelyDecision

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

the class OptimizelyUserContextTest method decideReasons_forcedVariationFound.

@Test
public void decideReasons_forcedVariationFound() {
    String flagKey = "feature_1";
    String variationKey = "b";
    Experiment experiment = getSpyExperiment(flagKey);
    when(experiment.getUserIdToVariationKeyMap()).thenReturn(Collections.singletonMap(userId, variationKey));
    addSpyExperiment(experiment);
    OptimizelyDecision decision = callDecideWithIncludeReasons(flagKey);
    assertTrue(decision.getReasons().contains(String.format("User \"%s\" is forced in variation \"%s\".", userId, variationKey)));
}
Also used : OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) Test(org.junit.Test)

Example 42 with OptimizelyDecision

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

the class OptimizelyUserContextTest method decideOptions_bypassUPS.

// options
@Test
public void decideOptions_bypassUPS() throws Exception {
    // embedding experiment: "exp_no_audience"
    String flagKey = "feature_2";
    // "exp_no_audience"
    String experimentId = "10420810910";
    String variationId1 = "10418551353";
    String variationId2 = "10418510624";
    String variationKey1 = "variation_with_traffic";
    String variationKey2 = "variation_no_traffic";
    UserProfileService ups = mock(UserProfileService.class);
    when(ups.lookup(userId)).thenReturn(createUserProfileMap(experimentId, variationId2));
    optimizely = new Optimizely.Builder().withDatafile(datafile).withUserProfileService(ups).build();
    OptimizelyUserContext user = optimizely.createUserContext(userId);
    OptimizelyDecision decision = user.decide(flagKey);
    // should return variationId2 set by UPS
    assertEquals(decision.getVariationKey(), variationKey2);
    decision = user.decide(flagKey, Arrays.asList(OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICE));
    // should ignore variationId2 set by UPS and return variationId1
    assertEquals(decision.getVariationKey(), variationKey1);
    // also should not save either
    verify(ups, never()).save(anyObject());
}
Also used : OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) FlagDecisionNotificationBuilder(com.optimizely.ab.notification.DecisionNotification.FlagDecisionNotificationBuilder) UserProfileService(com.optimizely.ab.bucketing.UserProfileService) Test(org.junit.Test)

Example 43 with OptimizelyDecision

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

the class OptimizelyUserContextTest method decideReasons_missingAttributeValue.

@Test
public void decideReasons_missingAttributeValue() {
    String flagKey = "feature_1";
    String audienceId = "age_18";
    Experiment experiment = getSpyExperiment(flagKey);
    when(experiment.getAudienceIds()).thenReturn(Arrays.asList(audienceId));
    addSpyExperiment(experiment);
    OptimizelyDecision decision = callDecideWithIncludeReasons(flagKey);
    assertTrue(decision.getReasons().contains(String.format("Audiences for experiment \"%s\" collectively evaluated to null.", experiment.getKey())));
}
Also used : OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) Test(org.junit.Test)

Example 44 with OptimizelyDecision

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

the class OptimizelyUserContextTest method setForcedDecisionsAndCallDecide.

@Test
public void setForcedDecisionsAndCallDecide() {
    String flagKey = "feature_2";
    String ruleKey = "exp_no_audience";
    String variationKey = "variation_with_traffic";
    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());
    // Test to confirm decide uses proper FD
    OptimizelyDecision decision = optimizelyUserContext.decide(flagKey, Arrays.asList(OptimizelyDecideOption.INCLUDE_REASONS));
    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 : OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) Test(org.junit.Test)

Example 45 with OptimizelyDecision

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

the class OptimizelyUserContextTest method decideReasons_sdkNotReady.

// reasons (errors)
@Test
public void decideReasons_sdkNotReady() {
    String flagKey = "feature_1";
    Optimizely optimizely = new Optimizely.Builder().build();
    OptimizelyUserContext user = optimizely.createUserContext(userId);
    OptimizelyDecision decision = user.decide(flagKey);
    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)

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