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