Search in sources :

Example 11 with EventType

use of com.optimizely.ab.config.EventType in project java-sdk by optimizely.

the class OptimizelyTest method clearNotificationListenersNotificationCenter.

/**
 * Verify that {@link com.optimizely.ab.notification.NotificationCenter}
 * clearAllListerners removes all listeners
 * and no longer notified when an experiment is activated.
 */
@Test
public void clearNotificationListenersNotificationCenter() throws Exception {
    Experiment activatedExperiment;
    Map<String, String> attributes = new HashMap<String, String>();
    if (datafileVersion >= 4) {
        activatedExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_MULTIVARIATE_EXPERIMENT_KEY);
        attributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
    } else {
        activatedExperiment = validProjectConfig.getExperiments().get(0);
        attributes.put("browser_type", "chrome");
    }
    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> 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);
    // set up argument captor for the attributes map to compare map equality
    ArgumentCaptor<Map> attributeCaptor = ArgumentCaptor.forClass(Map.class);
    when(mockEventBuilder.createImpressionEvent(eq(validProjectConfig), eq(activatedExperiment), eq(bucketedVariation), eq(genericUserId), attributeCaptor.capture())).thenReturn(logEventToDispatch);
    ActivateNotificationListener activateNotification = mock(ActivateNotificationListener.class);
    TrackNotificationListener trackNotification = mock(TrackNotificationListener.class);
    optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Activate, activateNotification);
    optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Track, trackNotification);
    optimizely.notificationCenter.clearAllNotificationListeners();
    // Check if listener is notified after an experiment is activated
    Variation actualVariation = optimizely.activate(activatedExperiment, genericUserId, attributes);
    // check that the argument that was captured by the mockEventBuilder attribute captor,
    // was equal to the attributes passed in to activate
    assertEquals(attributes, attributeCaptor.getValue());
    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 a event is tracked
    EventType eventType = validProjectConfig.getEventTypes().get(0);
    String eventKey = eventType.getKey();
    Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, mockDecisionService, eventType.getKey(), OptimizelyTest.genericUserId, attributes);
    when(mockEventBuilder.createConversionEvent(eq(validProjectConfig), eq(experimentVariationMap), eq(OptimizelyTest.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);
}
Also used : HashMap(java.util.HashMap) LogEvent(com.optimizely.ab.event.LogEvent) EventType(com.optimizely.ab.config.EventType) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) EventBuilder(com.optimizely.ab.event.internal.EventBuilder) TrackNotificationListener(com.optimizely.ab.notification.TrackNotificationListener) ActivateNotificationListener(com.optimizely.ab.notification.ActivateNotificationListener) Variation(com.optimizely.ab.config.Variation) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) EventBuilderTest.createExperimentVariationMap(com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap) Test(org.junit.Test)

Example 12 with EventType

use of com.optimizely.ab.config.EventType in project java-sdk by optimizely.

the class OptimizelyTest method trackEventWithUnknownEventKeyAndNoOpErrorHandler.

/**
 * Verify that {@link Optimizely#track(String, String)} handles the case where an unknown event type
 * (i.e., not in the config) is passed through and a {@link NoOpErrorHandler} is used by default.
 */
@Test
public void trackEventWithUnknownEventKeyAndNoOpErrorHandler() throws Exception {
    EventType unknownEventType = createUnknownEventType();
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).withErrorHandler(new NoOpErrorHandler()).build();
    logbackVerifier.expectMessage(Level.ERROR, "Event \"unknown_event_type\" is not in the datafile.");
    logbackVerifier.expectMessage(Level.INFO, "Not tracking event \"unknown_event_type\" for user \"userId\".");
    optimizely.track(unknownEventType.getKey(), testUserId);
    // verify that we did NOT dispatch an event
    verify(mockEventHandler, never()).dispatchEvent(any(LogEvent.class));
}
Also used : EventType(com.optimizely.ab.config.EventType) LogEvent(com.optimizely.ab.event.LogEvent) NoOpErrorHandler(com.optimizely.ab.error.NoOpErrorHandler) Test(org.junit.Test)

Example 13 with EventType

use of com.optimizely.ab.config.EventType in project java-sdk by optimizely.

the class OptimizelyTest method trackEventWithNullAttributeValues.

/**
 * Verify that {@link Optimizely#track(String, String)} gracefully handles null attribute values.
 */
@Test
public void trackEventWithNullAttributeValues() 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<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 \"\"");
    // call track
    Map<String, String> attributes = new HashMap<String, String>();
    attributes.put("test", null);
    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()));
    verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
Also used : EventType(com.optimizely.ab.config.EventType) HashMap(java.util.HashMap) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) EventBuilder(com.optimizely.ab.event.internal.EventBuilder) Variation(com.optimizely.ab.config.Variation) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) EventBuilderTest.createExperimentVariationMap(com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap) Test(org.junit.Test)

Example 14 with EventType

use of com.optimizely.ab.config.EventType in project java-sdk by optimizely.

the class OptimizelyTest method trackEventWithUnknownEventKeyAndRaiseExceptionErrorHandler.

/**
 * Verify that {@link Optimizely#track(String, String)} handles the case where an unknown event type
 * (i.e., not in the config) is passed through and a {@link RaiseExceptionErrorHandler} is provided.
 */
@Test
public void trackEventWithUnknownEventKeyAndRaiseExceptionErrorHandler() throws Exception {
    thrown.expect(UnknownEventTypeException.class);
    EventType unknownEventType = createUnknownEventType();
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).withErrorHandler(new RaiseExceptionErrorHandler()).build();
    // since we use a RaiseExceptionErrorHandler, we should throw an error
    optimizely.track(unknownEventType.getKey(), testUserId);
}
Also used : EventType(com.optimizely.ab.config.EventType) RaiseExceptionErrorHandler(com.optimizely.ab.error.RaiseExceptionErrorHandler) Test(org.junit.Test)

Example 15 with EventType

use of com.optimizely.ab.config.EventType 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));
}
Also used : ProjectConfig(com.optimizely.ab.config.ProjectConfig) EventBuilder(com.optimizely.ab.event.internal.EventBuilder) EventType(com.optimizely.ab.config.EventType) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) DecisionService(com.optimizely.ab.bucketing.DecisionService) Test(org.junit.Test)

Aggregations

EventType (com.optimizely.ab.config.EventType)29 LogEvent (com.optimizely.ab.event.LogEvent)25 Experiment (com.optimizely.ab.config.Experiment)24 Test (org.junit.Test)24 Variation (com.optimizely.ab.config.Variation)20 HashMap (java.util.HashMap)15 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)12 Matchers.anyString (org.mockito.Matchers.anyString)12 Bucketer (com.optimizely.ab.bucketing.Bucketer)9 DecisionService (com.optimizely.ab.bucketing.DecisionService)8 Map (java.util.Map)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 Attribute (com.optimizely.ab.config.Attribute)7 NoOpErrorHandler (com.optimizely.ab.error.NoOpErrorHandler)7 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)7 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)7 UserProfileService (com.optimizely.ab.bucketing.UserProfileService)6 ProjectConfig (com.optimizely.ab.config.ProjectConfig)6 ErrorHandler (com.optimizely.ab.error.ErrorHandler)5 TrackNotificationListener (com.optimizely.ab.notification.TrackNotificationListener)4