1 changed files with 188 additions and 188 deletions
@ -1,188 +1,188 @@ |
|||||||
package com.jayway.jsonassert; |
package com.jayway.jsonassert; |
||||||
|
|
||||||
import org.hamcrest.Matcher; |
import org.hamcrest.Matcher; |
||||||
import org.hamcrest.Matchers; |
import org.hamcrest.Matchers; |
||||||
import org.junit.Before; |
import org.junit.Before; |
||||||
import org.junit.BeforeClass; |
import org.junit.BeforeClass; |
||||||
import org.junit.Test; |
import org.junit.Test; |
||||||
|
|
||||||
import com.jayway.jsonpath.json.JsonElement; |
import com.jayway.jsonpath.json.JsonElement; |
||||||
import com.jayway.jsonpath.json.JsonException; |
import com.jayway.jsonpath.json.JsonException; |
||||||
import com.jayway.jsonpath.json.JsonFactory; |
import com.jayway.jsonpath.json.JsonFactory; |
||||||
|
|
||||||
import java.io.InputStream; |
import java.io.InputStream; |
||||||
|
|
||||||
import static com.jayway.jsonassert.JsonAssert.*; |
import static com.jayway.jsonassert.JsonAssert.*; |
||||||
import static org.hamcrest.Matchers.*; |
import static org.hamcrest.Matchers.*; |
||||||
|
|
||||||
/** |
/** |
||||||
* User: kalle stenflo |
* User: kalle stenflo |
||||||
* Date: 1/21/11 |
* Date: 1/21/11 |
||||||
* Time: 4:04 PM |
* Time: 4:04 PM |
||||||
*/ |
*/ |
||||||
public abstract class JsonAssertTest { |
public abstract class JsonAssertTest { |
||||||
|
|
||||||
public final static String JSON = |
public final static String JSON = |
||||||
"{ \"store\": {\n" + |
"{ \"store\": {\n" + |
||||||
" \"book\": [ \n" + |
" \"book\": [ \n" + |
||||||
" { \"category\": \"reference\",\n" + |
" { \"category\": \"reference\",\n" + |
||||||
" \"author\": \"Nigel Rees\",\n" + |
" \"author\": \"Nigel Rees\",\n" + |
||||||
" \"title\": \"Sayings of the Century\",\n" + |
" \"title\": \"Sayings of the Century\",\n" + |
||||||
" \"price\": 8.95\n" + |
" \"price\": 8.95\n" + |
||||||
" },\n" + |
" },\n" + |
||||||
" { \"category\": \"fiction\",\n" + |
" { \"category\": \"fiction\",\n" + |
||||||
" \"author\": \"Evelyn Waugh\",\n" + |
" \"author\": \"Evelyn Waugh\",\n" + |
||||||
" \"title\": \"Sword of Honour\",\n" + |
" \"title\": \"Sword of Honour\",\n" + |
||||||
" \"price\": 12.99\n" + |
" \"price\": 12.99\n" + |
||||||
" },\n" + |
" },\n" + |
||||||
" { \"category\": \"fiction\",\n" + |
" { \"category\": \"fiction\",\n" + |
||||||
" \"author\": \"Herman Melville\",\n" + |
" \"author\": \"Herman Melville\",\n" + |
||||||
" \"title\": \"Moby Dick\",\n" + |
" \"title\": \"Moby Dick\",\n" + |
||||||
" \"isbn\": \"0-553-21311-3\",\n" + |
" \"isbn\": \"0-553-21311-3\",\n" + |
||||||
" \"price\": 8.99\n" + |
" \"price\": 8.99\n" + |
||||||
" },\n" + |
" },\n" + |
||||||
" { \"category\": \"fiction\",\n" + |
" { \"category\": \"fiction\",\n" + |
||||||
" \"author\": \"J. R. R. Tolkien\",\n" + |
" \"author\": \"J. R. R. Tolkien\",\n" + |
||||||
" \"title\": \"The Lord of the Rings\",\n" + |
" \"title\": \"The Lord of the Rings\",\n" + |
||||||
" \"isbn\": \"0-395-19395-8\",\n" + |
" \"isbn\": \"0-395-19395-8\",\n" + |
||||||
" \"price\": 22.99\n" + |
" \"price\": 22.99\n" + |
||||||
" }\n" + |
" }\n" + |
||||||
" ],\n" + |
" ],\n" + |
||||||
" \"bicycle\": {\n" + |
" \"bicycle\": {\n" + |
||||||
" \"color\": \"red\",\n" + |
" \"color\": \"red\",\n" + |
||||||
" \"price\": 19.95\n," + |
" \"price\": 19.95\n," + |
||||||
" \"nullValue\": null\n" + |
" \"nullValue\": null\n" + |
||||||
" }\n" + |
" }\n" + |
||||||
" }\n" + |
" }\n" + |
||||||
"}"; |
"}"; |
||||||
|
|
||||||
protected JsonFactory factory = null; |
protected JsonFactory factory = null; |
||||||
@Before |
@Before |
||||||
public void init(){ |
public void init(){ |
||||||
init_factory(); |
init_factory(); |
||||||
} |
} |
||||||
protected void init_factory(){ |
protected void init_factory(){ |
||||||
|
|
||||||
|
|
||||||
} |
} |
||||||
|
|
||||||
@Test |
@Test |
||||||
public void links_document() throws Exception { |
public void links_document() throws Exception { |
||||||
|
|
||||||
with(getResourceAsStream("links.json")).assertEquals("count", 2) |
with(getResourceAsStream("links.json")).assertEquals("count", 2) |
||||||
.assertThat("links.gc:this.href", endsWith("?pageNumber=1&pageSize=2")) |
.assertThat("links.gc:this.href", endsWith("?pageNumber=1&pageSize=2")) |
||||||
.assertNotDefined("links.gc:prev") |
.assertNotDefined("links.gc:prev") |
||||||
.assertNotDefined("links.gc:next") |
.assertNotDefined("links.gc:next") |
||||||
.assertThat("rows", collectionWithSize(equalTo(2))); |
.assertThat("rows", collectionWithSize(equalTo(2))); |
||||||
|
|
||||||
} |
} |
||||||
|
|
||||||
|
|
||||||
@Test |
@Test |
||||||
public void a_document_can_be_expected_not_to_contain_a_path() throws Exception { |
public void a_document_can_be_expected_not_to_contain_a_path() throws Exception { |
||||||
with(JSON).assertNotDefined("$.store.bicycle.cool"); |
with(JSON).assertNotDefined("$.store.bicycle.cool"); |
||||||
} |
} |
||||||
|
|
||||||
@Test |
@Test |
||||||
public void a_value_can_asserted_to_be_null() throws Exception { |
public void a_value_can_asserted_to_be_null() throws Exception { |
||||||
with(JSON).assertNull("$.store.bicycle.nullValue"); |
with(JSON).assertNull("$.store.bicycle.nullValue"); |
||||||
} |
} |
||||||
|
|
||||||
@Test |
@Test |
||||||
public void ends_with_evalueates() throws Exception { |
public void ends_with_evalueates() throws Exception { |
||||||
with(JSON).assertThat("$.store.book[0].category", endsWith("nce")); |
with(JSON).assertThat("$.store.book[0].category", endsWith("nce")); |
||||||
} |
} |
||||||
|
|
||||||
@Test |
@Test |
||||||
public void a_path_can_be_asserted_with_matcher() throws Exception { |
public void a_path_can_be_asserted_with_matcher() throws Exception { |
||||||
|
|
||||||
with(JSON).assertThat("$.store.bicycle.color", equalTo("red")) |
with(JSON).assertThat("$.store.bicycle.color", equalTo("red")) |
||||||
.assertThat("$.store.bicycle.price", equalTo(19.95D)); |
.assertThat("$.store.bicycle.price", equalTo(19.95D)); |
||||||
} |
} |
||||||
|
|
||||||
@Test |
@Test |
||||||
public void list_content_can_be_asserted_with_matcher() throws Exception { |
public void list_content_can_be_asserted_with_matcher() throws Exception { |
||||||
|
|
||||||
with(JSON).assertThat("$..book[*].author", hasItems( factory.createJsonPrimitive("Nigel Rees"), factory.createJsonPrimitive("Evelyn Waugh"), factory.createJsonPrimitive("Herman Melville"),factory.createJsonPrimitive( "J. R. R. Tolkien"))); |
with(JSON).assertThat("$..book[*].author", hasItems( factory.createJsonPrimitive("Nigel Rees"), factory.createJsonPrimitive("Evelyn Waugh"), factory.createJsonPrimitive("Herman Melville"),factory.createJsonPrimitive( "J. R. R. Tolkien"))); |
||||||
|
|
||||||
with(JSON).assertThat("$..author.(value)", hasItems( factory.createJsonPrimitive("Nigel Rees"), factory.createJsonPrimitive("Evelyn Waugh"), factory.createJsonPrimitive("Herman Melville"),factory.createJsonPrimitive( "J. R. R. Tolkien"))) |
with(JSON).assertThat("$..author.(value)", hasItems( factory.createJsonPrimitive("Nigel Rees"), factory.createJsonPrimitive("Evelyn Waugh"), factory.createJsonPrimitive("Herman Melville"),factory.createJsonPrimitive( "J. R. R. Tolkien"))) |
||||||
.assertThat("$..author.(value)", is(collectionWithSize(equalTo(4)))); |
.assertThat("$..author.(value)", is(collectionWithSize(equalTo(4)))); |
||||||
} |
} |
||||||
|
|
||||||
@Test |
@Test |
||||||
public void list_content_can_be_asserted_with_nested_matcher() throws Exception { |
public void list_content_can_be_asserted_with_nested_matcher() throws Exception { |
||||||
with(JSON).assertThat("$..book[*]", hasItems(hasEntry("author", factory.createJsonPrimitive("Nigel Rees")), hasEntry("author", factory.createJsonPrimitive("Evelyn Waugh")))); |
with(JSON).assertThat("$..book[*]", hasItems(hasEntry("author", factory.createJsonPrimitive("Nigel Rees")), hasEntry("author", factory.createJsonPrimitive("Evelyn Waugh")))); |
||||||
} |
} |
||||||
|
|
||||||
@Test |
@Test |
||||||
public void map_content_can_be_asserted_with_matcher() throws Exception { |
public void map_content_can_be_asserted_with_matcher() throws Exception { |
||||||
|
|
||||||
with(JSON).assertThat("$.store.book[0]", hasEntry("category", w("reference"))) |
with(JSON).assertThat("$.store.book[0]", hasEntry("category", w("reference"))) |
||||||
.assertThat("$.store.book[0]", hasEntry("title", w("Sayings of the Century"))) |
.assertThat("$.store.book[0]", hasEntry("title", w("Sayings of the Century"))) |
||||||
.and() |
.and() |
||||||
.assertThat("$..book[0]", hasEntry("category", w("reference"))) |
.assertThat("$..book[0]", hasEntry("category", w("reference"))) |
||||||
.and() |
.and() |
||||||
.assertThat("$.store.book[0]", mapContainingKey(equalTo("category"))) |
.assertThat("$.store.book[0]", mapContainingKey(equalTo("category"))) |
||||||
.and() |
.and() |
||||||
.assertThat("$.store.book[0]", mapContainingValue(equalTo( w("reference")))); |
.assertThat("$.store.book[0]", mapContainingValue(equalTo( w("reference")))); |
||||||
|
|
||||||
with(JSON).assertThat("$.['store'].['book'][0]", hasEntry(w("category"), w("reference"))) |
with(JSON).assertThat("$.['store'].['book'][0]", hasEntry(w("category"), w("reference"))) |
||||||
.assertThat("$.['store'].['book'][0]", hasEntry("title", w("Sayings of the Century"))) |
.assertThat("$.['store'].['book'][0]", hasEntry("title", w("Sayings of the Century"))) |
||||||
.and() |
.and() |
||||||
.assertThat("$..['book'][0]", hasEntry(w("category"), w("reference"))) |
.assertThat("$..['book'][0]", hasEntry(w("category"), w("reference"))) |
||||||
.and() |
.and() |
||||||
.assertThat("$.['store'].['book'][0]", mapContainingKey(equalTo(("category")))) |
.assertThat("$.['store'].['book'][0]", mapContainingKey(equalTo(("category")))) |
||||||
.and() |
.and() |
||||||
.assertThat("$.['store'].['book'][0]", mapContainingValue(equalTo(w("reference")))); |
.assertThat("$.['store'].['book'][0]", mapContainingValue(equalTo(w("reference")))); |
||||||
} |
} |
||||||
|
|
||||||
|
|
||||||
private JsonElement w(Object obj) throws JsonException { |
private JsonElement w(Object obj) throws JsonException { |
||||||
return factory.createJsonPrimitive(obj); |
return factory.createJsonPrimitive(obj); |
||||||
|
|
||||||
} |
} |
||||||
private JsonElement[] w(Object ... objs) throws JsonException { |
private JsonElement[] w(Object ... objs) throws JsonException { |
||||||
JsonElement je[]= new JsonElement[objs.length]; |
JsonElement je[]= new JsonElement[objs.length]; |
||||||
for(int i=0;i<objs.length;i++){ |
for(int i=0;i<objs.length;i++){ |
||||||
je[i] = w(objs[i]); |
je[i] = w(objs[i]); |
||||||
} |
} |
||||||
return je; |
return je; |
||||||
} |
} |
||||||
|
|
||||||
|
|
||||||
@Test |
@Test |
||||||
public void an_empty_collection() throws Exception { |
public void an_empty_collection() throws Exception { |
||||||
with(JSON).assertThat("$.store.book[?(@.category = 'x')]", isnull()); |
with(JSON).assertThat("$.store.book[?(@.category = 'x')]", isnull()); |
||||||
} |
} |
||||||
|
|
||||||
@Test |
@Test |
||||||
public void a_path_can_be_asserted_equal_to() throws Exception { |
public void a_path_can_be_asserted_equal_to() throws Exception { |
||||||
|
|
||||||
with(JSON).assertEquals("$.store.book[0].title", "Sayings of the Century") |
with(JSON).assertEquals("$.store.book[0].title", "Sayings of the Century") |
||||||
.assertThat("$.store.book[0].title", equalTo("Sayings of the Century")); |
.assertThat("$.store.book[0].title", equalTo("Sayings of the Century")); |
||||||
|
|
||||||
with(JSON).assertEquals("$['store']['book'][0].['title']", "Shttp://www.theregister.co.uk/public_sector/government/ayings of the Century") |
with(JSON).assertEquals("$['store']['book'][0].['title']", "Shttp://www.theregister.co.uk/public_sector/government/ayings of the Century") |
||||||
.assertThat("$['store'].book[0].title", equalTo("Sayings of the Century")); |
.assertThat("$['store'].book[0].title", equalTo("Sayings of the Century")); |
||||||
} |
} |
||||||
|
|
||||||
@Test |
@Test |
||||||
public void no_hit_returns_null() throws Exception { |
public void no_hit_returns_null() throws Exception { |
||||||
with(JSON).assertThat("$.store.book[1000].title", Matchers.<Object>nullValue()); |
with(JSON).assertThat("$.store.book[1000].title", Matchers.<Object>nullValue()); |
||||||
} |
} |
||||||
|
|
||||||
@Test |
@Test |
||||||
public void invalid_path() throws Exception { |
public void invalid_path() throws Exception { |
||||||
with(JSON).assertThat("$.store.book[*].fooBar.(value)", Matchers.<Object>nullValue()); |
with(JSON).assertThat("$.store.book[*].fooBar.(value)", Matchers.<Object>nullValue()); |
||||||
} |
} |
||||||
|
|
||||||
@Test |
@Test |
||||||
public void path_including_wildcard_path_followed_by_another_path_concatenates_results_to_list() throws Exception { |
public void path_including_wildcard_path_followed_by_another_path_concatenates_results_to_list() throws Exception { |
||||||
with(getResourceAsStream("lotto.json")).assertThat("lotto.winners[*].winnerId", hasItems( factory.createJsonPrimitive(23), factory.createJsonPrimitive(54))); |
with(getResourceAsStream("lotto.json")).assertThat("lotto.winners[*].winnerId", hasItems( factory.createJsonPrimitive(23), factory.createJsonPrimitive(54))); |
||||||
} |
} |
||||||
|
|
||||||
|
|
||||||
private InputStream getResourceAsStream(String resourceName) { |
private InputStream getResourceAsStream(String resourceName) { |
||||||
return getClass().getClassLoader().getResourceAsStream(resourceName); |
return getClass().getClassLoader().getResourceAsStream(resourceName); |
||||||
} |
} |
||||||
|
|
||||||
} |
} |
||||||
|
Loading…
Reference in new issue