use of com.optimizely.ab.config.Experiment 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.Experiment in project java-sdk by optimizely.
the class EventBuilder method createConversionEvent.
public LogEvent createConversionEvent(@Nonnull ProjectConfig projectConfig, @Nonnull Map<Experiment, Variation> experimentVariationMap, @Nonnull String userId, @Nonnull String eventId, @Nonnull String eventName, @Nonnull Map<String, String> attributes, @Nonnull Map<String, ?> eventTags) {
if (experimentVariationMap.isEmpty()) {
return null;
}
ArrayList<Decision> decisions = new ArrayList<Decision>();
for (Map.Entry<Experiment, Variation> entry : experimentVariationMap.entrySet()) {
Decision decision = new Decision(entry.getKey().getLayerId(), entry.getKey().getId(), entry.getValue().getId(), false);
decisions.add(decision);
}
EventType eventType = projectConfig.getEventNameMapping().get(eventName);
Event conversionEvent = new Event(System.currentTimeMillis(), UUID.randomUUID().toString(), eventType.getId(), eventType.getKey(), null, EventTagUtils.getRevenueValue(eventTags), eventTags, eventType.getKey(), EventTagUtils.getNumericValue(eventTags));
Snapshot snapshot = new Snapshot(decisions, Arrays.asList(conversionEvent));
Visitor visitor = new Visitor(userId, null, buildAttributeList(projectConfig, attributes), Arrays.asList(snapshot));
List<Visitor> visitors = Arrays.asList(visitor);
EventBatch eventBatch = new EventBatch(clientEngine.getClientEngineValue(), clientVersion, projectConfig.getAccountId(), visitors, projectConfig.getAnonymizeIP(), projectConfig.getProjectId(), projectConfig.getRevision());
String payload = this.serializer.serialize(eventBatch);
return new LogEvent(LogEvent.RequestMethod.POST, EVENT_ENDPOINT, Collections.<String, String>emptyMap(), payload);
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class OptimizelyTest method getVariationWithExperimentKeyForced.
/**
* Verify that the {@link Optimizely#getVariation(String, String, Map<String, String>)} call
* uses forced variation to force the user into the second variation. The mock bucket returns
* the first variation. Then remove the forced variation and confirm that the forced variation is null.
*/
@Test
public void getVariationWithExperimentKeyForced() throws Exception {
Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
Variation forcedVariation = activatedExperiment.getVariations().get(1);
Variation bucketedVariation = activatedExperiment.getVariations().get(0);
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());
Map<String, String> testUserAttributes = new HashMap<String, String>();
if (datafileVersion >= 4) {
testUserAttributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
} else {
testUserAttributes.put("browser_type", "chrome");
}
testUserAttributes.put(testBucketingIdKey, testBucketingId);
Map<String, String> testParams = new HashMap<String, String>();
testParams.put("test", "params");
LogEvent logEventToDispatch = new LogEvent(RequestMethod.GET, "test_url", testParams, "");
when(mockEventBuilder.createImpressionEvent(eq(validProjectConfig), eq(activatedExperiment), eq(forcedVariation), eq(testUserId), eq(testUserAttributes))).thenReturn(logEventToDispatch);
when(mockBucketer.bucket(activatedExperiment, testBucketingId)).thenReturn(bucketedVariation);
// activate the experiment
Variation actualVariation = optimizely.getVariation(activatedExperiment.getKey(), testUserId, testUserAttributes);
assertThat(actualVariation, is(forcedVariation));
optimizely.setForcedVariation(activatedExperiment.getKey(), testUserId, null);
assertEquals(optimizely.getForcedVariation(activatedExperiment.getKey(), testUserId), null);
actualVariation = optimizely.getVariation(activatedExperiment.getKey(), testUserId, testUserAttributes);
assertThat(actualVariation, is(bucketedVariation));
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class DecisionServiceTest method getVariationForcedPrecedesAudienceEval.
/**
* Verify that {@link DecisionService#getVariation(Experiment, String, Map)}
* gives precedence to forced variation bucketing over audience evaluation.
*/
@Test
public void getVariationForcedPrecedesAudienceEval() 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(1);
// user excluded without audiences and whitelisting
assertNull(decisionService.getVariation(experiment, genericUserId, Collections.<String, String>emptyMap()));
logbackVerifier.expectMessage(Level.INFO, "User \"" + genericUserId + "\" does not meet conditions to be in experiment \"etag1\".");
// set the runtimeForcedVariation
validProjectConfig.setForcedVariation(experiment.getKey(), genericUserId, expectedVariation.getKey());
// no attributes provided for a experiment that has an audience
assertThat(decisionService.getVariation(experiment, genericUserId, Collections.<String, String>emptyMap()), is(expectedVariation));
verify(decisionService, never()).getStoredVariation(eq(experiment), any(UserProfile.class));
assertEquals(validProjectConfig.setForcedVariation(experiment.getKey(), genericUserId, null), true);
assertNull(validProjectConfig.getForcedVariation(experiment.getKey(), genericUserId));
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class DecisionServiceTest method getVariationOnNonRunningExperimentWithForcedVariation.
/**
* Verify that {@link DecisionService#getVariation(Experiment, String, Map)}
* gives a null variation on a Experiment that is not running. Set the forced variation.
* And, test to make sure that after setting forced variation, the getVariation still returns
* null.
*/
@Test
public void getVariationOnNonRunningExperimentWithForcedVariation() {
Experiment experiment = validProjectConfig.getExperiments().get(1);
assertFalse(experiment.isRunning());
Variation variation = experiment.getVariations().get(0);
Bucketer bucketer = new Bucketer(validProjectConfig);
DecisionService decisionService = spy(new DecisionService(bucketer, mockErrorHandler, validProjectConfig, null));
// ensure that the not running variation returns null with no forced variation set.
assertNull(decisionService.getVariation(experiment, "userId", Collections.<String, String>emptyMap()));
// we call getVariation 3 times on an experiment that is not running.
logbackVerifier.expectMessage(Level.INFO, "Experiment \"etag2\" is not running.", times(3));
// set a forced variation on the user that got back null
assertTrue(validProjectConfig.setForcedVariation(experiment.getKey(), "userId", variation.getKey()));
// ensure that a user with a forced variation set
// still gets back a null variation if the variation is not running.
assertNull(decisionService.getVariation(experiment, "userId", Collections.<String, String>emptyMap()));
// set the forced variation back to null
assertTrue(validProjectConfig.setForcedVariation(experiment.getKey(), "userId", null));
// test one more time that the getVariation returns null for the experiment that is not running.
assertNull(decisionService.getVariation(experiment, "userId", Collections.<String, String>emptyMap()));
}
Aggregations