use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class DecisionServiceTest method getVariationEvaluatesUserProfileBeforeAudienceTargeting.
/**
* Verify that {@link DecisionService#getVariation(Experiment, String, Map)}
* gives precedence to user profile over audience evaluation.
*/
@Test
public void getVariationEvaluatesUserProfileBeforeAudienceTargeting() throws Exception {
Experiment experiment = validProjectConfig.getExperiments().get(0);
Variation variation = experiment.getVariations().get(0);
Bucketer bucketer = spy(new Bucketer(validProjectConfig));
Decision decision = new Decision(variation.getId());
UserProfile userProfile = new UserProfile(userProfileId, Collections.singletonMap(experiment.getId(), decision));
UserProfileService userProfileService = mock(UserProfileService.class);
when(userProfileService.lookup(userProfileId)).thenReturn(userProfile.toMap());
DecisionService decisionService = spy(new DecisionService(bucketer, mockErrorHandler, validProjectConfig, userProfileService));
// ensure that normal users still get excluded from the experiment when they fail audience evaluation
assertNull(decisionService.getVariation(experiment, genericUserId, Collections.<String, String>emptyMap()));
logbackVerifier.expectMessage(Level.INFO, "User \"" + genericUserId + "\" does not meet conditions to be in experiment \"" + experiment.getKey() + "\".");
// ensure that a user with a saved user profile, sees the same variation regardless of audience evaluation
assertEquals(variation, decisionService.getVariation(experiment, userProfileId, Collections.<String, String>emptyMap()));
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class DecisionServiceTest method getVariationForcedBeforeUserProfile.
/**
* Verify that {@link DecisionService#getVariation(Experiment, String, Map)}
* gives precedence to forced variation bucketing over user profile.
*/
@Test
public void getVariationForcedBeforeUserProfile() throws Exception {
Experiment experiment = validProjectConfig.getExperiments().get(0);
Variation variation = experiment.getVariations().get(0);
Bucketer bucketer = spy(new Bucketer(validProjectConfig));
Decision decision = new Decision(variation.getId());
UserProfile userProfile = new UserProfile(userProfileId, Collections.singletonMap(experiment.getId(), decision));
UserProfileService userProfileService = mock(UserProfileService.class);
when(userProfileService.lookup(userProfileId)).thenReturn(userProfile.toMap());
DecisionService decisionService = spy(new DecisionService(bucketer, mockErrorHandler, validProjectConfig, userProfileService));
// ensure that normal users still get excluded from the experiment when they fail audience evaluation
assertNull(decisionService.getVariation(experiment, genericUserId, Collections.<String, String>emptyMap()));
logbackVerifier.expectMessage(Level.INFO, "User \"" + genericUserId + "\" does not meet conditions to be in experiment \"" + experiment.getKey() + "\".");
// ensure that a user with a saved user profile, sees the same variation regardless of audience evaluation
assertEquals(variation, decisionService.getVariation(experiment, userProfileId, Collections.<String, String>emptyMap()));
Variation forcedVariation = experiment.getVariations().get(1);
validProjectConfig.setForcedVariation(experiment.getKey(), userProfileId, forcedVariation.getKey());
assertEquals(forcedVariation, decisionService.getVariation(experiment, userProfileId, Collections.<String, String>emptyMap()));
assertTrue(validProjectConfig.setForcedVariation(experiment.getKey(), userProfileId, null));
assertNull(validProjectConfig.getForcedVariation(experiment.getKey(), userProfileId));
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class DecisionServiceTest method getForcedVariationBeforeWhitelisting.
/**
* Verify that {@link DecisionService#getVariation(Experiment, String, Map)}
* gives precedence to forced variation bucketing over whitelisting.
*/
@Test
public void getForcedVariationBeforeWhitelisting() throws Exception {
Bucketer bucketer = new Bucketer(validProjectConfig);
DecisionService decisionService = spy(new DecisionService(bucketer, mockErrorHandler, validProjectConfig, null));
Experiment experiment = validProjectConfig.getExperiments().get(0);
Variation whitelistVariation = experiment.getVariations().get(0);
Variation expectedVariation = experiment.getVariations().get(1);
// user excluded without audiences and whitelisting
assertNull(decisionService.getVariation(experiment, genericUserId, Collections.<String, String>emptyMap()));
logbackVerifier.expectMessage(Level.INFO, "User \"" + genericUserId + "\" does not meet conditions to be in experiment \"etag1\".");
// set the runtimeForcedVariation
validProjectConfig.setForcedVariation(experiment.getKey(), whitelistedUserId, expectedVariation.getKey());
// no attributes provided for a experiment that has an audience
assertThat(decisionService.getVariation(experiment, whitelistedUserId, Collections.<String, String>emptyMap()), is(expectedVariation));
// verify(decisionService).getForcedVariation(experiment.getKey(), whitelistedUserId);
verify(decisionService, never()).getStoredVariation(eq(experiment), any(UserProfile.class));
assertEquals(decisionService.getWhitelistedVariation(experiment, whitelistedUserId), whitelistVariation);
assertTrue(validProjectConfig.setForcedVariation(experiment.getKey(), whitelistedUserId, null));
assertNull(validProjectConfig.getForcedVariation(experiment.getKey(), whitelistedUserId));
assertThat(decisionService.getVariation(experiment, whitelistedUserId, Collections.<String, String>emptyMap()), is(whitelistVariation));
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class DecisionServiceTest method getVariationForFeatureInRolloutReturnsVariationWhenUserFailsTrafficInRuleButWouldPassForAnotherRuleAndPassesInEveryoneElse.
/**
* Verify that {@link DecisionService#getVariationForFeatureInRollout(FeatureFlag, String, Map)}
* returns the variation of "Everyone Else" rule
* when the user passes targeting for a rule, but was failed the traffic allocation for that rule,
* and is bucketed successfully into the "Everyone Else" rule.
* Fallback bucketing should not evaluate any other audiences.
* Even though the user would satisfy a later rollout rule, they are never evaluated for it or bucketed into it.
*/
@Test
public void getVariationForFeatureInRolloutReturnsVariationWhenUserFailsTrafficInRuleButWouldPassForAnotherRuleAndPassesInEveryoneElse() {
Bucketer mockBucketer = mock(Bucketer.class);
Rollout rollout = ROLLOUT_2;
Experiment englishCitizensRule = rollout.getExperiments().get(2);
Variation englishCitizenVariation = englishCitizensRule.getVariations().get(0);
Experiment everyoneElseRule = rollout.getExperiments().get(rollout.getExperiments().size() - 1);
Variation expectedVariation = everyoneElseRule.getVariations().get(0);
when(mockBucketer.bucket(any(Experiment.class), anyString())).thenReturn(null);
when(mockBucketer.bucket(eq(everyoneElseRule), anyString())).thenReturn(expectedVariation);
when(mockBucketer.bucket(eq(englishCitizensRule), anyString())).thenReturn(englishCitizenVariation);
DecisionService decisionService = new DecisionService(mockBucketer, mockErrorHandler, v4ProjectConfig, null);
FeatureDecision featureDecision = decisionService.getVariationForFeatureInRollout(FEATURE_FLAG_MULTI_VARIATE_FEATURE, genericUserId, ProjectConfigTestUtils.createMapOfObjects(ProjectConfigTestUtils.createListOfObjects(ATTRIBUTE_HOUSE_KEY, ATTRIBUTE_NATIONALITY_KEY), ProjectConfigTestUtils.createListOfObjects(AUDIENCE_GRYFFINDOR_VALUE, AUDIENCE_ENGLISH_CITIZENS_VALUE)));
assertEquals(expectedVariation, featureDecision.variation);
assertEquals(FeatureDecision.DecisionSource.ROLLOUT, featureDecision.decisionSource);
// verify user is only bucketed once for everyone else rule
verify(mockBucketer, times(2)).bucket(any(Experiment.class), anyString());
}
Aggregations