Search in sources :

Example 6 with Decision

use of com.optimizely.ab.event.internal.payload.Decision in project java-sdk by optimizely.

the class EventBuilderTest method createConversionEvent.

/**
 * Verify {@link com.optimizely.ab.event.internal.payload.EventBatch} event creation
 */
@Test
public void createConversionEvent() 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";
    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, userId)).thenReturn(experiment.getVariations().get(0));
    }
    DecisionService decisionService = new DecisionService(mockBucketAlgorithm, mock(ErrorHandler.class), validProjectConfig, mock(UserProfileService.class));
    Map<String, String> attributeMap = Collections.singletonMap(attribute.getKey(), AUDIENCE_GRYFFINDOR_VALUE);
    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 layerState = new Decision(experiment.getLayerId(), experiment.getId(), experiment.getVariations().get(0).getId(), false);
            expectedDecisions.add(layerState);
        }
    }
    // 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(), 120.0));
    assertThat(conversion.getProjectId(), is(validProjectConfig.getProjectId()));
    assertThat(conversion.getAccountId(), is(validProjectConfig.getAccountId()));
    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, AUDIENCE_GRYFFINDOR_VALUE);
    List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures = Collections.singletonList(feature);
    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).getKey(), eventType.getKey());
    assertEquals(conversion.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getRevenue(), null);
    assertTrue(conversion.getVisitors().get(0).getAttributes().containsAll(expectedUserFeatures));
    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);
}
Also used : Attribute(com.optimizely.ab.config.Attribute) EventType(com.optimizely.ab.config.EventType) HashMap(java.util.HashMap) UserProfileService(com.optimizely.ab.bucketing.UserProfileService) ArrayList(java.util.ArrayList) DecisionService(com.optimizely.ab.bucketing.DecisionService) NoOpErrorHandler(com.optimizely.ab.error.NoOpErrorHandler) ErrorHandler(com.optimizely.ab.error.ErrorHandler) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Decision(com.optimizely.ab.event.internal.payload.Decision) Variation(com.optimizely.ab.config.Variation) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Bucketer(com.optimizely.ab.bucketing.Bucketer) Test(org.junit.Test)

Example 7 with Decision

use of com.optimizely.ab.event.internal.payload.Decision 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);
}
Also used : Visitor(com.optimizely.ab.event.internal.payload.Visitor) EventType(com.optimizely.ab.config.EventType) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) Decision(com.optimizely.ab.event.internal.payload.Decision) Snapshot(com.optimizely.ab.event.internal.payload.Snapshot) Event(com.optimizely.ab.event.internal.payload.Event) LogEvent(com.optimizely.ab.event.LogEvent) Variation(com.optimizely.ab.config.Variation) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Map(java.util.Map)

Example 8 with Decision

use of com.optimizely.ab.event.internal.payload.Decision in project java-sdk by optimizely.

the class EventFactoryTest 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
    Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
    Variation bucketedVariation = activatedExperiment.getVariations().get(0);
    Attribute attribute = validProjectConfig.getAttributes().get(0);
    String userId = "userId";
    Map<String, String> attributeMap = Collections.singletonMap(attribute.getKey(), "value");
    DecisionMetadata decisionMetadata = new DecisionMetadata.Builder().setFlagKey(activatedExperiment.getKey()).setRuleType("experiment").setVariationKey(bucketedVariation.getKey()).build();
    Decision expectedDecision = new Decision.Builder().setCampaignId(activatedExperiment.getLayerId()).setExperimentId(activatedExperiment.getId()).setVariationId(bucketedVariation.getId()).setMetadata(decisionMetadata).setIsCampaignHoldback(false).build();
    com.optimizely.ab.event.internal.payload.Attribute feature = new com.optimizely.ab.event.internal.payload.Attribute.Builder().setEntityId(attribute.getId()).setKey(attribute.getKey()).setType(com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE).setValue("value").build();
    com.optimizely.ab.event.internal.payload.Attribute botFilteringFeature = getBotFilteringAttribute();
    List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures;
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()))
        expectedUserFeatures = Arrays.asList(feature, botFilteringFeature);
    else
        expectedUserFeatures = Arrays.asList(feature);
    LogEvent impressionEvent = createImpressionEvent(validProjectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
    // verify that request endpoint is correct
    assertThat(impressionEvent.getEndpointUrl(), is(EventFactory.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(validProjectConfig.getAnonymizeIP()));
    assertTrue(eventBatch.getEnrichDecisions());
    assertThat(eventBatch.getProjectId(), is(validProjectConfig.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(validProjectConfig.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());
}
Also used : ControlAttribute(com.optimizely.ab.internal.ControlAttribute) LogEvent(com.optimizely.ab.event.LogEvent) GsonBuilder(com.google.gson.GsonBuilder) Decision(com.optimizely.ab.event.internal.payload.Decision) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) DecisionMetadata(com.optimizely.ab.event.internal.payload.DecisionMetadata) Test(org.junit.Test)

Example 9 with Decision

use of com.optimizely.ab.event.internal.payload.Decision in project java-sdk by optimizely.

the class EventFactoryTest method createImpressionEventPassingUserAgentAttribute.

/**
 * Verify {@link com.optimizely.ab.event.internal.payload.EventBatch} event creation
 */
@Test
public void createImpressionEventPassingUserAgentAttribute() throws Exception {
    // use the "valid" project config and its associated experiment, variation, and attributes
    Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
    Variation bucketedVariation = activatedExperiment.getVariations().get(0);
    Attribute attribute = validProjectConfig.getAttributes().get(0);
    String userId = "userId";
    String ruleType = "experiment";
    Map<String, String> attributeMap = new HashMap<String, String>();
    attributeMap.put(attribute.getKey(), "value");
    attributeMap.put(ControlAttribute.USER_AGENT_ATTRIBUTE.toString(), "Chrome");
    DecisionMetadata metadata = new DecisionMetadata(activatedExperiment.getKey(), activatedExperiment.getKey(), ruleType, "variationKey", true);
    Decision expectedDecision = new Decision.Builder().setCampaignId(activatedExperiment.getLayerId()).setExperimentId(activatedExperiment.getId()).setVariationId(bucketedVariation.getId()).setMetadata(metadata).setIsCampaignHoldback(false).build();
    com.optimizely.ab.event.internal.payload.Attribute feature = new com.optimizely.ab.event.internal.payload.Attribute.Builder().setEntityId(attribute.getId()).setKey(attribute.getKey()).setType(com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE).setValue("value").build();
    com.optimizely.ab.event.internal.payload.Attribute userAgentFeature = new com.optimizely.ab.event.internal.payload.Attribute.Builder().setEntityId(ControlAttribute.USER_AGENT_ATTRIBUTE.toString()).setKey(ControlAttribute.USER_AGENT_ATTRIBUTE.toString()).setType(com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE).setValue("Chrome").build();
    com.optimizely.ab.event.internal.payload.Attribute botFilteringFeature = getBotFilteringAttribute();
    List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures;
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()))
        expectedUserFeatures = Arrays.asList(userAgentFeature, feature, botFilteringFeature);
    else
        expectedUserFeatures = Arrays.asList(userAgentFeature, feature);
    LogEvent impressionEvent = createImpressionEvent(validProjectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
    // verify that request endpoint is correct
    assertThat(impressionEvent.getEndpointUrl(), is(EventFactory.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(validProjectConfig.getAnonymizeIP()));
    assertTrue(eventBatch.getEnrichDecisions());
    assertThat(eventBatch.getProjectId(), is(validProjectConfig.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(validProjectConfig.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());
}
Also used : ControlAttribute(com.optimizely.ab.internal.ControlAttribute) LogEvent(com.optimizely.ab.event.LogEvent) GsonBuilder(com.google.gson.GsonBuilder) Decision(com.optimizely.ab.event.internal.payload.Decision) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) DecisionMetadata(com.optimizely.ab.event.internal.payload.DecisionMetadata) Test(org.junit.Test)

Example 10 with Decision

use of com.optimizely.ab.event.internal.payload.Decision in project java-sdk by optimizely.

the class EventFactoryTest 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 = validProjectConfig;
    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(ControlAttribute.BUCKETING_ATTRIBUTE.toString(), "variation");
    Decision expectedDecision = new Decision.Builder().setCampaignId(activatedExperiment.getLayerId()).setExperimentId(activatedExperiment.getId()).setVariationId(bucketedVariation.getId()).setIsCampaignHoldback(false).build();
    com.optimizely.ab.event.internal.payload.Attribute feature = new com.optimizely.ab.event.internal.payload.Attribute.Builder().setEntityId(attribute.getId()).setKey(attribute.getKey()).setType(com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE).setValue("value").build();
    com.optimizely.ab.event.internal.payload.Attribute feature1 = new com.optimizely.ab.event.internal.payload.Attribute.Builder().setEntityId(ControlAttribute.BUCKETING_ATTRIBUTE.toString()).setKey(ControlAttribute.BUCKETING_ATTRIBUTE.toString()).setType(com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE).setValue("variation").build();
    List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures = new ArrayList<com.optimizely.ab.event.internal.payload.Attribute>();
    expectedUserFeatures.add(feature);
    expectedUserFeatures.add(feature1);
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString())) {
        expectedUserFeatures.add(getBotFilteringAttribute());
    }
    LogEvent impressionEvent = createImpressionEvent(projectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
    // verify that request endpoint is correct
    assertThat(impressionEvent.getEndpointUrl(), is(EventFactory.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()));
    assertTrue(impression.getEnrichDecisions());
    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());
}
Also used : ControlAttribute(com.optimizely.ab.internal.ControlAttribute) LogEvent(com.optimizely.ab.event.LogEvent) Decision(com.optimizely.ab.event.internal.payload.Decision) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Aggregations

LogEvent (com.optimizely.ab.event.LogEvent)10 Decision (com.optimizely.ab.event.internal.payload.Decision)10 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)9 Test (org.junit.Test)7 Experiment (com.optimizely.ab.config.Experiment)5 Variation (com.optimizely.ab.config.Variation)5 Attribute (com.optimizely.ab.config.Attribute)4 EventType (com.optimizely.ab.config.EventType)3 Event (com.optimizely.ab.event.internal.payload.Event)3 Snapshot (com.optimizely.ab.event.internal.payload.Snapshot)3 ControlAttribute (com.optimizely.ab.internal.ControlAttribute)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 GsonBuilder (com.google.gson.GsonBuilder)2 Bucketer (com.optimizely.ab.bucketing.Bucketer)2 DecisionService (com.optimizely.ab.bucketing.DecisionService)2 UserProfileService (com.optimizely.ab.bucketing.UserProfileService)2 ProjectConfig (com.optimizely.ab.config.ProjectConfig)2 ErrorHandler (com.optimizely.ab.error.ErrorHandler)2 NoOpErrorHandler (com.optimizely.ab.error.NoOpErrorHandler)2