Search in sources :

Example 16 with EventBuilder

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);
}
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 17 with EventBuilder

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);
}
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)

Example 18 with EventBuilder

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);
}
Also used : EventBuilder(com.optimizely.ab.event.internal.EventBuilder) HashMap(java.util.HashMap) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 19 with EventBuilder

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));
}
Also used : EventBuilder(com.optimizely.ab.event.internal.EventBuilder) Experiment(com.optimizely.ab.config.Experiment) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 20 with EventBuilder

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);
}
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) Bucketer(com.optimizely.ab.bucketing.Bucketer) Test(org.junit.Test)

Aggregations

Experiment (com.optimizely.ab.config.Experiment)23 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)23 Test (org.junit.Test)23 Variation (com.optimizely.ab.config.Variation)21 LogEvent (com.optimizely.ab.event.LogEvent)21 Matchers.anyString (org.mockito.Matchers.anyString)21 HashMap (java.util.HashMap)19 EventType (com.optimizely.ab.config.EventType)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)11 Map (java.util.Map)11 ActivateNotificationListener (com.optimizely.ab.notification.ActivateNotificationListener)5 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)5 TrackNotificationListener (com.optimizely.ab.notification.TrackNotificationListener)4 Nonnull (javax.annotation.Nonnull)3 DecisionService (com.optimizely.ab.bucketing.DecisionService)2 ProjectConfig (com.optimizely.ab.config.ProjectConfig)2 RaiseExceptionErrorHandler (com.optimizely.ab.error.RaiseExceptionErrorHandler)2 Bucketer (com.optimizely.ab.bucketing.Bucketer)1 ArrayList (java.util.ArrayList)1