|
7 | 7 |
|
8 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; |
9 | 9 | import static org.junit.jupiter.api.Assertions.assertThrows; |
10 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
11 | | -import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_FALSE; |
12 | | -import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_MISSING; |
13 | | -import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_NULL; |
14 | | -import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_TRUE; |
15 | 10 | import static org.opensearch.sql.expression.function.jsonUDF.JsonUtils.*; |
16 | 11 |
|
17 | 12 | import com.fasterxml.jackson.databind.JsonNode; |
18 | | -import java.util.LinkedHashMap; |
19 | 13 | import java.util.List; |
20 | | -import java.util.Map; |
21 | 14 | import org.junit.jupiter.api.Test; |
22 | 15 | import org.junit.jupiter.api.extension.ExtendWith; |
23 | 16 | import org.mockito.junit.jupiter.MockitoExtension; |
24 | | -import org.opensearch.sql.data.model.ExprBooleanValue; |
25 | | -import org.opensearch.sql.data.model.ExprCollectionValue; |
26 | | -import org.opensearch.sql.data.model.ExprDoubleValue; |
27 | | -import org.opensearch.sql.data.model.ExprIntegerValue; |
28 | | -import org.opensearch.sql.data.model.ExprLongValue; |
29 | | -import org.opensearch.sql.data.model.ExprNullValue; |
30 | | -import org.opensearch.sql.data.model.ExprStringValue; |
31 | | -import org.opensearch.sql.data.model.ExprTupleValue; |
32 | | -import org.opensearch.sql.data.model.ExprValue; |
33 | 17 | import org.opensearch.sql.data.model.ExprValueUtils; |
34 | 18 | import org.opensearch.sql.exception.ExpressionEvaluationException; |
35 | 19 | import org.opensearch.sql.exception.SemanticCheckException; |
36 | 20 | import org.opensearch.sql.expression.DSL; |
37 | | -import org.opensearch.sql.expression.FunctionExpression; |
38 | 21 | import org.opensearch.sql.expression.LiteralExpression; |
39 | 22 | import org.opensearch.sql.expression.function.jsonUDF.JsonUtils; |
40 | 23 |
|
41 | 24 | @ExtendWith(MockitoExtension.class) |
42 | 25 | public class JsonFunctionsTest { |
43 | | - @Test |
44 | | - public void json_valid_returns_false() { |
45 | | - List<LiteralExpression> expressions = |
46 | | - List.of( |
47 | | - DSL.literal(LITERAL_MISSING), // missing returns false |
48 | | - DSL.literal(LITERAL_NULL), // null returns false |
49 | | - DSL.literal("invalid"), // invalid type |
50 | | - DSL.literal("{{[}}"), // missing bracket |
51 | | - DSL.literal("[}"), // missing bracket |
52 | | - DSL.literal("}"), // missing bracket |
53 | | - DSL.literal("\"missing quote"), // missing quote |
54 | | - DSL.literal("abc"), // not a type |
55 | | - DSL.literal("97ab"), // not a type |
56 | | - DSL.literal("{1, 2, 3, 4}"), // invalid object |
57 | | - DSL.literal("{\"invalid\":\"json\", \"string\"}"), // invalid object |
58 | | - DSL.literal("{123: 1, true: 2, null: 3}"), // invalid object |
59 | | - DSL.literal("[\"a\": 1, \"b\": 2]") // invalid array |
60 | | - ); |
61 | | - |
62 | | - expressions.stream() |
63 | | - .forEach( |
64 | | - expr -> |
65 | | - assertEquals( |
66 | | - LITERAL_FALSE, |
67 | | - DSL.jsonValid(expr).valueOf(), |
68 | | - "Expected FALSE when calling jsonValid with " + expr)); |
69 | | - } |
70 | | - |
71 | 26 | @Test |
72 | 27 | public void json_valid_throws_ExpressionEvaluationException() { |
73 | 28 | assertThrows( |
74 | 29 | ExpressionEvaluationException.class, |
75 | 30 | () -> DSL.jsonValid(DSL.literal((ExprValueUtils.booleanValue(true)))).valueOf()); |
76 | 31 | } |
77 | 32 |
|
78 | | - @Test |
79 | | - public void json_valid_returns_true() { |
80 | | - |
81 | | - List<String> validJsonStrings = |
82 | | - List.of( |
83 | | - // test json objects are valid |
84 | | - "{\"a\":\"1\",\"b\":\"2\"}", |
85 | | - "{\"a\":1,\"b\":{\"c\":2,\"d\":3}}", |
86 | | - "{\"arr1\": [1,2,3], \"arr2\": [4,5,6]}", |
87 | | - |
88 | | - // test json arrays are valid |
89 | | - "[1, 2, 3, 4]", |
90 | | - "[{\"a\":1,\"b\":2}, {\"c\":3,\"d\":2}]", |
91 | | - |
92 | | - // test json scalars are valid |
93 | | - "\"abc\"", |
94 | | - "1234", |
95 | | - "12.34", |
96 | | - "true", |
97 | | - "false", |
98 | | - "null", |
99 | | - |
100 | | - // test empty string is valid |
101 | | - ""); |
102 | | - |
103 | | - validJsonStrings.stream() |
104 | | - .forEach( |
105 | | - str -> |
106 | | - assertEquals( |
107 | | - LITERAL_TRUE, |
108 | | - DSL.jsonValid(DSL.literal(str)).valueOf(), |
109 | | - String.format("String %s must be valid json", str))); |
110 | | - } |
111 | | - |
112 | | - @Test |
113 | | - void json_returnsJsonObject() { |
114 | | - FunctionExpression exp; |
115 | | - |
116 | | - // Setup |
117 | | - final String objectJson = |
118 | | - "{\"foo\": \"foo\", \"fuzz\": true, \"bar\": 1234, \"bar2\": 12.34, \"baz\": null, " |
119 | | - + "\"obj\": {\"internal\": \"value\"}, \"arr\": [\"string\", true, null]}"; |
120 | | - |
121 | | - LinkedHashMap<String, ExprValue> objectMap = new LinkedHashMap<>(); |
122 | | - objectMap.put("foo", new ExprStringValue("foo")); |
123 | | - objectMap.put("fuzz", ExprBooleanValue.of(true)); |
124 | | - objectMap.put("bar", new ExprLongValue(1234)); |
125 | | - objectMap.put("bar2", new ExprDoubleValue(12.34)); |
126 | | - objectMap.put("baz", ExprNullValue.of()); |
127 | | - objectMap.put( |
128 | | - "obj", ExprTupleValue.fromExprValueMap(Map.of("internal", new ExprStringValue("value")))); |
129 | | - objectMap.put( |
130 | | - "arr", |
131 | | - new ExprCollectionValue( |
132 | | - List.of(new ExprStringValue("string"), ExprBooleanValue.of(true), ExprNullValue.of()))); |
133 | | - ExprValue expectedTupleExpr = ExprTupleValue.fromExprValueMap(objectMap); |
134 | | - |
135 | | - // exercise |
136 | | - exp = DSL.stringToJson(DSL.literal(objectJson)); |
137 | | - |
138 | | - // Verify |
139 | | - var value = exp.valueOf(); |
140 | | - assertTrue(value instanceof ExprTupleValue); |
141 | | - assertEquals(expectedTupleExpr, value); |
142 | | - |
143 | | - // also test the empty object case |
144 | | - assertEquals( |
145 | | - ExprTupleValue.fromExprValueMap(Map.of()), DSL.stringToJson(DSL.literal("{}")).valueOf()); |
146 | | - } |
147 | | - |
148 | | - @Test |
149 | | - void json_returnsJsonArray() { |
150 | | - FunctionExpression exp; |
151 | | - |
152 | | - // Setup |
153 | | - final String arrayJson = "[\"foo\", \"fuzz\", true, \"bar\", 1234, 12.34, null]"; |
154 | | - ExprValue expectedArrayExpr = |
155 | | - new ExprCollectionValue( |
156 | | - List.of( |
157 | | - new ExprStringValue("foo"), |
158 | | - new ExprStringValue("fuzz"), |
159 | | - LITERAL_TRUE, |
160 | | - new ExprStringValue("bar"), |
161 | | - new ExprIntegerValue(1234), |
162 | | - new ExprDoubleValue(12.34), |
163 | | - LITERAL_NULL)); |
164 | | - |
165 | | - // exercise |
166 | | - exp = DSL.stringToJson(DSL.literal(arrayJson)); |
167 | | - |
168 | | - // Verify |
169 | | - var value = exp.valueOf(); |
170 | | - assertTrue(value instanceof ExprCollectionValue); |
171 | | - assertEquals(expectedArrayExpr, value); |
172 | | - |
173 | | - // also test the empty-array case |
174 | | - assertEquals(new ExprCollectionValue(List.of()), DSL.stringToJson(DSL.literal("[]")).valueOf()); |
175 | | - } |
176 | | - |
177 | | - @Test |
178 | | - void json_returnsScalar() { |
179 | | - assertEquals( |
180 | | - new ExprStringValue("foobar"), DSL.stringToJson(DSL.literal("\"foobar\"")).valueOf()); |
181 | | - |
182 | | - assertEquals(new ExprIntegerValue(1234), DSL.stringToJson(DSL.literal("1234")).valueOf()); |
183 | | - |
184 | | - assertEquals(new ExprDoubleValue(12.34), DSL.stringToJson(DSL.literal("12.34")).valueOf()); |
185 | | - |
186 | | - assertEquals(LITERAL_TRUE, DSL.stringToJson(DSL.literal("true")).valueOf()); |
187 | | - assertEquals(LITERAL_FALSE, DSL.stringToJson(DSL.literal("false")).valueOf()); |
188 | | - |
189 | | - assertEquals(LITERAL_NULL, DSL.stringToJson(DSL.literal("null")).valueOf()); |
190 | | - |
191 | | - assertEquals(LITERAL_NULL, DSL.stringToJson(DSL.literal(LITERAL_NULL)).valueOf()); |
192 | | - |
193 | | - assertEquals(LITERAL_MISSING, DSL.stringToJson(DSL.literal(LITERAL_MISSING)).valueOf()); |
194 | | - |
195 | | - assertEquals(LITERAL_NULL, DSL.stringToJson(DSL.literal("")).valueOf()); |
196 | | - } |
197 | | - |
198 | 33 | @Test |
199 | 34 | void json_returnsSemanticCheckException() { |
200 | 35 | List<LiteralExpression> expressions = |
|
0 commit comments