Search in sources :

Example 26 with Variation

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

the class OptimizelyTest method activateWithUnknownExperimentKeyAndNoOpErrorHandler.

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

Example 27 with Variation

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

the class OptimizelyTest method activateForcedVariationPrecedesAudienceEval.

/**
 * Verify that {@link Optimizely#activate(String, String, Map)} gives precedence to forced variation bucketing
 * over audience evaluation.
 */
@Test
public void activateForcedVariationPrecedesAudienceEval() throws Exception {
    Experiment experiment;
    String whitelistedUserId;
    Variation expectedVariation;
    if (datafileVersion == 4) {
        experiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_MULTIVARIATE_EXPERIMENT_KEY);
        whitelistedUserId = MULTIVARIATE_EXPERIMENT_FORCED_VARIATION_USER_ID_GRED;
        expectedVariation = experiment.getVariationKeyToVariationMap().get(VARIATION_MULTIVARIATE_EXPERIMENT_GRED_KEY);
    } else {
        experiment = validProjectConfig.getExperiments().get(0);
        whitelistedUserId = "testUser1";
        expectedVariation = experiment.getVariations().get(0);
    }
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).build();
    logbackVerifier.expectMessage(Level.INFO, "User \"" + whitelistedUserId + "\" is forced in variation \"" + expectedVariation.getKey() + "\".");
    // no attributes provided for a experiment that has an audience
    assertTrue(experiment.getUserIdToVariationKeyMap().containsKey(whitelistedUserId));
    assertThat(optimizely.activate(experiment.getKey(), whitelistedUserId), is(expectedVariation));
}
Also used : Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 28 with Variation

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

the class OptimizelyTest method getVariationBucketingIdAttribute.

/**
 * Verify that {@link Optimizely#getVariation(String, String)} returns a variation when given an experiment
 * with no audiences and no user attributes.
 */
@Test
public void getVariationBucketingIdAttribute() throws Exception {
    Experiment experiment = noAudienceProjectConfig.getExperiments().get(0);
    Variation bucketedVariation = experiment.getVariations().get(0);
    String bucketingKey = testBucketingIdKey;
    String bucketingId = "blah";
    String userId = testUserId;
    Map<String, String> testUserAttributes = new HashMap<String, String>();
    testUserAttributes.put("browser_type", "chrome");
    testUserAttributes.put(bucketingKey, bucketingId);
    when(mockBucketer.bucket(experiment, bucketingId)).thenReturn(bucketedVariation);
    Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withConfig(noAudienceProjectConfig).withBucketing(mockBucketer).withErrorHandler(mockErrorHandler).build();
    Variation actualVariation = optimizely.getVariation(experiment.getKey(), userId, testUserAttributes);
    verify(mockBucketer).bucket(experiment, bucketingId);
    assertThat(actualVariation, is(bucketedVariation));
}
Also used : HashMap(java.util.HashMap) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 29 with Variation

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

the class OptimizelyTest method getVariationForGroupExperimentWithMatchingAttributes.

/**
 * Verify that {@link Optimizely#getVariation(String, String, Map)} returns a variation when given matching
 * user attributes.
 */
@Test
public void getVariationForGroupExperimentWithMatchingAttributes() throws Exception {
    Experiment experiment = validProjectConfig.getGroups().get(0).getExperiments().get(0);
    Variation variation = experiment.getVariations().get(0);
    Map<String, String> attributes = new HashMap<String, String>();
    if (datafileVersion >= 4) {
        attributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
    } else {
        attributes.put("browser_type", "chrome");
    }
    when(mockBucketer.bucket(experiment, "user")).thenReturn(variation);
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).withBucketing(mockBucketer).build();
    assertThat(optimizely.getVariation(experiment.getKey(), "user", attributes), is(variation));
}
Also used : HashMap(java.util.HashMap) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 30 with Variation

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

the class OptimizelyTest method activateForGroupExperimentWithMatchingAttributes.

/**
 * Verify that {@link Optimizely#activate(String, String, Map)} returns a variation when given matching
 * user attributes.
 */
@Test
public void activateForGroupExperimentWithMatchingAttributes() throws Exception {
    Experiment experiment = validProjectConfig.getGroups().get(0).getExperiments().get(0);
    Variation variation = experiment.getVariations().get(0);
    Map<String, String> attributes = new HashMap<String, String>();
    if (datafileVersion == 4) {
        attributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
    } else {
        attributes.put("browser_type", "chrome");
    }
    when(mockBucketer.bucket(experiment, "user")).thenReturn(variation);
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).withBucketing(mockBucketer).build();
    assertThat(optimizely.activate(experiment.getKey(), "user", attributes), is(variation));
}
Also used : HashMap(java.util.HashMap) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) 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