@ -7,8 +7,8 @@ import org.junit.BeforeClass;
import org.junit.Test ;
import org.junit.Test ;
import java.io.File ;
import java.io.File ;
import java.nio.file.Paths ;
import java.util.List ;
import java.util.Collection ;
import java.util.Map ;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.* ;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.* ;
import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resource ;
import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resource ;
@ -17,8 +17,6 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat ;
import static org.junit.Assert.assertThat ;
public class JsonPathMatchersTest {
public class JsonPathMatchersTest {
private static final String VALID_JSON = resource ( "example.json" ) ;
private static final String BOOKS_JSON = resource ( "books.json" ) ;
private static final String BOOKS_JSON = resource ( "books.json" ) ;
private static final String INVALID_JSON = "{ invalid-json }" ;
private static final String INVALID_JSON = "{ invalid-json }" ;
private static final File BOOKS_JSON_FILE = resourceAsFile ( "books.json" ) ;
private static final File BOOKS_JSON_FILE = resourceAsFile ( "books.json" ) ;
@ -35,84 +33,164 @@ public class JsonPathMatchersTest {
}
}
@Test
@Test
public void shouldMatchValidJson ( ) {
public void shouldMatchJsonPathToStringValue ( ) {
assertThat ( VALID_JSON , isJson ( ) ) ;
final String json = "{\"name\": \"Jessie\"}" ;
assertThat ( BOOKS_JSON , isJson ( ) ) ;
assertThat ( json , hasJsonPath ( "$.name" ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.name" ) ) ) ;
assertThat ( json , hasJsonPath ( "$.name" , equalTo ( "Jessie" ) ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.name" , equalTo ( "Jessie" ) ) ) ) ;
assertThat ( json , not ( hasJsonPath ( "$.name" , equalTo ( "John" ) ) ) ) ;
assertThat ( json , not ( isJson ( withJsonPath ( "$.name" , equalTo ( "John" ) ) ) ) ) ;
}
}
@Test
@Test
public void shouldNotMatchInvalidJson ( ) {
public void shouldMatchJsonPathToIntegerValue ( ) {
assertThat ( INVALID_JSON , not ( isJson ( ) ) ) ;
final String json = "{\"number\": 10}" ;
assertThat ( new Object ( ) , not ( isJson ( ) ) ) ;
assertThat ( "{}" , not ( isJson ( ) ) ) ;
assertThat ( json , hasJsonPath ( "$.number" ) ) ;
assertThat ( null , not ( isJson ( ) ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.number" ) ) ) ;
assertThat ( json , hasJsonPath ( "$.number" , equalTo ( 10 ) ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.number" , equalTo ( 10 ) ) ) ) ;
assertThat ( json , not ( hasJsonPath ( "$.number" , equalTo ( 3 ) ) ) ) ;
assertThat ( json , not ( isJson ( withJsonPath ( "$.number" , equalTo ( 3 ) ) ) ) ) ;
}
}
@Test
@Test
public void shouldMatchExistingJsonPath ( ) {
public void shouldMatchJsonPathToDoubleValue ( ) {
assertThat ( BOOKS_JSON , hasJsonPath ( "$.store.name" ) ) ;
final String json = "{\"price\": 19.95}" ;
assertThat ( BOOKS_JSON , hasJsonPath ( "$.store.book[2].title" ) ) ;
assertThat ( BOOKS_JSON , hasJsonPath ( "$.store.book[*].author" ) ) ;
assertThat ( json , hasJsonPath ( "$.price" ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.price" ) ) ) ;
assertThat ( json , hasJsonPath ( "$.price" , equalTo ( 19 . 95 ) ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.price" , equalTo ( 19 . 95 ) ) ) ) ;
assertThat ( json , not ( hasJsonPath ( "$.price" , equalTo ( 3 . 3 ) ) ) ) ;
assertThat ( json , not ( isJson ( withJsonPath ( "$.price" , equalTo ( 42 ) ) ) ) ) ;
}
}
@Test
@Test
public void shouldMatchExistingJsonPathAlternative ( ) {
public void shouldMatchJsonPathToBooleanValue ( ) {
assertThat ( BOOKS_JSON , isJson ( withJsonPath ( "$.store.name" ) ) ) ;
final String json = "{\"flag\": false}" ;
assertThat ( BOOKS_JSON , isJson ( withJsonPath ( "$.store.book[2].title" ) ) ) ;
assertThat ( BOOKS_JSON , isJson ( withJsonPath ( "$.store.book[*].author" ) ) ) ;
assertThat ( json , hasJsonPath ( "$.flag" ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.flag" ) ) ) ;
assertThat ( json , hasJsonPath ( "$.flag" , equalTo ( false ) ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.flag" , equalTo ( false ) ) ) ) ;
assertThat ( json , not ( hasJsonPath ( "$.flag" , equalTo ( true ) ) ) ) ;
assertThat ( json , not ( isJson ( withJsonPath ( "$.flag" , equalTo ( true ) ) ) ) ) ;
}
}
@Test
@Test
public void shouldNotMatchInvalidJsonWithPath ( ) {
public void shouldMatchJsonPathToJsonObject ( ) {
assertThat ( INVALID_JSON , not ( hasJsonPath ( "$.path" ) ) ) ;
final String json = "{\"object\": { \"name\":\"Oscar\"}}" ;
assertThat ( new Object ( ) , not ( hasJsonPath ( "$.path" ) ) ) ;
assertThat ( "{}" , not ( hasJsonPath ( "$.path" ) ) ) ;
assertThat ( json , hasJsonPath ( "$.object" ) ) ;
assertThat ( null , not ( hasJsonPath ( "$.path" ) ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.object" ) ) ) ;
assertThat ( json , hasJsonPath ( "$.object" , instanceOf ( Map . class ) ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.object" , instanceOf ( Map . class ) ) ) ) ;
assertThat ( json , not ( hasJsonPath ( "$.object" , instanceOf ( List . class ) ) ) ) ;
assertThat ( json , not ( isJson ( withJsonPath ( "$.object" , instanceOf ( List . class ) ) ) ) ) ;
}
}
@Test
@Test
public void shouldNotMatchInvalidJsonWithPathAndValue ( ) {
public void shouldMatchJsonPathToEmptyJsonObject ( ) {
assertThat ( INVALID_JSON , not ( hasJsonPath ( "$.path" , anything ( ) ) ) ) ;
final String json = "{\"empty_object\": {}}" ;
assertThat ( new Object ( ) , not ( hasJsonPath ( "$.path" , anything ( ) ) ) ) ;
assertThat ( null , not ( hasJsonPath ( "$.message" , anything ( ) ) ) ) ;
assertThat ( json , hasJsonPath ( "$.empty_object" ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.empty_object" ) ) ) ;
assertThat ( json , hasJsonPath ( "$.empty_object" , instanceOf ( Map . class ) ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.empty_object" , instanceOf ( Map . class ) ) ) ) ;
assertThat ( json , not ( hasJsonPath ( "$.empty_object" , instanceOf ( List . class ) ) ) ) ;
assertThat ( json , not ( isJson ( withJsonPath ( "$.empty_object" , instanceOf ( List . class ) ) ) ) ) ;
}
}
@Test
@Test
public void shouldNotMatchNonExistingJsonPath ( ) {
public void shouldMatchJsonPathToJsonArray ( ) {
assertThat ( BOOKS_JSON , not ( hasJsonPath ( "$.not_there" ) ) ) ;
final String json = "{\"list\": [ \"one\",\"two\",\"three\"]}" ;
assertThat ( BOOKS_JSON , not ( hasJsonPath ( "$.store.book[5].title" ) ) ) ;
assertThat ( BOOKS_JSON , not ( hasJsonPath ( "$.store.book[*].not_there" ) ) ) ;
assertThat ( json , hasJsonPath ( "$.list" ) ) ;
assertThat ( json , hasJsonPath ( "$.list[*]" ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.list" ) ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.list[*]" ) ) ) ;
assertThat ( json , hasJsonPath ( "$.list" , contains ( "one" , "two" , "three" ) ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.list" , hasItem ( "two" ) ) ) ) ;
assertThat ( json , not ( hasJsonPath ( "$.list" , hasSize ( 2 ) ) ) ) ;
assertThat ( json , not ( isJson ( withJsonPath ( "$.list" , contains ( "four" ) ) ) ) ) ;
}
}
@Test
@Test
public void shouldNotMatchNonExistingJsonPathAlternative ( ) {
public void shouldMatchJsonPathToEmptyJsonArray ( ) {
assertThat ( BOOKS_JSON , not ( isJson ( withJsonPath ( "$.not_there" ) ) ) ) ;
final String json = "{\"empty_list\": []}" ;
assertThat ( BOOKS_JSON , not ( isJson ( withJsonPath ( "$.store.book[5].title" ) ) ) ) ;
assertThat ( BOOKS_JSON , not ( isJson ( withJsonPath ( "$.store.book[*].not_there" ) ) ) ) ;
assertThat ( json , hasJsonPath ( "$.empty_list" ) ) ;
assertThat ( json , hasJsonPath ( "$.empty_list[*]" ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.empty_list" ) ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.empty_list[*]" ) ) ) ;
assertThat ( json , hasJsonPath ( "$.empty_list" , empty ( ) ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.empty_list" , hasSize ( 0 ) ) ) ) ;
assertThat ( json , not ( hasJsonPath ( "$.empty_list" , hasSize ( 2 ) ) ) ) ;
assertThat ( json , not ( isJson ( withJsonPath ( "$.empty_list" , contains ( "four" ) ) ) ) ) ;
}
}
@Test
@Test
public void shouldMatchJsonPathWithStringValue ( ) {
public void willMatchIndefiniteJsonPathsEvaluatedToEmptyLists ( ) {
assertThat ( BOOKS_JSON , hasJsonPath ( "$.store.name" , equalTo ( "Little Shop" ) ) ) ;
// This is just a test to demonstrate that indefinite paths
assertThat ( BOOKS_JSON , hasJsonPath ( "$.store.book[2].title" , equalTo ( "Moby Dick" ) ) ) ;
// will always match, regardless of result. This is because
// the evaluation of these expressions will return lists,
// even though they may be empty.
String json = "{\"items\": []}" ;
assertThat ( json , hasJsonPath ( "$.items[*].name" ) ) ;
assertThat ( json , hasJsonPath ( "$.items[*]" ) ) ;
assertThat ( json , hasJsonPath ( "$.items[*]" , hasSize ( 0 ) ) ) ;
}
}
@Test
@Test
public void shouldMatchJsonPathWithIntegerValue ( ) {
public void shouldMatchJsonPathToNullValue ( ) {
assertThat ( BOOKS_JSON , hasJsonPath ( "$.expensive" , equalTo ( 10 ) ) ) ;
final String json = "{\"none\": null}" ;
assertThat ( json , hasJsonPath ( "$.none" ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.none" ) ) ) ;
assertThat ( json , hasJsonPath ( "$.none" , nullValue ( ) ) ) ;
assertThat ( json , isJson ( withJsonPath ( "$.none" , nullValue ( ) ) ) ) ;
assertThat ( json , not ( hasJsonPath ( "$.none" , equalTo ( "something" ) ) ) ) ;
assertThat ( json , not ( isJson ( withJsonPath ( "$.none" , empty ( ) ) ) ) ) ;
}
}
@Test
@Test
public void shouldMatchJsonPathWithDoubleValue ( ) {
public void shouldNotMatchNonExistingJsonPath ( ) {
assertThat ( BOOKS_JSON , hasJsonPath ( "$.store.bicycle.price" , equalTo ( 19 . 95 ) ) ) ;
final String json = "{}" ;
assertThat ( json , not ( hasJsonPath ( "$.not_there" ) ) ) ;
assertThat ( json , not ( hasJsonPath ( "$.not_there" , anything ( ) ) ) ) ;
assertThat ( json , not ( hasJsonPath ( "$.not_there[*]" ) ) ) ;
assertThat ( json , not ( hasJsonPath ( "$.not_there[*]" , anything ( ) ) ) ) ;
assertThat ( json , not ( isJson ( withJsonPath ( "$.not_there" ) ) ) ) ;
assertThat ( json , not ( isJson ( withJsonPath ( "$.not_there" , anything ( ) ) ) ) ) ;
assertThat ( json , not ( isJson ( withJsonPath ( "$.not_there[*]" ) ) ) ) ;
assertThat ( json , not ( isJson ( withJsonPath ( "$.not_there[*]" , anything ( ) ) ) ) ) ;
}
}
@Test
@Test
public void shouldMatchJsonPathWithCollectionValue ( ) {
public void shouldNotMatchInvalidJsonWithPath ( ) {
assertThat ( BOOKS_JSON , hasJsonPath ( "$.store.book[*].author" , instanceOf ( Collection . class ) ) ) ;
assertThat ( INVALID_JSON , not ( hasJsonPath ( "$.path" ) ) ) ;
assertThat ( BOOKS_JSON , hasJsonPath ( "$.store.book[*].author" , hasSize ( 4 ) ) ) ;
assertThat ( new Object ( ) , not ( hasJsonPath ( "$.path" ) ) ) ;
assertThat ( BOOKS_JSON , hasJsonPath ( "$.store.book[*].author" , hasItem ( "Evelyn Waugh" ) ) ) ;
assertThat ( null , not ( hasJsonPath ( "$.path" ) ) ) ;
assertThat ( BOOKS_JSON , hasJsonPath ( "$..book[2].title" , hasItem ( "Moby Dick" ) ) ) ;
}
@Test
public void shouldNotMatchInvalidJsonWithPathAndValue ( ) {
assertThat ( INVALID_JSON , not ( hasJsonPath ( "$.path" , anything ( ) ) ) ) ;
assertThat ( new Object ( ) , not ( hasJsonPath ( "$.path" , anything ( ) ) ) ) ;
assertThat ( null , not ( hasJsonPath ( "$.message" , anything ( ) ) ) ) ;
}
}
@Test
@Test
@ -122,7 +200,7 @@ public class JsonPathMatchersTest {
@Test
@Test
public void shouldNotMatchJsonPathOnNonExistingFile ( ) {
public void shouldNotMatchJsonPathOnNonExistingFile ( ) {
File nonExistingFile = Paths . get ( "missing-file" ) . toFile ( ) ;
File nonExistingFile = new File ( "missing-file" ) ;
assertThat ( nonExistingFile , not ( hasJsonPath ( "$..*" , anything ( ) ) ) ) ;
assertThat ( nonExistingFile , not ( hasJsonPath ( "$..*" , anything ( ) ) ) ) ;
}
}
@ -131,14 +209,4 @@ public class JsonPathMatchersTest {
Object json = Configuration . defaultConfiguration ( ) . jsonProvider ( ) . parse ( BOOKS_JSON ) ;
Object json = Configuration . defaultConfiguration ( ) . jsonProvider ( ) . parse ( BOOKS_JSON ) ;
assertThat ( json , hasJsonPath ( "$.store.name" , equalTo ( "Little Shop" ) ) ) ;
assertThat ( json , hasJsonPath ( "$.store.name" , equalTo ( "Little Shop" ) ) ) ;
}
}
@Test
public void shouldMatchMissingJsonPath ( ) {
assertThat ( BOOKS_JSON , hasNoJsonPath ( "$.not_there" ) ) ;
}
@Test
public void shouldNotMatchExistingJsonPath ( ) {
assertThat ( BOOKS_JSON , not ( hasNoJsonPath ( "$.store.name" ) ) ) ;
}
}
}