Search in sources :

Example 46 with Message

use of org.springframework.messaging.Message in project spring-framework by spring-projects.

the class SendToMethodReturnValueHandlerTests method sendToUserWithUserNameProvider.

@Test
public void sendToUserWithUserNameProvider() throws Exception {
    given(this.messageChannel.send(any(Message.class))).willReturn(true);
    String sessionId = "sess1";
    TestUser user = new UniqueUser();
    Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, user);
    this.handler.handleReturnValue(PAYLOAD, this.sendToUserReturnType, inputMessage);
    verify(this.messageChannel, times(2)).send(this.messageCaptor.capture());
    SimpMessageHeaderAccessor accessor = getCapturedAccessor(0);
    assertEquals("/user/Me myself and I/dest1", accessor.getDestination());
    accessor = getCapturedAccessor(1);
    assertEquals("/user/Me myself and I/dest2", accessor.getDestination());
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 47 with Message

use of org.springframework.messaging.Message in project spring-framework by spring-projects.

the class SendToMethodReturnValueHandlerTests method sendToUserDefaultDestination.

@Test
public void sendToUserDefaultDestination() throws Exception {
    given(this.messageChannel.send(any(Message.class))).willReturn(true);
    String sessionId = "sess1";
    TestUser user = new TestUser();
    Message<?> inputMessage = createMessage(sessionId, "sub1", "/app", "/dest", user);
    this.handler.handleReturnValue(PAYLOAD, this.sendToUserDefaultDestReturnType, inputMessage);
    verify(this.messageChannel, times(1)).send(this.messageCaptor.capture());
    SimpMessageHeaderAccessor accessor = getCapturedAccessor(0);
    assertNull(accessor.getSessionId());
    assertNull(accessor.getSubscriptionId());
    assertEquals("/user/" + user.getName() + "/queue/dest", accessor.getDestination());
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 48 with Message

use of org.springframework.messaging.Message in project spring-framework by spring-projects.

the class SimpAnnotationMethodMessageHandlerTests method completableFutureFailure.

@Test
@SuppressWarnings("unchecked")
public void completableFutureFailure() {
    Message emptyMessage = (Message) MessageBuilder.withPayload(new byte[0]).build();
    given(this.channel.send(any(Message.class))).willReturn(true);
    given(this.converter.toMessage(any(), any(MessageHeaders.class))).willReturn(emptyMessage);
    CompletableFutureController controller = new CompletableFutureController();
    this.messageHandler.registerHandler(controller);
    this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/"));
    Message<?> message = createMessage("/app1/completable-future");
    this.messageHandler.handleMessage(message);
    controller.future.completeExceptionally(new IllegalStateException());
    assertTrue(controller.exceptionCaught);
}
Also used : Message(org.springframework.messaging.Message) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 49 with Message

use of org.springframework.messaging.Message in project spring-framework by spring-projects.

the class SubscriptionMethodReturnValueHandlerTests method testMessageSentToChannel.

@Test
public void testMessageSentToChannel() throws Exception {
    given(this.messageChannel.send(any(Message.class))).willReturn(true);
    String sessionId = "sess1";
    String subscriptionId = "subs1";
    String destination = "/dest";
    Message<?> inputMessage = createInputMessage(sessionId, subscriptionId, destination, null);
    this.handler.handleReturnValue(PAYLOAD, this.subscribeEventReturnType, inputMessage);
    verify(this.messageChannel).send(this.messageCaptor.capture());
    assertNotNull(this.messageCaptor.getValue());
    Message<?> message = this.messageCaptor.getValue();
    SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.wrap(message);
    assertNull("SimpMessageHeaderAccessor should have disabled id", headerAccessor.getId());
    assertNull("SimpMessageHeaderAccessor should have disabled timestamp", headerAccessor.getTimestamp());
    assertEquals(sessionId, headerAccessor.getSessionId());
    assertEquals(subscriptionId, headerAccessor.getSubscriptionId());
    assertEquals(destination, headerAccessor.getDestination());
    assertEquals(MIME_TYPE, headerAccessor.getContentType());
    assertEquals(this.subscribeEventReturnType, headerAccessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER));
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 50 with Message

use of org.springframework.messaging.Message in project spring-framework by spring-projects.

the class MessageMethodArgumentResolverTests method resolveWithConversion.

@Test
public void resolveWithConversion() throws Exception {
    Message<String> message = MessageBuilder.withPayload("test").build();
    MethodParameter parameter = new MethodParameter(this.method, 1);
    when(this.converter.fromMessage(message, Integer.class)).thenReturn(4);
    @SuppressWarnings("unchecked") Message<Integer> actual = (Message<Integer>) this.resolver.resolveArgument(parameter, message);
    assertNotNull(actual);
    assertSame(message.getHeaders(), actual.getHeaders());
    assertEquals(new Integer(4), actual.getPayload());
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Aggregations

Message (org.springframework.messaging.Message)50 Test (org.junit.Test)41 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)16 MessageHeaders (org.springframework.messaging.MessageHeaders)13 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)9 BinaryMessage (org.springframework.web.socket.BinaryMessage)9 TextMessage (org.springframework.web.socket.TextMessage)9 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)6 ByteBuffer (java.nio.ByteBuffer)5 MessageChannel (org.springframework.messaging.MessageChannel)4 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)4 Method (java.lang.reflect.Method)3 MethodParameter (org.springframework.core.MethodParameter)3 TestPrincipal (org.springframework.messaging.simp.TestPrincipal)3 StompHeaders (org.springframework.messaging.simp.stomp.StompHeaders)3 PongMessage (org.springframework.web.socket.PongMessage)3 Principal (java.security.Principal)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 TextMessage (javax.jms.TextMessage)2 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)2