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();
}
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));
}
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() + "\".");
}
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);
}
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.");
}
Aggregations