Search in sources :

Example 1 with OptimizelyDecision

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

the class Example method processVisitor.

private void processVisitor(String userId, Map<String, Object> attributes) {
    OptimizelyUserContext user = optimizely.createUserContext(userId, attributes);
    OptimizelyDecision decision = user.decide("eet_feature");
    String variationKey = decision.getVariationKey();
    if (variationKey != null) {
        boolean enabled = decision.getEnabled();
        System.out.println("[Example] feature enabled: " + enabled);
        OptimizelyJSON variables = decision.getVariables();
        System.out.println("[Example] feature variables: " + variables.toString());
        user.trackEvent("eet_conversion");
    } else {
        System.out.println("[Example] decision failed: " + decision.getReasons().toString());
    }
}
Also used : OptimizelyJSON(com.optimizely.ab.optimizelyjson.OptimizelyJSON) OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) OptimizelyUserContext(com.optimizely.ab.OptimizelyUserContext)

Example 2 with OptimizelyDecision

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

the class OptimizelyUserContextTest method decideReasons_userAttributeInvalidType.

@Test
public void decideReasons_userAttributeInvalidType() {
    String flagKey = "feature_1";
    String audienceId = "invalid_type";
    Experiment experiment = getSpyExperiment(flagKey);
    when(experiment.getAudienceIds()).thenReturn(Arrays.asList(audienceId));
    addSpyExperiment(experiment);
    OptimizelyDecision decision = callDecideWithIncludeReasons(flagKey, Collections.singletonMap("age", 25));
    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 3 with OptimizelyDecision

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

the class OptimizelyUserContextTest method decideReasons_userAttributeNilValue.

@Test
public void decideReasons_userAttributeNilValue() {
    String flagKey = "feature_1";
    String audienceId = "nil_value";
    Experiment experiment = getSpyExperiment(flagKey);
    when(experiment.getAudienceIds()).thenReturn(Arrays.asList(audienceId));
    addSpyExperiment(experiment);
    OptimizelyDecision decision = callDecideWithIncludeReasons(flagKey, Collections.singletonMap("age", 25));
    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 4 with OptimizelyDecision

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

the class OptimizelyUserContextTest method decideReasons_featureKeyInvalid.

@Test
public void decideReasons_featureKeyInvalid() {
    String flagKey = "invalid_key";
    OptimizelyUserContext user = optimizely.createUserContext(userId);
    OptimizelyDecision decision = user.decide(flagKey);
    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 5 with OptimizelyDecision

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

the class OptimizelyUserContextTest method decideAll_allFlags.

@Test
public void decideAll_allFlags() {
    optimizely = new Optimizely.Builder().withDatafile(datafile).withEventProcessor(new ForwardingEventProcessor(eventHandler, null)).build();
    String flagKey1 = "feature_1";
    String flagKey2 = "feature_2";
    String flagKey3 = "feature_3";
    Map<String, Object> attributes = Collections.singletonMap("gender", "f");
    OptimizelyJSON variablesExpected1 = optimizely.getAllFeatureVariables(flagKey1, userId);
    OptimizelyJSON variablesExpected2 = optimizely.getAllFeatureVariables(flagKey2, userId);
    OptimizelyJSON variablesExpected3 = new OptimizelyJSON(Collections.emptyMap());
    OptimizelyUserContext user = optimizely.createUserContext(userId, attributes);
    Map<String, OptimizelyDecision> decisions = user.decideAll();
    assertTrue(decisions.size() == 3);
    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()));
    assertEquals(decisions.get(flagKey3), new OptimizelyDecision(null, false, variablesExpected3, null, flagKey3, user, Collections.emptyList()));
    eventHandler.expectImpression("10390977673", "10389729780", userId, attributes);
    eventHandler.expectImpression("10420810910", "10418551353", userId, attributes);
    eventHandler.expectImpression(null, "", userId, attributes);
}
Also used : ForwardingEventProcessor(com.optimizely.ab.event.ForwardingEventProcessor) OptimizelyJSON(com.optimizely.ab.optimizelyjson.OptimizelyJSON) 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