Skip to content

Update conditions to use StringBuilder instead of StringJoiner #443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,14 @@ public String getOperandOrId() {

@Override
public String toJson() {
StringJoiner s = new StringJoiner(", ", "[", "]");
s.add("\"and\"");
StringBuilder s = new StringBuilder();
s.append("[\"and\", ");
for (int i = 0; i < conditions.size(); i++) {
s.add(conditions.get(i).toJson());
s.append(conditions.get(i).toJson());
if (i < conditions.size() - 1)
s.append(", ");
}
s.append("]");
return s.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ public String getOperandOrId() {

@Override
public String toJson() {
StringJoiner s = new StringJoiner(", ","[","]");
s.add("\"not\"");
s.add(condition.toJson());
StringBuilder s = new StringBuilder();
s.append("[\"not\", ");
s.append(condition.toJson());
s.append("]");
return s.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,14 @@ public String getOperandOrId() {

@Override
public String toJson() {
StringJoiner s = new StringJoiner(", ", "[", "]");
s.add("\"or\"");
StringBuilder s = new StringBuilder();
s.append("[\"or\", ");
for (int i = 0; i < conditions.size(); i++) {
s.add(conditions.get(i).toJson());
s.append(conditions.get(i).toJson());
if (i < conditions.size() - 1)
s.append(", ");
}
s.append("]");
return s.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.internal.matchers.Or;

import java.math.BigInteger;
import java.util.*;
Expand Down Expand Up @@ -56,6 +57,21 @@ public void initialize() {
testTypedUserAttributes.put("null_val", null);
}

@Test
public void nullConditionTest() throws Exception {
NullCondition nullCondition = new NullCondition();
assertEquals(null, nullCondition.toJson());
assertEquals(null, nullCondition.getOperandOrId());
}

@Test
public void emptyConditionTest() throws Exception {
EmptyCondition emptyCondition = new EmptyCondition();
assertEquals(null, emptyCondition.toJson());
assertEquals(null, emptyCondition.getOperandOrId());
assertEquals(true, emptyCondition.evaluate(null, null));
}

/**
* Verify that UserAttribute.toJson returns a json represented string of conditions.
*/
Expand All @@ -66,6 +82,41 @@ public void userAttributeConditionsToJson() throws Exception {
assertEquals(testInstance.toJson(), expectedConditionJsonString);
}

/**
* Verify that AndCondition.toJson returns a json represented string of conditions.
*/
@Test
public void andConditionsToJsonWithComma() throws Exception {
UserAttribute testInstance1 = new UserAttribute("browser_type", "custom_attribute", "true", "safari");
UserAttribute testInstance2 = new UserAttribute("browser_type", "custom_attribute", "true", "safari");
String expectedConditionJsonString = "[\"and\", [\"or\", {\"name\":\"browser_type\", \"type\":\"custom_attribute\", \"match\":\"true\", \"value\":\"safari\"}, {\"name\":\"browser_type\", \"type\":\"custom_attribute\", \"match\":\"true\", \"value\":\"safari\"}]]";
List<Condition> userConditions = new ArrayList<>();
userConditions.add(testInstance1);
userConditions.add(testInstance2);
OrCondition orCondition = new OrCondition(userConditions);
List<Condition> orConditions = new ArrayList<>();
orConditions.add(orCondition);
AndCondition andCondition = new AndCondition(orConditions);
assertEquals(andCondition.toJson(), expectedConditionJsonString);
}

/**
* Verify that orCondition.toJson returns a json represented string of conditions.
*/
@Test
public void orConditionsToJsonWithComma() throws Exception {
UserAttribute testInstance1 = new UserAttribute("browser_type", "custom_attribute", "true", "safari");
UserAttribute testInstance2 = new UserAttribute("browser_type", "custom_attribute", "true", "safari");
String expectedConditionJsonString = "[\"or\", [\"and\", {\"name\":\"browser_type\", \"type\":\"custom_attribute\", \"match\":\"true\", \"value\":\"safari\"}, {\"name\":\"browser_type\", \"type\":\"custom_attribute\", \"match\":\"true\", \"value\":\"safari\"}]]";
List<Condition> userConditions = new ArrayList<>();
userConditions.add(testInstance1);
userConditions.add(testInstance2);
AndCondition andCondition = new AndCondition(userConditions);
List<Condition> andConditions = new ArrayList<>();
andConditions.add(andCondition);
OrCondition orCondition = new OrCondition(andConditions);
assertEquals(orCondition.toJson(), expectedConditionJsonString);
}

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