GH-62: Align Message Handlers for common API

Fixes: spring-projects/spring-integration-aws#62

* Make `SnsMessageHandler extends AbstractAwsMessageHandler`
* Remove SNS Outbound Gateway variant since `SnsMessageHandler` covers
that part via `successChannel` and `failureChannel`
* Fix XSD for SQS and SNS
* Fix SQS and SNS tests according their logic changes
* Fix README for new changes
This commit is contained in:
Artem Bilan
2017-11-22 12:13:32 -05:00
parent b4c7690cf1
commit 2e63b2c682
23 changed files with 903 additions and 770 deletions

View File

@@ -8,34 +8,39 @@
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
<bean id="amazonSns" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="com.amazonaws.services.sns.AmazonSNS"/>
<constructor-arg value="com.amazonaws.services.sns.AmazonSNSAsync"/>
</bean>
<bean id="resourceIdResolver" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.cloud.aws.core.env.ResourceIdResolver" />
</bean>
<bean id="errorMessageStrategy" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.integration.support.ErrorMessageStrategy"/>
</bean>
<bean id="asyncHandler" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="com.amazonaws.handlers.AsyncHandler"/>
</bean>
<int:channel id="successChannel"/>
<int:channel id="notificationChannel"/>
<int-aws:sns-outbound-channel-adapter
id="defaultAdapter"
channel="notificationChannel"
failure-channel="errorChannel"
sns="amazonSns"
success-channel="successChannel"
error-message-strategy="errorMessageStrategy"
async-handler="asyncHandler"
send-timeout="202"
sync="false"
resource-id-resolver="resourceIdResolver">
<int-aws:request-handler-advice-chain>
<bean class="org.springframework.integration.handler.advice.RequestHandlerRetryAdvice"/>
</int-aws:request-handler-advice-chain>
</int-aws:sns-outbound-channel-adapter>
<int:channel id="notificationChannel"/>
<int-aws:sns-outbound-gateway
id="snsGateway"
sns="amazonSns"
request-channel="notificationChannel"
reply-channel="errorChannel"
topic-arn="foo"
subject="bar"
body-expression="payload.toUpperCase()"
resource-id-resolver="resourceIdResolver"
auto-startup="false"
phase="201"/>
</beans>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,18 +18,14 @@ package org.springframework.integration.aws.config.xml;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.aws.core.env.ResourceIdResolver;
import org.springframework.expression.Expression;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.handler.advice.RequestHandlerRetryAdvice;
import org.springframework.integration.support.ErrorMessageStrategy;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
@@ -37,7 +33,8 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.handlers.AsyncHandler;
import com.amazonaws.services.sns.AmazonSNSAsync;
/**
* @author Artem Bilan
@@ -48,18 +45,14 @@ import com.amazonaws.services.sns.AmazonSNS;
public class SnsOutboundChannelAdapterParserTests {
@Autowired
private AmazonSNS amazonSns;
@Autowired
@Qualifier("defaultAdapter")
private MessageChannel defaultAdapterChannel;
private AmazonSNSAsync amazonSns;
@Autowired
@Qualifier("errorChannel")
private MessageChannel errorChannel;
@Autowired
@Qualifier("defaultAdapter.adapter")
@Qualifier("defaultAdapter")
private AbstractEndpoint defaultAdapter;
@Autowired
@@ -70,29 +63,24 @@ public class SnsOutboundChannelAdapterParserTests {
@Qualifier("notificationChannel")
private MessageChannel notificationChannel;
@Autowired
@Qualifier("snsGateway")
private AbstractEndpoint snsGateway;
@Autowired
@Qualifier("snsGateway.handler")
private MessageHandler snsGatewayHandler;
@Autowired
private ResourceIdResolver resourceIdResolver;
@Autowired
private ErrorMessageStrategy errorMessageStrategy;
@Autowired
private AsyncHandler<?, ?> asyncHandler;
@Autowired
private MessageChannel successChannel;
@Test
public void testSnsOutboundChannelAdapterDefaultParser() throws Exception {
public void testSnsOutboundChannelAdapterDefaultParser() {
Object handler = TestUtils.getPropertyValue(this.defaultAdapter, "handler");
assertThat(AopUtils.isAopProxy(handler)).isFalse();
assertThat(handler).isSameAs(this.defaultAdapterHandler);
assertThat(TestUtils.getPropertyValue(handler, "adviceChain", List.class).get(0))
.isInstanceOf(RequestHandlerRetryAdvice.class);
assertThat(TestUtils.getPropertyValue(this.defaultAdapter, "inputChannel"))
.isSameAs(this.defaultAdapterChannel);
.isSameAs(this.notificationChannel);
assertThat(TestUtils.getPropertyValue(this.defaultAdapterHandler, "amazonSns")).isSameAs(this.amazonSns);
assertThat(TestUtils.getPropertyValue(this.defaultAdapterHandler, "evaluationContext")).isNotNull();
@@ -101,30 +89,28 @@ public class SnsOutboundChannelAdapterParserTests {
assertThat(TestUtils.getPropertyValue(this.defaultAdapterHandler, "bodyExpression")).isNull();
assertThat(TestUtils.getPropertyValue(this.defaultAdapterHandler, "resourceIdResolver"))
.isSameAs(this.resourceIdResolver);
}
assertThat(TestUtils.getPropertyValue(this.defaultAdapterHandler, "failureChannel"))
.isSameAs(this.errorChannel);
@Test
public void testSnsOutboundGatewayParser() {
assertThat(TestUtils.getPropertyValue(this.snsGateway, "inputChannel")).isSameAs(this.notificationChannel);
assertThat(TestUtils.getPropertyValue(this.snsGateway, "handler")).isSameAs(this.snsGatewayHandler);
assertThat(TestUtils.getPropertyValue(this.snsGateway, "autoStartup", Boolean.class)).isFalse();
assertThat(TestUtils.getPropertyValue(this.snsGateway, "phase", Integer.class)).isEqualTo(201);
assertThat(TestUtils.getPropertyValue(this.snsGatewayHandler, "produceReply", Boolean.class)).isTrue();
assertThat(TestUtils.getPropertyValue(this.snsGatewayHandler, "outputChannel")).isSameAs(this.errorChannel);
assertThat(TestUtils.getPropertyValue(this.snsGatewayHandler, "resourceIdResolver"))
assertThat(TestUtils.getPropertyValue(this.defaultAdapterHandler, "resourceIdResolver"))
.isSameAs(this.resourceIdResolver);
assertThat(TestUtils.getPropertyValue(this.snsGatewayHandler, "amazonSns")).isSameAs(this.amazonSns);
assertThat(TestUtils.getPropertyValue(this.snsGatewayHandler, "evaluationContext")).isNotNull();
assertThat(TestUtils.getPropertyValue(this.snsGatewayHandler, "topicArnExpression", Expression.class)
.getExpressionString())
.isEqualTo("foo");
assertThat(TestUtils.getPropertyValue(this.snsGatewayHandler, "subjectExpression", Expression.class)
.getExpressionString())
.isEqualTo("bar");
assertThat(TestUtils.getPropertyValue(this.snsGatewayHandler, "bodyExpression", Expression.class)
.getExpressionString())
.isEqualTo("payload.toUpperCase()");
assertThat(TestUtils.getPropertyValue(this.defaultAdapterHandler, "outputChannel"))
.isSameAs(this.successChannel);
assertThat(TestUtils.getPropertyValue(this.defaultAdapterHandler, "errorMessageStrategy"))
.isSameAs(this.errorMessageStrategy);
assertThat(TestUtils.getPropertyValue(this.defaultAdapterHandler, "asyncHandler"))
.isSameAs(this.asyncHandler);
assertThat(TestUtils.getPropertyValue(this.defaultAdapterHandler, "sync", Boolean.class))
.isFalse();
assertThat(TestUtils.getPropertyValue(this.defaultAdapterHandler,
"sendTimeoutExpression.literalValue"))
.isEqualTo("202");
}
}

View File

@@ -2,9 +2,11 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int-aws="http://www.springframework.org/schema/integration/aws"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:aws-messaging="http://www.springframework.org/schema/cloud/aws/messaging"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/aws http://www.springframework.org/schema/integration/aws/spring-integration-aws.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/cloud/aws/messaging http://www.springframework.org/schema/cloud/aws/messaging/spring-cloud-aws-messaging.xsd">
<aws-messaging:sqs-async-client id="sqs"/>
@@ -13,12 +15,38 @@
<constructor-arg value="org.springframework.cloud.aws.core.env.ResourceIdResolver"/>
</bean>
<bean id="errorMessageStrategy" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.integration.support.ErrorMessageStrategy"/>
</bean>
<bean id="messageConverter" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.messaging.converter.MessageConverter"/>
</bean>
<bean id="asyncHandler" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="com.amazonaws.handlers.AsyncHandler"/>
</bean>
<int:channel id="failureChannel"/>
<int:channel id="successChannel"/>
<int-aws:sqs-outbound-channel-adapter sqs="sqs"
auto-startup="false"
channel="errorChannel"
phase="100"
id="sqsOutboundChannelAdapter"
queue="foo"
delay-expression="'200'"
message-deduplication-id="foo"
message-group-id-expression="'bar'"
send-timeout="202"
sync="false"
error-message-strategy="errorMessageStrategy"
failure-channel="failureChannel"
success-channel="successChannel"
message-converter="messageConverter"
async-handler="asyncHandler"
resource-id-resolver="resourceIdResolver"/>
</beans>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 the original author or authors.
* Copyright 2015-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,13 +25,16 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.aws.core.env.ResourceIdResolver;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.support.ErrorMessageStrategy;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.amazonaws.handlers.AsyncHandler;
import com.amazonaws.services.sqs.AmazonSQS;
/**
@@ -48,9 +51,24 @@ public class SqsMessageHandlerParserTests {
@Autowired
private ResourceIdResolver resourceIdResolver;
@Autowired
private ErrorMessageStrategy errorMessageStrategy;
@Autowired
private MessageConverter messageConverter;
@Autowired
private AsyncHandler<?, ?> asyncHandler;
@Autowired
private MessageChannel errorChannel;
@Autowired
private MessageChannel failureChannel;
@Autowired
private MessageChannel successChannel;
@Autowired
private EventDrivenConsumer sqsOutboundChannelAdapter;
@@ -60,10 +78,10 @@ public class SqsMessageHandlerParserTests {
@Test
public void testSqsMessageHandlerParser() {
assertThat(TestUtils.getPropertyValue(this.sqsOutboundChannelAdapterHandler, "template.amazonSqs"))
assertThat(TestUtils.getPropertyValue(this.sqsOutboundChannelAdapterHandler, "amazonSqs"))
.isSameAs(this.amazonSqs);
assertThat(TestUtils.getPropertyValue(this.sqsOutboundChannelAdapterHandler,
"template.destinationResolver.targetDestinationResolver.resourceIdResolver"))
"destinationResolver.resourceIdResolver"))
.isSameAs(this.resourceIdResolver);
assertThat(TestUtils.getPropertyValue(this.sqsOutboundChannelAdapterHandler,
"queueExpression.literalValue"))
@@ -75,6 +93,40 @@ public class SqsMessageHandlerParserTests {
.isSameAs(this.errorChannel);
assertThat(TestUtils.getPropertyValue(this.sqsOutboundChannelAdapter, "handler"))
.isSameAs(this.sqsOutboundChannelAdapterHandler);
assertThat(TestUtils.getPropertyValue(this.sqsOutboundChannelAdapterHandler,
"delayExpression.expression"))
.isEqualTo("'200'");
assertThat(TestUtils.getPropertyValue(this.sqsOutboundChannelAdapterHandler,
"messageDeduplicationIdExpression.literalValue"))
.isEqualTo("foo");
assertThat(TestUtils.getPropertyValue(this.sqsOutboundChannelAdapterHandler,
"messageGroupIdExpression.expression"))
.isEqualTo("'bar'");
assertThat(TestUtils.getPropertyValue(this.sqsOutboundChannelAdapterHandler, "failureChannel"))
.isSameAs(this.failureChannel);
assertThat(TestUtils.getPropertyValue(this.sqsOutboundChannelAdapterHandler, "outputChannel"))
.isSameAs(this.successChannel);
assertThat(TestUtils.getPropertyValue(this.sqsOutboundChannelAdapterHandler, "messageConverter"))
.isSameAs(this.messageConverter);
assertThat(TestUtils.getPropertyValue(this.sqsOutboundChannelAdapterHandler, "errorMessageStrategy"))
.isSameAs(this.errorMessageStrategy);
assertThat(TestUtils.getPropertyValue(this.sqsOutboundChannelAdapterHandler, "asyncHandler"))
.isSameAs(this.asyncHandler);
assertThat(TestUtils.getPropertyValue(this.sqsOutboundChannelAdapterHandler, "sync", Boolean.class))
.isFalse();
assertThat(TestUtils.getPropertyValue(this.sqsOutboundChannelAdapterHandler,
"sendTimeoutExpression.literalValue"))
.isEqualTo("202");
}
}

View File

@@ -13,8 +13,6 @@
<constructor-arg value="org.springframework.cloud.aws.core.env.ResourceIdResolver"/>
</bean>
<aws-messaging:queue-messaging-template amazon-sqs="sqs" id="queueMessagingTemplate"/>
<int-aws:sqs-outbound-channel-adapter sqs="sqs"
auto-startup="false"
channel="errorChannel"
@@ -23,11 +21,4 @@
queue="foo"
resource-id-resolver="resourceIdResolver"/>
<int-aws:sqs-outbound-channel-adapter auto-startup="false"
channel="errorChannel"
phase="100"
id="sqsOutboundChannelAdapterWithQueueMessagingTemplate"
queue="foo"
queue-messaging-template="queueMessagingTemplate"/>
</beans>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 the original author or authors.
* Copyright 2015-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,15 +16,10 @@
package org.springframework.integration.aws.config.xml;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.aws.outbound.SqsMessageHandler;
import org.springframework.integration.test.util.TestUtils;
/**
* @author Rahul Pilani
@@ -52,21 +47,4 @@ public class SqsOutboundChannelAdapterParserTests {
new ClassPathXmlApplicationContext("SqsOutboundChannelAdapterParserTests-context-bad4.xml", getClass()).close();
}
@Test
public void test_happy_path_with_queue_messaging_template() {
ConfigurableApplicationContext applicationContext =
new ClassPathXmlApplicationContext("SqsOutboundChannelAdapterParserTests-context-good.xml", getClass());
SqsMessageHandler handlerWithTemplate =
applicationContext.getBean("sqsOutboundChannelAdapterWithQueueMessagingTemplate.handler",
SqsMessageHandler.class);
assertThat(TestUtils.getPropertyValue(handlerWithTemplate, "template")).isNotNull();
SqsMessageHandler handlerWithSqs = applicationContext.getBean("sqsOutboundChannelAdapterWithSqs.handler",
SqsMessageHandler.class);
assertThat(TestUtils.getPropertyValue(handlerWithSqs, "template")).isNotNull();
applicationContext.close();
}
}

View File

@@ -1,94 +0,0 @@
/*
* Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.aws.outbound;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.integration.aws.support.AwsHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.test.annotation.DirtiesContext;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.model.SendMessageRequest;
/**
* Parent class to contain tests that exercise the SqsMessageHandler class
*
* Subclasses can instantiate SqsMessageHandler in their own way.
*
* @author Rahul Pilani
* @author Artem Bilan
*/
@DirtiesContext
public abstract class AbstractSqsMessageHandlerTests {
@Autowired
protected AmazonSQS amazonSqs;
@Autowired
protected MessageChannel sqsSendChannel;
@Autowired
protected SqsMessageHandler sqsMessageHandler;
@Test
public void testSqsMessageHandler() {
Message<String> message = MessageBuilder.withPayload("message").build();
try {
this.sqsSendChannel.send(message);
}
catch (Exception e) {
assertThat(e).isInstanceOf(MessageHandlingException.class);
assertThat(e.getCause()).isInstanceOf(IllegalStateException.class);
}
this.sqsMessageHandler.setQueue("foo");
this.sqsSendChannel.send(message);
ArgumentCaptor<SendMessageRequest> sendMessageRequestArgumentCaptor =
ArgumentCaptor.forClass(SendMessageRequest.class);
verify(this.amazonSqs).sendMessage(sendMessageRequestArgumentCaptor.capture());
assertThat(sendMessageRequestArgumentCaptor.getValue().getQueueUrl())
.isEqualTo("http://queue-url.com/foo");
message = MessageBuilder.withPayload("message").setHeader(AwsHeaders.QUEUE, "bar").build();
this.sqsSendChannel.send(message);
verify(this.amazonSqs, times(2)).sendMessage(sendMessageRequestArgumentCaptor.capture());
assertThat(sendMessageRequestArgumentCaptor.getValue().getQueueUrl())
.isEqualTo("http://queue-url.com/bar");
SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
Expression expression = spelExpressionParser.parseExpression("headers.foo");
this.sqsMessageHandler.setQueueExpression(expression);
message = MessageBuilder.withPayload("message").setHeader("foo", "baz").build();
this.sqsSendChannel.send(message);
verify(this.amazonSqs, times(3)).sendMessage(sendMessageRequestArgumentCaptor.capture());
assertThat(sendMessageRequestArgumentCaptor.getValue().getQueueUrl())
.isEqualTo("http://queue-url.com/baz");
}
}

View File

@@ -39,11 +39,14 @@ import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.PollableChannel;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.handlers.AsyncHandler;
import com.amazonaws.services.sns.AmazonSNSAsync;
import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.sns.model.PublishResult;
@@ -61,28 +64,29 @@ public class SnsMessageHandlerTests {
private MessageChannel sendToSnsChannel;
@Autowired
private AmazonSNS amazonSNS;
private AmazonSNSAsync amazonSNS;
@Autowired
private PollableChannel resultChannel;
@Test
@SuppressWarnings("unchecked")
public void testSnsMessageHandler() {
SnsBodyBuilder payload = SnsBodyBuilder.withDefault("foo")
.forProtocols("{\"foo\" : \"bar\"}", "sms");
QueueChannel replyChannel = new QueueChannel();
Message<?> message = MessageBuilder.withPayload(payload)
.setHeader("topic", "topic")
.setHeader("subject", "subject")
.setReplyChannel(replyChannel)
.build();
this.sendToSnsChannel.send(message);
Message<?> reply = replyChannel.receive(1000);
Message<?> reply = this.resultChannel.receive(10000);
assertThat(reply).isNotNull();
ArgumentCaptor<PublishRequest> captor = ArgumentCaptor.forClass(PublishRequest.class);
verify(this.amazonSNS).publish(captor.capture());
verify(this.amazonSNS).publishAsync(captor.capture(), any(AsyncHandler.class));
PublishRequest publishRequest = captor.getValue();
@@ -92,9 +96,9 @@ public class SnsMessageHandlerTests {
assertThat(publishRequest.getMessage())
.isEqualTo("{\"default\":\"foo\",\"sms\":\"{\\\"foo\\\" : \\\"bar\\\"}\"}");
assertThat(reply.getHeaders().get(AwsHeaders.SNS_PUBLISHED_MESSAGE_ID)).isEqualTo("111");
assertThat(reply.getHeaders().get(AwsHeaders.MESSAGE_ID)).isEqualTo("111");
assertThat(reply.getHeaders().get(AwsHeaders.TOPIC)).isEqualTo("topic");
assertThat(reply.getPayload()).isSameAs(publishRequest);
assertThat(reply.getPayload()).isSameAs(payload);
}
@Configuration
@@ -102,23 +106,35 @@ public class SnsMessageHandlerTests {
public static class ContextConfiguration {
@Bean
public AmazonSNS amazonSNS() {
AmazonSNS mock = mock(AmazonSNS.class);
@SuppressWarnings("unchecked")
public AmazonSNSAsync amazonSNS() {
AmazonSNSAsync mock = mock(AmazonSNSAsync.class);
willAnswer(invocation -> new PublishResult().withMessageId("111"))
willAnswer(invocation -> {
PublishResult publishResult = new PublishResult().withMessageId("111");
AsyncHandler<PublishRequest, PublishResult> asyncHandler = invocation.getArgument(1);
asyncHandler.onSuccess(invocation.getArgument(0), publishResult);
return new AsyncResult<>(publishResult);
})
.given(mock)
.publish(any(PublishRequest.class));
.publishAsync(any(PublishRequest.class), any(AsyncHandler.class));
return mock;
}
@Bean
public PollableChannel resultChannel() {
return new QueueChannel();
}
@Bean
@ServiceActivator(inputChannel = "sendToSnsChannel")
public MessageHandler snsMessageHandler() {
SnsMessageHandler snsMessageHandler = new SnsMessageHandler(amazonSNS(), true);
SnsMessageHandler snsMessageHandler = new SnsMessageHandler(amazonSNS());
snsMessageHandler.setTopicArnExpression(PARSER.parseExpression("headers.topic"));
snsMessageHandler.setSubjectExpression(PARSER.parseExpression("headers.subject"));
snsMessageHandler.setBodyExpression(PARSER.parseExpression("payload"));
snsMessageHandler.setOutputChannel(resultChannel());
return snsMessageHandler;
}

View File

@@ -16,23 +16,38 @@
package org.springframework.integration.aws.outbound;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.willAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.aws.support.AwsHeaders;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.amazonaws.handlers.AsyncHandler;
import com.amazonaws.services.sqs.AmazonSQSAsync;
import com.amazonaws.services.sqs.model.GetQueueUrlRequest;
import com.amazonaws.services.sqs.model.GetQueueUrlResult;
import com.amazonaws.services.sqs.model.SendMessageRequest;
/**
* Instantiating SqsMessageHandler using amazonSqs.
@@ -42,8 +57,57 @@ import com.amazonaws.services.sqs.model.GetQueueUrlResult;
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SqsMessageHandlerTests extends AbstractSqsMessageHandlerTests {
public class SqsMessageHandlerTests {
@Autowired
protected AmazonSQSAsync amazonSqs;
@Autowired
protected MessageChannel sqsSendChannel;
@Autowired
protected SqsMessageHandler sqsMessageHandler;
@Test
@SuppressWarnings("unchecked")
public void testSqsMessageHandler() {
Message<String> message = MessageBuilder.withPayload("message").build();
try {
this.sqsSendChannel.send(message);
}
catch (Exception e) {
assertThat(e).isInstanceOf(MessageHandlingException.class);
assertThat(e.getCause()).isInstanceOf(IllegalStateException.class);
}
this.sqsMessageHandler.setQueue("foo");
this.sqsSendChannel.send(message);
ArgumentCaptor<SendMessageRequest> sendMessageRequestArgumentCaptor =
ArgumentCaptor.forClass(SendMessageRequest.class);
verify(this.amazonSqs)
.sendMessageAsync(sendMessageRequestArgumentCaptor.capture(), any(AsyncHandler.class));
assertThat(sendMessageRequestArgumentCaptor.getValue().getQueueUrl())
.isEqualTo("http://queue-url.com/foo");
message = MessageBuilder.withPayload("message").setHeader(AwsHeaders.QUEUE, "bar").build();
this.sqsSendChannel.send(message);
verify(this.amazonSqs, times(2))
.sendMessageAsync(sendMessageRequestArgumentCaptor.capture(), any(AsyncHandler.class));
assertThat(sendMessageRequestArgumentCaptor.getValue().getQueueUrl())
.isEqualTo("http://queue-url.com/bar");
SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
Expression expression = spelExpressionParser.parseExpression("headers.foo");
this.sqsMessageHandler.setQueueExpression(expression);
message = MessageBuilder.withPayload("message").setHeader("foo", "baz").build();
this.sqsSendChannel.send(message);
verify(this.amazonSqs, times(3))
.sendMessageAsync(sendMessageRequestArgumentCaptor.capture(), any(AsyncHandler.class));
assertThat(sendMessageRequestArgumentCaptor.getValue().getQueueUrl())
.isEqualTo("http://queue-url.com/baz");
}
@Configuration
@EnableIntegration

View File

@@ -1,81 +0,0 @@
/*
* Copyright 2015-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.aws.outbound;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.willAnswer;
import static org.mockito.Mockito.mock;
import org.junit.runner.RunWith;
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.messaging.MessageHandler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.amazonaws.services.sqs.AmazonSQSAsync;
import com.amazonaws.services.sqs.model.GetQueueUrlRequest;
import com.amazonaws.services.sqs.model.GetQueueUrlResult;
/**
* Instantiating SqsMessageHandler using QueueMessagingTemplate.
*
* @author Rahul Pilani
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SqsMessageHandlerWithQueueMessagingTemplateTests extends AbstractSqsMessageHandlerTests {
@Configuration
@EnableIntegration
public static class ContextConfiguration {
@Bean
public AmazonSQSAsync amazonSqs() {
AmazonSQSAsync amazonSqs = mock(AmazonSQSAsync.class);
willAnswer(invocation -> {
GetQueueUrlRequest getQueueUrlRequest = (GetQueueUrlRequest) invocation.getArguments()[0];
GetQueueUrlResult queueUrl = new GetQueueUrlResult();
queueUrl.setQueueUrl("http://queue-url.com/" + getQueueUrlRequest.getQueueName());
return queueUrl;
})
.given(amazonSqs)
.getQueueUrl(any(GetQueueUrlRequest.class));
return amazonSqs;
}
@Bean
public QueueMessagingTemplate queueMessagingTemplate() {
return new QueueMessagingTemplate(amazonSqs());
}
@Bean
@ServiceActivator(inputChannel = "sqsSendChannel")
public MessageHandler sqsMessageHandler() {
return new SqsMessageHandler(queueMessagingTemplate());
}
}
}