use of com.optimizely.ab.config.ProjectConfig in project java-sdk by optimizely.
the class JsonSimpleConfigParser method parseProjectConfig.
@Override
public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParseException {
try {
JSONParser parser = new JSONParser();
JSONObject rootObject = (JSONObject) parser.parse(json);
String accountId = (String) rootObject.get("accountId");
String projectId = (String) rootObject.get("projectId");
String revision = (String) rootObject.get("revision");
String version = (String) rootObject.get("version");
int datafileVersion = Integer.parseInt(version);
List<Experiment> experiments = parseExperiments((JSONArray) rootObject.get("experiments"));
List<Attribute> attributes;
attributes = parseAttributes((JSONArray) rootObject.get("attributes"));
List<EventType> events = parseEvents((JSONArray) rootObject.get("events"));
List<Audience> audiences = parseAudiences((JSONArray) parser.parse(rootObject.get("audiences").toString()));
List<Group> groups = parseGroups((JSONArray) rootObject.get("groups"));
boolean anonymizeIP = false;
List<LiveVariable> liveVariables = null;
if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V3.toString())) {
liveVariables = parseLiveVariables((JSONArray) rootObject.get("variables"));
anonymizeIP = (Boolean) rootObject.get("anonymizeIP");
}
List<FeatureFlag> featureFlags = null;
List<Rollout> rollouts = null;
if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString())) {
featureFlags = parseFeatureFlags((JSONArray) rootObject.get("featureFlags"));
rollouts = parseRollouts((JSONArray) rootObject.get("rollouts"));
}
return new ProjectConfig(accountId, anonymizeIP, projectId, revision, version, attributes, audiences, events, experiments, featureFlags, groups, liveVariables, rollouts);
} catch (Exception e) {
throw new ConfigParseException("Unable to parse datafile: " + json, e);
}
}
use of com.optimizely.ab.config.ProjectConfig in project java-sdk by optimizely.
the class JsonConfigParser method parseProjectConfig.
@Override
public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParseException {
try {
JSONObject rootObject = new JSONObject(json);
String accountId = rootObject.getString("accountId");
String projectId = rootObject.getString("projectId");
String revision = rootObject.getString("revision");
String version = rootObject.getString("version");
int datafileVersion = Integer.parseInt(version);
List<Experiment> experiments = parseExperiments(rootObject.getJSONArray("experiments"));
List<Attribute> attributes;
attributes = parseAttributes(rootObject.getJSONArray("attributes"));
List<EventType> events = parseEvents(rootObject.getJSONArray("events"));
List<Audience> audiences = parseAudiences(rootObject.getJSONArray("audiences"));
List<Group> groups = parseGroups(rootObject.getJSONArray("groups"));
boolean anonymizeIP = false;
List<LiveVariable> liveVariables = null;
if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V3.toString())) {
liveVariables = parseLiveVariables(rootObject.getJSONArray("variables"));
anonymizeIP = rootObject.getBoolean("anonymizeIP");
}
List<FeatureFlag> featureFlags = null;
List<Rollout> rollouts = null;
if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString())) {
featureFlags = parseFeatureFlags(rootObject.getJSONArray("featureFlags"));
rollouts = parseRollouts(rootObject.getJSONArray("rollouts"));
}
return new ProjectConfig(accountId, anonymizeIP, projectId, revision, version, attributes, audiences, events, experiments, featureFlags, groups, liveVariables, rollouts);
} catch (Exception e) {
throw new ConfigParseException("Unable to parse datafile: " + json, e);
}
}
use of com.optimizely.ab.config.ProjectConfig in project java-sdk by optimizely.
the class ProjectConfigJacksonDeserializer method deserialize.
@Override
public ProjectConfig deserialize(JsonParser parser, DeserializationContext context) throws IOException {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Audience.class, new AudienceJacksonDeserializer());
module.addDeserializer(Group.class, new GroupJacksonDeserializer());
mapper.registerModule(module);
JsonNode node = parser.getCodec().readTree(parser);
String accountId = node.get("accountId").textValue();
String projectId = node.get("projectId").textValue();
String revision = node.get("revision").textValue();
String version = node.get("version").textValue();
int datafileVersion = Integer.parseInt(version);
List<Group> groups = mapper.readValue(node.get("groups").toString(), new TypeReference<List<Group>>() {
});
List<Experiment> experiments = mapper.readValue(node.get("experiments").toString(), new TypeReference<List<Experiment>>() {
});
List<Attribute> attributes;
attributes = mapper.readValue(node.get("attributes").toString(), new TypeReference<List<Attribute>>() {
});
List<EventType> events = mapper.readValue(node.get("events").toString(), new TypeReference<List<EventType>>() {
});
List<Audience> audiences = mapper.readValue(node.get("audiences").toString(), new TypeReference<List<Audience>>() {
});
boolean anonymizeIP = false;
List<LiveVariable> liveVariables = null;
if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V3.toString())) {
liveVariables = mapper.readValue(node.get("variables").toString(), new TypeReference<List<LiveVariable>>() {
});
anonymizeIP = node.get("anonymizeIP").asBoolean();
}
List<FeatureFlag> featureFlags = null;
List<Rollout> rollouts = null;
if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString())) {
featureFlags = mapper.readValue(node.get("featureFlags").toString(), new TypeReference<List<FeatureFlag>>() {
});
rollouts = mapper.readValue(node.get("rollouts").toString(), new TypeReference<List<Rollout>>() {
});
}
return new ProjectConfig(accountId, anonymizeIP, projectId, revision, version, attributes, audiences, events, experiments, featureFlags, groups, liveVariables, rollouts);
}
use of com.optimizely.ab.config.ProjectConfig 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.ProjectConfig in project java-sdk by optimizely.
the class BucketerTest method bucketUserToVariationInOverlappingGroupExperiment.
/**
* Verify that {@link Bucketer#bucket(Experiment, String)} returns a variation when the user falls into an
* experiment within an overlapping group.
*/
@Test
public void bucketUserToVariationInOverlappingGroupExperiment() throws Exception {
final AtomicInteger bucketValue = new AtomicInteger();
Bucketer algorithm = mockBucketAlgorithm(bucketValue);
bucketValue.set(0);
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 \"blah\" is in variation \"e1_vtag1\" of experiment \"overlapping_etag1\".");
assertThat(algorithm.bucket(groupExperiment, "blah"), is(expectedVariation));
}
Aggregations