kalle
14 years ago
16 changed files with 369 additions and 235 deletions
@ -0,0 +1,66 @@
|
||||
package com.jayway.jsonassert; |
||||
|
||||
|
||||
import com.jayway.jsonassert.impl.JsonAsserterImpl; |
||||
import org.json.simple.parser.JSONParser; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.io.InputStreamReader; |
||||
import java.io.Reader; |
||||
import java.text.ParseException; |
||||
|
||||
/** |
||||
* User: kalle stenflo |
||||
* Date: 1/24/11 |
||||
* Time: 9:31 PM |
||||
*/ |
||||
public class JsonAssert { |
||||
|
||||
private static final JSONParser JSON_PARSER = new JSONParser(); |
||||
|
||||
/** |
||||
* Creates a JSONAsserter |
||||
* |
||||
* @param json the JSON document to create a JSONAsserter for |
||||
* @return a JSON asserter initialized with the provided document |
||||
* @throws ParseException when the given JSON could not be parsed |
||||
*/ |
||||
public static JsonAsserter with(String json) throws ParseException { |
||||
try { |
||||
return new JsonAsserterImpl(JSON_PARSER.parse(json)); |
||||
} catch (org.json.simple.parser.ParseException e) { |
||||
throw new ParseException(json, e.getPosition()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Creates a JSONAsserter |
||||
* |
||||
* @param reader the reader of the json document |
||||
* @return a JSON asserter initialized with the provided document |
||||
* @throws ParseException when the given JSON could not be parsed |
||||
*/ |
||||
public static JsonAsserter with(Reader reader) throws ParseException, IOException { |
||||
try { |
||||
return new JsonAsserterImpl(JSON_PARSER.parse(reader)); |
||||
} catch (org.json.simple.parser.ParseException e) { |
||||
throw new ParseException(e.toString(), e.getPosition()); |
||||
} finally { |
||||
reader.close(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Creates a JSONAsserter |
||||
* |
||||
* @param is the input stream |
||||
* @return a JSON asserter initialized with the provided document |
||||
* @throws ParseException when the given JSON could not be parsed |
||||
*/ |
||||
public static JsonAsserter with(InputStream is) throws ParseException, IOException { |
||||
Reader reader = new InputStreamReader(is); |
||||
return with(reader); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,115 @@
|
||||
package com.jayway.jsonassert; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import static com.jayway.jsonassert.JsonAssert.with; |
||||
import static org.hamcrest.Matchers.*; |
||||
|
||||
/** |
||||
* User: kalle stenflo |
||||
* Date: 1/21/11 |
||||
* Time: 4:04 PM |
||||
*/ |
||||
public class JsonAssertTest { |
||||
|
||||
public final static String JSON = |
||||
"{ \"store\": {\n" + |
||||
" \"book\": [ \n" + |
||||
" { \"category\": \"reference\",\n" + |
||||
" \"author\": \"Nigel Rees\",\n" + |
||||
" \"title\": \"Sayings of the Century\",\n" + |
||||
" \"price\": 8.95\n" + |
||||
" },\n" + |
||||
" { \"category\": \"fiction\",\n" + |
||||
" \"author\": \"Evelyn Waugh\",\n" + |
||||
" \"title\": \"Sword of Honour\",\n" + |
||||
" \"price\": 12.99\n" + |
||||
" },\n" + |
||||
" { \"category\": \"fiction\",\n" + |
||||
" \"author\": \"Herman Melville\",\n" + |
||||
" \"title\": \"Moby Dick\",\n" + |
||||
" \"isbn\": \"0-553-21311-3\",\n" + |
||||
" \"price\": 8.99\n" + |
||||
" },\n" + |
||||
" { \"category\": \"fiction\",\n" + |
||||
" \"author\": \"J. R. R. Tolkien\",\n" + |
||||
" \"title\": \"The Lord of the Rings\",\n" + |
||||
" \"isbn\": \"0-395-19395-8\",\n" + |
||||
" \"price\": 22.99\n" + |
||||
" }\n" + |
||||
" ],\n" + |
||||
" \"bicycle\": {\n" + |
||||
" \"color\": \"red\",\n" + |
||||
" \"price\": 19.95\n" + |
||||
" }\n" + |
||||
" }\n" + |
||||
"}"; |
||||
|
||||
@Test |
||||
public void a_path_can_be_asserted_with_matcher() throws Exception { |
||||
|
||||
with(JSON).assertThat("$.store.bicycle.color", equalTo("red")) |
||||
.assertThat("$.store.bicycle.price", equalTo(19.95D)); |
||||
} |
||||
|
||||
@Test |
||||
public void list_content_can_be_asserted_with_matcher() throws Exception { |
||||
|
||||
with(JSON).assertThat("$..book[*].author", hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); |
||||
} |
||||
|
||||
@Test |
||||
public void map_content_can_be_asserted_with_matcher() throws Exception { |
||||
|
||||
with(JSON).assertThat("$.store.book[0]", hasEntry("category", "reference")) |
||||
.assertThat("$.store.book[0]", hasEntry("title", "Sayings of the Century")); |
||||
} |
||||
|
||||
@Test |
||||
public void a_path_can_be_asserted_equal_to() throws Exception { |
||||
|
||||
with(JSON).assertEquals("$.store.book[0].title", "Sayings of the Century") |
||||
.assertThat("$.store.book[0].title", equalTo("Sayings of the Century")); |
||||
} |
||||
|
||||
|
||||
/* |
||||
@Test |
||||
public void a_sub_document_can_asserted_on__by_path() throws Exception { |
||||
JsonAssert.with(TEST_DOCUMENT).assertThat("subDocument.subField", is(equalTo("sub-field"))); |
||||
} |
||||
|
||||
@Test |
||||
public void a_path_can_be_asserted_equal_to() throws Exception { |
||||
|
||||
JsonAssert.with(TEST_DOCUMENT).assertEquals("stringField", "string-field"); |
||||
} |
||||
|
||||
@Test |
||||
public void a_path_can_be_asserted_is_null() throws Exception { |
||||
|
||||
JsonAssert.with(TEST_DOCUMENT).assertNull("nullField"); |
||||
} |
||||
|
||||
@Test(expected = AssertionError.class) |
||||
public void failed_assert_throws() throws Exception { |
||||
|
||||
JsonAssert.with(TEST_DOCUMENT).assertThat("stringField", equalTo("SOME CRAP")); |
||||
} |
||||
|
||||
@Test |
||||
public void multiple_asserts_can_be_chained() throws Exception { |
||||
|
||||
JsonAssert.with(TEST_DOCUMENT) |
||||
.assertThat("stringField", equalTo("string-field")) |
||||
.assertThat("numberField", is(notNullValue())) |
||||
.and() |
||||
.assertNull("nullField") |
||||
.and() |
||||
.assertEquals("stringField", "string-field"); |
||||
|
||||
} |
||||
*/ |
||||
|
||||
|
||||
} |
@ -1,84 +0,0 @@
|
||||
package com.jayway.jsonassert; |
||||
|
||||
import com.jayway.jsonassert.impl.JsonAsserterImpl; |
||||
import com.jayway.jsonassert.impl.JsonPathImpl; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.Reader; |
||||
import java.text.ParseException; |
||||
|
||||
/** |
||||
* User: kalle stenflo |
||||
* Date: 1/24/11 |
||||
* Time: 9:31 PM |
||||
*/ |
||||
public class JsonAssert { |
||||
|
||||
/** |
||||
* Creates a JSONReader |
||||
* |
||||
* @param jsonDoc the json document to read |
||||
* @return a new reader |
||||
* @throws ParseException |
||||
*/ |
||||
public static JsonPath parse(String jsonDoc) throws ParseException { |
||||
return JsonPathImpl.parse(jsonDoc); |
||||
} |
||||
|
||||
/** |
||||
* Creates a JSONReader |
||||
* |
||||
* @param reader he json document to read |
||||
* @return a new reader |
||||
* @throws ParseException document could not pe parsed |
||||
* @throws IOException |
||||
*/ |
||||
public static JsonPath parse(Reader reader) throws ParseException, IOException { |
||||
return parse(reader, false); |
||||
} |
||||
|
||||
/** |
||||
* Creates a JSONReader |
||||
* |
||||
* @param reader he json document to read |
||||
* @return a new reader |
||||
* @throws ParseException document could not pe parsed |
||||
* @throws IOException |
||||
*/ |
||||
public static JsonPath parse(Reader reader, boolean closeReader) throws ParseException, IOException { |
||||
JsonPath jsonReader = null; |
||||
try { |
||||
jsonReader = JsonPathImpl.parse(reader); |
||||
} finally { |
||||
if(closeReader){ |
||||
try { |
||||
reader.close(); |
||||
} catch (IOException ignore) {} |
||||
} |
||||
} |
||||
return jsonReader; |
||||
} |
||||
|
||||
/** |
||||
* Creates a JSONAsserter |
||||
* |
||||
* @param json the JSON document to create a JSONAsserter for |
||||
* @return a JSON asserter initialized with the provided document |
||||
* @throws ParseException when the given JSON could not be parsed |
||||
*/ |
||||
public static JsonAsserter with(String json) throws ParseException { |
||||
return new JsonAsserterImpl(JsonPathImpl.parse(json)); |
||||
} |
||||
|
||||
/** |
||||
* Creates a JSONAsserter |
||||
* |
||||
* @param reader the reader of the json document |
||||
* @return a JSON asserter initialized with the provided document |
||||
* @throws ParseException when the given JSON could not be parsed |
||||
*/ |
||||
public static JsonAsserter with(Reader reader) throws ParseException, IOException { |
||||
return new JsonAsserterImpl(JsonPathImpl.parse(reader)); |
||||
} |
||||
|
||||
} |
@ -1,29 +0,0 @@
|
||||
package com.jayway.jsonpath; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/2/11 |
||||
* Time: 2:33 PM |
||||
*/ |
||||
public class PathItem { |
||||
|
||||
private final String path; |
||||
|
||||
private final Object target; |
||||
|
||||
public PathItem(String path, Object target) { |
||||
this.path = path; |
||||
this.target = target; |
||||
} |
||||
|
||||
public String getPath() { |
||||
return path; |
||||
} |
||||
|
||||
public Object getTarget() { |
||||
return target; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,25 @@
|
||||
package com.jayway; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import javax.script.ScriptEngine; |
||||
import javax.script.ScriptEngineManager; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/3/11 |
||||
* Time: 8:52 PM |
||||
*/ |
||||
public class ScriptTest { |
||||
|
||||
|
||||
@Test |
||||
public void script() throws Exception { |
||||
ScriptEngineManager manager = new ScriptEngineManager(); |
||||
ScriptEngine engine = manager.getEngineByName("js"); |
||||
|
||||
System.out.println(engine.eval("1 === 2")); |
||||
System.out.println(engine.eval("'APA' === 'APA'").getClass()); |
||||
} |
||||
} |
@ -1,72 +0,0 @@
|
||||
package com.jayway.jsonassert; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import static org.hamcrest.Matchers.*; |
||||
|
||||
/** |
||||
* User: kalle stenflo |
||||
* Date: 1/21/11 |
||||
* Time: 4:04 PM |
||||
*/ |
||||
public class JsonAssertTest { |
||||
|
||||
private static String TEST_DOCUMENT = "{ \"nullField\" : null \"stringField\" : \"string-field\" , \"numberField\" : 1234 , \"booleanField\" : true , \"subDocument\" : {\"subField\" : \"sub-field\"} , \"stringList\" : [\"ONE\", \"TWO\"], \"objectList\" : [{\"subField\" : \"sub-field-0\"}, {\"subField\" : \"sub-field-1\"}], \"listList\" : [[\"0.0\", \"0.1\"], [\"1.0\", \"1.1\"]], }"; |
||||
|
||||
|
||||
@Test |
||||
public void a_path_can_be_asserted_with_matcher() throws Exception { |
||||
|
||||
JsonAssert.with(TEST_DOCUMENT).assertThat("stringField", equalTo("string-field")); |
||||
} |
||||
|
||||
@Test |
||||
public void list_content_can_be_asserted_with_matcher() throws Exception { |
||||
|
||||
JsonAssert.with(TEST_DOCUMENT).assertThat("stringList", hasItems("ONE", "TWO")); |
||||
} |
||||
|
||||
@Test |
||||
public void map_content_can_be_asserted_with_matcher() throws Exception { |
||||
|
||||
JsonAssert.with(TEST_DOCUMENT).assertThat("subDocument", hasEntry("subField", "sub-field")); |
||||
} |
||||
|
||||
@Test |
||||
public void a_sub_document_can_asserted_on__by_path() throws Exception { |
||||
JsonAssert.with(TEST_DOCUMENT).assertThat("subDocument.subField", is(equalTo("sub-field"))); |
||||
} |
||||
|
||||
@Test |
||||
public void a_path_can_be_asserted_equal_to() throws Exception { |
||||
|
||||
JsonAssert.with(TEST_DOCUMENT).assertEquals("stringField", "string-field"); |
||||
} |
||||
|
||||
@Test |
||||
public void a_path_can_be_asserted_is_null() throws Exception { |
||||
|
||||
JsonAssert.with(TEST_DOCUMENT).assertNull("nullField"); |
||||
} |
||||
|
||||
@Test(expected = AssertionError.class) |
||||
public void failed_assert_throws() throws Exception { |
||||
|
||||
JsonAssert.with(TEST_DOCUMENT).assertThat("stringField", equalTo("SOME CRAP")); |
||||
} |
||||
|
||||
@Test |
||||
public void multiple_asserts_can_be_chained() throws Exception { |
||||
|
||||
JsonAssert.with(TEST_DOCUMENT) |
||||
.assertThat("stringField", equalTo("string-field")) |
||||
.assertThat("numberField", is(notNullValue())) |
||||
.and() |
||||
.assertNull("nullField") |
||||
.and() |
||||
.assertEquals("stringField", "string-field"); |
||||
|
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,22 @@
|
||||
package com.jayway.jsonpath; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import static org.junit.Assert.assertFalse; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/3/11 |
||||
* Time: 9:58 PM |
||||
*/ |
||||
public class PathUtilTest { |
||||
|
||||
|
||||
@Test |
||||
public void wildcard_is_not_definite() throws Exception { |
||||
assertFalse(PathUtil.isPathDefinite("$..book[0]")); |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue