Search in sources :

Example 26 with Experiment

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

the class Bucketer method bucket.

/**
 * Assign a {@link Variation} of an {@link Experiment} to a user based on hashed value from murmurhash3.
 * @param experiment The Experiment in which the user is to be bucketed.
 * @param bucketingId string A customer-assigned value used to create the key for the murmur hash.
 * @return Variation the user is bucketed into or null.
 */
@Nullable
public Variation bucket(@Nonnull Experiment experiment, @Nonnull String bucketingId) {
    // ---------- Bucket User ----------
    String groupId = experiment.getGroupId();
    // check whether the experiment belongs to a group
    if (!groupId.isEmpty()) {
        Group experimentGroup = projectConfig.getGroupIdMapping().get(groupId);
        // bucket to an experiment only if group entities are to be mutually exclusive
        if (experimentGroup.getPolicy().equals(Group.RANDOM_POLICY)) {
            Experiment bucketedExperiment = bucketToExperiment(experimentGroup, bucketingId);
            if (bucketedExperiment == null) {
                logger.info("User with bucketingId \"{}\" is not in any experiment of group {}.", bucketingId, experimentGroup.getId());
                return null;
            } else {
            }
            // don't perform further bucketing within the experiment
            if (!bucketedExperiment.getId().equals(experiment.getId())) {
                logger.info("User with bucketingId \"{}\" is not in experiment \"{}\" of group {}.", bucketingId, experiment.getKey(), experimentGroup.getId());
                return null;
            }
            logger.info("User with bucketingId \"{}\" is in experiment \"{}\" of group {}.", bucketingId, experiment.getKey(), experimentGroup.getId());
        }
    }
    return bucketToVariation(experiment, bucketingId);
}
Also used : Group(com.optimizely.ab.config.Group) Experiment(com.optimizely.ab.config.Experiment) Nullable(javax.annotation.Nullable)

Example 27 with Experiment

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

the class OptimizelyTest method activateLaunchedExperimentDoesNotDispatchEvent.

/**
 * Verify that {@link Optimizely#activate(String, String)} doesn't dispatch an event for an experiment with a
 * "Launched" status.
 */
@Test
public void activateLaunchedExperimentDoesNotDispatchEvent() throws Exception {
    Experiment launchedExperiment;
    if (datafileVersion == 4) {
        launchedExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_LAUNCHED_EXPERIMENT_KEY);
    } else {
        launchedExperiment = noAudienceProjectConfig.getExperiments().get(2);
    }
    Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withBucketing(mockBucketer).withConfig(noAudienceProjectConfig).build();
    Variation expectedVariation = launchedExperiment.getVariations().get(0);
    when(mockBucketer.bucket(launchedExperiment, testUserId)).thenReturn(launchedExperiment.getVariations().get(0));
    logbackVerifier.expectMessage(Level.INFO, "Experiment has \"Launched\" status so not dispatching event during activation.");
    Variation variation = optimizely.activate(launchedExperiment.getKey(), testUserId);
    assertNotNull(variation);
    assertThat(variation.getKey(), is(expectedVariation.getKey()));
    // verify that we did NOT dispatch an event
    verify(mockEventHandler, never()).dispatchEvent(any(LogEvent.class));
}
Also used : LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 28 with Experiment

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

the class OptimizelyTest method activateForGroupExperimentWithNonMatchingAttributes.

/**
 * Verify that {@link Optimizely#activate(String, String, Map)} doesn't return a variation when given
 * non-matching user attributes.
 */
@Test
public void activateForGroupExperimentWithNonMatchingAttributes() throws Exception {
    Experiment experiment = validProjectConfig.getGroups().get(0).getExperiments().get(0);
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).build();
    String experimentKey = experiment.getKey();
    logbackVerifier.expectMessage(Level.INFO, "User \"user\" does not meet conditions to be in experiment \"" + experimentKey + "\".");
    logbackVerifier.expectMessage(Level.INFO, "Not activating user \"user\" for experiment \"" + experimentKey + "\".");
    assertNull(optimizely.activate(experiment.getKey(), "user", Collections.singletonMap("browser_type", "firefox")));
}
Also used : Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 29 with Experiment

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

the class OptimizelyTest method trackEventWithListenerNullAttributes.

/**
 * Track with listener and verify that {@link Optimizely#track(String, String)} ignores null attributes.
 */
@Test
@SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "testing nullness contract violation")
public void trackEventWithListenerNullAttributes() throws Exception {
    final 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<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()), eq(Collections.<String, String>emptyMap()), eq(Collections.<String, Object>emptyMap()))).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 \"\"");
    TrackNotificationListener trackNotification = new TrackNotificationListener() {

        @Override
        public void onTrack(@Nonnull String eventKey, @Nonnull String userId, @Nonnull Map<String, String> attributes, @Nonnull Map<String, ?> eventTags, @Nonnull LogEvent event) {
            assertEquals(eventType.getKey(), eventKey);
            assertEquals(genericUserId, userId);
            assertTrue(attributes.isEmpty());
            assertTrue(eventTags.isEmpty());
        }
    };
    int notificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Track, trackNotification);
    // call track
    Map<String, String> attributes = null;
    optimizely.track(eventType.getKey(), genericUserId, attributes);
    optimizely.notificationCenter.removeNotificationListener(notificationId);
    logbackVerifier.expectMessage(Level.WARN, "Attributes is null when non-null was expected. Defaulting to an empty attributes map.");
    // setup the attribute map captor (so we can verify its content)
    ArgumentCaptor<Map> attributeCaptor = 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()), attributeCaptor.capture(), eq(Collections.<String, Object>emptyMap()));
    Map<String, String> actualValue = attributeCaptor.getValue();
    assertThat(actualValue, is(Collections.<String, String>emptyMap()));
    verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
Also used : EventType(com.optimizely.ab.config.EventType) HashMap(java.util.HashMap) LogEvent(com.optimizely.ab.event.LogEvent) Nonnull(javax.annotation.Nonnull) 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) 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) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 30 with Experiment

use of com.optimizely.ab.config.Experiment 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)

Aggregations

Experiment (com.optimizely.ab.config.Experiment)137 Test (org.junit.Test)113 Variation (com.optimizely.ab.config.Variation)91 Matchers.anyString (org.mockito.Matchers.anyString)58 LogEvent (com.optimizely.ab.event.LogEvent)47 HashMap (java.util.HashMap)42 EventType (com.optimizely.ab.config.EventType)28 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)27 ProjectConfig (com.optimizely.ab.config.ProjectConfig)22 Map (java.util.Map)18 Attribute (com.optimizely.ab.config.Attribute)17 ImmutableMap (com.google.common.collect.ImmutableMap)15 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)15 ArrayList (java.util.ArrayList)14 Rollout (com.optimizely.ab.config.Rollout)13 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)12 TrafficAllocation (com.optimizely.ab.config.TrafficAllocation)11 ExperimentUtils.isUserInExperiment (com.optimizely.ab.internal.ExperimentUtils.isUserInExperiment)10 Bucketer (com.optimizely.ab.bucketing.Bucketer)9 ExhaustiveTest (com.optimizely.ab.categories.ExhaustiveTest)9