|
49 | 49 | import static com.optimizely.ab.config.ValidProjectConfigV4.AUDIENCE_ENGLISH_CITIZENS_VALUE; |
50 | 50 | import static com.optimizely.ab.config.ValidProjectConfigV4.AUDIENCE_GRYFFINDOR_VALUE; |
51 | 51 | 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; |
54 | 52 | import static com.optimizely.ab.config.ValidProjectConfigV4.ROLLOUT_2; |
55 | 53 | import static org.hamcrest.CoreMatchers.is; |
56 | 54 | import static org.hamcrest.MatcherAssert.assertThat; |
57 | 55 | import static org.junit.Assert.assertEquals; |
| 56 | +import static org.junit.Assert.assertNotNull; |
58 | 57 | import static org.junit.Assert.assertNull; |
59 | 58 | import static org.junit.Assert.assertTrue; |
60 | 59 | import static org.junit.Assert.assertFalse; |
@@ -374,7 +373,7 @@ public void getVariationForFeatureReturnsNullWhenItGetsNoVariationsForExperiment |
374 | 373 | */ |
375 | 374 | @Test |
376 | 375 | @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT") |
377 | | - public void getVariationForFeatureReturnsVariationReturnedFromGetVarition() { |
| 376 | + public void getVariationForFeatureReturnsVariationReturnedFromGetVariation() { |
378 | 377 | FeatureFlag spyFeatureFlag = spy(ValidProjectConfigV4.FEATURE_FLAG_MUTEX_GROUP_FEATURE); |
379 | 378 |
|
380 | 379 | DecisionService spyDecisionService = spy(new DecisionService( |
@@ -407,6 +406,69 @@ public void getVariationForFeatureReturnsVariationReturnedFromGetVarition() { |
407 | 406 | verify(spyFeatureFlag, never()).getKey(); |
408 | 407 | } |
409 | 408 |
|
| 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 | + |
410 | 472 | //========== getVariationForFeatureInRollout tests ==========// |
411 | 473 |
|
412 | 474 | /** |
|
0 commit comments