Search in sources :

Example 1 with AmqpOutboundEndpoint

use of org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint in project spring-integration by spring-projects.

the class AmqpOutboundGatewayParserTests method withHeaderMapperNothingToMap.

@Test
public void withHeaderMapperNothingToMap() {
    Object eventDrivenConsumer = this.context.getBean("withHeaderMapperNothingToMap");
    AmqpOutboundEndpoint endpoint = TestUtils.getPropertyValue(eventDrivenConsumer, "handler", AmqpOutboundEndpoint.class);
    Field amqpTemplateField = ReflectionUtils.findField(AmqpOutboundEndpoint.class, "amqpTemplate");
    amqpTemplateField.setAccessible(true);
    RabbitTemplate amqpTemplate = TestUtils.getPropertyValue(endpoint, "amqpTemplate", RabbitTemplate.class);
    amqpTemplate = Mockito.spy(amqpTemplate);
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        org.springframework.amqp.core.Message amqpRequestMessage = (org.springframework.amqp.core.Message) args[2];
        MessageProperties properties = amqpRequestMessage.getMessageProperties();
        assertNull(properties.getHeaders().get("foo"));
        // mock reply AMQP message
        MessageProperties amqpProperties = new MessageProperties();
        amqpProperties.setAppId("test.appId");
        amqpProperties.setHeader("foobar", "foobar");
        amqpProperties.setHeader("bar", "bar");
        return new org.springframework.amqp.core.Message("hello".getBytes(), amqpProperties);
    }).when(amqpTemplate).sendAndReceive(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(org.springframework.amqp.core.Message.class), isNull());
    ReflectionUtils.setField(amqpTemplateField, endpoint, amqpTemplate);
    MessageChannel requestChannel = this.context.getBean("toRabbit3", MessageChannel.class);
    Message<?> message = MessageBuilder.withPayload("hello").setHeader("foo", "foo").build();
    requestChannel.send(message);
    Mockito.verify(amqpTemplate, Mockito.times(1)).sendAndReceive(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(org.springframework.amqp.core.Message.class), isNull());
    // verify reply
    QueueChannel queueChannel = context.getBean("fromRabbit", QueueChannel.class);
    Message<?> replyMessage = queueChannel.receive(0);
    assertNull(replyMessage.getHeaders().get("bar"));
    // copied from request Message
    assertEquals("foo", replyMessage.getHeaders().get("foo"));
    assertNull(replyMessage.getHeaders().get("foobar"));
    assertNull(replyMessage.getHeaders().get(AmqpHeaders.DELIVERY_MODE));
    assertNull(replyMessage.getHeaders().get(AmqpHeaders.CONTENT_TYPE));
    assertNull(replyMessage.getHeaders().get(AmqpHeaders.APP_ID));
    assertEquals(1, adviceCalled);
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) Message(org.springframework.messaging.Message) QueueChannel(org.springframework.integration.channel.QueueChannel) Field(java.lang.reflect.Field) AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint) MessageChannel(org.springframework.messaging.MessageChannel) MessageProperties(org.springframework.amqp.core.MessageProperties) Test(org.junit.Test)

Example 2 with AmqpOutboundEndpoint

use of org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint in project spring-integration by spring-projects.

the class AmqpOutboundGatewayParserTests method withHeaderMapperCustomAndStandardResponse.

@Test
public void withHeaderMapperCustomAndStandardResponse() {
    Object eventDrivenConsumer = this.context.getBean("withHeaderMapperCustomAndStandardResponse");
    AmqpOutboundEndpoint endpoint = TestUtils.getPropertyValue(eventDrivenConsumer, "handler", AmqpOutboundEndpoint.class);
    assertNull(TestUtils.getPropertyValue(endpoint, "defaultDeliveryMode"));
    assertFalse(TestUtils.getPropertyValue(endpoint, "headersMappedLast", Boolean.class));
    Field amqpTemplateField = ReflectionUtils.findField(AmqpOutboundEndpoint.class, "amqpTemplate");
    amqpTemplateField.setAccessible(true);
    RabbitTemplate amqpTemplate = TestUtils.getPropertyValue(endpoint, "amqpTemplate", RabbitTemplate.class);
    amqpTemplate = Mockito.spy(amqpTemplate);
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        org.springframework.amqp.core.Message amqpRequestMessage = (org.springframework.amqp.core.Message) args[2];
        MessageProperties properties = amqpRequestMessage.getMessageProperties();
        assertEquals("foo", properties.getHeaders().get("foo"));
        // mock reply AMQP message
        MessageProperties amqpProperties = new MessageProperties();
        amqpProperties.setAppId("test.appId");
        amqpProperties.setHeader("foobar", "foobar");
        amqpProperties.setHeader("bar", "bar");
        assertEquals(MessageDeliveryMode.PERSISTENT, properties.getDeliveryMode());
        amqpProperties.setReceivedDeliveryMode(properties.getDeliveryMode());
        return new org.springframework.amqp.core.Message("hello".getBytes(), amqpProperties);
    }).when(amqpTemplate).sendAndReceive(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(org.springframework.amqp.core.Message.class), isNull());
    ReflectionUtils.setField(amqpTemplateField, endpoint, amqpTemplate);
    MessageChannel requestChannel = this.context.getBean("toRabbit2", MessageChannel.class);
    Message<?> message = MessageBuilder.withPayload("hello").setHeader("foo", "foo").build();
    requestChannel.send(message);
    Mockito.verify(amqpTemplate, Mockito.times(1)).sendAndReceive(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(org.springframework.amqp.core.Message.class), isNull());
    // verify reply
    QueueChannel queueChannel = this.context.getBean("fromRabbit", QueueChannel.class);
    Message<?> replyMessage = queueChannel.receive(0);
    assertEquals("bar", replyMessage.getHeaders().get("bar"));
    // copied from request Message
    assertEquals("foo", replyMessage.getHeaders().get("foo"));
    assertNull(replyMessage.getHeaders().get("foobar"));
    assertNotNull(replyMessage.getHeaders().get(AmqpHeaders.RECEIVED_DELIVERY_MODE));
    assertNotNull(replyMessage.getHeaders().get(AmqpHeaders.CONTENT_TYPE));
    assertNotNull(replyMessage.getHeaders().get(AmqpHeaders.APP_ID));
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) Message(org.springframework.messaging.Message) QueueChannel(org.springframework.integration.channel.QueueChannel) Field(java.lang.reflect.Field) AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint) MessageChannel(org.springframework.messaging.MessageChannel) MessageProperties(org.springframework.amqp.core.MessageProperties) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 3 with AmqpOutboundEndpoint

use of org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint in project spring-integration by spring-projects.

the class AmqpOutboundChannelAdapterParserTests method parseWithPublisherConfirms.

@Test
public void parseWithPublisherConfirms() {
    Object eventDrivenConsumer = context.getBean("withPublisherConfirms");
    AmqpOutboundEndpoint endpoint = TestUtils.getPropertyValue(eventDrivenConsumer, "handler", AmqpOutboundEndpoint.class);
    NullChannel nullChannel = context.getBean(NullChannel.class);
    MessageChannel ackChannel = context.getBean("ackChannel", MessageChannel.class);
    assertSame(ackChannel, TestUtils.getPropertyValue(endpoint, "confirmAckChannel"));
    assertSame(nullChannel, TestUtils.getPropertyValue(endpoint, "confirmNackChannel"));
    assertSame(context.getBean("ems"), TestUtils.getPropertyValue(endpoint, "errorMessageStrategy"));
}
Also used : AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint) MessageChannel(org.springframework.messaging.MessageChannel) NullChannel(org.springframework.integration.channel.NullChannel) Test(org.junit.Test)

Example 4 with AmqpOutboundEndpoint

use of org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint in project spring-integration by spring-projects.

the class OutboundGatewayTests method testExpressionsBeanResolver.

@Test
@SuppressWarnings("unchecked")
public void testExpressionsBeanResolver() throws Exception {
    ApplicationContext context = mock(ApplicationContext.class);
    doAnswer(invocation -> invocation.getArguments()[0] + "bar").when(context).getBean(anyString());
    when(context.containsBean(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME)).thenReturn(true);
    when(context.getBean(SpelPropertyAccessorRegistrar.class)).thenThrow(NoSuchBeanDefinitionException.class);
    IntegrationEvaluationContextFactoryBean integrationEvaluationContextFactoryBean = new IntegrationEvaluationContextFactoryBean();
    integrationEvaluationContextFactoryBean.setApplicationContext(context);
    integrationEvaluationContextFactoryBean.afterPropertiesSet();
    StandardEvaluationContext evalContext = integrationEvaluationContextFactoryBean.getObject();
    when(context.getBean(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME, StandardEvaluationContext.class)).thenReturn(evalContext);
    RabbitTemplate template = spy(new RabbitTemplate());
    AmqpOutboundEndpoint endpoint = new AmqpOutboundEndpoint(template);
    endpoint.setRoutingKeyExpression(PARSER.parseExpression("@foo"));
    endpoint.setExchangeNameExpression(PARSER.parseExpression("@bar"));
    endpoint.setConfirmCorrelationExpressionString("@baz");
    endpoint.setBeanFactory(context);
    endpoint.afterPropertiesSet();
    Message<?> message = new GenericMessage<String>("Hello, world!");
    assertEquals("foobar", TestUtils.getPropertyValue(endpoint, "routingKeyGenerator", MessageProcessor.class).processMessage(message));
    assertEquals("barbar", TestUtils.getPropertyValue(endpoint, "exchangeNameGenerator", MessageProcessor.class).processMessage(message));
    assertEquals("bazbar", TestUtils.getPropertyValue(endpoint, "correlationDataGenerator", MessageProcessor.class).processMessage(message));
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) GenericMessage(org.springframework.messaging.support.GenericMessage) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint) IntegrationEvaluationContextFactoryBean(org.springframework.integration.config.IntegrationEvaluationContextFactoryBean) Test(org.junit.Test)

Example 5 with AmqpOutboundEndpoint

use of org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint in project spring-integration by spring-projects.

the class AmqpOutboundGatewayParserTests method amqpOutboundGatewayWithinChain.

// INT-1029
@Test
public void amqpOutboundGatewayWithinChain() {
    Object eventDrivenConsumer = this.context.getBean("chainWithRabbitOutboundGateway");
    List<?> chainHandlers = TestUtils.getPropertyValue(eventDrivenConsumer, "handler.handlers", List.class);
    AmqpOutboundEndpoint endpoint = (AmqpOutboundEndpoint) chainHandlers.get(0);
    Field amqpTemplateField = ReflectionUtils.findField(AmqpOutboundEndpoint.class, "amqpTemplate");
    amqpTemplateField.setAccessible(true);
    RabbitTemplate amqpTemplate = TestUtils.getPropertyValue(endpoint, "amqpTemplate", RabbitTemplate.class);
    amqpTemplate = Mockito.spy(amqpTemplate);
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        org.springframework.amqp.core.Message amqpRequestMessage = (org.springframework.amqp.core.Message) args[2];
        MessageProperties properties = amqpRequestMessage.getMessageProperties();
        assertNull(properties.getHeaders().get("foo"));
        // mock reply AMQP message
        MessageProperties amqpProperties = new MessageProperties();
        amqpProperties.setAppId("test.appId");
        amqpProperties.setHeader("foobar", "foobar");
        amqpProperties.setHeader("bar", "bar");
        return new org.springframework.amqp.core.Message("hello".getBytes(), amqpProperties);
    }).when(amqpTemplate).sendAndReceive(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(org.springframework.amqp.core.Message.class), isNull());
    ReflectionUtils.setField(amqpTemplateField, endpoint, amqpTemplate);
    MessageChannel requestChannel = this.context.getBean("toRabbit4", MessageChannel.class);
    Message<?> message = MessageBuilder.withPayload("hello").setHeader("foo", "foo").build();
    requestChannel.send(message);
    Mockito.verify(amqpTemplate, Mockito.times(1)).sendAndReceive(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(org.springframework.amqp.core.Message.class), isNull());
    // verify reply
    QueueChannel queueChannel = this.context.getBean("fromRabbit", QueueChannel.class);
    Message<?> replyMessage = queueChannel.receive(0);
    assertEquals("hello", new String((byte[]) replyMessage.getPayload()));
    assertNull(replyMessage.getHeaders().get("bar"));
    // copied from request Message
    assertEquals("foo", replyMessage.getHeaders().get("foo"));
    assertNull(replyMessage.getHeaders().get("foobar"));
    assertNull(replyMessage.getHeaders().get(AmqpHeaders.DELIVERY_MODE));
    assertNull(replyMessage.getHeaders().get(AmqpHeaders.CONTENT_TYPE));
    assertNull(replyMessage.getHeaders().get(AmqpHeaders.APP_ID));
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) Message(org.springframework.messaging.Message) QueueChannel(org.springframework.integration.channel.QueueChannel) Field(java.lang.reflect.Field) AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint) MessageChannel(org.springframework.messaging.MessageChannel) MessageProperties(org.springframework.amqp.core.MessageProperties) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)10 AmqpOutboundEndpoint (org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint)10 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)8 MessageChannel (org.springframework.messaging.MessageChannel)7 Field (java.lang.reflect.Field)6 MessageProperties (org.springframework.amqp.core.MessageProperties)6 Message (org.springframework.messaging.Message)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 QueueChannel (org.springframework.integration.channel.QueueChannel)4 GenericMessage (org.springframework.messaging.support.GenericMessage)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 CorrelationData (org.springframework.amqp.rabbit.support.CorrelationData)2 ApplicationContext (org.springframework.context.ApplicationContext)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 List (java.util.List)1 Log (org.apache.commons.logging.Log)1 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)1 AsyncAmqpOutboundGateway (org.springframework.integration.amqp.outbound.AsyncAmqpOutboundGateway)1