use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method isFeatureEnabledWithExperimentKeyForcedWithNoFeatureEnabledSet.
/**
* Verify that the {@link Optimizely#activate(String, String, Map<String, String>)} call
* uses forced variation to force the user into the second variation in which FeatureEnabled is not set
* feature enabled will return false by default
*/
@Test
public void isFeatureEnabledWithExperimentKeyForcedWithNoFeatureEnabledSet() throws Exception {
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
Experiment activatedExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_DOUBLE_FEATURE_EXPERIMENT_KEY);
Variation forcedVariation = activatedExperiment.getVariations().get(1);
EventBuilder mockEventBuilder = mock(EventBuilder.class);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withEventBuilder(mockEventBuilder).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
optimizely.setForcedVariation(activatedExperiment.getKey(), testUserId, forcedVariation.getKey());
assertFalse(optimizely.isFeatureEnabled(FEATURE_SINGLE_VARIABLE_DOUBLE_KEY, testUserId));
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method activateEndToEnd.
// ======== activate tests ========//
/**
* Verify that the {@link Optimizely#activate(Experiment, String, Map)} call correctly builds an endpoint url and
* request params and passes them through {@link EventHandler#dispatchEvent(LogEvent)}.
*/
@Test
public void activateEndToEnd() throws Exception {
Experiment activatedExperiment;
Map<String, String> testUserAttributes = new HashMap<String, String>();
String bucketingKey = testBucketingIdKey;
String userId = testUserId;
String bucketingId = testBucketingId;
if (datafileVersion >= 4) {
activatedExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_MULTIVARIATE_EXPERIMENT_KEY);
testUserAttributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
} else {
activatedExperiment = validProjectConfig.getExperiments().get(0);
testUserAttributes.put("browser_type", "chrome");
}
testUserAttributes.put(bucketingKey, bucketingId);
Variation bucketedVariation = activatedExperiment.getVariations().get(0);
EventBuilder mockEventBuilder = mock(EventBuilder.class);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withEventBuilder(mockEventBuilder).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
Map<String, String> testParams = new HashMap<String, String>();
testParams.put("test", "params");
LogEvent logEventToDispatch = new LogEvent(RequestMethod.GET, "test_url", testParams, "");
when(mockEventBuilder.createImpressionEvent(validProjectConfig, activatedExperiment, bucketedVariation, testUserId, testUserAttributes)).thenReturn(logEventToDispatch);
when(mockBucketer.bucket(activatedExperiment, bucketingId)).thenReturn(bucketedVariation);
logbackVerifier.expectMessage(Level.INFO, "Activating user \"userId\" in experiment \"" + activatedExperiment.getKey() + "\".");
logbackVerifier.expectMessage(Level.DEBUG, "Dispatching impression event to URL test_url with params " + testParams + " and payload \"\"");
// activate the experiment
Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), userId, testUserAttributes);
// verify that the bucketing algorithm was called correctly
verify(mockBucketer).bucket(activatedExperiment, bucketingId);
assertThat(actualVariation, is(bucketedVariation));
// verify that dispatchEvent was called with the correct LogEvent object
verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method getVariationWithAudiencesNoAttributes.
/**
* Verify that {@link Optimizely#getVariation(String, String)} doesn't return a variation when
* given an experiment with audiences but no attributes.
*/
@Test
public void getVariationWithAudiencesNoAttributes() throws Exception {
Experiment experiment;
if (datafileVersion >= 4) {
experiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_MULTIVARIATE_EXPERIMENT_KEY);
} else {
experiment = validProjectConfig.getExperiments().get(0);
}
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withErrorHandler(mockErrorHandler).build();
logbackVerifier.expectMessage(Level.INFO, "User \"userId\" does not meet conditions to be in experiment \"" + experiment.getKey() + "\".");
Variation actualVariation = optimizely.getVariation(experiment.getKey(), testUserId);
assertNull(actualVariation);
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class BucketerTest method bucketUserToVariationInOverlappingGroupExperiment.
/**
* Verify that {@link Bucketer#bucket(Experiment, String, ProjectConfig)} 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 = testBucketAlgorithm(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", projectConfig).getResult(), is(expectedVariation));
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method activateWithListenerNullAttributes.
@Test
@SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "testing nullness contract violation")
public void activateWithListenerNullAttributes() throws Exception {
final Experiment activatedExperiment = noAudienceProjectConfig.getExperiments().get(0);
final Variation bucketedVariation = activatedExperiment.getVariations().get(0);
// setup a mock event builder to return expected impression params
EventBuilder mockEventBuilder = mock(EventBuilder.class);
Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withBucketing(mockBucketer).withEventBuilder(mockEventBuilder).withConfig(noAudienceProjectConfig).withErrorHandler(mockErrorHandler).build();
Map<String, String> testParams = new HashMap<String, String>();
testParams.put("test", "params");
LogEvent logEventToDispatch = new LogEvent(RequestMethod.GET, "test_url", testParams, "");
when(mockEventBuilder.createImpressionEvent(eq(noAudienceProjectConfig), eq(activatedExperiment), eq(bucketedVariation), eq(testUserId), eq(Collections.<String, String>emptyMap()))).thenReturn(logEventToDispatch);
when(mockBucketer.bucket(activatedExperiment, testUserId)).thenReturn(bucketedVariation);
ActivateNotificationListener activateNotification = new ActivateNotificationListener() {
@Override
public void onActivate(@Nonnull Experiment experiment, @Nonnull String userId, @Nonnull Map<String, String> attributes, @Nonnull Variation variation, @Nonnull LogEvent event) {
assertEquals(experiment.getKey(), activatedExperiment.getKey());
assertEquals(bucketedVariation.getKey(), variation.getKey());
assertEquals(userId, testUserId);
assertTrue(attributes.isEmpty());
assertEquals(event.getRequestMethod(), RequestMethod.GET);
}
};
int notificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Activate, activateNotification);
// activate the experiment
Map<String, String> attributes = null;
Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), testUserId, attributes);
optimizely.notificationCenter.removeNotificationListener(notificationId);
logbackVerifier.expectMessage(Level.WARN, "Attributes is null when non-null was expected. Defaulting to an empty attributes map.");
// verify that the bucketing algorithm was called correctly
verify(mockBucketer).bucket(activatedExperiment, testUserId);
assertThat(actualVariation, is(bucketedVariation));
// setup the attribute map captor (so we can verify its content)
ArgumentCaptor<Map> attributeCaptor = ArgumentCaptor.forClass(Map.class);
verify(mockEventBuilder).createImpressionEvent(eq(noAudienceProjectConfig), eq(activatedExperiment), eq(bucketedVariation), eq(testUserId), attributeCaptor.capture());
Map<String, String> actualValue = attributeCaptor.getValue();
assertThat(actualValue, is(Collections.<String, String>emptyMap()));
// verify that dispatchEvent was called with the correct LogEvent object
verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
Aggregations