use of com.optimizely.ab.event.internal.EventBuilder in project java-sdk by optimizely.
the class OptimizelyTest method trackEventEndToEnd.
/**
* Verify that the {@link Optimizely#track(String, String)} call correctly builds a V2 event and passes it
* through {@link EventHandler#dispatchEvent(LogEvent)}.
*/
@Test
public void trackEventEndToEnd() throws Exception {
EventType eventType;
String datafile;
ProjectConfig config;
if (datafileVersion >= 4) {
config = spy(validProjectConfig);
eventType = validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY);
datafile = validDatafile;
} else {
config = spy(noAudienceProjectConfig);
eventType = noAudienceProjectConfig.getEventTypes().get(0);
datafile = noAudienceDatafile;
}
List<Experiment> allExperiments = config.getExperiments();
EventBuilder eventBuilder = new EventBuilder();
DecisionService spyDecisionService = spy(new DecisionService(mockBucketer, mockErrorHandler, config, null));
Optimizely optimizely = Optimizely.builder(datafile, mockEventHandler).withDecisionService(spyDecisionService).withEventBuilder(eventBuilder).withConfig(noAudienceProjectConfig).withErrorHandler(mockErrorHandler).build();
// call the bucket function.
for (Experiment experiment : allExperiments) {
when(mockBucketer.bucket(experiment, testUserId)).thenReturn(experiment.getVariations().get(0));
}
// call track
optimizely.track(eventType.getKey(), testUserId);
// verify that the bucketing algorithm was called only on experiments corresponding to the specified goal.
List<Experiment> experimentsForEvent = config.getExperimentsForEventKey(eventType.getKey());
for (Experiment experiment : allExperiments) {
if (experiment.isRunning() && experimentsForEvent.contains(experiment)) {
verify(spyDecisionService).getVariation(experiment, testUserId, Collections.<String, String>emptyMap());
verify(config).getForcedVariation(experiment.getKey(), testUserId);
} else {
verify(spyDecisionService, never()).getVariation(experiment, testUserId, Collections.<String, String>emptyMap());
}
}
// verify that dispatchEvent was called
verify(mockEventHandler).dispatchEvent(any(LogEvent.class));
}
use of com.optimizely.ab.event.internal.EventBuilder in project java-sdk by optimizely.
the class OptimizelyTest method addNotificationListenerFromNotificationCenter.
/**
* Verify that {@link com.optimizely.ab.notification.NotificationCenter#addNotificationListener(
* com.optimizely.ab.notification.NotificationCenter.NotificationType,
* com.optimizely.ab.notification.NotificationListener)} properly used
* and the listener is
* added and notified when an experiment is activated.
*/
@Test
public void addNotificationListenerFromNotificationCenter() throws Exception {
Experiment activatedExperiment;
EventType eventType;
if (datafileVersion >= 4) {
activatedExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_BASIC_EXPERIMENT_KEY);
eventType = validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY);
} else {
activatedExperiment = validProjectConfig.getExperiments().get(0);
eventType = validProjectConfig.getEventTypes().get(0);
}
Variation bucketedVariation = activatedExperiment.getVariations().get(0);
EventBuilder mockEventBuilder = mock(EventBuilder.class);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withDecisionService(mockDecisionService).withEventBuilder(mockEventBuilder).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
Map<String, String> attributes = Collections.emptyMap();
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, genericUserId, attributes)).thenReturn(logEventToDispatch);
when(mockDecisionService.getVariation(eq(activatedExperiment), eq(genericUserId), eq(Collections.<String, String>emptyMap()))).thenReturn(bucketedVariation);
// Add listener
ActivateNotificationListener listener = mock(ActivateNotificationListener.class);
optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Activate, listener);
// Check if listener is notified when experiment is activated
Variation actualVariation = optimizely.activate(activatedExperiment, genericUserId, attributes);
verify(listener, times(1)).onActivate(activatedExperiment, genericUserId, attributes, bucketedVariation, logEventToDispatch);
assertEquals(actualVariation.getKey(), bucketedVariation.getKey());
// Check if listener is notified after an event is tracked
String eventKey = eventType.getKey();
Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, mockDecisionService, eventType.getKey(), genericUserId, attributes);
when(mockEventBuilder.createConversionEvent(eq(validProjectConfig), eq(experimentVariationMap), eq(genericUserId), eq(eventType.getId()), eq(eventKey), eq(attributes), anyMapOf(String.class, Object.class))).thenReturn(logEventToDispatch);
TrackNotificationListener trackNotification = mock(TrackNotificationListener.class);
optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Track, trackNotification);
optimizely.track(eventKey, genericUserId, attributes);
verify(trackNotification, times(1)).onTrack(eventKey, genericUserId, attributes, Collections.EMPTY_MAP, logEventToDispatch);
}
use of com.optimizely.ab.event.internal.EventBuilder in project java-sdk by optimizely.
the class OptimizelyTest method isFeatureEnabledWithExperimentKeyForced.
/**
* Verify that the {@link Optimizely#activate(String, String, Map<String, String>)} call
* uses forced variation to force the user into the second variation. The mock bucket returns
* the first variation. Then remove the forced variation and confirm that the forced variation is null.
*/
@Test
public void isFeatureEnabledWithExperimentKeyForced() throws Exception {
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
Experiment activatedExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_MULTIVARIATE_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());
Map<String, String> testUserAttributes = new HashMap<String, String>();
if (datafileVersion < 4) {
testUserAttributes.put("browser_type", "chrome");
}
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(forcedVariation), eq(testUserId), eq(testUserAttributes))).thenReturn(logEventToDispatch);
// activate the experiment
assertTrue(optimizely.isFeatureEnabled(FEATURE_FLAG_MULTI_VARIATE_FEATURE.getKey(), testUserId));
verify(mockEventHandler).dispatchEvent(logEventToDispatch);
assertTrue(optimizely.setForcedVariation(activatedExperiment.getKey(), testUserId, null));
assertNull(optimizely.getForcedVariation(activatedExperiment.getKey(), testUserId));
assertFalse(optimizely.isFeatureEnabled(FEATURE_FLAG_MULTI_VARIATE_FEATURE.getKey(), testUserId));
}
use of com.optimizely.ab.event.internal.EventBuilder in project java-sdk by optimizely.
the class OptimizelyTest method trackEventWithEventTags.
/**
* Verify that {@link Optimizely#track(String, String, Map, Map)} passes event features to
* {@link EventBuilder#createConversionEvent(ProjectConfig, Map, String, String, String, Map, Map)}
*/
@Test
public void trackEventWithEventTags() throws Exception {
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");
Map<String, Object> eventTags = new HashMap<String, Object>();
eventTags.put("int_param", 123);
eventTags.put("string_param", "123");
eventTags.put("boolean_param", false);
eventTags.put("float_param", 12.3f);
Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, mockDecisionService, eventType.getKey(), genericUserId, Collections.<String, String>emptyMap());
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(eventTags))).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 \"\"");
// call track
optimizely.track(eventType.getKey(), genericUserId, Collections.<String, String>emptyMap(), eventTags);
// setup the event map captor (so we can verify its content)
ArgumentCaptor<Map> eventTagCaptor = 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()), eq(Collections.<String, String>emptyMap()), eventTagCaptor.capture());
Map<String, ?> actualValue = eventTagCaptor.getValue();
assertThat(actualValue, hasEntry("int_param", eventTags.get("int_param")));
assertThat(actualValue, hasEntry("string_param", eventTags.get("string_param")));
assertThat(actualValue, hasEntry("boolean_param", eventTags.get("boolean_param")));
assertThat(actualValue, hasEntry("float_param", eventTags.get("float_param")));
verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
use of com.optimizely.ab.event.internal.EventBuilder in project java-sdk by optimizely.
the class OptimizelyTest method activateWithExperimentKeyForced.
/**
* Verify that the {@link Optimizely#activate(String, String, Map<String, String>)} call
* uses forced variation to force the user into the second variation. The mock bucket returns
* the first variation. Then remove the forced variation and confirm that the forced variation is null.
*/
@Test
public void activateWithExperimentKeyForced() throws Exception {
Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
Variation forcedVariation = activatedExperiment.getVariations().get(1);
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();
optimizely.setForcedVariation(activatedExperiment.getKey(), testUserId, forcedVariation.getKey());
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(forcedVariation), eq(testUserId), eq(testUserAttributes))).thenReturn(logEventToDispatch);
when(mockBucketer.bucket(activatedExperiment, testBucketingId)).thenReturn(bucketedVariation);
// activate the experiment
Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), testUserId, testUserAttributes);
assertThat(actualVariation, is(forcedVariation));
verify(mockEventHandler).dispatchEvent(logEventToDispatch);
optimizely.setForcedVariation(activatedExperiment.getKey(), testUserId, null);
assertEquals(optimizely.getForcedVariation(activatedExperiment.getKey(), testUserId), null);
}
Aggregations