Search in sources :

Example 1 with SimpSubscription

use of org.springframework.messaging.simp.user.SimpSubscription in project spring-framework by spring-projects.

the class DefaultSimpUserRegistryTests method findSubscriptions.

@Test
public void findSubscriptions() throws Exception {
    DefaultSimpUserRegistry registry = new DefaultSimpUserRegistry();
    TestPrincipal user = new TestPrincipal("joe");
    Message<byte[]> message = createMessage(SimpMessageType.CONNECT_ACK, "123");
    SessionConnectedEvent event = new SessionConnectedEvent(this, message, user);
    registry.onApplicationEvent(event);
    message = createMessage(SimpMessageType.SUBSCRIBE, "123", "sub1", "/match");
    SessionSubscribeEvent subscribeEvent = new SessionSubscribeEvent(this, message, user);
    registry.onApplicationEvent(subscribeEvent);
    message = createMessage(SimpMessageType.SUBSCRIBE, "123", "sub2", "/match");
    subscribeEvent = new SessionSubscribeEvent(this, message, user);
    registry.onApplicationEvent(subscribeEvent);
    message = createMessage(SimpMessageType.SUBSCRIBE, "123", "sub3", "/not-a-match");
    subscribeEvent = new SessionSubscribeEvent(this, message, user);
    registry.onApplicationEvent(subscribeEvent);
    Set<SimpSubscription> matches = registry.findSubscriptions(subscription -> subscription.getDestination().equals("/match"));
    assertThat(matches.size()).isEqualTo(2);
    Iterator<SimpSubscription> iterator = matches.iterator();
    Set<String> sessionIds = new HashSet<>(2);
    sessionIds.add(iterator.next().getId());
    sessionIds.add(iterator.next().getId());
    assertThat(sessionIds).isEqualTo(new HashSet<>(Arrays.asList("sub1", "sub2")));
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) SimpSubscription(org.springframework.messaging.simp.user.SimpSubscription) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

HashSet (java.util.HashSet)1 Test (org.junit.jupiter.api.Test)1 TestPrincipal (org.springframework.core.testfixture.security.TestPrincipal)1 SimpSubscription (org.springframework.messaging.simp.user.SimpSubscription)1