Search in sources :

Example 6 with AmqpOutboundEndpoint

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

the class AmqpOutboundGatewayParserTests method withHeaderMapperCustomRequestResponse.

@Test
public void withHeaderMapperCustomRequestResponse() {
    Object eventDrivenConsumer = this.context.getBean("withHeaderMapperCustomRequestResponse");
    AmqpOutboundEndpoint endpoint = TestUtils.getPropertyValue(eventDrivenConsumer, "handler", AmqpOutboundEndpoint.class);
    assertNotNull(TestUtils.getPropertyValue(endpoint, "defaultDeliveryMode"));
    assertFalse(TestUtils.getPropertyValue(endpoint, "lazyConnect", Boolean.class));
    assertFalse(TestUtils.getPropertyValue(endpoint, "requiresReply", Boolean.class));
    assertTrue(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);
    final AtomicBoolean shouldBePersistent = new AtomicBoolean();
    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"));
        assertEquals(shouldBePersistent.get() ? MessageDeliveryMode.PERSISTENT : MessageDeliveryMode.NON_PERSISTENT, properties.getDeliveryMode());
        // 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("toRabbit1", 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);
    assertNotNull(replyMessage);
    assertEquals("bar", 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));
    shouldBePersistent.set(true);
    message = MessageBuilder.withPayload("hello").setHeader("foo", "foo").setHeader(AmqpHeaders.DELIVERY_MODE, MessageDeliveryMode.PERSISTENT).build();
    requestChannel.send(message);
    replyMessage = queueChannel.receive(0);
    assertNotNull(replyMessage);
}
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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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 7 with AmqpOutboundEndpoint

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

the class AmqpOutboundGatewayParserTests method testGatewayConfig.

@Test
public void testGatewayConfig() {
    Object edc = this.context.getBean("rabbitGateway");
    assertFalse(TestUtils.getPropertyValue(edc, "autoStartup", Boolean.class));
    AmqpOutboundEndpoint gateway = TestUtils.getPropertyValue(edc, "handler", AmqpOutboundEndpoint.class);
    assertEquals("amqp:outbound-gateway", gateway.getComponentType());
    assertTrue(TestUtils.getPropertyValue(gateway, "requiresReply", Boolean.class));
    checkGWProps(this.context, gateway);
    AsyncAmqpOutboundGateway async = this.context.getBean("asyncGateway.handler", AsyncAmqpOutboundGateway.class);
    assertEquals("amqp:outbound-async-gateway", async.getComponentType());
    checkGWProps(this.context, async);
    assertSame(this.context.getBean("asyncTemplate"), TestUtils.getPropertyValue(async, "template"));
    assertSame(this.context.getBean("ems"), TestUtils.getPropertyValue(gateway, "errorMessageStrategy"));
}
Also used : AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint) AsyncAmqpOutboundGateway(org.springframework.integration.amqp.outbound.AsyncAmqpOutboundGateway) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 8 with AmqpOutboundEndpoint

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

the class AmqpOutboundChannelAdapterParserTests method testInt3430FailForNotLazyConnect.

@Test
public void testInt3430FailForNotLazyConnect() {
    RabbitTemplate amqpTemplate = spy(new RabbitTemplate());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    RuntimeException toBeThrown = new RuntimeException("Test Connection Exception");
    doThrow(toBeThrown).when(connectionFactory).createConnection();
    when(amqpTemplate.getConnectionFactory()).thenReturn(connectionFactory);
    AmqpOutboundEndpoint handler = new AmqpOutboundEndpoint(amqpTemplate);
    Log logger = spy(TestUtils.getPropertyValue(handler, "logger", Log.class));
    new DirectFieldAccessor(handler).setPropertyValue("logger", logger);
    doNothing().when(logger).error("Failed to eagerly establish the connection.", toBeThrown);
    ApplicationContext context = mock(ApplicationContext.class);
    handler.setApplicationContext(context);
    handler.setBeanFactory(context);
    handler.afterPropertiesSet();
    handler.start();
    handler.stop();
    verify(logger, never()).error(anyString(), any(RuntimeException.class));
    handler.setLazyConnect(false);
    handler.start();
    verify(logger).error("Failed to eagerly establish the connection.", toBeThrown);
    handler.stop();
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint) Log(org.apache.commons.logging.Log) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Test(org.junit.Test)

Example 9 with AmqpOutboundEndpoint

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

the class AmqpOutboundChannelAdapterParserTests method withHeaderMapperCustomHeaders.

@Test
public void withHeaderMapperCustomHeaders() {
    Object eventDrivenConsumer = context.getBean("withHeaderMapperCustomHeaders");
    AmqpOutboundEndpoint endpoint = TestUtils.getPropertyValue(eventDrivenConsumer, "handler", AmqpOutboundEndpoint.class);
    assertNotNull(TestUtils.getPropertyValue(endpoint, "defaultDeliveryMode"));
    assertFalse(TestUtils.getPropertyValue(endpoint, "lazyConnect", Boolean.class));
    assertEquals("42", TestUtils.getPropertyValue(endpoint, "delayExpression", org.springframework.expression.Expression.class).getExpressionString());
    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);
    final AtomicBoolean shouldBePersistent = new AtomicBoolean();
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        org.springframework.amqp.core.Message amqpMessage = (org.springframework.amqp.core.Message) args[2];
        MessageProperties properties = amqpMessage.getMessageProperties();
        assertEquals("foo", properties.getHeaders().get("foo"));
        assertEquals("foobar", properties.getHeaders().get("foobar"));
        assertNull(properties.getHeaders().get("bar"));
        assertEquals(shouldBePersistent.get() ? MessageDeliveryMode.PERSISTENT : MessageDeliveryMode.NON_PERSISTENT, properties.getDeliveryMode());
        return null;
    }).when(amqpTemplate).send(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(org.springframework.amqp.core.Message.class), Mockito.any(CorrelationData.class));
    ReflectionUtils.setField(amqpTemplateField, endpoint, amqpTemplate);
    MessageChannel requestChannel = context.getBean("requestChannel", MessageChannel.class);
    Message<?> message = MessageBuilder.withPayload("hello").setHeader("foo", "foo").setHeader("bar", "bar").setHeader("foobar", "foobar").build();
    requestChannel.send(message);
    Mockito.verify(amqpTemplate, Mockito.times(1)).send(anyString(), isNull(), Mockito.any(org.springframework.amqp.core.Message.class), isNull());
    shouldBePersistent.set(true);
    message = MessageBuilder.withPayload("hello").setHeader("foo", "foo").setHeader("bar", "bar").setHeader("foobar", "foobar").setHeader(AmqpHeaders.DELIVERY_MODE, MessageDeliveryMode.PERSISTENT).build();
    requestChannel.send(message);
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Field(java.lang.reflect.Field) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint) CorrelationData(org.springframework.amqp.rabbit.support.CorrelationData) MessageChannel(org.springframework.messaging.MessageChannel) MessageProperties(org.springframework.amqp.core.MessageProperties) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 10 with AmqpOutboundEndpoint

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

the class AmqpOutboundChannelAdapterParserTests method amqpOutboundChannelAdapterWithinChain.

@SuppressWarnings("rawtypes")
@Test
public void amqpOutboundChannelAdapterWithinChain() {
    Object eventDrivenConsumer = context.getBean("chainWithRabbitOutbound");
    List chainHandlers = TestUtils.getPropertyValue(eventDrivenConsumer, "handler.handlers", List.class);
    AmqpOutboundEndpoint endpoint = (AmqpOutboundEndpoint) chainHandlers.get(0);
    assertNull(TestUtils.getPropertyValue(endpoint, "defaultDeliveryMode"));
    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 amqpMessage = (org.springframework.amqp.core.Message) args[2];
        MessageProperties properties = amqpMessage.getMessageProperties();
        assertEquals("hello", new String(amqpMessage.getBody()));
        assertEquals(MessageDeliveryMode.PERSISTENT, properties.getDeliveryMode());
        return null;
    }).when(amqpTemplate).send(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(org.springframework.amqp.core.Message.class), Mockito.any(CorrelationData.class));
    ReflectionUtils.setField(amqpTemplateField, endpoint, amqpTemplate);
    MessageChannel requestChannel = context.getBean("amqpOutboundChannelAdapterWithinChain", MessageChannel.class);
    Message<?> message = MessageBuilder.withPayload("hello").build();
    requestChannel.send(message);
    Mockito.verify(amqpTemplate, Mockito.times(1)).send(Mockito.any(String.class), isNull(), Mockito.any(org.springframework.amqp.core.Message.class), isNull());
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Field(java.lang.reflect.Field) AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint) CorrelationData(org.springframework.amqp.rabbit.support.CorrelationData) MessageChannel(org.springframework.messaging.MessageChannel) MessageProperties(org.springframework.amqp.core.MessageProperties) List(java.util.List) 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