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());
}
}
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())));
}
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())));
}
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));
}
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);
}
Aggregations