From eae123249ec3f4c0c20a9c22bb7a7fc34259f424 Mon Sep 17 00:00:00 2001 From: "zhouyoulin12@163.com" Date: Tue, 17 Jan 2023 16:37:46 +0800 Subject: [PATCH 1/2] add test unit case by zhouyoulin --- .../collector/util/JsonPathParserTest.java | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/collector/src/test/java/com/usthe/collector/util/JsonPathParserTest.java b/collector/src/test/java/com/usthe/collector/util/JsonPathParserTest.java index 193182d815a..155054102bf 100644 --- a/collector/src/test/java/com/usthe/collector/util/JsonPathParserTest.java +++ b/collector/src/test/java/com/usthe/collector/util/JsonPathParserTest.java @@ -1,21 +1,125 @@ package com.usthe.collector.util; +import com.jayway.jsonpath.TypeRef; import com.usthe.collector.collect.common.cache.CommonCache; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.List; +import java.util.Map; + import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * Test case for {@link JsonPathParser} + * @author zhouyl + * modified by zhouyl at 2023-01-17 */ class JsonPathParserTest { + private static final String JSON_ARRAY = "[{'name': 'tom', 'speed': '433'},{'name': 'lili', 'speed': '543'}]"; + public final static String JSON_OBJECT = + "{ \"store\": {\n" + + " \"book\": [ \n" + + " { \"category\": \"reference\",\n" + + " \"author\": \"Nigel Rees\",\n" + + " \"title\": \"Sayings of the Century\",\n" + + " \"price\": 8.95\n" + + " },\n" + + " { \"category\": \"fiction\",\n" + + " \"author\": \"Evelyn Waugh\",\n" + + " \"title\": \"Sword of Honour\",\n" + + " \"price\": 12.99\n" + + " },\n" + + " { \"category\": \"fiction\",\n" + + " \"author\": \"Herman Melville\",\n" + + " \"title\": \"Moby Dick\",\n" + + " \"isbn\": \"0-553-21311-3\",\n" + + " \"price\": 8.99\n" + + " },\n" + + " { \"category\": \"fiction\",\n" + + " \"author\": \"J. R. R. Tolkien\",\n" + + " \"title\": \"The Lord of the Rings\",\n" + + " \"isbn\": \"0-395-19395-8\",\n" + + " \"price\": 22.99\n" + + " }\n" + + " ],\n" + + " \"bicycle\": {\n" + + " \"color\": \"red\",\n" + + " \"price\": 19.95\n," + + " \"gears\": [23, 50]\n," + + " \"extra\": {\"x\": 0}\n," + + " \"escape\" : \"Esc\\b\\f\\n\\r\\t\\u002A\",\n" + + " \"nullValue\": null\n" + + " }\n" + + " }\n" + + "}"; @BeforeEach void setUp() { } @Test void parseContentWithJsonPath() { + // process array + List tom = JsonPathParser.parseContentWithJsonPath(JSON_ARRAY,"$[0].name"); + assertNotNull(tom); + assertEquals("tom",tom.get(0)); + // get json array map + List entry = JsonPathParser.parseContentWithJsonPath(JSON_ARRAY,"$[1]"); + assertNotNull(entry); + entry.forEach(e -> { + assertInstanceOf(Map.class, e); + assertEquals("543",((Map)e).get("speed")); + }); + // process object + List author = JsonPathParser.parseContentWithJsonPath(JSON_OBJECT,"$.store.book[0].author"); + assertNotNull(author); + assertEquals("Nigel Rees",author.get(0)); + // get json object map + List book = JsonPathParser.parseContentWithJsonPath(JSON_OBJECT,"$.store.book[1]"); + assertNotNull(book); + book.forEach(e -> { + assertInstanceOf(Map.class, e); + assertEquals("Sword of Honour",((Map)e).get("title")); + }); + } + + /** + * @author zhouyl + * modified by zhouyl at 2023-01-17 + * @throws java.lang.UnsupportedOperationException: Json-smart provider does not support TypeRef! Use a Jackson or Gson based provider + * need provid an provider to support TypeRef,like this: + * final Configuration configuration = Configuration.builder()// + * .jsonProvider(new JacksonJsonProvider(Json.mapper()))// + * .mappingProvider(new JacksonMappingProvider(Json.mapper()))// + * .build(); + */ +// @Test + void parseContentWithJsonPath2() { + TypeRef> typeStringRef = new TypeRef>() {}; + // process array + List tom = JsonPathParser.parseContentWithJsonPath(JSON_ARRAY,"$[0].name",typeStringRef); + assertNotNull(tom); + assertEquals("tom",tom.get(0)); + TypeRef> typeMapRef = new TypeRef>() {}; + // get json array map + List entry = JsonPathParser.parseContentWithJsonPath(JSON_ARRAY,"$[1]",typeMapRef); + assertNotNull(entry); + entry.forEach(e -> { + assertEquals("543",e.get("speed")); + }); + TypeRef> typeStrRef = new TypeRef>() {}; + // process object + List author = JsonPathParser.parseContentWithJsonPath(JSON_OBJECT,"$.store.book[0].author",typeStrRef); + assertNotNull(author); + assertEquals("Nigel Rees",author.get(0)); + TypeRef> typeObjMapRef = new TypeRef>() {}; + // get json object map + List book = JsonPathParser.parseContentWithJsonPath(JSON_OBJECT,"$.store.book[1]",typeObjMapRef); + assertNotNull(book); + book.forEach(e -> { + assertEquals("Sword of Honour",e.get("title")); + }); } } \ No newline at end of file From 4e13ef1f5020535a746d0f8ecfb5689e66590e2d Mon Sep 17 00:00:00 2001 From: "zhouyoulin12@163.com" Date: Tue, 17 Jan 2023 17:45:24 +0800 Subject: [PATCH 2/2] add test unit case --- .../java/com/usthe/collector/util/JsonPathParserTest.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/collector/src/test/java/com/usthe/collector/util/JsonPathParserTest.java b/collector/src/test/java/com/usthe/collector/util/JsonPathParserTest.java index 155054102bf..8976a41a55a 100644 --- a/collector/src/test/java/com/usthe/collector/util/JsonPathParserTest.java +++ b/collector/src/test/java/com/usthe/collector/util/JsonPathParserTest.java @@ -1,7 +1,6 @@ package com.usthe.collector.util; import com.jayway.jsonpath.TypeRef; -import com.usthe.collector.collect.common.cache.CommonCache; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -13,8 +12,6 @@ /** * Test case for {@link JsonPathParser} - * @author zhouyl - * modified by zhouyl at 2023-01-17 */ class JsonPathParserTest { @@ -86,8 +83,6 @@ void parseContentWithJsonPath() { } /** - * @author zhouyl - * modified by zhouyl at 2023-01-17 * @throws java.lang.UnsupportedOperationException: Json-smart provider does not support TypeRef! Use a Jackson or Gson based provider * need provid an provider to support TypeRef,like this: * final Configuration configuration = Configuration.builder()//