Search in sources :

Example 1 with TrafficAllocation

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

the class JsonSimpleConfigParser method parseGroups.

private List<Group> parseGroups(JSONArray groupJson) {
    List<Group> groups = new ArrayList<Group>(groupJson.size());
    for (Object obj : groupJson) {
        JSONObject groupObject = (JSONObject) obj;
        String id = (String) groupObject.get("id");
        String policy = (String) groupObject.get("policy");
        List<Experiment> experiments = parseExperiments((JSONArray) groupObject.get("experiments"), id);
        List<TrafficAllocation> trafficAllocations = parseTrafficAllocation((JSONArray) groupObject.get("trafficAllocation"));
        groups.add(new Group(id, policy, experiments, trafficAllocations));
    }
    return groups;
}
Also used : Group(com.optimizely.ab.config.Group) JSONObject(org.json.simple.JSONObject) TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) JSONObject(org.json.simple.JSONObject)

Example 2 with TrafficAllocation

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

the class JsonConfigParser method parseGroups.

private List<Group> parseGroups(JSONArray groupJson) {
    List<Group> groups = new ArrayList<Group>(groupJson.length());
    for (Object obj : groupJson) {
        JSONObject groupObject = (JSONObject) obj;
        String id = groupObject.getString("id");
        String policy = groupObject.getString("policy");
        List<Experiment> experiments = parseExperiments(groupObject.getJSONArray("experiments"), id);
        List<TrafficAllocation> trafficAllocations = parseTrafficAllocation(groupObject.getJSONArray("trafficAllocation"));
        groups.add(new Group(id, policy, experiments, trafficAllocations));
    }
    return groups;
}
Also used : Group(com.optimizely.ab.config.Group) JSONObject(org.json.JSONObject) TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) JSONObject(org.json.JSONObject)

Example 3 with TrafficAllocation

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

the class JsonSimpleConfigParser method parseTrafficAllocation.

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

Example 4 with TrafficAllocation

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

the class GroupJacksonDeserializer method deserialize.

@Override
public Group deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = parser.getCodec().readTree(parser);
    String id = node.get("id").textValue();
    String policy = node.get("policy").textValue();
    List<TrafficAllocation> trafficAllocations = mapper.readValue(node.get("trafficAllocation").toString(), new TypeReference<List<TrafficAllocation>>() {
    });
    JsonNode groupExperimentsJson = node.get("experiments");
    List<Experiment> groupExperiments = new ArrayList<Experiment>();
    if (groupExperimentsJson.isArray()) {
        for (JsonNode groupExperimentJson : groupExperimentsJson) {
            groupExperiments.add(parseExperiment(groupExperimentJson, id));
        }
    }
    return new Group(id, policy, groupExperiments, trafficAllocations);
}
Also used : Group(com.optimizely.ab.config.Group) TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayList(java.util.ArrayList) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 5 with TrafficAllocation

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

the class GroupJacksonDeserializer method parseExperiment.

private Experiment parseExperiment(JsonNode experimentJson, String groupId) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    String id = experimentJson.get("id").textValue();
    String key = experimentJson.get("key").textValue();
    String status = experimentJson.get("status").textValue();
    JsonNode layerIdJson = experimentJson.get("layerId");
    String layerId = layerIdJson == null ? null : layerIdJson.textValue();
    List<String> audienceIds = mapper.readValue(experimentJson.get("audienceIds").toString(), new TypeReference<List<String>>() {
    });
    List<Variation> variations = mapper.readValue(experimentJson.get("variations").toString(), new TypeReference<List<Variation>>() {
    });
    List<TrafficAllocation> trafficAllocations = mapper.readValue(experimentJson.get("trafficAllocation").toString(), new TypeReference<List<TrafficAllocation>>() {
    });
    Map<String, String> userIdToVariationKeyMap = mapper.readValue(experimentJson.get("forcedVariations").toString(), new TypeReference<Map<String, String>>() {
    });
    return new Experiment(id, key, status, layerId, audienceIds, variations, userIdToVariationKeyMap, trafficAllocations, groupId);
}
Also used : Experiment(com.optimizely.ab.config.Experiment) JsonNode(com.fasterxml.jackson.databind.JsonNode) TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) ArrayList(java.util.ArrayList) List(java.util.List) Variation(com.optimizely.ab.config.Variation) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

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