forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendToMethodReturnValueHandlerTests.java
337 lines (252 loc) · 11.9 KB
/
SendToMethodReturnValueHandlerTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Copyright 2002-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.messaging.simp.annotation.support;
import java.lang.reflect.Method;
import java.security.Principal;
import javax.security.auth.Subject;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.core.MethodParameter;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.messaging.simp.user.DestinationUserNameProvider;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.messaging.converter.MessageConverter;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
/**
* Test fixture for {@link SendToMethodReturnValueHandlerTests}.
*
* @author Rossen Stoyanchev
*/
public class SendToMethodReturnValueHandlerTests {
private static final String payloadContent = "payload";
private SendToMethodReturnValueHandler handler;
private SendToMethodReturnValueHandler handlerAnnotationNotRequired;
@Mock private MessageChannel messageChannel;
@Captor ArgumentCaptor<Message<?>> messageCaptor;
@Mock private MessageConverter messageConverter;
private MethodParameter noAnnotationsReturnType;
private MethodParameter sendToReturnType;
private MethodParameter sendToDefaultDestReturnType;
private MethodParameter sendToUserReturnType;
private MethodParameter sendToUserDefaultDestReturnType;
private MethodParameter sendToUserOnSessionReturnType;
@Before
@SuppressWarnings({ "unchecked", "rawtypes" })
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
Message message = MessageBuilder.withPayload(payloadContent).build();
when(this.messageConverter.toMessage(payloadContent, null)).thenReturn(message);
SimpMessagingTemplate messagingTemplate = new SimpMessagingTemplate(this.messageChannel);
messagingTemplate.setMessageConverter(this.messageConverter);
this.handler = new SendToMethodReturnValueHandler(messagingTemplate, true);
this.handlerAnnotationNotRequired = new SendToMethodReturnValueHandler(messagingTemplate, false);
Method method = this.getClass().getDeclaredMethod("handleNoAnnotations");
this.noAnnotationsReturnType = new MethodParameter(method, -1);
method = this.getClass().getDeclaredMethod("handleAndSendToDefaultDestination");
this.sendToDefaultDestReturnType = new MethodParameter(method, -1);
method = this.getClass().getDeclaredMethod("handleAndSendTo");
this.sendToReturnType = new MethodParameter(method, -1);
method = this.getClass().getDeclaredMethod("handleAndSendToUser");
this.sendToUserReturnType = new MethodParameter(method, -1);
method = this.getClass().getDeclaredMethod("handleAndSendToUserOnSession");
this.sendToUserOnSessionReturnType = new MethodParameter(method, -1);
method = this.getClass().getDeclaredMethod("handleAndSendToUserDefaultDestination");
this.sendToUserDefaultDestReturnType = new MethodParameter(method, -1);
}
@Test
public void supportsReturnType() throws Exception {
assertTrue(this.handler.supportsReturnType(this.sendToReturnType));
assertTrue(this.handler.supportsReturnType(this.sendToUserReturnType));
assertFalse(this.handler.supportsReturnType(this.noAnnotationsReturnType));
assertTrue(this.handlerAnnotationNotRequired.supportsReturnType(this.noAnnotationsReturnType));
}
@Test
public void sendToNoAnnotations() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
Message<?> inputMessage = createInputMessage("sess1", "sub1", "/dest", null);
this.handler.handleReturnValue(payloadContent, this.noAnnotationsReturnType, inputMessage);
verify(this.messageChannel, times(1)).send(this.messageCaptor.capture());
Message<?> message = this.messageCaptor.getAllValues().get(0);
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
assertEquals("sess1", headers.getSessionId());
assertNull(headers.getSubscriptionId());
assertNull(headers.isSessionFiltered());
assertEquals("/topic/dest", headers.getDestination());
}
@Test
public void sendTo() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
String sessionId = "sess1";
Message<?> inputMessage = createInputMessage(sessionId, "sub1", null, null);
this.handler.handleReturnValue(payloadContent, this.sendToReturnType, inputMessage);
verify(this.messageChannel, times(2)).send(this.messageCaptor.capture());
Message<?> message = this.messageCaptor.getAllValues().get(0);
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
assertEquals(sessionId, headers.getSessionId());
assertNull(headers.getSubscriptionId());
assertEquals("/dest1", headers.getDestination());
message = this.messageCaptor.getAllValues().get(1);
headers = SimpMessageHeaderAccessor.wrap(message);
assertEquals(sessionId, headers.getSessionId());
assertNull(headers.getSubscriptionId());
assertNull(headers.isSessionFiltered());
assertEquals("/dest2", headers.getDestination());
}
@Test
public void sendToDefaultDestination() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
String sessionId = "sess1";
Message<?> inputMessage = createInputMessage(sessionId, "sub1", "/dest", null);
this.handler.handleReturnValue(payloadContent, this.sendToDefaultDestReturnType, inputMessage);
verify(this.messageChannel, times(1)).send(this.messageCaptor.capture());
Message<?> message = this.messageCaptor.getAllValues().get(0);
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
assertEquals(sessionId, headers.getSessionId());
assertNull(headers.getSubscriptionId());
assertNull(headers.isSessionFiltered());
assertEquals("/topic/dest", headers.getDestination());
}
@Test
public void sendToUser() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
String sessionId = "sess1";
TestUser user = new TestUser();
Message<?> inputMessage = createInputMessage(sessionId, "sub1", null, user);
this.handler.handleReturnValue(payloadContent, this.sendToUserReturnType, inputMessage);
verify(this.messageChannel, times(2)).send(this.messageCaptor.capture());
Message<?> message = this.messageCaptor.getAllValues().get(0);
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
assertEquals(sessionId, headers.getSessionId());
assertNull(headers.getSubscriptionId());
assertEquals("/user/" + user.getName() + "/dest1", headers.getDestination());
message = this.messageCaptor.getAllValues().get(1);
headers = SimpMessageHeaderAccessor.wrap(message);
assertEquals(sessionId, headers.getSessionId());
assertNull(headers.getSubscriptionId());
assertNull(headers.isSessionFiltered());
assertEquals("/user/" + user.getName() + "/dest2", headers.getDestination());
}
@Test
public void sendToUserWithUserNameProvider() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
String sessionId = "sess1";
TestUser user = new UniqueUser();
Message<?> inputMessage = createInputMessage(sessionId, "sub1", null, user);
this.handler.handleReturnValue(payloadContent, this.sendToUserReturnType, inputMessage);
verify(this.messageChannel, times(2)).send(this.messageCaptor.capture());
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(this.messageCaptor.getAllValues().get(0));
assertNull(headers.getSubscriptionId());
assertEquals("/user/Me myself and I/dest1", headers.getDestination());
headers = SimpMessageHeaderAccessor.wrap(this.messageCaptor.getAllValues().get(1));
assertNull(headers.getSubscriptionId());
assertNull(headers.isSessionFiltered());
assertEquals("/user/Me myself and I/dest2", headers.getDestination());
}
@Test
public void sendToUserDefaultDestination() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
String sessionId = "sess1";
TestUser user = new TestUser();
Message<?> inputMessage = createInputMessage(sessionId, "sub1", "/dest", user);
this.handler.handleReturnValue(payloadContent, this.sendToUserDefaultDestReturnType, inputMessage);
verify(this.messageChannel, times(1)).send(this.messageCaptor.capture());
Message<?> message = this.messageCaptor.getAllValues().get(0);
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
assertEquals(sessionId, headers.getSessionId());
assertNull(headers.getSubscriptionId());
assertNull(headers.isSessionFiltered());
assertEquals("/user/" + user.getName() + "/queue/dest", headers.getDestination());
}
@Test
public void sendToUserParticularSession() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
String sessionId = "sess1";
String subscriptionId = "subs1";
String destination = "/dest";
TestUser user = new TestUser();
Message<?> inputMessage = createInputMessage(sessionId, subscriptionId, destination, user);
this.handler.handleReturnValue(payloadContent, this.sendToUserOnSessionReturnType, inputMessage);
verify(this.messageChannel).send(this.messageCaptor.capture());
assertNotNull(this.messageCaptor.getValue());
verify(this.messageChannel, times(1)).send(this.messageCaptor.capture());
Message<?> message = this.messageCaptor.getAllValues().get(0);
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
assertEquals("sessionId should always be copied", sessionId, headers.getSessionId());
assertEquals(subscriptionId, headers.getSubscriptionId());
assertTrue(headers.isSessionFiltered());
assertEquals("/user/" + user.getName() + "/dest1", headers.getDestination());
}
private Message<?> createInputMessage(String sessId, String subsId, String destination, Principal principal) {
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
headers.setSessionId(sessId);
headers.setSubscriptionId(subsId);
if (destination != null) {
headers.setDestination(destination);
}
if (principal != null) {
headers.setUser(principal);
}
return MessageBuilder.withPayload(new byte[0]).copyHeaders(headers.toMap()).build();
}
private static class TestUser implements Principal {
public String getName() {
return "joe";
}
public boolean implies(Subject subject) {
return false;
}
}
private static class UniqueUser extends TestUser implements DestinationUserNameProvider {
@Override
public String getDestinationUserName() {
return "Me myself and I";
}
}
public String handleNoAnnotations() {
return payloadContent;
}
@SendTo
public String handleAndSendToDefaultDestination() {
return payloadContent;
}
@SendTo({"/dest1", "/dest2"})
public String handleAndSendTo() {
return payloadContent;
}
@SendToUser
public String handleAndSendToUserDefaultDestination() {
return payloadContent;
}
@SendToUser(value={"/dest1"}, onSession=true)
public String handleAndSendToUserOnSession() {
return payloadContent;
}
@SendToUser({"/dest1", "/dest2"})
public String handleAndSendToUser() {
return payloadContent;
}
}