Browse Source

hasJsonPath("path.to.empty.array") will match, e.g return true

pull/237/head
Patrik Helsing 9 years ago
parent
commit
6823a7de1d
  1. 21
      json-path-assert/README.md
  2. 4
      json-path-assert/src/main/java/com/jayway/jsonpath/matchers/JsonPathMatchers.java
  3. 47
      json-path-assert/src/test/java/com/jayway/jsonpath/matchers/JsonPathMatchersTest.java
  4. 7
      json-path-assert/src/test/java/com/jayway/jsonpath/matchers/WithJsonPathTest.java
  5. 3
      json-path-assert/src/test/resources/books.json

21
json-path-assert/README.md

@ -80,3 +80,24 @@ Use typed matchers for specific JSON representations, if needed
File json = ... File json = ...
assertThat(json, isJsonFile(withJsonPath("$..author"))); assertThat(json, isJsonFile(withJsonPath("$..author")));
---
CAUTION: regarding the use of indefinite paths
When using indefinite path expressions (e.g with wildcards '*'), the result will yield a list. Possibly an _empty_ list if no matching entries were found. If you want to assert that the list will actually contain something, make sure to express this explicitly, e.g checking for the size of the list.
// Given a JSON like this:
{
"items": []
}
// Both of these statements will succeed(!)
assertThat(json, hasJsonPath("$.items[*]"));
assertThat(json, hasJsonPath("$.items[*].name"));
// Make sure to explicitly check for size if you want to catch this scenario as a failure
assertThat(json, hasJsonPath("$.items[*]", hasSize(greaterThan(0))));
// However, checking for the existence of an array works fine, as is
assertThat(json, hasJsonPath("$.not_here[*]"));

4
json-path-assert/src/main/java/com/jayway/jsonpath/matchers/JsonPathMatchers.java

@ -19,7 +19,7 @@ public class JsonPathMatchers {
public static Matcher<? super Object> hasJsonPath(String jsonPath) { public static Matcher<? super Object> hasJsonPath(String jsonPath) {
return describedAs("has json path %0", return describedAs("has json path %0",
hasJsonPath(jsonPath, not(anyOf(nullValue(), empty()))), hasJsonPath(jsonPath, not(nullValue())),
jsonPath); jsonPath);
} }
@ -53,7 +53,7 @@ public class JsonPathMatchers {
public static Matcher<? super ReadContext> withJsonPath(JsonPath jsonPath) { public static Matcher<? super ReadContext> withJsonPath(JsonPath jsonPath) {
return describedAs("with json path %0", return describedAs("with json path %0",
withJsonPath(jsonPath, not(anyOf(nullValue(), empty()))), withJsonPath(jsonPath, not(nullValue())),
jsonPath.getPath()); jsonPath.getPath());
} }

47
json-path-assert/src/test/java/com/jayway/jsonpath/matchers/JsonPathMatchersTest.java

@ -73,16 +73,45 @@ public class JsonPathMatchersTest {
@Test @Test
public void shouldMatchExistingJsonPath() { public void shouldMatchExistingJsonPath() {
assertThat(BOOKS_JSON, hasJsonPath("$.store.name")); assertThat(BOOKS_JSON, hasJsonPath("$.store.name")); // string
assertThat(BOOKS_JSON, hasJsonPath("$.store.book[2].title")); assertThat(BOOKS_JSON, hasJsonPath("$.store.switch")); // boolean
assertThat(BOOKS_JSON, hasJsonPath("$.store.book[*].author")); assertThat(BOOKS_JSON, hasJsonPath("$.expensive")); // number
assertThat(BOOKS_JSON, hasJsonPath("$.store.bicycle")); // object
assertThat(BOOKS_JSON, hasJsonPath("$.store.truck")); // empty object
assertThat(BOOKS_JSON, hasJsonPath("$.store.book")); // non-empty array
assertThat(BOOKS_JSON, hasJsonPath("$.store.book[*]")); // non-empty array
assertThat(BOOKS_JSON, hasJsonPath("$.store.magazine")); // empty array
assertThat(BOOKS_JSON, hasJsonPath("$.store.magazine[*]")); // empty array
} }
@Test @Test
public void shouldMatchExistingJsonPathAlternative() { public void shouldMatchExistingJsonPathAlternative() {
assertThat(BOOKS_JSON, isJson(withJsonPath("$.store.name"))); assertThat(BOOKS_JSON, isJson(withJsonPath("$.store.name"))); // string
assertThat(BOOKS_JSON, isJson(withJsonPath("$.store.book[2].title"))); assertThat(BOOKS_JSON, isJson(withJsonPath("$.store.switch"))); // boolean
assertThat(BOOKS_JSON, isJson(withJsonPath("$.store.book[*].author"))); assertThat(BOOKS_JSON, isJson(withJsonPath("$.expensive"))); // number
assertThat(BOOKS_JSON, isJson(withJsonPath("$.store.bicycle"))); // object
assertThat(BOOKS_JSON, isJson(withJsonPath("$.store.truck"))); // empty object
assertThat(BOOKS_JSON, isJson(withJsonPath("$.store.book"))); // non-empty array
assertThat(BOOKS_JSON, isJson(withJsonPath("$.store.book[*]"))); // non-empty array
assertThat(BOOKS_JSON, isJson(withJsonPath("$.store.magazine"))); // empty array
assertThat(BOOKS_JSON, isJson(withJsonPath("$.store.magazine[*]"))); // empty array
}
@Test
public void shouldNotMatchNonExistentArrays() {
assertThat(BOOKS_JSON, not(hasJsonPath("$.store.not_here[*]")));
}
@Test
public void willMatchIndefiniteJsonPathsEvaluatedToEmptyLists() {
// This is just a test to demonstrate that wildcard paths
// will always match, regardless of result. This is because
// the evaluation of these expressions will at least return
// an empty list.
String json = "{\"items\": []}";
assertThat(json, hasJsonPath("$.items[*]"));
assertThat(json, hasJsonPath("$.items[*].name"));
assertThat(json, hasJsonPath("$.items[*]", hasSize(0)));
} }
@Test @Test
@ -103,15 +132,17 @@ public class JsonPathMatchersTest {
@Test @Test
public void shouldNotMatchNonExistingJsonPath() { public void shouldNotMatchNonExistingJsonPath() {
assertThat(BOOKS_JSON, not(hasJsonPath("$.not_there"))); assertThat(BOOKS_JSON, not(hasJsonPath("$.not_there")));
assertThat(BOOKS_JSON, not(hasJsonPath("$.store.not_there")));
assertThat(BOOKS_JSON, not(hasJsonPath("$.store.book[1].isbn")));
assertThat(BOOKS_JSON, not(hasJsonPath("$.store.book[5].title"))); assertThat(BOOKS_JSON, not(hasJsonPath("$.store.book[5].title")));
assertThat(BOOKS_JSON, not(hasJsonPath("$.store.book[*].not_there")));
} }
@Test @Test
public void shouldNotMatchNonExistingJsonPathAlternative() { public void shouldNotMatchNonExistingJsonPathAlternative() {
assertThat(BOOKS_JSON, not(isJson(withJsonPath("$.not_there")))); assertThat(BOOKS_JSON, not(isJson(withJsonPath("$.not_there"))));
assertThat(BOOKS_JSON, not(isJson(withJsonPath(("$.store.not_there")))));
assertThat(BOOKS_JSON, not(isJson(withJsonPath(("$.store.book[1].isbn")))));
assertThat(BOOKS_JSON, not(isJson(withJsonPath("$.store.book[5].title")))); assertThat(BOOKS_JSON, not(isJson(withJsonPath("$.store.book[5].title"))));
assertThat(BOOKS_JSON, not(isJson(withJsonPath("$.store.book[*].not_there"))));
} }
@Test @Test

7
json-path-assert/src/test/java/com/jayway/jsonpath/matchers/WithJsonPathTest.java

@ -9,6 +9,7 @@ import org.hamcrest.StringDescription;
import org.junit.Test; import org.junit.Test;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import static com.jayway.jsonpath.JsonPath.compile; import static com.jayway.jsonpath.JsonPath.compile;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.withJsonPath; import static com.jayway.jsonpath.matchers.JsonPathMatchers.withJsonPath;
@ -40,14 +41,14 @@ public class WithJsonPathTest {
public void shouldNotMatchNonExistingJsonPath() { public void shouldNotMatchNonExistingJsonPath() {
assertThat(BOOKS_JSON, not(withJsonPath(compile("$.not_there")))); assertThat(BOOKS_JSON, not(withJsonPath(compile("$.not_there"))));
assertThat(BOOKS_JSON, not(withJsonPath(compile("$.store.book[5].title")))); assertThat(BOOKS_JSON, not(withJsonPath(compile("$.store.book[5].title"))));
assertThat(BOOKS_JSON, not(withJsonPath(compile("$.store.book[*].not_there")))); assertThat(BOOKS_JSON, not(withJsonPath(compile("$.store.book[1].not_there"))));
} }
@Test @Test
public void shouldNotMatchNonExistingStringJsonPath() { public void shouldNotMatchNonExistingStringJsonPath() {
assertThat(BOOKS_JSON, not(withJsonPath("$.not_there"))); assertThat(BOOKS_JSON, not(withJsonPath("$.not_there")));
assertThat(BOOKS_JSON, not(withJsonPath("$.store.book[5].title"))); assertThat(BOOKS_JSON, not(withJsonPath("$.store.book[5].title")));
assertThat(BOOKS_JSON, not(withJsonPath("$.store.book[*].not_there"))); assertThat(BOOKS_JSON, not(withJsonPath("$.store.book[1].not_there")));
} }
@Test @Test
@ -92,7 +93,7 @@ public class WithJsonPathTest {
@Test @Test
public void shouldMatchJsonPathEvaluatedToCollectionValue() { public void shouldMatchJsonPathEvaluatedToCollectionValue() {
assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[*].author"), instanceOf(Collection.class))); assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[*].author"), instanceOf(List.class)));
assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[*].author"), hasSize(4))); assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[*].author"), hasSize(4)));
assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[*].author"), hasItem("Evelyn Waugh"))); assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[*].author"), hasItem("Evelyn Waugh")));
assertThat(BOOKS_JSON, withJsonPath(compile("$..book[2].title"), hasItem("Moby Dick"))); assertThat(BOOKS_JSON, withJsonPath(compile("$..book[2].title"), hasItem("Moby Dick")));

3
json-path-assert/src/test/resources/books.json

@ -1,6 +1,9 @@
{ {
"store": { "store": {
"name": "Little Shop", "name": "Little Shop",
"switch": true,
"magazine": [],
"truck": {},
"book": [ "book": [
{ {
"category": "reference", "category": "reference",

Loading…
Cancel
Save