use of com.optimizely.ab.config.ProjectConfig in project java-sdk by optimizely.
the class EventBuilderTest method createImpressionEvent.
/**
* Verify {@link com.optimizely.ab.event.internal.payload.EventBatch} event creation
*/
@Test
public void createImpressionEvent() throws Exception {
// use the "valid" project config and its associated experiment, variation, and attributes
ProjectConfig projectConfig = validProjectConfigV2();
Experiment activatedExperiment = projectConfig.getExperiments().get(0);
Variation bucketedVariation = activatedExperiment.getVariations().get(0);
Attribute attribute = projectConfig.getAttributes().get(0);
String userId = "userId";
Map<String, String> attributeMap = Collections.singletonMap(attribute.getKey(), "value");
Decision expectedDecision = new Decision(activatedExperiment.getLayerId(), activatedExperiment.getId(), bucketedVariation.getId(), false);
com.optimizely.ab.event.internal.payload.Attribute feature = new com.optimizely.ab.event.internal.payload.Attribute(attribute.getId(), attribute.getKey(), com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE, "value");
List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures = Collections.singletonList(feature);
LogEvent impressionEvent = builder.createImpressionEvent(projectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
// verify that request endpoint is correct
assertThat(impressionEvent.getEndpointUrl(), is(EventBuilder.EVENT_ENDPOINT));
EventBatch eventBatch = gson.fromJson(impressionEvent.getBody(), EventBatch.class);
// verify payload information
assertThat(eventBatch.getVisitors().get(0).getVisitorId(), is(userId));
assertThat((double) eventBatch.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getTimestamp(), closeTo((double) System.currentTimeMillis(), 1000.0));
assertFalse(eventBatch.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0).getIsCampaignHoldback());
assertThat(eventBatch.getAnonymizeIp(), is(projectConfig.getAnonymizeIP()));
assertThat(eventBatch.getProjectId(), is(projectConfig.getProjectId()));
assertThat(eventBatch.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0), is(expectedDecision));
assertThat(eventBatch.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0).getCampaignId(), is(activatedExperiment.getLayerId()));
assertThat(eventBatch.getAccountId(), is(projectConfig.getAccountId()));
assertThat(eventBatch.getVisitors().get(0).getAttributes(), is(expectedUserFeatures));
assertThat(eventBatch.getClientName(), is(EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue()));
assertThat(eventBatch.getClientVersion(), is(BuildVersionInfo.VERSION));
assertNull(eventBatch.getVisitors().get(0).getSessionId());
}
use of com.optimizely.ab.config.ProjectConfig in project java-sdk by optimizely.
the class ProjectConfigGsonDeserializer method deserialize.
@Override
public ProjectConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
String accountId = jsonObject.get("accountId").getAsString();
String projectId = jsonObject.get("projectId").getAsString();
String revision = jsonObject.get("revision").getAsString();
String version = jsonObject.get("version").getAsString();
int datafileVersion = Integer.parseInt(version);
// generic list type tokens
Type groupsType = new TypeToken<List<Group>>() {
}.getType();
Type experimentsType = new TypeToken<List<Experiment>>() {
}.getType();
Type attributesType = new TypeToken<List<Attribute>>() {
}.getType();
Type eventsType = new TypeToken<List<EventType>>() {
}.getType();
Type audienceType = new TypeToken<List<Audience>>() {
}.getType();
List<Group> groups = context.deserialize(jsonObject.get("groups").getAsJsonArray(), groupsType);
List<Experiment> experiments = context.deserialize(jsonObject.get("experiments").getAsJsonArray(), experimentsType);
List<Attribute> attributes;
attributes = context.deserialize(jsonObject.get("attributes"), attributesType);
List<EventType> events = context.deserialize(jsonObject.get("events").getAsJsonArray(), eventsType);
List<Audience> audiences = context.deserialize(jsonObject.get("audiences").getAsJsonArray(), audienceType);
boolean anonymizeIP = false;
// live variables should be null if using V2
List<LiveVariable> liveVariables = null;
if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V3.toString())) {
Type liveVariablesType = new TypeToken<List<LiveVariable>>() {
}.getType();
liveVariables = context.deserialize(jsonObject.getAsJsonArray("variables"), liveVariablesType);
anonymizeIP = jsonObject.get("anonymizeIP").getAsBoolean();
}
List<FeatureFlag> featureFlags = null;
List<Rollout> rollouts = null;
if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString())) {
Type featureFlagsType = new TypeToken<List<FeatureFlag>>() {
}.getType();
featureFlags = context.deserialize(jsonObject.getAsJsonArray("featureFlags"), featureFlagsType);
Type rolloutsType = new TypeToken<List<Rollout>>() {
}.getType();
rollouts = context.deserialize(jsonObject.get("rollouts").getAsJsonArray(), rolloutsType);
}
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 GsonConfigParserTest method parseFeatureVariablesWithJsonPatched.
@Test
public void parseFeatureVariablesWithJsonPatched() throws Exception {
JsonSimpleConfigParser parser = new JsonSimpleConfigParser();
ProjectConfig actual = parser.parseProjectConfig(validConfigJsonV4());
// "string" type + "json" subType
FeatureFlag featureFlag = actual.getFeatureKeyMapping().get("multi_variate_feature");
FeatureVariable variable = featureFlag.getVariableKeyToFeatureVariableMap().get("json_patched");
assertEquals(variable.getType(), "json");
}
use of com.optimizely.ab.config.ProjectConfig in project java-sdk by optimizely.
the class JsonConfigParserTest method parseNullFeatureEnabledProjectConfigV4.
@Test
public void parseNullFeatureEnabledProjectConfigV4() throws Exception {
JsonConfigParser parser = new JsonConfigParser();
ProjectConfig actual = parser.parseProjectConfig(nullFeatureEnabledConfigJsonV4());
assertNotNull(actual);
assertNotNull(actual.getExperiments());
assertNotNull(actual.getFeatureFlags());
}
use of com.optimizely.ab.config.ProjectConfig in project java-sdk by optimizely.
the class JsonConfigParserTest method parseFeatureVariablesWithFutureType.
@Test
public void parseFeatureVariablesWithFutureType() throws Exception {
JsonSimpleConfigParser parser = new JsonSimpleConfigParser();
ProjectConfig actual = parser.parseProjectConfig(validConfigJsonV4());
// unknown type
FeatureFlag featureFlag = actual.getFeatureKeyMapping().get("multi_variate_future_feature");
FeatureVariable variable = featureFlag.getVariableKeyToFeatureVariableMap().get("future_variable");
assertEquals(variable.getType(), "future_type");
}
Aggregations