Search in sources :

Example 56 with Experiment

use of com.optimizely.ab.config.Experiment 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 57 with Experiment

use of com.optimizely.ab.config.Experiment 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));
}
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 58 with Experiment

use of com.optimizely.ab.config.Experiment 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);
}
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 59 with Experiment

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

the class OptimizelyTest method activateWithEmptyUserId.

/**
 * Verify that {@link Optimizely#activate(String, String)} doesn't return a variation when provided an empty string.
 */
@Test
public void activateWithEmptyUserId() throws Exception {
    Experiment experiment = noAudienceProjectConfig.getExperiments().get(0);
    String experimentKey = experiment.getKey();
    Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withConfig(noAudienceProjectConfig).withErrorHandler(new RaiseExceptionErrorHandler()).build();
    logbackVerifier.expectMessage(Level.ERROR, "Non-empty user ID required");
    logbackVerifier.expectMessage(Level.INFO, "Not activating user for experiment \"" + experimentKey + "\".");
    assertNull(optimizely.activate(experimentKey, ""));
}
Also used : Experiment(com.optimizely.ab.config.Experiment) RaiseExceptionErrorHandler(com.optimizely.ab.error.RaiseExceptionErrorHandler) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 60 with Experiment

use of com.optimizely.ab.config.Experiment 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);
}
Also used : Experiment(com.optimizely.ab.config.Experiment) RaiseExceptionErrorHandler(com.optimizely.ab.error.RaiseExceptionErrorHandler) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Aggregations

Experiment (com.optimizely.ab.config.Experiment)125 Test (org.junit.Test)108 Variation (com.optimizely.ab.config.Variation)77 Matchers.anyString (org.mockito.Matchers.anyString)44 LogEvent (com.optimizely.ab.event.LogEvent)42 HashMap (java.util.HashMap)36 EventType (com.optimizely.ab.config.EventType)24 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)23 ProjectConfig (com.optimizely.ab.config.ProjectConfig)19 Map (java.util.Map)14 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 Attribute (com.optimizely.ab.config.Attribute)11 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)11 Bucketer (com.optimizely.ab.bucketing.Bucketer)9 ExhaustiveTest (com.optimizely.ab.categories.ExhaustiveTest)9 Rollout (com.optimizely.ab.config.Rollout)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 DecisionService (com.optimizely.ab.bucketing.DecisionService)8 ArrayList (java.util.ArrayList)8