Search in sources :

Example 51 with MessageHeaders

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

the class MappingJackson2MessageConverterTests method toMessageJsonView.

@Test
public void toMessageJsonView() throws Exception {
    MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
    Map<String, Object> map = new HashMap<>();
    Method method = getClass().getDeclaredMethod("jsonViewResponse");
    MethodParameter returnType = new MethodParameter(method, -1);
    Message<?> message = converter.toMessage(jsonViewResponse(), new MessageHeaders(map), returnType);
    String actual = new String((byte[]) message.getPayload(), StandardCharsets.UTF_8);
    assertThat(actual, containsString("\"withView1\":\"with\""));
    assertThat(actual, containsString("\"withView2\":\"with\""));
    assertThat(actual, not(containsString("\"withoutView\":\"with\"")));
    method = getClass().getDeclaredMethod("jsonViewPayload", JacksonViewBean.class);
    MethodParameter param = new MethodParameter(method, 0);
    JacksonViewBean back = (JacksonViewBean) converter.fromMessage(message, JacksonViewBean.class, param);
    assertNull(back.getWithView1());
    assertEquals("with", back.getWithView2());
    assertNull(back.getWithoutView());
}
Also used : HashMap(java.util.HashMap) Method(java.lang.reflect.Method) MessageHeaders(org.springframework.messaging.MessageHeaders) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 52 with MessageHeaders

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

the class SimpleMessageConverterTests method toMessageWithPayloadAndMutableHeaders.

@Test
public void toMessageWithPayloadAndMutableHeaders() {
    MessageHeaderAccessor accessor = new MessageHeaderAccessor();
    accessor.setHeader("foo", "bar");
    accessor.setLeaveMutable(true);
    MessageHeaders headers = accessor.getMessageHeaders();
    Message<?> message = this.converter.toMessage("payload", headers);
    assertEquals("payload", message.getPayload());
    assertSame(headers, message.getHeaders());
    assertEquals("bar", message.getHeaders().get("foo"));
}
Also used : MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 53 with MessageHeaders

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

the class StringMessageConverterTests method toMessage.

@Test
public void toMessage() {
    Map<String, Object> map = new HashMap<>();
    map.put(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN);
    MessageHeaders headers = new MessageHeaders(map);
    Message<?> message = this.converter.toMessage("ABC", headers);
    assertEquals("ABC", new String(((byte[]) message.getPayload())));
}
Also used : HashMap(java.util.HashMap) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 54 with MessageHeaders

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

the class TestMessagePostProcessor method convertAndSendPayloadAndMutableHeadersToDestination.

@Test
public void convertAndSendPayloadAndMutableHeadersToDestination() {
    MessageHeaderAccessor accessor = new MessageHeaderAccessor();
    accessor.setHeader("foo", "bar");
    accessor.setLeaveMutable(true);
    MessageHeaders messageHeaders = accessor.getMessageHeaders();
    this.template.setMessageConverter(new StringMessageConverter());
    this.template.convertAndSend("somewhere", "payload", messageHeaders);
    MessageHeaders actual = this.template.message.getHeaders();
    assertSame(messageHeaders, actual);
    assertEquals(new MimeType("text", "plain", StandardCharsets.UTF_8), actual.get(MessageHeaders.CONTENT_TYPE));
    assertEquals("bar", actual.get("foo"));
}
Also used : StringMessageConverter(org.springframework.messaging.converter.StringMessageConverter) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) MessageHeaders(org.springframework.messaging.MessageHeaders) MimeType(org.springframework.util.MimeType) Test(org.junit.Test)

Aggregations

MessageHeaders (org.springframework.messaging.MessageHeaders)54 Test (org.junit.Test)39 HashMap (java.util.HashMap)15 Message (org.springframework.messaging.Message)9 SimpMessageType (org.springframework.messaging.simp.SimpMessageType)6 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)5 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)4 MimeType (org.springframework.util.MimeType)3 Principal (java.security.Principal)2 HashSet (java.util.HashSet)2 MessageConverter (org.springframework.messaging.converter.MessageConverter)2 StringMessageConverter (org.springframework.messaging.converter.StringMessageConverter)2 TestPrincipal (org.springframework.messaging.simp.TestPrincipal)2 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 Method (java.lang.reflect.Method)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Destination (javax.jms.Destination)1 JMSException (javax.jms.JMSException)1