Search in sources :

Example 11 with TrafficAllocation

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

the class JsonConfigParser method parseTrafficAllocation.

private List<TrafficAllocation> parseTrafficAllocation(JSONArray trafficAllocationJson) {
    List<TrafficAllocation> trafficAllocation = new ArrayList<TrafficAllocation>(trafficAllocationJson.length());
    for (Object obj : trafficAllocationJson) {
        JSONObject allocationObject = (JSONObject) obj;
        String entityId = allocationObject.getString("entityId");
        int endOfRange = allocationObject.getInt("endOfRange");
        trafficAllocation.add(new TrafficAllocation(entityId, endOfRange));
    }
    return trafficAllocation;
}
Also used : TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONObject(org.json.JSONObject)

Example 12 with TrafficAllocation

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

the class BucketerTest method bucketToControl.

/**
 * Verify that in certain cases, users aren't assigned any variation and null is returned.
 */
@Test
public void bucketToControl() throws Exception {
    String bucketingId = "blah";
    String userId = "user1";
    List<String> audienceIds = Collections.emptyList();
    List<Variation> variations = Collections.singletonList(new Variation("1", "var1"));
    List<TrafficAllocation> trafficAllocations = Collections.singletonList(new TrafficAllocation("1", 999));
    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);
    logbackVerifier.expectMessage(Level.DEBUG, "Assigned bucket 0 to user with bucketingId \"" + bucketingId + "\" when bucketing to a variation.");
    logbackVerifier.expectMessage(Level.INFO, "User with bucketingId \"" + bucketingId + "\" is in variation \"var1\" of experiment \"exp_key\".");
    // verify bucketing to the first variation
    bucketValue.set(0);
    assertThat(algorithm.bucket(experiment, bucketingId), is(variations.get(0)));
    logbackVerifier.expectMessage(Level.DEBUG, "Assigned bucket 1000 to user with bucketingId \"" + bucketingId + "\" when bucketing to a variation.");
    logbackVerifier.expectMessage(Level.INFO, "User with bucketingId \"" + bucketingId + "\" is not in any variation of experiment \"exp_key\".");
    // verify bucketing to no variation (null)
    bucketValue.set(1000);
    assertNull(algorithm.bucket(experiment, bucketingId));
}
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 13 with TrafficAllocation

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

Example 14 with TrafficAllocation

use of com.optimizely.ab.config.TrafficAllocation 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 15 with TrafficAllocation

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

the class DecisionServiceTest method getWhitelistedWithInvalidVariation.

/**
 * Verify that {@link DecisionService#getWhitelistedVariation(Experiment, String)} returns null
 * when an invalid variation key is found in the forced variations mapping.
 */
@Test
public void getWhitelistedWithInvalidVariation() throws Exception {
    String userId = "testUser1";
    String invalidVariationKey = "invalidVarKey";
    Bucketer bucketer = new Bucketer(validProjectConfig);
    DecisionService decisionService = new DecisionService(bucketer, mockErrorHandler, validProjectConfig, null);
    List<Variation> variations = Collections.singletonList(new Variation("1", "var1"));
    List<TrafficAllocation> trafficAllocations = Collections.singletonList(new TrafficAllocation("1", 1000));
    Map<String, String> userIdToVariationKeyMap = Collections.singletonMap(userId, invalidVariationKey);
    Experiment experiment = new Experiment("1234", "exp_key", "Running", "1", Collections.<String>emptyList(), variations, userIdToVariationKeyMap, trafficAllocations);
    logbackVerifier.expectMessage(Level.ERROR, "Variation \"" + invalidVariationKey + "\" is not in the datafile. Not activating user \"" + userId + "\".");
    assertNull(decisionService.getWhitelistedVariation(experiment, userId));
}
Also used : TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Aggregations

TrafficAllocation (com.optimizely.ab.config.TrafficAllocation)15 Experiment (com.optimizely.ab.config.Experiment)11 ArrayList (java.util.ArrayList)11 Variation (com.optimizely.ab.config.Variation)8 Group (com.optimizely.ab.config.Group)4 JSONObject (org.json.JSONObject)3 JSONObject (org.json.simple.JSONObject)3 Test (org.junit.Test)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 ExhaustiveTest (com.optimizely.ab.categories.ExhaustiveTest)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 JsonElement (com.google.gson.JsonElement)1 Map (java.util.Map)1 JSONArray (org.json.JSONArray)1 JSONArray (org.json.simple.JSONArray)1 Matchers.anyString (org.mockito.Matchers.anyString)1