Browse Source

Extracted interfaces for Asserter and Reader

pull/1/merge
kalle 14 years ago
parent
commit
dc846258da
  1. 27
      json-assert/src/main/java/com/jayway/jsonassert/JSONAssert.java
  2. 61
      json-assert/src/main/java/com/jayway/jsonassert/JSONAsserter.java
  3. 70
      json-assert/src/main/java/com/jayway/jsonassert/JSONReader.java
  4. 86
      json-assert/src/main/java/com/jayway/jsonassert/impl/JSONAsserterImpl.java
  5. 4
      json-assert/src/main/java/com/jayway/jsonassert/impl/JSONPath.java
  6. 8
      json-assert/src/main/java/com/jayway/jsonassert/impl/JSONPathFragment.java
  7. 35
      json-assert/src/main/java/com/jayway/jsonassert/impl/JSONReaderImpl.java
  8. 16
      json-assert/src/test/java/com/jayway/jsonassert/JSONAsserterTest.java
  9. 48
      json-assert/src/test/java/com/jayway/jsonassert/JSONReaderTest.java

27
json-assert/src/main/java/com/jayway/jsonassert/JSONAssert.java

@ -0,0 +1,27 @@
package com.jayway.jsonassert;
import com.jayway.jsonassert.impl.JSONAsserterImpl;
import java.text.ParseException;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 1/24/11
* Time: 9:31 PM
*/
public class JSONAssert {
/**
* Creates a JSONAsserter instance that can be used to make
* assertions on the provided JSON document or array.
*
* @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 JSONAsserterImpl.with(json);
}
}

61
json-assert/src/main/java/com/jayway/jsonassert/impl/JSONAsserter.java → json-assert/src/main/java/com/jayway/jsonassert/JSONAsserter.java

@ -1,42 +1,21 @@
package com.jayway.jsonassert.impl;
package com.jayway.jsonassert;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import java.text.ParseException;
import static org.hamcrest.Matchers.*;
/**
* User: kalle stenflo
* Date: 1/21/11
* Time: 3:43 PM
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 1/24/11
* Time: 9:22 PM
*/
public class JSONAsserter {
private final JSONReader reader;
public interface JSONAsserter {
/**
* Creates a new instance of a JSONAsserter that can be used to make
* assertions on the provided JSON document or array.
* Gives access to the {@link JSONReader} used to base the assertions on
*
* @param json the JSON document to create a JSONAsserter for
* @return a JSON asserter initialized with the provided document
* @throws ParseException
* @return the underlying reader
*/
public static JSONAsserter with(String json) throws ParseException {
return new JSONAsserter(JSONReader.parse(json));
}
/**
* Instantiates a new JSONAsserter
*
* @param reader initialized with the JSON document to be asserted upon
*/
private JSONAsserter(JSONReader reader) {
this.reader = reader;
}
JSONReader reader();
/**
* Asserts that object specified by path satisfies the condition specified by matcher.
@ -53,10 +32,7 @@ public class JSONAsserter {
* @param <T> the static type accepted by the matcher
* @return this to allow fluent assertion chains
*/
public <T> JSONAsserter assertThat(String path, Matcher<T> matcher) {
MatcherAssert.assertThat((T) reader.get(path), matcher);
return this;
}
<T> JSONAsserter assertThat(String path, Matcher<T> matcher);
/**
* Asserts that object specified by path is equal to the expected value.
@ -67,9 +43,7 @@ public class JSONAsserter {
* @param <T> the static type that should be returned by the path
* @return this to allow fluent assertion chains
*/
public <T> JSONAsserter assertEquals(String path, T expected) {
return assertThat(path, equalTo(expected));
}
<T> JSONAsserter assertEquals(String path, T expected);
/**
* Asserts that object specified by path is null. If it is not, an AssertionError
@ -78,9 +52,7 @@ public class JSONAsserter {
* @param path the json path specifying the value that should be null
* @return this to allow fluent assertion chains
*/
public JSONAsserter assertNull(String path) {
return assertThat(path, nullValue());
}
JSONAsserter assertNull(String path);
/**
* Asserts that object specified by path is NOT null. If it is, an AssertionError
@ -89,9 +61,7 @@ public class JSONAsserter {
* @param path the json path specifying the value that should be NOT null
* @return this to allow fluent assertion chains
*/
public <T> JSONAsserter assertNotNull(String path) {
return assertThat(path, notNullValue());
}
<T> JSONAsserter assertNotNull(String path);
/**
* Syntactic sugar to allow chaining assertions with a separating and() statement
@ -103,8 +73,5 @@ public class JSONAsserter {
*
* @return this to allow fluent assertion chains
*/
public JSONAsserter and() {
return this;
}
JSONAsserter and();
}

70
json-assert/src/main/java/com/jayway/jsonassert/JSONReader.java

@ -0,0 +1,70 @@
package com.jayway.jsonassert;
import java.util.List;
import java.util.Map;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 1/24/11
* Time: 9:29 PM
*/
public interface JSONReader {
/**
*
* @param path
* @return
*/
boolean hasJsonPath(String path);
/**
*
* @param path
* @return
*/
boolean isNull(String path);
/**
*
* @param path
* @return
*/
Object get(String path);
/**
*
* @param path
* @return
*/
String getString(String path);
/**
*
* @param path
* @return
*/
Long getLong(String path);
/**
*
* @param path
* @return
*/
Boolean getBoolean(String path);
/**
*
* @param path
* @param <T>
* @return
*/
<T> List<T> getList(String path);
/**
*
* @param path
* @return
*/
Map getMap(String path);
}

86
json-assert/src/main/java/com/jayway/jsonassert/impl/JSONAsserterImpl.java

@ -0,0 +1,86 @@
package com.jayway.jsonassert.impl;
import com.jayway.jsonassert.JSONAsserter;
import com.jayway.jsonassert.JSONReader;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import java.text.ParseException;
import static org.hamcrest.Matchers.*;
/**
* User: kalle stenflo
* Date: 1/21/11
* Time: 3:43 PM
*/
public class JSONAsserterImpl implements JSONAsserter {
private final JSONReader reader;
/**
* Creates a new instance of a JSONAsserter that can be used to make
* assertions on the provided JSON document or array.
*
* @param json the JSON document to create a JSONAsserter for
* @return a JSON asserter initialized with the provided document
* @throws ParseException
*/
public static JSONAsserter with(String json) throws ParseException {
return new JSONAsserterImpl(JSONReaderImpl.parse(json));
}
/**
* Instantiates a new JSONAsserter
*
* @param reader initialized with the JSON document to be asserted upon
*/
private JSONAsserterImpl(JSONReader reader) {
this.reader = reader;
}
/**
* {@inheritDoc}
*/
public JSONReader reader() {
return reader;
}
/**
* {@inheritDoc}
*/
public <T> JSONAsserter assertThat(String path, Matcher<T> matcher) {
MatcherAssert.assertThat((T) reader.get(path), matcher);
return this;
}
/**
* {@inheritDoc}
*/
public <T> JSONAsserter assertEquals(String path, T expected) {
return assertThat(path, equalTo(expected));
}
/**
* {@inheritDoc}
*/
public JSONAsserter assertNull(String path) {
return assertThat(path, nullValue());
}
/**
* {@inheritDoc}
*/
public <T> JSONAsserter assertNotNull(String path) {
return assertThat(path, notNullValue());
}
/**
* {@inheritDoc}
*/
public JSONAsserter and() {
return this;
}
}

4
json-assert/src/main/java/com/jayway/jsonassert/impl/JSONPath.java

@ -16,11 +16,11 @@ class JSONPath {
this.path = compilePathFragments(path);
}
public boolean hasMoreFragments(){
boolean hasMoreFragments(){
return !path.isEmpty();
}
public JSONPathFragment nextFragment(){
JSONPathFragment nextFragment(){
return path.poll();
}

8
json-assert/src/main/java/com/jayway/jsonassert/impl/JSONPathFragment.java

@ -22,19 +22,19 @@ class JSONPathFragment {
this.fragment = fragment;
}
public String fragment() {
String fragment() {
return fragment;
}
public boolean isArrayIndex() {
boolean isArrayIndex() {
return ARRAY_POSITION_PATTER.matcher(fragment).matches() || GROOVY_POSITION_PATTER.matcher(fragment).matches();
}
public boolean isArrayWildcard() {
boolean isArrayWildcard() {
return ARRAY_WILDCARD_PATTER.matcher(fragment).matches() || GROOVY_WILDCARD_PATTER.matcher(fragment).matches();
}
public int getArrayIndex() {
int getArrayIndex() {
Matcher matcher = ARRAY_POSITION_PATTER.matcher(fragment);
if (matcher.matches()) {
return Integer.parseInt(matcher.group(1));

35
json-assert/src/main/java/com/jayway/jsonassert/impl/JSONReader.java → json-assert/src/main/java/com/jayway/jsonassert/impl/JSONReaderImpl.java

@ -1,6 +1,7 @@
package com.jayway.jsonassert.impl;
import com.jayway.jsonassert.InvalidPathException;
import com.jayway.jsonassert.JSONReader;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
@ -17,7 +18,7 @@ import static org.apache.commons.lang.Validate.notNull;
* Date: 1/20/11
* Time: 4:27 PM
*/
public class JSONReader {
public class JSONReaderImpl implements JSONReader {
private static final JSONParser JSON_PARSER = new JSONParser();
@ -27,12 +28,15 @@ public class JSONReader {
public static JSONReader parse(String jsonDoc) throws java.text.ParseException {
try {
return new JSONReader(JSON_PARSER.parse(jsonDoc));
return new JSONReaderImpl(JSON_PARSER.parse(jsonDoc));
} catch (ParseException e) {
throw new java.text.ParseException(e.getMessage(), e.getPosition());
}
}
/**
* {@inheritDoc}
*/
public boolean hasJsonPath(String path) {
boolean contains = true;
try {
@ -43,30 +47,51 @@ public class JSONReader {
return contains;
}
public boolean isNull(String path){
/**
* {@inheritDoc}
*/
public boolean isNull(String path) {
return (null == get(path));
}
/**
* {@inheritDoc}
*/
public Object get(String path) {
return getByPath(Object.class, path);
}
/**
* {@inheritDoc}
*/
public String getString(String path) {
return getByPath(String.class, path);
}
/**
* {@inheritDoc}
*/
public Long getLong(String path) {
return getByPath(Long.class, path);
}
/**
* {@inheritDoc}
*/
public Boolean getBoolean(String path) {
return getByPath(Boolean.class, path);
}
/**
* {@inheritDoc}
*/
public <T> List<T> getList(String path) {
return getByPath(List.class, path);
}
/**
* {@inheritDoc}
*/
public Map getMap(String path) {
return getByPath(Map.class, path);
}
@ -77,7 +102,7 @@ public class JSONReader {
// private methods
//
//------------------------------------------------------------
private JSONReader(Object root) {
private JSONReaderImpl(Object root) {
notNull(root, "root object can not be null");
this.root = root;
}
@ -130,7 +155,7 @@ public class JSONReader {
} else if (container instanceof JSONObject) {
JSONObject document = getDocument(container);
if(!document.containsKey(field)){
if (!document.containsKey(field)) {
throw new InvalidPathException();
}

16
json-assert/src/test/java/com/jayway/jsonassert/JSONAsserterTest.java

@ -1,6 +1,6 @@
package com.jayway.jsonassert;
import com.jayway.jsonassert.impl.JSONAsserter;
import com.jayway.jsonassert.impl.JSONAsserterImpl;
import org.junit.Test;
import static org.hamcrest.Matchers.*;
@ -18,44 +18,44 @@ public class JSONAsserterTest {
@Test
public void a_path_can_be_asserted_with_matcher() throws Exception {
JSONAsserter.with(TEST_DOCUMENT).assertThat("stringField", equalTo("string-field"));
JSONAsserterImpl.with(TEST_DOCUMENT).assertThat("stringField", equalTo("string-field"));
}
@Test
public void array_content_can_be_asserted_with_matcher() throws Exception {
JSONAsserter.with(TEST_DOCUMENT).assertThat("stringList", hasItems("ONE", "TWO"));
JSONAsserterImpl.with(TEST_DOCUMENT).assertThat("stringList", hasItems("ONE", "TWO"));
}
@Test
public void map_content_can_be_asserted_with_matcher() throws Exception {
JSONAsserter.with(TEST_DOCUMENT).assertThat("subDocument", hasEntry("subField", "sub-field"));
JSONAsserterImpl.with(TEST_DOCUMENT).assertThat("subDocument", hasEntry("subField", "sub-field"));
}
@Test
public void a_path_can_be_asserted_equal_to() throws Exception {
JSONAsserter.with(TEST_DOCUMENT).assertEquals("stringField", "string-field");
JSONAsserterImpl.with(TEST_DOCUMENT).assertEquals("stringField", "string-field");
}
@Test
public void a_path_can_be_asserted_is_null() throws Exception {
JSONAsserter.with(TEST_DOCUMENT).assertNull("nullField");
JSONAsserterImpl.with(TEST_DOCUMENT).assertNull("nullField");
}
@Test(expected = AssertionError.class)
public void failed_assert_throws() throws Exception {
JSONAsserter.with(TEST_DOCUMENT).assertThat("stringField", equalTo("SOME CRAP"));
JSONAsserterImpl.with(TEST_DOCUMENT).assertThat("stringField", equalTo("SOME CRAP"));
}
@Test
public void multiple_asserts_can_be_chained() throws Exception {
JSONAsserter.with(TEST_DOCUMENT)
JSONAsserterImpl.with(TEST_DOCUMENT)
.assertThat("stringField", equalTo("string-field"))
.and()
.assertNull("nullField")

48
json-assert/src/test/java/com/jayway/jsonassert/JSONReaderTest.java

@ -1,6 +1,6 @@
package com.jayway.jsonassert;
import com.jayway.jsonassert.impl.JSONReader;
import com.jayway.jsonassert.impl.JSONReaderImpl;
import org.junit.Test;
import java.text.ParseException;
@ -24,26 +24,26 @@ public class JSONReaderTest {
@Test(expected = ParseException.class)
public void invalid_json_not_accepted() throws Exception {
JSONReader.parse("not json");
JSONReaderImpl.parse("not json");
}
@Test
public void a_string_field_can_be_accessed() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
assertEquals("string-field", reader.getString("stringField"));
}
@Test
public void is_null_returns_true_for_null_fields() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
assertTrue(reader.isNull("nullField"));
}
@Test
public void is_null_returns_false_for_not_null_fields() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
assertFalse(reader.isNull("stringField"));
}
@ -51,42 +51,42 @@ public class JSONReaderTest {
@Test
public void a_long_field_can_be_accessed() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
assertTrue(1234L == reader.getLong("numberField"));
}
@Test
public void a_boolean_field_can_be_accessed() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
assertEquals(true, reader.getBoolean("booleanField"));
}
@Test
public void a_path_can_be_checked_for_existence() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DEEP_PATH_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DEEP_PATH_DOCUMENT);
assertTrue(reader.hasJsonPath("a.b.c.say"));
}
@Test
public void a_path_can_be_checked_for_non_existence() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DEEP_PATH_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DEEP_PATH_DOCUMENT);
assertFalse(reader.hasJsonPath("a.b.c.FOO"));
}
@Test
public void a_string_field_can_be_accessed_in_a_nested_document() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
assertEquals("sub-field", reader.getString("subDocument.subField"));
}
@Test
public void a_list_can_be_accessed_in_a_document() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
List<String> l = reader.<String>getList("stringList");
@ -95,7 +95,7 @@ public class JSONReaderTest {
@Test
public void a_list_can_be_accessed_by_array_index() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
assertEquals("ONE", reader.getString("stringList[0]"));
@ -104,7 +104,7 @@ public class JSONReaderTest {
@Test
public void a_list_can_be_accessed_by_groovy_index() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
assertEquals("ONE", reader.getString("stringList.get(0)"));
@ -113,21 +113,21 @@ public class JSONReaderTest {
@Test
public void a_document_contained_in_a_list_can_be_accessed_by_array_index() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
assertEquals("sub-field-0", reader.getString("objectList[0].subField"));
}
@Test
public void a_document_contained_in_a_list_can_be_accessed_by_groovy_index() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
assertEquals("sub-field-0", reader.getString("objectList.get(0).subField"));
}
@Test
public void an_array_in_an_array_can_be_accessed_by_array_index() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT_ARRAY);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT_ARRAY);
assertEquals("0.0", reader.getString("listList[0][0]"));
assertEquals("0.1", reader.getString("listList[0][1]"));
@ -137,7 +137,7 @@ public class JSONReaderTest {
@Test
public void an_array_in_an_array_can_be_accessed_by_groovy_index() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT_ARRAY);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT_ARRAY);
assertEquals("0.0", reader.getString("listList.get(0).get(0)"));
assertEquals("0.1", reader.getString("listList.get(0).get(1)"));
@ -147,7 +147,7 @@ public class JSONReaderTest {
@Test
public void an_array_with_documents_can_be_accessed_by_index() throws Exception {
JSONReader reader = JSONReader.parse(TEST_ARRAY);
JSONReader reader = JSONReaderImpl.parse(TEST_ARRAY);
assertEquals("name0", reader.getString("[0].name"));
assertEquals("name1", reader.getString("[1].name"));
@ -155,7 +155,7 @@ public class JSONReaderTest {
@Test
public void a_nested_document_can_be_accessed_as_a_map() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
assertEquals("sub-field", reader.getMap("subDocument").get("subField"));
}
@ -163,7 +163,7 @@ public class JSONReaderTest {
@Test
public void every_thing_can_be_fetched_as_object() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
assertEquals(true, reader.get("booleanField"));
assertEquals(1234L, reader.get("numberField"));
@ -172,14 +172,14 @@ public class JSONReaderTest {
@Test
public void a_deep_document_path_can_be_read() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DEEP_PATH_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DEEP_PATH_DOCUMENT);
assertEquals("hello", reader.getString("a.b.c.say"));
}
@Test(expected = InvalidPathException.class)
public void exception_is_thrown_when_field_is_not_found() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
String s = reader.getString("invalidProperty");
@ -190,7 +190,7 @@ public class JSONReaderTest {
@Test
public void array_wildcard_property_extract() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
List<String> l = reader.<String>getList("objectList[*].subField");
@ -211,7 +211,7 @@ public class JSONReaderTest {
@Test
public void list_to_string_returns_json() throws Exception {
JSONReader reader = JSONReader.parse(TEST_DOCUMENT);
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT);
assertEquals("[\"ONE\",\"TWO\"]", reader.getList("stringList").toString());

Loading…
Cancel
Save