-
Notifications
You must be signed in to change notification settings - Fork 999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add test unit case #589
Merged
Merged
add test unit case #589
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 100 additions & 1 deletion
101
collector/src/test/java/com/usthe/collector/util/JsonPathParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,120 @@ | ||
package com.usthe.collector.util; | ||
|
||
import com.usthe.collector.collect.common.cache.CommonCache; | ||
import com.jayway.jsonpath.TypeRef; | ||
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} | ||
*/ | ||
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<Object> tom = JsonPathParser.parseContentWithJsonPath(JSON_ARRAY,"$[0].name"); | ||
assertNotNull(tom); | ||
assertEquals("tom",tom.get(0)); | ||
// get json array map | ||
List<Object> entry = JsonPathParser.parseContentWithJsonPath(JSON_ARRAY,"$[1]"); | ||
assertNotNull(entry); | ||
entry.forEach(e -> { | ||
assertInstanceOf(Map.class, e); | ||
assertEquals("543",((Map)e).get("speed")); | ||
}); | ||
// process object | ||
List<Object> author = JsonPathParser.parseContentWithJsonPath(JSON_OBJECT,"$.store.book[0].author"); | ||
assertNotNull(author); | ||
assertEquals("Nigel Rees",author.get(0)); | ||
// get json object map | ||
List<Object> 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")); | ||
}); | ||
} | ||
|
||
/** | ||
* @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<List<String>> typeStringRef = new TypeRef<List<String>>() {}; | ||
// process array | ||
List<String> tom = JsonPathParser.parseContentWithJsonPath(JSON_ARRAY,"$[0].name",typeStringRef); | ||
assertNotNull(tom); | ||
assertEquals("tom",tom.get(0)); | ||
TypeRef<List<Map>> typeMapRef = new TypeRef<List<Map>>() {}; | ||
// get json array map | ||
List<Map> entry = JsonPathParser.parseContentWithJsonPath(JSON_ARRAY,"$[1]",typeMapRef); | ||
assertNotNull(entry); | ||
entry.forEach(e -> { | ||
assertEquals("543",e.get("speed")); | ||
}); | ||
TypeRef<List<String>> typeStrRef = new TypeRef<List<String>>() {}; | ||
// process object | ||
List<String> author = JsonPathParser.parseContentWithJsonPath(JSON_OBJECT,"$.store.book[0].author",typeStrRef); | ||
assertNotNull(author); | ||
assertEquals("Nigel Rees",author.get(0)); | ||
TypeRef<List<Map>> typeObjMapRef = new TypeRef<List<Map>>() {}; | ||
// get json object map | ||
List<Map> book = JsonPathParser.parseContentWithJsonPath(JSON_OBJECT,"$.store.book[1]",typeObjMapRef); | ||
assertNotNull(book); | ||
book.forEach(e -> { | ||
assertEquals("Sword of Honour",e.get("title")); | ||
}); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! LGTM. Thanks.
Is the
parseContentWithJsonPath2
need Test. Please add@test