use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class BucketerTest method testBucketWithBucketingId.
@Test
public void testBucketWithBucketingId() {
final AtomicInteger bucketValue = new AtomicInteger();
Bucketer algorithm = mockBucketAlgorithm(bucketValue);
bucketValue.set(0);
String bucketingId = "blah";
String userId = "blahUser";
ProjectConfig projectConfig = validProjectConfigV2();
List<Experiment> groupExperiments = projectConfig.getGroups().get(1).getExperiments();
Experiment groupExperiment = groupExperiments.get(0);
Variation expectedVariation = groupExperiment.getVariations().get(0);
logbackVerifier.expectMessage(Level.INFO, "User with bucketingId \"" + bucketingId + "\" is in variation \"e1_vtag1\" of experiment \"overlapping_etag1\".");
assertThat(algorithm.bucket(groupExperiment, bucketingId), is(expectedVariation));
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method getVariationNoAudiences.
/**
* Verify that {@link Optimizely#getVariation(String, String)} returns a variation when given an experiment
* with no audiences and no user attributes.
*/
@Test
public void getVariationNoAudiences() throws Exception {
Experiment experiment = noAudienceProjectConfig.getExperiments().get(0);
Variation bucketedVariation = experiment.getVariations().get(0);
when(mockBucketer.bucket(experiment, testUserId)).thenReturn(bucketedVariation);
Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withConfig(noAudienceProjectConfig).withBucketing(mockBucketer).withErrorHandler(mockErrorHandler).build();
Variation actualVariation = optimizely.getVariation(experiment.getKey(), testUserId);
verify(mockBucketer).bucket(experiment, testUserId);
assertThat(actualVariation, is(bucketedVariation));
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method activateUserInAudience.
/**
* Verify that a user who falls in an experiment's audience is assigned a variation.
*/
@Test
public void activateUserInAudience() throws Exception {
Experiment 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", "chrome");
Variation actualVariation = optimizely.activate(experimentToCheck.getKey(), testUserId, testUserAttributes);
assertNotNull(actualVariation);
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method trackEventWithNullEventTags.
/**
* Verify that {@link Optimizely#track(String, String, Map, Map)} called with null event tags will default to
* an empty map when calling {@link EventBuilder#createConversionEvent(ProjectConfig, Map, String, String, String, Map, Map)}
*/
@Test
@SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "testing nullness contract violation")
public void trackEventWithNullEventTags() 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, String>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
optimizely.track(eventType.getKey(), genericUserId, Collections.<String, String>emptyMap(), null);
// 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()), eq(Collections.<String, String>emptyMap()), eq(Collections.<String, String>emptyMap()));
verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class JsonSimpleConfigParser method parseExperiments.
private List<Experiment> parseExperiments(JSONArray experimentJson, String groupId) {
List<Experiment> experiments = new ArrayList<Experiment>(experimentJson.size());
for (Object obj : experimentJson) {
JSONObject experimentObject = (JSONObject) obj;
String id = (String) experimentObject.get("id");
String key = (String) experimentObject.get("key");
Object statusJson = experimentObject.get("status");
String status = statusJson == null ? ExperimentStatus.NOT_STARTED.toString() : (String) experimentObject.get("status");
Object layerIdObject = experimentObject.get("layerId");
String layerId = layerIdObject == null ? null : (String) layerIdObject;
JSONArray audienceIdsJson = (JSONArray) experimentObject.get("audienceIds");
List<String> audienceIds = new ArrayList<String>(audienceIdsJson.size());
for (Object audienceIdObj : audienceIdsJson) {
audienceIds.add((String) audienceIdObj);
}
// parse the child objects
List<Variation> variations = parseVariations((JSONArray) experimentObject.get("variations"));
Map<String, String> userIdToVariationKeyMap = parseForcedVariations((JSONObject) experimentObject.get("forcedVariations"));
List<TrafficAllocation> trafficAllocations = parseTrafficAllocation((JSONArray) experimentObject.get("trafficAllocation"));
experiments.add(new Experiment(id, key, status, layerId, audienceIds, variations, userIdToVariationKeyMap, trafficAllocations, groupId));
}
return experiments;
}
Aggregations