use of com.optimizely.ab.config.EventType in project java-sdk by optimizely.
the class OptimizelyTest method trackEventWithNullAttributes.
/**
* Verify that {@link Optimizely#track(String, String)} ignores null attributes.
*/
@Test
@SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "testing nullness contract violation")
public void trackEventWithNullAttributes() 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, Object>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
Map<String, String> attributes = null;
optimizely.track(eventType.getKey(), genericUserId, attributes);
logbackVerifier.expectMessage(Level.WARN, "Attributes is null when non-null was expected. Defaulting to an empty attributes map.");
// 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, is(Collections.<String, String>emptyMap()));
verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
use of com.optimizely.ab.config.EventType 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.EventType 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.EventType 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);
}
Aggregations