Repository navigation
Expand file tree
/
Copy pathAnnotationErrorDecoderExceptionConstructorsTest.java
More file actions
704 lines (616 loc) · 23.7 KB
/
Copy pathAnnotationErrorDecoderExceptionConstructorsTest.java
File metadata and controls
704 lines (616 loc) · 23.7 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/*
* Copyright © 2012 The Feign Authors (feign@commonhaus.dev)
*
* 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.error;
import static org.assertj.core.api.Assertions.assertThat;
import feign.Request;
import feign.codec.Decoder;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors.DeclaredDefaultConstructorException;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors.DeclaredDefaultConstructorWithOtherConstructorsException;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors.DefaultConstructorException;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors.DefinedConstructorWithAnnotationForBody;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors.DefinedConstructorWithAnnotationForBodyAndHeaders;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors.DefinedConstructorWithAnnotationForBodyAndHeadersSecondOrder;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors.DefinedConstructorWithAnnotationForHeaders;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors.DefinedConstructorWithAnnotationForHeadersButNotForBody;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors.DefinedConstructorWithAnnotationForNonSupportedBody;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors.DefinedConstructorWithAnnotationForOptionalBody;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors.DefinedConstructorWithNoAnnotationForBody;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors.DefinedConstructorWithRequest;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors.DefinedConstructorWithRequestAndAnnotationForResponseBody;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors.DefinedConstructorWithRequestAndResponseBody;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors.DefinedConstructorWithRequestAndResponseHeadersAndOptionalResponseBody;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors.DefinedConstructorWithRequestAndResponseHeadersAndResponseBody;
import feign.error.AnnotationErrorDecoderExceptionConstructorsTest.TestClientInterfaceWithDifferentExceptionConstructors.ParametersException;
import feign.optionals.OptionalDecoder;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
public class AnnotationErrorDecoderExceptionConstructorsTest
extends AbstractAnnotationErrorDecoderTest<
TestClientInterfaceWithDifferentExceptionConstructors> {
private static final String NO_BODY = "NO BODY";
private static final Object NULL_BODY = null;
private static final String NON_NULL_BODY = "A GIVEN BODY";
private static final feign.Request REQUEST =
feign.Request.create(
feign.Request.HttpMethod.GET,
"http://test",
Collections.emptyMap(),
Request.Body.empty(),
null);
private static final feign.Request NO_REQUEST = null;
private static final Map<String, Collection<String>> NON_NULL_HEADERS = new HashMap<>();
private static final Map<String, Collection<String>> NO_HEADERS = null;
@Override
public Class<TestClientInterfaceWithDifferentExceptionConstructors> interfaceAtTest() {
return TestClientInterfaceWithDifferentExceptionConstructors.class;
}
public static Iterable<Object[]> data() {
return Arrays.asList(
new Object[][] {
{
"Test Default Constructor",
500,
DefaultConstructorException.class,
NO_REQUEST,
NO_BODY,
NO_HEADERS
},
{
"test Default Constructor",
501,
DeclaredDefaultConstructorException.class,
NO_REQUEST,
NO_BODY,
NO_HEADERS
},
{
"test Default Constructor",
502,
DeclaredDefaultConstructorWithOtherConstructorsException.class,
NO_REQUEST,
NO_BODY,
NO_HEADERS
},
{
"test Declared Constructor",
503,
DefinedConstructorWithNoAnnotationForBody.class,
NO_REQUEST,
NON_NULL_BODY,
NO_HEADERS
},
{
"test Declared Constructor",
504,
DefinedConstructorWithAnnotationForBody.class,
NO_REQUEST,
NON_NULL_BODY,
NO_HEADERS
},
{
"test Declared Constructor",
505,
DefinedConstructorWithAnnotationForBodyAndHeaders.class,
NO_REQUEST,
NON_NULL_BODY,
NON_NULL_HEADERS
},
{
"test Declared Constructor",
506,
DefinedConstructorWithAnnotationForBodyAndHeadersSecondOrder.class,
NO_REQUEST,
NON_NULL_BODY,
NON_NULL_HEADERS
},
{
"test Declared Constructor",
507,
DefinedConstructorWithAnnotationForHeaders.class,
NO_REQUEST,
NO_BODY,
NON_NULL_HEADERS
},
{
"test Declared Constructor",
508,
DefinedConstructorWithAnnotationForHeadersButNotForBody.class,
NO_REQUEST,
NON_NULL_BODY,
NON_NULL_HEADERS
},
{
"test Declared Constructor",
509,
DefinedConstructorWithAnnotationForNonSupportedBody.class,
NO_REQUEST,
NULL_BODY,
NO_HEADERS
},
{
"test Declared Constructor",
510,
DefinedConstructorWithAnnotationForOptionalBody.class,
NO_REQUEST,
Optional.of(NON_NULL_BODY),
NO_HEADERS
},
{
"test Declared Constructor",
511,
DefinedConstructorWithRequest.class,
REQUEST,
NO_BODY,
NO_HEADERS
},
{
"test Declared Constructor",
512,
DefinedConstructorWithRequestAndResponseBody.class,
REQUEST,
NON_NULL_BODY,
NO_HEADERS
},
{
"test Declared Constructor",
513,
DefinedConstructorWithRequestAndAnnotationForResponseBody.class,
REQUEST,
NON_NULL_BODY,
NO_HEADERS
},
{
"test Declared Constructor",
514,
DefinedConstructorWithRequestAndResponseHeadersAndResponseBody.class,
REQUEST,
NON_NULL_BODY,
NON_NULL_HEADERS
},
{
"test Declared Constructor",
515,
DefinedConstructorWithRequestAndResponseHeadersAndOptionalResponseBody.class,
REQUEST,
Optional.of(NON_NULL_BODY),
NON_NULL_HEADERS
}
});
} // first data value (0) is default
public String testName;
public int errorCode;
public Class<? extends Exception> expectedExceptionClass;
public Object expectedRequest;
public Object expectedBody;
public Map<String, Collection<String>> expectedHeaders;
@MethodSource("data")
@ParameterizedTest(
name = "{0}: When error code ({1}) on method ({2}) should return exception type ({3})")
void test(
String testName,
int errorCode,
Class<? extends Exception> expectedExceptionClass,
Object expectedRequest,
Object expectedBody,
Map<String, Collection<String>> expectedHeaders)
throws Exception {
initAnnotationErrorDecoderExceptionConstructorsTest(
testName,
errorCode,
expectedExceptionClass,
expectedRequest,
expectedBody,
expectedHeaders);
AnnotationErrorDecoder decoder =
AnnotationErrorDecoder.builderFor(
TestClientInterfaceWithDifferentExceptionConstructors.class)
.withResponseBodyDecoder(new OptionalDecoder(new Decoder.Default()))
.build();
Exception genericException =
decoder.decode(
feignConfigKey("method1Test"),
testResponse(errorCode, NON_NULL_BODY, NON_NULL_HEADERS));
assertThat(genericException).isInstanceOf(expectedExceptionClass);
ParametersException exception = (ParametersException) genericException;
assertThat(exception.body()).isEqualTo(expectedBody);
assertThat(exception.headers()).isEqualTo(expectedHeaders);
}
@MethodSource("data")
@ParameterizedTest(
name = "{0}: When error code ({1}) on method ({2}) should return exception type ({3})")
void ifExceptionIsNotInTheList(
String testName,
int errorCode,
Class<? extends Exception> expectedExceptionClass,
Object expectedRequest,
Object expectedBody,
Map<String, Collection<String>> expectedHeaders)
throws Exception {
initAnnotationErrorDecoderExceptionConstructorsTest(
testName,
errorCode,
expectedExceptionClass,
expectedRequest,
expectedBody,
expectedHeaders);
AnnotationErrorDecoder decoder =
AnnotationErrorDecoder.builderFor(
TestClientInterfaceWithDifferentExceptionConstructors.class)
.withResponseBodyDecoder(new OptionalDecoder(new Decoder.Default()))
.build();
Exception genericException =
decoder.decode(
feignConfigKey("method1Test"), testResponse(-1, NON_NULL_BODY, NON_NULL_HEADERS));
assertThat(genericException)
.isInstanceOf(ErrorHandling.NO_DEFAULT.class)
.hasMessage("Endpoint responded with -1, reason: null");
}
interface TestClientInterfaceWithDifferentExceptionConstructors {
@ErrorHandling(
codeSpecific = {
@ErrorCodes(
codes = {500},
generate = DefaultConstructorException.class),
@ErrorCodes(
codes = {501},
generate = DeclaredDefaultConstructorException.class),
@ErrorCodes(
codes = {502},
generate = DeclaredDefaultConstructorWithOtherConstructorsException.class),
@ErrorCodes(
codes = {503},
generate = DefinedConstructorWithNoAnnotationForBody.class),
@ErrorCodes(
codes = {504},
generate = DefinedConstructorWithAnnotationForBody.class),
@ErrorCodes(
codes = {505},
generate = DefinedConstructorWithAnnotationForBodyAndHeaders.class),
@ErrorCodes(
codes = {506},
generate = DefinedConstructorWithAnnotationForBodyAndHeadersSecondOrder.class),
@ErrorCodes(
codes = {507},
generate = DefinedConstructorWithAnnotationForHeaders.class),
@ErrorCodes(
codes = {508},
generate = DefinedConstructorWithAnnotationForHeadersButNotForBody.class),
@ErrorCodes(
codes = {509},
generate = DefinedConstructorWithAnnotationForNonSupportedBody.class),
@ErrorCodes(
codes = {510},
generate = DefinedConstructorWithAnnotationForOptionalBody.class),
@ErrorCodes(
codes = {511},
generate = DefinedConstructorWithRequest.class),
@ErrorCodes(
codes = {512},
generate = DefinedConstructorWithRequestAndResponseBody.class),
@ErrorCodes(
codes = {513},
generate = DefinedConstructorWithRequestAndAnnotationForResponseBody.class),
@ErrorCodes(
codes = {514},
generate = DefinedConstructorWithRequestAndResponseHeadersAndResponseBody.class),
@ErrorCodes(
codes = {515},
generate =
DefinedConstructorWithRequestAndResponseHeadersAndOptionalResponseBody.class)
})
void method1Test();
class ParametersException extends Exception {
public Object body() {
return NO_BODY;
}
public feign.Request request() {
return null;
}
public Map<String, Collection<String>> headers() {
return null;
}
}
class DefaultConstructorException extends ParametersException {}
class DeclaredDefaultConstructorException extends ParametersException {
public DeclaredDefaultConstructorException() {}
}
class DeclaredDefaultConstructorWithOtherConstructorsException extends ParametersException {
public DeclaredDefaultConstructorWithOtherConstructorsException() {}
public DeclaredDefaultConstructorWithOtherConstructorsException(TestPojo testPojo) {
throw new UnsupportedOperationException("Should not be called");
}
public DeclaredDefaultConstructorWithOtherConstructorsException(Throwable cause) {
throw new UnsupportedOperationException("Should not be called");
}
public DeclaredDefaultConstructorWithOtherConstructorsException(
String message, Throwable cause) {
throw new UnsupportedOperationException("Should not be called");
}
}
class DefinedConstructorWithNoAnnotationForBody extends ParametersException {
String body;
public DefinedConstructorWithNoAnnotationForBody() {
throw new UnsupportedOperationException("Should not be called");
}
@FeignExceptionConstructor
public DefinedConstructorWithNoAnnotationForBody(String body) {
this.body = body;
}
public DefinedConstructorWithNoAnnotationForBody(TestPojo testPojo) {
throw new UnsupportedOperationException("Should not be called");
}
@Override
public Object body() {
return body;
}
}
class DefinedConstructorWithRequest extends ParametersException {
feign.Request request;
public DefinedConstructorWithRequest() {
throw new UnsupportedOperationException("Should not be called");
}
@FeignExceptionConstructor
public DefinedConstructorWithRequest(feign.Request request) {
this.request = request;
}
public DefinedConstructorWithRequest(TestPojo testPojo) {
throw new UnsupportedOperationException("Should not be called");
}
@Override
public feign.Request request() {
return request;
}
}
class DefinedConstructorWithRequestAndResponseBody extends ParametersException {
feign.Request request;
String body;
public DefinedConstructorWithRequestAndResponseBody() {
throw new UnsupportedOperationException("Should not be called");
}
@FeignExceptionConstructor
public DefinedConstructorWithRequestAndResponseBody(feign.Request request, String body) {
this.request = request;
this.body = body;
}
public DefinedConstructorWithRequestAndResponseBody(TestPojo testPojo) {
throw new UnsupportedOperationException("Should not be called");
}
@Override
public feign.Request request() {
return request;
}
@Override
public String body() {
return body;
}
}
class DefinedConstructorWithRequestAndAnnotationForResponseBody extends ParametersException {
feign.Request request;
String body;
public DefinedConstructorWithRequestAndAnnotationForResponseBody() {
throw new UnsupportedOperationException("Should not be called");
}
@FeignExceptionConstructor
public DefinedConstructorWithRequestAndAnnotationForResponseBody(
feign.Request request, @ResponseBody String body) {
this.request = request;
this.body = body;
}
public DefinedConstructorWithRequestAndAnnotationForResponseBody(TestPojo testPojo) {
throw new UnsupportedOperationException("Should not be called");
}
@Override
public feign.Request request() {
return request;
}
@Override
public String body() {
return body;
}
}
class DefinedConstructorWithRequestAndResponseHeadersAndResponseBody
extends ParametersException {
feign.Request request;
String body;
Map headers;
public DefinedConstructorWithRequestAndResponseHeadersAndResponseBody() {
throw new UnsupportedOperationException("Should not be called");
}
@FeignExceptionConstructor
public DefinedConstructorWithRequestAndResponseHeadersAndResponseBody(
feign.Request request, @ResponseHeaders Map headers, @ResponseBody String body) {
this.request = request;
this.body = body;
this.headers = headers;
}
public DefinedConstructorWithRequestAndResponseHeadersAndResponseBody(TestPojo testPojo) {
throw new UnsupportedOperationException("Should not be called");
}
@Override
public feign.Request request() {
return request;
}
@Override
public Map headers() {
return headers;
}
@Override
public String body() {
return body;
}
}
class DefinedConstructorWithRequestAndResponseHeadersAndOptionalResponseBody
extends ParametersException {
feign.Request request;
Optional<String> body;
Map headers;
public DefinedConstructorWithRequestAndResponseHeadersAndOptionalResponseBody() {
throw new UnsupportedOperationException("Should not be called");
}
@FeignExceptionConstructor
public DefinedConstructorWithRequestAndResponseHeadersAndOptionalResponseBody(
feign.Request request,
@ResponseHeaders Map headers,
@ResponseBody Optional<String> body) {
this.request = request;
this.body = body;
this.headers = headers;
}
public DefinedConstructorWithRequestAndResponseHeadersAndOptionalResponseBody(
TestPojo testPojo) {
throw new UnsupportedOperationException("Should not be called");
}
@Override
public feign.Request request() {
return request;
}
@Override
public Map headers() {
return headers;
}
@Override
public Object body() {
return body;
}
}
class DefinedConstructorWithAnnotationForBody extends ParametersException {
String body;
public DefinedConstructorWithAnnotationForBody() {
throw new UnsupportedOperationException("Should not be called");
}
@FeignExceptionConstructor
public DefinedConstructorWithAnnotationForBody(@ResponseBody String body) {
this.body = body;
}
@Override
public Object body() {
return body;
}
}
class DefinedConstructorWithAnnotationForOptionalBody extends ParametersException {
Optional<String> body;
public DefinedConstructorWithAnnotationForOptionalBody() {
throw new UnsupportedOperationException("Should not be called");
}
@FeignExceptionConstructor
public DefinedConstructorWithAnnotationForOptionalBody(@ResponseBody Optional<String> body) {
this.body = body;
}
@Override
public Object body() {
return body;
}
}
class DefinedConstructorWithAnnotationForNonSupportedBody extends ParametersException {
TestPojo body;
@FeignExceptionConstructor
public DefinedConstructorWithAnnotationForNonSupportedBody(@ResponseBody TestPojo body) {
this.body = body;
}
@Override
public Object body() {
return body;
}
}
class DefinedConstructorWithAnnotationForBodyAndHeaders extends ParametersException {
String body;
Map<String, Collection<String>> headers;
@FeignExceptionConstructor
public DefinedConstructorWithAnnotationForBodyAndHeaders(
@ResponseBody String body, @ResponseHeaders Map<String, Collection<String>> headers) {
this.body = body;
this.headers = headers;
}
@Override
public Object body() {
return body;
}
@Override
public Map<String, Collection<String>> headers() {
return headers;
}
}
class DefinedConstructorWithAnnotationForBodyAndHeadersSecondOrder extends ParametersException {
String body;
Map<String, Collection<String>> headers;
@FeignExceptionConstructor
public DefinedConstructorWithAnnotationForBodyAndHeadersSecondOrder(
@ResponseHeaders Map<String, Collection<String>> headers, @ResponseBody String body) {
this.body = body;
this.headers = headers;
}
@Override
public Object body() {
return body;
}
@Override
public Map<String, Collection<String>> headers() {
return headers;
}
}
class DefinedConstructorWithAnnotationForHeaders extends ParametersException {
Map<String, Collection<String>> headers;
@FeignExceptionConstructor
public DefinedConstructorWithAnnotationForHeaders(
@ResponseHeaders Map<String, Collection<String>> headers) {
this.headers = headers;
}
@Override
public Map<String, Collection<String>> headers() {
return headers;
}
}
class DefinedConstructorWithAnnotationForHeadersButNotForBody extends ParametersException {
String body;
Map<String, Collection<String>> headers;
@FeignExceptionConstructor
public DefinedConstructorWithAnnotationForHeadersButNotForBody(
@ResponseHeaders Map<String, Collection<String>> headers, String body) {
this.body = body;
this.headers = headers;
}
@Override
public Object body() {
return body;
}
@Override
public Map<String, Collection<String>> headers() {
return headers;
}
}
}
public void initAnnotationErrorDecoderExceptionConstructorsTest(
String testName,
int errorCode,
Class<? extends Exception> expectedExceptionClass,
Object expectedRequest,
Object expectedBody,
Map<String, Collection<String>> expectedHeaders) {
this.testName = testName;
this.errorCode = errorCode;
this.expectedExceptionClass = expectedExceptionClass;
this.expectedRequest = expectedRequest;
this.expectedBody = expectedBody;
this.expectedHeaders = expectedHeaders;
}
}