use of com.optimizely.ab.config.Variation 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.config.Variation 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.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method trackEventWithAttributes.
/**
* Verify that {@link Optimizely#track(String, String)} passes through attributes.
*/
@Test
@SuppressWarnings("unchecked")
public void trackEventWithAttributes() throws Exception {
Attribute attribute = validProjectConfig.getAttributes().get(0);
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, 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 \"\"");
// call track
optimizely.track(eventType.getKey(), genericUserId, attributes);
// 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 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.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method activateWhenExperimentIsNotInProject.
/**
* Verify the case were {@link Optimizely#activate(Experiment, String)} is called with an {@link Experiment}
* that is not present in the current {@link ProjectConfig}. We should NOT throw an error in that case.
*
* This may happen if an experiment is retrieved from the project config, the project config is updated and the
* referenced experiment removed, then activate is called given the now removed experiment.
* Could also happen if an experiment was manually created and passed through.
*/
@Test
public void activateWhenExperimentIsNotInProject() throws Exception {
Experiment unknownExperiment = createUnknownExperiment();
Variation bucketedVariation = unknownExperiment.getVariations().get(0);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withConfig(validProjectConfig).withErrorHandler(new RaiseExceptionErrorHandler()).build();
when(mockBucketer.bucket(unknownExperiment, testUserId)).thenReturn(bucketedVariation);
optimizely.activate(unknownExperiment, testUserId);
}
Aggregations