Search in sources :

Example 11 with Experiment

use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.

the class DecisionServiceTest method getVariationForFeatureReturnsVariationFromRolloutWhenExperimentFails.

/**
 * Verify that when getting a {@link Variation} for a {@link FeatureFlag} in
 * {@link DecisionService#getVariationForFeature(FeatureFlag, String, Map)},
 * check first if the user is bucketed to an {@link Rollout}
 * if the user is not bucketed to an experiment.
 */
@Test
public void getVariationForFeatureReturnsVariationFromRolloutWhenExperimentFails() {
    FeatureFlag featureFlag = FEATURE_FLAG_MULTI_VARIATE_FEATURE;
    Experiment featureExperiment = v4ProjectConfig.getExperimentIdMapping().get(featureFlag.getExperimentIds().get(0));
    assertNotNull(featureExperiment);
    Rollout featureRollout = v4ProjectConfig.getRolloutIdMapping().get(featureFlag.getRolloutId());
    Experiment rolloutExperiment = featureRollout.getExperiments().get(0);
    Variation rolloutVariation = rolloutExperiment.getVariations().get(0);
    DecisionService decisionService = spy(new DecisionService(mock(Bucketer.class), mockErrorHandler, v4ProjectConfig, null));
    // return variation for experiment
    doReturn(null).when(decisionService).getVariation(eq(featureExperiment), anyString(), anyMapOf(String.class, String.class));
    // return variation for rollout
    doReturn(new FeatureDecision(rolloutExperiment, rolloutVariation, FeatureDecision.DecisionSource.ROLLOUT)).when(decisionService).getVariationForFeatureInRollout(eq(featureFlag), anyString(), anyMapOf(String.class, String.class));
    // make sure we get the right variation back
    FeatureDecision featureDecision = decisionService.getVariationForFeature(featureFlag, genericUserId, Collections.<String, String>emptyMap());
    assertEquals(rolloutVariation, featureDecision.variation);
    assertEquals(FeatureDecision.DecisionSource.ROLLOUT, featureDecision.decisionSource);
    // make sure we do not even check for rollout bucketing
    verify(decisionService, times(1)).getVariationForFeatureInRollout(any(FeatureFlag.class), anyString(), anyMapOf(String.class, String.class));
    // make sure we ask for experiment bucketing once
    verify(decisionService, times(1)).getVariation(any(Experiment.class), anyString(), anyMapOf(String.class, String.class));
    logbackVerifier.expectMessage(Level.INFO, "The user \"" + genericUserId + "\" was bucketed into a rollout for feature flag \"" + featureFlag.getKey() + "\".");
}
Also used : Experiment(com.optimizely.ab.config.Experiment) FeatureFlag(com.optimizely.ab.config.FeatureFlag) Matchers.anyString(org.mockito.Matchers.anyString) Rollout(com.optimizely.ab.config.Rollout) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 12 with Experiment

use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.

the class DecisionServiceTest method bucketLogsCorrectlyWhenUserProfileFailsToSave.

/**
 * Verify that {@link DecisionService#getVariation(Experiment, String, Map)} logs correctly
 * when a {@link UserProfileService} is present but fails to save an activation.
 */
@Test
public void bucketLogsCorrectlyWhenUserProfileFailsToSave() throws Exception {
    final Experiment experiment = noAudienceProjectConfig.getExperiments().get(0);
    final Variation variation = experiment.getVariations().get(0);
    Decision decision = new Decision(variation.getId());
    Bucketer bucketer = new Bucketer(noAudienceProjectConfig);
    UserProfileService userProfileService = mock(UserProfileService.class);
    doThrow(new Exception()).when(userProfileService).save(anyMapOf(String.class, Object.class));
    Map<String, Decision> experimentBucketMap = new HashMap<String, Decision>();
    experimentBucketMap.put(experiment.getId(), decision);
    UserProfile expectedUserProfile = new UserProfile(userProfileId, experimentBucketMap);
    UserProfile saveUserProfile = new UserProfile(userProfileId, new HashMap<String, Decision>());
    DecisionService decisionService = new DecisionService(bucketer, mockErrorHandler, noAudienceProjectConfig, userProfileService);
    decisionService.saveVariation(experiment, variation, saveUserProfile);
    logbackVerifier.expectMessage(Level.WARN, String.format("Failed to save variation \"%s\" of experiment \"%s\" for user \"" + userProfileId + "\".", variation.getId(), experiment.getId()));
    verify(userProfileService).save(eq(expectedUserProfile.toMap()));
}
Also used : HashMap(java.util.HashMap) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 13 with Experiment

use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.

the class DecisionServiceTest method getVariationForRolloutWithBucketingId.

/**
 *    Verify that {@link DecisionService#getVariationForFeatureInRollout(FeatureFlag, String, Map)}
 *     uses bucketing ID to bucket the user into rollouts.
 */
@Test
public void getVariationForRolloutWithBucketingId() {
    Experiment rolloutRuleExperiment = ROLLOUT_3_EVERYONE_ELSE_RULE;
    Variation rolloutVariation = ROLLOUT_3_EVERYONE_ELSE_RULE_ENABLED_VARIATION;
    FeatureFlag featureFlag = FEATURE_FLAG_SINGLE_VARIABLE_INTEGER;
    String bucketingId = "user_bucketing_id";
    String userId = "user_id";
    Map<String, String> attributes = new HashMap<String, String>();
    attributes.put(DecisionService.BUCKETING_ATTRIBUTE, bucketingId);
    Bucketer bucketer = mock(Bucketer.class);
    when(bucketer.bucket(rolloutRuleExperiment, userId)).thenReturn(null);
    when(bucketer.bucket(rolloutRuleExperiment, bucketingId)).thenReturn(rolloutVariation);
    DecisionService decisionService = spy(new DecisionService(bucketer, mockErrorHandler, v4ProjectConfig, null));
    FeatureDecision expectedFeatureDecision = new FeatureDecision(rolloutRuleExperiment, rolloutVariation, FeatureDecision.DecisionSource.ROLLOUT);
    FeatureDecision featureDecision = decisionService.getVariationForFeature(featureFlag, userId, attributes);
    assertEquals(expectedFeatureDecision, featureDecision);
}
Also used : HashMap(java.util.HashMap) Experiment(com.optimizely.ab.config.Experiment) FeatureFlag(com.optimizely.ab.config.FeatureFlag) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 14 with Experiment

use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.

the class DecisionServiceTest method bucketReturnsVariationStoredInUserProfile.

// ======== User Profile tests =========//
/**
 * Verify that {@link DecisionService#getStoredVariation(Experiment, UserProfile)} returns a variation that is
 * stored in the provided {@link UserProfile}.
 */
@SuppressFBWarnings
@Test
public void bucketReturnsVariationStoredInUserProfile() throws Exception {
    final Experiment experiment = noAudienceProjectConfig.getExperiments().get(0);
    final Variation variation = experiment.getVariations().get(0);
    Decision decision = new Decision(variation.getId());
    UserProfile userProfile = new UserProfile(userProfileId, Collections.singletonMap(experiment.getId(), decision));
    UserProfileService userProfileService = mock(UserProfileService.class);
    when(userProfileService.lookup(userProfileId)).thenReturn(userProfile.toMap());
    Bucketer bucketer = new Bucketer(noAudienceProjectConfig);
    DecisionService decisionService = new DecisionService(bucketer, mockErrorHandler, noAudienceProjectConfig, userProfileService);
    logbackVerifier.expectMessage(Level.INFO, "Returning previously activated variation \"" + variation.getKey() + "\" of experiment \"" + experiment.getKey() + "\"" + " for user \"" + userProfileId + "\" from user profile.");
    // ensure user with an entry in the user profile is bucketed into the corresponding stored variation
    assertEquals(variation, decisionService.getVariation(experiment, userProfileId, Collections.<String, String>emptyMap()));
    verify(userProfileService).lookup(userProfileId);
}
Also used : Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 15 with Experiment

use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.

the class DecisionServiceTest method getVariationSavesBucketedVariationIntoUserProfile.

/**
 * Verify that {@link DecisionService#getVariation(Experiment, String, Map)}
 * saves a {@link Variation}of an {@link Experiment} for a user when a {@link UserProfileService} is present.
 */
@SuppressFBWarnings
@Test
public void getVariationSavesBucketedVariationIntoUserProfile() throws Exception {
    final Experiment experiment = noAudienceProjectConfig.getExperiments().get(0);
    final Variation variation = experiment.getVariations().get(0);
    Decision decision = new Decision(variation.getId());
    UserProfileService userProfileService = mock(UserProfileService.class);
    UserProfile originalUserProfile = new UserProfile(userProfileId, new HashMap<String, Decision>());
    when(userProfileService.lookup(userProfileId)).thenReturn(originalUserProfile.toMap());
    UserProfile expectedUserProfile = new UserProfile(userProfileId, Collections.singletonMap(experiment.getId(), decision));
    Bucketer mockBucketer = mock(Bucketer.class);
    when(mockBucketer.bucket(experiment, userProfileId)).thenReturn(variation);
    DecisionService decisionService = new DecisionService(mockBucketer, mockErrorHandler, noAudienceProjectConfig, userProfileService);
    assertEquals(variation, decisionService.getVariation(experiment, userProfileId, Collections.<String, String>emptyMap()));
    logbackVerifier.expectMessage(Level.INFO, String.format("Saved variation \"%s\" of experiment \"%s\" for user \"" + userProfileId + "\".", variation.getId(), experiment.getId()));
    verify(userProfileService).save(eq(expectedUserProfile.toMap()));
}
Also used : Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

Experiment (com.optimizely.ab.config.Experiment)137 Test (org.junit.Test)113 Variation (com.optimizely.ab.config.Variation)91 Matchers.anyString (org.mockito.Matchers.anyString)58 LogEvent (com.optimizely.ab.event.LogEvent)47 HashMap (java.util.HashMap)42 EventType (com.optimizely.ab.config.EventType)28 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)27 ProjectConfig (com.optimizely.ab.config.ProjectConfig)22 Map (java.util.Map)18 Attribute (com.optimizely.ab.config.Attribute)17 ImmutableMap (com.google.common.collect.ImmutableMap)15 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)15 ArrayList (java.util.ArrayList)14 Rollout (com.optimizely.ab.config.Rollout)13 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)12 TrafficAllocation (com.optimizely.ab.config.TrafficAllocation)11 ExperimentUtils.isUserInExperiment (com.optimizely.ab.internal.ExperimentUtils.isUserInExperiment)10 Bucketer (com.optimizely.ab.bucketing.Bucketer)9 ExhaustiveTest (com.optimizely.ab.categories.ExhaustiveTest)9