|
|
|
@ -29,21 +29,21 @@ public class JSONReaderTest {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void a_string_field_can_be_accessed() throws Exception { |
|
|
|
|
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT); |
|
|
|
|
|
|
|
|
|
assertEquals("string-field", reader.getString("stringField")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void is_null_returns_true_for_null_fields() throws Exception { |
|
|
|
|
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT); |
|
|
|
|
|
|
|
|
|
assertTrue(reader.isNull("nullField")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void is_null_returns_false_for_not_null_fields() throws Exception { |
|
|
|
|
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.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 = JSONReaderImpl.parse(TEST_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT); |
|
|
|
|
|
|
|
|
|
assertTrue(1234L == reader.getLong("numberField")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void a_boolean_field_can_be_accessed() throws Exception { |
|
|
|
|
JSONReader reader = JSONReaderImpl.parse(TEST_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT); |
|
|
|
|
|
|
|
|
|
assertEquals(true, reader.getBoolean("booleanField")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void a_path_can_be_checked_for_existence() throws Exception { |
|
|
|
|
JSONReader reader = JSONReaderImpl.parse(TEST_DEEP_PATH_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.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 = JSONReaderImpl.parse(TEST_DEEP_PATH_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.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 = JSONReaderImpl.parse(TEST_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.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 = JSONReaderImpl.parse(TEST_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.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 = JSONReaderImpl.parse(TEST_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.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 = JSONReaderImpl.parse(TEST_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.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 = JSONReaderImpl.parse(TEST_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.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 = JSONReaderImpl.parse(TEST_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.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 = JSONReaderImpl.parse(TEST_DOCUMENT_ARRAY); |
|
|
|
|
JSONReader reader = JSONAssert.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 = JSONReaderImpl.parse(TEST_DOCUMENT_ARRAY); |
|
|
|
|
JSONReader reader = JSONAssert.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 = JSONReaderImpl.parse(TEST_ARRAY); |
|
|
|
|
JSONReader reader = JSONAssert.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 = JSONReaderImpl.parse(TEST_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.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 = JSONReaderImpl.parse(TEST_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.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 = JSONReaderImpl.parse(TEST_DEEP_PATH_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.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 = JSONReaderImpl.parse(TEST_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.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 = JSONReaderImpl.parse(TEST_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.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 = JSONReaderImpl.parse(TEST_DOCUMENT); |
|
|
|
|
JSONReader reader = JSONAssert.parse(TEST_DOCUMENT); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals("[\"ONE\",\"TWO\"]", reader.getList("stringList").toString()); |
|
|
|
|