use of com.optimizely.ab.event.internal.EventBuilder in project java-sdk by optimizely.
the class OptimizelyTest method activateWithListener.
// ======== Notification listeners ========//
/**
* 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 activateWithListener() throws Exception {
final Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
final 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();
final 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);
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);
for (Map.Entry<String, String> entry : attributes.entrySet()) {
assertEquals(testUserAttributes.get(entry.getKey()), entry.getValue());
}
assertEquals(event.getRequestMethod(), RequestMethod.GET);
}
};
int notificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Activate, activateNotification);
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, testBucketingId)).thenReturn(bucketedVariation);
// activate the experiment
Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), testUserId, testUserAttributes);
assertTrue(optimizely.notificationCenter.removeNotificationListener(notificationId));
// 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.event.internal.EventBuilder in project java-sdk by optimizely.
the class OptimizelyTest method removeNotificationListenerNotificationCenter.
/**
* Verify that {@link com.optimizely.ab.notification.NotificationCenter} properly
* calls and the listener is removed and no longer notified when an experiment is activated.
*/
@Test
public void removeNotificationListenerNotificationCenter() throws Exception {
Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
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> attributes = new HashMap<String, String>();
attributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
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(mockBucketer.bucket(activatedExperiment, genericUserId)).thenReturn(bucketedVariation);
when(mockEventBuilder.createImpressionEvent(validProjectConfig, activatedExperiment, bucketedVariation, genericUserId, attributes)).thenReturn(logEventToDispatch);
// Add and remove listener
ActivateNotificationListener activateNotification = mock(ActivateNotificationListener.class);
int notificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Activate, activateNotification);
assertTrue(optimizely.notificationCenter.removeNotificationListener(notificationId));
TrackNotificationListener trackNotification = mock(TrackNotificationListener.class);
notificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Track, trackNotification);
assertTrue(optimizely.notificationCenter.removeNotificationListener(notificationId));
// Check if listener is notified after an experiment is activated
Variation actualVariation = optimizely.activate(activatedExperiment, genericUserId, attributes);
verify(activateNotification, never()).onActivate(activatedExperiment, genericUserId, attributes, actualVariation, logEventToDispatch);
// Check if listener is notified after a live variable is accessed
boolean activateExperiment = true;
verify(activateNotification, never()).onActivate(activatedExperiment, genericUserId, attributes, actualVariation, logEventToDispatch);
// Check if listener is notified after an event is tracked
EventType eventType = validProjectConfig.getEventTypes().get(0);
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);
optimizely.track(eventKey, genericUserId, attributes);
verify(trackNotification, never()).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 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.event.internal.EventBuilder in project java-sdk by optimizely.
the class OptimizelyTest method isFeatureEnabledWithExperimentKeyForcedOfFeatureEnabledFalse.
/**
* Verify that the {@link Optimizely#activate(String, String, Map<String, String>)} call
* uses forced variation to force the user into the third variation in which FeatureEnabled is set to
* false so feature enabled will return false
*/
@Test
public void isFeatureEnabledWithExperimentKeyForcedOfFeatureEnabledFalse() throws Exception {
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
Experiment activatedExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_MULTIVARIATE_EXPERIMENT_KEY);
Variation forcedVariation = activatedExperiment.getVariations().get(2);
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_FLAG_MULTI_VARIATE_FEATURE.getKey(), testUserId));
}
use of com.optimizely.ab.event.internal.EventBuilder in project java-sdk by optimizely.
the class OptimizelyTest method trackDispatchesWhenEventHasLaunchedAndRunningExperiments.
/**
* Verify that {@link Optimizely#track(String, String, Map)}
* dispatches log events when the tracked event links to both launched and running experiments.
*/
@Test
public void trackDispatchesWhenEventHasLaunchedAndRunningExperiments() throws Exception {
EventBuilder mockEventBuilder = mock(EventBuilder.class);
EventType eventType;
if (datafileVersion >= 4) {
eventType = validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY);
} else {
eventType = noAudienceProjectConfig.getEventNameMapping().get("event_with_launched_and_running_experiments");
}
Bucketer mockBucketAlgorithm = mock(Bucketer.class);
for (Experiment experiment : validProjectConfig.getExperiments()) {
when(mockBucketAlgorithm.bucket(experiment, genericUserId)).thenReturn(experiment.getVariations().get(0));
}
Optimizely client = Optimizely.builder(validDatafile, mockEventHandler).withConfig(noAudienceProjectConfig).withBucketing(mockBucketAlgorithm).withEventBuilder(mockEventBuilder).build();
Map<String, String> testParams = new HashMap<String, String>();
testParams.put("test", "params");
Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(noAudienceProjectConfig, client.decisionService, eventType.getKey(), genericUserId, Collections.<String, String>emptyMap());
// Create an Argument Captor to ensure we are creating a correct experiment variation map
ArgumentCaptor<Map> experimentVariationMapCaptor = ArgumentCaptor.forClass(Map.class);
LogEvent conversionEvent = new LogEvent(RequestMethod.GET, "test_url", testParams, "");
when(mockEventBuilder.createConversionEvent(eq(noAudienceProjectConfig), experimentVariationMapCaptor.capture(), eq(genericUserId), eq(eventType.getId()), eq(eventType.getKey()), eq(Collections.<String, String>emptyMap()), eq(Collections.<String, Object>emptyMap()))).thenReturn(conversionEvent);
List<Experiment> eventExperiments = noAudienceProjectConfig.getExperimentsForEventKey(eventType.getKey());
for (Experiment experiment : eventExperiments) {
if (experiment.isLaunched()) {
logbackVerifier.expectMessage(Level.INFO, "Not tracking event \"" + eventType.getKey() + "\" for experiment \"" + experiment.getKey() + "\" because experiment has status \"Launched\".");
}
}
// The event has 1 launched experiment and 1 running experiment.
// It should send a track event with the running experiment
client.track(eventType.getKey(), genericUserId, Collections.<String, String>emptyMap());
verify(client.eventHandler).dispatchEvent(eq(conversionEvent));
// Check the argument captor got the correct arguments
Map<Experiment, Variation> actualExperimentVariationMap = experimentVariationMapCaptor.getValue();
assertEquals(experimentVariationMap, actualExperimentVariationMap);
}
Aggregations