use of com.optimizely.ab.OptimizelyUserContext 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.OptimizelyUserContext in project java-sdk by optimizely.
the class DecisionServiceTest method validatedForcedDecisionWithoutRuleKey.
@Test
public void validatedForcedDecisionWithoutRuleKey() {
String userId = "testUser1";
String flagKey = "multi_variate_feature";
String variationKey = "521740985";
OptimizelyUserContext optimizelyUserContext = new OptimizelyUserContext(optimizely, userId, Collections.emptyMap());
OptimizelyDecisionContext optimizelyDecisionContext = new OptimizelyDecisionContext(flagKey, null);
OptimizelyForcedDecision optimizelyForcedDecision = new OptimizelyForcedDecision(variationKey);
optimizelyUserContext.setForcedDecision(optimizelyDecisionContext, optimizelyForcedDecision);
DecisionResponse<Variation> response = decisionService.validatedForcedDecision(optimizelyDecisionContext, v4ProjectConfig, optimizelyUserContext);
Variation variation = response.getResult();
assertEquals(variationKey, variation.getKey());
}
use of com.optimizely.ab.OptimizelyUserContext in project java-sdk by optimizely.
the class OptimizelyDecisionTest method testOptimizelyDecision.
@Test
public void testOptimizelyDecision() {
String variationKey = "var1";
boolean enabled = true;
OptimizelyJSON variables = new OptimizelyJSON("{\"k1\":\"v1\"}");
String ruleKey = null;
String flagKey = "flag1";
OptimizelyUserContext userContext = new OptimizelyUserContext(Optimizely.builder().build(), "tester");
List<String> reasons = new ArrayList<>();
OptimizelyDecision decision = new OptimizelyDecision(variationKey, enabled, variables, ruleKey, flagKey, userContext, reasons);
assertEquals(decision.getVariationKey(), variationKey);
assertEquals(decision.getEnabled(), enabled);
assertEquals(decision.getVariables(), variables);
assertEquals(decision.getRuleKey(), ruleKey);
assertEquals(decision.getFlagKey(), flagKey);
assertEquals(decision.getUserContext(), userContext);
assertEquals(decision.getReasons(), reasons);
}
use of com.optimizely.ab.OptimizelyUserContext in project java-sdk by optimizely.
the class OptimizelyDecisionTest method testNewErrorDecision.
@Test
public void testNewErrorDecision() {
String flagKey = "flag1";
OptimizelyUserContext userContext = new OptimizelyUserContext(Optimizely.builder().build(), "tester");
String error = "SDK has an error";
OptimizelyDecision decision = OptimizelyDecision.newErrorDecision(flagKey, userContext, error);
assertEquals(decision.getVariationKey(), null);
assertEquals(decision.getEnabled(), false);
assertTrue(decision.getVariables().isEmpty());
assertEquals(decision.getRuleKey(), null);
assertEquals(decision.getFlagKey(), flagKey);
assertEquals(decision.getUserContext(), userContext);
assertEquals(decision.getReasons().size(), 1);
assertEquals(decision.getReasons().get(0), error);
}
use of com.optimizely.ab.OptimizelyUserContext in project java-sdk by optimizely.
the class DecisionServiceTest method validatedForcedDecisionWithRuleKey.
@Test
public void validatedForcedDecisionWithRuleKey() {
String userId = "testUser1";
String ruleKey = "2637642575";
String flagKey = "multi_variate_feature";
String variationKey = "2346257680";
OptimizelyUserContext optimizelyUserContext = new OptimizelyUserContext(optimizely, userId, Collections.emptyMap());
OptimizelyDecisionContext optimizelyDecisionContext = new OptimizelyDecisionContext(flagKey, ruleKey);
OptimizelyForcedDecision optimizelyForcedDecision = new OptimizelyForcedDecision(variationKey);
optimizelyUserContext.setForcedDecision(optimizelyDecisionContext, optimizelyForcedDecision);
DecisionResponse<Variation> response = decisionService.validatedForcedDecision(optimizelyDecisionContext, v4ProjectConfig, optimizelyUserContext);
Variation variation = response.getResult();
assertEquals(variationKey, variation.getKey());
}
Aggregations