|
|
@ -1,3 +1,5 @@ |
|
|
|
|
|
|
|
// CS304 (manually written)
|
|
|
|
|
|
|
|
// Issue link: https://github.com/json-path/JsonPath/issues/356
|
|
|
|
package com.jayway.jsonpath; |
|
|
|
package com.jayway.jsonpath; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
@ -6,68 +8,84 @@ import java.math.BigDecimal; |
|
|
|
import java.util.LinkedHashMap; |
|
|
|
import java.util.LinkedHashMap; |
|
|
|
|
|
|
|
|
|
|
|
import static com.jayway.jsonpath.Criteria.where; |
|
|
|
import static com.jayway.jsonpath.Criteria.where; |
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Test for issue 356 |
|
|
|
* Test for issue 356 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class Issue_356 { |
|
|
|
public class Issue_356 { //NOPMD - suppressed AtLeastOneConstructor //NOPMD - suppressed ClassNamingConventions
|
|
|
|
private final static String json = "{\n" + |
|
|
|
/** |
|
|
|
" \"store\": {\n" + |
|
|
|
* The json data for testing |
|
|
|
" \"book\": [\n" + |
|
|
|
*/ |
|
|
|
" {\n" + |
|
|
|
private static final String JSON = "{\n" |
|
|
|
" \"title\": \"Sayings of the Century\",\n" + |
|
|
|
+ " \"store\": {\n" |
|
|
|
" \"price\": {\n" + |
|
|
|
+ " \"book\": [\n" |
|
|
|
" \"value\": 8.95,\n" + |
|
|
|
+ " {\n" |
|
|
|
" \"currency\": \"usd\"\n" + |
|
|
|
+ " \"title\": \"Sayings of the Century\",\n" |
|
|
|
" }\n" + |
|
|
|
+ " \"price\": {\n" |
|
|
|
" },\n" + |
|
|
|
+ " \"value\": 8.95,\n" |
|
|
|
" {\n" + |
|
|
|
+ " \"currency\": \"usd\"\n" |
|
|
|
" \"title\": \"Sword of Honour\",\n" + |
|
|
|
+ " }\n" |
|
|
|
" \"price\": {\n" + |
|
|
|
+ " },\n" |
|
|
|
" \"value\": 12.99,\n" + |
|
|
|
+ " {\n" |
|
|
|
" \"currency\": \"usd\"\n" + |
|
|
|
+ " \"title\": \"Sword of Honour\",\n" |
|
|
|
" }\n" + |
|
|
|
+ " \"price\": {\n" |
|
|
|
" },\n" + |
|
|
|
+ " \"value\": 12.99,\n" |
|
|
|
" {\n" + |
|
|
|
+ " \"currency\": \"usd\"\n" |
|
|
|
" \"title\": \"Moby Dick\",\n" + |
|
|
|
+ " }\n" |
|
|
|
" \"price\": {\n" + |
|
|
|
+ " },\n" |
|
|
|
" \"value\": 8.99,\n" + |
|
|
|
+ " {\n" |
|
|
|
" \"currency\": \"usd\"\n" + |
|
|
|
+ " \"title\": \"Moby Dick\",\n" |
|
|
|
" }\n" + |
|
|
|
+ " \"price\": {\n" |
|
|
|
" }\n" + |
|
|
|
+ " \"value\": 8.99,\n" |
|
|
|
" ]\n" + |
|
|
|
+ " \"currency\": \"usd\"\n" |
|
|
|
" }\n" + |
|
|
|
+ " }\n" |
|
|
|
"}"; |
|
|
|
+ " }\n" |
|
|
|
|
|
|
|
+ " ]\n" |
|
|
|
|
|
|
|
+ " }\n" |
|
|
|
|
|
|
|
+ "}"; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* The constraint for testing |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private static final double CONSTRAINT = 9; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* test1 for "...price.value" PredicateContext, |
|
|
|
|
|
|
|
* it will return the book with the price value less than 9 |
|
|
|
|
|
|
|
*/ |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void test1(){ |
|
|
|
public void test1() { |
|
|
|
Object ans = JsonPath.parse(json).read("$..book[?]", Filter.filter(where("price.value").matches(new Predicate() { |
|
|
|
final Object ans = JsonPath.parse(JSON).read("$..book[?]", Filter.filter(where("price.value").matches(new Predicate() { //NOPMD - suppressed DataflowAnomalyAnalysis
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public boolean apply(PredicateContext ctx) { |
|
|
|
public boolean apply(final PredicateContext ctx) { |
|
|
|
// some custom logic with expecting value number in context
|
|
|
|
// some custom logic with expecting value number in context
|
|
|
|
return ((BigDecimal)ctx.item()).doubleValue() < 9; |
|
|
|
return ((BigDecimal) ctx.item()).doubleValue() < CONSTRAINT; |
|
|
|
} |
|
|
|
} |
|
|
|
}))); |
|
|
|
}))); |
|
|
|
// System.out.println(ans);
|
|
|
|
assertThat(ans.toString().equals("[{\"title\":\"Sayings of the Century\"," //NOPMD - suppressed LawOfDemeter
|
|
|
|
assert(ans.toString().equals("[{\"title\":\"Sayings of the Century\"," + |
|
|
|
+ "\"price\":{\"value\":8.95,\"currency\":\"usd\"}}," |
|
|
|
"\"price\":{\"value\":8.95,\"currency\":\"usd\"}}," + |
|
|
|
+ "{\"title\":\"Moby Dick\"," |
|
|
|
"{\"title\":\"Moby Dick\"," + |
|
|
|
+ "\"price\":{\"value\":8.99,\"currency\":\"usd\"}}]")).isTrue(); |
|
|
|
"\"price\":{\"value\":8.99,\"currency\":\"usd\"}}]")); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* test2 for "...price" PredicateContext, |
|
|
|
|
|
|
|
* it will return the book with the price value less than 9 |
|
|
|
|
|
|
|
*/ |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void test2(){ |
|
|
|
public void test2() { |
|
|
|
Object ans = JsonPath.parse(json).read("$..book[?]", Filter.filter(where("price").matches(new Predicate() { |
|
|
|
final Object ans = JsonPath.parse(JSON).read("$..book[?]", Filter.filter(where("price").matches(new Predicate() { //NOPMD - suppressed DataflowAnomalyAnalysis
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public boolean apply(PredicateContext ctx) { |
|
|
|
public boolean apply(final PredicateContext ctx) { //NOPMD - suppressed CommentRequired
|
|
|
|
// some custom logic with expecting value number in context
|
|
|
|
// some custom logic with expecting value number in context
|
|
|
|
return ((LinkedHashMap<String, Double>) ctx.item()).get("value") < 9; |
|
|
|
return ((LinkedHashMap<String, Double>) ctx.item()).get("value") < CONSTRAINT; |
|
|
|
} |
|
|
|
} |
|
|
|
}))); |
|
|
|
}))); |
|
|
|
// System.out.println(ans);
|
|
|
|
assertThat(ans.toString().equals("[{\"title\":\"Sayings of the Century\"," //NOPMD - suppressed LawOfDemeter
|
|
|
|
assert(ans.toString().equals("[{\"title\":\"Sayings of the Century\"," + |
|
|
|
+ "\"price\":{\"value\":8.95,\"currency\":\"usd\"}}," |
|
|
|
"\"price\":{\"value\":8.95,\"currency\":\"usd\"}}," + |
|
|
|
+ "{\"title\":\"Moby Dick\"," |
|
|
|
"{\"title\":\"Moby Dick\"," + |
|
|
|
+ "\"price\":{\"value\":8.99,\"currency\":\"usd\"}}]")).isTrue(); |
|
|
|
"\"price\":{\"value\":8.99,\"currency\":\"usd\"}}]")); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|