use of org.springframework.integration.ip.tcp.serializer.MapJsonSerializer 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"));
}
use of org.springframework.integration.ip.tcp.serializer.MapJsonSerializer 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));
}
use of org.springframework.integration.ip.tcp.serializer.MapJsonSerializer 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"));
}
use of org.springframework.integration.ip.tcp.serializer.MapJsonSerializer 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"));
}
Aggregations