Search in sources :

Example 86 with Variation

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

the class EventBuilder method createConversionEvent.

public LogEvent createConversionEvent(@Nonnull ProjectConfig projectConfig, @Nonnull Map<Experiment, Variation> experimentVariationMap, @Nonnull String userId, @Nonnull String eventId, @Nonnull String eventName, @Nonnull Map<String, String> attributes, @Nonnull Map<String, ?> eventTags) {
    if (experimentVariationMap.isEmpty()) {
        return null;
    }
    ArrayList<Decision> decisions = new ArrayList<Decision>();
    for (Map.Entry<Experiment, Variation> entry : experimentVariationMap.entrySet()) {
        Decision decision = new Decision(entry.getKey().getLayerId(), entry.getKey().getId(), entry.getValue().getId(), false);
        decisions.add(decision);
    }
    EventType eventType = projectConfig.getEventNameMapping().get(eventName);
    Event conversionEvent = new Event(System.currentTimeMillis(), UUID.randomUUID().toString(), eventType.getId(), eventType.getKey(), null, EventTagUtils.getRevenueValue(eventTags), eventTags, eventType.getKey(), EventTagUtils.getNumericValue(eventTags));
    Snapshot snapshot = new Snapshot(decisions, Arrays.asList(conversionEvent));
    Visitor visitor = new Visitor(userId, null, buildAttributeList(projectConfig, attributes), Arrays.asList(snapshot));
    List<Visitor> visitors = Arrays.asList(visitor);
    EventBatch eventBatch = new EventBatch(clientEngine.getClientEngineValue(), clientVersion, projectConfig.getAccountId(), visitors, projectConfig.getAnonymizeIP(), projectConfig.getProjectId(), projectConfig.getRevision());
    String payload = this.serializer.serialize(eventBatch);
    return new LogEvent(LogEvent.RequestMethod.POST, EVENT_ENDPOINT, Collections.<String, String>emptyMap(), payload);
}
Also used : Visitor(com.optimizely.ab.event.internal.payload.Visitor) EventType(com.optimizely.ab.config.EventType) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) Decision(com.optimizely.ab.event.internal.payload.Decision) Snapshot(com.optimizely.ab.event.internal.payload.Snapshot) Event(com.optimizely.ab.event.internal.payload.Event) LogEvent(com.optimizely.ab.event.LogEvent) Variation(com.optimizely.ab.config.Variation) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Map(java.util.Map)

Example 87 with Variation

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

the class OptimizelyTest method getVariationWithExperimentKeyForced.

/**
 * Verify that the {@link Optimizely#getVariation(String, String, Map<String, String>)} call
 * uses forced variation to force the user into the second variation.  The mock bucket returns
 * the first variation. Then remove the forced variation and confirm that the forced variation is null.
 */
@Test
public void getVariationWithExperimentKeyForced() throws Exception {
    Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
    Variation forcedVariation = activatedExperiment.getVariations().get(1);
    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();
    optimizely.setForcedVariation(activatedExperiment.getKey(), testUserId, forcedVariation.getKey());
    Map<String, String> testUserAttributes = new HashMap<String, String>();
    if (datafileVersion >= 4) {
        testUserAttributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
    } else {
        testUserAttributes.put("browser_type", "chrome");
    }
    testUserAttributes.put(testBucketingIdKey, testBucketingId);
    Map<String, String> testParams = new HashMap<String, String>();
    testParams.put("test", "params");
    LogEvent logEventToDispatch = new LogEvent(RequestMethod.GET, "test_url", testParams, "");
    when(mockEventBuilder.createImpressionEvent(eq(validProjectConfig), eq(activatedExperiment), eq(forcedVariation), eq(testUserId), eq(testUserAttributes))).thenReturn(logEventToDispatch);
    when(mockBucketer.bucket(activatedExperiment, testBucketingId)).thenReturn(bucketedVariation);
    // activate the experiment
    Variation actualVariation = optimizely.getVariation(activatedExperiment.getKey(), testUserId, testUserAttributes);
    assertThat(actualVariation, is(forcedVariation));
    optimizely.setForcedVariation(activatedExperiment.getKey(), testUserId, null);
    assertEquals(optimizely.getForcedVariation(activatedExperiment.getKey(), testUserId), null);
    actualVariation = optimizely.getVariation(activatedExperiment.getKey(), testUserId, testUserAttributes);
    assertThat(actualVariation, is(bucketedVariation));
}
Also used : EventBuilder(com.optimizely.ab.event.internal.EventBuilder) HashMap(java.util.HashMap) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 88 with Variation

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

the class ActivateNotificationListener method notify.

/**
 * Base notify called with var args.  This method parses the parameters and calls the abstract method.
 * @param args - variable argument list based on the type of notification.
 */
@Override
public final void notify(Object... args) {
    assert (args[0] instanceof Experiment);
    Experiment experiment = (Experiment) args[0];
    assert (args[1] instanceof String);
    String userId = (String) args[1];
    assert (args[2] instanceof java.util.Map);
    Map<String, String> attributes = (Map<String, String>) args[2];
    assert (args[3] instanceof Variation);
    Variation variation = (Variation) args[3];
    assert (args[4] instanceof LogEvent);
    LogEvent logEvent = (LogEvent) args[4];
    onActivate(experiment, userId, attributes, variation, logEvent);
}
Also used : LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Map(java.util.Map) Variation(com.optimizely.ab.config.Variation) Map(java.util.Map)

Example 89 with Variation

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

the class BucketerTest method bucketToMultipleVariations.

/**
 * Given an experiment with 4 variations, verify that bucket values are correctly mapped to the associated range.
 */
@Test
public void bucketToMultipleVariations() throws Exception {
    List<String> audienceIds = Collections.emptyList();
    // create an experiment with 4 variations using ranges: [0 -> 999, 1000 -> 4999, 5000 -> 5999, 6000 -> 9999]
    List<Variation> variations = Arrays.asList(new Variation("1", "var1"), new Variation("2", "var2"), new Variation("3", "var3"), new Variation("4", "var4"));
    List<TrafficAllocation> trafficAllocations = Arrays.asList(new TrafficAllocation("1", 1000), new TrafficAllocation("2", 5000), new TrafficAllocation("3", 6000), new TrafficAllocation("4", 10000));
    Experiment experiment = new Experiment("1234", "exp_key", "Running", "1", audienceIds, variations, Collections.<String, String>emptyMap(), trafficAllocations, "");
    final AtomicInteger bucketValue = new AtomicInteger();
    Bucketer algorithm = mockBucketAlgorithm(bucketValue);
    // verify bucketing to the first variation
    bucketValue.set(0);
    assertThat(algorithm.bucket(experiment, "user1"), is(variations.get(0)));
    bucketValue.set(500);
    assertThat(algorithm.bucket(experiment, "user2"), is(variations.get(0)));
    bucketValue.set(999);
    assertThat(algorithm.bucket(experiment, "user3"), is(variations.get(0)));
    // verify the second variation
    bucketValue.set(1000);
    assertThat(algorithm.bucket(experiment, "user4"), is(variations.get(1)));
    bucketValue.set(4000);
    assertThat(algorithm.bucket(experiment, "user5"), is(variations.get(1)));
    bucketValue.set(4999);
    assertThat(algorithm.bucket(experiment, "user6"), is(variations.get(1)));
    // ...and the rest
    bucketValue.set(5100);
    assertThat(algorithm.bucket(experiment, "user7"), is(variations.get(2)));
    bucketValue.set(6500);
    assertThat(algorithm.bucket(experiment, "user8"), is(variations.get(3)));
}
Also used : TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Experiment(com.optimizely.ab.config.Experiment) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test) ExhaustiveTest(com.optimizely.ab.categories.ExhaustiveTest)

Example 90 with Variation

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

the class DecisionServiceTest method getVariationForcedPrecedesAudienceEval.

/**
 * Verify that {@link DecisionService#getVariation(Experiment, String, Map)}
 * gives precedence to forced variation bucketing over audience evaluation.
 */
@Test
public void getVariationForcedPrecedesAudienceEval() throws Exception {
    Bucketer bucketer = spy(new Bucketer(validProjectConfig));
    DecisionService decisionService = spy(new DecisionService(bucketer, mockErrorHandler, validProjectConfig, null));
    Experiment experiment = validProjectConfig.getExperiments().get(0);
    Variation expectedVariation = experiment.getVariations().get(1);
    // user excluded without audiences and whitelisting
    assertNull(decisionService.getVariation(experiment, genericUserId, Collections.<String, String>emptyMap()));
    logbackVerifier.expectMessage(Level.INFO, "User \"" + genericUserId + "\" does not meet conditions to be in experiment \"etag1\".");
    // set the runtimeForcedVariation
    validProjectConfig.setForcedVariation(experiment.getKey(), genericUserId, expectedVariation.getKey());
    // no attributes provided for a experiment that has an audience
    assertThat(decisionService.getVariation(experiment, genericUserId, Collections.<String, String>emptyMap()), is(expectedVariation));
    verify(decisionService, never()).getStoredVariation(eq(experiment), any(UserProfile.class));
    assertEquals(validProjectConfig.setForcedVariation(experiment.getKey(), genericUserId, null), true);
    assertNull(validProjectConfig.getForcedVariation(experiment.getKey(), genericUserId));
}
Also used : Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Aggregations

Variation (com.optimizely.ab.config.Variation)99 Experiment (com.optimizely.ab.config.Experiment)91 Test (org.junit.Test)81 Matchers.anyString (org.mockito.Matchers.anyString)50 LogEvent (com.optimizely.ab.event.LogEvent)44 HashMap (java.util.HashMap)42 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)25 EventType (com.optimizely.ab.config.EventType)22 Map (java.util.Map)18 ImmutableMap (com.google.common.collect.ImmutableMap)15 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)15 Attribute (com.optimizely.ab.config.Attribute)13 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)12 ArrayList (java.util.ArrayList)10 Bucketer (com.optimizely.ab.bucketing.Bucketer)9 ProjectConfig (com.optimizely.ab.config.ProjectConfig)9 TrafficAllocation (com.optimizely.ab.config.TrafficAllocation)8 Rollout (com.optimizely.ab.config.Rollout)7 NoOpErrorHandler (com.optimizely.ab.error.NoOpErrorHandler)7 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)7