@Test public void and_filter_can_be_serialized() { String filter = filter(where("a").eq(1).and("b").eq(2)).toString(); String parsed = parse("[?(@['a'] == 1 && @['b'] == 2)]").toString(); assertThat(filter).isEqualTo(parsed); }
@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 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(); }
@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 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")); }
@Bean public Predicate jsonPathFilter() { return Filter.filter(Criteria.where("isbn").exists(true).and("category").ne("fiction")); }
@Then("^the mailboxes should not contain \"([^\"]*)\" in \"([^\"]*)\" namespace, with parent mailbox \"([^\"]*)\" of user \"([^\"]*)\"$") public void assertNotMailboxesNames(String mailboxName, String namespace, String parentName, String parentOwner) throws Exception { MailboxId parentMailbox = mainStepdefs.mailboxProbe.getMailboxId(MailboxConstants.USER_NAMESPACE, parentOwner, parentName); assertThat(httpClient.jsonPath.<List<String>>read(ARGUMENTS + ".list[?].parentId", filter(where("name").is(mailboxName).and("namespace.type").is(namespace)))) .doesNotContain(parentMailbox.serialize()); }