use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class DecisionService method getVariationForFeatureInRollout.
/**
* Try to bucket the user into a rollout rule.
* Evaluate the user for rules in priority order by seeing if the user satisfies the audience.
* Fall back onto the everyone else rule if the user is ever excluded from a rule due to traffic allocation.
* @param featureFlag The feature flag the user wants to access.
* @param userId User Identifier
* @param filteredAttributes A map of filtered attributes.
* @return {@link FeatureDecision}
*/
@Nonnull
FeatureDecision getVariationForFeatureInRollout(@Nonnull FeatureFlag featureFlag, @Nonnull String userId, @Nonnull Map<String, String> filteredAttributes) {
// use rollout to get variation for feature
if (featureFlag.getRolloutId().isEmpty()) {
logger.info("The feature flag \"{}\" is not used in a rollout.", featureFlag.getKey());
return new FeatureDecision(null, null, null);
}
Rollout rollout = projectConfig.getRolloutIdMapping().get(featureFlag.getRolloutId());
if (rollout == null) {
logger.error("The rollout with id \"{}\" was not found in the datafile for feature flag \"{}\".", featureFlag.getRolloutId(), featureFlag.getKey());
return new FeatureDecision(null, null, null);
}
// for all rules before the everyone else rule
int rolloutRulesLength = rollout.getExperiments().size();
String bucketingId = userId;
if (filteredAttributes.containsKey(BUCKETING_ATTRIBUTE)) {
bucketingId = filteredAttributes.get(BUCKETING_ATTRIBUTE);
}
Variation variation;
for (int i = 0; i < rolloutRulesLength - 1; i++) {
Experiment rolloutRule = rollout.getExperiments().get(i);
Audience audience = projectConfig.getAudienceIdMapping().get(rolloutRule.getAudienceIds().get(0));
if (ExperimentUtils.isUserInExperiment(projectConfig, rolloutRule, filteredAttributes)) {
variation = bucketer.bucket(rolloutRule, bucketingId);
if (variation == null) {
break;
}
return new FeatureDecision(rolloutRule, variation, FeatureDecision.DecisionSource.ROLLOUT);
} else {
logger.debug("User \"{}\" did not meet the conditions to be in rollout rule for audience \"{}\".", userId, audience.getName());
}
}
// get last rule which is the fall back rule
Experiment finalRule = rollout.getExperiments().get(rolloutRulesLength - 1);
if (ExperimentUtils.isUserInExperiment(projectConfig, finalRule, filteredAttributes)) {
variation = bucketer.bucket(finalRule, bucketingId);
if (variation != null) {
return new FeatureDecision(finalRule, variation, FeatureDecision.DecisionSource.ROLLOUT);
}
}
return new FeatureDecision(null, null, null);
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method removeNotificationListenerNotificationCenter.
/**
* Verify that {@link com.optimizely.ab.notification.NotificationCenter} properly
* calls and the listener is removed and no longer notified when an experiment is activated.
*/
@Test
public void removeNotificationListenerNotificationCenter() throws Exception {
Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
Variation bucketedVariation = activatedExperiment.getVariations().get(0);
EventBuilder mockEventBuilder = mock(EventBuilder.class);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withEventBuilder(mockEventBuilder).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
Map<String, String> attributes = new HashMap<String, String>();
attributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
Map<String, String> testParams = new HashMap<String, String>();
testParams.put("test", "params");
LogEvent logEventToDispatch = new LogEvent(RequestMethod.GET, "test_url", testParams, "");
when(mockEventBuilder.createImpressionEvent(validProjectConfig, activatedExperiment, bucketedVariation, genericUserId, attributes)).thenReturn(logEventToDispatch);
when(mockBucketer.bucket(activatedExperiment, genericUserId)).thenReturn(bucketedVariation);
when(mockEventBuilder.createImpressionEvent(validProjectConfig, activatedExperiment, bucketedVariation, genericUserId, attributes)).thenReturn(logEventToDispatch);
// Add and remove listener
ActivateNotificationListener activateNotification = mock(ActivateNotificationListener.class);
int notificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Activate, activateNotification);
assertTrue(optimizely.notificationCenter.removeNotificationListener(notificationId));
TrackNotificationListener trackNotification = mock(TrackNotificationListener.class);
notificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Track, trackNotification);
assertTrue(optimizely.notificationCenter.removeNotificationListener(notificationId));
// Check if listener is notified after an experiment is activated
Variation actualVariation = optimizely.activate(activatedExperiment, genericUserId, attributes);
verify(activateNotification, never()).onActivate(activatedExperiment, genericUserId, attributes, actualVariation, logEventToDispatch);
// Check if listener is notified after a live variable is accessed
boolean activateExperiment = true;
verify(activateNotification, never()).onActivate(activatedExperiment, genericUserId, attributes, actualVariation, logEventToDispatch);
// Check if listener is notified after an event is tracked
EventType eventType = validProjectConfig.getEventTypes().get(0);
String eventKey = eventType.getKey();
Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, mockDecisionService, eventType.getKey(), genericUserId, attributes);
when(mockEventBuilder.createConversionEvent(eq(validProjectConfig), eq(experimentVariationMap), eq(genericUserId), eq(eventType.getId()), eq(eventKey), eq(attributes), anyMapOf(String.class, Object.class))).thenReturn(logEventToDispatch);
optimizely.track(eventKey, genericUserId, attributes);
verify(trackNotification, never()).onTrack(eventKey, genericUserId, attributes, Collections.EMPTY_MAP, logEventToDispatch);
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method activateWithExperimentKey.
/**
* Verify that the {@link Optimizely#activate(String, String, Map<String, String>)} call
* correctly builds an endpoint url and request params
* and passes them through {@link EventHandler#dispatchEvent(LogEvent)}.
*/
@Test
public void activateWithExperimentKey() throws Exception {
Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
Variation bucketedVariation = activatedExperiment.getVariations().get(0);
Variation userIdBucketVariation = activatedExperiment.getVariations().get(1);
EventBuilder mockEventBuilder = mock(EventBuilder.class);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withEventBuilder(mockEventBuilder).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
Map<String, String> testUserAttributes = new HashMap<String, String>();
if (datafileVersion >= 4) {
testUserAttributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
} else {
testUserAttributes.put("browser_type", "chrome");
}
testUserAttributes.put(testBucketingIdKey, testBucketingId);
Map<String, String> testParams = new HashMap<String, String>();
testParams.put("test", "params");
LogEvent logEventToDispatch = new LogEvent(RequestMethod.GET, "test_url", testParams, "");
when(mockEventBuilder.createImpressionEvent(eq(validProjectConfig), eq(activatedExperiment), eq(bucketedVariation), eq(testUserId), eq(testUserAttributes))).thenReturn(logEventToDispatch);
when(mockBucketer.bucket(activatedExperiment, testUserId)).thenReturn(userIdBucketVariation);
when(mockBucketer.bucket(activatedExperiment, testBucketingId)).thenReturn(bucketedVariation);
// activate the experiment
Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), testUserId, testUserAttributes);
// verify that the bucketing algorithm was called correctly
verify(mockBucketer).bucket(activatedExperiment, testBucketingId);
assertThat(actualVariation, is(bucketedVariation));
// verify that dispatchEvent was called with the correct LogEvent object
verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method activateForNullVariation.
/**
* Verify that the {@link Optimizely#activate(Experiment, String, Map)} DOES NOT dispatch an impression event
* when the user isn't bucketed to a variation.
*/
@Test
public void activateForNullVariation() throws Exception {
Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
Map<String, String> testUserAttributes = new HashMap<String, String>();
testUserAttributes.put("browser_type", "chrome");
testUserAttributes.put(testBucketingIdKey, testBucketingId);
when(mockBucketer.bucket(activatedExperiment, testBucketingId)).thenReturn(null);
logbackVerifier.expectMessage(Level.INFO, "Not activating user \"userId\" for experiment \"" + activatedExperiment.getKey() + "\".");
// activate the experiment
Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), testUserId, testUserAttributes);
// verify that the bucketing algorithm was called correctly
verify(mockBucketer).bucket(activatedExperiment, testBucketingId);
assertNull(actualVariation);
// verify that dispatchEvent was NOT called
verify(mockEventHandler, never()).dispatchEvent(any(LogEvent.class));
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method getVariationWithExperimentKey.
/**
* Verify that {@link Optimizely#getVariation(String, String)} correctly makes the
* {@link Bucketer#bucket(Experiment, String)} call and does NOT dispatch an event.
*/
@Test
public void getVariationWithExperimentKey() throws Exception {
Experiment activatedExperiment = noAudienceProjectConfig.getExperiments().get(0);
Variation bucketedVariation = activatedExperiment.getVariations().get(0);
Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withBucketing(mockBucketer).withConfig(noAudienceProjectConfig).withErrorHandler(mockErrorHandler).build();
when(mockBucketer.bucket(activatedExperiment, testUserId)).thenReturn(bucketedVariation);
// activate the experiment
Variation actualVariation = optimizely.getVariation(activatedExperiment.getKey(), testUserId);
// verify that the bucketing algorithm was called correctly
verify(mockBucketer).bucket(activatedExperiment, testUserId);
assertThat(actualVariation, is(bucketedVariation));
// verify that we didn't attempt to dispatch an event
verify(mockEventHandler, never()).dispatchEvent(any(LogEvent.class));
}
Aggregations