use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method activateWithUnknownExperimentKeyAndNoOpErrorHandler.
/**
* Verify that {@link Optimizely#activate(String, String)} handles the case where an unknown experiment
* (i.e., not in the config) is passed through and a {@link NoOpErrorHandler} is used by default.
*/
@Test
public void activateWithUnknownExperimentKeyAndNoOpErrorHandler() throws Exception {
Experiment unknownExperiment = createUnknownExperiment();
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).build();
logbackVerifier.expectMessage(Level.ERROR, "Experiment \"unknown_experiment\" is not in the datafile.");
logbackVerifier.expectMessage(Level.INFO, "Not activating user \"userId\" for experiment \"unknown_experiment\".");
// since we use a NoOpErrorHandler, we should fail and return null
Variation actualVariation = optimizely.activate(unknownExperiment.getKey(), testUserId);
// verify that null is returned, as no project config was available
assertNull(actualVariation);
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method activateForcedVariationPrecedesAudienceEval.
/**
* Verify that {@link Optimizely#activate(String, String, Map)} gives precedence to forced variation bucketing
* over audience evaluation.
*/
@Test
public void activateForcedVariationPrecedesAudienceEval() throws Exception {
Experiment experiment;
String whitelistedUserId;
Variation expectedVariation;
if (datafileVersion == 4) {
experiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_MULTIVARIATE_EXPERIMENT_KEY);
whitelistedUserId = MULTIVARIATE_EXPERIMENT_FORCED_VARIATION_USER_ID_GRED;
expectedVariation = experiment.getVariationKeyToVariationMap().get(VARIATION_MULTIVARIATE_EXPERIMENT_GRED_KEY);
} else {
experiment = validProjectConfig.getExperiments().get(0);
whitelistedUserId = "testUser1";
expectedVariation = experiment.getVariations().get(0);
}
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).build();
logbackVerifier.expectMessage(Level.INFO, "User \"" + whitelistedUserId + "\" is forced in variation \"" + expectedVariation.getKey() + "\".");
// no attributes provided for a experiment that has an audience
assertTrue(experiment.getUserIdToVariationKeyMap().containsKey(whitelistedUserId));
assertThat(optimizely.activate(experiment.getKey(), whitelistedUserId), is(expectedVariation));
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method getVariationBucketingIdAttribute.
/**
* Verify that {@link Optimizely#getVariation(String, String)} returns a variation when given an experiment
* with no audiences and no user attributes.
*/
@Test
public void getVariationBucketingIdAttribute() throws Exception {
Experiment experiment = noAudienceProjectConfig.getExperiments().get(0);
Variation bucketedVariation = experiment.getVariations().get(0);
String bucketingKey = testBucketingIdKey;
String bucketingId = "blah";
String userId = testUserId;
Map<String, String> testUserAttributes = new HashMap<String, String>();
testUserAttributes.put("browser_type", "chrome");
testUserAttributes.put(bucketingKey, bucketingId);
when(mockBucketer.bucket(experiment, bucketingId)).thenReturn(bucketedVariation);
Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withConfig(noAudienceProjectConfig).withBucketing(mockBucketer).withErrorHandler(mockErrorHandler).build();
Variation actualVariation = optimizely.getVariation(experiment.getKey(), userId, testUserAttributes);
verify(mockBucketer).bucket(experiment, bucketingId);
assertThat(actualVariation, is(bucketedVariation));
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method getVariationForGroupExperimentWithMatchingAttributes.
/**
* Verify that {@link Optimizely#getVariation(String, String, Map)} returns a variation when given matching
* user attributes.
*/
@Test
public void getVariationForGroupExperimentWithMatchingAttributes() throws Exception {
Experiment experiment = validProjectConfig.getGroups().get(0).getExperiments().get(0);
Variation variation = experiment.getVariations().get(0);
Map<String, String> attributes = new HashMap<String, String>();
if (datafileVersion >= 4) {
attributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
} else {
attributes.put("browser_type", "chrome");
}
when(mockBucketer.bucket(experiment, "user")).thenReturn(variation);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).withBucketing(mockBucketer).build();
assertThat(optimizely.getVariation(experiment.getKey(), "user", attributes), is(variation));
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method activateForGroupExperimentWithMatchingAttributes.
/**
* Verify that {@link Optimizely#activate(String, String, Map)} returns a variation when given matching
* user attributes.
*/
@Test
public void activateForGroupExperimentWithMatchingAttributes() throws Exception {
Experiment experiment = validProjectConfig.getGroups().get(0).getExperiments().get(0);
Variation variation = experiment.getVariations().get(0);
Map<String, String> attributes = new HashMap<String, String>();
if (datafileVersion == 4) {
attributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
} else {
attributes.put("browser_type", "chrome");
}
when(mockBucketer.bucket(experiment, "user")).thenReturn(variation);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).withBucketing(mockBucketer).build();
assertThat(optimizely.activate(experiment.getKey(), "user", attributes), is(variation));
}
Aggregations