Skip to content

Commit c366dc6

Browse files
committed
add test to make sure we return rollout variation when we do not get a varaition for the experiment
1 parent c255e9f commit c366dc6

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

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

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,69 @@ public void getVariationForFeatureReturnsVariationFromExperimentBeforeRollout()
332332
);
333333
}
334334

335-
//========== getVariationForFeatureInRollout tests ==========//
335+
/**
336+
* Verify that when getting a {@link Variation} for a {@link FeatureFlag} in
337+
* {@link DecisionService#getVariationForFeature(FeatureFlag, String, Map)},
338+
* check first if the user is bucketed to an {@link Rollout}
339+
* if the user is not bucketed to an experiment.
340+
*/
341+
@Test
342+
public void getVariationForFeatureReturnsVariationFromRolloutWhenExperimentFails() {
343+
FeatureFlag featureFlag = FEATURE_FLAG_MULTI_VARIATE_FEATURE;
344+
Experiment featureExperiment = v4ProjectConfig.getExperimentIdMapping().get(featureFlag.getExperimentIds().get(0));
345+
assertNotNull(featureExperiment);
346+
Rollout featureRollout = v4ProjectConfig.getRolloutIdMapping().get(featureFlag.getRolloutId());
347+
Variation experimentVariation = featureExperiment.getVariations().get(0);
348+
Variation rolloutVariation = featureRollout.getExperiments().get(0).getVariations().get(0);
349+
350+
DecisionService decisionService = spy(new DecisionService(
351+
mock(Bucketer.class),
352+
mockErrorHandler,
353+
v4ProjectConfig,
354+
null
355+
)
356+
);
357+
358+
// return variation for experiment
359+
doReturn(null)
360+
.when(decisionService).getVariation(
361+
eq(featureExperiment),
362+
anyString(),
363+
anyMapOf(String.class, String.class)
364+
);
365+
366+
// return variation for rollout
367+
doReturn(rolloutVariation)
368+
.when(decisionService).getVariationForFeatureInRollout(
369+
eq(featureFlag),
370+
anyString(),
371+
anyMapOf(String.class, String.class)
372+
);
373+
374+
// make sure we get the right variation back
375+
assertEquals(rolloutVariation,
376+
decisionService.getVariationForFeature(featureFlag,
377+
genericUserId,
378+
Collections.<String, String>emptyMap()
379+
)
380+
);
381+
382+
// make sure we do not even check for rollout bucketing
383+
verify(decisionService,times(1)).getVariationForFeatureInRollout(
384+
any(FeatureFlag.class),
385+
anyString(),
386+
anyMapOf(String.class, String.class)
387+
);
388+
389+
// make sure we ask for experiment bucketing once
390+
verify(decisionService, times(1)).getVariation(
391+
any(Experiment.class),
392+
anyString(),
393+
anyMapOf(String.class, String.class)
394+
);
395+
}
396+
397+
//========== getVariationForFeatureInRollout tests ==========//
336398

337399
/**
338400
* Verify that {@link DecisionService#getVariationForFeatureInRollout(FeatureFlag, String, Map)}

0 commit comments

Comments
 (0)