Search in sources :

Example 21 with EventBatch

use of com.optimizely.ab.event.internal.payload.EventBatch 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 22 with EventBatch

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

the class GsonSerializerTest method serializeConversion.

@Test
public void serializeConversion() throws IOException {
    EventBatch conversion = generateConversion();
    // can't compare JSON strings since orders could vary so compare objects instead
    EventBatch actual = gson.fromJson(serializer.serialize(conversion), EventBatch.class);
    EventBatch expected = gson.fromJson(generateConversionJson(), EventBatch.class);
    assertThat(actual, is(expected));
}
Also used : EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Example 23 with EventBatch

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

the class GsonSerializerTest method serializeConversionWithSessionId.

@Test
public void serializeConversionWithSessionId() throws Exception {
    EventBatch conversion = generateConversionWithSessionId();
    // can't compare JSON strings since orders could vary so compare objects instead
    EventBatch actual = gson.fromJson(serializer.serialize(conversion), EventBatch.class);
    EventBatch expected = gson.fromJson(generateConversionWithSessionIdJson(), EventBatch.class);
    assertThat(actual, is(expected));
}
Also used : EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Example 24 with EventBatch

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

the class JsonSimpleSerializerTest method serializeConversionWithSessionId.

@Test
public void serializeConversionWithSessionId() throws IOException, ParseException {
    EventBatch conversion = generateConversionWithSessionId();
    // can't compare JSON strings since orders could vary so compare JSONObjects instead
    JSONObject actual = (JSONObject) parser.parse(serializer.serialize(conversion));
    JSONObject expected = (JSONObject) parser.parse(generateConversionWithSessionIdJson());
    assertThat(actual, is(expected));
}
Also used : JSONObject(org.json.simple.JSONObject) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Example 25 with EventBatch

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

the class JsonSimpleSerializerTest method serializeImpression.

@Test
public void serializeImpression() throws IOException, ParseException {
    EventBatch impression = generateImpression();
    // can't compare JSON strings since orders could vary so compare JSONObjects instead
    JSONObject actual = (JSONObject) parser.parse(serializer.serialize(impression));
    JSONObject expected = (JSONObject) parser.parse(generateImpressionJson());
    assertThat(actual, is(expected));
}
Also used : JSONObject(org.json.simple.JSONObject) 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