Search in sources :

Example 16 with Experiment

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

the class DecisionServiceTest method getStoredVariationReturnsNullWhenVariationIsNoLongerInConfig.

/**
 * Verify that {@link DecisionService#getStoredVariation(Experiment, UserProfile)} returns null
 * when a {@link UserProfile} is present, contains a decision for the experiment in question,
 * but the variation ID for that decision does not exist in the datafile.
 */
@Test
public void getStoredVariationReturnsNullWhenVariationIsNoLongerInConfig() throws Exception {
    final Experiment experiment = noAudienceProjectConfig.getExperiments().get(0);
    final String storedVariationId = "missingVariation";
    final Decision storedDecision = new Decision(storedVariationId);
    final Map<String, Decision> storedDecisions = new HashMap<String, Decision>();
    storedDecisions.put(experiment.getId(), storedDecision);
    final UserProfile storedUserProfile = new UserProfile(userProfileId, storedDecisions);
    Bucketer bucketer = mock(Bucketer.class);
    UserProfileService userProfileService = mock(UserProfileService.class);
    when(userProfileService.lookup(userProfileId)).thenReturn(storedUserProfile.toMap());
    DecisionService decisionService = new DecisionService(bucketer, mockErrorHandler, noAudienceProjectConfig, userProfileService);
    logbackVerifier.expectMessage(Level.INFO, "User \"" + userProfileId + "\" was previously bucketed into variation with ID \"" + storedVariationId + "\" for " + "experiment \"" + experiment.getKey() + "\", but no matching variation " + "was found for that user. We will re-bucket the user.");
    assertNull(decisionService.getStoredVariation(experiment, storedUserProfile));
}
Also used : HashMap(java.util.HashMap) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 17 with Experiment

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

the class OptimizelyTest method getVariationExperimentStatusPrecedesForcedVariation.

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

Example 18 with Experiment

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

the class DecisionServiceTest method getVariationSavesANewUserProfile.

/**
 * Verify that a {@link UserProfile} is saved when the user is brand new and did not have anything returned from
 * {@link UserProfileService#lookup(String)}.
 */
@Test
public void getVariationSavesANewUserProfile() throws Exception {
    final Experiment experiment = noAudienceProjectConfig.getExperiments().get(0);
    final Variation variation = experiment.getVariations().get(0);
    final Decision decision = new Decision(variation.getId());
    final UserProfile expectedUserProfile = new UserProfile(userProfileId, Collections.singletonMap(experiment.getId(), decision));
    Bucketer bucketer = mock(Bucketer.class);
    UserProfileService userProfileService = mock(UserProfileService.class);
    DecisionService decisionService = new DecisionService(bucketer, mockErrorHandler, noAudienceProjectConfig, userProfileService);
    when(bucketer.bucket(experiment, userProfileId)).thenReturn(variation);
    when(userProfileService.lookup(userProfileId)).thenReturn(null);
    assertEquals(variation, decisionService.getVariation(experiment, userProfileId, Collections.<String, String>emptyMap()));
    verify(userProfileService).save(expectedUserProfile.toMap());
}
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 19 with Experiment

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

the class DecisionServiceTest method getVariationBucketingId.

@Test
public void getVariationBucketingId() throws Exception {
    Bucketer bucketer = mock(Bucketer.class);
    DecisionService decisionService = spy(new DecisionService(bucketer, mockErrorHandler, validProjectConfig, null));
    Experiment experiment = validProjectConfig.getExperiments().get(0);
    Variation expectedVariation = experiment.getVariations().get(0);
    when(bucketer.bucket(experiment, "bucketId")).thenReturn(expectedVariation);
    Map<String, String> attr = new HashMap<String, String>();
    attr.put(DecisionService.BUCKETING_ATTRIBUTE, "bucketId");
    // user excluded without audiences and whitelisting
    assertThat(decisionService.getVariation(experiment, genericUserId, attr), is(expectedVariation));
}
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 20 with Experiment

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

the class BucketerTest method bucketUserToVariationInOverlappingGroupExperiment.

/**
 * Verify that {@link Bucketer#bucket(Experiment, String)} 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 = mockBucketAlgorithm(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"), 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)

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