use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class Bucketer method bucket.
/**
* Assign a {@link Variation} of an {@link Experiment} to a user based on hashed value from murmurhash3.
* @param experiment The Experiment in which the user is to be bucketed.
* @param bucketingId string A customer-assigned value used to create the key for the murmur hash.
* @return Variation the user is bucketed into or null.
*/
@Nullable
public Variation bucket(@Nonnull Experiment experiment, @Nonnull String bucketingId) {
// ---------- Bucket User ----------
String groupId = experiment.getGroupId();
// check whether the experiment belongs to a group
if (!groupId.isEmpty()) {
Group experimentGroup = projectConfig.getGroupIdMapping().get(groupId);
// bucket to an experiment only if group entities are to be mutually exclusive
if (experimentGroup.getPolicy().equals(Group.RANDOM_POLICY)) {
Experiment bucketedExperiment = bucketToExperiment(experimentGroup, bucketingId);
if (bucketedExperiment == null) {
logger.info("User with bucketingId \"{}\" is not in any experiment of group {}.", bucketingId, experimentGroup.getId());
return null;
} else {
}
// don't perform further bucketing within the experiment
if (!bucketedExperiment.getId().equals(experiment.getId())) {
logger.info("User with bucketingId \"{}\" is not in experiment \"{}\" of group {}.", bucketingId, experiment.getKey(), experimentGroup.getId());
return null;
}
logger.info("User with bucketingId \"{}\" is in experiment \"{}\" of group {}.", bucketingId, experiment.getKey(), experimentGroup.getId());
}
}
return bucketToVariation(experiment, bucketingId);
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method activateLaunchedExperimentDoesNotDispatchEvent.
/**
* Verify that {@link Optimizely#activate(String, String)} doesn't dispatch an event for an experiment with a
* "Launched" status.
*/
@Test
public void activateLaunchedExperimentDoesNotDispatchEvent() throws Exception {
Experiment launchedExperiment;
if (datafileVersion == 4) {
launchedExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_LAUNCHED_EXPERIMENT_KEY);
} else {
launchedExperiment = noAudienceProjectConfig.getExperiments().get(2);
}
Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withBucketing(mockBucketer).withConfig(noAudienceProjectConfig).build();
Variation expectedVariation = launchedExperiment.getVariations().get(0);
when(mockBucketer.bucket(launchedExperiment, testUserId)).thenReturn(launchedExperiment.getVariations().get(0));
logbackVerifier.expectMessage(Level.INFO, "Experiment has \"Launched\" status so not dispatching event during activation.");
Variation variation = optimizely.activate(launchedExperiment.getKey(), testUserId);
assertNotNull(variation);
assertThat(variation.getKey(), is(expectedVariation.getKey()));
// verify that we did NOT dispatch an event
verify(mockEventHandler, never()).dispatchEvent(any(LogEvent.class));
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method activateForGroupExperimentWithNonMatchingAttributes.
/**
* Verify that {@link Optimizely#activate(String, String, Map)} doesn't return a variation when given
* non-matching user attributes.
*/
@Test
public void activateForGroupExperimentWithNonMatchingAttributes() throws Exception {
Experiment experiment = validProjectConfig.getGroups().get(0).getExperiments().get(0);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).build();
String experimentKey = experiment.getKey();
logbackVerifier.expectMessage(Level.INFO, "User \"user\" does not meet conditions to be in experiment \"" + experimentKey + "\".");
logbackVerifier.expectMessage(Level.INFO, "Not activating user \"user\" for experiment \"" + experimentKey + "\".");
assertNull(optimizely.activate(experiment.getKey(), "user", Collections.singletonMap("browser_type", "firefox")));
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method trackEventWithListenerNullAttributes.
/**
* Track with listener and verify that {@link Optimizely#track(String, String)} ignores null attributes.
*/
@Test
@SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "testing nullness contract violation")
public void trackEventWithListenerNullAttributes() throws Exception {
final EventType eventType;
if (datafileVersion >= 4) {
eventType = validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY);
} else {
eventType = validProjectConfig.getEventTypes().get(0);
}
// setup a mock event builder to return expected conversion params
EventBuilder mockEventBuilder = mock(EventBuilder.class);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withEventBuilder(mockEventBuilder).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
Map<String, String> testParams = new HashMap<String, String>();
testParams.put("test", "params");
Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, mockDecisionService, eventType.getKey(), genericUserId, Collections.<String, String>emptyMap());
LogEvent logEventToDispatch = new LogEvent(RequestMethod.GET, "test_url", testParams, "");
when(mockEventBuilder.createConversionEvent(eq(validProjectConfig), eq(experimentVariationMap), eq(genericUserId), eq(eventType.getId()), eq(eventType.getKey()), eq(Collections.<String, String>emptyMap()), eq(Collections.<String, Object>emptyMap()))).thenReturn(logEventToDispatch);
logbackVerifier.expectMessage(Level.INFO, "Tracking event \"" + eventType.getKey() + "\" for user \"" + genericUserId + "\".");
logbackVerifier.expectMessage(Level.DEBUG, "Dispatching conversion event to URL test_url with params " + testParams + " and payload \"\"");
TrackNotificationListener trackNotification = new TrackNotificationListener() {
@Override
public void onTrack(@Nonnull String eventKey, @Nonnull String userId, @Nonnull Map<String, String> attributes, @Nonnull Map<String, ?> eventTags, @Nonnull LogEvent event) {
assertEquals(eventType.getKey(), eventKey);
assertEquals(genericUserId, userId);
assertTrue(attributes.isEmpty());
assertTrue(eventTags.isEmpty());
}
};
int notificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Track, trackNotification);
// call track
Map<String, String> attributes = null;
optimizely.track(eventType.getKey(), genericUserId, attributes);
optimizely.notificationCenter.removeNotificationListener(notificationId);
logbackVerifier.expectMessage(Level.WARN, "Attributes is null when non-null was expected. Defaulting to an empty attributes map.");
// setup the attribute map captor (so we can verify its content)
ArgumentCaptor<Map> attributeCaptor = ArgumentCaptor.forClass(Map.class);
// verify that the event builder was called with the expected attributes
verify(mockEventBuilder).createConversionEvent(eq(validProjectConfig), eq(experimentVariationMap), eq(genericUserId), eq(eventType.getId()), eq(eventType.getKey()), attributeCaptor.capture(), eq(Collections.<String, Object>emptyMap()));
Map<String, String> actualValue = attributeCaptor.getValue();
assertThat(actualValue, is(Collections.<String, String>emptyMap()));
verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method trackDoesNotSendEventWhenExperimentsAreLaunchedOnly.
/**
* Verify that {@link Optimizely#track(String, String, Map)}
* doesn't dispatch events when the event links only to launched experiments
*/
@Test
public void trackDoesNotSendEventWhenExperimentsAreLaunchedOnly() throws Exception {
EventType eventType;
if (datafileVersion >= 4) {
eventType = validProjectConfig.getEventNameMapping().get(EVENT_LAUNCHED_EXPERIMENT_ONLY_KEY);
} else {
eventType = noAudienceProjectConfig.getEventNameMapping().get("launched_exp_event");
}
Bucketer mockBucketAlgorithm = mock(Bucketer.class);
for (Experiment experiment : noAudienceProjectConfig.getExperiments()) {
Variation variation = experiment.getVariations().get(0);
when(mockBucketAlgorithm.bucket(eq(experiment), eq(genericUserId))).thenReturn(variation);
}
Optimizely client = Optimizely.builder(noAudienceDatafile, mockEventHandler).withConfig(noAudienceProjectConfig).withBucketing(mockBucketAlgorithm).build();
List<Experiment> eventExperiments = noAudienceProjectConfig.getExperimentsForEventKey(eventType.getKey());
for (Experiment experiment : eventExperiments) {
logbackVerifier.expectMessage(Level.INFO, "Not tracking event \"" + eventType.getKey() + "\" for experiment \"" + experiment.getKey() + "\" because experiment has status \"Launched\".");
}
logbackVerifier.expectMessage(Level.INFO, "There are no valid experiments for event \"" + eventType.getKey() + "\" to track.");
logbackVerifier.expectMessage(Level.INFO, "Not tracking event \"" + eventType.getKey() + "\" for user \"" + genericUserId + "\".");
// only 1 experiment uses the event and it has a "Launched" status so experimentsForEvent map is empty
// and the returned event will be null
// this means we will never call the dispatcher
client.track(eventType.getKey(), genericUserId, Collections.<String, String>emptyMap());
// bucket should never be called since experiments are launched so we never get variation for them
verify(mockBucketAlgorithm, never()).bucket(any(Experiment.class), anyString());
verify(mockEventHandler, never()).dispatchEvent(any(LogEvent.class));
}
Aggregations