-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathJsonNullableSimpleTest.java
More file actions
254 lines (223 loc) · 10.8 KB
/
Copy pathJsonNullableSimpleTest.java
File metadata and controls
254 lines (223 loc) · 10.8 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package org.openapitools.jackson.nullable;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*;
final class JsonNullableSimpleTest {
static Stream<JsonProcessor> jsonProcessors() {
return Stream.of(
new Jackson2Processor().mapperWithModule(),
new Jackson3Processor().mapperWithModule()
);
}
@Test
void get() {
JsonNullable<String> test = JsonNullable.of("hello");
assertTrue(test.isPresent());
assertEquals("hello", test.get());
}
@Test
void getMissing() {
JsonNullable<String> test = JsonNullable.undefined();
assertFalse(test.isPresent());
try {
test.get();
fail("should have thrown exception");
} catch (Exception e) {
assertTrue(e instanceof NoSuchElementException);
}
}
@Test
void orElse() {
JsonNullable<String> test = JsonNullable.of("hello");
assertTrue(test.isPresent());
assertEquals("hello", test.orElse("world"));
}
@Test
void orElseMissing() {
JsonNullable<String> test = JsonNullable.undefined();
assertFalse(test.isPresent());
assertEquals("world", test.orElse("world"));
}
@Test
void ifPresentWithValueNotPresent() {
JsonNullable<String> test = JsonNullable.undefined();
assertDoesNotThrow(() -> test.ifPresent(string -> {
throw new RuntimeException();
}));
}
@Test
void ifPresentWithNullValuePresent() {
JsonNullable<String> test = JsonNullable.of(null);
assertThrows(RuntimeException.class, () -> test.ifPresent(string -> {
throw new RuntimeException();
}));
}
@Test
void ifPresentWithNonNullValuePresent() {
JsonNullable<String> test = JsonNullable.of("test");
assertThrows(RuntimeException.class, () -> test.ifPresent(string -> {
throw new RuntimeException();
}));
}
@ParameterizedTest
@MethodSource("jsonProcessors")
void serializeNonBeanProperty(JsonProcessor jsonProcessor) throws Exception {
assertEquals("null", jsonProcessor.writeValueAsString(JsonNullable.of(null)));
assertEquals("\"foo\"", jsonProcessor.writeValueAsString(JsonNullable.of("foo")));
// TODO: Serialize non bean JsonNullable.undefined to empty string
assertEquals("null", jsonProcessor.writeValueAsString(JsonNullable.undefined()));
}
@ParameterizedTest
@MethodSource("jsonProcessors")
void serializeAlways(JsonProcessor jsonProcessor) throws Exception {
assertEquals("{}", jsonProcessor.writeValueAsString(new Pet().name(JsonNullable.<String>undefined())));
assertEquals("{\"name\":null}", jsonProcessor.writeValueAsString(new Pet().name(null)));
assertEquals("{\"name\":null}", jsonProcessor.writeValueAsString(new Pet().name(JsonNullable.<String>of(null))));
assertEquals("{\"name\":\"Rex\"}", jsonProcessor.writeValueAsString(new Pet().name(JsonNullable.of("Rex"))));
}
@ParameterizedTest
@MethodSource("jsonProcessors")
void serializeNonNull(JsonProcessor jsonProcessor) throws Exception {
jsonProcessor.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
assertEquals("{}", jsonProcessor.writeValueAsString(new Pet().name(JsonNullable.<String>undefined())));
assertEquals("{}", jsonProcessor.writeValueAsString(new Pet().name(null)));
assertEquals("{\"name\":null}", jsonProcessor.writeValueAsString(new Pet().name(JsonNullable.<String>of(null))));
assertEquals("{\"name\":\"Rex\"}", jsonProcessor.writeValueAsString(new Pet().name(JsonNullable.of("Rex"))));
}
@ParameterizedTest
@MethodSource("jsonProcessors")
void serializeNonAbsent(JsonProcessor jsonProcessor) throws Exception {
jsonProcessor.setDefaultPropertyInclusion(JsonInclude.Include.NON_ABSENT);
assertEquals("{}", jsonProcessor.writeValueAsString(new Pet().name(JsonNullable.<String>undefined())));
assertEquals("{}", jsonProcessor.writeValueAsString(new Pet().name(null)));
assertEquals("{\"name\":null}", jsonProcessor.writeValueAsString(new Pet().name(JsonNullable.<String>of(null))));
assertEquals("{\"name\":\"Rex\"}", jsonProcessor.writeValueAsString(new Pet().name(JsonNullable.of("Rex"))));
}
@ParameterizedTest
@MethodSource("jsonProcessors")
void serializeCollection(JsonProcessor jsonProcessor) throws Exception {
assertEquals("[\"foo\",null,null,null]", jsonProcessor.writeValueAsString(Arrays.asList(
JsonNullable.of("foo"),
JsonNullable.of(null),
JsonNullable.<String>undefined(),
null
)));
}
@ParameterizedTest
@MethodSource("jsonProcessors")
void deserializeStringMembers(JsonProcessor jsonProcessor) throws Exception {
testReadPetName(jsonProcessor, JsonNullable.of("Rex"), "{\"name\":\"Rex\"}");
testReadPetName(jsonProcessor, JsonNullable.<String>of(null), "{\"name\":null}");
testReadPetName(jsonProcessor, JsonNullable.<String>of(""), "{\"name\":\"\"}");
testReadPetName(jsonProcessor, JsonNullable.<String>of(" "), "{\"name\":\" \"}");
testReadPetName(jsonProcessor, JsonNullable.<String>undefined(), "{}");
}
@ParameterizedTest
@MethodSource("jsonProcessors")
void deserializeNonStringMembers(JsonProcessor jsonProcessor) throws Exception {
testReadPetAge(jsonProcessor, JsonNullable.of(Integer.valueOf(15)), "{\"age\":\"15\"}");
testReadPetAge(jsonProcessor, JsonNullable.<Integer>of(null), "{\"age\":null}");
testReadPetAge(jsonProcessor, JsonNullable.<Integer>of(null), "{\"age\":\"\"}");
testReadPetAge(jsonProcessor, JsonNullable.<Integer>of(null), "{\"age\":\" \"}");
testReadPetAge(jsonProcessor, JsonNullable.<Integer>undefined(), "{}");
}
@ParameterizedTest
@MethodSource("jsonProcessors")
void deserializeStringNonBeanMembers(JsonProcessor jsonProcessor) throws Exception {
assertEquals(JsonNullable.of(null), jsonProcessor.readValue("null", TypeReferences.STRING.getType(jsonProcessor)));
assertEquals(JsonNullable.of("42"), jsonProcessor.readValue("\"42\"", TypeReferences.STRING.getType(jsonProcessor)));
assertEquals(JsonNullable.of(""), jsonProcessor.readValue("\"\"", TypeReferences.STRING.getType(jsonProcessor)));
assertEquals(JsonNullable.of(" "), jsonProcessor.readValue("\" \"", TypeReferences.STRING.getType(jsonProcessor)));
}
@ParameterizedTest
@MethodSource("jsonProcessors")
void deserializeNonStringNonBeanMembers(JsonProcessor jsonProcessor) throws Exception {
assertEquals(JsonNullable.of(null), jsonProcessor.readValue("\"null\"", TypeReferences.INTEGER.getType(jsonProcessor)));
assertEquals(JsonNullable.of(42), jsonProcessor.readValue("\"42\"", TypeReferences.INTEGER.getType(jsonProcessor)));
assertEquals(JsonNullable.of(null), jsonProcessor.readValue("\"\"", TypeReferences.INTEGER.getType(jsonProcessor)));
assertEquals(JsonNullable.of(null), jsonProcessor.readValue("\" \"", TypeReferences.INTEGER.getType(jsonProcessor)));
}
@ParameterizedTest
@MethodSource("jsonProcessors")
void deserializeCollection(JsonProcessor jsonProcessor) throws Exception {
List<JsonNullable<String>> values = jsonProcessor.readValue("[\"foo\", null]",
TypeReferences.LIST_NULLABLE_STRING.getType(jsonProcessor));
assertEquals(2, values.size());
assertEquals(JsonNullable.of("foo"), values.get(0));
assertEquals(JsonNullable.of(null), values.get(1));
}
private void testReadPetName(JsonProcessor jsonProcessor, JsonNullable<String> expected, String json) throws Exception {
Pet pet = jsonProcessor.readValue(json, Pet.class);
JsonNullable<String> name = pet.name;
assertEquals(expected, name);
}
private void testReadPetAge(JsonProcessor jsonProcessor, JsonNullable<Integer> expected, String json) throws Exception {
Pet pet = jsonProcessor.readValue(json, Pet.class);
JsonNullable<Integer> age = pet.age;
assertEquals(expected, age);
}
private static class Pet {
public JsonNullable<String> name = JsonNullable.undefined();
public JsonNullable<Integer> age = JsonNullable.undefined();
public Pet name(JsonNullable<String> name) {
this.name = name;
return this;
}
public Pet age(JsonNullable<Integer> age) {
this.age = age;
return this;
}
}
private enum TypeReferences {
STRING {
@Override
public Object getType(JsonProcessor jsonProcessor) {
if (jsonProcessor instanceof Jackson2Processor) {
return new TypeReference<JsonNullable<String>>() {
};
}
if (jsonProcessor instanceof Jackson3Processor) {
return new tools.jackson.core.type.TypeReference<JsonNullable<String>>() {
};
}
throw new RuntimeException("jsonProcessor type not implemented");
}
},
INTEGER {
@Override
public Object getType(JsonProcessor jsonProcessor) {
if (jsonProcessor instanceof Jackson2Processor) {
return new TypeReference<JsonNullable<Integer>>() {
};
}
if (jsonProcessor instanceof Jackson3Processor) {
return new tools.jackson.core.type.TypeReference<JsonNullable<Integer>>() {
};
}
throw new RuntimeException("jsonProcessor type not implemented");
}
},
LIST_NULLABLE_STRING {
@Override
public Object getType(JsonProcessor jsonProcessor) {
if (jsonProcessor instanceof Jackson2Processor) {
return new TypeReference<List<JsonNullable<String>>>() {
};
}
if (jsonProcessor instanceof Jackson3Processor) {
return new tools.jackson.core.type.TypeReference<List<JsonNullable<String>>>() {
};
}
throw new RuntimeException("jsonProcessor type not implemented");
}
};
public abstract Object getType(JsonProcessor jsonProcessor);
}
}