Search in sources :

Example 1 with RaiseExceptionErrorHandler

use of com.optimizely.ab.error.RaiseExceptionErrorHandler in project java-sdk by optimizely.

the class OptimizelyTest method trackEventWithUnknownAttribute.

/**
 * Verify that {@link Optimizely#track(String, String)} handles the case where an unknown attribute
 * (i.e., not in the config) is passed through.
 *
 * In this case, the track event call should remove the unknown attribute from the given map.
 */
@Test
@SuppressWarnings("unchecked")
public void trackEventWithUnknownAttribute() 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(new RaiseExceptionErrorHandler()).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()), anyMapOf(String.class, String.class), eq(Collections.<String, Object>emptyMap()))).thenReturn(logEventToDispatch);
    logbackVerifier.expectMessage(Level.INFO, "Tracking event \"" + eventType.getKey() + "\" for user \"" + genericUserId + "\".");
    logbackVerifier.expectMessage(Level.WARN, "Attribute(s) [unknownAttribute] not in the datafile.");
    logbackVerifier.expectMessage(Level.DEBUG, "Dispatching conversion event to URL test_url with params " + testParams + " and payload \"\"");
    // call track
    optimizely.track(eventType.getKey(), genericUserId, ImmutableMap.of("unknownAttribute", "attributeValue"));
    // 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, not(hasKey("unknownAttribute")));
    verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
Also used : EventType(com.optimizely.ab.config.EventType) HashMap(java.util.HashMap) LogEvent(com.optimizely.ab.event.LogEvent) RaiseExceptionErrorHandler(com.optimizely.ab.error.RaiseExceptionErrorHandler) 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 2 with RaiseExceptionErrorHandler

use of com.optimizely.ab.error.RaiseExceptionErrorHandler 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)

Example 3 with RaiseExceptionErrorHandler

use of com.optimizely.ab.error.RaiseExceptionErrorHandler in project java-sdk by optimizely.

the class OptimizelyTest method activateWithUnknownExperimentKeyAndRaiseExceptionErrorHandler.

/**
 * Verify that {@link Optimizely#activate(String, String)} handles the case where an unknown experiment
 * (i.e., not in the config) is passed through and a {@link RaiseExceptionErrorHandler} is provided.
 */
@Test
public void activateWithUnknownExperimentKeyAndRaiseExceptionErrorHandler() throws Exception {
    thrown.expect(UnknownExperimentException.class);
    Experiment unknownExperiment = createUnknownExperiment();
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).withErrorHandler(new RaiseExceptionErrorHandler()).build();
    // since we use a RaiseExceptionErrorHandler, we should throw an error
    optimizely.activate(unknownExperiment.getKey(), testUserId);
}
Also used : Experiment(com.optimizely.ab.config.Experiment) RaiseExceptionErrorHandler(com.optimizely.ab.error.RaiseExceptionErrorHandler) Test(org.junit.Test)

Example 4 with RaiseExceptionErrorHandler

use of com.optimizely.ab.error.RaiseExceptionErrorHandler in project java-sdk by optimizely.

the class OptimizelyTest method getVariationWithUnknownExperimentKeyAndRaiseExceptionErrorHandler.

/**
 * Verify that {@link Optimizely#getVariation(String, String)} handles the case where an unknown experiment
 * (i.e., not in the config) is passed through and a {@link RaiseExceptionErrorHandler} is provided.
 */
@Test
public void getVariationWithUnknownExperimentKeyAndRaiseExceptionErrorHandler() throws Exception {
    thrown.expect(UnknownExperimentException.class);
    Experiment unknownExperiment = createUnknownExperiment();
    Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withConfig(noAudienceProjectConfig).withErrorHandler(new RaiseExceptionErrorHandler()).build();
    // since we use a RaiseExceptionErrorHandler, we should throw an error
    optimizely.getVariation(unknownExperiment.getKey(), testUserId);
}
Also used : Experiment(com.optimizely.ab.config.Experiment) RaiseExceptionErrorHandler(com.optimizely.ab.error.RaiseExceptionErrorHandler) Test(org.junit.Test)

Example 5 with RaiseExceptionErrorHandler

use of com.optimizely.ab.error.RaiseExceptionErrorHandler in project java-sdk by optimizely.

the class OptimizelyTest method getVariationWithEmptyUserId.

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

Aggregations

RaiseExceptionErrorHandler (com.optimizely.ab.error.RaiseExceptionErrorHandler)8 Test (org.junit.Test)8 Experiment (com.optimizely.ab.config.Experiment)7 Variation (com.optimizely.ab.config.Variation)3 Matchers.anyString (org.mockito.Matchers.anyString)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 EventType (com.optimizely.ab.config.EventType)2 LogEvent (com.optimizely.ab.event.LogEvent)2 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)2 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2