use of com.optimizely.ab.bucketing.Bucketer 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));
}
use of com.optimizely.ab.bucketing.Bucketer in project java-sdk by optimizely.
the class EventBuilderTest method createConversionEventAndroidClientEngineClientVersion.
/**
* Verify that supplying {@link EventBuilder} with a custom client engine and client version results in conversion
* events being sent with the overriden values.
*/
@Test
public void createConversionEventAndroidClientEngineClientVersion() throws Exception {
EventBuilder builder = new EventBuilder(EventBatch.ClientEngine.ANDROID_SDK, "0.0.0");
Attribute attribute = validProjectConfig.getAttributes().get(0);
EventType eventType = validProjectConfig.getEventTypes().get(0);
Bucketer mockBucketAlgorithm = mock(Bucketer.class);
for (Experiment experiment : validProjectConfig.getExperiments()) {
when(mockBucketAlgorithm.bucket(experiment, userId)).thenReturn(experiment.getVariations().get(0));
}
DecisionService decisionService = new DecisionService(mockBucketAlgorithm, mock(ErrorHandler.class), validProjectConfig, mock(UserProfileService.class));
Map<String, String> attributeMap = Collections.singletonMap(attribute.getKey(), "value");
Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, decisionService, eventType.getKey(), userId, attributeMap);
LogEvent conversionEvent = builder.createConversionEvent(validProjectConfig, experimentVariationMap, userId, eventType.getId(), eventType.getKey(), attributeMap, Collections.<String, Object>emptyMap());
EventBatch conversion = gson.fromJson(conversionEvent.getBody(), EventBatch.class);
assertThat(conversion.getClientName(), is(EventBatch.ClientEngine.ANDROID_SDK.getClientEngineValue()));
assertThat(conversion.getClientVersion(), is("0.0.0"));
}
use of com.optimizely.ab.bucketing.Bucketer in project java-sdk by optimizely.
the class EventBuilderTest method createConversionEventForcedVariationBucketingPrecedesAudienceEval.
/**
* Verify that precedence is given to forced variation bucketing over audience evaluation when constructing a
* conversion event.
*/
@Test
public void createConversionEventForcedVariationBucketingPrecedesAudienceEval() {
EventType eventType;
String whitelistedUserId;
if (datafileVersion == 4) {
eventType = validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY);
whitelistedUserId = MULTIVARIATE_EXPERIMENT_FORCED_VARIATION_USER_ID_GRED;
} else {
eventType = validProjectConfig.getEventTypes().get(0);
whitelistedUserId = "testUser1";
}
DecisionService decisionService = new DecisionService(new Bucketer(validProjectConfig), new NoOpErrorHandler(), validProjectConfig, mock(UserProfileService.class));
// attributes are empty so user won't be in the audience for experiment using the event, but bucketing
// will still take place
Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, decisionService, eventType.getKey(), whitelistedUserId, Collections.<String, String>emptyMap());
LogEvent conversionEvent = builder.createConversionEvent(validProjectConfig, experimentVariationMap, whitelistedUserId, eventType.getId(), eventType.getKey(), Collections.<String, String>emptyMap(), Collections.<String, Object>emptyMap());
assertNotNull(conversionEvent);
EventBatch conversion = gson.fromJson(conversionEvent.getBody(), EventBatch.class);
if (datafileVersion == 4) {
// 2 experiments use the event
// basic experiment has no audience
// user is whitelisted in to one audience
assertEquals(2, conversion.getVisitors().get(0).getSnapshots().get(0).getDecisions().size());
} else {
assertEquals(1, conversion.getVisitors().get(0).getSnapshots().get(0).getDecisions().size());
}
}
use of com.optimizely.ab.bucketing.Bucketer in project java-sdk by optimizely.
the class EventFactoryTest method createConversionEventAndroidTVClientEngineClientVersion.
/**
* Verify that supplying {@link ClientEngineInfo} with a Android TV client engine and client version results in
* conversion events being sent with the overriden values.
*/
@Test
public void createConversionEventAndroidTVClientEngineClientVersion() throws Exception {
String clientVersion = "0.0.0";
ClientEngineInfo.setClientEngine(EventBatch.ClientEngine.ANDROID_TV_SDK);
ProjectConfig projectConfig = validProjectConfigV2();
Attribute attribute = projectConfig.getAttributes().get(0);
EventType eventType = projectConfig.getEventTypes().get(0);
String userId = "userId";
Bucketer mockBucketAlgorithm = mock(Bucketer.class);
for (Experiment experiment : projectConfig.getExperiments()) {
when(mockBucketAlgorithm.bucket(experiment, userId, validProjectConfig)).thenReturn(DecisionResponse.responseNoReasons(experiment.getVariations().get(0)));
}
Map<String, String> attributeMap = Collections.singletonMap(attribute.getKey(), "value");
LogEvent conversionEvent = createConversionEvent(projectConfig, userId, eventType.getId(), eventType.getKey(), attributeMap, Collections.emptyMap());
EventBatch conversion = gson.fromJson(conversionEvent.getBody(), EventBatch.class);
assertThat(conversion.getClientName(), is(EventBatch.ClientEngine.ANDROID_TV_SDK.getClientEngineValue()));
// assertThat(conversion.getClientVersion(), is(clientVersion));
}
use of com.optimizely.ab.bucketing.Bucketer in project java-sdk by optimizely.
the class EventBuilderTest method createConversionEventExperimentStatusPrecedesForcedVariation.
/**
* Verify that precedence is given to experiment status over forced variation bucketing when constructing a
* conversion event.
*/
@Test
public void createConversionEventExperimentStatusPrecedesForcedVariation() {
EventType eventType;
if (datafileVersion == 4) {
eventType = validProjectConfig.getEventNameMapping().get(EVENT_PAUSED_EXPERIMENT_KEY);
} else {
eventType = validProjectConfig.getEventTypes().get(3);
}
String whitelistedUserId = PAUSED_EXPERIMENT_FORCED_VARIATION_USER_ID_CONTROL;
Bucketer bucketer = spy(new Bucketer(validProjectConfig));
DecisionService decisionService = new DecisionService(bucketer, mock(ErrorHandler.class), validProjectConfig, mock(UserProfileService.class));
Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, decisionService, eventType.getKey(), whitelistedUserId, Collections.<String, String>emptyMap());
LogEvent conversionEvent = builder.createConversionEvent(validProjectConfig, experimentVariationMap, whitelistedUserId, eventType.getId(), eventType.getKey(), Collections.<String, String>emptyMap(), Collections.<String, Object>emptyMap());
for (Experiment experiment : validProjectConfig.getExperiments()) {
verify(bucketer, never()).bucket(experiment, whitelistedUserId);
}
assertNull(conversionEvent);
}
Aggregations