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;
}
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));
}
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;
}
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)));
}
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));
}
Aggregations