use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class JsonConfigParser method parseGroups.
private List<Group> parseGroups(JSONArray groupJson) {
List<Group> groups = new ArrayList<Group>(groupJson.length());
for (Object obj : groupJson) {
JSONObject groupObject = (JSONObject) obj;
String id = groupObject.getString("id");
String policy = groupObject.getString("policy");
List<Experiment> experiments = parseExperiments(groupObject.getJSONArray("experiments"), id);
List<TrafficAllocation> trafficAllocations = parseTrafficAllocation(groupObject.getJSONArray("trafficAllocation"));
groups.add(new Group(id, policy, experiments, trafficAllocations));
}
return groups;
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method trackEventEndToEndForced.
// ======== track tests ========//
/**
* Verify that the {@link Optimizely#track(String, String)} call correctly builds a V2 event and passes it
* through {@link EventHandler#dispatchEvent(LogEvent)}.
*/
@Test
public void trackEventEndToEndForced() throws Exception {
EventType eventType;
String datafile;
ProjectConfig config;
if (datafileVersion >= 4) {
config = spy(validProjectConfig);
eventType = validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY);
datafile = validDatafile;
} else {
config = spy(noAudienceProjectConfig);
eventType = noAudienceProjectConfig.getEventTypes().get(0);
datafile = noAudienceDatafile;
}
List<Experiment> allExperiments = new ArrayList<Experiment>();
allExperiments.add(config.getExperiments().get(0));
EventBuilder eventBuilder = new EventBuilder();
DecisionService spyDecisionService = spy(new DecisionService(mockBucketer, mockErrorHandler, config, null));
Optimizely optimizely = Optimizely.builder(datafile, mockEventHandler).withDecisionService(spyDecisionService).withEventBuilder(eventBuilder).withConfig(noAudienceProjectConfig).withErrorHandler(mockErrorHandler).build();
// call the bucket function.
for (Experiment experiment : allExperiments) {
when(mockBucketer.bucket(experiment, testUserId)).thenReturn(null);
}
// call get forced.
for (Experiment experiment : allExperiments) {
optimizely.projectConfig.setForcedVariation(experiment.getKey(), testUserId, experiment.getVariations().get(0).getKey());
}
// call track
optimizely.track(eventType.getKey(), testUserId);
// verify that the bucketing algorithm was called only on experiments corresponding to the specified goal.
List<Experiment> experimentsForEvent = config.getExperimentsForEventKey(eventType.getKey());
for (Experiment experiment : allExperiments) {
if (experiment.isRunning() && experimentsForEvent.contains(experiment)) {
verify(spyDecisionService).getVariation(experiment, testUserId, Collections.<String, String>emptyMap());
verify(config).getForcedVariation(experiment.getKey(), testUserId);
} else {
verify(spyDecisionService, never()).getVariation(experiment, testUserId, Collections.<String, String>emptyMap());
}
}
// verify that dispatchEvent was called
verify(mockEventHandler).dispatchEvent(any(LogEvent.class));
for (Experiment experiment : allExperiments) {
assertEquals(optimizely.projectConfig.getForcedVariation(experiment.getKey(), testUserId), experiment.getVariations().get(0));
optimizely.projectConfig.setForcedVariation(experiment.getKey(), testUserId, null);
assertNull(optimizely.projectConfig.getForcedVariation(experiment.getKey(), testUserId));
}
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method trackEventWithUnknownAttribute.
/**
* Verify that {@link Optimizely#track(String, String)} handles the case where an unknown attribute
* (i.e., not in the config) is passed through.
*
* In this case, the track event call should remove the unknown attribute from the given map.
*/
@Test
@SuppressWarnings("unchecked")
public void trackEventWithUnknownAttribute() 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(new RaiseExceptionErrorHandler()).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()), anyMapOf(String.class, String.class), eq(Collections.<String, Object>emptyMap()))).thenReturn(logEventToDispatch);
logbackVerifier.expectMessage(Level.INFO, "Tracking event \"" + eventType.getKey() + "\" for user \"" + genericUserId + "\".");
logbackVerifier.expectMessage(Level.WARN, "Attribute(s) [unknownAttribute] not in the datafile.");
logbackVerifier.expectMessage(Level.DEBUG, "Dispatching conversion event to URL test_url with params " + testParams + " and payload \"\"");
// call track
optimizely.track(eventType.getKey(), genericUserId, ImmutableMap.of("unknownAttribute", "attributeValue"));
// 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, not(hasKey("unknownAttribute")));
verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class DecisionServiceTest method getVariationWhitelistedPrecedesAudienceEval.
// ========= getVariation tests =========/
/**
* Verify that {@link DecisionService#getVariation(Experiment, String, Map)}
* gives precedence to forced variation bucketing over audience evaluation.
*/
@Test
public void getVariationWhitelistedPrecedesAudienceEval() throws Exception {
Bucketer bucketer = spy(new Bucketer(validProjectConfig));
DecisionService decisionService = spy(new DecisionService(bucketer, mockErrorHandler, validProjectConfig, null));
Experiment experiment = validProjectConfig.getExperiments().get(0);
Variation expectedVariation = experiment.getVariations().get(0);
// user excluded without audiences and whitelisting
assertNull(decisionService.getVariation(experiment, genericUserId, Collections.<String, String>emptyMap()));
logbackVerifier.expectMessage(Level.INFO, "User \"" + whitelistedUserId + "\" is forced in variation \"vtag1\".");
// no attributes provided for a experiment that has an audience
assertThat(decisionService.getVariation(experiment, whitelistedUserId, Collections.<String, String>emptyMap()), is(expectedVariation));
verify(decisionService).getWhitelistedVariation(experiment, whitelistedUserId);
verify(decisionService, never()).getStoredVariation(eq(experiment), any(UserProfile.class));
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class DecisionServiceTest method getVariationForFeatureReturnsVariationFromExperimentBeforeRollout.
/**
* Verify that when getting a {@link Variation} for a {@link FeatureFlag} in
* {@link DecisionService#getVariationForFeature(FeatureFlag, String, Map)},
* check first if the user is bucketed to an {@link Experiment}
* then check if the user is not bucketed to an experiment,
* check for a {@link Rollout}.
*/
@Test
public void getVariationForFeatureReturnsVariationFromExperimentBeforeRollout() {
FeatureFlag featureFlag = FEATURE_FLAG_MULTI_VARIATE_FEATURE;
Experiment featureExperiment = v4ProjectConfig.getExperimentIdMapping().get(featureFlag.getExperimentIds().get(0));
assertNotNull(featureExperiment);
Rollout featureRollout = v4ProjectConfig.getRolloutIdMapping().get(featureFlag.getRolloutId());
Variation experimentVariation = featureExperiment.getVariations().get(0);
Experiment rolloutExperiment = featureRollout.getExperiments().get(0);
Variation rolloutVariation = rolloutExperiment.getVariations().get(0);
DecisionService decisionService = spy(new DecisionService(mock(Bucketer.class), mockErrorHandler, v4ProjectConfig, null));
// return variation for experiment
doReturn(experimentVariation).when(decisionService).getVariation(eq(featureExperiment), anyString(), anyMapOf(String.class, String.class));
// return variation for rollout
doReturn(new FeatureDecision(rolloutExperiment, rolloutVariation, FeatureDecision.DecisionSource.ROLLOUT)).when(decisionService).getVariationForFeatureInRollout(eq(featureFlag), anyString(), anyMapOf(String.class, String.class));
// make sure we get the right variation back
FeatureDecision featureDecision = decisionService.getVariationForFeature(featureFlag, genericUserId, Collections.<String, String>emptyMap());
assertEquals(experimentVariation, featureDecision.variation);
assertEquals(FeatureDecision.DecisionSource.EXPERIMENT, featureDecision.decisionSource);
// make sure we do not even check for rollout bucketing
verify(decisionService, never()).getVariationForFeatureInRollout(any(FeatureFlag.class), anyString(), anyMapOf(String.class, String.class));
// make sure we ask for experiment bucketing once
verify(decisionService, times(1)).getVariation(any(Experiment.class), anyString(), anyMapOf(String.class, String.class));
}
Aggregations