Search in sources :

Example 1 with EmbeddedJsonHeadersMessageMapper

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));
}
Also used : EmbeddedJsonHeadersMessageMapper(org.springframework.integration.support.json.EmbeddedJsonHeadersMessageMapper) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 2 with EmbeddedJsonHeadersMessageMapper

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"));
}
Also used : DefaultPahoMessageConverter(org.springframework.integration.mqtt.support.DefaultPahoMessageConverter) MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) QueueChannel(org.springframework.integration.channel.QueueChannel) EmbeddedJsonHeadersMessageMapper(org.springframework.integration.support.json.EmbeddedJsonHeadersMessageMapper) MqttPahoMessageHandler(org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 3 with EmbeddedJsonHeadersMessageMapper

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"));
}
Also used : AsyncHandler(com.amazonaws.handlers.AsyncHandler) PutRecordsRequestEntry(com.amazonaws.services.kinesis.model.PutRecordsRequestEntry) PutRecordRequest(com.amazonaws.services.kinesis.model.PutRecordRequest) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) EmbeddedJsonHeadersMessageMapper(org.springframework.integration.support.json.EmbeddedJsonHeadersMessageMapper) PutRecordsRequest(com.amazonaws.services.kinesis.model.PutRecordsRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 EmbeddedJsonHeadersMessageMapper (org.springframework.integration.support.json.EmbeddedJsonHeadersMessageMapper)3 AsyncHandler (com.amazonaws.handlers.AsyncHandler)1 PutRecordRequest (com.amazonaws.services.kinesis.model.PutRecordRequest)1 PutRecordsRequest (com.amazonaws.services.kinesis.model.PutRecordsRequest)1 PutRecordsRequestEntry (com.amazonaws.services.kinesis.model.PutRecordsRequestEntry)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 MqttPahoMessageDrivenChannelAdapter (org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter)1 MqttPahoMessageHandler (org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler)1 DefaultPahoMessageConverter (org.springframework.integration.mqtt.support.DefaultPahoMessageConverter)1 MessageHandlingException (org.springframework.messaging.MessageHandlingException)1 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)1