#2173 - ebean-test: Add assertContains - DbJson.of(entityBean).assertContains(...)

This commit is contained in:
Robin Bygrave committed 2021-02-19 09:29:06 +13:00
1 parent b4ae721998
commit b7da2cbd97
23 files changed
+727 -31

No files matched your search

@@ -1,11 +1,13 @@
package io.ebean.test;
import com.fasterxml.jackson.databind.JsonNode;
import io.ebean.DB;
import org.junit.Test;
import org.test.BSimpleWithGen;
import java.util.List;
import static io.ebean.test.DbJson.readResource;
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,6 +38,37 @@ public class DbJsonTest {
//.withPlaceholder("_")
.replace("id", "whenModified")
.assertContentMatches("/bean/example-list.json");
}
@Test
public void assertContains_pass() {
BSimpleWithGen bean = new BSimpleWithGen("something-contains-me", "YeahNah");
DB.save(bean);
BSimpleWithGen found = DB.find(BSimpleWithGen.class, bean.getId());
DbJson.of(found).assertContainsResource("/bean/contains-minimal.json");
DbJson.of(found).assertContains(readResource("/bean/contains-with-version.json"));
DB.delete(bean);
}
@Test
public void asJson() {
BSimpleWithGen bean = new BSimpleWithGen("other");
DB.save(bean);
String asJson = DbJson.of(bean)
.withPlaceholder("\"*Replaced*\"")
.replace("id")
.asJson();
JsonNode node = Json.readNode(asJson);
assertThat(node.get("id").asText()).isEqualTo("*Replaced*");
assertThat(node.get("name").asText()).isEqualTo("other");
DB.delete(bean);
}
}
@@ -0,0 +1,86 @@
package io.ebean.test;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Test;
import java.util.stream.Stream;
import static io.ebean.test.Json.readNodeFromResource;
import static org.assertj.core.api.Assertions.assertThat;
public class JsonAssertContainsTest {
@Test
public void assertContains_itself() {
JsonNode original = readNodeFromResource("/contains/original.json");
JsonAssertContains.assertContains(original, original);
}
@Test
public void assertContains_subset() {
JsonNode original = readNodeFromResource("/contains/original.json");
JsonNode expected = readNodeFromResource("/contains/original-subset.json");
JsonAssertContains.assertContains(original, expected);
}
@Test
public void testContainsFails() {
JsonNode original = readNodeFromResource("/contains/original.json");
JsonNode expected = readNodeFromResource("/contains/original-subset-modified.json");
try {
JsonAssertContains.assertContains(original, expected);
} catch (AssertionError e) {
String exceptionMessage = e.getMessage();
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'")
.forEach(assertionError -> assertThat(exceptionMessage).contains(assertionError));
}
}
@Test
public void assertContains_checkNull() {
JsonNode original = readNodeFromResource("/contains/check-null-actual.json");
JsonNode expected = readNodeFromResource("/contains/check-null-expected.json");
try {
JsonAssertContains.assertContains(original, expected);
} catch (AssertionError e) {
String exceptionMessage = e.getMessage();
Stream.of("Expected field 'someNull' to be of type 'NULL' but was 'STRING'",
"Expected field 'extra' to be present")
.forEach(assertionError -> assertThat(exceptionMessage).contains(assertionError));
}
}
@Test
public void assertContains_checkType() {
JsonNode original = readNodeFromResource("/contains/check-type-actual.json");
JsonNode expected = readNodeFromResource("/contains/check-type-expected.json");
try {
JsonAssertContains.assertContains(original, expected);
} catch (AssertionError e) {
String exceptionMessage = e.getMessage();
Stream.of("Expected field 'some' to be of type 'NUMBER' but was 'STRING'")
.forEach(assertionError -> assertThat(exceptionMessage).contains(assertionError));
}
}
@Test
public void path_when_empty() {
JsonAssertContains contains = new JsonAssertContains();
assertThat(contains.path()).isEqualTo("");
assertThat(contains.path("a")).isEqualTo("a");
assertThat(contains.path("b")).isEqualTo("b");
}
}
@@ -0,0 +1,75 @@
package io.ebean.test;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Test;
import org.test.PlainBean;
import java.util.List;
import static io.ebean.test.Json.readNodeFromResource;
import static io.ebean.test.Json.readResource;
import static org.assertj.core.api.Assertions.assertThat;
public class JsonTest {
@Test
public void assertContains_subset() {
JsonNode original = readNodeFromResource("/contains/original.json");
JsonNode expected = readNodeFromResource("/contains/original-subset.json");
Json.assertContains(original, expected);
}
@Test
public void readNode() {
JsonNode original = Json.readNode(readResource("/contains/original.json"));
Json.assertContains(original, original);
}
@Test
public void resource_asNode() {
JsonNode original = Json.resource("/contains/original.json").asNode();
JsonNode nonFluid = Json.readNode(readResource("/contains/original.json"));
assertThat(original).isEqualTo(nonFluid);
}
@Test
public void readBean_resource_asBean() {
// traditional style from resource
PlainBean bean1 = Json.readFromResource(PlainBean.class, "/example/plain.json");
assertThat(bean1.id).isEqualTo(42);
assertThat(bean1.name).isEqualTo("foo");
// traditional style given json content
PlainBean bean2 = Json.read(PlainBean.class, readResource("/example/plain.json"));
assertThat(bean2.id).isEqualTo(42);
assertThat(bean2.name).isEqualTo("foo");
// fluid style - resource asBean()
PlainBean bean3 = Json.resource("/example/plain.json").asBean(PlainBean.class);
assertThat(bean3.id).isEqualTo(42);
assertThat(bean3.name).isEqualTo("foo");
}
@Test
public void readList() {
List<PlainBean> list = Json.readList(PlainBean.class, readResource("/example/plain-list.json"));
String asJson = Json.toJsonString(list);
assertThat(list).hasSize(2);
assertThat(list.get(0).name).isEqualTo("foo");
assertThat(list.get(1).name).isEqualTo("bar");
assertThat(asJson).contains("\"name\" : \"foo\"");
}
@Test
public void resourceAsList() {
// fluid style - resource asList()
List<PlainBean> list = Json.resource("/example/plain-list.json").asList(PlainBean.class);
// traditional style
List<PlainBean> list2 = Json.readList(PlainBean.class, readResource("/example/plain-list.json"));
assertThat(list).isEqualTo(list2);
}
}