use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method isFeatureEnabledWithExperimentKeyForcedOfFeatureEnabledFalse.
/**
* Verify that the {@link Optimizely#activate(String, String, Map<String, String>)} call
* uses forced variation to force the user into the third variation in which FeatureEnabled is set to
* false so feature enabled will return false
*/
@Test
public void isFeatureEnabledWithExperimentKeyForcedOfFeatureEnabledFalse() throws Exception {
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
Experiment activatedExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_MULTIVARIATE_EXPERIMENT_KEY);
Variation forcedVariation = activatedExperiment.getVariations().get(2);
EventBuilder mockEventBuilder = mock(EventBuilder.class);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withEventBuilder(mockEventBuilder).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
optimizely.setForcedVariation(activatedExperiment.getKey(), testUserId, forcedVariation.getKey());
assertFalse(optimizely.isFeatureEnabled(FEATURE_FLAG_MULTI_VARIATE_FEATURE.getKey(), testUserId));
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method trackDispatchesWhenEventHasLaunchedAndRunningExperiments.
/**
* Verify that {@link Optimizely#track(String, String, Map)}
* dispatches log events when the tracked event links to both launched and running experiments.
*/
@Test
public void trackDispatchesWhenEventHasLaunchedAndRunningExperiments() throws Exception {
EventBuilder mockEventBuilder = mock(EventBuilder.class);
EventType eventType;
if (datafileVersion >= 4) {
eventType = validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY);
} else {
eventType = noAudienceProjectConfig.getEventNameMapping().get("event_with_launched_and_running_experiments");
}
Bucketer mockBucketAlgorithm = mock(Bucketer.class);
for (Experiment experiment : validProjectConfig.getExperiments()) {
when(mockBucketAlgorithm.bucket(experiment, genericUserId)).thenReturn(experiment.getVariations().get(0));
}
Optimizely client = Optimizely.builder(validDatafile, mockEventHandler).withConfig(noAudienceProjectConfig).withBucketing(mockBucketAlgorithm).withEventBuilder(mockEventBuilder).build();
Map<String, String> testParams = new HashMap<String, String>();
testParams.put("test", "params");
Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(noAudienceProjectConfig, client.decisionService, eventType.getKey(), genericUserId, Collections.<String, String>emptyMap());
// Create an Argument Captor to ensure we are creating a correct experiment variation map
ArgumentCaptor<Map> experimentVariationMapCaptor = ArgumentCaptor.forClass(Map.class);
LogEvent conversionEvent = new LogEvent(RequestMethod.GET, "test_url", testParams, "");
when(mockEventBuilder.createConversionEvent(eq(noAudienceProjectConfig), experimentVariationMapCaptor.capture(), eq(genericUserId), eq(eventType.getId()), eq(eventType.getKey()), eq(Collections.<String, String>emptyMap()), eq(Collections.<String, Object>emptyMap()))).thenReturn(conversionEvent);
List<Experiment> eventExperiments = noAudienceProjectConfig.getExperimentsForEventKey(eventType.getKey());
for (Experiment experiment : eventExperiments) {
if (experiment.isLaunched()) {
logbackVerifier.expectMessage(Level.INFO, "Not tracking event \"" + eventType.getKey() + "\" for experiment \"" + experiment.getKey() + "\" because experiment has status \"Launched\".");
}
}
// The event has 1 launched experiment and 1 running experiment.
// It should send a track event with the running experiment
client.track(eventType.getKey(), genericUserId, Collections.<String, String>emptyMap());
verify(client.eventHandler).dispatchEvent(eq(conversionEvent));
// Check the argument captor got the correct arguments
Map<Experiment, Variation> actualExperimentVariationMap = experimentVariationMapCaptor.getValue();
assertEquals(experimentVariationMap, actualExperimentVariationMap);
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method activateUserNotInAudience.
/**
* Verify that a user not in any of an experiment's audiences isn't assigned to a variation.
*/
@Test
public void activateUserNotInAudience() throws Exception {
Experiment experimentToCheck;
if (datafileVersion == 4) {
experimentToCheck = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_MULTIVARIATE_EXPERIMENT_KEY);
} else {
experimentToCheck = validProjectConfig.getExperiments().get(0);
}
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
Map<String, String> testUserAttributes = new HashMap<String, String>();
testUserAttributes.put("browser_type", "firefox");
logbackVerifier.expectMessage(Level.INFO, "User \"userId\" does not meet conditions to be in experiment \"" + experimentToCheck.getKey() + "\".");
logbackVerifier.expectMessage(Level.INFO, "Not activating user \"userId\" for experiment \"" + experimentToCheck.getKey() + "\".");
Variation actualVariation = optimizely.activate(experimentToCheck.getKey(), testUserId, testUserAttributes);
assertNull(actualVariation);
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method trackEventWithNullAttributes.
/**
* Verify that {@link Optimizely#track(String, String)} ignores null attributes.
*/
@Test
@SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "testing nullness contract violation")
public void trackEventWithNullAttributes() throws Exception {
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 \"\"");
// call track
Map<String, String> attributes = null;
optimizely.track(eventType.getKey(), genericUserId, attributes);
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.Variation in project java-sdk by optimizely.
the class BucketerTest method bucketToControl.
/**
* Verify that in certain cases, users aren't assigned any variation and null is returned.
*/
@Test
public void bucketToControl() throws Exception {
String bucketingId = "blah";
String userId = "user1";
List<String> audienceIds = Collections.emptyList();
List<Variation> variations = Collections.singletonList(new Variation("1", "var1"));
List<TrafficAllocation> trafficAllocations = Collections.singletonList(new TrafficAllocation("1", 999));
Experiment experiment = new Experiment("1234", "exp_key", "Running", "1", audienceIds, variations, Collections.<String, String>emptyMap(), trafficAllocations, "");
final AtomicInteger bucketValue = new AtomicInteger();
Bucketer algorithm = mockBucketAlgorithm(bucketValue);
logbackVerifier.expectMessage(Level.DEBUG, "Assigned bucket 0 to user with bucketingId \"" + bucketingId + "\" when bucketing to a variation.");
logbackVerifier.expectMessage(Level.INFO, "User with bucketingId \"" + bucketingId + "\" is in variation \"var1\" of experiment \"exp_key\".");
// verify bucketing to the first variation
bucketValue.set(0);
assertThat(algorithm.bucket(experiment, bucketingId), is(variations.get(0)));
logbackVerifier.expectMessage(Level.DEBUG, "Assigned bucket 1000 to user with bucketingId \"" + bucketingId + "\" when bucketing to a variation.");
logbackVerifier.expectMessage(Level.INFO, "User with bucketingId \"" + bucketingId + "\" is not in any variation of experiment \"exp_key\".");
// verify bucketing to no variation (null)
bucketValue.set(1000);
assertNull(algorithm.bucket(experiment, bucketingId));
}
Aggregations