Skip to content
This repository was archived by the owner on Oct 13, 2020. It is now read-only.

Commit 2585843

Browse files
committed
#35 - Add DbJson for easier asserting of JSON content of bean(s)
1 parent b80d2ee commit 2585843

7 files changed

Lines changed: 194 additions & 9 deletions

File tree

pom.xml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,20 @@
2929
<dependency>
3030
<groupId>io.ebean</groupId>
3131
<artifactId>ebean</artifactId>
32-
<version>12.1.3</version>
32+
<version>12.1.6</version>
3333
<scope>provided</scope>
3434
</dependency>
3535

3636
<dependency>
3737
<groupId>io.ebean.test</groupId>
3838
<artifactId>ebean-test-docker</artifactId>
39-
<version>2.11.1</version>
39+
<version>2.11.2</version>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.assertj</groupId>
44+
<artifactId>assertj-core</artifactId>
45+
<version>3.14.0</version>
4046
</dependency>
4147

4248
<!-- Including JAXB for DB Migration generation with Java 11+ -->
@@ -90,13 +96,6 @@
9096
<scope>test</scope>
9197
</dependency>
9298

93-
<dependency>
94-
<groupId>org.assertj</groupId>
95-
<artifactId>assertj-core</artifactId>
96-
<version>3.3.0</version>
97-
<scope>test</scope>
98-
</dependency>
99-
10099
<dependency>
101100
<groupId>junit</groupId>
102101
<artifactId>junit</artifactId>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package io.ebean.test;
2+
3+
import io.ebean.DB;
4+
import io.ebean.migration.util.IOUtils;
5+
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
/**
12+
* Helper for testing to assert that the JSON form of an entity
13+
* or list of entities match a String / typically test resource.
14+
*
15+
* <pre>{@code
16+
*
17+
* DbJson.of(timedEntries)
18+
* .replace("id", "eventTime")
19+
* .assertContentMatches("/assertJson/full-1-timed.json");
20+
*
21+
* }</pre>
22+
*/
23+
public class DbJson {
24+
25+
/**
26+
* Create a PrettyJson object that has the JSON form of the
27+
* entity bean or beans.
28+
*
29+
* <pre>{@code
30+
*
31+
* DbJson.of(timedEntries)
32+
* .replace("id", "eventTime")
33+
* .assertContentMatches("/assertJson/full-1-timed.json");
34+
*
35+
* }</pre>
36+
*/
37+
public static PrettyJson of(Object bean) {
38+
return new PrettyJson(DB.json().toJsonPretty(bean));
39+
}
40+
41+
/**
42+
* Read the content for the given resource path.
43+
*/
44+
public static String readResource(String resourcePath) {
45+
InputStream is = DbJson.class.getResourceAsStream(resourcePath);
46+
try {
47+
return IOUtils.readUtf8(is).trim();
48+
} catch (IOException e) {
49+
throw new IllegalArgumentException(e);
50+
}
51+
}
52+
53+
/**
54+
* Contains the JSON of beans(s).
55+
*/
56+
public static class PrettyJson {
57+
58+
private String placeHolder = "\"*\"";
59+
private String rawJson;
60+
61+
PrettyJson(String rawJson) {
62+
this.rawJson = rawJson;
63+
}
64+
65+
/**
66+
* Set the placeHolder to use when replacing property values.
67+
*/
68+
public PrettyJson withPlaceholder(String placeHolder) {
69+
this.placeHolder = placeHolder;
70+
return this;
71+
}
72+
73+
/**
74+
* Replace the values of the given properties with a placeholder value.
75+
* <p>
76+
* Typically we do this on generated properties such as id and timestamp properties.
77+
* </p>
78+
*/
79+
public PrettyJson replace(String... propertyNames) {
80+
for (String propertyName : propertyNames) {
81+
String placeholder = "\"" + propertyName + "\": " + placeHolder;
82+
rawJson = rawJson.replaceAll("\"" + propertyName + "\": (\\d+)", placeholder);
83+
rawJson = rawJson.replaceAll("\"" + propertyName + "\": \"(.*?)\"", placeholder);
84+
}
85+
return this;
86+
}
87+
88+
/**
89+
* Return the JSON content.
90+
*/
91+
public String asJson() {
92+
return rawJson;
93+
}
94+
95+
/**
96+
* Assert the json matches the content at the given resource path.
97+
*
98+
* <pre>{@code
99+
*
100+
* DbJson.of(timedEntries)
101+
* .replace("id", "eventTime")
102+
* .assertContentMatches("/assertJson/full-1-timed.json");
103+
*
104+
* }</pre>
105+
*/
106+
public void assertContentMatches(String resourcePath) {
107+
assertThat(rawJson).isEqualTo(readResource(resourcePath));
108+
}
109+
}
110+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.ebean.test;
2+
3+
import io.ebean.DB;
4+
import org.junit.Test;
5+
import org.test.BSimpleWithGen;
6+
7+
import java.util.List;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
12+
public class DbJsonTest {
13+
14+
@Test
15+
public void of() {
16+
17+
DB.find(BSimpleWithGen.class).delete();
18+
19+
BSimpleWithGen bean = new BSimpleWithGen("something");
20+
DB.save(bean);
21+
22+
assertThat(bean.getVersion()).isEqualTo(1);
23+
24+
DbJson.of(bean)
25+
.withPlaceholder("_")
26+
.replace("id", "whenModified")
27+
.assertContentMatches("/bean/example-bean.json");
28+
29+
30+
BSimpleWithGen bean2 = new BSimpleWithGen("other");
31+
DB.save(bean2);
32+
33+
final List<BSimpleWithGen> beans = DB.find(BSimpleWithGen.class).findList();
34+
35+
DbJson.of(beans)
36+
//.withPlaceholder("_")
37+
.replace("id", "whenModified")
38+
.assertContentMatches("/bean/example-list.json");
39+
40+
}
41+
}

src/test/java/org/test/BSimpleWithGen.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package org.test;
22

3+
import io.ebean.annotation.WhenModified;
4+
35
import javax.persistence.Entity;
46
import javax.persistence.Id;
57
import javax.persistence.Transient;
8+
import javax.persistence.Version;
9+
import java.time.Instant;
610
import java.util.List;
711
import java.util.Map;
812

@@ -17,6 +21,12 @@ public class BSimpleWithGen {
1721
@Transient
1822
private Map<String, List<String>> someMap;
1923

24+
@WhenModified
25+
private Instant whenModified;
26+
27+
@Version
28+
private long version;
29+
2030
public BSimpleWithGen(String name) {
2131
this.name = name;
2232
}
@@ -45,4 +55,11 @@ public void setSomeMap(Map<String, List<String>> someMap) {
4555
this.someMap = someMap;
4656
}
4757

58+
public Instant getWhenModified() {
59+
return whenModified;
60+
}
61+
62+
public long getVersion() {
63+
return version;
64+
}
4865
}

src/test/java/org/test/ForTestsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ private void doInsert() {
5050
BSimpleWithGen bean = new BSimpleWithGen("doInsert");
5151
DB.save(bean);
5252
}
53+
5354
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"id": _,
3+
"name": "something",
4+
"whenModified": _,
5+
"version": 1
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[ {
2+
"id": "*",
3+
"name": "something",
4+
"whenModified": "*",
5+
"version": 1
6+
}, {
7+
"id": "*",
8+
"name": "other",
9+
"whenModified": "*",
10+
"version": 1
11+
} ]

0 commit comments

Comments
 (0)