Handle searching for matching elements in an array

Add unit tests.
Refactor internals to allow reusing assertion method for finding matches in an array.
This commit is contained in:
sebastian-mrozek committed 2021-03-15 12:51:36 +13:00
1 parent ef3980a3ee
commit 4731ee034e
9 files changed
+256 -54

No files matched your search

@@ -1,10 +1,12 @@
package io.ebean.test;
import com.fasterxml.jackson.databind.JsonNode;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import java.util.stream.Stream;
import static io.ebean.test.Json.readNode;
import static io.ebean.test.Json.readNodeFromResource;
import static org.assertj.core.api.Assertions.assertThat;
@@ -31,18 +33,20 @@ public class JsonAssertContainsTest {
JsonNode expected = readNodeFromResource("/contains/original-subset-modified.json");
try {
JsonAssertContains.assertContains(original, expected);
Assertions.fail("Expected an exception to be thrown");
} catch (AssertionError e) {
String exceptionMessage = e.getMessage();
System.out.println(exceptionMessage);
Stream.of("Expected field 'someString1' to be equal to '\"aaaa\"' but was '\"string1\"",
"Expected field 'someValue1' to be equal to '99' but was '1'",
"Expected field 'someArray1[0]' to be of type 'STRING' but was 'NUMBER",
"Expected field 'someArray2[0].value1' to be of type 'ARRAY' but was 'NUMBER'",
"Expected field 'someArray2[0].value2' to be of type 'OBJECT' but was 'STRING'",
"Expected field 'someArray2[0].array1[0]' to be '\"1\"' but was null",
"Expected field 'someArray2[0].object1.val5' to be present",
"Expected field 'someArray2[0].object1.val6' to be present",
"Expected field 'someArray2[0].object2' to be of type 'NULL' but was 'OBJECT'",
"Expected field 'someArray2[0].objectNull' to be of type 'OBJECT' but was 'NULL'")
"Unable to match expected element 'someArray1[0]' in the actual array",
"Expected field 'someObject1.value1' to be of type 'ARRAY' but was 'NUMBER'",
"Expected field 'someObject1.value2' to be of type 'OBJECT' but was 'STRING'",
"Unable to match expected element 'someObject1.array1[0]' in the actual array",
"Expected field 'someObject1.object1.val5' to be present",
"Expected field 'someObject1.object1.val6' to be present",
"Expected field 'someObject1.object2' to be of type 'NULL' but was 'OBJECT'",
"Expected field 'someObject1.objectNull' to be of type 'OBJECT' but was 'NULL'")
.forEach(assertionError -> assertThat(exceptionMessage).contains(assertionError));
}
}
@@ -54,6 +58,7 @@ public class JsonAssertContainsTest {
JsonNode expected = readNodeFromResource("/contains/check-null-expected.json");
try {
JsonAssertContains.assertContains(original, expected);
Assertions.fail("Expected an exception to be thrown");
} catch (AssertionError e) {
String exceptionMessage = e.getMessage();
Stream.of("Expected field 'someNull' to be of type 'NULL' but was 'STRING'",
@@ -68,6 +73,7 @@ public class JsonAssertContainsTest {
JsonNode expected = readNodeFromResource("/contains/check-type-expected.json");
try {
JsonAssertContains.assertContains(original, expected);
Assertions.fail("Expected an exception to be thrown");
} catch (AssertionError e) {
String exceptionMessage = e.getMessage();
Stream.of("Expected field 'some' to be of type 'NUMBER' but was 'STRING'")
@@ -84,12 +90,37 @@ public class JsonAssertContainsTest {
assertThat(contains.path("b")).isEqualTo("b");
}
@Test
public void assertContainsArrayShuffled() {
JsonNode array = readNodeFromResource("/contains/array.json");
JsonNode arrayShuffled = readNodeFromResource("/contains/array-shuffled.json");
JsonAssertContains.assertContains(array, arrayShuffled);
@Test
public void assertContainsNumbersArrayShuffled() {
JsonNode array = readNode("[2, 54, 13, 10]");
JsonNode arrayShuffled = readNode("[2, 13, 10, 54]");
JsonAssertContains.assertContains(arrayShuffled, array);
}
@Test
public void assertContainsObjectsArrayShuffled() {
JsonNode array = readNodeFromResource("/contains/array-objects.json");
JsonNode arrayShuffled = readNodeFromResource("/contains/array-objects-shuffled.json");
JsonAssertContains.assertContains(arrayShuffled, array);
}
@Test
public void assertArrayElementsNotFound() {
JsonNode original = readNodeFromResource("/contains/array-multi-match.json");
JsonNode actual = readNodeFromResource("/contains/array-multi-match-duplicate-props.json");
try {
JsonAssertContains.assertContains(actual, original);
Assertions.fail("Expected an exception to be thrown");
} catch (AssertionError e) {
System.out.println(e);
String exceptionMessage = e.getMessage();
Stream.of("Unable to match expected element '[5]' in the actual array",
"Unable to match expected element '[4]' in the actual array")
.forEach(assertionError -> assertThat(exceptionMessage).contains(assertionError));
}
}
}
@@ -0,0 +1,18 @@
[
{
"b": 2,
"c": 3
},
{
"d": 4,
"e": 5
},
{
"a": 1,
"b": 2,
"c": 3
},
{
"b": 2
}
]
@@ -0,0 +1,20 @@
[
{
"a": 1
},
{
"b": 2
},
{
"c": 3
},
{
"d": 4
},
{
"e": 5
},
{
"f": 6
}
]
@@ -0,0 +1,14 @@
[
{
"id": "tyu",
"c": 3
},
{
"id": "zxy",
"a": 1
},
{
"id": "123",
"b": 2
}
]
@@ -0,0 +1,11 @@
[
{
"a": 1
},
{
"b": 2
},
{
"c": 3
}
]
@@ -1 +0,0 @@
[2, 54, 13, 10]
@@ -1 +0,0 @@
[10, 2, 13, 54]