Search in sources :

Example 31 with Variation

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

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

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

the class OptimizelyTest method getVariationWithAudiencesNoAttributes.

/**
 * Verify that {@link Optimizely#getVariation(String, String)} doesn't return a variation when
 * given an experiment with audiences but no attributes.
 */
@Test
public void getVariationWithAudiencesNoAttributes() throws Exception {
    Experiment experiment;
    if (datafileVersion >= 4) {
        experiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_MULTIVARIATE_EXPERIMENT_KEY);
    } else {
        experiment = validProjectConfig.getExperiments().get(0);
    }
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withErrorHandler(mockErrorHandler).build();
    logbackVerifier.expectMessage(Level.INFO, "User \"userId\" does not meet conditions to be in experiment \"" + experiment.getKey() + "\".");
    Variation actualVariation = optimizely.getVariation(experiment.getKey(), testUserId);
    assertNull(actualVariation);
}
Also used : Experiment(com.optimizely.ab.config.Experiment) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 34 with Variation

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

the class BucketerTest method bucketUserToVariationInOverlappingGroupExperiment.

/**
 * Verify that {@link Bucketer#bucket(Experiment, String, ProjectConfig)} returns a variation when the user falls into an
 * experiment within an overlapping group.
 */
@Test
public void bucketUserToVariationInOverlappingGroupExperiment() throws Exception {
    final AtomicInteger bucketValue = new AtomicInteger();
    Bucketer algorithm = testBucketAlgorithm(bucketValue);
    bucketValue.set(0);
    ProjectConfig projectConfig = validProjectConfigV2();
    List<Experiment> groupExperiments = projectConfig.getGroups().get(1).getExperiments();
    Experiment groupExperiment = groupExperiments.get(0);
    Variation expectedVariation = groupExperiment.getVariations().get(0);
    logbackVerifier.expectMessage(Level.INFO, "User with bucketingId \"blah\" is in variation \"e1_vtag1\" of experiment \"overlapping_etag1\".");
    assertThat(algorithm.bucket(groupExperiment, "blah", projectConfig).getResult(), is(expectedVariation));
}
Also used : ProjectConfig(com.optimizely.ab.config.ProjectConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Experiment(com.optimizely.ab.config.Experiment) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test) ExhaustiveTest(com.optimizely.ab.categories.ExhaustiveTest)

Example 35 with Variation

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

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