Search in sources :

Example 1 with AmqpInboundGateway

use of org.springframework.integration.amqp.inbound.AmqpInboundGateway in project spring-integration by spring-projects.

the class AmqpInboundGatewayParserTests method verifyUsageWithHeaderMapper.

@Test
public void verifyUsageWithHeaderMapper() throws Exception {
    DirectChannel requestChannel = context.getBean("requestChannel", DirectChannel.class);
    requestChannel.subscribe(siMessage -> {
        org.springframework.messaging.Message<?> replyMessage = MessageBuilder.fromMessage(siMessage).setHeader("bar", "bar").build();
        MessageChannel replyChannel = (MessageChannel) siMessage.getHeaders().getReplyChannel();
        replyChannel.send(replyMessage);
    });
    final AmqpInboundGateway gateway = context.getBean("withHeaderMapper", AmqpInboundGateway.class);
    Field amqpTemplateField = ReflectionUtils.findField(AmqpInboundGateway.class, "amqpTemplate");
    amqpTemplateField.setAccessible(true);
    RabbitTemplate amqpTemplate = TestUtils.getPropertyValue(gateway, "amqpTemplate", RabbitTemplate.class);
    amqpTemplate = Mockito.spy(amqpTemplate);
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        Message amqpReplyMessage = (Message) args[2];
        MessageProperties properties = amqpReplyMessage.getMessageProperties();
        assertEquals("bar", properties.getHeaders().get("bar"));
        return null;
    }).when(amqpTemplate).send(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(Message.class), isNull());
    ReflectionUtils.setField(amqpTemplateField, gateway, amqpTemplate);
    AbstractMessageListenerContainer mlc = TestUtils.getPropertyValue(gateway, "messageListenerContainer", AbstractMessageListenerContainer.class);
    ChannelAwareMessageListener listener = TestUtils.getPropertyValue(mlc, "messageListener", ChannelAwareMessageListener.class);
    MessageProperties amqpProperties = new MessageProperties();
    amqpProperties.setAppId("test.appId");
    amqpProperties.setClusterId("test.clusterId");
    amqpProperties.setContentEncoding("test.contentEncoding");
    amqpProperties.setContentLength(99L);
    amqpProperties.setReplyTo("oleg");
    amqpProperties.setContentType("test.contentType");
    amqpProperties.setHeader("foo", "foo");
    amqpProperties.setHeader("bar", "bar");
    Message amqpMessage = new Message("hello".getBytes(), amqpProperties);
    listener.onMessage(amqpMessage, null);
    Mockito.verify(amqpTemplate, Mockito.times(1)).send(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(Message.class), isNull());
}
Also used : AmqpInboundGateway(org.springframework.integration.amqp.inbound.AmqpInboundGateway) RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) Message(org.springframework.amqp.core.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) Field(java.lang.reflect.Field) MessageChannel(org.springframework.messaging.MessageChannel) MessageProperties(org.springframework.amqp.core.MessageProperties) AbstractMessageListenerContainer(org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer) Test(org.junit.Test)

Aggregations

Field (java.lang.reflect.Field)1 Test (org.junit.Test)1 Message (org.springframework.amqp.core.Message)1 MessageProperties (org.springframework.amqp.core.MessageProperties)1 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)1 AbstractMessageListenerContainer (org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer)1 ChannelAwareMessageListener (org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener)1 AmqpInboundGateway (org.springframework.integration.amqp.inbound.AmqpInboundGateway)1 DirectChannel (org.springframework.integration.channel.DirectChannel)1 MessageChannel (org.springframework.messaging.MessageChannel)1