Skip to content

Commit c255e9f

Browse files
committed
add test to make sure feature variable bucketing checks experiment first
1 parent b65274c commit c255e9f

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
@@ -48,12 +48,11 @@
4848
import static com.optimizely.ab.config.ValidProjectConfigV4.AUDIENCE_ENGLISH_CITIZENS_VALUE;
4949
import static com.optimizely.ab.config.ValidProjectConfigV4.AUDIENCE_GRYFFINDOR_VALUE;
5050
import static com.optimizely.ab.config.ValidProjectConfigV4.FEATURE_FLAG_MULTI_VARIATE_FEATURE;
51-
import static com.optimizely.ab.config.ValidProjectConfigV4.FEATURE_FLAG_SINGLE_VARIABLE_STRING;
52-
import static com.optimizely.ab.config.ValidProjectConfigV4.ROLLOUT_1;
5351
import static com.optimizely.ab.config.ValidProjectConfigV4.ROLLOUT_2;
5452
import static org.hamcrest.CoreMatchers.is;
5553
import static org.hamcrest.MatcherAssert.assertThat;
5654
import static org.junit.Assert.assertEquals;
55+
import static org.junit.Assert.assertNotNull;
5756
import static org.junit.Assert.assertNull;
5857
import static org.mockito.Matchers.any;
5958
import static org.mockito.Matchers.anyMapOf;
@@ -237,7 +236,7 @@ public void getVariationForFeatureReturnsNullWhenItGetsNoVariationsForExperiment
237236
*/
238237
@Test
239238
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
240-
public void getVariationForFeatureReturnsVariationReturnedFromGetVarition() {
239+
public void getVariationForFeatureReturnsVariationReturnedFromGetVariation() {
241240
FeatureFlag spyFeatureFlag = spy(ValidProjectConfigV4.FEATURE_FLAG_MUTEX_GROUP_FEATURE);
242241

243242
DecisionService spyDecisionService = spy(new DecisionService(
@@ -270,6 +269,69 @@ public void getVariationForFeatureReturnsVariationReturnedFromGetVarition() {
270269
verify(spyFeatureFlag, never()).getKey();
271270
}
272271

272+
/**
273+
* Verify that when getting a {@link Variation} for a {@link FeatureFlag} in
274+
* {@link DecisionService#getVariationForFeature(FeatureFlag, String, Map)},
275+
* check first if the user is bucketed to an {@link Experiment}
276+
* then check if the user is not bucketed to an experiment,
277+
* check for a {@link Rollout}.
278+
*/
279+
@Test
280+
public void getVariationForFeatureReturnsVariationFromExperimentBeforeRollout() {
281+
FeatureFlag featureFlag = FEATURE_FLAG_MULTI_VARIATE_FEATURE;
282+
Experiment featureExperiment = v4ProjectConfig.getExperimentIdMapping().get(featureFlag.getExperimentIds().get(0));
283+
assertNotNull(featureExperiment);
284+
Rollout featureRollout = v4ProjectConfig.getRolloutIdMapping().get(featureFlag.getRolloutId());
285+
Variation experimentVariation = featureExperiment.getVariations().get(0);
286+
Variation rolloutVariation = featureRollout.getExperiments().get(0).getVariations().get(0);
287+
288+
DecisionService decisionService = spy(new DecisionService(
289+
mock(Bucketer.class),
290+
mockErrorHandler,
291+
v4ProjectConfig,
292+
null
293+
)
294+
);
295+
296+
// return variation for experiment
297+
doReturn(experimentVariation)
298+
.when(decisionService).getVariation(
299+
eq(featureExperiment),
300+
anyString(),
301+
anyMapOf(String.class, String.class)
302+
);
303+
304+
// return variation for rollout
305+
doReturn(rolloutVariation)
306+
.when(decisionService).getVariationForFeatureInRollout(
307+
eq(featureFlag),
308+
anyString(),
309+
anyMapOf(String.class, String.class)
310+
);
311+
312+
// make sure we get the right variation back
313+
assertEquals(experimentVariation,
314+
decisionService.getVariationForFeature(featureFlag,
315+
genericUserId,
316+
Collections.<String, String>emptyMap()
317+
)
318+
);
319+
320+
// make sure we do not even check for rollout bucketing
321+
verify(decisionService, never()).getVariationForFeatureInRollout(
322+
any(FeatureFlag.class),
323+
anyString(),
324+
anyMapOf(String.class, String.class)
325+
);
326+
327+
// make sure we ask for experiment bucketing once
328+
verify(decisionService, times(1)).getVariation(
329+
any(Experiment.class),
330+
anyString(),
331+
anyMapOf(String.class, String.class)
332+
);
333+
}
334+
273335
//========== getVariationForFeatureInRollout tests ==========//
274336

275337
/**

0 commit comments

Comments
 (0)