8000 Update conditions to use StringBuilder instead of StringJoiner (#443) · optimizely/java-sdk@5de0a7b · GitHub
[go: up one dir, main page]

Skip to content

Commit 5de0a7b

Browse files
Update conditions to use StringBuilder instead of StringJoiner (#443)
- Changing StringJoiner to StringBuilder for compatibility with Android - Additional test cases for coverage and to verify json format of condition string
1 parent 70a42c6 commit 5de0a7b

File tree

4 files changed

+67
-9
lines changed

4 files changed

+67
-9
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,14 @@ public String getOperandOrId() {
7676

7777
@Override
7878
public String toJson() {
79-
StringJoiner s = new StringJoiner(", ", "[", "]");
80-
s.add("\"and\"");
79+
StringBuilder s = new StringBuilder();
80+
s.append("[\"and\", ");
8181
for (int i = 0; i < conditions.size(); i++) {
82-
s.add(conditions.get(i).toJson());
82+
s.append(conditions.get(i).toJson());
83+
if (i < conditions.size() - 1)
84+
s.append(", ");
8385
}
86+
s.append("]");
8487
return s.toString();
8588
}
8689

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ public String getOperandOrId() {
5656

5757
@Override
5858
public String toJson() {
59-
StringJoiner s = new StringJoiner(", ","[","]");
60-
s.add("\"not\"");
61-
s.add(condition.toJson());
59+
StringBuilder s = new StringBuilder();
60+
s.append("[\"not\", ");
61+
s.append(condition.toJson());
62+
s.append("]");
6263
return s.toString();
6364
}
6465

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ public String getOperandOrId() {
7474

7575
@Override
7676
public String toJson() {
77-
StringJoiner s = new StringJoiner(", ", "[", "]");
78-
s.add("\"or\"");
77+
StringBuilder s = new StringBuilder();
78+
s.append("[\"or\", ");
7979
for (int i = 0; i < conditions.size(); i++) {
80-
s.add(conditions.get(i).toJson());
80+
s.append(conditions.get(i).toJson());
81+
if (i < conditions.size() - 1)
82+
s.append(", ");
8183
}
84+
s.append("]");
8285
return s.toString();
8386
}
8487

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.Before;
2323
import org.junit.Rule;
2424
import org.junit.Test;
25+
import org.mockito.internal.matchers.Or;
2526

2627
import java.math.BigInteger;
2728
import java.util.*;
@@ -56,6 +57,21 @@ public void initialize() {
5657
testTypedUserAttributes.put("null_val", null);
5758
}
5859

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+
5975
/**
6076
* Verify that UserAttribute.toJson returns a json represented string of conditions.
6177
*/
@@ -66,6 +82,41 @@ public void userAttributeConditionsToJson() throws Exception {
6682
assertEquals(testInstance.toJson(), expectedConditionJsonString);
6783
}
6884

85+
/**
86+
* Verify that AndCondition.toJson returns a json represented string of conditions.
87+
*/
88+
@Test
89+
public void andConditionsToJsonWithComma() throws Exception {
90+
UserAttribute testInstance1 = new UserAttribute("browser_type", "custom_attribute", "true", "safari");
91+
UserAttribute testInstance2 = new UserAttribute("browser_type", "custom_attribute", "true", "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\"}]]";
93+
List<Condition> userConditions = new ArrayList<>();
94+
userConditions.add(testInstance1);
95+
userConditions.add(testInstance2);
96+
OrCondition orCondition = new OrCondition(userConditions);
97+
List<Condition> orConditions = new ArrayList<>();
98+
orConditions.add(orCondition);
99+
AndCondition andCondition = new AndCondition(orConditions);
100+
assertEquals(andCondition.toJson(), expectedConditionJsonString);
101+
}
102+
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+
}
69120

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

0 commit comments

Comments
 (0)
0