Search in sources :

Example 1 with ObjectToJsonTransformer

use of org.springframework.integration.json.ObjectToJsonTransformer in project spring-integration by spring-projects.

the class JsonConverterCompatibilityTests method testInbound.

@Test
public void testInbound() {
    @SuppressWarnings("unchecked") final Message<String> out = (Message<String>) new ObjectToJsonTransformer().transform(new GenericMessage<Foo>(new Foo()));
    MessageProperties messageProperties = new MessageProperties();
    DefaultAmqpHeaderMapper.outboundMapper().fromHeadersToRequest(out.getHeaders(), messageProperties);
    final BasicProperties props = new DefaultMessagePropertiesConverter().fromMessageProperties(messageProperties, "UTF-8");
    this.rabbitTemplate.execute(channel -> {
        channel.basicPublish("", JSON_TESTQ, props, out.getPayload().getBytes());
        return null;
    });
    Object received = this.rabbitTemplate.receiveAndConvert(JSON_TESTQ);
    assertThat(received, instanceOf(Foo.class));
}
Also used : ObjectToJsonTransformer(org.springframework.integration.json.ObjectToJsonTransformer) GenericMessage(org.springframework.messaging.support.GenericMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageProperties(org.springframework.amqp.core.MessageProperties) BasicProperties(com.rabbitmq.client.AMQP.BasicProperties) DefaultMessagePropertiesConverter(org.springframework.amqp.rabbit.support.DefaultMessagePropertiesConverter) Test(org.junit.Test)

Example 2 with ObjectToJsonTransformer

use of org.springframework.integration.json.ObjectToJsonTransformer in project spring-integration by spring-projects.

the class InboundEndpointTests method testInt2809JavaTypePropertiesToAmqp.

@Test
public void testInt2809JavaTypePropertiesToAmqp() throws Exception {
    Connection connection = mock(Connection.class);
    doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container);
    adapter.setMessageConverter(new Jackson2JsonMessageConverter());
    PollableChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    Object payload = new Foo("bar1");
    Transformer objectToJsonTransformer = new ObjectToJsonTransformer();
    Message<?> jsonMessage = objectToJsonTransformer.transform(new GenericMessage<Object>(payload));
    MessageProperties amqpMessageProperties = new MessageProperties();
    amqpMessageProperties.setDeliveryTag(123L);
    org.springframework.amqp.core.Message amqpMessage = new SimpleMessageConverter().toMessage(jsonMessage.getPayload(), amqpMessageProperties);
    DefaultAmqpHeaderMapper.inboundMapper().fromHeadersToRequest(jsonMessage.getHeaders(), amqpMessageProperties);
    ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
    Channel rabbitChannel = mock(Channel.class);
    listener.onMessage(amqpMessage, rabbitChannel);
    Message<?> result = channel.receive(1000);
    assertEquals(payload, result.getPayload());
    assertSame(rabbitChannel, result.getHeaders().get(AmqpHeaders.CHANNEL));
    assertEquals(123L, result.getHeaders().get(AmqpHeaders.DELIVERY_TAG));
}
Also used : ObjectToJsonTransformer(org.springframework.integration.json.ObjectToJsonTransformer) Transformer(org.springframework.integration.transformer.Transformer) JsonToObjectTransformer(org.springframework.integration.json.JsonToObjectTransformer) ObjectToJsonTransformer(org.springframework.integration.json.ObjectToJsonTransformer) QueueChannel(org.springframework.integration.channel.QueueChannel) PollableChannel(org.springframework.messaging.PollableChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) Channel(com.rabbitmq.client.Channel) QueueChannel(org.springframework.integration.channel.QueueChannel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) Jackson2JsonMessageConverter(org.springframework.amqp.support.converter.Jackson2JsonMessageConverter) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) MessageProperties(org.springframework.amqp.core.MessageProperties) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 MessageProperties (org.springframework.amqp.core.MessageProperties)2 ObjectToJsonTransformer (org.springframework.integration.json.ObjectToJsonTransformer)2 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)1 Channel (com.rabbitmq.client.Channel)1 Connection (org.springframework.amqp.rabbit.connection.Connection)1 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)1 SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)1 ChannelAwareMessageListener (org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener)1 DefaultMessagePropertiesConverter (org.springframework.amqp.rabbit.support.DefaultMessagePropertiesConverter)1 Jackson2JsonMessageConverter (org.springframework.amqp.support.converter.Jackson2JsonMessageConverter)1 SimpleMessageConverter (org.springframework.amqp.support.converter.SimpleMessageConverter)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 DirectChannel (org.springframework.integration.channel.DirectChannel)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 JsonToObjectTransformer (org.springframework.integration.json.JsonToObjectTransformer)1 Transformer (org.springframework.integration.transformer.Transformer)1 Message (org.springframework.messaging.Message)1 PollableChannel (org.springframework.messaging.PollableChannel)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1