|
| 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 org.assertj.core.api.Assertions.assertThat; |
| 17 | +import feign.error.AnnotationErrorDecoderClassInheritanceTest.ParentInterfaceWithErrorHandling.Method1NotFoundException; |
| 18 | +import feign.error.AnnotationErrorDecoderClassInheritanceTest.ParentInterfaceWithErrorHandling.UnauthenticatedOrUnauthorizedException; |
| 19 | +import feign.error.AnnotationErrorDecoderClassInheritanceTest.ParentInterfaceWithErrorHandling.Method2NotFoundException; |
| 20 | +import feign.error.AnnotationErrorDecoderClassInheritanceTest.ParentInterfaceWithErrorHandling.ServeErrorException; |
| 21 | +import feign.error.AnnotationErrorDecoderClassInheritanceTest.ParentInterfaceWithErrorHandling.ClassLevelNotFoundException; |
| 22 | +import feign.error.AnnotationErrorDecoderClassInheritanceTest.ParentInterfaceWithErrorHandling.Method1DefaultException; |
| 23 | +import feign.error.AnnotationErrorDecoderClassInheritanceTest.ParentInterfaceWithErrorHandling.Method3DefaultException; |
| 24 | +import feign.error.AnnotationErrorDecoderClassInheritanceTest.ParentInterfaceWithErrorHandling.ClassLevelDefaultException; |
| 25 | +import java.util.Arrays; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | +import org.junit.runners.Parameterized; |
| 29 | +import org.junit.runners.Parameterized.Parameter; |
| 30 | +import org.junit.runners.Parameterized.Parameters; |
| 31 | + |
| 32 | +@RunWith(Parameterized.class) |
| 33 | +public class AnnotationErrorDecoderClassInheritanceTest extends |
| 34 | + AbstractAnnotationErrorDecoderTest<AnnotationErrorDecoderClassInheritanceTest.GrandChild> { |
| 35 | + |
| 36 | + @Override |
| 37 | + public Class<GrandChild> interfaceAtTest() { |
| 38 | + return GrandChild.class; |
| 39 | + } |
| 40 | + |
| 41 | + @Parameters( |
| 42 | + name = "{0}: When error code ({1}) on method ({2}) should return exception type ({3})") |
| 43 | + public static Iterable<Object[]> data() { |
| 44 | + return Arrays.asList(new Object[][] { |
| 45 | + {"Test Code Specific At Method", 404, "method1Test", Method1NotFoundException.class}, |
| 46 | + {"Test Code Specific At Method", 401, "method1Test", |
| 47 | + UnauthenticatedOrUnauthorizedException.class}, |
| 48 | + {"Test Code Specific At Method", 404, "method2Test", Method2NotFoundException.class}, |
| 49 | + {"Test Code Specific At Method", 500, "method2Test", ServeErrorException.class}, |
| 50 | + {"Test Code Specific At Method", 503, "method2Test", ServeErrorException.class}, |
| 51 | + {"Test Code Specific At Class", 403, "method1Test", |
| 52 | + UnauthenticatedOrUnauthorizedException.class}, |
| 53 | + {"Test Code Specific At Class", 403, "method2Test", |
| 54 | + UnauthenticatedOrUnauthorizedException.class}, |
| 55 | + {"Test Code Specific At Class", 404, "method3Test", ClassLevelNotFoundException.class}, |
| 56 | + {"Test Code Specific At Class", 403, "method3Test", |
| 57 | + UnauthenticatedOrUnauthorizedException.class}, |
| 58 | + {"Test Default At Method", 504, "method1Test", Method1DefaultException.class}, |
| 59 | + {"Test Default At Method", 504, "method3Test", Method3DefaultException.class}, |
| 60 | + {"Test Default At Class", 504, "method2Test", ClassLevelDefaultException.class}, |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + @Parameter // first data value (0) is default |
| 65 | + public String testType; |
| 66 | + |
| 67 | + @Parameter(1) |
| 68 | + public int errorCode; |
| 69 | + |
| 70 | + @Parameter(2) |
| 71 | + public String method; |
| 72 | + |
| 73 | + @Parameter(3) |
| 74 | + public Class<? extends Exception> expectedExceptionClass; |
| 75 | + |
| 76 | + @Test |
| 77 | + public void test() throws Exception { |
| 78 | + AnnotationErrorDecoder decoder = |
| 79 | + AnnotationErrorDecoder.builderFor(GrandChild.class).build(); |
| 80 | + |
| 81 | + assertThat(decoder.decode(feignConfigKey(method), testResponse(errorCode)).getClass()) |
| 82 | + .isEqualTo(expectedExceptionClass); |
| 83 | + } |
| 84 | + |
| 85 | + @ErrorHandling(codeSpecific = { |
| 86 | + @ErrorCodes(codes = {404}, generate = ClassLevelNotFoundException.class), |
| 87 | + @ErrorCodes(codes = {403}, generate = UnauthenticatedOrUnauthorizedException.class) |
| 88 | + }, |
| 89 | + defaultException = ClassLevelDefaultException.class) |
| 90 | + interface ParentInterfaceWithErrorHandling { |
| 91 | + @ErrorHandling(codeSpecific = { |
| 92 | + @ErrorCodes(codes = {404}, generate = Method1NotFoundException.class), |
| 93 | + @ErrorCodes(codes = {401}, generate = UnauthenticatedOrUnauthorizedException.class) |
| 94 | + }, |
| 95 | + defaultException = Method1DefaultException.class) |
| 96 | + void method1Test(); |
| 97 | + |
| 98 | + @ErrorHandling(codeSpecific = { |
| 99 | + @ErrorCodes(codes = {404}, generate = Method2NotFoundException.class), |
| 100 | + @ErrorCodes(codes = {500, 503}, generate = ServeErrorException.class) |
| 101 | + }) |
| 102 | + void method2Test(); |
| 103 | + |
| 104 | + @ErrorHandling( |
| 105 | + defaultException = Method3DefaultException.class) |
| 106 | + void method3Test(); |
| 107 | + |
| 108 | + class ClassLevelDefaultException extends Exception { |
| 109 | + } |
| 110 | + class Method1DefaultException extends Exception { |
| 111 | + } |
| 112 | + class Method3DefaultException extends Exception { |
| 113 | + } |
| 114 | + class Method1NotFoundException extends Exception { |
| 115 | + } |
| 116 | + class Method2NotFoundException extends Exception { |
| 117 | + } |
| 118 | + class ClassLevelNotFoundException extends Exception { |
| 119 | + } |
| 120 | + class UnauthenticatedOrUnauthorizedException extends Exception { |
| 121 | + } |
| 122 | + class ServeErrorException extends Exception { |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + abstract class Child implements ParentInterfaceWithErrorHandling { |
| 127 | + } |
| 128 | + |
| 129 | + abstract class GrandChild extends Child { |
| 130 | + } |
| 131 | +} |
0 commit comments