Search in sources :

Example 6 with EventBuilder

use of com.optimizely.ab.event.internal.EventBuilder 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 7 with EventBuilder

use of com.optimizely.ab.event.internal.EventBuilder in project java-sdk by optimizely.

the class OptimizelyTest method activateWithNullAttributes.

/**
 * Verify that {@link Optimizely#activate(String, String, Map)} ignores null attributes.
 */
@Test
@SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "testing nullness contract violation")
public void activateWithNullAttributes() throws Exception {
    Experiment activatedExperiment = noAudienceProjectConfig.getExperiments().get(0);
    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);
    // activate the experiment
    Map<String, String> attributes = null;
    Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), testUserId, attributes);
    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 : 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) 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 8 with EventBuilder

use of com.optimizely.ab.event.internal.EventBuilder in project java-sdk by optimizely.

the class OptimizelyTest method isFeatureEnabledWithExperimentKeyForcedWithNoFeatureEnabledSet.

/**
 * Verify that the {@link Optimizely#activate(String, String, Map<String, String>)} call
 * uses forced variation to force the user into the second variation in which FeatureEnabled is not set
 * feature enabled will return false by default
 */
@Test
public void isFeatureEnabledWithExperimentKeyForcedWithNoFeatureEnabledSet() throws Exception {
    assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
    Experiment activatedExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_DOUBLE_FEATURE_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());
    assertFalse(optimizely.isFeatureEnabled(FEATURE_SINGLE_VARIABLE_DOUBLE_KEY, 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 9 with EventBuilder

use of com.optimizely.ab.event.internal.EventBuilder in project java-sdk by optimizely.

the class OptimizelyTest method activateEndToEnd.

// ======== activate tests ========//
/**
 * Verify that the {@link Optimizely#activate(Experiment, String, Map)} call correctly builds an endpoint url and
 * request params and passes them through {@link EventHandler#dispatchEvent(LogEvent)}.
 */
@Test
public void activateEndToEnd() throws Exception {
    Experiment activatedExperiment;
    Map<String, String> testUserAttributes = new HashMap<String, String>();
    String bucketingKey = testBucketingIdKey;
    String userId = testUserId;
    String bucketingId = testBucketingId;
    if (datafileVersion >= 4) {
        activatedExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_MULTIVARIATE_EXPERIMENT_KEY);
        testUserAttributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
    } else {
        activatedExperiment = validProjectConfig.getExperiments().get(0);
        testUserAttributes.put("browser_type", "chrome");
    }
    testUserAttributes.put(bucketingKey, bucketingId);
    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, testUserId, testUserAttributes)).thenReturn(logEventToDispatch);
    when(mockBucketer.bucket(activatedExperiment, bucketingId)).thenReturn(bucketedVariation);
    logbackVerifier.expectMessage(Level.INFO, "Activating user \"userId\" in experiment \"" + activatedExperiment.getKey() + "\".");
    logbackVerifier.expectMessage(Level.DEBUG, "Dispatching impression event to URL test_url with params " + testParams + " and payload \"\"");
    // activate the experiment
    Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), userId, testUserAttributes);
    // verify that the bucketing algorithm was called correctly
    verify(mockBucketer).bucket(activatedExperiment, bucketingId);
    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 10 with EventBuilder

use of com.optimizely.ab.event.internal.EventBuilder 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)

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