Polish "user" destination support package
Issue: SPR-11620
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.messaging.simp.user;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -26,8 +28,6 @@ import org.springframework.messaging.simp.TestPrincipal;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Unit tests for
|
||||
* {@link org.springframework.messaging.simp.user.DefaultUserDestinationResolver}.
|
||||
@@ -57,7 +57,7 @@ public class DefaultUserDestinationResolverTests {
|
||||
@Test
|
||||
public void handleSubscribe() {
|
||||
String sourceDestination = "/user/queue/foo";
|
||||
Message<?> message = createMessage(SimpMessageType.SUBSCRIBE, this.user, SESSION_ID, sourceDestination);
|
||||
Message<?> message = createWith(SimpMessageType.SUBSCRIBE, this.user, SESSION_ID, sourceDestination);
|
||||
UserDestinationResult actual = this.resolver.resolveDestination(message);
|
||||
|
||||
assertEquals(sourceDestination, actual.getSourceDestination());
|
||||
@@ -75,7 +75,7 @@ public class DefaultUserDestinationResolverTests {
|
||||
this.registry.registerSessionId("joe", "456");
|
||||
this.registry.registerSessionId("joe", "789");
|
||||
|
||||
Message<?> message = createMessage(SimpMessageType.SUBSCRIBE, this.user, SESSION_ID, "/user/queue/foo");
|
||||
Message<?> message = createWith(SimpMessageType.SUBSCRIBE, this.user, SESSION_ID, "/user/queue/foo");
|
||||
UserDestinationResult actual = this.resolver.resolveDestination(message);
|
||||
|
||||
assertEquals(1, actual.getTargetDestinations().size());
|
||||
@@ -85,7 +85,7 @@ public class DefaultUserDestinationResolverTests {
|
||||
@Test
|
||||
public void handleSubscribeNoUser() {
|
||||
String sourceDestination = "/user/queue/foo";
|
||||
Message<?> message = createMessage(SimpMessageType.SUBSCRIBE, null, SESSION_ID, sourceDestination);
|
||||
Message<?> message = createWith(SimpMessageType.SUBSCRIBE, null, SESSION_ID, sourceDestination);
|
||||
UserDestinationResult actual = this.resolver.resolveDestination(message);
|
||||
|
||||
assertEquals(sourceDestination, actual.getSourceDestination());
|
||||
@@ -97,7 +97,7 @@ public class DefaultUserDestinationResolverTests {
|
||||
|
||||
@Test
|
||||
public void handleUnsubscribe() {
|
||||
Message<?> message = createMessage(SimpMessageType.UNSUBSCRIBE, this.user, SESSION_ID, "/user/queue/foo");
|
||||
Message<?> message = createWith(SimpMessageType.UNSUBSCRIBE, this.user, SESSION_ID, "/user/queue/foo");
|
||||
UserDestinationResult actual = this.resolver.resolveDestination(message);
|
||||
|
||||
assertEquals(1, actual.getTargetDestinations().size());
|
||||
@@ -107,7 +107,7 @@ public class DefaultUserDestinationResolverTests {
|
||||
@Test
|
||||
public void handleMessage() {
|
||||
String sourceDestination = "/user/joe/queue/foo";
|
||||
Message<?> message = createMessage(SimpMessageType.MESSAGE, this.user, SESSION_ID, sourceDestination);
|
||||
Message<?> message = createWith(SimpMessageType.MESSAGE, this.user, SESSION_ID, sourceDestination);
|
||||
UserDestinationResult actual = this.resolver.resolveDestination(message);
|
||||
|
||||
assertEquals(sourceDestination, actual.getSourceDestination());
|
||||
@@ -126,7 +126,7 @@ public class DefaultUserDestinationResolverTests {
|
||||
String sourceDestination = "/user/"+OTHER_USER_NAME+"/queue/foo";
|
||||
TestPrincipal otherUser = new TestPrincipal(OTHER_USER_NAME);
|
||||
this.registry.registerSessionId(otherUser.getName(), OTHER_SESSION_ID);
|
||||
Message<?> message = createMessage(SimpMessageType.MESSAGE, this.user, SESSION_ID, sourceDestination);
|
||||
Message<?> message = createWith(SimpMessageType.MESSAGE, this.user, SESSION_ID, sourceDestination);
|
||||
UserDestinationResult actual = this.resolver.resolveDestination(message);
|
||||
|
||||
assertEquals(sourceDestination, actual.getSourceDestination());
|
||||
@@ -142,7 +142,7 @@ public class DefaultUserDestinationResolverTests {
|
||||
String userName = "http://joe.openid.example.org/";
|
||||
this.registry.registerSessionId(userName, "openid123");
|
||||
String destination = "/user/" + StringUtils.replace(userName, "/", "%2F") + "/queue/foo";
|
||||
Message<?> message = createMessage(SimpMessageType.MESSAGE, this.user, null, destination);
|
||||
Message<?> message = createWith(SimpMessageType.MESSAGE, this.user, null, destination);
|
||||
UserDestinationResult actual = this.resolver.resolveDestination(message);
|
||||
|
||||
assertEquals(1, actual.getTargetDestinations().size());
|
||||
@@ -152,7 +152,7 @@ public class DefaultUserDestinationResolverTests {
|
||||
@Test
|
||||
public void handleMessageWithNoUser() {
|
||||
String sourceDestination = "/user/" + SESSION_ID + "/queue/foo";
|
||||
Message<?> message = createMessage(SimpMessageType.MESSAGE, null, SESSION_ID, sourceDestination);
|
||||
Message<?> message = createWith(SimpMessageType.MESSAGE, null, SESSION_ID, sourceDestination);
|
||||
UserDestinationResult actual = this.resolver.resolveDestination(message);
|
||||
|
||||
assertEquals(sourceDestination, actual.getSourceDestination());
|
||||
@@ -166,29 +166,29 @@ public class DefaultUserDestinationResolverTests {
|
||||
public void ignoreMessage() {
|
||||
|
||||
// no destination
|
||||
Message<?> message = createMessage(SimpMessageType.MESSAGE, this.user, SESSION_ID, null);
|
||||
Message<?> message = createWith(SimpMessageType.MESSAGE, this.user, SESSION_ID, null);
|
||||
UserDestinationResult actual = this.resolver.resolveDestination(message);
|
||||
assertNull(actual);
|
||||
|
||||
// not a user destination
|
||||
message = createMessage(SimpMessageType.MESSAGE, this.user, SESSION_ID, "/queue/foo");
|
||||
message = createWith(SimpMessageType.MESSAGE, this.user, SESSION_ID, "/queue/foo");
|
||||
actual = this.resolver.resolveDestination(message);
|
||||
assertNull(actual);
|
||||
|
||||
// subscribe + not a user destination
|
||||
message = createMessage(SimpMessageType.SUBSCRIBE, this.user, SESSION_ID, "/queue/foo");
|
||||
message = createWith(SimpMessageType.SUBSCRIBE, this.user, SESSION_ID, "/queue/foo");
|
||||
actual = this.resolver.resolveDestination(message);
|
||||
assertNull(actual);
|
||||
|
||||
// no match on message type
|
||||
message = createMessage(SimpMessageType.CONNECT, this.user, SESSION_ID, "user/joe/queue/foo");
|
||||
message = createWith(SimpMessageType.CONNECT, this.user, SESSION_ID, "user/joe/queue/foo");
|
||||
actual = this.resolver.resolveDestination(message);
|
||||
assertNull(actual);
|
||||
}
|
||||
|
||||
|
||||
private Message<?> createMessage(SimpMessageType messageType, TestPrincipal user, String sessionId, String destination) {
|
||||
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(messageType);
|
||||
private Message<?> createWith(SimpMessageType type, TestPrincipal user, String sessionId, String destination) {
|
||||
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(type);
|
||||
if (destination != null) {
|
||||
headers.setDestination(destination);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.messaging.simp.user;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
@@ -23,10 +25,9 @@ import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link org.springframework.messaging.simp.user.DefaultUserSessionRegistry}
|
||||
* Test fixture for
|
||||
* {@link org.springframework.messaging.simp.user.DefaultUserSessionRegistry}
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
@@ -57,10 +58,9 @@ public class DefaultUserSessionRegistryTests {
|
||||
}
|
||||
|
||||
assertEquals(new LinkedHashSet<>(sessionIds), resolver.getSessionIds(user));
|
||||
assertEquals(Collections.emptySet(), resolver.getSessionIds("jane"));
|
||||
assertEquals(Collections.<String>emptySet(), resolver.getSessionIds("jane"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void removeSessionIds() {
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,6 +16,10 @@
|
||||
|
||||
package org.springframework.messaging.simp.user;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
import static org.springframework.messaging.simp.SimpMessageHeaderAccessor.ORIGINAL_DESTINATION;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
@@ -31,30 +35,29 @@ import org.springframework.messaging.simp.SimpMessageType;
|
||||
import org.springframework.messaging.simp.TestPrincipal;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link org.springframework.messaging.simp.user.UserDestinationMessageHandler}.
|
||||
* Unit tests for
|
||||
* {@link org.springframework.messaging.simp.user.UserDestinationMessageHandler}.
|
||||
*/
|
||||
public class UserDestinationMessageHandlerTests {
|
||||
|
||||
public static final String SESSION_ID = "123";
|
||||
private UserDestinationMessageHandler messageHandler;
|
||||
private static final String SESSION_ID = "123";
|
||||
|
||||
|
||||
private UserDestinationMessageHandler handler;
|
||||
|
||||
private UserSessionRegistry registry;
|
||||
|
||||
@Mock
|
||||
private SubscribableChannel brokerChannel;
|
||||
|
||||
private UserSessionRegistry registry;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
this.registry = new DefaultUserSessionRegistry();
|
||||
DefaultUserDestinationResolver resolver = new DefaultUserDestinationResolver(this.registry);
|
||||
this.messageHandler = new UserDestinationMessageHandler(new StubMessageChannel(), this.brokerChannel, resolver);
|
||||
UserDestinationResolver resolver = new DefaultUserDestinationResolver(this.registry);
|
||||
this.handler = new UserDestinationMessageHandler(new StubMessageChannel(), this.brokerChannel, resolver);
|
||||
}
|
||||
|
||||
|
||||
@@ -62,24 +65,26 @@ public class UserDestinationMessageHandlerTests {
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void handleSubscribe() {
|
||||
given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
|
||||
this.messageHandler.handleMessage(createMessage(SimpMessageType.SUBSCRIBE, "joe", SESSION_ID, "/user/queue/foo"));
|
||||
this.handler.handleMessage(createWith(SimpMessageType.SUBSCRIBE, "joe", SESSION_ID, "/user/queue/foo"));
|
||||
|
||||
ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
|
||||
Mockito.verify(this.brokerChannel).send(captor.capture());
|
||||
|
||||
assertEquals("/queue/foo-user123", SimpMessageHeaderAccessor.getDestination(captor.getValue().getHeaders()));
|
||||
Message message = captor.getValue();
|
||||
assertEquals("/queue/foo-user123", SimpMessageHeaderAccessor.getDestination(message.getHeaders()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void handleUnsubscribe() {
|
||||
given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
|
||||
this.messageHandler.handleMessage(createMessage(SimpMessageType.UNSUBSCRIBE, "joe", "123", "/user/queue/foo"));
|
||||
this.handler.handleMessage(createWith(SimpMessageType.UNSUBSCRIBE, "joe", "123", "/user/queue/foo"));
|
||||
|
||||
ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
|
||||
Mockito.verify(this.brokerChannel).send(captor.capture());
|
||||
|
||||
assertEquals("/queue/foo-user123", SimpMessageHeaderAccessor.getDestination(captor.getValue().getHeaders()));
|
||||
Message message = captor.getValue();
|
||||
assertEquals("/queue/foo-user123", SimpMessageHeaderAccessor.getDestination(message.getHeaders()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -87,14 +92,14 @@ public class UserDestinationMessageHandlerTests {
|
||||
public void handleMessage() {
|
||||
this.registry.registerSessionId("joe", "123");
|
||||
given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
|
||||
this.messageHandler.handleMessage(createMessage(SimpMessageType.MESSAGE, "joe", "123", "/user/joe/queue/foo"));
|
||||
this.handler.handleMessage(createWith(SimpMessageType.MESSAGE, "joe", "123", "/user/joe/queue/foo"));
|
||||
|
||||
ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
|
||||
Mockito.verify(this.brokerChannel).send(captor.capture());
|
||||
|
||||
SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(captor.getValue());
|
||||
assertEquals("/queue/foo-user123", accessor.getDestination());
|
||||
assertEquals("/user/queue/foo", accessor.getFirstNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION));
|
||||
assertEquals("/user/queue/foo", accessor.getFirstNativeHeader(ORIGINAL_DESTINATION));
|
||||
}
|
||||
|
||||
|
||||
@@ -102,25 +107,25 @@ public class UserDestinationMessageHandlerTests {
|
||||
public void ignoreMessage() {
|
||||
|
||||
// no destination
|
||||
this.messageHandler.handleMessage(createMessage(SimpMessageType.MESSAGE, "joe", "123", null));
|
||||
this.handler.handleMessage(createWith(SimpMessageType.MESSAGE, "joe", "123", null));
|
||||
Mockito.verifyZeroInteractions(this.brokerChannel);
|
||||
|
||||
// not a user destination
|
||||
this.messageHandler.handleMessage(createMessage(SimpMessageType.MESSAGE, "joe", "123", "/queue/foo"));
|
||||
this.handler.handleMessage(createWith(SimpMessageType.MESSAGE, "joe", "123", "/queue/foo"));
|
||||
Mockito.verifyZeroInteractions(this.brokerChannel);
|
||||
|
||||
// subscribe + not a user destination
|
||||
this.messageHandler.handleMessage(createMessage(SimpMessageType.SUBSCRIBE, "joe", "123", "/queue/foo"));
|
||||
this.handler.handleMessage(createWith(SimpMessageType.SUBSCRIBE, "joe", "123", "/queue/foo"));
|
||||
Mockito.verifyZeroInteractions(this.brokerChannel);
|
||||
|
||||
// no match on message type
|
||||
this.messageHandler.handleMessage(createMessage(SimpMessageType.CONNECT, "joe", "123", "user/joe/queue/foo"));
|
||||
this.handler.handleMessage(createWith(SimpMessageType.CONNECT, "joe", "123", "user/joe/queue/foo"));
|
||||
Mockito.verifyZeroInteractions(this.brokerChannel);
|
||||
}
|
||||
|
||||
|
||||
private Message<?> createMessage(SimpMessageType messageType, String user, String sessionId, String destination) {
|
||||
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(messageType);
|
||||
private Message<?> createWith(SimpMessageType type, String user, String sessionId, String destination) {
|
||||
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(type);
|
||||
if (destination != null) {
|
||||
headers.setDestination(destination);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user