Search in sources :

Example 1 with MapMessageConverter

use of org.springframework.integration.support.converter.MapMessageConverter in project spring-integration by spring-projects.

the class TcpMessageMapperTests method testMapMessageConvertingOutboundJson.

@Test
public void testMapMessageConvertingOutboundJson() throws Exception {
    Message<String> message = MessageBuilder.withPayload("foo").setHeader("bar", "baz").build();
    MapMessageConverter converter = new MapMessageConverter();
    converter.setHeaderNames("bar");
    MessageConvertingTcpMessageMapper mapper = new MessageConvertingTcpMessageMapper(converter);
    Map<?, ?> map = (Map<?, ?>) mapper.fromMessage(message);
    MapJsonSerializer serializer = new MapJsonSerializer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    serializer.serialize(map, baos);
    assertEquals("{\"headers\":{\"bar\":\"baz\"},\"payload\":\"foo\"}\n", new String(baos.toByteArray(), "UTF-8"));
}
Also used : MapMessageConverter(org.springframework.integration.support.converter.MapMessageConverter) MapJsonSerializer(org.springframework.integration.ip.tcp.serializer.MapJsonSerializer) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with MapMessageConverter

use of org.springframework.integration.support.converter.MapMessageConverter in project spring-integration by spring-projects.

the class TcpMessageMapperTests method testMapMessageConvertingInboundJson.

@Test
public void testMapMessageConvertingInboundJson() throws Exception {
    String json = "{\"headers\":{\"bar\":\"baz\"},\"payload\":\"foo\"}\n";
    MapMessageConverter converter = new MapMessageConverter();
    MessageConvertingTcpMessageMapper mapper = new MessageConvertingTcpMessageMapper(converter);
    MapJsonSerializer deserializer = new MapJsonSerializer();
    Map<?, ?> map = deserializer.deserialize(new ByteArrayInputStream(json.getBytes("UTF-8")));
    TcpConnection connection = mock(TcpConnection.class);
    when(connection.getPayload()).thenReturn(map);
    when(connection.getHostName()).thenReturn("someHost");
    when(connection.getHostAddress()).thenReturn("1.1.1.1");
    when(connection.getPort()).thenReturn(1234);
    when(connection.getConnectionId()).thenReturn("someId");
    Message<?> message = mapper.toMessage(connection);
    assertEquals("foo", message.getPayload());
    assertEquals("baz", message.getHeaders().get("bar"));
    assertEquals("someHost", message.getHeaders().get(IpHeaders.HOSTNAME));
    assertEquals("1.1.1.1", message.getHeaders().get(IpHeaders.IP_ADDRESS));
    assertEquals(1234, message.getHeaders().get(IpHeaders.REMOTE_PORT));
    assertEquals("someId", message.getHeaders().get(IpHeaders.CONNECTION_ID));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MapMessageConverter(org.springframework.integration.support.converter.MapMessageConverter) MapJsonSerializer(org.springframework.integration.ip.tcp.serializer.MapJsonSerializer) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 3 with MapMessageConverter

use of org.springframework.integration.support.converter.MapMessageConverter in project spring-integration by spring-projects.

the class TcpMessageMapperTests method testMapMessageConvertingBothWaysJava.

@Test
public void testMapMessageConvertingBothWaysJava() throws Exception {
    Message<String> outMessage = MessageBuilder.withPayload("foo").setHeader("bar", "baz").build();
    MapMessageConverter converter = new MapMessageConverter();
    converter.setHeaderNames("bar");
    MessageConvertingTcpMessageMapper mapper = new MessageConvertingTcpMessageMapper(converter);
    Map<?, ?> map = (Map<?, ?>) mapper.fromMessage(outMessage);
    DefaultSerializer serializer = new DefaultSerializer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    serializer.serialize(map, baos);
    DefaultDeserializer deserializer = new DefaultDeserializer();
    map = (Map<?, ?>) deserializer.deserialize(new ByteArrayInputStream(baos.toByteArray()));
    TcpConnection connection = mock(TcpConnection.class);
    when(connection.getPayload()).thenReturn(map);
    when(connection.getHostName()).thenReturn("someHost");
    when(connection.getHostAddress()).thenReturn("1.1.1.1");
    when(connection.getPort()).thenReturn(1234);
    when(connection.getConnectionId()).thenReturn("someId");
    Message<?> message = mapper.toMessage(connection);
    assertEquals("foo", message.getPayload());
    assertEquals("baz", message.getHeaders().get("bar"));
    assertEquals("someHost", message.getHeaders().get(IpHeaders.HOSTNAME));
    assertEquals("1.1.1.1", message.getHeaders().get(IpHeaders.IP_ADDRESS));
    assertEquals(1234, message.getHeaders().get(IpHeaders.REMOTE_PORT));
    assertEquals("someId", message.getHeaders().get(IpHeaders.CONNECTION_ID));
}
Also used : DefaultSerializer(org.springframework.core.serializer.DefaultSerializer) DefaultDeserializer(org.springframework.core.serializer.DefaultDeserializer) ByteArrayInputStream(java.io.ByteArrayInputStream) MapMessageConverter(org.springframework.integration.support.converter.MapMessageConverter) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with MapMessageConverter

use of org.springframework.integration.support.converter.MapMessageConverter in project spring-integration by spring-projects.

the class TcpNetConnectionTests method transferHeaders.

@Test
public void transferHeaders() throws Exception {
    Socket inSocket = mock(Socket.class);
    PipedInputStream pipe = new PipedInputStream();
    when(inSocket.getInputStream()).thenReturn(pipe);
    TcpConnectionSupport inboundConnection = new TcpNetConnection(inSocket, true, false, e -> {
    }, null);
    inboundConnection.setDeserializer(new MapJsonSerializer());
    MapMessageConverter inConverter = new MapMessageConverter();
    MessageConvertingTcpMessageMapper inMapper = new MessageConvertingTcpMessageMapper(inConverter);
    inboundConnection.setMapper(inMapper);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Socket outSocket = mock(Socket.class);
    TcpNetConnection outboundConnection = new TcpNetConnection(outSocket, true, false, e -> {
    }, null);
    when(outSocket.getOutputStream()).thenReturn(baos);
    MapMessageConverter outConverter = new MapMessageConverter();
    outConverter.setHeaderNames("bar");
    MessageConvertingTcpMessageMapper outMapper = new MessageConvertingTcpMessageMapper(outConverter);
    outboundConnection.setMapper(outMapper);
    outboundConnection.setSerializer(new MapJsonSerializer());
    Message<String> message = MessageBuilder.withPayload("foo").setHeader("bar", "baz").build();
    outboundConnection.send(message);
    PipedOutputStream out = new PipedOutputStream(pipe);
    out.write(baos.toByteArray());
    out.close();
    final AtomicReference<Message<?>> inboundMessage = new AtomicReference<Message<?>>();
    TcpListener listener = message1 -> {
        if (!(message1 instanceof ErrorMessage)) {
            inboundMessage.set(message1);
        }
        return false;
    };
    inboundConnection.registerListener(listener);
    inboundConnection.run();
    assertNotNull(inboundMessage.get());
    assertEquals("foo", inboundMessage.get().getPayload());
    assertEquals("baz", inboundMessage.get().getHeaders().get("bar"));
}
Also used : Socket(java.net.Socket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ErrorMessage(org.springframework.messaging.support.ErrorMessage) TestUtils(org.springframework.integration.test.util.TestUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) MessageBuilder(org.springframework.integration.support.MessageBuilder) SocketChannel(java.nio.channels.SocketChannel) PipedInputStream(java.io.PipedInputStream) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MapJsonSerializer(org.springframework.integration.ip.tcp.serializer.MapJsonSerializer) Message(org.springframework.messaging.Message) MapMessageConverter(org.springframework.integration.support.converter.MapMessageConverter) ChannelInputStream(org.springframework.integration.ip.tcp.connection.TcpNioConnection.ChannelInputStream) ByteArrayStxEtxSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) PipedOutputStream(java.io.PipedOutputStream) Mockito(org.mockito.Mockito) Log(org.apache.commons.logging.Log) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) InputStream(java.io.InputStream) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) PipedOutputStream(java.io.PipedOutputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) PipedInputStream(java.io.PipedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MapMessageConverter(org.springframework.integration.support.converter.MapMessageConverter) MapJsonSerializer(org.springframework.integration.ip.tcp.serializer.MapJsonSerializer) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Socket(java.net.Socket) Test(org.junit.Test)

Example 5 with MapMessageConverter

use of org.springframework.integration.support.converter.MapMessageConverter in project spring-integration by spring-projects.

the class TcpNioConnectionTests method transferHeaders.

@Test
public void transferHeaders() throws Exception {
    Socket inSocket = mock(Socket.class);
    SocketChannel inChannel = mock(SocketChannel.class);
    when(inChannel.socket()).thenReturn(inSocket);
    TcpNioConnection inboundConnection = new TcpNioConnection(inChannel, true, false, nullPublisher, null);
    inboundConnection.setDeserializer(new MapJsonSerializer());
    MapMessageConverter inConverter = new MapMessageConverter();
    MessageConvertingTcpMessageMapper inMapper = new MessageConvertingTcpMessageMapper(inConverter);
    inboundConnection.setMapper(inMapper);
    final ByteArrayOutputStream written = new ByteArrayOutputStream();
    doAnswer(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            ByteBuffer buff = invocation.getArgument(0);
            byte[] bytes = written.toByteArray();
            buff.put(bytes);
            return bytes.length;
        }
    }).when(inChannel).read(any(ByteBuffer.class));
    Socket outSocket = mock(Socket.class);
    SocketChannel outChannel = mock(SocketChannel.class);
    when(outChannel.socket()).thenReturn(outSocket);
    TcpNioConnection outboundConnection = new TcpNioConnection(outChannel, true, false, nullPublisher, null);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ByteBuffer buff = invocation.getArgument(0);
            byte[] bytes = new byte[buff.limit()];
            buff.get(bytes);
            written.write(bytes);
            return null;
        }
    }).when(outChannel).write(any(ByteBuffer.class));
    MapMessageConverter outConverter = new MapMessageConverter();
    outConverter.setHeaderNames("bar");
    MessageConvertingTcpMessageMapper outMapper = new MessageConvertingTcpMessageMapper(outConverter);
    outboundConnection.setMapper(outMapper);
    outboundConnection.setSerializer(new MapJsonSerializer());
    Message<String> message = MessageBuilder.withPayload("foo").setHeader("bar", "baz").build();
    outboundConnection.send(message);
    final AtomicReference<Message<?>> inboundMessage = new AtomicReference<Message<?>>();
    final CountDownLatch latch = new CountDownLatch(1);
    TcpListener listener = new TcpListener() {

        @Override
        public boolean onMessage(Message<?> message) {
            inboundMessage.set(message);
            latch.countDown();
            return false;
        }
    };
    inboundConnection.registerListener(listener);
    inboundConnection.readPacket();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertNotNull(inboundMessage.get());
    assertEquals("foo", inboundMessage.get().getPayload());
    assertEquals("baz", inboundMessage.get().getHeaders().get("bar"));
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MapMessageConverter(org.springframework.integration.support.converter.MapMessageConverter) MapJsonSerializer(org.springframework.integration.ip.tcp.serializer.MapJsonSerializer) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 MapMessageConverter (org.springframework.integration.support.converter.MapMessageConverter)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 MapJsonSerializer (org.springframework.integration.ip.tcp.serializer.MapJsonSerializer)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Socket (java.net.Socket)2 ByteBuffer (java.nio.ByteBuffer)2 SocketChannel (java.nio.channels.SocketChannel)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Message (org.springframework.messaging.Message)2 ErrorMessage (org.springframework.messaging.support.ErrorMessage)2 InputStream (java.io.InputStream)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 ServerSocket (java.net.ServerSocket)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Log (org.apache.commons.logging.Log)1