Add S3MessageHandler Namespace Support
* Add `<int-aws:s3-outbound-channel-adapter>` and `<int-aws:s3-outbound-gateway>` components. Their parsers and tests for them. * Remove the old S3 Outbound Channel Adapter implementation and its tests Rename `AwsNamespaceHandler` properly `git mv -f File file` does the trick Add `.travis.yml` to enable Travis CI on PRs
This commit is contained in:
committed by
Gary Russell
parent
a0893cb88e
commit
5167b36b4d
@@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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"
|
||||
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">
|
||||
|
||||
<bean id="s3" class="org.mockito.Mockito" factory-method="mock">
|
||||
<constructor-arg value="com.amazonaws.services.s3.AmazonS3"/>
|
||||
</bean>
|
||||
|
||||
<bean id="s3ProgressListener" class="org.mockito.Mockito" factory-method="mock">
|
||||
<constructor-arg value="com.amazonaws.services.s3.transfer.internal.S3ProgressListener"/>
|
||||
</bean>
|
||||
|
||||
<bean id="uploadMetadataProvider" class="org.mockito.Mockito" factory-method="mock">
|
||||
<constructor-arg value="org.springframework.integration.aws.outbound.S3MessageHandler$UploadMetadataProvider"/>
|
||||
</bean>
|
||||
|
||||
<int-aws:s3-outbound-channel-adapter s3="s3"
|
||||
auto-startup="false"
|
||||
channel="errorChannel"
|
||||
phase="100"
|
||||
id="s3OutboundChannelAdapter"
|
||||
bucket="foo"
|
||||
key-expression="payload.name"
|
||||
command="COPY"
|
||||
destination-bucket-expression="'bar'"
|
||||
destination-key-expression="'baz'"
|
||||
object-acl-expression="'qux'"
|
||||
progress-listener="s3ProgressListener"
|
||||
upload-metadata-provider="uploadMetadataProvider"/>
|
||||
|
||||
<bean id="transferManager" class="com.amazonaws.services.s3.transfer.TransferManager"/>
|
||||
|
||||
<int-aws:s3-outbound-gateway transfer-manager="transferManager"
|
||||
request-channel="errorChannel"
|
||||
id="s3OutboundGateway"
|
||||
bucket-expression="'FOO'"
|
||||
command-expression="'DOWNLOAD'"
|
||||
reply-channel="nullChannel"/>
|
||||
|
||||
<!--Invalid configs-->
|
||||
|
||||
<!--One of 'bucket' or 'bucket-expression' is required-->
|
||||
<!--<int-aws:s3-outbound-channel-adapter s3="s3" id="bucketRequired"/>-->
|
||||
|
||||
<!--One and only of 's3' and 'transfer-manager' attributes must be provided-->
|
||||
<!--<int-aws:s3-outbound-channel-adapter s3="s3" transfer-manager="transferManager" id="onlyOneS3OrTransferManager"/>-->
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright 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.config.xml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.integration.aws.outbound.S3MessageHandler;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.expression.ExpressionUtils;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
import com.amazonaws.services.s3.transfer.TransferManager;
|
||||
import com.amazonaws.services.s3.transfer.internal.S3ProgressListener;
|
||||
|
||||
/**
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
public class S3MessageHandlerParserTests {
|
||||
|
||||
@Autowired
|
||||
private AmazonS3 amazonS3;
|
||||
|
||||
@Autowired
|
||||
private TransferManager transferManager;
|
||||
|
||||
@Autowired
|
||||
private MessageChannel errorChannel;
|
||||
|
||||
@Autowired
|
||||
private MessageChannel nullChannel;
|
||||
|
||||
@Autowired
|
||||
private EventDrivenConsumer s3OutboundChannelAdapter;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("s3OutboundChannelAdapter.handler")
|
||||
private MessageHandler s3OutboundChannelAdapterHandler;
|
||||
|
||||
@Autowired
|
||||
private EventDrivenConsumer s3OutboundGateway;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("s3OutboundGateway.handler")
|
||||
private MessageHandler s3OutboundGatewayHandler;
|
||||
|
||||
@Autowired
|
||||
private S3ProgressListener progressListener;
|
||||
|
||||
@Autowired
|
||||
private S3MessageHandler.UploadMetadataProvider uploadMetadataProvider;
|
||||
|
||||
@Autowired
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
@Test
|
||||
public void testS3OutboundChannelAdapterParser() {
|
||||
assertSame(this.amazonS3,
|
||||
TestUtils.getPropertyValue(this.s3OutboundChannelAdapterHandler, "transferManager.s3"));
|
||||
assertEquals("foo", TestUtils.getPropertyValue(this.s3OutboundChannelAdapterHandler,
|
||||
"bucketExpression.literalValue"));
|
||||
assertEquals("'bar'", TestUtils.getPropertyValue(this.s3OutboundChannelAdapterHandler,
|
||||
"destinationBucketExpression.expression"));
|
||||
assertEquals("'baz'", TestUtils.getPropertyValue(this.s3OutboundChannelAdapterHandler,
|
||||
"destinationKeyExpression.expression"));
|
||||
assertEquals("payload.name", TestUtils.getPropertyValue(this.s3OutboundChannelAdapterHandler,
|
||||
"keyExpression.expression"));
|
||||
assertEquals("'qux'", TestUtils.getPropertyValue(this.s3OutboundChannelAdapterHandler,
|
||||
"objectAclExpression.expression"));
|
||||
assertEquals(S3MessageHandler.Command.COPY.name(),
|
||||
TestUtils.getPropertyValue(this.s3OutboundChannelAdapterHandler, "commandExpression.literalValue"));
|
||||
|
||||
assertFalse(TestUtils.getPropertyValue(this.s3OutboundChannelAdapterHandler, "produceReply", Boolean.class));
|
||||
|
||||
assertSame(this.progressListener,
|
||||
TestUtils.getPropertyValue(this.s3OutboundChannelAdapterHandler, "s3ProgressListener"));
|
||||
assertSame(this.uploadMetadataProvider,
|
||||
TestUtils.getPropertyValue(this.s3OutboundChannelAdapterHandler, "uploadMetadataProvider"));
|
||||
|
||||
assertEquals(100, this.s3OutboundChannelAdapter.getPhase());
|
||||
assertFalse(this.s3OutboundChannelAdapter.isAutoStartup());
|
||||
assertFalse(this.s3OutboundChannelAdapter.isRunning());
|
||||
assertSame(this.errorChannel, TestUtils.getPropertyValue(this.s3OutboundChannelAdapter, "inputChannel"));
|
||||
assertSame(this.s3OutboundChannelAdapterHandler,
|
||||
TestUtils.getPropertyValue(this.s3OutboundChannelAdapter, "handler"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testS3OutboundGatewayParser() {
|
||||
assertSame(this.transferManager,
|
||||
TestUtils.getPropertyValue(this.s3OutboundGatewayHandler, "transferManager"));
|
||||
assertEquals("'FOO'", TestUtils.getPropertyValue(this.s3OutboundGatewayHandler,
|
||||
"bucketExpression.expression"));
|
||||
Expression commandExpression =
|
||||
TestUtils.getPropertyValue(this.s3OutboundGatewayHandler, "commandExpression", Expression.class);
|
||||
assertEquals("'" + S3MessageHandler.Command.DOWNLOAD.name() + "'",
|
||||
TestUtils.getPropertyValue(commandExpression, "expression"));
|
||||
|
||||
StandardEvaluationContext evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.beanFactory);
|
||||
S3MessageHandler.Command command =
|
||||
commandExpression.getValue(evaluationContext, S3MessageHandler.Command.class);
|
||||
|
||||
assertEquals(S3MessageHandler.Command.DOWNLOAD, command);
|
||||
|
||||
assertTrue(TestUtils.getPropertyValue(this.s3OutboundGatewayHandler, "produceReply", Boolean.class));
|
||||
assertSame(this.nullChannel, TestUtils.getPropertyValue(this.s3OutboundGatewayHandler, "outputChannel"));
|
||||
|
||||
assertTrue(this.s3OutboundGateway.isRunning());
|
||||
assertSame(this.errorChannel, TestUtils.getPropertyValue(this.s3OutboundGateway, "inputChannel"));
|
||||
assertSame(this.s3OutboundGatewayHandler, TestUtils.getPropertyValue(this.s3OutboundGateway, "handler"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -57,6 +57,7 @@ import org.mockito.stubbing.Answer;
|
||||
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.http.MediaType;
|
||||
import org.springframework.integration.annotation.ServiceActivator;
|
||||
@@ -383,7 +384,9 @@ public class S3MessageHandlerTests {
|
||||
public MessageHandler s3MessageHandler() {
|
||||
S3MessageHandler s3MessageHandler = new S3MessageHandler(amazonS3(), "myBucket");
|
||||
s3MessageHandler.setCommandExpression(PARSER.parseExpression("headers.s3Command"));
|
||||
s3MessageHandler.setKeyExpression(PARSER.parseExpression("payload instanceof T(java.io.File) ? payload.name : headers.key"));
|
||||
Expression keyExpression =
|
||||
PARSER.parseExpression("payload instanceof T(java.io.File) ? payload.name : headers.key");
|
||||
s3MessageHandler.setKeyExpression(keyExpression);
|
||||
s3MessageHandler.setObjectAclExpression(new ValueExpression<>(CannedAccessControlList.PublicReadWrite));
|
||||
s3MessageHandler.setUploadMetadataProvider(new S3MessageHandler.UploadMetadataProvider() {
|
||||
|
||||
@@ -397,7 +400,7 @@ public class S3MessageHandlerTests {
|
||||
}
|
||||
|
||||
});
|
||||
s3MessageHandler.setS3ProgressListener(s3ProgressListener());
|
||||
s3MessageHandler.setProgressListener(s3ProgressListener());
|
||||
return s3MessageHandler;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,271 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2015 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.s3;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.springframework.integration.aws.s3.AmazonS3MessageHeaders.FILE_NAME;
|
||||
import static org.springframework.integration.aws.s3.AmazonS3MessageHeaders.METADATA;
|
||||
import static org.springframework.integration.aws.s3.AmazonS3MessageHeaders.OBJECT_ACLS;
|
||||
import static org.springframework.integration.aws.s3.AmazonS3MessageHeaders.USER_METADATA;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.expression.common.LiteralExpression;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.integration.aws.core.BasicAWSCredentials;
|
||||
import org.springframework.integration.aws.s3.core.AmazonS3Object;
|
||||
import org.springframework.integration.aws.s3.core.AmazonS3Operations;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandlingException;
|
||||
|
||||
/**
|
||||
* The test class for {@link AmazonS3MessageHandler}, we rely on mock of {@link AmazonS3Operations}
|
||||
* to test the behavior.
|
||||
*
|
||||
* @author Amol Nayak
|
||||
* @author Rob Harrop
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 0.5
|
||||
*
|
||||
*/
|
||||
public class AmazonS3MessageHandlerTests {
|
||||
|
||||
private static AmazonS3Operations operations;
|
||||
private static PutObjectParameterHolder holder = new PutObjectParameterHolder();
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder tempFolder = new TemporaryFolder();
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
operations = Mockito.mock(AmazonS3Operations.class);
|
||||
|
||||
doAnswer(new Answer<Object>() {
|
||||
public Object answer(InvocationOnMock inv) {
|
||||
Object[] args = inv.getArguments();
|
||||
holder.setBucket((String)args[0]);
|
||||
holder.setFolder((String)args[1]);
|
||||
holder.setObjectName((String)args[2]);
|
||||
holder.setS3Object((AmazonS3Object)args[3]);
|
||||
return null;
|
||||
}
|
||||
}).
|
||||
when(operations)
|
||||
.putObject(anyString(), anyString(), anyString(), any(AmazonS3Object.class));
|
||||
|
||||
|
||||
}
|
||||
private AmazonS3MessageHandler getHandler() {
|
||||
AmazonS3MessageHandler handler = new AmazonS3MessageHandler(new BasicAWSCredentials(), operations);
|
||||
//set the remote directory to root by default
|
||||
handler.setRemoteDirectoryExpression(new LiteralExpression("/"));
|
||||
handler.setBucket("TestBucket");
|
||||
handler.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
handler.afterPropertiesSet();
|
||||
return handler;
|
||||
}
|
||||
|
||||
private static class PutObjectParameterHolder {
|
||||
private String bucket;
|
||||
private String folder;
|
||||
private String objectName;
|
||||
private AmazonS3Object s3Object;
|
||||
|
||||
public String getBucket() {
|
||||
return bucket;
|
||||
}
|
||||
public void setBucket(String bucket) {
|
||||
this.bucket = bucket;
|
||||
}
|
||||
public String getFolder() {
|
||||
return folder;
|
||||
}
|
||||
public void setFolder(String folder) {
|
||||
this.folder = folder;
|
||||
}
|
||||
public String getObjectName() {
|
||||
return objectName;
|
||||
}
|
||||
public void setObjectName(String objectName) {
|
||||
this.objectName = objectName;
|
||||
}
|
||||
public AmazonS3Object getS3Object() {
|
||||
return s3Object;
|
||||
}
|
||||
public void setS3Object(AmazonS3Object s3Object) {
|
||||
this.s3Object = s3Object;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests with a message payload of type {@link String}
|
||||
*/
|
||||
@Test
|
||||
public void withStringPayload() {
|
||||
Message<String> message = MessageBuilder.withPayload("Test String").build();
|
||||
AmazonS3MessageHandler handler = getHandler();
|
||||
handler.handleMessage(message);
|
||||
AmazonS3Object object = holder.getS3Object();
|
||||
Assert.assertNotNull(object.getInputStream());
|
||||
Assert.assertNull(object.getFileSource());
|
||||
assertCommonValues(message,object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests with a message with payload of type {@link InputStream}
|
||||
*/
|
||||
@Test
|
||||
public void withInputStreamPayload() {
|
||||
InputStream bin = new ByteArrayInputStream("SomeString".getBytes());
|
||||
Message<InputStream> message = MessageBuilder.withPayload(bin).build();
|
||||
AmazonS3MessageHandler handler = getHandler();
|
||||
handler.handleMessage(message);
|
||||
AmazonS3Object object = holder.getS3Object();
|
||||
Assert.assertNotNull(object.getInputStream());
|
||||
Assert.assertNull(object.getFileSource());
|
||||
assertCommonValues(message,object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests with a message with payload of type byte[]
|
||||
*/
|
||||
@Test
|
||||
public void withByteArrayPayload() {
|
||||
Message<byte[]> message = MessageBuilder.withPayload("String".getBytes()).build();
|
||||
AmazonS3MessageHandler handler = getHandler();
|
||||
handler.handleMessage(message);
|
||||
AmazonS3Object object = holder.getS3Object();
|
||||
Assert.assertNotNull(object.getInputStream());
|
||||
Assert.assertNull(object.getFileSource());
|
||||
assertCommonValues(message,object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests with a message with payload of type {@link File} which is a file with temporary suffix
|
||||
*/
|
||||
@Test
|
||||
public void withTempFileTypePayload() throws Exception {
|
||||
final File file = tempFolder.newFile("TempFile.txt.writing");
|
||||
messageWithFileTypePayload(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests with a message with payload of type {@link File} which is a file without temporary suffix
|
||||
*/
|
||||
@Test
|
||||
public void withFileTypePayload() throws Exception {
|
||||
final File file = tempFolder.newFile("TempFile.txt");
|
||||
messageWithFileTypePayload(file);
|
||||
}
|
||||
|
||||
/**
|
||||
*Test case to with message of an incompatible type, {@link Integer} in this case.
|
||||
*
|
||||
*/
|
||||
@Test(expected=MessageHandlingException.class)
|
||||
public void withIncompatiblePayload() {
|
||||
Message<Integer> message = MessageBuilder.withPayload(1).build();
|
||||
AmazonS3MessageHandler handler = getHandler();
|
||||
handler.handleMessage(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests with all the header provided in the message
|
||||
*/
|
||||
@Test
|
||||
public void withAllHeaders() {
|
||||
Map<String, Collection<String>> acls = new HashMap<String, Collection<String>>();
|
||||
acls.put("test@test.com", Arrays.asList("Read", "Write acp"));
|
||||
Message<String> message = MessageBuilder.withPayload("Test Content")
|
||||
.setHeader(FILE_NAME, "TestFileName.txt")
|
||||
.setHeader(USER_METADATA, Collections.singletonMap("UserMD", "UserMD"))
|
||||
.setHeader(METADATA, Collections.singletonMap("Metadata", "Metadata"))
|
||||
.setHeader(OBJECT_ACLS, acls)
|
||||
.setHeader("remoteDirectory", "/remote")
|
||||
.build();
|
||||
AmazonS3MessageHandler handler = getHandler();
|
||||
SpelExpressionParser parser = new SpelExpressionParser();
|
||||
handler.setRemoteDirectoryExpression(parser.parseExpression("headers['remoteDirectory']"));
|
||||
handler.afterPropertiesSet();
|
||||
handler.handleMessage(message);
|
||||
Assert.assertEquals("TestBucket", holder.getBucket());
|
||||
Assert.assertEquals("TestFileName.txt", holder.getObjectName());
|
||||
Assert.assertEquals("/remote", holder.getFolder());
|
||||
AmazonS3Object object = holder.getS3Object();
|
||||
Assert.assertNotNull(object);
|
||||
Assert.assertNotNull(object.getInputStream());
|
||||
Assert.assertNotNull(object.getMetaData());
|
||||
Assert.assertNotNull(object.getUserMetaData());
|
||||
Assert.assertNotNull(object.getObjectACL());
|
||||
Assert.assertEquals(2,object.getObjectACL().getGrants().size());
|
||||
}
|
||||
|
||||
/**
|
||||
* The common method to test messages with payload of type {@link File}
|
||||
* @param file
|
||||
*/
|
||||
private void messageWithFileTypePayload(File file) throws Exception {
|
||||
file.createNewFile();
|
||||
Message<File> message = MessageBuilder.withPayload(file).build();
|
||||
AmazonS3MessageHandler handler = getHandler();
|
||||
handler.handleMessage(message);
|
||||
AmazonS3Object object = holder.getS3Object();
|
||||
Assert.assertEquals("TempFile.txt", holder.getObjectName());
|
||||
Assert.assertNotNull(object.getFileSource());
|
||||
Assert.assertNull(object.getInputStream());
|
||||
Assert.assertNull(object.getMetaData());
|
||||
Assert.assertNull(object.getObjectACL());
|
||||
Assert.assertNull(object.getUserMetaData());
|
||||
file.delete();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The method used to assert the values for tests with String, InputStream and byte[] parameters
|
||||
* @param message
|
||||
*/
|
||||
private void assertCommonValues(Message<?> message,AmazonS3Object object) {
|
||||
Assert.assertEquals(message.getHeaders().getId().toString() + ".ext", holder.getObjectName());
|
||||
Assert.assertEquals("/", holder.getFolder());
|
||||
Assert.assertEquals("TestBucket", holder.getBucket());
|
||||
Assert.assertNotNull(object);
|
||||
Assert.assertNull(object.getMetaData());
|
||||
Assert.assertNull(object.getObjectACL());
|
||||
Assert.assertNull(object.getUserMetaData());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,201 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2015 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.s3.config.xml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.springframework.integration.test.util.TestUtils.getPropertyValue;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.common.LiteralExpression;
|
||||
import org.springframework.expression.spel.standard.SpelExpression;
|
||||
import org.springframework.integration.aws.s3.AmazonS3MessageHandler;
|
||||
import org.springframework.integration.aws.s3.FileNameGenerationStrategy;
|
||||
import org.springframework.integration.aws.s3.core.AmazonS3Operations;
|
||||
import org.springframework.integration.aws.s3.core.DefaultAmazonS3Operations;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
/**
|
||||
* The test case for the aws-s3 namespace's {@link AmazonS3OutboundChannelAdapterParser} class
|
||||
* @author Amol Nayak
|
||||
* @author Rob Harrop
|
||||
* @author Karthikeyan Palanivelu
|
||||
* @since 0.5
|
||||
*/
|
||||
public class AmazonS3OutboundChannelAdapterParserTests {
|
||||
|
||||
private volatile static int adviceCalled;
|
||||
|
||||
/**
|
||||
* Test case for the xml definition with a custom implementation of {@link AmazonS3Operations}
|
||||
*/
|
||||
@Test
|
||||
public void withCustomOperations() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:s3-valid-outbound-cases.xml");
|
||||
EventDrivenConsumer consumer = ctx.getBean("withCustomService", EventDrivenConsumer.class);
|
||||
AmazonS3MessageHandler handler = getPropertyValue(consumer, "handler", AmazonS3MessageHandler.class);
|
||||
assertEquals(AmazonS3DummyOperations.class, getPropertyValue(handler, "operations").getClass());
|
||||
Expression expression =
|
||||
getPropertyValue(handler, "remoteDirectoryProcessor.expression", Expression.class);
|
||||
assertNotNull(expression);
|
||||
assertEquals(LiteralExpression.class, expression.getClass());
|
||||
assertEquals("/", getPropertyValue(expression, "literalValue", String.class));
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case for the xml definition with the default implementation of {@link AmazonS3Operations}
|
||||
*/
|
||||
@Test
|
||||
public void withDefaultOperationsImplementation() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:s3-valid-outbound-cases.xml");
|
||||
EventDrivenConsumer consumer = ctx.getBean("withDefaultServices", EventDrivenConsumer.class);
|
||||
AmazonS3MessageHandler handler = getPropertyValue(consumer, "handler", AmazonS3MessageHandler.class);
|
||||
assertEquals(DefaultAmazonS3Operations.class, getPropertyValue(handler, "operations").getClass());
|
||||
Expression expression =
|
||||
getPropertyValue(handler, "remoteDirectoryProcessor.expression", Expression.class);
|
||||
assertNotNull(expression);
|
||||
assertEquals(SpelExpression.class, expression.getClass());
|
||||
assertEquals("headers['remoteDirectory']", getPropertyValue(expression, "expression", String.class));
|
||||
assertEquals("TestBucket", getPropertyValue(handler, "bucket", String.class));
|
||||
assertEquals("US-ASCII", getPropertyValue(handler, "charset", String.class));
|
||||
assertEquals("dummy", getPropertyValue(handler, "credentials.accessKey", String.class));
|
||||
assertEquals("dummy", getPropertyValue(handler, "credentials.secretKey", String.class));
|
||||
assertEquals("dummy", getPropertyValue(handler, "operations.credentials.accessKey", String.class));
|
||||
assertEquals("dummy", getPropertyValue(handler, "operations.credentials.secretKey", String.class));
|
||||
assertEquals(5120, getPropertyValue(handler, "operations.multipartUploadThreshold", Long.class).longValue());
|
||||
assertEquals(".write", getPropertyValue(handler, "operations.temporaryFileSuffix", String.class));
|
||||
assertEquals(".write", getPropertyValue(handler, "fileNameGenerator.temporarySuffix", String.class));
|
||||
assertEquals("headers['name']", getPropertyValue(handler, "fileNameGenerator.fileNameExpression", String.class));
|
||||
assertEquals(ctx.getBean("executor"), getPropertyValue(handler, "operations.threadPoolExecutor"));
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case for the xml definition with a custom implementation of {@link FileNameGenerationStrategy}
|
||||
*/
|
||||
@Test
|
||||
public void withCustomNameGenerator() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("s3-valid-outbound-cases.xml");
|
||||
EventDrivenConsumer consumer = ctx.getBean("withCustomNameGenerator", EventDrivenConsumer.class);
|
||||
AmazonS3MessageHandler handler = getPropertyValue(consumer, "handler", AmazonS3MessageHandler.class);
|
||||
assertEquals(DummyFileNameGenerator.class, getPropertyValue(handler, "fileNameGenerator").getClass());
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case for the xml definition with a custom AWS endpoint
|
||||
*/
|
||||
@Test
|
||||
public void withCustomEndpoint() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("s3-valid-outbound-cases.xml");
|
||||
EventDrivenConsumer consumer = ctx.getBean("withCustomEndpoint", EventDrivenConsumer.class);
|
||||
AmazonS3MessageHandler handler = getPropertyValue(consumer, "handler", AmazonS3MessageHandler.class);
|
||||
assertEquals("http://s3-eu-west-1.amazonaws.com",
|
||||
getPropertyValue(handler, "operations.client.endpoint", URI.class).toString());
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Multi part upload should have a size of 5120 and above, any value less than 5120 will
|
||||
* thrown an exception
|
||||
*/
|
||||
@Test(expected = BeanCreationException.class)
|
||||
public void withMultiUploadLessThan5120() {
|
||||
new ClassPathXmlApplicationContext("s3-multiupload-lessthan-5120.xml").close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test with both the custom file generator and expression attribute set.
|
||||
*/
|
||||
@Test(expected = BeanDefinitionStoreException.class)
|
||||
public void withBothFileGeneratorAndExpression() {
|
||||
new ClassPathXmlApplicationContext("s3-both-customfilegenerator-and-expression.xml").close();
|
||||
}
|
||||
|
||||
/**
|
||||
* When custom implementation of {@link AmazonS3Operations} is provided, the attributes
|
||||
* multipart-upload-threshold, temporary-directory, temporary-suffix and thread-pool-executor
|
||||
* are not allowed
|
||||
*/
|
||||
@Test(expected = BeanDefinitionStoreException.class)
|
||||
public void withCustomOperationsAndDisallowedAttributes() {
|
||||
new ClassPathXmlApplicationContext("s3-custom-operations-with-disallowed-attributes.xml").close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the outbound channel adapter definition with a valid combination of attributes along with
|
||||
* request handler chain.
|
||||
*/
|
||||
@Test
|
||||
public void withHandlerAdviceChain() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("s3-valid-outbound-cases.xml");
|
||||
EventDrivenConsumer consumer = ctx.getBean("withHandlerChain", EventDrivenConsumer.class);
|
||||
MessageHandler handler = getPropertyValue(consumer, "handler", MessageHandler.class);
|
||||
handler.handleMessage(new GenericMessage<String>("String content: Test AWS advice chain"));
|
||||
assertEquals(1, adviceCalled);
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case for the xml definition with the Channel Attributes.
|
||||
*/
|
||||
@Test
|
||||
public void withChannelAttributeImplementation() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:s3-valid-outbound-cases.xml");
|
||||
EventDrivenConsumer consumer = ctx.getBean("withChannelAdapterAttributes", EventDrivenConsumer.class);
|
||||
AmazonS3MessageHandler handler = getPropertyValue(consumer, "handler", AmazonS3MessageHandler.class);
|
||||
assertEquals(DefaultAmazonS3Operations.class, getPropertyValue(handler, "operations").getClass());
|
||||
assertEquals("input", getPropertyValue(consumer, "inputChannel.beanName", String.class));
|
||||
assertEquals("US-ASCII", getPropertyValue(handler, "charset", String.class));
|
||||
assertEquals(100, getPropertyValue(consumer, "phase"));
|
||||
assertFalse(getPropertyValue(consumer, "autoStartup", Boolean.class));
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
public static class DummyFileNameGenerator implements FileNameGenerationStrategy {
|
||||
|
||||
@Override
|
||||
public String generateFileName(Message<?> message) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class FooAdvice extends AbstractRequestHandlerAdvice {
|
||||
|
||||
@Override
|
||||
protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
|
||||
adviceCalled++;
|
||||
return callback.execute();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user