Skip to content

Commit bc2c95e

Browse files
committed
add test to make sure feature variable bucketing checks experiment first
1 parent 4f60179 commit bc2c95e

File tree

1 file changed

+65
-3
lines changed

1 file changed

+65
-3
lines changed

core-api/src/test/java/com/optimizely/ab/bucketing/DecisionServiceTest.java

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,11 @@
4949
import static com.optimizely.ab.config.ValidProjectConfigV4.AUDIENCE_ENGLISH_CITIZENS_VALUE;
5050
import static com.optimizely.ab.config.ValidProjectConfigV4.AUDIENCE_GRYFFINDOR_VALUE;
5151
import static com.optimizely.ab.config.ValidProjectConfigV4.FEATURE_FLAG_MULTI_VARIATE_FEATURE;
52-
import static com.optimizely.ab.config.ValidProjectConfigV4.FEATURE_FLAG_SINGLE_VARIABLE_STRING;
53-
import static com.optimizely.ab.config.ValidProjectConfigV4.ROLLOUT_1;
5452
import static com.optimizely.ab.config.ValidProjectConfigV4.ROLLOUT_2;
5553
import static org.hamcrest.CoreMatchers.is;
5654
import static org.hamcrest.MatcherAssert.assertThat;
5755
import static org.junit.Assert.assertEquals;
56+
import static org.junit.Assert.assertNotNull;
5857
import static org.junit.Assert.assertNull;
5958
import static org.junit.Assert.assertTrue;
6059
import static org.junit.Assert.assertFalse;
@@ -374,7 +373,7 @@ public void getVariationForFeatureReturnsNullWhenItGetsNoVariationsForExperiment
374373
*/
375374
@Test
376375
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
377-
public void getVariationForFeatureReturnsVariationReturnedFromGetVarition() {
376+
public void getVariationForFeatureReturnsVariationReturnedFromGetVariation() {
378377
FeatureFlag spyFeatureFlag = spy(ValidProjectConfigV4.FEATURE_FLAG_MUTEX_GROUP_FEATURE);
379378

380379
DecisionService spyDecisionService = spy(new DecisionService(
@@ -407,6 +406,69 @@ public void getVariationForFeatureReturnsVariationReturnedFromGetVarition() {
407406
verify(spyFeatureFlag, never()).getKey();
408407
}
409408

409+
/**
410+
* Verify that when getting a {@link Variation} for a {@link FeatureFlag} in
411+
* {@link DecisionService#getVariationForFeature(FeatureFlag, String, Map)},
412+
* check first if the user is bucketed to an {@link Experiment}
413+
* then check if the user is not bucketed to an experiment,
414+
* check for a {@link Rollout}.
415+
*/
416+
@Test
417+
public void getVariationForFeatureReturnsVariationFromExperimentBeforeRollout() {
418+
FeatureFlag featureFlag = FEATURE_FLAG_MULTI_VARIATE_FEATURE;
419+
Experiment featureExperiment = v4ProjectConfig.getExperimentIdMapping().get(featureFlag.getExperimentIds().get(0));
420+
assertNotNull(featureExperiment);
421+
Rollout featureRollout = v4ProjectConfig.getRolloutIdMapping().get(featureFlag.getRolloutId());
422+
Variation experimentVariation = featureExperiment.getVariations().get(0);
423+
Variation rolloutVariation = featureRollout.getExperiments().get(0).getVariations().get(0);
424+
425+
DecisionService decisionService = spy(new DecisionService(
426+
mock(Bucketer.class),
427+
mockErrorHandler,
428+
v4ProjectConfig,
429+
null
430+
)
431+
);
432+
433+
// return variation for experiment
434+
doReturn(experimentVariation)
435+
.when(decisionService).getVariation(
436+
eq(featureExperiment),
437+
anyString(),
438+
anyMapOf(String.class, String.class)
439+
);
440+
441+
// return variation for rollout
442+
doReturn(rolloutVariation)
443+
.when(decisionService).getVariationForFeatureInRollout(
444+
eq(featureFlag),
445+
anyString(),
446+
anyMapOf(String.class, String.class)
447+
);
448+
449+
// make sure we get the right variation back
450+
assertEquals(experimentVariation,
451+
decisionService.getVariationForFeature(featureFlag,
452+
genericUserId,
453+
Collections.<String, String>emptyMap()
454+
)
455+
);
456+
457+
// make sure we do not even check for rollout bucketing
458+
verify(decisionService, never()).getVariationForFeatureInRollout(
459+
any(FeatureFlag.class),
460+
anyString(),
461+
anyMapOf(String.class, String.class)
462+
);
463+
464+
// make sure we ask for experiment bucketing once
465+
verify(decisionService, times(1)).getVariation(
466+
any(Experiment.class),
467+
anyString(),
468+
anyMapOf(String.class, String.class)
469+
);
470+
}
471+
410472
//========== getVariationForFeatureInRollout tests ==========//
411473

412474
/**

0 commit comments

Comments
 (0)