public void is_filters_evaluates() throws Exception { final Map<String, Object> check = new HashMap<String, Object>(); check.put("foo", "foo"); check.put("bar", null); assertTrue(filter(where("bar").is(null)).apply(createPredicateContext(check))); assertTrue(filter(where("foo").is("foo")).apply(createPredicateContext(check))); assertFalse(filter(where("foo").is("xxx")).apply(createPredicateContext(check))); assertFalse(filter(where("bar").is("xxx")).apply(createPredicateContext(check))); }
/** * Static factory method to create a Criteria using the provided key * * @param key ads new filed to criteria * @return the criteria builder */ public Criteria and(String key) { checkComplete(); return new Criteria(this.criteriaChain, ValueNode.toValueNode(prefixPath(key))); }
/** * Static factory method to create a Criteria using the provided key * * @param key filed name * @return the new criteria */ public static Criteria where(String key) { return new Criteria(ValueNode.toValueNode(prefixPath(key))); }
@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))); }
@Test public void combine_filter_deep_criteria() { String json = "[\n" + " {\n" + " \"first-name\" : \"John\",\n" + " \"last-name\" : \"Irving\",\n" + " \"address\" : {\"state\" : \"Texas\"}\n" + " },\n" + " {\n" + " \"first-name\" : \"Jock\",\n" + " \"last-name\" : \"Ewing\",\n" + " \"address\" : {\"state\" : \"Texas\"}\n" + " },\n" + " {\n" + " \"first-name\" : \"Jock\",\n" + " \"last-name\" : \"Barnes\",\n" + " \"address\" : {\"state\" : \"Nevada\"}\n" + " } \n" + "]"; Filter filter = filter( where("first-name").is("Jock") .and("address.state").is("Texas")); List<Map<String, Object>> jocksInTexas1 = JsonPath.read(json, "$[?]", filter); List<Map<String, Object>> jocksInTexas2 = JsonPath.read(json, "$[?(@.first-name == 'Jock' && @.address.state == 'Texas')]"); JsonPath.parse(json).json(); assertThat((String)JsonPath.read(jocksInTexas1, "$[0].address.state"), is("Texas")); assertThat((String)JsonPath.read(jocksInTexas1, "$[0].first-name"), is("Jock")); assertThat((String)JsonPath.read(jocksInTexas1, "$[0].last-name"), is("Ewing")); }
@Test public void matches_evals() { Predicate p = new Predicate() { @Override public boolean apply(PredicateContext ctx) { Map<String, Object> t = (Map<String, Object>) ctx.item(); Object o = t.get("int-key"); Integer i = (Integer) o; return i == 1; } }; assertThat(filter(where("string-key").eq("string").and("$").matches(p)).apply(createPredicateContext(json))).isEqualTo(true); }
@Test public void cached_path_with_predicates() { Filter feq = Filter.filter(Criteria.where("category").eq("reference")); Filter fne = Filter.filter(Criteria.where("category").ne("reference")); DocumentContext JsonDoc = JsonPath.parse(JSON_DOCUMENT); List<String> eq = JsonDoc.read("$.store.book[?].category", feq); List<String> ne = JsonDoc.read("$.store.book[?].category", fne); Assertions.assertThat(eq).contains("reference"); Assertions.assertThat(ne).doesNotContain("reference"); }
@Test public void int_gte_evals() { assertThat(filter(where("int-key").gte(10)).apply(createPredicateContext(json))).isEqualTo(false); assertThat(filter(where("int-key").gte(1)).apply(createPredicateContext(json))).isEqualTo(true); assertThat(filter(where("int-key").gte(0)).apply(createPredicateContext(json))).isEqualTo(true); }
@Test public void gt_filters_evaluates() throws Exception { final Map<String, Object> check = new HashMap<String, Object>(); check.put("foo", 12.5D); check.put("foo_null", null); assertTrue(filter(where("foo").gt(12D)).apply(createPredicateContext(check))); assertFalse(filter(where("foo").gt(null)).apply(createPredicateContext(check))); assertFalse(filter(where("foo").gt(20D)).apply(createPredicateContext(check))); assertFalse(filter(where("foo_null").gt(20D)).apply(createPredicateContext(check))); }