1+ /**
2+ * Copyright 2017-2019 The Feign Authors
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+ * in compliance with the License. You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software distributed under the License
10+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+ * or implied. See the License for the specific language governing permissions and limitations under
12+ * the License.
13+ */
14+ package feign .error ;
15+
16+ import static feign .error .AnnotationErrorDecoderInheritanceTest .TestClientInterfaceWithExceptionPriority .ClassLevelDefaultException ;
17+ import static feign .error .AnnotationErrorDecoderInheritanceTest .TestClientInterfaceWithExceptionPriority .ClassLevelNotFoundException ;
18+ import static feign .error .AnnotationErrorDecoderInheritanceTest .TestClientInterfaceWithExceptionPriority .Method1DefaultException ;
19+ import static feign .error .AnnotationErrorDecoderInheritanceTest .TestClientInterfaceWithExceptionPriority .Method1NotFoundException ;
20+ import static feign .error .AnnotationErrorDecoderInheritanceTest .TestClientInterfaceWithExceptionPriority .Method2NotFoundException ;
21+ import static feign .error .AnnotationErrorDecoderInheritanceTest .TestClientInterfaceWithExceptionPriority .Method3DefaultException ;
22+ import static feign .error .AnnotationErrorDecoderInheritanceTest .TestClientInterfaceWithExceptionPriority .ServeErrorException ;
23+ import static feign .error .AnnotationErrorDecoderInheritanceTest .TestClientInterfaceWithExceptionPriority .UnauthenticatedOrUnauthorizedException ;
24+ import static org .assertj .core .api .Assertions .assertThat ;
25+
26+ import java .util .Arrays ;
27+ import org .junit .Test ;
28+ import org .junit .runner .RunWith ;
29+ import org .junit .runners .Parameterized ;
30+ import org .junit .runners .Parameterized .Parameter ;
31+ import org .junit .runners .Parameterized .Parameters ;
32+
33+ @ RunWith (Parameterized .class )
34+ public class AnnotationErrorDecoderInheritanceTest extends AbstractAnnotationErrorDecoderTest <AnnotationErrorDecoderInheritanceTest .TestClientInterfaceWithExceptionPriority > {
35+
36+ @ Override
37+ public Class <TestClientInterfaceWithExceptionPriority > interfaceAtTest () {
38+ return TestClientInterfaceWithExceptionPriority .class ;
39+ }
40+
41+ @ Parameters (name = "{0}: When error code ({1}) on method ({2}) should return exception type ({3})" )
42+ public static Iterable <Object []> data () {
43+ return Arrays .asList (new Object [][] {
44+ {"Test Code Specific At Method" , 404 , "method1Test" , Method1NotFoundException .class },
45+ {"Test Code Specific At Method" , 401 , "method1Test" , UnauthenticatedOrUnauthorizedException .class },
46+ {"Test Code Specific At Method" , 404 , "method2Test" , Method2NotFoundException .class },
47+ {"Test Code Specific At Method" , 500 , "method2Test" , ServeErrorException .class },
48+ {"Test Code Specific At Method" , 503 , "method2Test" , ServeErrorException .class },
49+ {"Test Code Specific At Class" , 403 , "method1Test" , UnauthenticatedOrUnauthorizedException .class },
50+ {"Test Code Specific At Class" , 403 , "method2Test" , UnauthenticatedOrUnauthorizedException .class },
51+ {"Test Code Specific At Class" , 404 , "method3Test" , ClassLevelNotFoundException .class },
52+ {"Test Code Specific At Class" , 403 , "method3Test" , UnauthenticatedOrUnauthorizedException .class },
53+ {"Test Default At Method" , 504 , "method1Test" , Method1DefaultException .class },
54+ {"Test Default At Method" , 504 , "method3Test" , Method3DefaultException .class },
55+ {"Test Default At Class" , 504 , "method2Test" , ClassLevelDefaultException .class },
56+ });
57+ }
58+
59+ @ Parameter // first data value (0) is default
60+ public String testType ;
61+
62+ @ Parameter (1 )
63+ public int errorCode ;
64+
65+ @ Parameter (2 )
66+ public String method ;
67+
68+ @ Parameter (3 )
69+ public Class <? extends Exception > expectedExceptionClass ;
70+
71+ @ Test
72+ public void test () throws Exception {
73+ AnnotationErrorDecoder decoder = AnnotationErrorDecoder .builderFor ( TestClientInterfaceWithExceptionPriority .class ).build ();
74+
75+ assertThat (decoder .decode (feignConfigKey (method ), testResponse (errorCode )).getClass ())
76+ .isEqualTo (expectedExceptionClass );
77+ }
78+
79+
80+ @ ErrorHandling (codeSpecific =
81+ {
82+ @ ErrorCodes ( codes = {404 }, generate = ClassLevelNotFoundException .class ),
83+ @ ErrorCodes ( codes = {403 }, generate = UnauthenticatedOrUnauthorizedException .class )
84+ },
85+ defaultException = ClassLevelDefaultException .class
86+ )
87+ interface TopLevelInterface {
88+ @ ErrorHandling (codeSpecific =
89+ {
90+ @ ErrorCodes ( codes = {404 }, generate = Method1NotFoundException .class ),
91+ @ ErrorCodes ( codes = {401 }, generate = UnauthenticatedOrUnauthorizedException .class )
92+ }
93+ ,
94+ defaultException = Method1DefaultException .class
95+ )
96+ void method1Test ();
97+
98+ class ClassLevelDefaultException extends Exception {}
99+ class Method1DefaultException extends Exception {}
100+ class Method3DefaultException extends Exception {}
101+ class Method1NotFoundException extends Exception {}
102+ class Method2NotFoundException extends Exception {}
103+ class ClassLevelNotFoundException extends Exception {}
104+ class UnauthenticatedOrUnauthorizedException extends Exception {}
105+ class ServeErrorException extends Exception {}
106+
107+ }
108+
109+ interface SecondTopLevelInterface {}
110+ interface SecondLevelInterface extends SecondTopLevelInterface , TopLevelInterface {}
111+
112+ interface TestClientInterfaceWithExceptionPriority extends SecondLevelInterface {
113+
114+ @ ErrorHandling (codeSpecific =
115+ {
116+ @ ErrorCodes ( codes = {404 }, generate = Method2NotFoundException .class ),
117+ @ ErrorCodes ( codes = {500 , 503 }, generate = ServeErrorException .class )
118+ }
119+ )
120+ void method2Test ();
121+
122+ @ ErrorHandling (
123+ defaultException = Method3DefaultException .class
124+ )
125+ void method3Test ();
126+ }
127+ }
0 commit comments