use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class DecisionServiceTest method getStoredVariationReturnsNullWhenVariationIsNoLongerInConfig.
/**
* Verify that {@link DecisionService#getStoredVariation(Experiment, UserProfile)} returns null
* when a {@link UserProfile} is present, contains a decision for the experiment in question,
* but the variation ID for that decision does not exist in the datafile.
*/
@Test
public void getStoredVariationReturnsNullWhenVariationIsNoLongerInConfig() throws Exception {
final Experiment experiment = noAudienceProjectConfig.getExperiments().get(0);
final String storedVariationId = "missingVariation";
final Decision storedDecision = new Decision(storedVariationId);
final Map<String, Decision> storedDecisions = new HashMap<String, Decision>();
storedDecisions.put(experiment.getId(), storedDecision);
final UserProfile storedUserProfile = new UserProfile(userProfileId, storedDecisions);
Bucketer bucketer = mock(Bucketer.class);
UserProfileService userProfileService = mock(UserProfileService.class);
when(userProfileService.lookup(userProfileId)).thenReturn(storedUserProfile.toMap());
DecisionService decisionService = new DecisionService(bucketer, mockErrorHandler, noAudienceProjectConfig, userProfileService);
logbackVerifier.expectMessage(Level.INFO, "User \"" + userProfileId + "\" was previously bucketed into variation with ID \"" + storedVariationId + "\" for " + "experiment \"" + experiment.getKey() + "\", but no matching variation " + "was found for that user. We will re-bucket the user.");
assertNull(decisionService.getStoredVariation(experiment, storedUserProfile));
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method getVariationExperimentStatusPrecedesForcedVariation.
/**
* Verify that {@link Optimizely#getVariation(String, String)} gives precedence to experiment status over forced
* variation bucketing.
*/
@Test
public void getVariationExperimentStatusPrecedesForcedVariation() throws Exception {
Experiment experiment;
if (datafileVersion >= 4) {
experiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_PAUSED_EXPERIMENT_KEY);
} else {
experiment = validProjectConfig.getExperiments().get(1);
}
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).build();
logbackVerifier.expectMessage(Level.INFO, "Experiment \"" + experiment.getKey() + "\" is not running.");
// testUser3 has a corresponding forced variation, but experiment status should be checked first
assertNull(optimizely.getVariation(experiment.getKey(), "testUser3"));
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class DecisionServiceTest method getVariationSavesANewUserProfile.
/**
* Verify that a {@link UserProfile} is saved when the user is brand new and did not have anything returned from
* {@link UserProfileService#lookup(String)}.
*/
@Test
public void getVariationSavesANewUserProfile() throws Exception {
final Experiment experiment = noAudienceProjectConfig.getExperiments().get(0);
final Variation variation = experiment.getVariations().get(0);
final Decision decision = new Decision(variation.getId());
final UserProfile expectedUserProfile = new UserProfile(userProfileId, Collections.singletonMap(experiment.getId(), decision));
Bucketer bucketer = mock(Bucketer.class);
UserProfileService userProfileService = mock(UserProfileService.class);
DecisionService decisionService = new DecisionService(bucketer, mockErrorHandler, noAudienceProjectConfig, userProfileService);
when(bucketer.bucket(experiment, userProfileId)).thenReturn(variation);
when(userProfileService.lookup(userProfileId)).thenReturn(null);
assertEquals(variation, decisionService.getVariation(experiment, userProfileId, Collections.<String, String>emptyMap()));
verify(userProfileService).save(expectedUserProfile.toMap());
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class DecisionServiceTest method getVariationBucketingId.
@Test
public void getVariationBucketingId() throws Exception {
Bucketer bucketer = mock(Bucketer.class);
DecisionService decisionService = spy(new DecisionService(bucketer, mockErrorHandler, validProjectConfig, null));
Experiment experiment = validProjectConfig.getExperiments().get(0);
Variation expectedVariation = experiment.getVariations().get(0);
when(bucketer.bucket(experiment, "bucketId")).thenReturn(expectedVariation);
Map<String, String> attr = new HashMap<String, String>();
attr.put(DecisionService.BUCKETING_ATTRIBUTE, "bucketId");
// user excluded without audiences and whitelisting
assertThat(decisionService.getVariation(experiment, genericUserId, attr), is(expectedVariation));
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class BucketerTest method bucketUserToVariationInOverlappingGroupExperiment.
/**
* Verify that {@link Bucketer#bucket(Experiment, String)} returns a variation when the user falls into an
* experiment within an overlapping group.
*/
@Test
public void bucketUserToVariationInOverlappingGroupExperiment() throws Exception {
final AtomicInteger bucketValue = new AtomicInteger();
Bucketer algorithm = mockBucketAlgorithm(bucketValue);
bucketValue.set(0);
ProjectConfig projectConfig = validProjectConfigV2();
List<Experiment> groupExperiments = projectConfig.getGroups().get(1).getExperiments();
Experiment groupExperiment = groupExperiments.get(0);
Variation expectedVariation = groupExperiment.getVariations().get(0);
logbackVerifier.expectMessage(Level.INFO, "User with bucketingId \"blah\" is in variation \"e1_vtag1\" of experiment \"overlapping_etag1\".");
assertThat(algorithm.bucket(groupExperiment, "blah"), is(expectedVariation));
}
Aggregations