|
| 1 | + |
| 2 | +package net.devh.boot.grpc.server.advice; |
| 3 | + |
| 4 | +import static java.util.Collections.singletonMap; |
| 5 | +import static org.assertj.core.api.Assertions.assertThat; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | +import static org.mockito.Mockito.reset; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import java.lang.reflect.Method; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.springframework.context.ApplicationContext; |
| 15 | + |
| 16 | +import io.grpc.Status; |
| 17 | + |
| 18 | +/** |
| 19 | + * Tests for {@link GrpcAdviceDiscoverer}. |
| 20 | + */ |
| 21 | +class GrpcAdviceDiscovererTest { |
| 22 | + |
| 23 | + private final ApplicationContext context = mock(ApplicationContext.class); |
| 24 | + |
| 25 | + @BeforeEach |
| 26 | + void beforeEach() { |
| 27 | + reset(this.context); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Tests that the {@link GrpcAdviceDiscoverer} discovers inherited methods. |
| 32 | + */ |
| 33 | + @Test |
| 34 | + void testDiscoversInheritedMethods() { |
| 35 | + when(this.context.getBeansWithAnnotation(GrpcAdvice.class)) |
| 36 | + .thenReturn(singletonMap("bean", new Extended())); |
| 37 | + |
| 38 | + final GrpcAdviceDiscoverer disco = new GrpcAdviceDiscoverer(); |
| 39 | + disco.setApplicationContext(this.context); |
| 40 | + disco.afterPropertiesSet(); |
| 41 | + |
| 42 | + assertThat(disco.getAnnotatedMethods()) |
| 43 | + .containsExactlyInAnyOrder( |
| 44 | + findMethod(Base.class, "handleRuntimeException", RuntimeException.class), |
| 45 | + findMethod(Extended.class, "handleIllegalArgument", IllegalArgumentException.class)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void testOverriddenMethods() { |
| 50 | + when(this.context.getBeansWithAnnotation(GrpcAdvice.class)) |
| 51 | + .thenReturn(singletonMap("bean", new Overriden())); |
| 52 | + |
| 53 | + final GrpcAdviceDiscoverer disco = new GrpcAdviceDiscoverer(); |
| 54 | + disco.setApplicationContext(this.context); |
| 55 | + disco.afterPropertiesSet(); |
| 56 | + |
| 57 | + assertThat(disco.getAnnotatedMethods()) |
| 58 | + .containsExactly(findMethod(Overriden.class, "handleRuntimeException", RuntimeException.class)); |
| 59 | + } |
| 60 | + |
| 61 | + private static Method findMethod(final Class<?> clazz, final String method, final Class<?>... parameters) { |
| 62 | + try { |
| 63 | + return clazz.getDeclaredMethod(method, parameters); |
| 64 | + } catch (NoSuchMethodException | SecurityException e) { |
| 65 | + throw new IllegalStateException("Failed to find method", e); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @GrpcAdvice |
| 70 | + private class Base { |
| 71 | + |
| 72 | + @GrpcExceptionHandler |
| 73 | + Status handleRuntimeException(final RuntimeException e) { |
| 74 | + return Status.INTERNAL; |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + @GrpcAdvice |
| 80 | + private class Extended extends Base { |
| 81 | + |
| 82 | + @GrpcExceptionHandler |
| 83 | + Status handleIllegalArgument(final IllegalArgumentException e) { |
| 84 | + return Status.INVALID_ARGUMENT; |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + @GrpcAdvice |
| 90 | + private class Overriden extends Base { |
| 91 | + |
| 92 | + @Override |
| 93 | + @GrpcExceptionHandler |
| 94 | + Status handleRuntimeException(final RuntimeException e) { |
| 95 | + return Status.INVALID_ARGUMENT; |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments