@Test public void a_placeholder_criteria_can_be_parsed() { Predicate p = new Predicate() { @Override public boolean apply(PredicateContext ctx) { return false; } }; assertThat(compile("$[?]", p).toString()).isEqualTo("$[?]"); assertThat(compile("$[?,?]", p, p).toString()).isEqualTo("$[?,?]"); assertThat(compile("$[?,?,?]", p, p, p).toString()).isEqualTo("$[?,?,?]"); }
@Test public void a_bracket_notation_property_token_can_be_compiled() { assertThat(compile("$['prop']").toString()).isEqualTo("$['prop']"); assertThat(compile("$['1prop']").toString()).isEqualTo("$['1prop']"); assertThat(compile("$['@prop']").toString()).isEqualTo("$['@prop']"); assertThat(compile("$[ '@prop' ]").toString()).isEqualTo("$['@prop']"); assertThat(compile("$[\"prop\"]").toString()).isEqualTo("$[\"prop\"]"); }
@Test public void a_function_can_be_compiled() { assertThat(compile("$.aaa.foo()").toString()).isEqualTo("$['aaa'].foo()"); assertThat(compile("$.aaa.foo(5)").toString()).isEqualTo("$['aaa'].foo(...)"); assertThat(compile("$.aaa.foo($.bar)").toString()).isEqualTo("$['aaa'].foo(...)"); assertThat(compile("$.aaa.foo(5,10,15)").toString()).isEqualTo("$['aaa'].foo(...)"); }
@Test(expected = InvalidPathException.class) public void property_must_be_separated_by_commas() { compile("$['aaa'}'bbb']"); } }
@Test public void a_definite_array_item_property_can_be_evaluated() { String result = compile("$.store.book[0].author").evaluate(DOC, DOC, CONF).getValue(); assertThat(result).isEqualTo("Nigel Rees"); }
@Ignore("Backward compatibility <= 2.0.0") @Test(expected = InvalidPathException.class) public void a_path_must_start_with_$_or_at() { compile("x"); }
@Test public void a_document_can_be_scanned_for_wildcard3() { List<String> result = PathCompiler.compile("$.phoneNumbers[0]..*").evaluate(DOCUMENT2, DOCUMENT, Configuration.defaultConfiguration()).getPathList(); assertThat(result).containsOnly( "$['phoneNumbers'][0]['number']", "$['phoneNumbers'][0]['type']"); }
@Test public void a_document_can_be_scanned_for_predicate_match() { List<String> result = PathCompiler.compile("$..[?(@.address.city == 'Stockholm')]").evaluate(DOCUMENT, DOCUMENT, Configuration.defaultConfiguration()).getPathList(); assertThat(result).containsOnly( "$['store']['bicycle']", "$['store']['book'][2]"); }
@Test public void a_document_can_be_scanned_for_existence() { List<String> result = PathCompiler.compile("$..[?(@.isbn)]").evaluate(DOCUMENT, DOCUMENT, Configuration.defaultConfiguration()).getPathList(); assertThat(result).containsOnly( "$['store']['book'][2]"); }
@Test public void a_document_can_be_scanned_for_wildcard2() { List<String> result = PathCompiler.compile("$.store.book[0]..*").evaluate(DOCUMENT, DOCUMENT, Configuration.defaultConfiguration()).getPathList(); assertThat(result).containsOnly( "$['store']['book'][0]['address']", "$['store']['book'][0]['author']", "$['store']['book'][0]['price']", "$['store']['book'][0]['category']", "$['store']['book'][0]['title']", "$['store']['book'][0]['address']['city']", "$['store']['book'][0]['address']['street']"); }
@Test public void a_root_object_can_be_evaluated() { Map<String, Object> result = compile("$").evaluate(DOC, DOC, CONF).getValue(); assertThat(result) .containsKey("store") .hasSize(1); }