Search in sources :

Example 16 with UserAttribute

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

the class JsonConfigParser method parseAudiences.

private List<Audience> parseAudiences(JSONArray audienceJson) {
    List<Audience> audiences = new ArrayList<Audience>(audienceJson.length());
    for (int i = 0; i < audienceJson.length(); i++) {
        Object obj = audienceJson.get(i);
        JSONObject audienceObject = (JSONObject) obj;
        String id = audienceObject.getString("id");
        String key = audienceObject.getString("name");
        Object conditionsObject = audienceObject.get("conditions");
        if (conditionsObject instanceof String) {
            // should always be true
            JSONTokener tokener = new JSONTokener((String) conditionsObject);
            char token = tokener.nextClean();
            if (token == '[') {
                // must be an array
                conditionsObject = new JSONArray((String) conditionsObject);
            } else if (token == '{') {
                conditionsObject = new JSONObject((String) conditionsObject);
            }
        }
        Condition conditions = ConditionUtils.<UserAttribute>parseConditions(UserAttribute.class, conditionsObject);
        audiences.add(new Audience(id, key, conditions));
    }
    return audiences;
}
Also used : JSONTokener(org.json.JSONTokener) Condition(com.optimizely.ab.config.audience.Condition) AudienceIdCondition(com.optimizely.ab.config.audience.AudienceIdCondition) JSONObject(org.json.JSONObject) Audience(com.optimizely.ab.config.audience.Audience) UserAttribute(com.optimizely.ab.config.audience.UserAttribute) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject)

Example 17 with UserAttribute

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

the class ConditionUtils method parseConditions.

public static <T> Condition parseConditions(Class<T> clazz, Object object) throws InvalidAudienceCondition {
    if (object instanceof List) {
        List<Object> objectList = (List<Object>) object;
        return ConditionUtils.<T>parseConditions(clazz, objectList);
    } else if (object instanceof String) {
        // looking for audience conditions in experiment
        AudienceIdCondition audienceIdCondition = new AudienceIdCondition<T>((String) object);
        if (clazz.isInstance(audienceIdCondition)) {
            return audienceIdCondition;
        } else {
            throw new InvalidAudienceCondition(String.format("Expected AudienceIdCondition got %s", clazz.getCanonicalName()));
        }
    } else {
        try {
            if (object instanceof LinkedTreeMap) {
                // gson
                if (clazz != UserAttribute.class) {
                    throw new InvalidAudienceCondition(String.format("Expected UserAttributes got %s", clazz.getCanonicalName()));
                }
                LinkedTreeMap<String, ?> conditionMap = (LinkedTreeMap<String, ?>) object;
                return new UserAttribute((String) conditionMap.get("name"), (String) conditionMap.get("type"), (String) conditionMap.get("match"), conditionMap.get("value"));
            }
        } catch (NoClassDefFoundError ex) {
            // no gson loaded... not sure we need to log this if they don't use gson.
            logger.debug("parser: gson library not loaded");
        }
        try {
            if (object instanceof JSONObject) {
                // simple json
                if (clazz != UserAttribute.class) {
                    throw new InvalidAudienceCondition(String.format("Expected UserAttributes got %s", clazz.getCanonicalName()));
                }
                JSONObject conditionMap = (JSONObject) object;
                return new UserAttribute((String) conditionMap.get("name"), (String) conditionMap.get("type"), (String) conditionMap.get("match"), conditionMap.get("value"));
            }
        } catch (NoClassDefFoundError ex) {
            logger.debug("parser: simple json not found");
        }
        try {
            if (object instanceof org.json.JSONArray) {
                // json
                return ConditionUtils.<T>parseConditions(clazz, (org.json.JSONArray) object);
            } else if (object instanceof org.json.JSONObject) {
                // json
                if (clazz != UserAttribute.class) {
                    throw new InvalidAudienceCondition(String.format("Expected UserAttributes got %s", clazz.getCanonicalName()));
                }
                org.json.JSONObject conditionMap = (org.json.JSONObject) object;
                String match = null;
                Object value = null;
                if (conditionMap.has("match")) {
                    match = (String) conditionMap.get("match");
                }
                if (conditionMap.has("value")) {
                    value = conditionMap.get("value");
                }
                return new UserAttribute((String) conditionMap.get("name"), (String) conditionMap.get("type"), match, value);
            }
        } catch (NoClassDefFoundError ex) {
            logger.debug("parser: json package not found.");
        }
        if (clazz != UserAttribute.class) {
            throw new InvalidAudienceCondition(String.format("Expected UserAttributes got %s", clazz.getCanonicalName()));
        }
        Map<String, ?> conditionMap = (Map<String, ?>) object;
        return new UserAttribute((String) conditionMap.get("name"), (String) conditionMap.get("type"), (String) conditionMap.get("match"), conditionMap.get("value"));
    }
}
Also used : AudienceIdCondition(com.optimizely.ab.config.audience.AudienceIdCondition) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) UserAttribute(com.optimizely.ab.config.audience.UserAttribute) JSONObject(org.json.simple.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) JSONObject(org.json.simple.JSONObject) Map(java.util.Map) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap)

Example 18 with UserAttribute

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

the class JsonConfigParserTest method parseAudience.

@Test
public void parseAudience() throws Exception {
    JSONObject jsonObject = new JSONObject();
    jsonObject.append("id", "123");
    jsonObject.append("name", "blah");
    jsonObject.append("conditions", "[\"and\", [\"or\", [\"or\", {\"name\": \"doubleKey\", \"type\": \"custom_attribute\", \"match\":\"lt\", \"value\":100.0}]]]");
    Condition<UserAttribute> condition = ConditionUtils.parseConditions(UserAttribute.class, new JSONArray("[\"and\", [\"or\", [\"or\", {\"name\": \"doubleKey\", \"type\": \"custom_attribute\", \"match\":\"lt\", \"value\":100.0}]]]"));
    assertNotNull(condition);
}
Also used : JSONObject(org.json.JSONObject) UserAttribute(com.optimizely.ab.config.audience.UserAttribute) JSONArray(org.json.JSONArray) Test(org.junit.Test)

Example 19 with UserAttribute

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

the class JsonSimpleConfigParserTest method parseAudienceLeaf.

@Test
public void parseAudienceLeaf() throws Exception {
    JSONObject jsonObject = new JSONObject();
    jsonObject.append("id", "123");
    jsonObject.append("name", "blah");
    jsonObject.append("conditions", "{\"name\": \"doubleKey\", \"type\": \"custom_attribute\", \"match\":\"lt\", \"value\":100.0}");
    Condition<UserAttribute> condition = ConditionUtils.parseConditions(UserAttribute.class, new JSONObject("{\"name\": \"doubleKey\", \"type\": \"custom_attribute\", \"match\":\"lt\", \"value\":100.0}"));
    assertNotNull(condition);
}
Also used : JSONObject(org.json.JSONObject) UserAttribute(com.optimizely.ab.config.audience.UserAttribute) Test(org.junit.Test)

Example 20 with UserAttribute

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

the class DatafileProjectConfigTest method verifyGetAudienceConditionsFromValidId.

/**
 * Asserts that getAudience returns the respective audience, provided the
 * audience ID parameter is valid.
 */
@Test
public void verifyGetAudienceConditionsFromValidId() throws Exception {
    List<Condition> userAttributes = new ArrayList<Condition>();
    userAttributes.add(new UserAttribute("browser_type", "custom_attribute", null, "firefox"));
    OrCondition orInner = new OrCondition(userAttributes);
    NotCondition notCondition = new NotCondition(orInner);
    List<Condition> outerOrList = new ArrayList<Condition>();
    outerOrList.add(notCondition);
    OrCondition orOuter = new OrCondition(outerOrList);
    List<Condition> andList = new ArrayList<Condition>();
    andList.add(orOuter);
    Condition expectedConditions = new AndCondition(andList);
    Condition actualConditions = projectConfig.getAudience("100").getConditions();
    assertThat(actualConditions, is(expectedConditions));
}
Also used : Condition(com.optimizely.ab.config.audience.Condition) OrCondition(com.optimizely.ab.config.audience.OrCondition) NotCondition(com.optimizely.ab.config.audience.NotCondition) AndCondition(com.optimizely.ab.config.audience.AndCondition) NotCondition(com.optimizely.ab.config.audience.NotCondition) UserAttribute(com.optimizely.ab.config.audience.UserAttribute) OrCondition(com.optimizely.ab.config.audience.OrCondition) AndCondition(com.optimizely.ab.config.audience.AndCondition) Test(org.junit.Test)

Aggregations

UserAttribute (com.optimizely.ab.config.audience.UserAttribute)20 Condition (com.optimizely.ab.config.audience.Condition)15 AndCondition (com.optimizely.ab.config.audience.AndCondition)10 NotCondition (com.optimizely.ab.config.audience.NotCondition)10 OrCondition (com.optimizely.ab.config.audience.OrCondition)10 ArrayList (java.util.ArrayList)10 Audience (com.optimizely.ab.config.audience.Audience)8 JSONObject (org.json.JSONObject)7 Test (org.junit.Test)6 AudienceIdCondition (com.optimizely.ab.config.audience.AudienceIdCondition)5 HashMap (java.util.HashMap)5 JSONArray (org.json.JSONArray)4 JSONObject (org.json.simple.JSONObject)4 List (java.util.List)3 LinkedTreeMap (com.google.gson.internal.LinkedTreeMap)2 ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 JsonObject (com.google.gson.JsonObject)1 TypedAudience (com.optimizely.ab.config.audience.TypedAudience)1 Map (java.util.Map)1