Search in sources :

Example 1 with EventBatch

use of com.optimizely.ab.event.internal.payload.EventBatch 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);
}
Also used : Snapshot(com.optimizely.ab.event.internal.payload.Snapshot) Visitor(com.optimizely.ab.event.internal.payload.Visitor) LogEvent(com.optimizely.ab.event.LogEvent) Event(com.optimizely.ab.event.internal.payload.Event) LogEvent(com.optimizely.ab.event.LogEvent) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Decision(com.optimizely.ab.event.internal.payload.Decision)

Example 2 with EventBatch

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

the class EventBuilderTest method createImpressionEventIgnoresUnknownAttributes.

/**
 * Verify that passing through an unknown attribute causes that attribute to be ignored, rather than
 * causing an exception to be thrown.
 */
@Test
public void createImpressionEventIgnoresUnknownAttributes() 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);
    LogEvent impressionEvent = builder.createImpressionEvent(projectConfig, activatedExperiment, bucketedVariation, "userId", Collections.singletonMap("unknownAttribute", "blahValue"));
    EventBatch impression = gson.fromJson(impressionEvent.getBody(), EventBatch.class);
    // verify that no Feature is created for "unknownAtrribute" -> "blahValue"
    for (com.optimizely.ab.event.internal.payload.Attribute feature : impression.getVisitors().get(0).getAttributes()) {
        assertFalse(feature.getKey() == "unknownAttribute");
        assertFalse(feature.getValue() == "blahValue");
    }
}
Also used : ProjectConfig(com.optimizely.ab.config.ProjectConfig) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Variation(com.optimizely.ab.config.Variation) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Example 3 with EventBatch

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

the class EventBuilderTest method createConversionEventAndroidClientEngineClientVersion.

/**
 * Verify that supplying {@link EventBuilder} with a custom client engine and client version results in conversion
 * events being sent with the overriden values.
 */
@Test
public void createConversionEventAndroidClientEngineClientVersion() throws Exception {
    EventBuilder builder = new EventBuilder(EventBatch.ClientEngine.ANDROID_SDK, "0.0.0");
    Attribute attribute = validProjectConfig.getAttributes().get(0);
    EventType eventType = validProjectConfig.getEventTypes().get(0);
    Bucketer mockBucketAlgorithm = mock(Bucketer.class);
    for (Experiment experiment : validProjectConfig.getExperiments()) {
        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(), "value");
    Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, decisionService, eventType.getKey(), userId, attributeMap);
    LogEvent conversionEvent = builder.createConversionEvent(validProjectConfig, experimentVariationMap, userId, eventType.getId(), eventType.getKey(), attributeMap, Collections.<String, Object>emptyMap());
    EventBatch conversion = gson.fromJson(conversionEvent.getBody(), EventBatch.class);
    assertThat(conversion.getClientName(), is(EventBatch.ClientEngine.ANDROID_SDK.getClientEngineValue()));
    assertThat(conversion.getClientVersion(), is("0.0.0"));
}
Also used : NoOpErrorHandler(com.optimizely.ab.error.NoOpErrorHandler) ErrorHandler(com.optimizely.ab.error.ErrorHandler) Attribute(com.optimizely.ab.config.Attribute) EventType(com.optimizely.ab.config.EventType) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) UserProfileService(com.optimizely.ab.bucketing.UserProfileService) DecisionService(com.optimizely.ab.bucketing.DecisionService) 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 4 with EventBatch

use of com.optimizely.ab.event.internal.payload.EventBatch 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());
}
Also used : Attribute(com.optimizely.ab.config.Attribute) HashMap(java.util.HashMap) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Decision(com.optimizely.ab.event.internal.payload.Decision) ProjectConfig(com.optimizely.ab.config.ProjectConfig) Variation(com.optimizely.ab.config.Variation) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Example 5 with EventBatch

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

the class EventBuilderTest method createImpressionEventAndroidTVClientEngineClientVersion.

/**
 * Verify that supplying {@link EventBuilder} with a custom Android TV client engine and client version
 * results in impression events being sent with the overriden values.
 */
@Test
public void createImpressionEventAndroidTVClientEngineClientVersion() throws Exception {
    String clientVersion = "0.0.0";
    EventBuilder builder = new EventBuilder(EventBatch.ClientEngine.ANDROID_TV_SDK, clientVersion);
    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");
    LogEvent impressionEvent = builder.createImpressionEvent(projectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
    EventBatch impression = gson.fromJson(impressionEvent.getBody(), EventBatch.class);
    assertThat(impression.getClientName(), is(EventBatch.ClientEngine.ANDROID_TV_SDK.getClientEngineValue()));
    assertThat(impression.getClientVersion(), is(clientVersion));
}
Also used : ProjectConfig(com.optimizely.ab.config.ProjectConfig) Attribute(com.optimizely.ab.config.Attribute) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Variation(com.optimizely.ab.config.Variation) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Aggregations

EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)33 Test (org.junit.Test)27 LogEvent (com.optimizely.ab.event.LogEvent)13 Experiment (com.optimizely.ab.config.Experiment)12 Variation (com.optimizely.ab.config.Variation)12 Attribute (com.optimizely.ab.config.Attribute)9 EventType (com.optimizely.ab.config.EventType)7 Bucketer (com.optimizely.ab.bucketing.Bucketer)6 ProjectConfig (com.optimizely.ab.config.ProjectConfig)6 Decision (com.optimizely.ab.event.internal.payload.Decision)6 DecisionService (com.optimizely.ab.bucketing.DecisionService)5 UserProfileService (com.optimizely.ab.bucketing.UserProfileService)5 NoOpErrorHandler (com.optimizely.ab.error.NoOpErrorHandler)5 HashMap (java.util.HashMap)5 ErrorHandler (com.optimizely.ab.error.ErrorHandler)4 JSONObject (org.json.JSONObject)4 JSONObject (org.json.simple.JSONObject)4 Snapshot (com.optimizely.ab.event.internal.payload.Snapshot)3 Visitor (com.optimizely.ab.event.internal.payload.Visitor)3 ArrayList (java.util.ArrayList)3