Search in sources :

Example 96 with Experiment

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

the class DecisionServiceTest method getVariationForFeatureInRolloutReturnsVariationWhenUserFailsTargetingInPreviousRulesButPassesRule3.

/**
 * Verify that {@link DecisionService#getVariationForFeatureInRollout(FeatureFlag, String, Map)}
 * returns the variation of "English Citizens" rule
 * when the user fails targeting for previous rules, but passes targeting and traffic for Rule 3.
 */
@Test
public void getVariationForFeatureInRolloutReturnsVariationWhenUserFailsTargetingInPreviousRulesButPassesRule3() {
    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 everyoneElseVariation = everyoneElseRule.getVariations().get(0);
    when(mockBucketer.bucket(any(Experiment.class), anyString())).thenReturn(null);
    when(mockBucketer.bucket(eq(everyoneElseRule), anyString())).thenReturn(everyoneElseVariation);
    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, Collections.singletonMap(ATTRIBUTE_NATIONALITY_KEY, AUDIENCE_ENGLISH_CITIZENS_VALUE));
    assertEquals(englishCitizenVariation, featureDecision.variation);
    assertEquals(FeatureDecision.DecisionSource.ROLLOUT, featureDecision.decisionSource);
    // verify user is only bucketed once for everyone else rule
    verify(mockBucketer, times(1)).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)

Example 97 with Experiment

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

the class DecisionServiceTest method getVariationForFeatureInRolloutReturnsVariationWhenUserFailsAllAudienceButSatisfiesTraffic.

/**
 * Verify that {@link DecisionService#getVariationForFeatureInRollout(FeatureFlag, String, Map)}
 * returns the variation of "Everyone Else" rule
 * when the user fails targeting for all rules, but is bucketed into the "Everyone Else" rule.
 */
@Test
public void getVariationForFeatureInRolloutReturnsVariationWhenUserFailsAllAudienceButSatisfiesTraffic() {
    Bucketer mockBucketer = mock(Bucketer.class);
    Rollout rollout = ROLLOUT_2;
    Experiment everyoneElseRule = rollout.getExperiments().get(rollout.getExperiments().size() - 1);
    Variation expectedVariation = everyoneElseRule.getVariations().get(0);
    when(mockBucketer.bucket(eq(everyoneElseRule), anyString())).thenReturn(expectedVariation);
    DecisionService decisionService = new DecisionService(mockBucketer, mockErrorHandler, v4ProjectConfig, null);
    FeatureDecision featureDecision = decisionService.getVariationForFeatureInRollout(FEATURE_FLAG_MULTI_VARIATE_FEATURE, genericUserId, Collections.<String, String>emptyMap());
    assertEquals(expectedVariation, featureDecision.variation);
    assertEquals(FeatureDecision.DecisionSource.ROLLOUT, featureDecision.decisionSource);
    // verify user is only bucketed once for everyone else rule
    verify(mockBucketer, times(1)).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)

Example 98 with Experiment

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

the class DecisionServiceTest method getWhitelistedWithInvalidVariation.

/**
 * Verify that {@link DecisionService#getWhitelistedVariation(Experiment, String)} returns null
 * when an invalid variation key is found in the forced variations mapping.
 */
@Test
public void getWhitelistedWithInvalidVariation() throws Exception {
    String userId = "testUser1";
    String invalidVariationKey = "invalidVarKey";
    Bucketer bucketer = new Bucketer(validProjectConfig);
    DecisionService decisionService = new DecisionService(bucketer, mockErrorHandler, validProjectConfig, null);
    List<Variation> variations = Collections.singletonList(new Variation("1", "var1"));
    List<TrafficAllocation> trafficAllocations = Collections.singletonList(new TrafficAllocation("1", 1000));
    Map<String, String> userIdToVariationKeyMap = Collections.singletonMap(userId, invalidVariationKey);
    Experiment experiment = new Experiment("1234", "exp_key", "Running", "1", Collections.<String>emptyList(), variations, userIdToVariationKeyMap, trafficAllocations);
    logbackVerifier.expectMessage(Level.ERROR, "Variation \"" + invalidVariationKey + "\" is not in the datafile. Not activating user \"" + userId + "\".");
    assertNull(decisionService.getWhitelistedVariation(experiment, userId));
}
Also used : TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) 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 Experiment

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

the class DecisionServiceTest method getStoredVariationLogsWhenLookupReturnsNull.

/**
 * Verify that {@link DecisionService#getStoredVariation(Experiment, UserProfile)} returns null and logs properly
 * when there is no stored variation for that user in that {@link Experiment} in the {@link UserProfileService}.
 */
@Test
public void getStoredVariationLogsWhenLookupReturnsNull() throws Exception {
    final Experiment experiment = noAudienceProjectConfig.getExperiments().get(0);
    Bucketer bucketer = new Bucketer(noAudienceProjectConfig);
    UserProfileService userProfileService = mock(UserProfileService.class);
    UserProfile userProfile = new UserProfile(userProfileId, Collections.<String, Decision>emptyMap());
    when(userProfileService.lookup(userProfileId)).thenReturn(userProfile.toMap());
    DecisionService decisionService = new DecisionService(bucketer, mockErrorHandler, noAudienceProjectConfig, userProfileService);
    logbackVerifier.expectMessage(Level.INFO, "No previously activated variation of experiment " + "\"" + experiment.getKey() + "\" for user \"" + userProfileId + "\" found in user profile.");
    assertNull(decisionService.getStoredVariation(experiment, userProfile));
}
Also used : Experiment(com.optimizely.ab.config.Experiment) Test(org.junit.Test)

Example 100 with Experiment

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

the class DecisionServiceTest method getVariationForFeatureInRolloutReturnsVariationWhenUserFailsTrafficInRuleAndPassesInEveryoneElse.

/**
 * 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.
 */
@Test
public void getVariationForFeatureInRolloutReturnsVariationWhenUserFailsTrafficInRuleAndPassesInEveryoneElse() {
    Bucketer mockBucketer = mock(Bucketer.class);
    Rollout rollout = ROLLOUT_2;
    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);
    DecisionService decisionService = new DecisionService(mockBucketer, mockErrorHandler, v4ProjectConfig, null);
    FeatureDecision featureDecision = decisionService.getVariationForFeatureInRollout(FEATURE_FLAG_MULTI_VARIATE_FEATURE, genericUserId, Collections.singletonMap(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_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

Experiment (com.optimizely.ab.config.Experiment)125 Test (org.junit.Test)108 Variation (com.optimizely.ab.config.Variation)77 Matchers.anyString (org.mockito.Matchers.anyString)44 LogEvent (com.optimizely.ab.event.LogEvent)42 HashMap (java.util.HashMap)36 EventType (com.optimizely.ab.config.EventType)24 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)23 ProjectConfig (com.optimizely.ab.config.ProjectConfig)19 Map (java.util.Map)14 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 Attribute (com.optimizely.ab.config.Attribute)11 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)11 Bucketer (com.optimizely.ab.bucketing.Bucketer)9 ExhaustiveTest (com.optimizely.ab.categories.ExhaustiveTest)9 Rollout (com.optimizely.ab.config.Rollout)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 DecisionService (com.optimizely.ab.bucketing.DecisionService)8 ArrayList (java.util.ArrayList)8