You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2020/09/10 16:20:06 UTC
[camel] 01/04: Camel-AWS2-S3: Adding tests with localstack and test
containers
This is an automated email from the ASF dual-hosted git repository.
acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
commit dcb9a828cd93af407324881a262e8551d931d419
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Thu Sep 10 12:30:23 2020 +0200
Camel-AWS2-S3: Adding tests with localstack and test containers
---
components/camel-aws2-s3/pom.xml | 82 +++++++++++++++++++++
.../aws2/s3/localstack/Aws2S3BaseTest.java | 46 ++++++++++++
.../s3/localstack/S3ConsumerLocalstackTest.java | 86 ++++++++++++++++++++++
3 files changed, 214 insertions(+)
diff --git a/components/camel-aws2-s3/pom.xml b/components/camel-aws2-s3/pom.xml
index b22aa1b..5cf3106 100644
--- a/components/camel-aws2-s3/pom.xml
+++ b/components/camel-aws2-s3/pom.xml
@@ -62,5 +62,87 @@
<artifactId>log4j-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-testcontainers-junit5</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.testcontainers</groupId>
+ <artifactId>localstack</artifactId>
+ <version>${testcontainers-version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.amazonaws</groupId>
+ <artifactId>aws-java-sdk-s3</artifactId>
+ <version>${aws-java-sdk-version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
+
+ <profiles>
+ <profile>
+ <id>aws2-s3-skip-tests</id>
+ <activation>
+ <activeByDefault>true</activeByDefault>
+ </activation>
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <skipTests>true</skipTests>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+
+ <!-- activate test if the docker socket file is accessible -->
+ <profile>
+ <id>aws2-s3-tests-docker-file</id>
+ <activation>
+ <file>
+ <exists>/var/run/docker.sock</exists>
+ </file>
+ </activation>
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <skipTests>${skipTests}</skipTests>
+ <systemPropertyVariables>
+ <visibleassertions.silence>true</visibleassertions.silence>
+ </systemPropertyVariables>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+
+ <!-- activate test if the DOCKER_HOST env var is set -->
+ <profile>
+ <id>aws2-s3-tests-docker-env</id>
+ <activation>
+ <property>
+ <name>env.DOCKER_HOST</name>
+ </property>
+ </activation>
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <skipTests>${skipTests}</skipTests>
+ <systemPropertyVariables>
+ <visibleassertions.silence>true</visibleassertions.silence>
+ </systemPropertyVariables>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ </profiles>
</project>
diff --git a/components/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/localstack/Aws2S3BaseTest.java b/components/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/localstack/Aws2S3BaseTest.java
new file mode 100644
index 0000000..26f459d
--- /dev/null
+++ b/components/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/localstack/Aws2S3BaseTest.java
@@ -0,0 +1,46 @@
+package org.apache.camel.component.aws2.s3.localstack;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.component.aws2.s3.AWS2S3Component;
+import org.apache.camel.test.testcontainers.junit5.ContainerAwareTestSupport;
+import org.junit.jupiter.api.TestInstance;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.localstack.LocalStackContainer;
+import org.testcontainers.containers.localstack.LocalStackContainer.Service;
+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.s3.S3Client;
+
+@TestInstance(TestInstance.Lifecycle.PER_CLASS)
+public class Aws2S3BaseTest extends ContainerAwareTestSupport {
+
+ public static final String CONTAINER_IMAGE = "localstack:0.11.4";
+ public static final String CONTAINER_NAME = "s3";
+
+ @Override
+ protected GenericContainer<?> createContainer() {
+ return localstackContainer();
+ }
+
+ public static LocalStackContainer localstackContainer() {
+ return new LocalStackContainer()
+ .withNetworkAliases(CONTAINER_NAME)
+ .withServices(Service.S3);
+ }
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+ AWS2S3Component s3 = context.getComponent("aws2-s3", AWS2S3Component.class);
+ S3Client s3Client = S3Client
+ .builder()
+ .endpointOverride(localstackContainer().getEndpointOverride(LocalStackContainer.Service.S3))
+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(
+ localstackContainer().getAccessKey(), localstackContainer().getSecretKey())))
+ .region(Region.of(localstackContainer().getRegion()))
+ .build();
+ s3.getConfiguration().setAmazonS3Client(s3Client);
+ return context;
+ }
+}
diff --git a/components/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/localstack/S3ConsumerLocalstackTest.java b/components/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/localstack/S3ConsumerLocalstackTest.java
new file mode 100644
index 0000000..541a0c8
--- /dev/null
+++ b/components/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/localstack/S3ConsumerLocalstackTest.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.camel.component.aws2.s3.localstack;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.aws2.s3.AWS2S3Constants;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+public class S3ConsumerLocalstackTest extends Aws2S3BaseTest {
+
+ @EndpointInject
+ private ProducerTemplate template;
+
+ @EndpointInject("mock:result")
+ private MockEndpoint result;
+
+ @Test
+ public void sendIn() throws Exception {
+ result.expectedMessageCount(3);
+
+ template.send("direct:putObject", new Processor() {
+
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ exchange.getIn().setHeader(AWS2S3Constants.KEY, "test.txt");
+ exchange.getIn().setBody("Test");
+ }
+ });
+
+ template.send("direct:putObject", new Processor() {
+
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ exchange.getIn().setHeader(AWS2S3Constants.KEY, "test1.txt");
+ exchange.getIn().setBody("Test1");
+ }
+ });
+
+ template.send("direct:putObject", new Processor() {
+
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ exchange.getIn().setHeader(AWS2S3Constants.KEY, "test2.txt");
+ exchange.getIn().setBody("Test2");
+ }
+ });
+
+ Thread.sleep(10000);
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ String awsEndpoint = "aws2-s3://mycamel?autoCreateBucket=false";
+
+ from("direct:putObject").startupOrder(1).to(awsEndpoint).to("mock:result");
+
+ from("aws2-s3://mycamel?moveAfterRead=true&destinationBucket=camel-kafka-connector&autoCreateBucket=false&destinationBucketPrefix=RAW(movedPrefix)&destinationBucketSuffix=RAW(movedSuffix)")
+ .startupOrder(2).log("${body}");
+
+ }
+ };
+ }
+}