Search in sources :

Example 31 with Experiment

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

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

the class OptimizelyTest method activateExperimentStatusPrecedesForcedVariation.

/**
 * Verify that {@link Optimizely#activate(String, String)} gives precedence to experiment status over forced
 * variation bucketing.
 */
@Test
public void activateExperimentStatusPrecedesForcedVariation() throws Exception {
    Experiment experiment;
    String whitelistedUserId;
    if (datafileVersion == 4) {
        experiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_PAUSED_EXPERIMENT_KEY);
        whitelistedUserId = PAUSED_EXPERIMENT_FORCED_VARIATION_USER_ID_CONTROL;
    } else {
        experiment = validProjectConfig.getExperiments().get(1);
        whitelistedUserId = "testUser3";
    }
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).build();
    logbackVerifier.expectMessage(Level.INFO, "Experiment \"" + experiment.getKey() + "\" is not running.");
    logbackVerifier.expectMessage(Level.INFO, "Not activating user \"" + whitelistedUserId + "\" for experiment \"" + experiment.getKey() + "\".");
    // testUser3 has a corresponding forced variation, but experiment status should be checked first
    assertTrue(experiment.getUserIdToVariationKeyMap().containsKey(whitelistedUserId));
    assertNull(optimizely.activate(experiment.getKey(), whitelistedUserId));
}
Also used : Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 33 with Experiment

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

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

the class OptimizelyTest method getVariationWithUnknownExperimentKeyAndNoOpErrorHandler.

/**
 * 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 NoOpErrorHandler} is used by default.
 */
@Test
public void getVariationWithUnknownExperimentKeyAndNoOpErrorHandler() throws Exception {
    Experiment unknownExperiment = createUnknownExperiment();
    Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withErrorHandler(new NoOpErrorHandler()).build();
    logbackVerifier.expectMessage(Level.ERROR, "Experiment \"unknown_experiment\" is not in the datafile");
    // since we use a NoOpErrorHandler, we should fail and return null
    Variation actualVariation = optimizely.getVariation(unknownExperiment.getKey(), testUserId);
    // verify that null is returned, as no project config was available
    assertNull(actualVariation);
}
Also used : Experiment(com.optimizely.ab.config.Experiment) NoOpErrorHandler(com.optimizely.ab.error.NoOpErrorHandler) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 35 with Experiment

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

Aggregations

Experiment (com.optimizely.ab.config.Experiment)137 Test (org.junit.Test)113 Variation (com.optimizely.ab.config.Variation)91 Matchers.anyString (org.mockito.Matchers.anyString)58 LogEvent (com.optimizely.ab.event.LogEvent)47 HashMap (java.util.HashMap)42 EventType (com.optimizely.ab.config.EventType)28 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)27 ProjectConfig (com.optimizely.ab.config.ProjectConfig)22 Map (java.util.Map)18 Attribute (com.optimizely.ab.config.Attribute)17 ImmutableMap (com.google.common.collect.ImmutableMap)15 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)15 ArrayList (java.util.ArrayList)14 Rollout (com.optimizely.ab.config.Rollout)13 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)12 TrafficAllocation (com.optimizely.ab.config.TrafficAllocation)11 ExperimentUtils.isUserInExperiment (com.optimizely.ab.internal.ExperimentUtils.isUserInExperiment)10 Bucketer (com.optimizely.ab.bucketing.Bucketer)9 ExhaustiveTest (com.optimizely.ab.categories.ExhaustiveTest)9