|
1 | 1 | package sparkles.support.json.resources; |
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.annotation.JsonInclude; |
3 | 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
4 | 5 |
|
5 | 6 | import org.junit.Test; |
6 | 7 |
|
| 8 | +import java.util.UUID; |
| 9 | + |
| 10 | +import sparkles.support.common.ToStringBuilder; |
| 11 | + |
7 | 12 | import static org.assertj.core.api.Assertions.assertThat; |
8 | 13 |
|
9 | 14 | public class JsonResourcesModuleTest { |
10 | 15 |
|
11 | 16 | private final ObjectMapper om = new ObjectMapper() |
12 | | - .registerModule(new JsonResourcesModule()); |
| 17 | + .registerModule(new JsonResourcesModule()) |
| 18 | + .setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); |
13 | 19 |
|
14 | 20 | @Test |
15 | | - public void foo() throws Exception { |
| 21 | + public void embedded_shouldSerialize() throws Exception { |
16 | 22 | Foo foo = new Foo(); |
17 | 23 | foo.related = new Bar("hello world!"); |
18 | 24 |
|
19 | 25 | String serialized = om.writeValueAsString(foo); |
20 | | - //assertThat(serialized).isNull(); |
| 26 | + assertThat(serialized).isEqualTo("{\"_embedded\":{\"related\":{\"name\":\"hello world!\"}}}"); |
21 | 27 |
|
22 | | - Foo deserialized = om.readValue(serialized, Foo.class); |
23 | | - assertThat(deserialized).isNull(); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void embedded_shouldDeserialize() throws Exception { |
| 32 | + Foo deserialized = om.readValue("{\"_embedded\":{\"related\":{\"name\":\"hello world!\"}}}", Foo.class); |
| 33 | + assertThat(deserialized).isNotNull(); |
| 34 | + assertThat(deserialized.related).isNotNull(); |
| 35 | + assertThat(deserialized.related.name).isEqualTo("hello world!"); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void shouldSerializeProperties() throws Exception { |
| 40 | + Foo foo = new Foo(); |
| 41 | + foo.id = UUID.fromString("f6eb1bd7-7611-49b1-ba15-5d1fa59a30a3"); |
| 42 | + |
| 43 | + String serialized = om.writeValueAsString(foo); |
| 44 | + assertThat(serialized).isEqualTo("{\"id\":\"f6eb1bd7-7611-49b1-ba15-5d1fa59a30a3\"}"); |
24 | 45 | } |
25 | 46 |
|
26 | 47 | @Resource |
27 | 48 | private static class Foo { |
28 | 49 |
|
| 50 | + @Links |
| 51 | + public LinkCollection links; |
| 52 | + |
29 | 53 | @Embedded |
30 | 54 | public Bar related; |
31 | 55 |
|
| 56 | + public UUID id; |
| 57 | + |
32 | 58 | @Override |
33 | 59 | public String toString() { |
34 | | - return "Foo[related=" + related.toString() + "]"; |
| 60 | + return new ToStringBuilder(Foo.class) |
| 61 | + .append("id", id) |
| 62 | + .append("related", related) |
| 63 | + .toString(); |
35 | 64 | } |
36 | 65 | } |
37 | 66 |
|
|
0 commit comments