Repository navigation
Expand file tree
/
Copy pathJacksonCodecTest.java
More file actions
313 lines (275 loc) · 10.3 KB
/
Copy pathJacksonCodecTest.java
File metadata and controls
313 lines (275 loc) · 10.3 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* Copyright 2012-2020 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package feign.jackson;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import feign.Request;
import feign.Request.HttpMethod;
import feign.Util;
import org.junit.Test;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import feign.RequestTemplate;
import feign.Response;
import static feign.Util.UTF_8;
import static feign.assertj.FeignAssertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@SuppressWarnings("deprecation")
public class JacksonCodecTest {
private String zonesJson = ""//
+ "[" + System.lineSeparator() //
+ " {" + System.lineSeparator() //
+ " \"name\": \"denominator.io.\"" + System.lineSeparator()//
+ " }," + System.lineSeparator()//
+ " {" + System.lineSeparator()//
+ " \"name\": \"denominator.io.\"," + System.lineSeparator()//
+ " \"id\": \"ABCD\"" + System.lineSeparator()//
+ " }" + System.lineSeparator()//
+ "]" + System.lineSeparator();
@Test
public void encodesMapObjectNumericalValuesAsInteger() {
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("foo", 1);
RequestTemplate template = new RequestTemplate();
new JacksonEncoder().encode(map, map.getClass(), template);
assertThat(template).hasBody(""//
+ "{" + System.lineSeparator() //
+ " \"foo\" : 1" + System.lineSeparator() //
+ "}");
}
@Test
public void encodesFormParams() {
Map<String, Object> form = new LinkedHashMap<String, Object>();
form.put("foo", 1);
form.put("bar", Arrays.asList(2, 3));
RequestTemplate template = new RequestTemplate();
new JacksonEncoder().encode(form, new TypeReference<Map<String, ?>>() {}.getType(), template);
assertThat(template).hasBody(""//
+ "{" + System.lineSeparator() //
+ " \"foo\" : 1," + System.lineSeparator() //
+ " \"bar\" : [ 2, 3 ]" + System.lineSeparator() //
+ "}");
}
@Test
public void decodes() throws Exception {
List<Zone> zones = new LinkedList<>();
zones.add(new Zone("denominator.io."));
zones.add(new Zone("denominator.io.", "ABCD"));
Response response = Response.builder()
.status(200)
.reason("OK")
.request(
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8, null))
.headers(Collections.emptyMap())
.body(zonesJson, UTF_8)
.build();
assertEquals(zones,
new JacksonDecoder().decode(response, new TypeReference<List<Zone>>() {}.getType()));
}
@Test
public void nullBodyDecodesToNull() throws Exception {
Response response = Response.builder()
.status(204)
.reason("OK")
.request(
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8, null))
.headers(Collections.emptyMap())
.build();
assertNull(new JacksonDecoder().decode(response, String.class));
}
@Test
public void emptyBodyDecodesToNull() throws Exception {
Response response = Response.builder()
.status(204)
.reason("OK")
.request(
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8, null))
.headers(Collections.emptyMap())
.body(new byte[0])
.build();
assertNull(new JacksonDecoder().decode(response, String.class));
}
@Test
public void customDecoder() throws Exception {
JacksonDecoder decoder = new JacksonDecoder(
Arrays.asList(
new SimpleModule().addDeserializer(Zone.class, new ZoneDeserializer())));
List<Zone> zones = new LinkedList<Zone>();
zones.add(new Zone("DENOMINATOR.IO."));
zones.add(new Zone("DENOMINATOR.IO.", "ABCD"));
Response response = Response.builder()
.status(200)
.reason("OK")
.request(
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8, null))
.headers(Collections.emptyMap())
.body(zonesJson, UTF_8)
.build();
assertEquals(zones, decoder.decode(response, new TypeReference<List<Zone>>() {}.getType()));
}
@Test
public void customEncoder() {
JacksonEncoder encoder = new JacksonEncoder(
Arrays.asList(new SimpleModule().addSerializer(Zone.class, new ZoneSerializer())));
List<Zone> zones = new LinkedList<Zone>();
zones.add(new Zone("denominator.io."));
zones.add(new Zone("denominator.io.", "abcd"));
RequestTemplate template = new RequestTemplate();
encoder.encode(zones, new TypeReference<List<Zone>>() {}.getType(), template);
assertThat(template).hasBody("" //
+ "[ {" + System.lineSeparator()
+ " \"name\" : \"DENOMINATOR.IO.\"" + System.lineSeparator()
+ "}, {" + System.lineSeparator()
+ " \"name\" : \"DENOMINATOR.IO.\"," + System.lineSeparator()
+ " \"id\" : \"ABCD\"" + System.lineSeparator()
+ "} ]");
}
@Test
public void decodesIterator() throws Exception {
List<Zone> zones = new LinkedList<Zone>();
zones.add(new Zone("denominator.io."));
zones.add(new Zone("denominator.io.", "ABCD"));
Response response = Response.builder()
.status(200)
.reason("OK")
.request(
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8, null))
.headers(Collections.emptyMap())
.body(zonesJson, UTF_8)
.build();
Object decoded = JacksonIteratorDecoder.create().decode(response,
new TypeReference<Iterator<Zone>>() {}.getType());
assertTrue(Iterator.class.isAssignableFrom(decoded.getClass()));
assertTrue(Closeable.class.isAssignableFrom(decoded.getClass()));
assertEquals(zones, asList((Iterator<?>) decoded));
}
private <T> List<T> asList(Iterator<T> iter) {
final List<T> copy = new ArrayList<T>();
while (iter.hasNext())
copy.add(iter.next());
return copy;
}
@Test
public void nullBodyDecodesToNullIterator() throws Exception {
Response response = Response.builder()
.status(204)
.reason("OK")
.request(
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8, null))
.headers(Collections.emptyMap())
.build();
assertNull(JacksonIteratorDecoder.create().decode(response, Iterator.class));
}
@Test
public void emptyBodyDecodesToNullIterator() throws Exception {
Response response = Response.builder()
.status(204)
.reason("OK")
.request(
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8, null))
.headers(Collections.emptyMap())
.body(new byte[0])
.build();
assertNull(JacksonIteratorDecoder.create().decode(response, Iterator.class));
}
static class Zone extends LinkedHashMap<String, Object> {
private static final long serialVersionUID = 1L;
Zone() {
// for reflective instantiation.
}
Zone(String name) {
this(name, null);
}
Zone(String name, String id) {
put("name", name);
if (id != null) {
put("id", id);
}
}
}
static class ZoneDeserializer extends StdDeserializer<Zone> {
public ZoneDeserializer() {
super(Zone.class);
}
@Override
public Zone deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
Zone zone = new Zone();
jp.nextToken();
while (jp.nextToken() != JsonToken.END_OBJECT) {
String name = jp.getCurrentName();
String value = jp.getValueAsString();
if (value != null) {
zone.put(name, value.toUpperCase());
}
}
return zone;
}
}
static class ZoneSerializer extends StdSerializer<Zone> {
public ZoneSerializer() {
super(Zone.class);
}
@Override
public void serialize(Zone value, JsonGenerator jgen, SerializerProvider provider)
throws IOException {
jgen.writeStartObject();
for (Map.Entry<String, Object> entry : value.entrySet()) {
jgen.writeFieldName(entry.getKey());
jgen.writeString(entry.getValue().toString().toUpperCase());
}
jgen.writeEndObject();
}
}
/** Enabled via {@link feign.Feign.Builder#decode404()} */
@Test
public void notFoundDecodesToNull() throws Exception {
Response response = Response.builder()
.status(404)
.reason("NOT FOUND")
.request(
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8, null))
.headers(Collections.emptyMap())
.build();
assertThat((byte[]) new JacksonDecoder().decode(response, byte[].class)).isNull();
}
/** Enabled via {@link feign.Feign.Builder#decode404()} */
@Test
public void notFoundDecodesToNullIterator() throws Exception {
Response response = Response.builder()
.status(404)
.reason("NOT FOUND")
.request(
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8, null))
.headers(Collections.emptyMap())
.build();
assertThat((byte[]) JacksonIteratorDecoder.create().decode(response, byte[].class)).isNull();
}
}