|
16 | 16 |
|
17 | 17 | package org.springframework.boot.rsocket.autoconfigure; |
18 | 18 |
|
| 19 | +import io.rsocket.frame.FrameType; |
19 | 20 | import org.junit.jupiter.api.Test; |
| 21 | +import reactor.core.publisher.Mono; |
| 22 | +import reactor.test.StepVerifier; |
20 | 23 |
|
21 | 24 | import org.springframework.boot.autoconfigure.AutoConfigurations; |
22 | 25 | import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
23 | 26 | import org.springframework.context.annotation.Bean; |
24 | 27 | import org.springframework.context.annotation.Configuration; |
25 | 28 | import org.springframework.core.codec.CharSequenceEncoder; |
26 | 29 | import org.springframework.core.codec.StringDecoder; |
| 30 | +import org.springframework.messaging.Message; |
| 31 | +import org.springframework.messaging.handler.DestinationPatternsMessageCondition; |
| 32 | +import org.springframework.messaging.handler.annotation.MessageExceptionHandler; |
| 33 | +import org.springframework.messaging.handler.annotation.MessageMapping; |
27 | 34 | import org.springframework.messaging.rsocket.RSocketStrategies; |
| 35 | +import org.springframework.messaging.rsocket.annotation.support.RSocketFrameTypeMessageCondition; |
28 | 36 | import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler; |
| 37 | +import org.springframework.messaging.support.MessageBuilder; |
| 38 | +import org.springframework.messaging.support.MessageHeaderAccessor; |
| 39 | +import org.springframework.stereotype.Controller; |
29 | 40 | import org.springframework.util.MimeType; |
| 41 | +import org.springframework.util.RouteMatcher; |
| 42 | +import org.springframework.web.bind.annotation.ControllerAdvice; |
30 | 43 |
|
31 | 44 | import static org.assertj.core.api.Assertions.assertThat; |
32 | 45 |
|
@@ -72,6 +85,22 @@ void shouldApplyMessageHandlerCustomizers() { |
72 | 85 | }); |
73 | 86 | } |
74 | 87 |
|
| 88 | + @Test |
| 89 | + void shouldRegisterControllerAdvice() { |
| 90 | + this.contextRunner.withBean(TestControllerAdvice.class).withBean(TestController.class).run((context) -> { |
| 91 | + RSocketMessageHandler handler = context.getBean(RSocketMessageHandler.class); |
| 92 | + TestControllerAdvice controllerAdvice = context.getBean(TestControllerAdvice.class); |
| 93 | + |
| 94 | + MessageHeaderAccessor headers = new MessageHeaderAccessor(); |
| 95 | + RouteMatcher.Route route = handler.getRouteMatcher().parseRoute("exception"); |
| 96 | + headers.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, route); |
| 97 | + headers.setHeader(RSocketFrameTypeMessageCondition.FRAME_TYPE_HEADER, FrameType.REQUEST_FNF); |
| 98 | + Message<?> message = MessageBuilder.createMessage(Mono.empty(), headers.getMessageHeaders()); |
| 99 | + StepVerifier.create(handler.handleMessage(message)).expectComplete().verify(); |
| 100 | + assertThat(controllerAdvice.isExceptionHandled()).isTrue(); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
75 | 104 | @Configuration(proxyBeanMethods = false) |
76 | 105 | static class BaseConfiguration { |
77 | 106 |
|
@@ -111,4 +140,30 @@ RSocketMessageHandlerCustomizer customizer() { |
111 | 140 |
|
112 | 141 | } |
113 | 142 |
|
| 143 | + @Controller |
| 144 | + static final class TestController { |
| 145 | + |
| 146 | + @MessageMapping("exception") |
| 147 | + void handleWithSimulatedException() { |
| 148 | + throw new IllegalStateException("simulated exception"); |
| 149 | + } |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + @ControllerAdvice |
| 154 | + static final class TestControllerAdvice { |
| 155 | + |
| 156 | + boolean exceptionHandled; |
| 157 | + |
| 158 | + boolean isExceptionHandled() { |
| 159 | + return this.exceptionHandled; |
| 160 | + } |
| 161 | + |
| 162 | + @MessageExceptionHandler |
| 163 | + void handleException(IllegalStateException ex) { |
| 164 | + this.exceptionHandled = true; |
| 165 | + } |
| 166 | + |
| 167 | + } |
| 168 | + |
114 | 169 | } |
0 commit comments