Search in sources :

Example 1 with ActivateNotificationListener

use of com.optimizely.ab.notification.ActivateNotificationListener 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 2 with ActivateNotificationListener

use of com.optimizely.ab.notification.ActivateNotificationListener in project java-sdk by optimizely.

the class OptimizelyTest method activateWithListenerNullAttributes.

@Test
@SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "testing nullness contract violation")
public void activateWithListenerNullAttributes() throws Exception {
    final Experiment activatedExperiment = noAudienceProjectConfig.getExperiments().get(0);
    final Variation bucketedVariation = activatedExperiment.getVariations().get(0);
    // setup a mock event builder to return expected impression params
    EventBuilder mockEventBuilder = mock(EventBuilder.class);
    Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withBucketing(mockBucketer).withEventBuilder(mockEventBuilder).withConfig(noAudienceProjectConfig).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(eq(noAudienceProjectConfig), eq(activatedExperiment), eq(bucketedVariation), eq(testUserId), eq(Collections.<String, String>emptyMap()))).thenReturn(logEventToDispatch);
    when(mockBucketer.bucket(activatedExperiment, testUserId)).thenReturn(bucketedVariation);
    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);
            assertTrue(attributes.isEmpty());
            assertEquals(event.getRequestMethod(), RequestMethod.GET);
        }
    };
    int notificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Activate, activateNotification);
    // activate the experiment
    Map<String, String> attributes = null;
    Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), testUserId, attributes);
    optimizely.notificationCenter.removeNotificationListener(notificationId);
    logbackVerifier.expectMessage(Level.WARN, "Attributes is null when non-null was expected. Defaulting to an empty attributes map.");
    // verify that the bucketing algorithm was called correctly
    verify(mockBucketer).bucket(activatedExperiment, testUserId);
    assertThat(actualVariation, is(bucketedVariation));
    // setup the attribute map captor (so we can verify its content)
    ArgumentCaptor<Map> attributeCaptor = ArgumentCaptor.forClass(Map.class);
    verify(mockEventBuilder).createImpressionEvent(eq(noAudienceProjectConfig), eq(activatedExperiment), eq(bucketedVariation), eq(testUserId), attributeCaptor.capture());
    Map<String, String> actualValue = attributeCaptor.getValue();
    assertThat(actualValue, is(Collections.<String, String>emptyMap()));
    // verify that dispatchEvent was called with the correct LogEvent object
    verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
Also used : HashMap(java.util.HashMap) LogEvent(com.optimizely.ab.event.LogEvent) Nonnull(javax.annotation.Nonnull) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) EventBuilder(com.optimizely.ab.event.internal.EventBuilder) 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) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 3 with ActivateNotificationListener

use of com.optimizely.ab.notification.ActivateNotificationListener 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);
}
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) TrackNotificationListener(com.optimizely.ab.notification.TrackNotificationListener) ActivateNotificationListener(com.optimizely.ab.notification.ActivateNotificationListener) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 4 with ActivateNotificationListener

use of com.optimizely.ab.notification.ActivateNotificationListener 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);
}
Also used : HashMap(java.util.HashMap) Nonnull(javax.annotation.Nonnull) 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) 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 5 with ActivateNotificationListener

use of com.optimizely.ab.notification.ActivateNotificationListener 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);
}
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) Test(org.junit.Test)

Aggregations

Experiment (com.optimizely.ab.config.Experiment)5 Variation (com.optimizely.ab.config.Variation)5 LogEvent (com.optimizely.ab.event.LogEvent)5 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)5 ActivateNotificationListener (com.optimizely.ab.notification.ActivateNotificationListener)5 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 Matchers.anyString (org.mockito.Matchers.anyString)5 ImmutableMap (com.google.common.collect.ImmutableMap)3 EventType (com.optimizely.ab.config.EventType)3 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)3 TrackNotificationListener (com.optimizely.ab.notification.TrackNotificationListener)3 Map (java.util.Map)3 Nonnull (javax.annotation.Nonnull)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1