|
48 | 48 | import static com.optimizely.ab.config.ValidProjectConfigV4.AUDIENCE_ENGLISH_CITIZENS_VALUE; |
49 | 49 | import static com.optimizely.ab.config.ValidProjectConfigV4.AUDIENCE_GRYFFINDOR_VALUE; |
50 | 50 | 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; |
53 | 51 | import static com.optimizely.ab.config.ValidProjectConfigV4.ROLLOUT_2; |
54 | 52 | import static org.hamcrest.CoreMatchers.is; |
55 | 53 | import static org.hamcrest.MatcherAssert.assertThat; |
56 | 54 | import static org.junit.Assert.assertEquals; |
| 55 | +import static org.junit.Assert.assertNotNull; |
57 | 56 | import static org.junit.Assert.assertNull; |
58 | 57 | import static org.mockito.Matchers.any; |
59 | 58 | import static org.mockito.Matchers.anyMapOf; |
@@ -237,7 +236,7 @@ public void getVariationForFeatureReturnsNullWhenItGetsNoVariationsForExperiment |
237 | 236 | */ |
238 | 237 | @Test |
239 | 238 | @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT") |
240 | | - public void getVariationForFeatureReturnsVariationReturnedFromGetVarition() { |
| 239 | + public void getVariationForFeatureReturnsVariationReturnedFromGetVariation() { |
241 | 240 | FeatureFlag spyFeatureFlag = spy(ValidProjectConfigV4.FEATURE_FLAG_MUTEX_GROUP_FEATURE); |
242 | 241 |
|
243 | 242 | DecisionService spyDecisionService = spy(new DecisionService( |
@@ -270,6 +269,69 @@ public void getVariationForFeatureReturnsVariationReturnedFromGetVarition() { |
270 | 269 | verify(spyFeatureFlag, never()).getKey(); |
271 | 270 | } |
272 | 271 |
|
| 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 | + |
273 | 335 | //========== getVariationForFeatureInRollout tests ==========// |
274 | 336 |
|
275 | 337 | /** |
|
0 commit comments