Search in sources :

Example 1 with Bucketer

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

the class OptimizelyTest method trackDoesNotSendEventWhenExperimentsAreLaunchedOnly.

/**
 * Verify that {@link Optimizely#track(String, String, Map)}
 * doesn't dispatch events when the event links only to launched experiments
 */
@Test
public void trackDoesNotSendEventWhenExperimentsAreLaunchedOnly() throws Exception {
    EventType eventType;
    if (datafileVersion >= 4) {
        eventType = validProjectConfig.getEventNameMapping().get(EVENT_LAUNCHED_EXPERIMENT_ONLY_KEY);
    } else {
        eventType = noAudienceProjectConfig.getEventNameMapping().get("launched_exp_event");
    }
    Bucketer mockBucketAlgorithm = mock(Bucketer.class);
    for (Experiment experiment : noAudienceProjectConfig.getExperiments()) {
        Variation variation = experiment.getVariations().get(0);
        when(mockBucketAlgorithm.bucket(eq(experiment), eq(genericUserId))).thenReturn(variation);
    }
    Optimizely client = Optimizely.builder(noAudienceDatafile, mockEventHandler).withConfig(noAudienceProjectConfig).withBucketing(mockBucketAlgorithm).build();
    List<Experiment> eventExperiments = noAudienceProjectConfig.getExperimentsForEventKey(eventType.getKey());
    for (Experiment experiment : eventExperiments) {
        logbackVerifier.expectMessage(Level.INFO, "Not tracking event \"" + eventType.getKey() + "\" for experiment \"" + experiment.getKey() + "\" because experiment has status \"Launched\".");
    }
    logbackVerifier.expectMessage(Level.INFO, "There are no valid experiments for event \"" + eventType.getKey() + "\" to track.");
    logbackVerifier.expectMessage(Level.INFO, "Not tracking event \"" + eventType.getKey() + "\" for user \"" + genericUserId + "\".");
    // only 1 experiment uses the event and it has a "Launched" status so experimentsForEvent map is empty
    // and the returned event will be null
    // this means we will never call the dispatcher
    client.track(eventType.getKey(), genericUserId, Collections.<String, String>emptyMap());
    // bucket should never be called since experiments are launched so we never get variation for them
    verify(mockBucketAlgorithm, never()).bucket(any(Experiment.class), anyString());
    verify(mockEventHandler, never()).dispatchEvent(any(LogEvent.class));
}
Also used : EventType(com.optimizely.ab.config.EventType) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Variation(com.optimizely.ab.config.Variation) Bucketer(com.optimizely.ab.bucketing.Bucketer) Test(org.junit.Test)

Example 2 with Bucketer

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

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

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

the class EventFactoryTest method createConversionEventAndroidTVClientEngineClientVersion.

/**
 * Verify that supplying {@link ClientEngineInfo} 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";
    ClientEngineInfo.setClientEngine(EventBatch.ClientEngine.ANDROID_TV_SDK);
    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, validProjectConfig)).thenReturn(DecisionResponse.responseNoReasons(experiment.getVariations().get(0)));
    }
    Map<String, String> attributeMap = Collections.singletonMap(attribute.getKey(), "value");
    LogEvent conversionEvent = createConversionEvent(projectConfig, userId, eventType.getId(), eventType.getKey(), attributeMap, Collections.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 : 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 5 with Bucketer

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

Bucketer (com.optimizely.ab.bucketing.Bucketer)12 LogEvent (com.optimizely.ab.event.LogEvent)12 Test (org.junit.Test)12 EventType (com.optimizely.ab.config.EventType)9 Experiment (com.optimizely.ab.config.Experiment)9 Variation (com.optimizely.ab.config.Variation)9 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)9 DecisionService (com.optimizely.ab.bucketing.DecisionService)6 UserProfileService (com.optimizely.ab.bucketing.UserProfileService)6 NoOpErrorHandler (com.optimizely.ab.error.NoOpErrorHandler)6 Attribute (com.optimizely.ab.config.Attribute)5 ErrorHandler (com.optimizely.ab.error.ErrorHandler)5 HashMap (java.util.HashMap)5 ControlAttribute (com.optimizely.ab.internal.ControlAttribute)3 Decision (com.optimizely.ab.event.internal.payload.Decision)2 ArrayList (java.util.ArrayList)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ProjectConfig (com.optimizely.ab.config.ProjectConfig)1 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)1 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)1