Add array root asserts

This commit is contained in:
John Ahlroos 2024-11-15 15:25:10 +01:00
parent c3236f3249
commit c77db1dc5d
Signed by: john
GPG Key ID: 258D0F70DB84CD5D
3 changed files with 99 additions and 3 deletions

View File

@ -2,5 +2,47 @@
This library provides helper methods for asserting JSON strings with AssertJ.
Syntax Examples:
```java
// Assert that a root property is equal to a string
JsonAssert.assertThat("myprop").isEqualTo("foo");
// Assert that the traversed path exists and the property is larger than two
JsonAssert.assertThat(JSON).path("a", "b", "c").isEqualTo(2);
// OR
JsonAssert.assertThat(JSON).field("a").field("b").field("c").isEqualTo(2);
// Cast property before asserting
JsonAssert.assertThat(JSON).field("bigdecimal").asBigDecimal().isEqualTo(BigDecimal.valueOf(1234.0));
// Match by regular expression (myprop:"foobar")
JsonAssert.assertThat(JSON).field("myprop").matches("foo.*")
// Assert first property in Object matches
JsonAssert.assertThat(JSON).firstField("lastProp").isEqualTo(1);
// Assert last property in Object matches
JsonAssert.assertThat(JSON).lastField("lastProp").isEqualTo(3);
// Assert nth property in Object matches
JsonAssert.assertThat(JSON).nthField("secondProp").isEqualTo(2);
// Assert root typed array "[1,2,3]"
JsonAssert.assertThat(JSON).asArray(Integer.class).first().isEqualTo(1);
// Assert root array with objects "[{ prop: 1}]"
JsonAssert.assertThat(JSON).asArray().first().field("prop").isEqualTo(1);
// Assert matrix [[1,2],[3,4]]
assertThat(JSON).asArray().last().asArray().first().asInteger().isEqualTo(3);
```
For more examples see the unit tests in [unit tests](src/test/java/com/devsoap/json).
Licensed under [CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0/).

View File

@ -45,6 +45,35 @@ public class JsonAssert extends AbstractAssert<JsonAssert, String> {
return assertThat(object.toString());
}
public JsonArrayAssert<?> asArray() {
try (var stream = new ByteArrayInputStream(actual.getBytes(StandardCharsets.UTF_8))) {
var actualParser = Json.createParser(stream);
actualParser.next(); // START_ARRAY
return typedJsonArray(null, actualParser.getArray());
} catch (IOException e) {
throw failure("Failed to parse root array");
}
}
public <ITEM> ListAssert<ITEM> asArray(Class<ITEM> itemClass) {
try (var stream = new ByteArrayInputStream(actual.getBytes(StandardCharsets.UTF_8))) {
var actualParser = Json.createParser(stream);
actualParser.next(); // START_ARRAY
var array =actualParser.getArray();
return new ListAssert<>(array.getValuesAs(value -> (ITEM) switch (value.getValueType()) {
case ARRAY -> throw failure("Cannot convert ARRAY to %s", itemClass);
case OBJECT -> throw failure("Cannot convert OBJECT to %s", itemClass);
case STRING -> ((JsonString) value).toString();
case NUMBER -> ((JsonNumber) value).intValue();
case TRUE -> Boolean.TRUE;
case FALSE -> Boolean.FALSE;
case NULL -> null;
}));
} catch (IOException e) {
throw failure("Failed to parse root array");
}
}
public JsonValueAssert<?> firstField(String fieldName) {
try (var stream = new ByteArrayInputStream(actual.getBytes(StandardCharsets.UTF_8))) {
var actualParser = Json.createParser(stream);

View File

@ -25,7 +25,7 @@ import static com.devsoap.json.JsonAssert.assertThat;
public class ArrayTraversalAssertionTest {
private static final String JSON = """
{
[{
"array": [
{ "item": 1 },
{ "item": 2 }
@ -49,18 +49,22 @@ public class ArrayTraversalAssertionTest {
[1,2],
[3,4]
]
}
}]
""";
@Test
public void rootArray() {
public void rootFieldArray() {
assertThat(JSON)
.asArray()
.first()
.array("array")
.first()
.field("item")
.isEqualTo(1);
assertThat(JSON)
.asArray()
.first()
.array("array")
.last()
.field("item")
@ -70,6 +74,8 @@ public class ArrayTraversalAssertionTest {
@Test
public void fieldArray() {
assertThat(JSON)
.asArray()
.first()
.field("fieldArray")
.array("array")
.first()
@ -77,6 +83,8 @@ public class ArrayTraversalAssertionTest {
.isEqualTo(3);
assertThat(JSON)
.asArray()
.first()
.field("fieldArray")
.array("array")
.last()
@ -87,20 +95,31 @@ public class ArrayTraversalAssertionTest {
@Test
public void numericArray() {
assertThat(JSON)
.asArray()
.first()
.array("numericArray", Integer.class)
.first()
.isEqualTo(1);
assertThat(JSON)
.asArray()
.first()
.field("fieldArray")
.array("numericArray", Integer.class)
.first()
.isEqualTo(1);
assertThat("[1,2,3]")
.asArray(Integer.class)
.last()
.isEqualTo(3);
}
@Test
public void matrix() {
assertThat(JSON)
.asArray()
.first()
.array("matrix")
.first()
.asArray()
@ -109,6 +128,8 @@ public class ArrayTraversalAssertionTest {
.isEqualTo(1);
assertThat(JSON)
.asArray()
.first()
.array("matrix")
.first()
.asArray()
@ -117,6 +138,8 @@ public class ArrayTraversalAssertionTest {
.isEqualTo(2);
assertThat(JSON)
.asArray()
.first()
.array("matrix")
.last()
.asArray()
@ -125,6 +148,8 @@ public class ArrayTraversalAssertionTest {
.isEqualTo(3);
assertThat(JSON)
.asArray()
.first()
.array("matrix")
.last()
.asArray()