Search in sources :

Example 96 with Variation

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

the class DecisionServiceTest method getVariationEvaluatesUserProfileBeforeAudienceTargeting.

/**
 * Verify that {@link DecisionService#getVariation(Experiment, String, Map)}
 * gives precedence to user profile over audience evaluation.
 */
@Test
public void getVariationEvaluatesUserProfileBeforeAudienceTargeting() throws Exception {
    Experiment experiment = validProjectConfig.getExperiments().get(0);
    Variation variation = experiment.getVariations().get(0);
    Bucketer bucketer = spy(new Bucketer(validProjectConfig));
    Decision decision = new Decision(variation.getId());
    UserProfile userProfile = new UserProfile(userProfileId, Collections.singletonMap(experiment.getId(), decision));
    UserProfileService userProfileService = mock(UserProfileService.class);
    when(userProfileService.lookup(userProfileId)).thenReturn(userProfile.toMap());
    DecisionService decisionService = spy(new DecisionService(bucketer, mockErrorHandler, validProjectConfig, userProfileService));
    // ensure that normal users still get excluded from the experiment when they fail audience evaluation
    assertNull(decisionService.getVariation(experiment, genericUserId, Collections.<String, String>emptyMap()));
    logbackVerifier.expectMessage(Level.INFO, "User \"" + genericUserId + "\" does not meet conditions to be in experiment \"" + experiment.getKey() + "\".");
    // ensure that a user with a saved user profile, sees the same variation regardless of audience evaluation
    assertEquals(variation, decisionService.getVariation(experiment, userProfileId, Collections.<String, String>emptyMap()));
}
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 97 with Variation

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

the class DecisionServiceTest method getVariationForcedBeforeUserProfile.

/**
 * Verify that {@link DecisionService#getVariation(Experiment, String, Map)}
 * gives precedence to forced variation bucketing over user profile.
 */
@Test
public void getVariationForcedBeforeUserProfile() throws Exception {
    Experiment experiment = validProjectConfig.getExperiments().get(0);
    Variation variation = experiment.getVariations().get(0);
    Bucketer bucketer = spy(new Bucketer(validProjectConfig));
    Decision decision = new Decision(variation.getId());
    UserProfile userProfile = new UserProfile(userProfileId, Collections.singletonMap(experiment.getId(), decision));
    UserProfileService userProfileService = mock(UserProfileService.class);
    when(userProfileService.lookup(userProfileId)).thenReturn(userProfile.toMap());
    DecisionService decisionService = spy(new DecisionService(bucketer, mockErrorHandler, validProjectConfig, userProfileService));
    // ensure that normal users still get excluded from the experiment when they fail audience evaluation
    assertNull(decisionService.getVariation(experiment, genericUserId, Collections.<String, String>emptyMap()));
    logbackVerifier.expectMessage(Level.INFO, "User \"" + genericUserId + "\" does not meet conditions to be in experiment \"" + experiment.getKey() + "\".");
    // ensure that a user with a saved user profile, sees the same variation regardless of audience evaluation
    assertEquals(variation, decisionService.getVariation(experiment, userProfileId, Collections.<String, String>emptyMap()));
    Variation forcedVariation = experiment.getVariations().get(1);
    validProjectConfig.setForcedVariation(experiment.getKey(), userProfileId, forcedVariation.getKey());
    assertEquals(forcedVariation, decisionService.getVariation(experiment, userProfileId, Collections.<String, String>emptyMap()));
    assertTrue(validProjectConfig.setForcedVariation(experiment.getKey(), userProfileId, null));
    assertNull(validProjectConfig.getForcedVariation(experiment.getKey(), userProfileId));
}
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 98 with Variation

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

the class DecisionServiceTest method getForcedVariationBeforeWhitelisting.

/**
 * Verify that {@link DecisionService#getVariation(Experiment, String, Map)}
 * gives precedence to forced variation bucketing over whitelisting.
 */
@Test
public void getForcedVariationBeforeWhitelisting() throws Exception {
    Bucketer bucketer = new Bucketer(validProjectConfig);
    DecisionService decisionService = spy(new DecisionService(bucketer, mockErrorHandler, validProjectConfig, null));
    Experiment experiment = validProjectConfig.getExperiments().get(0);
    Variation whitelistVariation = experiment.getVariations().get(0);
    Variation expectedVariation = experiment.getVariations().get(1);
    // user excluded without audiences and whitelisting
    assertNull(decisionService.getVariation(experiment, genericUserId, Collections.<String, String>emptyMap()));
    logbackVerifier.expectMessage(Level.INFO, "User \"" + genericUserId + "\" does not meet conditions to be in experiment \"etag1\".");
    // set the runtimeForcedVariation
    validProjectConfig.setForcedVariation(experiment.getKey(), whitelistedUserId, expectedVariation.getKey());
    // no attributes provided for a experiment that has an audience
    assertThat(decisionService.getVariation(experiment, whitelistedUserId, Collections.<String, String>emptyMap()), is(expectedVariation));
    // verify(decisionService).getForcedVariation(experiment.getKey(), whitelistedUserId);
    verify(decisionService, never()).getStoredVariation(eq(experiment), any(UserProfile.class));
    assertEquals(decisionService.getWhitelistedVariation(experiment, whitelistedUserId), whitelistVariation);
    assertTrue(validProjectConfig.setForcedVariation(experiment.getKey(), whitelistedUserId, null));
    assertNull(validProjectConfig.getForcedVariation(experiment.getKey(), whitelistedUserId));
    assertThat(decisionService.getVariation(experiment, whitelistedUserId, Collections.<String, String>emptyMap()), is(whitelistVariation));
}
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 99 with Variation

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

the class DecisionServiceTest method getVariationForFeatureInRolloutReturnsVariationWhenUserFailsTrafficInRuleButWouldPassForAnotherRuleAndPassesInEveryoneElse.

/**
 * Verify that {@link DecisionService#getVariationForFeatureInRollout(FeatureFlag, String, Map)}
 * returns the variation of "Everyone Else" rule
 * when the user passes targeting for a rule, but was failed the traffic allocation for that rule,
 * and is bucketed successfully into the "Everyone Else" rule.
 * Fallback bucketing should not evaluate any other audiences.
 *  Even though the user would satisfy a later rollout rule, they are never evaluated for it or bucketed into it.
 */
@Test
public void getVariationForFeatureInRolloutReturnsVariationWhenUserFailsTrafficInRuleButWouldPassForAnotherRuleAndPassesInEveryoneElse() {
    Bucketer mockBucketer = mock(Bucketer.class);
    Rollout rollout = ROLLOUT_2;
    Experiment englishCitizensRule = rollout.getExperiments().get(2);
    Variation englishCitizenVariation = englishCitizensRule.getVariations().get(0);
    Experiment everyoneElseRule = rollout.getExperiments().get(rollout.getExperiments().size() - 1);
    Variation expectedVariation = everyoneElseRule.getVariations().get(0);
    when(mockBucketer.bucket(any(Experiment.class), anyString())).thenReturn(null);
    when(mockBucketer.bucket(eq(everyoneElseRule), anyString())).thenReturn(expectedVariation);
    when(mockBucketer.bucket(eq(englishCitizensRule), anyString())).thenReturn(englishCitizenVariation);
    DecisionService decisionService = new DecisionService(mockBucketer, mockErrorHandler, v4ProjectConfig, null);
    FeatureDecision featureDecision = decisionService.getVariationForFeatureInRollout(FEATURE_FLAG_MULTI_VARIATE_FEATURE, genericUserId, ProjectConfigTestUtils.createMapOfObjects(ProjectConfigTestUtils.createListOfObjects(ATTRIBUTE_HOUSE_KEY, ATTRIBUTE_NATIONALITY_KEY), ProjectConfigTestUtils.createListOfObjects(AUDIENCE_GRYFFINDOR_VALUE, AUDIENCE_ENGLISH_CITIZENS_VALUE)));
    assertEquals(expectedVariation, featureDecision.variation);
    assertEquals(FeatureDecision.DecisionSource.ROLLOUT, featureDecision.decisionSource);
    // verify user is only bucketed once for everyone else rule
    verify(mockBucketer, times(2)).bucket(any(Experiment.class), anyString());
}
Also used : Experiment(com.optimizely.ab.config.Experiment) Rollout(com.optimizely.ab.config.Rollout) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Aggregations

Variation (com.optimizely.ab.config.Variation)99 Experiment (com.optimizely.ab.config.Experiment)91 Test (org.junit.Test)81 Matchers.anyString (org.mockito.Matchers.anyString)50 LogEvent (com.optimizely.ab.event.LogEvent)44 HashMap (java.util.HashMap)42 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)25 EventType (com.optimizely.ab.config.EventType)22 Map (java.util.Map)18 ImmutableMap (com.google.common.collect.ImmutableMap)15 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)15 Attribute (com.optimizely.ab.config.Attribute)13 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)12 ArrayList (java.util.ArrayList)10 Bucketer (com.optimizely.ab.bucketing.Bucketer)9 ProjectConfig (com.optimizely.ab.config.ProjectConfig)9 TrafficAllocation (com.optimizely.ab.config.TrafficAllocation)8 Rollout (com.optimizely.ab.config.Rollout)7 NoOpErrorHandler (com.optimizely.ab.error.NoOpErrorHandler)7 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)7