use of com.optimizely.ab.event.internal.payload.Decision in project java-sdk by optimizely.
the class EventBuilder method createImpressionEvent.
public LogEvent createImpressionEvent(@Nonnull ProjectConfig projectConfig, @Nonnull Experiment activatedExperiment, @Nonnull Variation variation, @Nonnull String userId, @Nonnull Map<String, String> attributes) {
Decision decision = new Decision(activatedExperiment.getLayerId(), activatedExperiment.getId(), variation.getId(), false);
Event impressionEvent = new Event(System.currentTimeMillis(), UUID.randomUUID().toString(), activatedExperiment.getLayerId(), ACTIVATE_EVENT_KEY, null, null, null, ACTIVATE_EVENT_KEY, null);
Snapshot snapshot = new Snapshot(Arrays.asList(decision), Arrays.asList(impressionEvent));
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.event.internal.payload.Decision in project java-sdk by optimizely.
the class EventBuilderTest method createImpressionEventWithBucketingId.
/**
* Verify {@link com.optimizely.ab.event.internal.payload.EventBatch} event creation
*/
@Test
public void createImpressionEventWithBucketingId() 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 = new HashMap<String, String>();
attributeMap.put(attribute.getKey(), "value");
attributeMap.put(com.optimizely.ab.bucketing.DecisionService.BUCKETING_ATTRIBUTE, "variation");
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");
com.optimizely.ab.event.internal.payload.Attribute feature1 = new com.optimizely.ab.event.internal.payload.Attribute(com.optimizely.ab.bucketing.DecisionService.BUCKETING_ATTRIBUTE, EventBuilder.ATTRIBUTE_KEY_FOR_BUCKETING_ATTRIBUTE, com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE, "variation");
List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures = Arrays.asList(feature, feature1);
LogEvent impressionEvent = builder.createImpressionEvent(projectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
// verify that request endpoint is correct
assertThat(impressionEvent.getEndpointUrl(), is(EventBuilder.EVENT_ENDPOINT));
EventBatch impression = gson.fromJson(impressionEvent.getBody(), EventBatch.class);
// verify payload information
assertThat(impression.getVisitors().get(0).getVisitorId(), is(userId));
assertThat((double) impression.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getTimestamp(), closeTo((double) System.currentTimeMillis(), 1000.0));
assertFalse(impression.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0).getIsCampaignHoldback());
assertThat(impression.getAnonymizeIp(), is(projectConfig.getAnonymizeIP()));
assertThat(impression.getProjectId(), is(projectConfig.getProjectId()));
assertThat(impression.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0), is(expectedDecision));
assertThat(impression.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0).getCampaignId(), is(activatedExperiment.getLayerId()));
assertThat(impression.getAccountId(), is(projectConfig.getAccountId()));
assertThat(impression.getVisitors().get(0).getAttributes(), is(expectedUserFeatures));
assertThat(impression.getClientName(), is(EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue()));
assertThat(impression.getClientVersion(), is(BuildVersionInfo.VERSION));
assertNull(impression.getVisitors().get(0).getSessionId());
}
use of com.optimizely.ab.event.internal.payload.Decision in project java-sdk by optimizely.
the class EventFactory method createVisitor.
private static Visitor createVisitor(ImpressionEvent impressionEvent) {
if (impressionEvent == null) {
return null;
}
UserContext userContext = impressionEvent.getUserContext();
Decision decision = new Decision.Builder().setCampaignId(impressionEvent.getLayerId()).setExperimentId(impressionEvent.getExperimentId()).setVariationId(impressionEvent.getVariationId()).setMetadata(impressionEvent.getMetadata()).setIsCampaignHoldback(false).build();
Event event = new Event.Builder().setTimestamp(impressionEvent.getTimestamp()).setUuid(impressionEvent.getUUID()).setEntityId(impressionEvent.getLayerId()).setKey(ACTIVATE_EVENT_KEY).setType(ACTIVATE_EVENT_KEY).build();
Snapshot snapshot = new Snapshot.Builder().setDecisions(Collections.singletonList(decision)).setEvents(Collections.singletonList(event)).build();
return new Visitor.Builder().setVisitorId(userContext.getUserId()).setAttributes(buildAttributeList(userContext.getProjectConfig(), userContext.getAttributes())).setSnapshots(Collections.singletonList((snapshot))).build();
}
use of com.optimizely.ab.event.internal.payload.Decision in project java-sdk by optimizely.
the class EventBuilderTest method createConversionEventWithBucketingId.
/**
* Verify {@link EventBatch} event creation
*/
@Test
public void createConversionEventWithBucketingId() throws Exception {
// use the "valid" project config and its associated experiment, variation, and attributes
Attribute attribute = validProjectConfig.getAttributes().get(0);
EventType eventType = validProjectConfig.getEventTypes().get(0);
String userId = "userId";
String bucketingId = "bucketingId";
Bucketer mockBucketAlgorithm = mock(Bucketer.class);
List<Experiment> allExperiments = validProjectConfig.getExperiments();
List<Experiment> experimentsForEventKey = validProjectConfig.getExperimentsForEventKey(eventType.getKey());
// call the bucket function.
for (Experiment experiment : allExperiments) {
when(mockBucketAlgorithm.bucket(experiment, bucketingId)).thenReturn(experiment.getVariations().get(0));
}
DecisionService decisionService = new DecisionService(mockBucketAlgorithm, mock(ErrorHandler.class), validProjectConfig, mock(UserProfileService.class));
Map<String, String> attributeMap = new java.util.HashMap<String, String>();
attributeMap.put(attribute.getKey(), AUDIENCE_GRYFFINDOR_VALUE);
attributeMap.put(com.optimizely.ab.bucketing.DecisionService.BUCKETING_ATTRIBUTE, bucketingId);
Map<String, Object> eventTagMap = new HashMap<String, Object>();
eventTagMap.put("boolean_param", false);
eventTagMap.put("string_param", "123");
Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, decisionService, eventType.getKey(), userId, attributeMap);
LogEvent conversionEvent = builder.createConversionEvent(validProjectConfig, experimentVariationMap, userId, eventType.getId(), eventType.getKey(), attributeMap, eventTagMap);
List<Decision> expectedDecisions = new ArrayList<Decision>();
for (Experiment experiment : experimentsForEventKey) {
if (experiment.isRunning()) {
Decision decision = new Decision(experiment.getLayerId(), experiment.getId(), experiment.getVariations().get(0).getId(), false);
expectedDecisions.add(decision);
}
}
// verify that the request endpoint is correct
assertThat(conversionEvent.getEndpointUrl(), is(EventBuilder.EVENT_ENDPOINT));
EventBatch conversion = gson.fromJson(conversionEvent.getBody(), EventBatch.class);
// verify payload information
assertThat(conversion.getVisitors().get(0).getVisitorId(), is(userId));
assertThat((double) conversion.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getTimestamp(), closeTo((double) System.currentTimeMillis(), 1000.0));
assertThat(conversion.getProjectId(), is(validProjectConfig.getProjectId()));
assertThat(conversion.getAccountId(), is(validProjectConfig.getAccountId()));
com.optimizely.ab.event.internal.payload.Attribute attribute1 = new com.optimizely.ab.event.internal.payload.Attribute(attribute.getId(), attribute.getKey(), com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE, AUDIENCE_GRYFFINDOR_VALUE);
com.optimizely.ab.event.internal.payload.Attribute attribute2 = new com.optimizely.ab.event.internal.payload.Attribute(com.optimizely.ab.bucketing.DecisionService.BUCKETING_ATTRIBUTE, EventBuilder.ATTRIBUTE_KEY_FOR_BUCKETING_ATTRIBUTE, com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE, bucketingId);
List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures = Arrays.asList(attribute1, attribute2);
assertEquals(conversion.getVisitors().get(0).getAttributes(), expectedUserFeatures);
assertThat(conversion.getVisitors().get(0).getSnapshots().get(0).getDecisions(), containsInAnyOrder(expectedDecisions.toArray()));
assertEquals(conversion.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getEntityId(), eventType.getId());
assertEquals(conversion.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getType(), eventType.getKey());
assertEquals(conversion.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getKey(), eventType.getKey());
assertEquals(conversion.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getRevenue(), null);
assertEquals(conversion.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getQuantity(), null);
assertTrue(conversion.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getTags().equals(eventTagMap));
assertFalse(conversion.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0).getIsCampaignHoldback());
assertEquals(conversion.getAnonymizeIp(), validProjectConfig.getAnonymizeIP());
assertEquals(conversion.getClientName(), EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue());
assertEquals(conversion.getClientVersion(), BuildVersionInfo.VERSION);
}
use of com.optimizely.ab.event.internal.payload.Decision 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());
}
Aggregations