Skip to content

Commit 59358aa

Browse files
committed
Add jsonpath expression tests
1 parent 60598cf commit 59358aa

12 files changed

+211
-1
lines changed

src/test/java/io/github/jamsesso/jsonlogic/AllExpressionTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.Test;
44

5+
import static io.github.jamsesso.jsonlogic.JsonLogicExceptionTestUtility.testErrorJsonPath;
56
import static org.junit.Assert.assertEquals;
67

78
public class AllExpressionTests {
@@ -17,4 +18,13 @@ public void testAll() throws JsonLogicException {
1718
assertEquals(true, jsonLogic.apply("{\"all\": [[1, 2, 3], {\">\": [{\"var\": \"\"}, 0]}]}", null));
1819
assertEquals(false, jsonLogic.apply("{\"all\": [[1, 2, 3], {\">\": [{\"var\": \"\"}, 1]}]}", null));
1920
}
21+
22+
@Test
23+
public void testInvalidAll() {
24+
String json = "{\"all\": [[1, 2, 3], {\">\": [{\"var\": \"\"}, {}]}]}";
25+
// ---------------------------------------------------------- ^ ----
26+
String expectedErrorJsonPath = "$.all[1].>[1]";
27+
28+
testErrorJsonPath(jsonLogic, json, expectedErrorJsonPath);
29+
}
2030
}

src/test/java/io/github/jamsesso/jsonlogic/ArrayHasExpressionTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.Test;
44

5+
import static io.github.jamsesso.jsonlogic.JsonLogicExceptionTestUtility.testErrorJsonPath;
56
import static org.junit.Assert.assertEquals;
67

78
public class ArrayHasExpressionTests {
@@ -38,4 +39,13 @@ public void testNoneAll() throws JsonLogicException {
3839
assertEquals(true, jsonLogic.apply("{\"none\": [[1, 2, 3], {\">\": [{\"var\": \"\"}, 3]}]}", null));
3940
assertEquals(false, jsonLogic.apply("{\"none\": [[1, 2, 3], {\">\": [{\"var\": \"\"}, 2]}]}", null));
4041
}
42+
43+
@Test
44+
public void testInvalidArrayHasExpression() {
45+
String json = "{\"some\": [[1, 2, 3], {\">\": [{\"var\": \"\"}, {}]}]}";
46+
// ----------------------------------------------------------- ^ ----
47+
String expectedErrorJsonPath = "$.some[1].>[1]";
48+
49+
testErrorJsonPath(jsonLogic, json, expectedErrorJsonPath);
50+
}
4151
}

src/test/java/io/github/jamsesso/jsonlogic/EqualityExpressionTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.Test;
44

5+
import static io.github.jamsesso.jsonlogic.JsonLogicExceptionTestUtility.testErrorJsonPath;
56
import static org.junit.Assert.assertEquals;
67

78
public class EqualityExpressionTests {
@@ -26,4 +27,22 @@ public void testDifferentValueDifferentType() throws JsonLogicException {
2627
public void testEmptyStringAndZeroComparison() throws JsonLogicException {
2728
assertEquals(true, jsonLogic.apply("{\"==\": [\" \", 0]}", null));
2829
}
30+
31+
@Test
32+
public void testInvalidArgumentCountEquality() {
33+
String json = "{\"==\": [1]}";
34+
// --------------------- ^ -
35+
String expectedErrorJsonPath = "$.==";
36+
37+
testErrorJsonPath(jsonLogic, json, expectedErrorJsonPath);
38+
}
39+
40+
@Test
41+
public void testInvalidEquality() {
42+
String json = "{\"==\": [{}, true]}";
43+
// --------------------- ^ --------
44+
String expectedErrorJsonPath = "$.==[0]";
45+
46+
testErrorJsonPath(jsonLogic, json, expectedErrorJsonPath);
47+
}
2948
}

src/test/java/io/github/jamsesso/jsonlogic/FilterExpressionTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
import java.util.List;
66

7+
import static io.github.jamsesso.jsonlogic.JsonLogicExceptionTestUtility.testErrorJsonPath;
78
import static org.junit.Assert.assertEquals;
89

910
public class FilterExpressionTests {
1011
private static final JsonLogic jsonLogic = new JsonLogic();
1112

1213
@Test
13-
public void testMap() throws JsonLogicException {
14+
public void testFilter() throws JsonLogicException {
1415
String json = "{\"filter\": [\n" +
1516
" {\"var\": \"\"},\n" +
1617
" {\"==\": [{\"%\": [{\"var\": \"\"}, 2]}, 0]}\n" +
@@ -23,4 +24,17 @@ public void testMap() throws JsonLogicException {
2324
assertEquals(4.0, ((List) result).get(1));
2425
assertEquals(6.0, ((List) result).get(2));
2526
}
27+
28+
@Test
29+
public void testInvalidFilter() {
30+
String json = "{\"filter\": [\n" +
31+
" {\"var\": {}},\n" +
32+
// -------- ^ ------
33+
" {\"==\": [{\"%\": [{\"var\": \"\"}, 2]}, 0]}\n" +
34+
"]}";
35+
36+
String expectedErrorJsonPath = "$.filter[0].var";
37+
38+
testErrorJsonPath(jsonLogic, json, expectedErrorJsonPath);
39+
}
2640
}

src/test/java/io/github/jamsesso/jsonlogic/IfExpressionTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import org.junit.Test;
44

5+
import static io.github.jamsesso.jsonlogic.JsonLogicExceptionTestUtility.testErrorJsonPath;
56
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.fail;
68

79
public class IfExpressionTests {
810
private static final JsonLogic jsonLogic = new JsonLogic();
@@ -34,4 +36,31 @@ public void testIfElseIfElse() throws JsonLogicException {
3436

3537
assertEquals("liquid", result);
3638
}
39+
40+
@Test
41+
public void testIfElseJsonPath_pos0() {
42+
String json = "{\"if\" : [{}, \"yes\", \"no\"]}";
43+
// --------------------- ^ --------------------
44+
String expectedErrorJsonPath = "$.if[0]";
45+
46+
testErrorJsonPath(jsonLogic, json, expectedErrorJsonPath);
47+
}
48+
49+
@Test
50+
public void testIfElseJsonPath_pos1() {
51+
String json = "{\"if\" : [true, {}, \"no\"]}";
52+
// --------------------------- ^ -----------
53+
String expectedErrorJsonPath = "$.if[1]";
54+
55+
testErrorJsonPath(jsonLogic, json, expectedErrorJsonPath);
56+
}
57+
58+
@Test
59+
public void testIfElseJsonPath_pos2() {
60+
String json = "{\"if\" : [false, \"yes\", {}]}";
61+
// ------------------------------------- ^ ---
62+
String expectedErrorJsonPath = "$.if[2]";
63+
64+
testErrorJsonPath(jsonLogic, json, expectedErrorJsonPath);
65+
}
3766
}

src/test/java/io/github/jamsesso/jsonlogic/InequalityExpressionTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.Test;
44

5+
import static io.github.jamsesso.jsonlogic.JsonLogicExceptionTestUtility.testErrorJsonPath;
56
import static org.junit.Assert.assertEquals;
67

78
public class InequalityExpressionTests {
@@ -16,4 +17,22 @@ public void testDifferentValueSameType() throws JsonLogicException {
1617
public void testSameValueDifferentType() throws JsonLogicException {
1718
assertEquals(false, jsonLogic.apply("{\"!=\": [1.0, \"1\"]}", null));
1819
}
20+
21+
@Test
22+
public void testInvalidArgumentCountInequality() {
23+
String json = "{\"!=\": [1]}";
24+
// --------------------- ^ -
25+
String expectedErrorJsonPath = "$.!=";
26+
27+
testErrorJsonPath(jsonLogic, json, expectedErrorJsonPath);
28+
}
29+
30+
@Test
31+
public void testInvalidInequality() {
32+
String json = "{\"!=\": [{}, true]}";
33+
// --------------------- ^ --------
34+
String expectedErrorJsonPath = "$.!=[0]";
35+
36+
testErrorJsonPath(jsonLogic, json, expectedErrorJsonPath);
37+
}
1938
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.jamsesso.jsonlogic;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.fail;
5+
6+
public class JsonLogicExceptionTestUtility {
7+
private JsonLogicExceptionTestUtility() {
8+
}
9+
10+
public static void testErrorJsonPath(JsonLogic jsonLogic, String json, String expectedErrorJsonPath) {
11+
try {
12+
Object result = jsonLogic.apply(json,null);
13+
fail("Expected JsonLogicException, but got result: " + result);
14+
} catch (JsonLogicException e) {
15+
String path = e.getJsonPath();
16+
assertEquals(expectedErrorJsonPath, path);
17+
}
18+
}
19+
}

src/test/java/io/github/jamsesso/jsonlogic/LogicExpressionTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.Test;
44

5+
import static io.github.jamsesso.jsonlogic.JsonLogicExceptionTestUtility.testErrorJsonPath;
56
import static org.junit.Assert.assertEquals;
67

78
public class LogicExpressionTests {
@@ -16,4 +17,13 @@ public void testOr() throws JsonLogicException {
1617
public void testAnd() throws JsonLogicException {
1718
assertEquals("", jsonLogic.apply("{\"and\": [true, \"\", 3]}", null));
1819
}
20+
21+
@Test
22+
public void testInvalidLogicExpression() {
23+
String json = "{\"or\": [0, {}, \"a\"]}";
24+
// ----------------------- ^ ----------
25+
String expectedErrorJsonPath = "$.or[1]";
26+
27+
testErrorJsonPath(jsonLogic, json, expectedErrorJsonPath);
28+
}
1929
}

src/test/java/io/github/jamsesso/jsonlogic/MapExpressionTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.List;
66

7+
import static io.github.jamsesso.jsonlogic.JsonLogicExceptionTestUtility.testErrorJsonPath;
78
import static org.junit.Assert.assertEquals;
89

910
public class MapExpressionTests {
@@ -23,4 +24,17 @@ public void testMap() throws JsonLogicException {
2324
assertEquals(4.0, ((List) result).get(1));
2425
assertEquals(6.0, ((List) result).get(2));
2526
}
27+
28+
@Test
29+
public void testInvalidMap() {
30+
String json = "{\"map\": [\n" +
31+
" {\"var\": \"\"},\n" +
32+
" {\"*\": [{}, 2]}\n" +
33+
// ------- ^ ---------
34+
"]}";
35+
36+
String expectedErrorJsonPath = "$.map[1].*[0]";
37+
38+
testErrorJsonPath(jsonLogic, json, expectedErrorJsonPath);
39+
}
2640
}

src/test/java/io/github/jamsesso/jsonlogic/StrictEqualityExpressionTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.Test;
44

5+
import static io.github.jamsesso.jsonlogic.JsonLogicExceptionTestUtility.testErrorJsonPath;
56
import static org.junit.Assert.assertEquals;
67

78
public class StrictEqualityExpressionTests {
@@ -16,4 +17,22 @@ public void testSameValueSameType() throws JsonLogicException {
1617
public void testSameValueDifferentType() throws JsonLogicException {
1718
assertEquals(false, jsonLogic.apply("{\"===\": [1, \"1\"]}", null));
1819
}
20+
21+
@Test
22+
public void testInvalidArgumentCountStrictEquality() {
23+
String json = "{\"===\": [1]}";
24+
// ---------------------- ^ -
25+
String expectedErrorJsonPath = "$.===";
26+
27+
testErrorJsonPath(jsonLogic, json, expectedErrorJsonPath);
28+
}
29+
30+
@Test
31+
public void testInvalidStrictEquality() {
32+
String json = "{\"===\": [{}, true]}";
33+
// ---------------------- ^ --------
34+
String expectedErrorJsonPath = "$.===[0]";
35+
36+
testErrorJsonPath(jsonLogic, json, expectedErrorJsonPath);
37+
}
1938
}

0 commit comments

Comments
 (0)