|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.expression.json; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 10 | +import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_FALSE; |
| 11 | +import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_MISSING; |
| 12 | +import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_NULL; |
| 13 | +import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_TRUE; |
| 14 | + |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 17 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 18 | +import org.opensearch.sql.data.model.ExprValue; |
| 19 | +import org.opensearch.sql.data.model.ExprValueUtils; |
| 20 | +import org.opensearch.sql.exception.ExpressionEvaluationException; |
| 21 | +import org.opensearch.sql.expression.DSL; |
| 22 | +import org.opensearch.sql.expression.FunctionExpression; |
| 23 | + |
| 24 | +@ExtendWith(MockitoExtension.class) |
| 25 | +public class JsonFunctionsTest { |
| 26 | + private static final ExprValue JsonNestedObject = |
| 27 | + ExprValueUtils.stringValue("{\"a\":\"1\",\"b\":{\"c\":\"2\",\"d\":\"3\"}}"); |
| 28 | + private static final ExprValue JsonObject = |
| 29 | + ExprValueUtils.stringValue("{\"a\":\"1\",\"b\":\"2\"}"); |
| 30 | + private static final ExprValue JsonArray = ExprValueUtils.stringValue("[1, 2, 3, 4]"); |
| 31 | + private static final ExprValue JsonScalarString = ExprValueUtils.stringValue("\"abc\""); |
| 32 | + private static final ExprValue JsonEmptyString = ExprValueUtils.stringValue(""); |
| 33 | + private static final ExprValue JsonInvalidObject = |
| 34 | + ExprValueUtils.stringValue("{\"invalid\":\"json\", \"string\"}"); |
| 35 | + private static final ExprValue JsonInvalidScalar = ExprValueUtils.stringValue("abc"); |
| 36 | + |
| 37 | + @Test |
| 38 | + public void json_valid_returns_false() { |
| 39 | + assertEquals(LITERAL_FALSE, execute(JsonInvalidObject)); |
| 40 | + assertEquals(LITERAL_FALSE, execute(JsonInvalidScalar)); |
| 41 | + assertEquals(LITERAL_FALSE, execute(LITERAL_NULL)); |
| 42 | + assertEquals(LITERAL_FALSE, execute(LITERAL_MISSING)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void json_valid_throws_ExpressionEvaluationException() { |
| 47 | + assertThrows( |
| 48 | + ExpressionEvaluationException.class, () -> execute(ExprValueUtils.booleanValue(true))); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void json_valid_returns_true() { |
| 53 | + assertEquals(LITERAL_TRUE, execute(JsonNestedObject)); |
| 54 | + assertEquals(LITERAL_TRUE, execute(JsonObject)); |
| 55 | + assertEquals(LITERAL_TRUE, execute(JsonArray)); |
| 56 | + assertEquals(LITERAL_TRUE, execute(JsonScalarString)); |
| 57 | + assertEquals(LITERAL_TRUE, execute(JsonEmptyString)); |
| 58 | + } |
| 59 | + |
| 60 | + private ExprValue execute(ExprValue jsonString) { |
| 61 | + FunctionExpression exp = DSL.jsonValid(DSL.literal(jsonString)); |
| 62 | + return exp.valueOf(); |
| 63 | + } |
| 64 | +} |
0 commit comments