Search in sources :

Example 81 with Variation

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

the class BucketerTest method testBucketWithBucketingId.

@Test
public void testBucketWithBucketingId() {
    final AtomicInteger bucketValue = new AtomicInteger();
    Bucketer algorithm = mockBucketAlgorithm(bucketValue);
    bucketValue.set(0);
    String bucketingId = "blah";
    String userId = "blahUser";
    ProjectConfig projectConfig = validProjectConfigV2();
    List<Experiment> groupExperiments = projectConfig.getGroups().get(1).getExperiments();
    Experiment groupExperiment = groupExperiments.get(0);
    Variation expectedVariation = groupExperiment.getVariations().get(0);
    logbackVerifier.expectMessage(Level.INFO, "User with bucketingId \"" + bucketingId + "\" is in variation \"e1_vtag1\" of experiment \"overlapping_etag1\".");
    assertThat(algorithm.bucket(groupExperiment, bucketingId), is(expectedVariation));
}
Also used : ProjectConfig(com.optimizely.ab.config.ProjectConfig) 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 82 with Variation

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

the class OptimizelyTest method getVariationNoAudiences.

/**
 * Verify that {@link Optimizely#getVariation(String, String)} returns a variation when given an experiment
 * with no audiences and no user attributes.
 */
@Test
public void getVariationNoAudiences() throws Exception {
    Experiment experiment = noAudienceProjectConfig.getExperiments().get(0);
    Variation bucketedVariation = experiment.getVariations().get(0);
    when(mockBucketer.bucket(experiment, testUserId)).thenReturn(bucketedVariation);
    Optimizely optimizely = Optimizely.builder(noAudienceDatafile, mockEventHandler).withConfig(noAudienceProjectConfig).withBucketing(mockBucketer).withErrorHandler(mockErrorHandler).build();
    Variation actualVariation = optimizely.getVariation(experiment.getKey(), testUserId);
    verify(mockBucketer).bucket(experiment, testUserId);
    assertThat(actualVariation, is(bucketedVariation));
}
Also used : Experiment(com.optimizely.ab.config.Experiment) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 83 with Variation

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

the class OptimizelyTest method activateUserInAudience.

/**
 * Verify that a user who falls in an experiment's audience is assigned a variation.
 */
@Test
public void activateUserInAudience() throws Exception {
    Experiment experimentToCheck = validProjectConfig.getExperiments().get(0);
    Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
    Map<String, String> testUserAttributes = new HashMap<String, String>();
    testUserAttributes.put("browser_type", "chrome");
    Variation actualVariation = optimizely.activate(experimentToCheck.getKey(), testUserId, testUserAttributes);
    assertNotNull(actualVariation);
}
Also used : HashMap(java.util.HashMap) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 84 with Variation

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

the class OptimizelyTest method trackEventWithNullEventTags.

/**
 * Verify that {@link Optimizely#track(String, String, Map, Map)} called with null event tags will default to
 * an empty map when calling {@link EventBuilder#createConversionEvent(ProjectConfig, Map, String, String, String, Map, Map)}
 */
@Test
@SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "testing nullness contract violation")
public void trackEventWithNullEventTags() throws Exception {
    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, String>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 \"\"");
    // call track
    optimizely.track(eventType.getKey(), genericUserId, Collections.<String, String>emptyMap(), null);
    // 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()), eq(Collections.<String, String>emptyMap()), eq(Collections.<String, String>emptyMap()));
    verify(mockEventHandler).dispatchEvent(logEventToDispatch);
}
Also used : EventBuilder(com.optimizely.ab.event.internal.EventBuilder) 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) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 85 with Variation

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

the class JsonSimpleConfigParser method parseExperiments.

private List<Experiment> parseExperiments(JSONArray experimentJson, String groupId) {
    List<Experiment> experiments = new ArrayList<Experiment>(experimentJson.size());
    for (Object obj : experimentJson) {
        JSONObject experimentObject = (JSONObject) obj;
        String id = (String) experimentObject.get("id");
        String key = (String) experimentObject.get("key");
        Object statusJson = experimentObject.get("status");
        String status = statusJson == null ? ExperimentStatus.NOT_STARTED.toString() : (String) experimentObject.get("status");
        Object layerIdObject = experimentObject.get("layerId");
        String layerId = layerIdObject == null ? null : (String) layerIdObject;
        JSONArray audienceIdsJson = (JSONArray) experimentObject.get("audienceIds");
        List<String> audienceIds = new ArrayList<String>(audienceIdsJson.size());
        for (Object audienceIdObj : audienceIdsJson) {
            audienceIds.add((String) audienceIdObj);
        }
        // parse the child objects
        List<Variation> variations = parseVariations((JSONArray) experimentObject.get("variations"));
        Map<String, String> userIdToVariationKeyMap = parseForcedVariations((JSONObject) experimentObject.get("forcedVariations"));
        List<TrafficAllocation> trafficAllocations = parseTrafficAllocation((JSONArray) experimentObject.get("trafficAllocation"));
        experiments.add(new Experiment(id, key, status, layerId, audienceIds, variations, userIdToVariationKeyMap, trafficAllocations, groupId));
    }
    return experiments;
}
Also used : Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) JSONObject(org.json.simple.JSONObject) TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) JSONObject(org.json.simple.JSONObject) Variation(com.optimizely.ab.config.Variation)

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