-
Notifications
You must be signed in to change notification settings - Fork 124
/
JsonTest.java
153 lines (130 loc) · 5.6 KB
/
JsonTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.stubbornjava.common;
import static org.junit.Assert.assertEquals;
import java.time.LocalDate;
import org.junit.Ignore;
import org.junit.Test;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.stubbornjava.common.Json.JsonException;
public class JsonTest {
/*
* No need to test every method in here since we just delegate.
* Only test that the customizations made are working properly.
*
* When testing writing json we already tested reading so abuse
* that to compare objects so we don;t have to deal with whitespace issues.
*/
@Test
public void jsonSerializingShouldPass() {
// {{start:toJson}}
Message message = new Message(LocalDate.of(2017, 1, 1), "Happy New Year!");
String json = Json.serializer().toString(message);
// {{end:toJson}}
// {{start:fromJson}}
String expectedJson = Resources.asString("json-test/full-message.json");
Message expectedMessage = Json.serializer().fromJson(expectedJson, new TypeReference<Message>() {});
// {{end:fromJson}}
assertEquals(expectedMessage, Json.serializer().fromJson(json, new TypeReference<Message>() {}));
}
@Test
public void parseShouldNotFailOnFullMessage() {
String rawJson = Resources.asString("json-test/full-message.json");
Message message = Json.serializer().fromJson(rawJson, new TypeReference<Message>() {});
assertEquals("Happy New Year!", message.getMessage());
assertEquals(LocalDate.of(2017, 1, 1), message.getDate());
String actualJson = Json.serializer().toString(message);
assertEquals(message, Json.serializer().fromJson(actualJson, new TypeReference<Message>() {}));
}
@Test
public void parseShouldNotFailOnPartialMessage() {
String rawJson = Resources.asString("json-test/partial-message.json");
Message message = Json.serializer().fromJson(rawJson, new TypeReference<Message>() {});
assertEquals("Partial", message.getMessage());
assertEquals(null, message.getDate());
String actualJson = Json.serializer().toString(message);
assertEquals(message, Json.serializer().fromJson(actualJson, new TypeReference<Message>() {}));
}
@Test
public void parseShouldNotFailOnExtraFields() {
// {{start:fromJsonExtraFields}}
String rawJson = Resources.asString("json-test/extra-fields.json");
Message message = Json.serializer().fromJson(rawJson, new TypeReference<Message>() {});
// {{end:fromJsonExtraFields}}
assertEquals("Happy New Year!", message.getMessage());
assertEquals(LocalDate.of(2017, 1, 1), message.getDate());
String actualJson = Json.serializer().toString(message);
assertEquals(message, Json.serializer().fromJson(actualJson, new TypeReference<Message>() {}));
}
@Ignore // apparently this is expected now
@Test(expected=JsonException.class)
public void parseShouldFailOnInvalidType() {
String rawJson = Resources.asString("json-test/invalid-message.json");
Json.serializer().fromJson(rawJson, new TypeReference<Message>() {});
}
/*
* This show cases used Jackson's JsonNode.path() for traversing deep object graphs.
*/
@Test
public void parseShouldNotFailOnJsonNode() {
// {{start:fromJsonNode}}
String rawJson = Resources.asString("json-test/nested.json");
JsonNode node = Json.serializer()
.nodeFromJson(rawJson)
.path("nested1")
.path("nested2")
.path("nested3");
Message message = Json.serializer().fromNode(node, new TypeReference<Message>() {});
// {{end:fromJsonNode}}
assertEquals("Nested!", message.getMessage());
assertEquals(null, message.getDate());
String actualJson = Json.serializer().toString(message);
assertEquals(message, Json.serializer().fromJson(actualJson, new TypeReference<Message>() {}));
}
// {{start:pojo}}
private static class Message {
private final LocalDate date;
private final String message;
public Message(@JsonProperty("date") LocalDate date, @JsonProperty("message") String message) {
super();
this.date = date;
this.message = message;
}
public LocalDate getDate() {
return date;
}
public String getMessage() {
return message;
}
// {{end:pojo}}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((date == null) ? 0 : date.hashCode());
result = prime * result + ((message == null) ? 0 : message.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Message other = (Message) obj;
if (date == null) {
if (other.date != null)
return false;
} else if (!date.equals(other.date))
return false;
if (message == null) {
if (other.message != null)
return false;
} else if (!message.equals(other.message))
return false;
return true;
}
}
}