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