use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class DecisionServiceTest method getVariationForFeatureInRolloutReturnsVariationWhenUserFailsTargetingInPreviousRulesButPassesRule3.
/**
* Verify that {@link DecisionService#getVariationForFeatureInRollout(FeatureFlag, String, Map)}
* returns the variation of "English Citizens" rule
* when the user fails targeting for previous rules, but passes targeting and traffic for Rule 3.
*/
@Test
public void getVariationForFeatureInRolloutReturnsVariationWhenUserFailsTargetingInPreviousRulesButPassesRule3() {
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 everyoneElseVariation = everyoneElseRule.getVariations().get(0);
when(mockBucketer.bucket(any(Experiment.class), anyString())).thenReturn(null);
when(mockBucketer.bucket(eq(everyoneElseRule), anyString())).thenReturn(everyoneElseVariation);
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, Collections.singletonMap(ATTRIBUTE_NATIONALITY_KEY, AUDIENCE_ENGLISH_CITIZENS_VALUE));
assertEquals(englishCitizenVariation, featureDecision.variation);
assertEquals(FeatureDecision.DecisionSource.ROLLOUT, featureDecision.decisionSource);
// verify user is only bucketed once for everyone else rule
verify(mockBucketer, times(1)).bucket(any(Experiment.class), anyString());
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class DecisionServiceTest method getVariationForFeatureInRolloutReturnsVariationWhenUserFailsAllAudienceButSatisfiesTraffic.
/**
* Verify that {@link DecisionService#getVariationForFeatureInRollout(FeatureFlag, String, Map)}
* returns the variation of "Everyone Else" rule
* when the user fails targeting for all rules, but is bucketed into the "Everyone Else" rule.
*/
@Test
public void getVariationForFeatureInRolloutReturnsVariationWhenUserFailsAllAudienceButSatisfiesTraffic() {
Bucketer mockBucketer = mock(Bucketer.class);
Rollout rollout = ROLLOUT_2;
Experiment everyoneElseRule = rollout.getExperiments().get(rollout.getExperiments().size() - 1);
Variation expectedVariation = everyoneElseRule.getVariations().get(0);
when(mockBucketer.bucket(eq(everyoneElseRule), anyString())).thenReturn(expectedVariation);
DecisionService decisionService = new DecisionService(mockBucketer, mockErrorHandler, v4ProjectConfig, null);
FeatureDecision featureDecision = decisionService.getVariationForFeatureInRollout(FEATURE_FLAG_MULTI_VARIATE_FEATURE, genericUserId, Collections.<String, String>emptyMap());
assertEquals(expectedVariation, featureDecision.variation);
assertEquals(FeatureDecision.DecisionSource.ROLLOUT, featureDecision.decisionSource);
// verify user is only bucketed once for everyone else rule
verify(mockBucketer, times(1)).bucket(any(Experiment.class), anyString());
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class DecisionServiceTest method getWhitelistedWithInvalidVariation.
/**
* Verify that {@link DecisionService#getWhitelistedVariation(Experiment, String)} returns null
* when an invalid variation key is found in the forced variations mapping.
*/
@Test
public void getWhitelistedWithInvalidVariation() throws Exception {
String userId = "testUser1";
String invalidVariationKey = "invalidVarKey";
Bucketer bucketer = new Bucketer(validProjectConfig);
DecisionService decisionService = new DecisionService(bucketer, mockErrorHandler, validProjectConfig, null);
List<Variation> variations = Collections.singletonList(new Variation("1", "var1"));
List<TrafficAllocation> trafficAllocations = Collections.singletonList(new TrafficAllocation("1", 1000));
Map<String, String> userIdToVariationKeyMap = Collections.singletonMap(userId, invalidVariationKey);
Experiment experiment = new Experiment("1234", "exp_key", "Running", "1", Collections.<String>emptyList(), variations, userIdToVariationKeyMap, trafficAllocations);
logbackVerifier.expectMessage(Level.ERROR, "Variation \"" + invalidVariationKey + "\" is not in the datafile. Not activating user \"" + userId + "\".");
assertNull(decisionService.getWhitelistedVariation(experiment, userId));
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class DecisionServiceTest method getStoredVariationLogsWhenLookupReturnsNull.
/**
* Verify that {@link DecisionService#getStoredVariation(Experiment, UserProfile)} returns null and logs properly
* when there is no stored variation for that user in that {@link Experiment} in the {@link UserProfileService}.
*/
@Test
public void getStoredVariationLogsWhenLookupReturnsNull() throws Exception {
final Experiment experiment = noAudienceProjectConfig.getExperiments().get(0);
Bucketer bucketer = new Bucketer(noAudienceProjectConfig);
UserProfileService userProfileService = mock(UserProfileService.class);
UserProfile userProfile = new UserProfile(userProfileId, Collections.<String, Decision>emptyMap());
when(userProfileService.lookup(userProfileId)).thenReturn(userProfile.toMap());
DecisionService decisionService = new DecisionService(bucketer, mockErrorHandler, noAudienceProjectConfig, userProfileService);
logbackVerifier.expectMessage(Level.INFO, "No previously activated variation of experiment " + "\"" + experiment.getKey() + "\" for user \"" + userProfileId + "\" found in user profile.");
assertNull(decisionService.getStoredVariation(experiment, userProfile));
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class DecisionServiceTest method getVariationForFeatureInRolloutReturnsVariationWhenUserFailsTrafficInRuleAndPassesInEveryoneElse.
/**
* 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.
*/
@Test
public void getVariationForFeatureInRolloutReturnsVariationWhenUserFailsTrafficInRuleAndPassesInEveryoneElse() {
Bucketer mockBucketer = mock(Bucketer.class);
Rollout rollout = ROLLOUT_2;
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);
DecisionService decisionService = new DecisionService(mockBucketer, mockErrorHandler, v4ProjectConfig, null);
FeatureDecision featureDecision = decisionService.getVariationForFeatureInRollout(FEATURE_FLAG_MULTI_VARIATE_FEATURE, genericUserId, Collections.singletonMap(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_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