Skip to content

Commit

Permalink
Integration test status quo for SpEL 'selector' support in messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jun 4, 2023
1 parent cc50af0 commit 2f35e77
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.messaging.simp.broker.DefaultSubscriptionRegistry;
import org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.messaging.simp.stomp.StompCommand;
Expand All @@ -60,13 +61,15 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.InstanceOfAssertFactories.BOOLEAN;
import static org.assertj.core.api.InstanceOfAssertFactories.type;
import static org.mockito.Mockito.mock;

/**
* Tests for {@link WebSocketMessageBrokerConfigurationSupport}.
*
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
* @author Sam Brannen
*/
class WebSocketMessageBrokerConfigurationSupportTests {

Expand Down Expand Up @@ -161,6 +164,32 @@ void taskScheduler() {
assertThat(handler.getHeartbeatValue()).containsExactly(15000, 15000);
}

@Test
void selectorHeaderEnabledByDefault() {
ApplicationContext context = createContext(TestChannelConfig.class, TestConfigurer.class);
SimpleBrokerMessageHandler simpleBrokerMessageHandler = simpleBrokerMessageHandler(context);

assertThat(simpleBrokerMessageHandler.getSubscriptionRegistry())
.asInstanceOf(type(DefaultSubscriptionRegistry.class))
.extracting(DefaultSubscriptionRegistry::getSelectorHeaderName)
.isEqualTo("selector");
}

@Test
void selectorHeaderDisabled() {
ApplicationContext context = createContext(TestChannelConfig.class, SelectorHeaderConfigurer.class);
SimpleBrokerMessageHandler simpleBrokerMessageHandler = simpleBrokerMessageHandler(context);

assertThat(simpleBrokerMessageHandler.getSubscriptionRegistry())
.asInstanceOf(type(DefaultSubscriptionRegistry.class))
.extracting(DefaultSubscriptionRegistry::getSelectorHeaderName)
.isNull();
}

private static SimpleBrokerMessageHandler simpleBrokerMessageHandler(ApplicationContext context) {
return context.getBean("simpleBrokerMessageHandler", SimpleBrokerMessageHandler.class);
}

@Test
void webSocketMessageBrokerStats() {
ApplicationContext context = createContext(TestChannelConfig.class, TestConfigurer.class);
Expand Down Expand Up @@ -243,6 +272,22 @@ public void configureMessageBroker(MessageBrokerRegistry registry) {
}
}

@Configuration
static class SelectorHeaderConfigurer implements WebSocketMessageBrokerConfigurer {

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/simpleBroker");
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
// Explicitly disable selector header support
registry.enableSimpleBroker().setSelectorHeaderName(null);
}

}

@Configuration
static class TestChannelConfig extends DelegatingWebSocketMessageBrokerConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,15 @@ void sendMessageToControllerAndReceiveReplyViaTopic(WebSocketTestServer server,
}

@ParameterizedWebSocketTest // SPR-10930
void sendMessageToBrokerAndReceiveReplyViaTopic(WebSocketTestServer server, WebSocketClient webSocketClient, TestInfo testInfo) throws Exception {
void sendMessageToBrokerAndReceiveReplyViaTopicWithSelectorHeader(WebSocketTestServer server, WebSocketClient webSocketClient, TestInfo testInfo) throws Exception {
super.setup(server, webSocketClient, testInfo);

String destination = "destination:/topic/foo";
String selector = "selector:headers.foo == 'bar'";

TextMessage m0 = create(StompCommand.CONNECT).headers("accept-version:1.1").build();
TextMessage m1 = create(StompCommand.SUBSCRIBE).headers("id:subs1", "destination:/topic/foo").build();
TextMessage m2 = create(StompCommand.SEND).headers("destination:/topic/foo").body("5").build();
TextMessage m1 = create(StompCommand.SUBSCRIBE).headers("id:subs1", destination, selector).build();
TextMessage m2 = create(StompCommand.SEND).headers(destination, "foo:bar").body("5").build();

TestClientWebSocketHandler clientHandler = new TestClientWebSocketHandler(2, m0, m1, m2);

Expand Down

0 comments on commit 2f35e77

Please sign in to comment.