Skip to content

Commit

Permalink
Enhance unit tests for status quo for SpEL 'selector' support in mess…
Browse files Browse the repository at this point in the history
…aging

See gh-30550
  • Loading branch information
sbrannen committed Jun 4, 2023
1 parent d08e408 commit cc50af0
Showing 1 changed file with 46 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void registerSubscriptionWithDestinationPatternRegex() {
}

@Test
void registerSubscriptionWithSelector() {
void registerSubscriptionWithSelectorHeaderEnabledByDefault() {
String sessionId1 = "sess01";
String sessionId2 = "sess02";
String sessionId3 = "sess03";
Expand All @@ -278,41 +278,70 @@ void registerSubscriptionWithSelector() {
accessor.setNativeHeader("foo", "bar");
Message<?> message = MessageBuilder.createMessage("", accessor.getMessageHeaders());

MultiValueMap<String, String> actual = this.registry.findSubscriptions(message);
assertThat(actual).hasSize(2);
MultiValueMap<String, String> subscriptions = this.registry.findSubscriptions(message);
assertThat(subscriptions).hasSize(2);

// Subscription #1 has a 'selector' header that DOES match.
assertThat(actual.get(sessionId1)).containsExactly(subscriptionId1);
assertThat(subscriptions.get(sessionId1)).containsExactly(subscriptionId1);
// Subscription #2 has a 'selector' header that does NOT match.
assertThat(actual.get(sessionId2)).isNull();
assertThat(subscriptions.get(sessionId2)).isNull();
// Subscription #3 does NOT have a 'selector' header, so it matches anyway.
assertThat(actual.get(sessionId3)).containsExactly(subscriptionId3);
assertThat(subscriptions.get(sessionId3)).containsExactly(subscriptionId3);

// Then try with message WITHOUT selected 'foo' header present

actual = this.registry.findSubscriptions(createMessage(destination));
subscriptions = this.registry.findSubscriptions(createMessage(destination));
assertThat(subscriptions).hasSize(1);
// Subscription #3 does NOT have a 'selector' header, so it matches anyway.
assertThat(actual.get(sessionId3)).containsExactly(subscriptionId3);
assertThat(subscriptions.get(sessionId3)).containsExactly(subscriptionId3);
}

@Test
void registerSubscriptionWithSelectorNotSupported() {
String sessionId = "sess01";
String subscriptionId = "subs01";
void registerSubscriptionWithSelectorHeaderDisabled() {
String sessionId1 = "sess01";
String sessionId2 = "sess02";
String sessionId3 = "sess03";
String subscriptionId1 = "subs01";
String subscriptionId2 = "subs02";
String subscriptionId3 = "subs02";
String destination = "/foo";
String selector = "headers.foo == 'bar'";
String selector1 = "headers.foo == 'bar'";
String selector2 = "headers.foo == 'enigma'";

// Explicitly disable selector header support
this.registry.setSelectorHeaderName(null);
this.registry.registerSubscription(subscribeMessage(sessionId, subscriptionId, destination, selector));

// Register subscription with matching selector header
this.registry.registerSubscription(subscribeMessage(sessionId1, subscriptionId1, destination, selector1));
// Register subscription with non-matching selector header
this.registry.registerSubscription(subscribeMessage(sessionId2, subscriptionId2, destination, selector2));
// Register subscription without a selector header
this.registry.registerSubscription(subscribeMessage(sessionId3, subscriptionId3, destination, null));

// First, try with message WITH selected 'foo' header present

SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
accessor.setDestination(destination);
accessor.setNativeHeader("foo", "bazz");
accessor.setNativeHeader("foo", "bar");
Message<?> message = MessageBuilder.createMessage("", accessor.getMessageHeaders());

MultiValueMap<String, String> actual = this.registry.findSubscriptions(message);
assertThat(actual).hasSize(1);
assertThat(actual.get(sessionId)).containsExactly(subscriptionId);
MultiValueMap<String, String> subscriptions = this.registry.findSubscriptions(message);

// 'selector' header is ignored, so all 3 subscriptions should be found
assertThat(subscriptions).hasSize(3);
assertThat(subscriptions.get(sessionId1)).containsExactly(subscriptionId1);
assertThat(subscriptions.get(sessionId2)).containsExactly(subscriptionId2);
assertThat(subscriptions.get(sessionId3)).containsExactly(subscriptionId3);

// Then try with message WITHOUT selected 'foo' header present

subscriptions = this.registry.findSubscriptions(createMessage(destination));

// 'selector' header is ignored, so all 3 subscriptions should be found
assertThat(subscriptions).hasSize(3);
assertThat(subscriptions.get(sessionId1)).containsExactly(subscriptionId1);
assertThat(subscriptions.get(sessionId2)).containsExactly(subscriptionId2);
assertThat(subscriptions.get(sessionId3)).containsExactly(subscriptionId3);
}

@Test // SPR-11931
Expand Down

0 comments on commit cc50af0

Please sign in to comment.