use of com.optimizely.ab.config.EventType in project java-sdk by optimizely.
the class OptimizelyTest method trackEventWithListenerNullAttributes.
/**
* Track with listener and verify that {@link Optimizely#track(String, String)} ignores null attributes.
*/
@Test
@SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "testing nullness contract violation")
public void trackEventWithListenerNullAttributes() throws Exception {
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");
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()), eq(Collections.<String, String>emptyMap()), 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);
assertTrue(attributes.isEmpty());
assertTrue(eventTags.isEmpty());
}
};
int notificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Track, trackNotification);
// call track
Map<String, String> attributes = null;
optimizely.track(eventType.getKey(), genericUserId, attributes);
optimizely.notificationCenter.removeNotificationListener(notificationId);
logbackVerifier.expectMessage(Level.WARN, "Attributes is null when non-null was expected. Defaulting to an empty attributes map.");
// 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, is(Collections.<String, String>emptyMap()));
verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
use of com.optimizely.ab.config.EventType in project java-sdk by optimizely.
the class OptimizelyTest method trackDoesNotSendEventWhenExperimentsAreLaunchedOnly.
/**
* Verify that {@link Optimizely#track(String, String, Map)}
* doesn't dispatch events when the event links only to launched experiments
*/
@Test
public void trackDoesNotSendEventWhenExperimentsAreLaunchedOnly() throws Exception {
EventType eventType;
if (datafileVersion >= 4) {
eventType = validProjectConfig.getEventNameMapping().get(EVENT_LAUNCHED_EXPERIMENT_ONLY_KEY);
} else {
eventType = noAudienceProjectConfig.getEventNameMapping().get("launched_exp_event");
}
Bucketer mockBucketAlgorithm = mock(Bucketer.class);
for (Experiment experiment : noAudienceProjectConfig.getExperiments()) {
Variation variation = experiment.getVariations().get(0);
when(mockBucketAlgorithm.bucket(eq(experiment), eq(genericUserId))).thenReturn(variation);
}
Optimizely client = Optimizely.builder(noAudienceDatafile, mockEventHandler).withConfig(noAudienceProjectConfig).withBucketing(mockBucketAlgorithm).build();
List<Experiment> eventExperiments = noAudienceProjectConfig.getExperimentsForEventKey(eventType.getKey());
for (Experiment experiment : eventExperiments) {
logbackVerifier.expectMessage(Level.INFO, "Not tracking event \"" + eventType.getKey() + "\" for experiment \"" + experiment.getKey() + "\" because experiment has status \"Launched\".");
}
logbackVerifier.expectMessage(Level.INFO, "There are no valid experiments for event \"" + eventType.getKey() + "\" to track.");
logbackVerifier.expectMessage(Level.INFO, "Not tracking event \"" + eventType.getKey() + "\" for user \"" + genericUserId + "\".");
// only 1 experiment uses the event and it has a "Launched" status so experimentsForEvent map is empty
// and the returned event will be null
// this means we will never call the dispatcher
client.track(eventType.getKey(), genericUserId, Collections.<String, String>emptyMap());
// bucket should never be called since experiments are launched so we never get variation for them
verify(mockBucketAlgorithm, never()).bucket(any(Experiment.class), anyString());
verify(mockEventHandler, never()).dispatchEvent(any(LogEvent.class));
}
use of com.optimizely.ab.config.EventType in project java-sdk by optimizely.
the class EventBuilderTest method createConversionEventAndroidClientEngineClientVersion.
/**
* Verify that supplying {@link EventBuilder} with a custom client engine and client version results in conversion
* events being sent with the overriden values.
*/
@Test
public void createConversionEventAndroidClientEngineClientVersion() throws Exception {
EventBuilder builder = new EventBuilder(EventBatch.ClientEngine.ANDROID_SDK, "0.0.0");
Attribute attribute = validProjectConfig.getAttributes().get(0);
EventType eventType = validProjectConfig.getEventTypes().get(0);
Bucketer mockBucketAlgorithm = mock(Bucketer.class);
for (Experiment experiment : validProjectConfig.getExperiments()) {
when(mockBucketAlgorithm.bucket(experiment, userId)).thenReturn(experiment.getVariations().get(0));
}
DecisionService decisionService = new DecisionService(mockBucketAlgorithm, mock(ErrorHandler.class), validProjectConfig, mock(UserProfileService.class));
Map<String, String> attributeMap = Collections.singletonMap(attribute.getKey(), "value");
Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, decisionService, eventType.getKey(), userId, attributeMap);
LogEvent conversionEvent = builder.createConversionEvent(validProjectConfig, experimentVariationMap, userId, eventType.getId(), eventType.getKey(), attributeMap, Collections.<String, Object>emptyMap());
EventBatch conversion = gson.fromJson(conversionEvent.getBody(), EventBatch.class);
assertThat(conversion.getClientName(), is(EventBatch.ClientEngine.ANDROID_SDK.getClientEngineValue()));
assertThat(conversion.getClientVersion(), is("0.0.0"));
}
use of com.optimizely.ab.config.EventType in project java-sdk by optimizely.
the class EventBuilderTest method createConversionEventForcedVariationBucketingPrecedesAudienceEval.
/**
* Verify that precedence is given to forced variation bucketing over audience evaluation when constructing a
* conversion event.
*/
@Test
public void createConversionEventForcedVariationBucketingPrecedesAudienceEval() {
EventType eventType;
String whitelistedUserId;
if (datafileVersion == 4) {
eventType = validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY);
whitelistedUserId = MULTIVARIATE_EXPERIMENT_FORCED_VARIATION_USER_ID_GRED;
} else {
eventType = validProjectConfig.getEventTypes().get(0);
whitelistedUserId = "testUser1";
}
DecisionService decisionService = new DecisionService(new Bucketer(validProjectConfig), new NoOpErrorHandler(), validProjectConfig, mock(UserProfileService.class));
// attributes are empty so user won't be in the audience for experiment using the event, but bucketing
// will still take place
Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, decisionService, eventType.getKey(), whitelistedUserId, Collections.<String, String>emptyMap());
LogEvent conversionEvent = builder.createConversionEvent(validProjectConfig, experimentVariationMap, whitelistedUserId, eventType.getId(), eventType.getKey(), Collections.<String, String>emptyMap(), Collections.<String, Object>emptyMap());
assertNotNull(conversionEvent);
EventBatch conversion = gson.fromJson(conversionEvent.getBody(), EventBatch.class);
if (datafileVersion == 4) {
// 2 experiments use the event
// basic experiment has no audience
// user is whitelisted in to one audience
assertEquals(2, conversion.getVisitors().get(0).getSnapshots().get(0).getDecisions().size());
} else {
assertEquals(1, conversion.getVisitors().get(0).getSnapshots().get(0).getDecisions().size());
}
}
use of com.optimizely.ab.config.EventType in project java-sdk by optimizely.
the class Optimizely method getEventTypeOrThrow.
/**
* Helper method to retrieve the {@link EventType} for the given event name.
* If {@link RaiseExceptionErrorHandler} is provided, either an event type is returned, or an exception is thrown.
* If {@link NoOpErrorHandler} is used, either an event type or {@code null} is returned.
*
* @param projectConfig the current project config
* @param eventName the event type to retrieve from the current project config
* @return the event type for the given event name
*
* @throws UnknownEventTypeException if there are no event types in the current project config with the given name
*/
private EventType getEventTypeOrThrow(ProjectConfig projectConfig, String eventName) throws UnknownEventTypeException {
EventType eventType = projectConfig.getEventNameMapping().get(eventName);
// if the given event name isn't present in the config, log and potentially throw an exception
if (eventType == null) {
String unknownEventTypeError = String.format("Event \"%s\" is not in the datafile.", eventName);
logger.error(unknownEventTypeError);
errorHandler.handleError(new UnknownEventTypeException(unknownEventTypeError));
}
return eventType;
}
Aggregations