Search in sources :

Example 6 with FeatureFlag

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

the class DecisionServiceTest method getVariationForFeatureReturnsNullWhenItGetsNoVariationsForExperimentsAndRollouts.

/**
 * Verify that {@link DecisionService#getVariationForFeature(FeatureFlag, String, Map)}
 * returns null when the user is not bucketed into any experiments or rollouts for the {@link FeatureFlag}.
 */
@Test
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
public void getVariationForFeatureReturnsNullWhenItGetsNoVariationsForExperimentsAndRollouts() {
    FeatureFlag spyFeatureFlag = spy(FEATURE_FLAG_MULTI_VARIATE_FEATURE);
    DecisionService spyDecisionService = spy(new DecisionService(mock(Bucketer.class), mockErrorHandler, validProjectConfig, null));
    // do not bucket to any experiments
    doReturn(null).when(spyDecisionService).getVariation(any(Experiment.class), anyString(), anyMapOf(String.class, String.class));
    // do not bucket to any rollouts
    doReturn(new FeatureDecision(null, null, null)).when(spyDecisionService).getVariationForFeatureInRollout(any(FeatureFlag.class), anyString(), anyMapOf(String.class, String.class));
    // try to get a variation back from the decision service for the feature flag
    FeatureDecision featureDecision = spyDecisionService.getVariationForFeature(spyFeatureFlag, genericUserId, Collections.<String, String>emptyMap());
    assertNull(featureDecision.variation);
    assertNull(featureDecision.decisionSource);
    logbackVerifier.expectMessage(Level.INFO, "The user \"" + genericUserId + "\" was not bucketed into a rollout for feature flag \"" + FEATURE_MULTI_VARIATE_FEATURE_KEY + "\".");
    verify(spyFeatureFlag, times(2)).getExperimentIds();
    verify(spyFeatureFlag, times(1)).getKey();
}
Also used : Experiment(com.optimizely.ab.config.Experiment) FeatureFlag(com.optimizely.ab.config.FeatureFlag) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 7 with FeatureFlag

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

the class DecisionServiceTest method getVariationForFeatureReturnsVariationFromExperimentBeforeRollout.

/**
 * Verify that when getting a {@link Variation} for a {@link FeatureFlag} in
 * {@link DecisionService#getVariationForFeature(FeatureFlag, String, Map)},
 * check first if the user is bucketed to an {@link Experiment}
 * then check if the user is not bucketed to an experiment,
 * check for a {@link Rollout}.
 */
@Test
public void getVariationForFeatureReturnsVariationFromExperimentBeforeRollout() {
    FeatureFlag featureFlag = FEATURE_FLAG_MULTI_VARIATE_FEATURE;
    Experiment featureExperiment = v4ProjectConfig.getExperimentIdMapping().get(featureFlag.getExperimentIds().get(0));
    assertNotNull(featureExperiment);
    Rollout featureRollout = v4ProjectConfig.getRolloutIdMapping().get(featureFlag.getRolloutId());
    Variation experimentVariation = featureExperiment.getVariations().get(0);
    Experiment rolloutExperiment = featureRollout.getExperiments().get(0);
    Variation rolloutVariation = rolloutExperiment.getVariations().get(0);
    DecisionService decisionService = spy(new DecisionService(mock(Bucketer.class), mockErrorHandler, v4ProjectConfig, null));
    // return variation for experiment
    doReturn(experimentVariation).when(decisionService).getVariation(eq(featureExperiment), anyString(), anyMapOf(String.class, String.class));
    // return variation for rollout
    doReturn(new FeatureDecision(rolloutExperiment, rolloutVariation, FeatureDecision.DecisionSource.ROLLOUT)).when(decisionService).getVariationForFeatureInRollout(eq(featureFlag), anyString(), anyMapOf(String.class, String.class));
    // make sure we get the right variation back
    FeatureDecision featureDecision = decisionService.getVariationForFeature(featureFlag, genericUserId, Collections.<String, String>emptyMap());
    assertEquals(experimentVariation, featureDecision.variation);
    assertEquals(FeatureDecision.DecisionSource.EXPERIMENT, featureDecision.decisionSource);
    // make sure we do not even check for rollout bucketing
    verify(decisionService, never()).getVariationForFeatureInRollout(any(FeatureFlag.class), anyString(), anyMapOf(String.class, String.class));
    // make sure we ask for experiment bucketing once
    verify(decisionService, times(1)).getVariation(any(Experiment.class), anyString(), anyMapOf(String.class, String.class));
}
Also used : Experiment(com.optimizely.ab.config.Experiment) FeatureFlag(com.optimizely.ab.config.FeatureFlag) Matchers.anyString(org.mockito.Matchers.anyString) Rollout(com.optimizely.ab.config.Rollout) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 8 with FeatureFlag

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

the class DecisionServiceTest method getVariationForFeatureReturnsVariationFromRolloutWhenExperimentFails.

/**
 * Verify that when getting a {@link Variation} for a {@link FeatureFlag} in
 * {@link DecisionService#getVariationForFeature(FeatureFlag, String, Map)},
 * check first if the user is bucketed to an {@link Rollout}
 * if the user is not bucketed to an experiment.
 */
@Test
public void getVariationForFeatureReturnsVariationFromRolloutWhenExperimentFails() {
    FeatureFlag featureFlag = FEATURE_FLAG_MULTI_VARIATE_FEATURE;
    Experiment featureExperiment = v4ProjectConfig.getExperimentIdMapping().get(featureFlag.getExperimentIds().get(0));
    assertNotNull(featureExperiment);
    Rollout featureRollout = v4ProjectConfig.getRolloutIdMapping().get(featureFlag.getRolloutId());
    Experiment rolloutExperiment = featureRollout.getExperiments().get(0);
    Variation rolloutVariation = rolloutExperiment.getVariations().get(0);
    DecisionService decisionService = spy(new DecisionService(mock(Bucketer.class), mockErrorHandler, v4ProjectConfig, null));
    // return variation for experiment
    doReturn(null).when(decisionService).getVariation(eq(featureExperiment), anyString(), anyMapOf(String.class, String.class));
    // return variation for rollout
    doReturn(new FeatureDecision(rolloutExperiment, rolloutVariation, FeatureDecision.DecisionSource.ROLLOUT)).when(decisionService).getVariationForFeatureInRollout(eq(featureFlag), anyString(), anyMapOf(String.class, String.class));
    // make sure we get the right variation back
    FeatureDecision featureDecision = decisionService.getVariationForFeature(featureFlag, genericUserId, Collections.<String, String>emptyMap());
    assertEquals(rolloutVariation, featureDecision.variation);
    assertEquals(FeatureDecision.DecisionSource.ROLLOUT, featureDecision.decisionSource);
    // make sure we do not even check for rollout bucketing
    verify(decisionService, times(1)).getVariationForFeatureInRollout(any(FeatureFlag.class), anyString(), anyMapOf(String.class, String.class));
    // make sure we ask for experiment bucketing once
    verify(decisionService, times(1)).getVariation(any(Experiment.class), anyString(), anyMapOf(String.class, String.class));
    logbackVerifier.expectMessage(Level.INFO, "The user \"" + genericUserId + "\" was bucketed into a rollout for feature flag \"" + featureFlag.getKey() + "\".");
}
Also used : Experiment(com.optimizely.ab.config.Experiment) FeatureFlag(com.optimizely.ab.config.FeatureFlag) Matchers.anyString(org.mockito.Matchers.anyString) Rollout(com.optimizely.ab.config.Rollout) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 9 with FeatureFlag

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

the class DecisionServiceTest method getVariationForRolloutWithBucketingId.

/**
 *    Verify that {@link DecisionService#getVariationForFeatureInRollout(FeatureFlag, String, Map)}
 *     uses bucketing ID to bucket the user into rollouts.
 */
@Test
public void getVariationForRolloutWithBucketingId() {
    Experiment rolloutRuleExperiment = ROLLOUT_3_EVERYONE_ELSE_RULE;
    Variation rolloutVariation = ROLLOUT_3_EVERYONE_ELSE_RULE_ENABLED_VARIATION;
    FeatureFlag featureFlag = FEATURE_FLAG_SINGLE_VARIABLE_INTEGER;
    String bucketingId = "user_bucketing_id";
    String userId = "user_id";
    Map<String, String> attributes = new HashMap<String, String>();
    attributes.put(DecisionService.BUCKETING_ATTRIBUTE, bucketingId);
    Bucketer bucketer = mock(Bucketer.class);
    when(bucketer.bucket(rolloutRuleExperiment, userId)).thenReturn(null);
    when(bucketer.bucket(rolloutRuleExperiment, bucketingId)).thenReturn(rolloutVariation);
    DecisionService decisionService = spy(new DecisionService(bucketer, mockErrorHandler, v4ProjectConfig, null));
    FeatureDecision expectedFeatureDecision = new FeatureDecision(rolloutRuleExperiment, rolloutVariation, FeatureDecision.DecisionSource.ROLLOUT);
    FeatureDecision featureDecision = decisionService.getVariationForFeature(featureFlag, userId, attributes);
    assertEquals(expectedFeatureDecision, featureDecision);
}
Also used : HashMap(java.util.HashMap) Experiment(com.optimizely.ab.config.Experiment) FeatureFlag(com.optimizely.ab.config.FeatureFlag) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 10 with FeatureFlag

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

the class DecisionServiceTest method getVariationForFeatureInRolloutReturnsNullWhenFeatureIsNotAttachedToRollout.

// ========== getVariationForFeatureInRollout tests ==========//
/**
 * Verify that {@link DecisionService#getVariationForFeatureInRollout(FeatureFlag, String, Map)}
 * returns null when trying to bucket a user into a {@link FeatureFlag}
 * that does not have a {@link Rollout} attached.
 */
@Test
public void getVariationForFeatureInRolloutReturnsNullWhenFeatureIsNotAttachedToRollout() {
    FeatureFlag mockFeatureFlag = mock(FeatureFlag.class);
    when(mockFeatureFlag.getRolloutId()).thenReturn("");
    String featureKey = "featureKey";
    when(mockFeatureFlag.getKey()).thenReturn(featureKey);
    DecisionService decisionService = new DecisionService(mock(Bucketer.class), mockErrorHandler, validProjectConfig, null);
    FeatureDecision featureDecision = decisionService.getVariationForFeatureInRollout(mockFeatureFlag, genericUserId, Collections.<String, String>emptyMap());
    assertNull(featureDecision.variation);
    assertNull(featureDecision.decisionSource);
    logbackVerifier.expectMessage(Level.INFO, "The feature flag \"" + featureKey + "\" is not used in a rollout.");
}
Also used : FeatureFlag(com.optimizely.ab.config.FeatureFlag) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

FeatureFlag (com.optimizely.ab.config.FeatureFlag)19 Experiment (com.optimizely.ab.config.Experiment)9 Test (org.junit.Test)9 Matchers.anyString (org.mockito.Matchers.anyString)9 LiveVariable (com.optimizely.ab.config.LiveVariable)8 Rollout (com.optimizely.ab.config.Rollout)6 ArrayList (java.util.ArrayList)5 Attribute (com.optimizely.ab.config.Attribute)4 EventType (com.optimizely.ab.config.EventType)4 Group (com.optimizely.ab.config.Group)4 ProjectConfig (com.optimizely.ab.config.ProjectConfig)4 Audience (com.optimizely.ab.config.audience.Audience)4 Variation (com.optimizely.ab.config.Variation)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 List (java.util.List)3 FeatureDecision (com.optimizely.ab.bucketing.FeatureDecision)2 UserAttribute (com.optimizely.ab.config.audience.UserAttribute)2 Type (java.lang.reflect.Type)2 HashMap (java.util.HashMap)2 JSONObject (org.json.JSONObject)2