Search in sources :

Example 61 with LogEvent

use of com.optimizely.ab.event.LogEvent in project java-sdk by optimizely.

the class EventFactoryTest method createConversionParamsWithEventMetrics.

/**
 * Verify that "revenue" and "value" are properly recorded in a conversion request as {@link com.optimizely.ab.event.internal.payload.Event} objects.
 * "revenue" is fixed-point and "value" is floating-point.
 */
@Test
public void createConversionParamsWithEventMetrics() throws Exception {
    Long revenue = 1234L;
    Double value = 13.37;
    // use the "valid" project config and its associated experiment, variation, and attributes
    Attribute attribute = validProjectConfig.getAttributes().get(0);
    EventType eventType = validProjectConfig.getEventTypes().get(0);
    Bucketer mockBucketAlgorithm = mock(Bucketer.class);
    // Bucket to the first variation for all experiments.
    for (Experiment experiment : validProjectConfig.getExperiments()) {
        when(mockBucketAlgorithm.bucket(experiment, userId, validProjectConfig)).thenReturn(DecisionResponse.responseNoReasons(experiment.getVariations().get(0)));
    }
    Map<String, String> attributeMap = Collections.singletonMap(attribute.getKey(), "value");
    Map<String, Object> eventTagMap = new HashMap<String, Object>();
    eventTagMap.put(ReservedEventKey.REVENUE.toString(), revenue);
    eventTagMap.put(ReservedEventKey.VALUE.toString(), value);
    LogEvent conversionEvent = createConversionEvent(validProjectConfig, userId, eventType.getId(), eventType.getKey(), attributeMap, eventTagMap);
    EventBatch conversion = gson.fromJson(conversionEvent.getBody(), EventBatch.class);
    // we're not going to verify everything, only the event metrics
    assertThat(conversion.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getRevenue().longValue(), is(revenue));
    assertThat(conversion.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getValue().doubleValue(), is(value));
}
Also used : ControlAttribute(com.optimizely.ab.internal.ControlAttribute) LogEvent(com.optimizely.ab.event.LogEvent) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Bucketer(com.optimizely.ab.bucketing.Bucketer) Test(org.junit.Test)

Example 62 with LogEvent

use of com.optimizely.ab.event.LogEvent in project java-sdk by optimizely.

the class EventFactoryTest 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";
    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");
    LogEvent conversionEvent = createConversionEvent(validProjectConfig, userId, eventType.getId(), eventType.getKey(), attributeMap, eventTagMap);
    // verify that the request endpoint is correct
    assertThat(conversionEvent.getEndpointUrl(), is(EventFactory.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.Builder().setEntityId(attribute.getId()).setKey(attribute.getKey()).setType(com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE).setValue(AUDIENCE_GRYFFINDOR_VALUE).build();
    List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures = new ArrayList<com.optimizely.ab.event.internal.payload.Attribute>();
    expectedUserFeatures.add(feature);
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString())) {
        expectedUserFeatures.add(getBotFilteringAttribute());
    }
    assertEquals(conversion.getVisitors().get(0).getAttributes(), expectedUserFeatures);
    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));
    assertEquals(conversion.getAnonymizeIp(), validProjectConfig.getAnonymizeIP());
    assertTrue(conversion.getEnrichDecisions());
    assertEquals(conversion.getClientName(), EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue());
    assertEquals(conversion.getClientVersion(), BuildVersionInfo.VERSION);
}
Also used : ControlAttribute(com.optimizely.ab.internal.ControlAttribute) LogEvent(com.optimizely.ab.event.LogEvent) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Example 63 with LogEvent

use of com.optimizely.ab.event.LogEvent in project java-sdk by optimizely.

the class EventFactoryTest 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";
    Map<String, String> attributeMap = new java.util.HashMap<String, String>();
    attributeMap.put(attribute.getKey(), AUDIENCE_GRYFFINDOR_VALUE);
    attributeMap.put(ControlAttribute.BUCKETING_ATTRIBUTE.toString(), bucketingId);
    Map<String, Object> eventTagMap = new HashMap<String, Object>();
    eventTagMap.put("boolean_param", false);
    eventTagMap.put("string_param", "123");
    LogEvent conversionEvent = createConversionEvent(validProjectConfig, userId, eventType.getId(), eventType.getKey(), attributeMap, eventTagMap);
    // verify that the request endpoint is correct
    assertThat(conversionEvent.getEndpointUrl(), is(EventFactory.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.Builder().setEntityId(attribute.getId()).setKey(attribute.getKey()).setType(com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE).setValue(AUDIENCE_GRYFFINDOR_VALUE).build();
    com.optimizely.ab.event.internal.payload.Attribute attribute2 = 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(bucketingId).build();
    List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures = new ArrayList<com.optimizely.ab.event.internal.payload.Attribute>();
    expectedUserFeatures.add(attribute1);
    expectedUserFeatures.add(attribute2);
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString())) {
        expectedUserFeatures.add(getBotFilteringAttribute());
    }
    assertEquals(conversion.getVisitors().get(0).getAttributes(), expectedUserFeatures);
    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));
    assertEquals(conversion.getAnonymizeIp(), validProjectConfig.getAnonymizeIP());
    assertTrue(conversion.getEnrichDecisions());
    assertEquals(conversion.getClientName(), EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue());
    assertEquals(conversion.getClientVersion(), BuildVersionInfo.VERSION);
}
Also used : ControlAttribute(com.optimizely.ab.internal.ControlAttribute) LogEvent(com.optimizely.ab.event.LogEvent) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Example 64 with LogEvent

use of com.optimizely.ab.event.LogEvent in project java-sdk by optimizely.

the class EventFactoryTest method createImpressionEventAndroidTVClientEngineClientVersion.

/**
 * Verify that supplying {@link ClientEngineInfo} 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";
    ClientEngineInfo.setClientEngine(EventBatch.ClientEngine.ANDROID_TV_SDK);
    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 = 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 : ControlAttribute(com.optimizely.ab.internal.ControlAttribute) LogEvent(com.optimizely.ab.event.LogEvent) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Example 65 with LogEvent

use of com.optimizely.ab.event.LogEvent in project java-sdk by optimizely.

the class NotificationCenterTest method testSendWithHandler.

@Test
public void testSendWithHandler() {
    testSendWithNotification(new TrackNotification());
    testSendWithNotification(new DecisionNotification());
    testSendWithNotification(new ActivateNotification());
    testSendWithNotification(new LogEvent(LogEvent.RequestMethod.GET, "localhost", Collections.emptyMap(), null));
}
Also used : LogEvent(com.optimizely.ab.event.LogEvent) Test(org.junit.Test)

Aggregations

LogEvent (com.optimizely.ab.event.LogEvent)66 Test (org.junit.Test)58 Experiment (com.optimizely.ab.config.Experiment)37 Variation (com.optimizely.ab.config.Variation)34 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)32 HashMap (java.util.HashMap)25 EventType (com.optimizely.ab.config.EventType)23 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)21 Matchers.anyString (org.mockito.Matchers.anyString)21 ControlAttribute (com.optimizely.ab.internal.ControlAttribute)17 Map (java.util.Map)14 ImmutableMap (com.google.common.collect.ImmutableMap)11 Bucketer (com.optimizely.ab.bucketing.Bucketer)11 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)11 ProjectConfig (com.optimizely.ab.config.ProjectConfig)10 Attribute (com.optimizely.ab.config.Attribute)9 Decision (com.optimizely.ab.event.internal.payload.Decision)9 DecisionService (com.optimizely.ab.bucketing.DecisionService)8 UserProfileService (com.optimizely.ab.bucketing.UserProfileService)6 NoOpErrorHandler (com.optimizely.ab.error.NoOpErrorHandler)6