Search in sources :

Example 91 with Variation

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

the class DecisionServiceTest method getVariationOnNonRunningExperimentWithForcedVariation.

/**
 * Verify that {@link DecisionService#getVariation(Experiment, String, Map)}
 * gives a null variation on a Experiment that is not running. Set the forced variation.
 * And, test to make sure that after setting forced variation, the getVariation still returns
 * null.
 */
@Test
public void getVariationOnNonRunningExperimentWithForcedVariation() {
    Experiment experiment = validProjectConfig.getExperiments().get(1);
    assertFalse(experiment.isRunning());
    Variation variation = experiment.getVariations().get(0);
    Bucketer bucketer = new Bucketer(validProjectConfig);
    DecisionService decisionService = spy(new DecisionService(bucketer, mockErrorHandler, validProjectConfig, null));
    // ensure that the not running variation returns null with no forced variation set.
    assertNull(decisionService.getVariation(experiment, "userId", Collections.<String, String>emptyMap()));
    // we call getVariation 3 times on an experiment that is not running.
    logbackVerifier.expectMessage(Level.INFO, "Experiment \"etag2\" is not running.", times(3));
    // set a forced variation on the user that got back null
    assertTrue(validProjectConfig.setForcedVariation(experiment.getKey(), "userId", variation.getKey()));
    // ensure that a user with a forced variation set
    // still gets back a null variation if the variation is not running.
    assertNull(decisionService.getVariation(experiment, "userId", Collections.<String, String>emptyMap()));
    // set the forced variation back to null
    assertTrue(validProjectConfig.setForcedVariation(experiment.getKey(), "userId", null));
    // test one more time that the getVariation returns null for the experiment that is not running.
    assertNull(decisionService.getVariation(experiment, "userId", 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 92 with Variation

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

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

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

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

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