Search in sources :

Example 11 with Variation

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

the class OptimizelyTest method activateLaunchedExperimentDoesNotDispatchEvent.

/**
 * Verify that {@link Optimizely#activate(String, String)} doesn't dispatch an event for an experiment with a
 * "Launched" status.
 */
@Test
public void activateLaunchedExperimentDoesNotDispatchEvent() throws Exception {
    Experiment launchedExperiment;
    if (datafileVersion == 4) {
        launchedExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_LAUNCHED_EXPERIMENT_KEY);
    } else {
        launchedExperiment = noAudienceProjectConfig.getExperiments().get(2);
    }
    Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withBucketing(mockBucketer).withConfig(noAudienceProjectConfig).build();
    Variation expectedVariation = launchedExperiment.getVariations().get(0);
    when(mockBucketer.bucket(launchedExperiment, testUserId)).thenReturn(launchedExperiment.getVariations().get(0));
    logbackVerifier.expectMessage(Level.INFO, "Experiment has \"Launched\" status so not dispatching event during activation.");
    Variation variation = optimizely.activate(launchedExperiment.getKey(), testUserId);
    assertNotNull(variation);
    assertThat(variation.getKey(), is(expectedVariation.getKey()));
    // verify that we did NOT dispatch an event
    verify(mockEventHandler, never()).dispatchEvent(any(LogEvent.class));
}
Also used : LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 12 with Variation

use of com.optimizely.ab.config.Variation 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);
}
Also used : EventType(com.optimizely.ab.config.EventType) 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) TrackNotificationListener(com.optimizely.ab.notification.TrackNotificationListener) 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 13 with Variation

use of com.optimizely.ab.config.Variation 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));
}
Also used : EventType(com.optimizely.ab.config.EventType) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Variation(com.optimizely.ab.config.Variation) Bucketer(com.optimizely.ab.bucketing.Bucketer) Test(org.junit.Test)

Example 14 with Variation

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

the class OptimizelyTest method getVariation.

// ======== getVariation tests ========//
/**
 * Verify that {@link Optimizely#getVariation(Experiment, String)} correctly makes the
 * {@link Bucketer#bucket(Experiment, String)} call and does NOT dispatch an event.
 */
@Test
public void getVariation() throws Exception {
    Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
    Variation bucketedVariation = activatedExperiment.getVariations().get(0);
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
    when(mockBucketer.bucket(activatedExperiment, testUserId)).thenReturn(bucketedVariation);
    Map<String, String> testUserAttributes = new HashMap<String, String>();
    testUserAttributes.put("browser_type", "chrome");
    // activate the experiment
    Variation actualVariation = optimizely.getVariation(activatedExperiment.getKey(), testUserId, testUserAttributes);
    // verify that the bucketing algorithm was called correctly
    verify(mockBucketer).bucket(activatedExperiment, testUserId);
    assertThat(actualVariation, is(bucketedVariation));
    // verify that we didn't attempt to dispatch an event
    verify(mockEventHandler, never()).dispatchEvent(any(LogEvent.class));
}
Also used : 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 15 with Variation

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

the class OptimizelyTest method activateWithUnknownAttribute.

/**
 * Verify that {@link Optimizely#activate(String, String, Map<String, String>)} handles the case
 * where an unknown attribute (i.e., not in the config) is passed through.
 *
 * In this case, the activate call should remove the unknown attribute from the given map.
 */
@Test
@SuppressWarnings("unchecked")
public void activateWithUnknownAttribute() throws Exception {
    Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
    Variation bucketedVariation = activatedExperiment.getVariations().get(0);
    // setup a mock event builder to return mock params and endpoint
    EventBuilder mockEventBuilder = mock(EventBuilder.class);
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withEventBuilder(mockEventBuilder).withConfig(validProjectConfig).withErrorHandler(new RaiseExceptionErrorHandler()).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("unknownAttribute", "dimValue");
    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), anyMapOf(String.class, String.class))).thenReturn(logEventToDispatch);
    when(mockBucketer.bucket(activatedExperiment, testUserId)).thenReturn(bucketedVariation);
    logbackVerifier.expectMessage(Level.INFO, "Activating user \"userId\" in experiment \"" + activatedExperiment.getKey() + "\".");
    logbackVerifier.expectMessage(Level.WARN, "Attribute(s) [unknownAttribute] not in the datafile.");
    logbackVerifier.expectMessage(Level.DEBUG, "Dispatching impression event to URL test_url with params " + testParams + " and payload \"\"");
    // Use an immutable map to also check that we're not attempting to change the provided attribute map
    Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), testUserId, testUserAttributes);
    assertThat(actualVariation, is(bucketedVariation));
    // 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).createImpressionEvent(eq(validProjectConfig), eq(activatedExperiment), eq(bucketedVariation), eq(testUserId), attributeCaptor.capture());
    Map<String, String> actualValue = attributeCaptor.getValue();
    assertThat(actualValue, not(hasKey("unknownAttribute")));
    // 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) RaiseExceptionErrorHandler(com.optimizely.ab.error.RaiseExceptionErrorHandler) Matchers.anyString(org.mockito.Matchers.anyString) 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)

Aggregations

Variation (com.optimizely.ab.config.Variation)81 Experiment (com.optimizely.ab.config.Experiment)77 Test (org.junit.Test)68 LogEvent (com.optimizely.ab.event.LogEvent)39 HashMap (java.util.HashMap)37 Matchers.anyString (org.mockito.Matchers.anyString)37 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)21 EventType (com.optimizely.ab.config.EventType)20 Map (java.util.Map)14 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)11 Bucketer (com.optimizely.ab.bucketing.Bucketer)9 Attribute (com.optimizely.ab.config.Attribute)9 ProjectConfig (com.optimizely.ab.config.ProjectConfig)9 Rollout (com.optimizely.ab.config.Rollout)7 NoOpErrorHandler (com.optimizely.ab.error.NoOpErrorHandler)7 DecisionService (com.optimizely.ab.bucketing.DecisionService)6 UserProfileService (com.optimizely.ab.bucketing.UserProfileService)6 ArrayList (java.util.ArrayList)6