Search in sources :

Example 1 with OptimizelyForcedDecision

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

the class DecisionService method validatedForcedDecision.

/**
 * Find a validated forced decision
 *
 * @param optimizelyDecisionContext The OptimizelyDecisionContext containing flagKey and ruleKey
 * @param projectConfig             The Project config
 * @param user                      The OptimizelyUserContext
 * @return Returns a DecisionResponse structure of type Variation, otherwise null result with reasons
 */
public DecisionResponse<Variation> validatedForcedDecision(@Nonnull OptimizelyDecisionContext optimizelyDecisionContext, @Nonnull ProjectConfig projectConfig, @Nonnull OptimizelyUserContext user) {
    DecisionReasons reasons = DefaultDecisionReasons.newInstance();
    String userId = user.getUserId();
    OptimizelyForcedDecision optimizelyForcedDecision = user.findForcedDecision(optimizelyDecisionContext);
    String variationKey = optimizelyForcedDecision != null ? optimizelyForcedDecision.getVariationKey() : null;
    if (projectConfig != null && variationKey != null) {
        Variation variation = projectConfig.getFlagVariationByKey(optimizelyDecisionContext.getFlagKey(), variationKey);
        String ruleKey = optimizelyDecisionContext.getRuleKey();
        String flagKey = optimizelyDecisionContext.getFlagKey();
        String info;
        String target = ruleKey != OptimizelyDecisionContext.OPTI_NULL_RULE_KEY ? String.format("flag (%s), rule (%s)", flagKey, ruleKey) : String.format("flag (%s)", flagKey);
        if (variation != null) {
            info = String.format("Variation (%s) is mapped to %s and user (%s) in the forced decision map.", variationKey, target, userId);
            logger.debug(info);
            reasons.addInfo(info);
            return new DecisionResponse(variation, reasons);
        } else {
            info = String.format("Invalid variation is mapped to %s and user (%s) in the forced decision map.", target, userId);
            logger.debug(info);
            reasons.addInfo(info);
        }
    }
    return new DecisionResponse<>(null, reasons);
}
Also used : DecisionResponse(com.optimizely.ab.optimizelydecision.DecisionResponse) DefaultDecisionReasons(com.optimizely.ab.optimizelydecision.DefaultDecisionReasons) DecisionReasons(com.optimizely.ab.optimizelydecision.DecisionReasons) OptimizelyForcedDecision(com.optimizely.ab.OptimizelyForcedDecision)

Example 2 with OptimizelyForcedDecision

use of com.optimizely.ab.OptimizelyForcedDecision 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 OptimizelyForcedDecision

use of com.optimizely.ab.OptimizelyForcedDecision 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

OptimizelyForcedDecision (com.optimizely.ab.OptimizelyForcedDecision)3 OptimizelyDecisionContext (com.optimizely.ab.OptimizelyDecisionContext)2 OptimizelyUserContext (com.optimizely.ab.OptimizelyUserContext)2 Test (org.junit.Test)2 DecisionReasons (com.optimizely.ab.optimizelydecision.DecisionReasons)1 DecisionResponse (com.optimizely.ab.optimizelydecision.DecisionResponse)1 DefaultDecisionReasons (com.optimizely.ab.optimizelydecision.DefaultDecisionReasons)1