Search in sources :

Example 1 with OptimizelyUserContext

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());
    }
}
Also used : OptimizelyJSON(com.optimizely.ab.optimizelyjson.OptimizelyJSON) OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) OptimizelyUserContext(com.optimizely.ab.OptimizelyUserContext)

Example 2 with OptimizelyUserContext

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());
}
Also used : OptimizelyDecisionContext(com.optimizely.ab.OptimizelyDecisionContext) OptimizelyUserContext(com.optimizely.ab.OptimizelyUserContext) OptimizelyForcedDecision(com.optimizely.ab.OptimizelyForcedDecision) Test(org.junit.Test)

Example 3 with OptimizelyUserContext

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);
}
Also used : OptimizelyJSON(com.optimizely.ab.optimizelyjson.OptimizelyJSON) ArrayList(java.util.ArrayList) OptimizelyUserContext(com.optimizely.ab.OptimizelyUserContext) Test(org.junit.Test)

Example 4 with OptimizelyUserContext

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);
}
Also used : OptimizelyUserContext(com.optimizely.ab.OptimizelyUserContext) Test(org.junit.Test)

Example 5 with OptimizelyUserContext

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());
}
Also used : OptimizelyDecisionContext(com.optimizely.ab.OptimizelyDecisionContext) OptimizelyUserContext(com.optimizely.ab.OptimizelyUserContext) OptimizelyForcedDecision(com.optimizely.ab.OptimizelyForcedDecision) Test(org.junit.Test)

Aggregations

OptimizelyUserContext (com.optimizely.ab.OptimizelyUserContext)5 Test (org.junit.Test)4 OptimizelyDecisionContext (com.optimizely.ab.OptimizelyDecisionContext)2 OptimizelyForcedDecision (com.optimizely.ab.OptimizelyForcedDecision)2 OptimizelyJSON (com.optimizely.ab.optimizelyjson.OptimizelyJSON)2 OptimizelyDecision (com.optimizely.ab.optimizelydecision.OptimizelyDecision)1 ArrayList (java.util.ArrayList)1