Search in sources :

Example 16 with EventType

use of com.optimizely.ab.config.EventType in project java-sdk by optimizely.

the class OptimizelyTest method addNotificationListenerFromNotificationCenter.

/**
 * Verify that {@link com.optimizely.ab.notification.NotificationCenter#addNotificationListener(
 * com.optimizely.ab.notification.NotificationCenter.NotificationType,
 * com.optimizely.ab.notification.NotificationListener)} properly used
 *  and the listener is
 * added and notified when an experiment is activated.
 */
@Test
public void addNotificationListenerFromNotificationCenter() throws Exception {
    Experiment activatedExperiment;
    EventType eventType;
    if (datafileVersion >= 4) {
        activatedExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_BASIC_EXPERIMENT_KEY);
        eventType = validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY);
    } else {
        activatedExperiment = validProjectConfig.getExperiments().get(0);
        eventType = validProjectConfig.getEventTypes().get(0);
    }
    Variation bucketedVariation = activatedExperiment.getVariations().get(0);
    EventBuilder mockEventBuilder = mock(EventBuilder.class);
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withDecisionService(mockDecisionService).withEventBuilder(mockEventBuilder).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
    Map<String, String> attributes = Collections.emptyMap();
    Map<String, String> testParams = new HashMap<String, String>();
    testParams.put("test", "params");
    LogEvent logEventToDispatch = new LogEvent(RequestMethod.GET, "test_url", testParams, "");
    when(mockEventBuilder.createImpressionEvent(validProjectConfig, activatedExperiment, bucketedVariation, genericUserId, attributes)).thenReturn(logEventToDispatch);
    when(mockDecisionService.getVariation(eq(activatedExperiment), eq(genericUserId), eq(Collections.<String, String>emptyMap()))).thenReturn(bucketedVariation);
    // Add listener
    ActivateNotificationListener listener = mock(ActivateNotificationListener.class);
    optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Activate, listener);
    // Check if listener is notified when experiment is activated
    Variation actualVariation = optimizely.activate(activatedExperiment, genericUserId, attributes);
    verify(listener, times(1)).onActivate(activatedExperiment, genericUserId, attributes, bucketedVariation, logEventToDispatch);
    assertEquals(actualVariation.getKey(), bucketedVariation.getKey());
    // Check if listener is notified after an event is tracked
    String eventKey = eventType.getKey();
    Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, mockDecisionService, eventType.getKey(), genericUserId, attributes);
    when(mockEventBuilder.createConversionEvent(eq(validProjectConfig), eq(experimentVariationMap), eq(genericUserId), eq(eventType.getId()), eq(eventKey), eq(attributes), anyMapOf(String.class, Object.class))).thenReturn(logEventToDispatch);
    TrackNotificationListener trackNotification = mock(TrackNotificationListener.class);
    optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Track, trackNotification);
    optimizely.track(eventKey, genericUserId, attributes);
    verify(trackNotification, times(1)).onTrack(eventKey, genericUserId, attributes, Collections.EMPTY_MAP, logEventToDispatch);
}
Also used : EventType(com.optimizely.ab.config.EventType) HashMap(java.util.HashMap) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) EventBuilder(com.optimizely.ab.event.internal.EventBuilder) TrackNotificationListener(com.optimizely.ab.notification.TrackNotificationListener) ActivateNotificationListener(com.optimizely.ab.notification.ActivateNotificationListener) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 17 with EventType

use of com.optimizely.ab.config.EventType in project java-sdk by optimizely.

the class OptimizelyTest method trackEventWithEventTags.

/**
 * Verify that {@link Optimizely#track(String, String, Map, Map)} passes event features to
 * {@link EventBuilder#createConversionEvent(ProjectConfig, Map, String, String, String, Map, Map)}
 */
@Test
public void trackEventWithEventTags() throws Exception {
    EventType eventType;
    if (datafileVersion >= 4) {
        eventType = validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY);
    } else {
        eventType = validProjectConfig.getEventTypes().get(0);
    }
    // setup a mock event builder to return expected conversion params
    EventBuilder mockEventBuilder = mock(EventBuilder.class);
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withEventBuilder(mockEventBuilder).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
    Map<String, String> testParams = new HashMap<String, String>();
    testParams.put("test", "params");
    Map<String, Object> eventTags = new HashMap<String, Object>();
    eventTags.put("int_param", 123);
    eventTags.put("string_param", "123");
    eventTags.put("boolean_param", false);
    eventTags.put("float_param", 12.3f);
    Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, mockDecisionService, eventType.getKey(), genericUserId, Collections.<String, String>emptyMap());
    LogEvent logEventToDispatch = new LogEvent(RequestMethod.GET, "test_url", testParams, "");
    when(mockEventBuilder.createConversionEvent(eq(validProjectConfig), eq(experimentVariationMap), eq(genericUserId), eq(eventType.getId()), eq(eventType.getKey()), anyMapOf(String.class, String.class), eq(eventTags))).thenReturn(logEventToDispatch);
    logbackVerifier.expectMessage(Level.INFO, "Tracking event \"" + eventType.getKey() + "\" for user \"" + genericUserId + "\".");
    logbackVerifier.expectMessage(Level.DEBUG, "Dispatching conversion event to URL test_url with params " + testParams + " and payload \"\"");
    // call track
    optimizely.track(eventType.getKey(), genericUserId, Collections.<String, String>emptyMap(), eventTags);
    // setup the event map captor (so we can verify its content)
    ArgumentCaptor<Map> eventTagCaptor = ArgumentCaptor.forClass(Map.class);
    // verify that the event builder was called with the expected attributes
    verify(mockEventBuilder).createConversionEvent(eq(validProjectConfig), eq(experimentVariationMap), eq(genericUserId), eq(eventType.getId()), eq(eventType.getKey()), eq(Collections.<String, String>emptyMap()), eventTagCaptor.capture());
    Map<String, ?> actualValue = eventTagCaptor.getValue();
    assertThat(actualValue, hasEntry("int_param", eventTags.get("int_param")));
    assertThat(actualValue, hasEntry("string_param", eventTags.get("string_param")));
    assertThat(actualValue, hasEntry("boolean_param", eventTags.get("boolean_param")));
    assertThat(actualValue, hasEntry("float_param", eventTags.get("float_param")));
    verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
Also used : EventType(com.optimizely.ab.config.EventType) HashMap(java.util.HashMap) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) EventBuilder(com.optimizely.ab.event.internal.EventBuilder) Variation(com.optimizely.ab.config.Variation) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) EventBuilderTest.createExperimentVariationMap(com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap) Test(org.junit.Test)

Example 18 with EventType

use of com.optimizely.ab.config.EventType in project java-sdk by optimizely.

the class EventBuilderTest method createConversionEventExperimentStatusPrecedesForcedVariation.

/**
 * Verify that precedence is given to experiment status over forced variation bucketing when constructing a
 * conversion event.
 */
@Test
public void createConversionEventExperimentStatusPrecedesForcedVariation() {
    EventType eventType;
    if (datafileVersion == 4) {
        eventType = validProjectConfig.getEventNameMapping().get(EVENT_PAUSED_EXPERIMENT_KEY);
    } else {
        eventType = validProjectConfig.getEventTypes().get(3);
    }
    String whitelistedUserId = PAUSED_EXPERIMENT_FORCED_VARIATION_USER_ID_CONTROL;
    Bucketer bucketer = spy(new Bucketer(validProjectConfig));
    DecisionService decisionService = new DecisionService(bucketer, mock(ErrorHandler.class), validProjectConfig, mock(UserProfileService.class));
    Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, decisionService, eventType.getKey(), whitelistedUserId, Collections.<String, String>emptyMap());
    LogEvent conversionEvent = builder.createConversionEvent(validProjectConfig, experimentVariationMap, whitelistedUserId, eventType.getId(), eventType.getKey(), Collections.<String, String>emptyMap(), Collections.<String, Object>emptyMap());
    for (Experiment experiment : validProjectConfig.getExperiments()) {
        verify(bucketer, never()).bucket(experiment, whitelistedUserId);
    }
    assertNull(conversionEvent);
}
Also used : NoOpErrorHandler(com.optimizely.ab.error.NoOpErrorHandler) ErrorHandler(com.optimizely.ab.error.ErrorHandler) EventType(com.optimizely.ab.config.EventType) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) UserProfileService(com.optimizely.ab.bucketing.UserProfileService) Variation(com.optimizely.ab.config.Variation) Bucketer(com.optimizely.ab.bucketing.Bucketer) DecisionService(com.optimizely.ab.bucketing.DecisionService) Test(org.junit.Test)

Example 19 with EventType

use of com.optimizely.ab.config.EventType 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);
}
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 20 with EventType

use of com.optimizely.ab.config.EventType in project java-sdk by optimizely.

the class EventBuilderTest 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)).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<String, Object> eventTagMap = new HashMap<String, Object>();
    eventTagMap.put(ReservedEventKey.REVENUE.toString(), revenue);
    eventTagMap.put(ReservedEventKey.VALUE.toString(), value);
    Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(validProjectConfig, decisionService, eventType.getKey(), userId, attributeMap);
    LogEvent conversionEvent = builder.createConversionEvent(validProjectConfig, experimentVariationMap, 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 : NoOpErrorHandler(com.optimizely.ab.error.NoOpErrorHandler) ErrorHandler(com.optimizely.ab.error.ErrorHandler) Attribute(com.optimizely.ab.config.Attribute) EventType(com.optimizely.ab.config.EventType) HashMap(java.util.HashMap) 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)

Aggregations

EventType (com.optimizely.ab.config.EventType)29 LogEvent (com.optimizely.ab.event.LogEvent)25 Experiment (com.optimizely.ab.config.Experiment)24 Test (org.junit.Test)24 Variation (com.optimizely.ab.config.Variation)20 HashMap (java.util.HashMap)15 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)12 Matchers.anyString (org.mockito.Matchers.anyString)12 Bucketer (com.optimizely.ab.bucketing.Bucketer)9 DecisionService (com.optimizely.ab.bucketing.DecisionService)8 Map (java.util.Map)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 Attribute (com.optimizely.ab.config.Attribute)7 NoOpErrorHandler (com.optimizely.ab.error.NoOpErrorHandler)7 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)7 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)7 UserProfileService (com.optimizely.ab.bucketing.UserProfileService)6 ProjectConfig (com.optimizely.ab.config.ProjectConfig)6 ErrorHandler (com.optimizely.ab.error.ErrorHandler)5 TrackNotificationListener (com.optimizely.ab.notification.TrackNotificationListener)4