Search in sources :

Example 21 with EventType

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

the class EventBuilderTest method createConversionEventReturnsNullWhenExperimentVariationMapIsEmpty.

/**
 * Verify that supplying an empty Experiment Variation map to
 * {@link EventBuilder#createConversionEvent(ProjectConfig, Map, String, String, String, Map, Map)}
 * returns a null {@link LogEvent}.
 */
@Test
public void createConversionEventReturnsNullWhenExperimentVariationMapIsEmpty() {
    EventType eventType = validProjectConfig.getEventTypes().get(0);
    EventBuilder builder = new EventBuilder();
    LogEvent conversionEvent = builder.createConversionEvent(validProjectConfig, Collections.<Experiment, Variation>emptyMap(), userId, eventType.getId(), eventType.getKey(), Collections.<String, String>emptyMap(), Collections.<String, String>emptyMap());
    assertNull(conversionEvent);
}
Also used : EventType(com.optimizely.ab.config.EventType) LogEvent(com.optimizely.ab.event.LogEvent) Test(org.junit.Test)

Example 22 with EventType

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

the class EventBuilderTest method createConversionEventAndroidTVClientEngineClientVersion.

/**
 * Verify that supplying {@link EventBuilder} with a Android TV client engine and client version results in
 * conversion events being sent with the overriden values.
 */
@Test
public void createConversionEventAndroidTVClientEngineClientVersion() throws Exception {
    String clientVersion = "0.0.0";
    EventBuilder builder = new EventBuilder(EventBatch.ClientEngine.ANDROID_TV_SDK, clientVersion);
    ProjectConfig projectConfig = validProjectConfigV2();
    Attribute attribute = projectConfig.getAttributes().get(0);
    EventType eventType = projectConfig.getEventTypes().get(0);
    String userId = "userId";
    Bucketer mockBucketAlgorithm = mock(Bucketer.class);
    for (Experiment experiment : projectConfig.getExperiments()) {
        when(mockBucketAlgorithm.bucket(experiment, userId)).thenReturn(experiment.getVariations().get(0));
    }
    Map<String, String> attributeMap = Collections.singletonMap(attribute.getKey(), "value");
    List<Experiment> experimentList = projectConfig.getExperimentsForEventKey(eventType.getKey());
    Map<Experiment, Variation> experimentVariationMap = new HashMap<Experiment, Variation>(experimentList.size());
    for (Experiment experiment : experimentList) {
        experimentVariationMap.put(experiment, experiment.getVariations().get(0));
    }
    LogEvent conversionEvent = builder.createConversionEvent(projectConfig, 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_TV_SDK.getClientEngineValue()));
    assertThat(conversion.getClientVersion(), is(clientVersion));
}
Also used : 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) ProjectConfig(com.optimizely.ab.config.ProjectConfig) 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 23 with EventType

use of com.optimizely.ab.config.EventType 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 24 with EventType

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

the class OptimizelyTest method removeNotificationListenerNotificationCenter.

/**
 * Verify that {@link com.optimizely.ab.notification.NotificationCenter} properly
 * calls and the listener is removed and no longer notified when an experiment is activated.
 */
@Test
public void removeNotificationListenerNotificationCenter() throws Exception {
    Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
    Variation bucketedVariation = activatedExperiment.getVariations().get(0);
    EventBuilder mockEventBuilder = mock(EventBuilder.class);
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withEventBuilder(mockEventBuilder).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
    Map<String, String> attributes = new HashMap<String, String>();
    attributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
    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(mockBucketer.bucket(activatedExperiment, genericUserId)).thenReturn(bucketedVariation);
    when(mockEventBuilder.createImpressionEvent(validProjectConfig, activatedExperiment, bucketedVariation, genericUserId, attributes)).thenReturn(logEventToDispatch);
    // Add and remove listener
    ActivateNotificationListener activateNotification = mock(ActivateNotificationListener.class);
    int notificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Activate, activateNotification);
    assertTrue(optimizely.notificationCenter.removeNotificationListener(notificationId));
    TrackNotificationListener trackNotification = mock(TrackNotificationListener.class);
    notificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Track, trackNotification);
    assertTrue(optimizely.notificationCenter.removeNotificationListener(notificationId));
    // Check if listener is notified after an experiment is activated
    Variation actualVariation = optimizely.activate(activatedExperiment, genericUserId, attributes);
    verify(activateNotification, never()).onActivate(activatedExperiment, genericUserId, attributes, actualVariation, logEventToDispatch);
    // Check if listener is notified after a live variable is accessed
    boolean activateExperiment = true;
    verify(activateNotification, never()).onActivate(activatedExperiment, genericUserId, attributes, actualVariation, logEventToDispatch);
    // Check if listener is notified after an event is tracked
    EventType eventType = validProjectConfig.getEventTypes().get(0);
    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);
    optimizely.track(eventKey, genericUserId, attributes);
    verify(trackNotification, never()).onTrack(eventKey, genericUserId, attributes, Collections.EMPTY_MAP, logEventToDispatch);
}
Also used : HashMap(java.util.HashMap) LogEvent(com.optimizely.ab.event.LogEvent) EventType(com.optimizely.ab.config.EventType) 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 25 with EventType

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

the class OptimizelyTest method trackDispatchesWhenEventHasLaunchedAndRunningExperiments.

/**
 * Verify that {@link Optimizely#track(String, String, Map)}
 * dispatches log events when the tracked event links to both launched and running experiments.
 */
@Test
public void trackDispatchesWhenEventHasLaunchedAndRunningExperiments() throws Exception {
    EventBuilder mockEventBuilder = mock(EventBuilder.class);
    EventType eventType;
    if (datafileVersion >= 4) {
        eventType = validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY);
    } else {
        eventType = noAudienceProjectConfig.getEventNameMapping().get("event_with_launched_and_running_experiments");
    }
    Bucketer mockBucketAlgorithm = mock(Bucketer.class);
    for (Experiment experiment : validProjectConfig.getExperiments()) {
        when(mockBucketAlgorithm.bucket(experiment, genericUserId)).thenReturn(experiment.getVariations().get(0));
    }
    Optimizely client = Optimizely.builder(validDatafile, mockEventHandler).withConfig(noAudienceProjectConfig).withBucketing(mockBucketAlgorithm).withEventBuilder(mockEventBuilder).build();
    Map<String, String> testParams = new HashMap<String, String>();
    testParams.put("test", "params");
    Map<Experiment, Variation> experimentVariationMap = createExperimentVariationMap(noAudienceProjectConfig, client.decisionService, eventType.getKey(), genericUserId, Collections.<String, String>emptyMap());
    // Create an Argument Captor to ensure we are creating a correct experiment variation map
    ArgumentCaptor<Map> experimentVariationMapCaptor = ArgumentCaptor.forClass(Map.class);
    LogEvent conversionEvent = new LogEvent(RequestMethod.GET, "test_url", testParams, "");
    when(mockEventBuilder.createConversionEvent(eq(noAudienceProjectConfig), experimentVariationMapCaptor.capture(), eq(genericUserId), eq(eventType.getId()), eq(eventType.getKey()), eq(Collections.<String, String>emptyMap()), eq(Collections.<String, Object>emptyMap()))).thenReturn(conversionEvent);
    List<Experiment> eventExperiments = noAudienceProjectConfig.getExperimentsForEventKey(eventType.getKey());
    for (Experiment experiment : eventExperiments) {
        if (experiment.isLaunched()) {
            logbackVerifier.expectMessage(Level.INFO, "Not tracking event \"" + eventType.getKey() + "\" for experiment \"" + experiment.getKey() + "\" because experiment has status \"Launched\".");
        }
    }
    // The event has 1 launched experiment and 1 running experiment.
    // It should send a track event with the running experiment
    client.track(eventType.getKey(), genericUserId, Collections.<String, String>emptyMap());
    verify(client.eventHandler).dispatchEvent(eq(conversionEvent));
    // Check the argument captor got the correct arguments
    Map<Experiment, Variation> actualExperimentVariationMap = experimentVariationMapCaptor.getValue();
    assertEquals(experimentVariationMap, actualExperimentVariationMap);
}
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) 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