|
4 | 4 | import com.fasterxml.jackson.databind.ObjectMapper;
|
5 | 5 | import org.junit.jupiter.api.Test;
|
6 | 6 |
|
| 7 | +import java.util.Arrays; |
7 | 8 | import java.util.List;
|
8 | 9 |
|
9 | 10 | import static com.github.mhewedy.expressions.Expression.*;
|
10 | 11 | import static org.assertj.core.api.Assertions.assertThat;
|
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
11 | 14 |
|
12 | 15 | class ExpressionsTest {
|
13 | 16 |
|
| 17 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 18 | + |
14 | 19 | @Test
|
15 | 20 | public void testObjectWithOrExpression() throws Exception {
|
16 | 21 |
|
17 |
| - ObjectMapper objectMapper = new ObjectMapper(); |
18 |
| - |
19 | 22 | Expressions conditions =
|
20 |
| - objectMapper.readValue( |
21 |
| - "{ \"status\": \"A\", \"$or\": [{ \"qty\": { \"$lt\": 30 } }, { \"item\": { \"$in\": [\"A\", \"D\"] } }] }", |
| 23 | + objectMapper.readValue(""" |
| 24 | + { |
| 25 | + "status": "A", |
| 26 | + "$or": [ |
| 27 | + { |
| 28 | + "qty": { "$lt": 30 } |
| 29 | + }, |
| 30 | + { |
| 31 | + "item": {"$in": ["A", "D"] } |
| 32 | + } |
| 33 | + ] |
| 34 | + } |
| 35 | + """, |
22 | 36 | Expressions.class);
|
23 | 37 |
|
24 | 38 | final List<Expression> expression = conditions.getExpressions();
|
@@ -68,4 +82,72 @@ public void testObjectWithOrExpression() throws Exception {
|
68 | 82 | }
|
69 | 83 | }
|
70 | 84 |
|
| 85 | + @Test |
| 86 | + void testExtractFieldsWithCompoundOperatorsJSON() throws Exception { |
| 87 | + String json = """ |
| 88 | + { |
| 89 | + "firstName": "John", |
| 90 | + "$or": [ |
| 91 | + { |
| 92 | + "lastName": "Doe" |
| 93 | + }, |
| 94 | + { |
| 95 | + "age": { "$gt": 30 } |
| 96 | + } |
| 97 | + ] |
| 98 | + } |
| 99 | + """; |
| 100 | + Expressions expressions = objectMapper.readValue(json, Expressions.class); |
| 101 | + List<String> fields = Expressions.extractFields(expressions); |
| 102 | + |
| 103 | + assertEquals(Arrays.asList("firstName", "lastName", "age"), fields); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void testExtractFieldsWithEmptyJSON() throws Exception { |
| 108 | + String json = "{}"; |
| 109 | + Expressions expressions = objectMapper.readValue(json, Expressions.class); |
| 110 | + List<String> fields = Expressions.extractFields(expressions); |
| 111 | + |
| 112 | + assertTrue(fields.isEmpty()); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void testExtractFieldsWithNestedAndOperatorJSON() throws Exception { |
| 117 | + String json = """ |
| 118 | + { |
| 119 | + "$and": [ |
| 120 | + { "country": "USA" }, |
| 121 | + { "state": "California" } |
| 122 | + ] |
| 123 | + } |
| 124 | + """; |
| 125 | + Expressions expressions = objectMapper.readValue(json, Expressions.class); |
| 126 | + List<String> fields = Expressions.extractFields(expressions); |
| 127 | + |
| 128 | + assertEquals(Arrays.asList("country", "state"), fields); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void testExtractFieldsWithMultipleNestedOperatorsJSON() throws Exception { |
| 133 | + String json = """ |
| 134 | + { |
| 135 | + "$and": [ |
| 136 | + { |
| 137 | + "$or": [ |
| 138 | + { "city": "New York" }, |
| 139 | + { "zipcode": "10001" } |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "city.country": "USA" |
| 144 | + } |
| 145 | + ] |
| 146 | + } |
| 147 | + """; |
| 148 | + Expressions expressions = objectMapper.readValue(json, Expressions.class); |
| 149 | + List<String> fields = Expressions.extractFields(expressions); |
| 150 | + |
| 151 | + assertEquals(Arrays.asList("city", "zipcode", "city.country"), fields); |
| 152 | + } |
71 | 153 | }
|
0 commit comments