Skip to content

Commit 038157a

Browse files
additional tests for coverage and added comma for json format.
1 parent 256f578 commit 038157a

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

core-api/src/main/java/com/optimizely/ab/config/audience/AndCondition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public String getOperandOrId() {
7777
@Override
7878
public String toJson() {
7979
StringBuilder s = new StringBuilder();
80-
s.append("[\"and\"");
80+
s.append("[\"and\", ");
8181
for (int i = 0; i < conditions.size(); i++) {
8282
s.append(conditions.get(i).toJson());
8383
if (i < conditions.size() - 1)

core-api/src/main/java/com/optimizely/ab/config/audience/OrCondition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public String getOperandOrId() {
7575
@Override
7676
public String toJson() {
7777
StringBuilder s = new StringBuilder();
78-
s.append("[\"or\"");
78+
s.append("[\"or\", ");
7979
for (int i = 0; i < conditions.size(); i++) {
8080
s.append(conditions.get(i).toJson());
8181
if (i < conditions.size() - 1)

core-api/src/test/java/com/optimizely/ab/config/audience/AudienceConditionEvaluationTest.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ public void initialize() {
5757
testTypedUserAttributes.put("null_val", null);
5858
}
5959

60+
@Test
61+
public void nullConditionTest() throws Exception {
62+
NullCondition nullCondition = new NullCondition();
63+
assertEquals(null, nullCondition.toJson());
64+
assertEquals(null, nullCondition.getOperandOrId());
65+
}
66+
67+
@Test
68+
public void emptyConditionTest() throws Exception {
69+
EmptyCondition emptyCondition = new EmptyCondition();
70+
assertEquals(null, emptyCondition.toJson());
71+
assertEquals(null, emptyCondition.getOperandOrId());
72+
assertEquals(true, emptyCondition.evaluate(null, null));
73+
}
74+
6075
/**
6176
* Verify that UserAttribute.toJson returns a json represented string of conditions.
6277
*/
@@ -67,11 +82,14 @@ public void userAttributeConditionsToJson() throws Exception {
6782
assertEquals(testInstance.toJson(), expectedConditionJsonString);
6883
}
6984

85+
/**
86+
* Verify that AndCondition.toJson returns a json represented string of conditions.
87+
*/
7088
@Test
71-
public void userAttributeConditionsToJsonWithComma() throws Exception {
89+
public void andConditionsToJsonWithComma() throws Exception {
7290
UserAttribute testInstance1 = new UserAttribute("browser_type", "custom_attribute", "true", "safari");
7391
UserAttribute testInstance2 = new UserAttribute("browser_type", "custom_attribute", "true", "safari");
74-
String expectedConditionJsonString = "[\"and\"[\"or\"{\"name\":\"browser_type\", \"type\":\"custom_attribute\", \"match\":\"true\", \"value\":\"safari\"}, {\"name\":\"browser_type\", \"type\":\"custom_attribute\", \"match\":\"true\", \"value\":\"safari\"}]]";
92+
String expectedConditionJsonString = "[\"and\", [\"or\", {\"name\":\"browser_type\", \"type\":\"custom_attribute\", \"match\":\"true\", \"value\":\"safari\"}, {\"name\":\"browser_type\", \"type\":\"custom_attribute\", \"match\":\"true\", \"value\":\"safari\"}]]";
7593
List<Condition> userConditions = new ArrayList<>();
7694
userConditions.add(testInstance1);
7795
userConditions.add(testInstance2);
@@ -82,6 +100,23 @@ public void userAttributeConditionsToJsonWithComma() throws Exception {
82100
assertEquals(andCondition.toJson(), expectedConditionJsonString);
83101
}
84102

103+
/**
104+
* Verify that orCondition.toJson returns a json represented string of conditions.
105+
*/
106+
@Test
107+
public void orConditionsToJsonWithComma() throws Exception {
108+
UserAttribute testInstance1 = new UserAttribute("browser_type", "custom_attribute", "true", "safari");
109+
UserAttribute testInstance2 = new UserAttribute("browser_type", "custom_attribute", "true", "safari");
110+
String expectedConditionJsonString = "[\"or\", [\"and\", {\"name\":\"browser_type\", \"type\":\"custom_attribute\", \"match\":\"true\", \"value\":\"safari\"}, {\"name\":\"browser_type\", \"type\":\"custom_attribute\", \"match\":\"true\", \"value\":\"safari\"}]]";
111+
List<Condition> userConditions = new ArrayList<>();
112+
userConditions.add(testInstance1);
113+
userConditions.add(testInstance2);
114+
AndCondition andCondition = new AndCondition(userConditions);
115+
List<Condition> andConditions = new ArrayList<>();
116+
andConditions.add(andCondition);
117+
OrCondition orCondition = new OrCondition(andConditions);
118+
assertEquals(orCondition.toJson(), expectedConditionJsonString);
119+
}
85120

86121
/**
87122
* Verify that UserAttribute.evaluate returns true on exact-matching visitor attribute data.

0 commit comments

Comments
 (0)