Search in sources :

Example 1 with DecisionService

use of com.optimizely.ab.bucketing.DecisionService in project java-sdk by optimizely.

the class OptimizelyTest method trackEventEndToEndForced.

// ======== track tests ========//
/**
 * Verify that the {@link Optimizely#track(String, String)} call correctly builds a V2 event and passes it
 * through {@link EventHandler#dispatchEvent(LogEvent)}.
 */
@Test
public void trackEventEndToEndForced() throws Exception {
    EventType eventType;
    String datafile;
    ProjectConfig config;
    if (datafileVersion >= 4) {
        config = spy(validProjectConfig);
        eventType = validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY);
        datafile = validDatafile;
    } else {
        config = spy(noAudienceProjectConfig);
        eventType = noAudienceProjectConfig.getEventTypes().get(0);
        datafile = noAudienceDatafile;
    }
    List<Experiment> allExperiments = new ArrayList<Experiment>();
    allExperiments.add(config.getExperiments().get(0));
    EventBuilder eventBuilder = new EventBuilder();
    DecisionService spyDecisionService = spy(new DecisionService(mockBucketer, mockErrorHandler, config, null));
    Optimizely optimizely = Optimizely.builder(datafile, mockEventHandler).withDecisionService(spyDecisionService).withEventBuilder(eventBuilder).withConfig(noAudienceProjectConfig).withErrorHandler(mockErrorHandler).build();
    // call the bucket function.
    for (Experiment experiment : allExperiments) {
        when(mockBucketer.bucket(experiment, testUserId)).thenReturn(null);
    }
    // call get forced.
    for (Experiment experiment : allExperiments) {
        optimizely.projectConfig.setForcedVariation(experiment.getKey(), testUserId, experiment.getVariations().get(0).getKey());
    }
    // call track
    optimizely.track(eventType.getKey(), testUserId);
    // verify that the bucketing algorithm was called only on experiments corresponding to the specified goal.
    List<Experiment> experimentsForEvent = config.getExperimentsForEventKey(eventType.getKey());
    for (Experiment experiment : allExperiments) {
        if (experiment.isRunning() && experimentsForEvent.contains(experiment)) {
            verify(spyDecisionService).getVariation(experiment, testUserId, Collections.<String, String>emptyMap());
            verify(config).getForcedVariation(experiment.getKey(), testUserId);
        } else {
            verify(spyDecisionService, never()).getVariation(experiment, testUserId, Collections.<String, String>emptyMap());
        }
    }
    // verify that dispatchEvent was called
    verify(mockEventHandler).dispatchEvent(any(LogEvent.class));
    for (Experiment experiment : allExperiments) {
        assertEquals(optimizely.projectConfig.getForcedVariation(experiment.getKey(), testUserId), experiment.getVariations().get(0));
        optimizely.projectConfig.setForcedVariation(experiment.getKey(), testUserId, null);
        assertNull(optimizely.projectConfig.getForcedVariation(experiment.getKey(), testUserId));
    }
}
Also used : ProjectConfig(com.optimizely.ab.config.ProjectConfig) EventBuilder(com.optimizely.ab.event.internal.EventBuilder) EventType(com.optimizely.ab.config.EventType) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) DecisionService(com.optimizely.ab.bucketing.DecisionService) Test(org.junit.Test)

Example 2 with DecisionService

use of com.optimizely.ab.bucketing.DecisionService 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 3 with DecisionService

use of com.optimizely.ab.bucketing.DecisionService in project java-sdk by optimizely.

the class EventBuilderTest method createConversionEventForcedVariationBucketingPrecedesAudienceEval.

/**
 * Verify that precedence is given to forced variation bucketing over audience evaluation when constructing a
 * conversion event.
 */
@Test
public void createConversionEventForcedVariationBucketingPrecedesAudienceEval() {
    EventType eventType;
    String whitelistedUserId;
    if (datafileVersion == 4) {
        eventType = validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY);
        whitelistedUserId = MULTIVARIATE_EXPERIMENT_FORCED_VARIATION_USER_ID_GRED;
    } else {
        eventType = validProjectConfig.getEventTypes().get(0);
        whitelistedUserId = "testUser1";
    }
    DecisionService decisionService = new DecisionService(new Bucketer(validProjectConfig), new NoOpErrorHandler(), validProjectConfig, mock(UserProfileService.class));
    // attributes are empty so user won't be in the audience for experiment using the event, but bucketing
    // will still take place
    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());
    assertNotNull(conversionEvent);
    EventBatch conversion = gson.fromJson(conversionEvent.getBody(), EventBatch.class);
    if (datafileVersion == 4) {
        // 2 experiments use the event
        // basic experiment has no audience
        // user is whitelisted in to one audience
        assertEquals(2, conversion.getVisitors().get(0).getSnapshots().get(0).getDecisions().size());
    } else {
        assertEquals(1, conversion.getVisitors().get(0).getSnapshots().get(0).getDecisions().size());
    }
}
Also used : EventType(com.optimizely.ab.config.EventType) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) NoOpErrorHandler(com.optimizely.ab.error.NoOpErrorHandler) UserProfileService(com.optimizely.ab.bucketing.UserProfileService) Variation(com.optimizely.ab.config.Variation) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Bucketer(com.optimizely.ab.bucketing.Bucketer) DecisionService(com.optimizely.ab.bucketing.DecisionService) Test(org.junit.Test)

Example 4 with DecisionService

use of com.optimizely.ab.bucketing.DecisionService in project java-sdk by optimizely.

the class OptimizelyTest method trackEventEndToEnd.

/**
 * Verify that the {@link Optimizely#track(String, String)} call correctly builds a V2 event and passes it
 * through {@link EventHandler#dispatchEvent(LogEvent)}.
 */
@Test
public void trackEventEndToEnd() throws Exception {
    EventType eventType;
    String datafile;
    ProjectConfig config;
    if (datafileVersion >= 4) {
        config = spy(validProjectConfig);
        eventType = validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY);
        datafile = validDatafile;
    } else {
        config = spy(noAudienceProjectConfig);
        eventType = noAudienceProjectConfig.getEventTypes().get(0);
        datafile = noAudienceDatafile;
    }
    List<Experiment> allExperiments = config.getExperiments();
    EventBuilder eventBuilder = new EventBuilder();
    DecisionService spyDecisionService = spy(new DecisionService(mockBucketer, mockErrorHandler, config, null));
    Optimizely optimizely = Optimizely.builder(datafile, mockEventHandler).withDecisionService(spyDecisionService).withEventBuilder(eventBuilder).withConfig(noAudienceProjectConfig).withErrorHandler(mockErrorHandler).build();
    // call the bucket function.
    for (Experiment experiment : allExperiments) {
        when(mockBucketer.bucket(experiment, testUserId)).thenReturn(experiment.getVariations().get(0));
    }
    // call track
    optimizely.track(eventType.getKey(), testUserId);
    // verify that the bucketing algorithm was called only on experiments corresponding to the specified goal.
    List<Experiment> experimentsForEvent = config.getExperimentsForEventKey(eventType.getKey());
    for (Experiment experiment : allExperiments) {
        if (experiment.isRunning() && experimentsForEvent.contains(experiment)) {
            verify(spyDecisionService).getVariation(experiment, testUserId, Collections.<String, String>emptyMap());
            verify(config).getForcedVariation(experiment.getKey(), testUserId);
        } else {
            verify(spyDecisionService, never()).getVariation(experiment, testUserId, Collections.<String, String>emptyMap());
        }
    }
    // verify that dispatchEvent was called
    verify(mockEventHandler).dispatchEvent(any(LogEvent.class));
}
Also used : ProjectConfig(com.optimizely.ab.config.ProjectConfig) EventBuilder(com.optimizely.ab.event.internal.EventBuilder) EventType(com.optimizely.ab.config.EventType) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) DecisionService(com.optimizely.ab.bucketing.DecisionService) Test(org.junit.Test)

Example 5 with DecisionService

use of com.optimizely.ab.bucketing.DecisionService 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)

Aggregations

DecisionService (com.optimizely.ab.bucketing.DecisionService)8 EventType (com.optimizely.ab.config.EventType)8 Experiment (com.optimizely.ab.config.Experiment)8 LogEvent (com.optimizely.ab.event.LogEvent)8 Test (org.junit.Test)8 Bucketer (com.optimizely.ab.bucketing.Bucketer)6 UserProfileService (com.optimizely.ab.bucketing.UserProfileService)6 Variation (com.optimizely.ab.config.Variation)6 NoOpErrorHandler (com.optimizely.ab.error.NoOpErrorHandler)6 ErrorHandler (com.optimizely.ab.error.ErrorHandler)5 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)5 Attribute (com.optimizely.ab.config.Attribute)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ProjectConfig (com.optimizely.ab.config.ProjectConfig)2 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)2 Decision (com.optimizely.ab.event.internal.payload.Decision)2 Matchers.anyString (org.mockito.Matchers.anyString)2