|
21 | 21 | import io.grpc.BindableService; |
22 | 22 | import io.grpc.ServerServiceDefinition; |
23 | 23 | import io.grpc.ServiceDescriptor; |
| 24 | +import io.grpc.Status; |
| 25 | +import io.grpc.StatusException; |
24 | 26 | import io.grpc.inprocess.InProcessServerBuilder; |
25 | 27 | import io.grpc.internal.GrpcUtil; |
26 | 28 | import io.grpc.netty.NettyServerBuilder; |
|
36 | 38 | import org.springframework.boot.autoconfigure.AutoConfigurations; |
37 | 39 | import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration; |
38 | 40 | import org.springframework.boot.grpc.server.GrpcServletRegistration; |
| 41 | +import org.springframework.boot.grpc.server.autoconfigure.GrpcServerAutoConfiguration.GrpcAdviceConfiguration; |
39 | 42 | import org.springframework.boot.test.context.FilteredClassLoader; |
40 | 43 | import org.springframework.boot.test.context.assertj.ApplicationContextAssertProvider; |
41 | 44 | import org.springframework.boot.test.context.runner.AbstractApplicationContextRunner; |
|
51 | 54 | import org.springframework.grpc.server.NettyGrpcServerFactory; |
52 | 55 | import org.springframework.grpc.server.ServerBuilderCustomizer; |
53 | 56 | import org.springframework.grpc.server.ShadedNettyGrpcServerFactory; |
| 57 | +import org.springframework.grpc.server.advice.GrpcAdvice; |
| 58 | +import org.springframework.grpc.server.advice.GrpcAdviceDiscoverer; |
| 59 | +import org.springframework.grpc.server.advice.GrpcAdviceExceptionHandler; |
| 60 | +import org.springframework.grpc.server.advice.GrpcExceptionHandler; |
| 61 | +import org.springframework.grpc.server.advice.GrpcExceptionHandlerMethodResolver; |
54 | 62 | import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle; |
55 | 63 | import org.springframework.grpc.server.service.DefaultGrpcServiceDiscoverer; |
56 | 64 | import org.springframework.grpc.server.service.GrpcServiceDiscoverer; |
@@ -469,6 +477,43 @@ void whenHttp2EnabledPropertyMissingAndValidationDisabled() { |
469 | 477 |
|
470 | 478 | } |
471 | 479 |
|
| 480 | + @Nested |
| 481 | + class GrpcAdviceAutoConfigurationTests { |
| 482 | + |
| 483 | + private final ApplicationContextRunner contextRunner = GrpcServerAutoConfigurationTests.this.contextRunner; |
| 484 | + |
| 485 | + @Test |
| 486 | + void whenNoAdviceDoesNotCreateBeans() { |
| 487 | + this.contextRunner.run((context) -> { |
| 488 | + assertThat(context).doesNotHaveBean(GrpcAdviceConfiguration.class); |
| 489 | + assertThat(context).doesNotHaveBean(GrpcAdviceDiscoverer.class); |
| 490 | + assertThat(context).doesNotHaveBean(GrpcExceptionHandlerMethodResolver.class); |
| 491 | + assertThat(context).doesNotHaveBean(GrpcAdviceExceptionHandler.class); |
| 492 | + }); |
| 493 | + } |
| 494 | + |
| 495 | + @Test |
| 496 | + void whenHasAdviceCreatesBeans() { |
| 497 | + this.contextRunner.withBean(GrpAdviceComponent.class).run((context) -> { |
| 498 | + assertThat(context).hasSingleBean(GrpcAdviceConfiguration.class); |
| 499 | + assertThat(context).hasSingleBean(GrpcAdviceDiscoverer.class); |
| 500 | + assertThat(context).hasSingleBean(GrpcExceptionHandlerMethodResolver.class); |
| 501 | + assertThat(context).hasSingleBean(GrpcAdviceExceptionHandler.class); |
| 502 | + }); |
| 503 | + } |
| 504 | + |
| 505 | + @GrpcAdvice |
| 506 | + static class GrpAdviceComponent { |
| 507 | + |
| 508 | + @GrpcExceptionHandler |
| 509 | + StatusException onIllegalStateException(IllegalStateException ex) { |
| 510 | + return Status.FAILED_PRECONDITION.withDescription(ex.getMessage()).withCause(ex).asException(); |
| 511 | + } |
| 512 | + |
| 513 | + } |
| 514 | + |
| 515 | + } |
| 516 | + |
472 | 517 | @Configuration(proxyBeanMethods = false) |
473 | 518 | static class ServerBuilderCustomizersConfig { |
474 | 519 |
|
|
0 commit comments