use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method activateWithExperimentKey.
/**
* Verify that the {@link Optimizely#activate(String, String, Map<String, String>)} call
* correctly builds an endpoint url and request params
* and passes them through {@link EventHandler#dispatchEvent(LogEvent)}.
*/
@Test
public void activateWithExperimentKey() throws Exception {
Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
Variation bucketedVariation = activatedExperiment.getVariations().get(0);
Variation userIdBucketVariation = activatedExperiment.getVariations().get(1);
EventBuilder mockEventBuilder = mock(EventBuilder.class);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withEventBuilder(mockEventBuilder).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
Map<String, String> testUserAttributes = new HashMap<String, String>();
if (datafileVersion >= 4) {
testUserAttributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
} else {
testUserAttributes.put("browser_type", "chrome");
}
testUserAttributes.put(testBucketingIdKey, testBucketingId);
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(validProjectConfig), eq(activatedExperiment), eq(bucketedVariation), eq(testUserId), eq(testUserAttributes))).thenReturn(logEventToDispatch);
when(mockBucketer.bucket(activatedExperiment, testUserId)).thenReturn(userIdBucketVariation);
when(mockBucketer.bucket(activatedExperiment, testBucketingId)).thenReturn(bucketedVariation);
// activate the experiment
Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), testUserId, testUserAttributes);
// verify that the bucketing algorithm was called correctly
verify(mockBucketer).bucket(activatedExperiment, testBucketingId);
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 activateForNullVariation.
/**
* Verify that the {@link Optimizely#activate(Experiment, String, Map)} DOES NOT dispatch an impression event
* when the user isn't bucketed to a variation.
*/
@Test
public void activateForNullVariation() throws Exception {
Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
Map<String, String> testUserAttributes = new HashMap<String, String>();
testUserAttributes.put("browser_type", "chrome");
testUserAttributes.put(testBucketingIdKey, testBucketingId);
when(mockBucketer.bucket(activatedExperiment, testBucketingId)).thenReturn(null);
logbackVerifier.expectMessage(Level.INFO, "Not activating user \"userId\" for experiment \"" + activatedExperiment.getKey() + "\".");
// activate the experiment
Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), testUserId, testUserAttributes);
// verify that the bucketing algorithm was called correctly
verify(mockBucketer).bucket(activatedExperiment, testBucketingId);
assertNull(actualVariation);
// verify that dispatchEvent was NOT called
verify(mockEventHandler, never()).dispatchEvent(any(LogEvent.class));
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method getVariationWithExperimentKey.
/**
* Verify that {@link Optimizely#getVariation(String, String)} correctly makes the
* {@link Bucketer#bucket(Experiment, String)} call and does NOT dispatch an event.
*/
@Test
public void getVariationWithExperimentKey() throws Exception {
Experiment activatedExperiment = noAudienceProjectConfig.getExperiments().get(0);
Variation bucketedVariation = activatedExperiment.getVariations().get(0);
Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withBucketing(mockBucketer).withConfig(noAudienceProjectConfig).withErrorHandler(mockErrorHandler).build();
when(mockBucketer.bucket(activatedExperiment, testUserId)).thenReturn(bucketedVariation);
// activate the experiment
Variation actualVariation = optimizely.getVariation(activatedExperiment.getKey(), testUserId);
// verify that the bucketing algorithm was called correctly
verify(mockBucketer).bucket(activatedExperiment, testUserId);
assertThat(actualVariation, is(bucketedVariation));
// verify that we didn't attempt to dispatch an event
verify(mockEventHandler, never()).dispatchEvent(any(LogEvent.class));
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method trackEventWithListenerAttributes.
/**
* Add notificaiton listener for track {@link com.optimizely.ab.notification.NotificationCenter}. Verify called and
* remove.
* @throws Exception
*/
@Test
@SuppressWarnings("unchecked")
public void trackEventWithListenerAttributes() throws Exception {
final Attribute attribute = validProjectConfig.getAttributes().get(0);
final EventType eventType;
if (datafileVersion >= 4) {
eventType = validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY);
} else {
eventType = validProjectConfig.getEventTypes().get(0);
}
// setup a mock event builder to return expected conversion params
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");
final Map<String, String> attributes = ImmutableMap.of(attribute.getKey(), "attributeValue");
Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, mockDecisionService, eventType.getKey(), genericUserId, attributes);
LogEvent logEventToDispatch = new LogEvent(RequestMethod.GET, "test_url", testParams, "");
when(mockEventBuilder.createConversionEvent(eq(validProjectConfig), eq(experimentVariationMap), eq(genericUserId), eq(eventType.getId()), eq(eventType.getKey()), anyMapOf(String.class, String.class), eq(Collections.<String, Object>emptyMap()))).thenReturn(logEventToDispatch);
logbackVerifier.expectMessage(Level.INFO, "Tracking event \"" + eventType.getKey() + "\" for user \"" + genericUserId + "\".");
logbackVerifier.expectMessage(Level.DEBUG, "Dispatching conversion event to URL test_url with params " + testParams + " and payload \"\"");
TrackNotificationListener trackNotification = new TrackNotificationListener() {
@Override
public void onTrack(@Nonnull String eventKey, @Nonnull String userId, @Nonnull Map<String, String> _attributes, @Nonnull Map<String, ?> eventTags, @Nonnull LogEvent event) {
assertEquals(eventType.getKey(), eventKey);
assertEquals(genericUserId, userId);
assertEquals(attributes, _attributes);
assertTrue(eventTags.isEmpty());
}
};
int notificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Track, trackNotification);
// call track
optimizely.track(eventType.getKey(), genericUserId, attributes);
optimizely.notificationCenter.removeNotificationListener(notificationId);
// setup the attribute map captor (so we can verify its content)
ArgumentCaptor<Map> attributeCaptor = ArgumentCaptor.forClass(Map.class);
// verify that the event builder was called with the expected attributes
verify(mockEventBuilder).createConversionEvent(eq(validProjectConfig), eq(experimentVariationMap), eq(genericUserId), eq(eventType.getId()), eq(eventType.getKey()), attributeCaptor.capture(), eq(Collections.<String, Object>emptyMap()));
Map<String, String> actualValue = attributeCaptor.getValue();
assertThat(actualValue, hasEntry(attribute.getKey(), "attributeValue"));
verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method activateDraftExperiment.
/**
* Verify that {@link Optimizely#activate(String, String)} returns null when the experiment id corresponds to a
* non-running experiment.
*/
@Test
public void activateDraftExperiment() throws Exception {
Experiment inactiveExperiment;
if (datafileVersion == 4) {
inactiveExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_PAUSED_EXPERIMENT_KEY);
} else {
inactiveExperiment = validProjectConfig.getExperiments().get(1);
}
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).build();
logbackVerifier.expectMessage(Level.INFO, "Experiment \"" + inactiveExperiment.getKey() + "\" is not running.");
logbackVerifier.expectMessage(Level.INFO, "Not activating user \"userId\" for experiment \"" + inactiveExperiment.getKey() + "\".");
Variation variation = optimizely.activate(inactiveExperiment.getKey(), testUserId);
// verify that null is returned, as the experiment isn't running
assertNull(variation);
}
Aggregations