mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-12 22:41:35 +00:00
#2173 - ebean-test: Add assertContains - DbJson.of(entityBean).assertContains(...)
This commit is contained in:
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,6 +18,8 @@ public class BSimpleWithGen {
|
||||
|
||||
private String name;
|
||||
|
||||
private String other;
|
||||
|
||||
@Transient
|
||||
private Map<String, List<String>> someMap;
|
||||
|
||||
@@ -31,6 +33,11 @@ public class BSimpleWithGen {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public BSimpleWithGen(String name, String other) {
|
||||
this.name = name;
|
||||
this.other = other;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.test;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class PlainBean {
|
||||
|
||||
public int id;
|
||||
public String name;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
PlainBean plainBean = (PlainBean) o;
|
||||
return id == plainBean.id && name.equals(plainBean.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"name": "something-contains-me",
|
||||
"version": 1
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "something-contains-me",
|
||||
"other": "YeahNah",
|
||||
"version": 1
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"id": _,
|
||||
"name": "something",
|
||||
"other": null,
|
||||
"whenModified": _,
|
||||
"version": 1
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
[ {
|
||||
"id": "*",
|
||||
"name": "something",
|
||||
"other": null,
|
||||
"whenModified": "*",
|
||||
"version": 1
|
||||
}, {
|
||||
"id": "*",
|
||||
"name": "other",
|
||||
"other": null,
|
||||
"whenModified": "*",
|
||||
"version": 1
|
||||
} ]
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"some": "val",
|
||||
"someNull": "actualNotNull",
|
||||
"matchNull": null,
|
||||
"nullObject": {
|
||||
"id": 42
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"some": "val",
|
||||
"someNull": null,
|
||||
"extra": "notThere",
|
||||
"matchNull": null,
|
||||
"nullObject": null
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"some": "val"
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"some": 42
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"someString1": "aaaa",
|
||||
"someString2": "string2",
|
||||
"someValue1": 99,
|
||||
"someValue2": 2,
|
||||
"someArray1": [
|
||||
"a"
|
||||
],
|
||||
"someArray2": [
|
||||
{
|
||||
"value1": [],
|
||||
"value2": {},
|
||||
"array1": [
|
||||
"1"
|
||||
],
|
||||
"object1": {
|
||||
"val5": "a",
|
||||
"val6": 1
|
||||
},
|
||||
"object2": null,
|
||||
"objectNull": {
|
||||
"val": []
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"someString1": "string1",
|
||||
"someValue1": 1,
|
||||
"someArray2": [
|
||||
{
|
||||
"value1": 11,
|
||||
"array1": [],
|
||||
"object2": {
|
||||
"v1": "v1",
|
||||
"v4": []
|
||||
},
|
||||
"objectNull": null
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"someString1": "string1",
|
||||
"someString2": "string2",
|
||||
"someValue1": 1,
|
||||
"someValue2": 2,
|
||||
"someArray1": [
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4
|
||||
],
|
||||
"someArray2": [
|
||||
{
|
||||
"value1": 11,
|
||||
"value2": "22",
|
||||
"array1": [],
|
||||
"object1": {},
|
||||
"object2": {
|
||||
"v1": "v1",
|
||||
"v2": 1,
|
||||
"v3": {},
|
||||
"v4": []
|
||||
},
|
||||
"objectNull": null
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
[
|
||||
{
|
||||
"id": 42,
|
||||
"name": "foo"
|
||||
},
|
||||
{
|
||||
"id": 55,
|
||||
"name": "bar"
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"id": 42,
|
||||
"name": "foo"
|
||||
}
|
||||
Reference in new issue
Block a user