Search in sources :

Example 6 with Filter

use of com.jayway.jsonpath.Filter in project JsonPath by jayway.

the class FilterTest method filters_can_be_combined.

//-------------------------------------------------
//
// Single filter tests
//
//-------------------------------------------------
@Test
public void filters_can_be_combined() throws Exception {
    Map<String, Object> check = new HashMap<String, Object>();
    check.put("string", "foo");
    check.put("string_null", null);
    check.put("int", 10);
    check.put("long", 1L);
    check.put("double", 1.12D);
    Filter shouldMarch = filter(where("string").is("foo").and("int").lt(11));
    Filter shouldNotMarch = filter(where("string").is("foo").and("int").gt(11));
    assertTrue(shouldMarch.apply(createPredicateContext(check)));
    assertFalse(shouldNotMarch.apply(createPredicateContext(check)));
}
Also used : HashMap(java.util.HashMap) Filter(com.jayway.jsonpath.Filter) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 7 with Filter

use of com.jayway.jsonpath.Filter in project JsonPath by jayway.

the class FilterTest method contains_filter_evaluates_on_string.

@Test
public void contains_filter_evaluates_on_string() {
    String json = "{\n" + "\"store\": {\n" + "    \"book\": [\n" + "        {\n" + "            \"category\": \"reference\",\n" + "            \"title\": \"Sayings of the Century\",\n" + "            \"price\": 8.95\n" + "        },\n" + "        {\n" + "            \"category\": \"fiction\",\n" + "            \"title\": \"Sword of Honour\",\n" + "            \"price\": 12.99\n" + "        }\n" + "    ]\n" + "  }\n" + "}";
    Filter filter = filter(where("category").contains("fic"));
    List<String> result = JsonPath.parse(json).read("$.store.book[?].title", filter);
    Assertions.assertThat(result).containsExactly("Sword of Honour");
}
Also used : Filter(com.jayway.jsonpath.Filter) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 8 with Filter

use of com.jayway.jsonpath.Filter in project JsonPath by jayway.

the class IssuesTest method issue_129.

@Test
public void issue_129() throws Exception {
    final Map<String, Integer> match = new HashMap<String, Integer>();
    match.put("a", 1);
    match.put("b", 2);
    Map<String, Integer> noMatch = new HashMap<String, Integer>();
    noMatch.put("a", -1);
    noMatch.put("b", -2);
    Filter orig = filter(where("a").eq(1).and("b").eq(2));
    String filterAsString = orig.toString();
    Filter parsed = Filter.parse(filterAsString);
    assertThat(orig.apply(createPredicateContext(match))).isTrue();
    assertThat(parsed.apply(createPredicateContext(match))).isTrue();
    assertThat(orig.apply(createPredicateContext(noMatch))).isFalse();
    assertThat(parsed.apply(createPredicateContext(noMatch))).isFalse();
}
Also used : HashMap(java.util.HashMap) Filter(com.jayway.jsonpath.Filter) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Aggregations

BaseTest (com.jayway.jsonpath.BaseTest)8 Filter (com.jayway.jsonpath.Filter)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)4 JsonObject (com.google.gson.JsonObject)1 DocumentContext (com.jayway.jsonpath.DocumentContext)1 Predicate (com.jayway.jsonpath.Predicate)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 Map (java.util.Map)1