Search in sources :

Example 26 with OptimizelyDecision

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

the class OptimizelyUserContextTest method decide_invalidFeatureKey.

@Test
public void decide_invalidFeatureKey() {
    String flagKey = "invalid_key";
    OptimizelyUserContext user = optimizely.createUserContext(userId);
    OptimizelyDecision decision = user.decide(flagKey);
    assertNull(decision.getVariationKey());
    assertFalse(decision.getEnabled());
    assertTrue(decision.getVariables().isEmpty());
    assertEquals(decision.getReasons().size(), 1);
    assertEquals(decision.getReasons().get(0), DecisionMessage.FLAG_KEY_INVALID.reason(flagKey));
}
Also used : OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) Test(org.junit.Test)

Example 27 with OptimizelyDecision

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

the class OptimizelyUserContextTest method decideReasons_userNotBucketedIntoAnyExperimentInGroup.

@Test
public void decideReasons_userNotBucketedIntoAnyExperimentInGroup() {
    String flagKey = "feature_3";
    // "group_exp_1"
    String experimentId = "10390965532";
    String groupId = "13142870430";
    FeatureFlag flag = getSpyFeatureFlag(flagKey);
    when(flag.getExperimentIds()).thenReturn(Arrays.asList(experimentId));
    addSpyFeatureFlag(flag);
    Group group = getSpyGroup(groupId);
    when(group.getTrafficAllocation()).thenReturn(Collections.emptyList());
    addSpyGroup(group);
    OptimizelyDecision decision = callDecideWithIncludeReasons(flagKey);
    assertTrue(decision.getReasons().contains(String.format("User with bucketingId \"tester\" is not in any experiment of group 13142870430.")));
}
Also used : OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) Test(org.junit.Test)

Example 28 with OptimizelyDecision

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

the class OptimizelyUserContextTest method setForcedDecisionsWithRuleKeyTest.

@Test
public void setForcedDecisionsWithRuleKeyTest() {
    String flagKey = "feature_2";
    String ruleKey = "exp_no_audience";
    String ruleKey2 = "88888";
    String variationKey = "33333";
    String variationKey2 = "variation_with_traffic";
    OptimizelyUserContext optimizelyUserContext = new OptimizelyUserContext(optimizely, userId, Collections.emptyMap());
    OptimizelyDecisionContext optimizelyDecisionContext = new OptimizelyDecisionContext(flagKey, ruleKey);
    OptimizelyForcedDecision optimizelyForcedDecision = new OptimizelyForcedDecision(variationKey);
    OptimizelyDecisionContext optimizelyDecisionContext2 = new OptimizelyDecisionContext(flagKey, ruleKey2);
    OptimizelyForcedDecision optimizelyForcedDecision2 = new OptimizelyForcedDecision(variationKey2);
    optimizelyUserContext.setForcedDecision(optimizelyDecisionContext, optimizelyForcedDecision);
    optimizelyUserContext.setForcedDecision(optimizelyDecisionContext2, optimizelyForcedDecision2);
    assertEquals(variationKey, optimizelyUserContext.getForcedDecision(optimizelyDecisionContext).getVariationKey());
    assertEquals(variationKey2, optimizelyUserContext.getForcedDecision(optimizelyDecisionContext2).getVariationKey());
    // Update first forcedDecision
    optimizelyUserContext.setForcedDecision(optimizelyDecisionContext, optimizelyForcedDecision2);
    assertEquals(variationKey2, optimizelyUserContext.getForcedDecision(optimizelyDecisionContext).getVariationKey());
    // Test to confirm decide uses proper FD
    OptimizelyDecision decision = optimizelyUserContext.decide(flagKey, Arrays.asList(OptimizelyDecideOption.INCLUDE_REASONS));
    assertTrue(decision.getReasons().contains(String.format("Variation (%s) is mapped to flag (%s), rule (%s) and user (%s) in the forced decision map.", variationKey2, flagKey, ruleKey, userId)));
}
Also used : OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) Test(org.junit.Test)

Example 29 with OptimizelyDecision

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

the class OptimizelyUserContextTest method decideReasons_variableValueInvalid.

@Test
public void decideReasons_variableValueInvalid() {
    String flagKey = "feature_1";
    FeatureFlag flag = getSpyFeatureFlag(flagKey);
    List<FeatureVariable> variables = Arrays.asList(new FeatureVariable("any-id", "any-key", "invalid", null, "integer", null));
    when(flag.getVariables()).thenReturn(variables);
    addSpyFeatureFlag(flag);
    OptimizelyUserContext user = optimizely.createUserContext(userId);
    OptimizelyDecision decision = user.decide(flagKey);
    assertEquals(decision.getReasons().get(0), DecisionMessage.VARIABLE_VALUE_INVALID.reason("any-key"));
}
Also used : OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) Test(org.junit.Test)

Example 30 with OptimizelyDecision

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

the class OptimizelyUserContextTest method decideAll_allFlags_enabledFlagsOnly.

@Test
public void decideAll_allFlags_enabledFlagsOnly() {
    String flagKey1 = "feature_1";
    OptimizelyJSON variablesExpected1 = optimizely.getAllFeatureVariables(flagKey1, userId);
    OptimizelyUserContext user = optimizely.createUserContext(userId, Collections.singletonMap("gender", "f"));
    Map<String, OptimizelyDecision> decisions = user.decideAll(Arrays.asList(OptimizelyDecideOption.ENABLED_FLAGS_ONLY));
    assertTrue(decisions.size() == 2);
    assertEquals(decisions.get(flagKey1), new OptimizelyDecision("a", true, variablesExpected1, "exp_with_audience", flagKey1, 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