use of org.springframework.integration.support.json.EmbeddedJsonHeadersMessageMapper in project spring-integration by spring-projects.
the class TcpMessageMapperTests method testWithBytesMapper.
@Test
public void testWithBytesMapper() throws Exception {
Message<String> outMessage = MessageBuilder.withPayload("foo").setHeader("bar", "baz").build();
TcpMessageMapper mapper = new TcpMessageMapper();
mapper.setBytesMessageMapper(new EmbeddedJsonHeadersMessageMapper());
byte[] bytes = (byte[]) mapper.fromMessage(outMessage);
TcpConnection connection = mock(TcpConnection.class);
when(connection.getPayload()).thenReturn(bytes);
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.support.json.EmbeddedJsonHeadersMessageMapper in project spring-integration by spring-projects.
the class BackToBackAdapterTests method testJson.
@Test
public void testJson() {
MqttPahoMessageHandler adapter = new MqttPahoMessageHandler("tcp://localhost:1883", "si-test-out");
adapter.setDefaultTopic("mqtt-foo");
adapter.setBeanFactory(mock(BeanFactory.class));
EmbeddedJsonHeadersMessageMapper mapper = new EmbeddedJsonHeadersMessageMapper(JacksonJsonUtils.messagingAwareMapper("org.springframework"));
DefaultPahoMessageConverter converter = new DefaultPahoMessageConverter();
converter.setBytesMessageMapper(mapper);
adapter.setConverter(converter);
adapter.afterPropertiesSet();
adapter.start();
MqttPahoMessageDrivenChannelAdapter inbound = new MqttPahoMessageDrivenChannelAdapter("tcp://localhost:1883", "si-test-in", "mqtt-foo");
QueueChannel outputChannel = new QueueChannel();
inbound.setOutputChannel(outputChannel);
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.initialize();
inbound.setTaskScheduler(taskScheduler);
inbound.setBeanFactory(mock(BeanFactory.class));
inbound.setConverter(converter);
inbound.afterPropertiesSet();
inbound.start();
adapter.handleMessage(new GenericMessage<Foo>(new Foo("bar"), Collections.singletonMap("baz", "qux")));
Message<?> out = outputChannel.receive(20000);
assertNotNull(out);
adapter.stop();
inbound.stop();
assertEquals(new Foo("bar"), out.getPayload());
assertEquals("mqtt-foo", out.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
assertEquals("qux", out.getHeaders().get("baz"));
}
use of org.springframework.integration.support.json.EmbeddedJsonHeadersMessageMapper in project spring-integration-aws by spring-projects.
the class KinesisMessageHandlerTests method testKinesisMessageHandler.
@Test
@SuppressWarnings("unchecked")
public void testKinesisMessageHandler() throws Exception {
Message<?> message = MessageBuilder.withPayload("message").build();
try {
this.kinesisSendChannel.send(message);
} catch (Exception e) {
assertThat(e).isInstanceOf(MessageHandlingException.class);
assertThat(e.getCause()).isInstanceOf(IllegalStateException.class);
assertThat(e.getMessage()).contains("'stream' must not be null for sending a Kinesis record");
}
this.kinesisMessageHandler.setStream("foo");
try {
this.kinesisSendChannel.send(message);
} catch (Exception e) {
assertThat(e).isInstanceOf(MessageHandlingException.class);
assertThat(e.getCause()).isInstanceOf(IllegalStateException.class);
assertThat(e.getMessage()).contains("'partitionKey' must not be null for sending a Kinesis record");
}
message = MessageBuilder.fromMessage(message).setHeader(AwsHeaders.PARTITION_KEY, "fooKey").setHeader(AwsHeaders.SEQUENCE_NUMBER, "10").setHeader("foo", "bar").build();
this.kinesisSendChannel.send(message);
ArgumentCaptor<PutRecordRequest> putRecordRequestArgumentCaptor = ArgumentCaptor.forClass(PutRecordRequest.class);
ArgumentCaptor<AsyncHandler<PutRecordRequest, PutRecordResult>> asyncHandlerArgumentCaptor = ArgumentCaptor.forClass((Class<AsyncHandler<PutRecordRequest, PutRecordResult>>) (Class<?>) AsyncHandler.class);
verify(this.amazonKinesis).putRecordAsync(putRecordRequestArgumentCaptor.capture(), asyncHandlerArgumentCaptor.capture());
PutRecordRequest putRecordRequest = putRecordRequestArgumentCaptor.getValue();
assertThat(putRecordRequest.getStreamName()).isEqualTo("foo");
assertThat(putRecordRequest.getPartitionKey()).isEqualTo("fooKey");
assertThat(putRecordRequest.getSequenceNumberForOrdering()).isEqualTo("10");
assertThat(putRecordRequest.getExplicitHashKey()).isNull();
Message<?> messageToCheck = new EmbeddedJsonHeadersMessageMapper().toMessage(putRecordRequest.getData().array());
assertThat(messageToCheck.getHeaders()).contains(entry("foo", "bar"));
assertThat(messageToCheck.getPayload()).isEqualTo("message".getBytes());
AsyncHandler<?, ?> asyncHandler = asyncHandlerArgumentCaptor.getValue();
RuntimeException testingException = new RuntimeException("testingException");
asyncHandler.onError(testingException);
verify(this.asyncHandler).onError(eq(testingException));
message = new GenericMessage<>(new PutRecordsRequest().withStreamName("myStream").withRecords(new PutRecordsRequestEntry().withData(ByteBuffer.wrap("test".getBytes())).withPartitionKey("testKey")));
this.kinesisSendChannel.send(message);
ArgumentCaptor<PutRecordsRequest> putRecordsRequestArgumentCaptor = ArgumentCaptor.forClass(PutRecordsRequest.class);
verify(this.amazonKinesis).putRecordsAsync(putRecordsRequestArgumentCaptor.capture(), any(AsyncHandler.class));
PutRecordsRequest putRecordsRequest = putRecordsRequestArgumentCaptor.getValue();
assertThat(putRecordsRequest.getStreamName()).isEqualTo("myStream");
assertThat(putRecordsRequest.getRecords()).containsExactlyInAnyOrder(new PutRecordsRequestEntry().withData(ByteBuffer.wrap("test".getBytes())).withPartitionKey("testKey"));
}
Aggregations