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
The following commit(s) were added to refs/heads/master by this push: new 1a9ae1c Camel-AWS2-SQS: Adding tests with localstack and Testcontainers 1a9ae1c is described below commit 1a9ae1c86425226410eb8c724ebfa18c350da081 Author: Andrea Cosentino <anco...@gmail.com> AuthorDate: Fri Sep 11 11:43:49 2020 +0200 Camel-AWS2-SQS: Adding tests with localstack and Testcontainers --- components/camel-aws2-sqs/pom.xml | 70 ++++++++++++++ .../aws2/sqs/localstack/Aws2SQSBaseTest.java | 76 +++++++++++++++ .../sqs/localstack/SqsComponentLocalstackTest.java | 106 +++++++++++++++++++++ 3 files changed, 252 insertions(+) diff --git a/components/camel-aws2-sqs/pom.xml b/components/camel-aws2-sqs/pom.xml index 99ed574..25d3056 100644 --- a/components/camel-aws2-sqs/pom.xml +++ b/components/camel-aws2-sqs/pom.xml @@ -62,5 +62,75 @@ <artifactId>log4j-slf4j-impl</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-testcontainers-junit5</artifactId> + <scope>test</scope> + </dependency> </dependencies> + + <profiles> + <profile> + <id>aws2-sqs-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-sqs-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-sqs-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-sqs/src/test/java/org/apache/camel/component/aws2/sqs/localstack/Aws2SQSBaseTest.java b/components/camel-aws2-sqs/src/test/java/org/apache/camel/component/aws2/sqs/localstack/Aws2SQSBaseTest.java new file mode 100644 index 0000000..f15fab6 --- /dev/null +++ b/components/camel-aws2-sqs/src/test/java/org/apache/camel/component/aws2/sqs/localstack/Aws2SQSBaseTest.java @@ -0,0 +1,76 @@ +/* + * 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.sqs.localstack; + +import java.net.URI; + +import org.apache.camel.CamelContext; +import org.apache.camel.component.aws2.sqs.Sqs2Component; +import org.apache.camel.test.testcontainers.junit5.ContainerAwareTestSupport; +import org.apache.camel.test.testcontainers.junit5.Wait; +import org.junit.jupiter.api.TestInstance; +import org.testcontainers.containers.GenericContainer; +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.sqs.SqsClient; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class Aws2SQSBaseTest extends ContainerAwareTestSupport { + + public static final String CONTAINER_IMAGE = "localstack/localstack:0.11.4"; + public static final String CONTAINER_NAME = "sqs"; + + @Override + protected GenericContainer<?> createContainer() { + return localstackContainer(); + } + + public static GenericContainer localstackContainer() { + return new GenericContainer(CONTAINER_IMAGE) + .withNetworkAliases(CONTAINER_NAME) + .withEnv("SERVICES", "sqs") + .withExposedPorts(4566) + .waitingFor(Wait.forListeningPort()) + .waitingFor(Wait.forLogMessageContaining("Ready.", 1)); + } + + public String getS3Url() { + return String.format( + "%s:%d", + getContainerHost(CONTAINER_NAME), + getContainerPort(CONTAINER_NAME, 4566)); + } + + public SqsClient getSQSClient() { + SqsClient sqsClient = SqsClient + .builder() + .endpointOverride(URI.create("http://" + getS3Url())) + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("xxx", "yyy"))) + .region(Region.EU_WEST_1) + .build(); + return sqsClient; + } + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = super.createCamelContext(); + Sqs2Component sqs = context.getComponent("aws2-sqs", Sqs2Component.class); + sqs.getConfiguration().setAmazonSQSClient(getSQSClient()); + return context; + } +} diff --git a/components/camel-aws2-sqs/src/test/java/org/apache/camel/component/aws2/sqs/localstack/SqsComponentLocalstackTest.java b/components/camel-aws2-sqs/src/test/java/org/apache/camel/component/aws2/sqs/localstack/SqsComponentLocalstackTest.java new file mode 100644 index 0000000..698c687 --- /dev/null +++ b/components/camel-aws2-sqs/src/test/java/org/apache/camel/component/aws2/sqs/localstack/SqsComponentLocalstackTest.java @@ -0,0 +1,106 @@ +/* + * 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.sqs.localstack; + +import org.apache.camel.EndpointInject; +import org.apache.camel.Exchange; +import org.apache.camel.ExchangePattern; +import org.apache.camel.Processor; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.aws2.sqs.Sqs2Constants; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class SqsComponentLocalstackTest extends Aws2SQSBaseTest { + + @EndpointInject("direct:start") + private ProducerTemplate template; + + @EndpointInject("mock:result") + private MockEndpoint result; + + @Test + public void sendInOnly() throws Exception { + result.expectedMessageCount(1); + + Exchange exchange = template.send("direct:start", ExchangePattern.InOnly, new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setBody("This is my message text."); + } + }); + + assertMockEndpointsSatisfied(); + + Exchange resultExchange = result.getExchanges().get(0); + assertEquals("This is my message text.", resultExchange.getIn().getBody()); + assertNotNull(resultExchange.getIn().getHeader(Sqs2Constants.MESSAGE_ID)); + assertNotNull(resultExchange.getIn().getHeader(Sqs2Constants.RECEIPT_HANDLE)); + assertEquals("6a1559560f67c5e7a7d5d838bf0272ee", resultExchange.getIn().getHeader(Sqs2Constants.MD5_OF_BODY)); + assertNotNull(resultExchange.getIn().getHeader(Sqs2Constants.ATTRIBUTES)); + assertNotNull(resultExchange.getIn().getHeader(Sqs2Constants.MESSAGE_ATTRIBUTES)); + + assertNotNull(exchange.getIn().getHeader(Sqs2Constants.MESSAGE_ID)); + assertEquals("6a1559560f67c5e7a7d5d838bf0272ee", exchange.getIn().getHeader(Sqs2Constants.MD5_OF_BODY)); + } + + @Test + public void sendInOut() throws Exception { + result.expectedMessageCount(1); + + Exchange exchange = template.send("direct:start", ExchangePattern.InOut, new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setBody("This is my message text."); + } + }); + + assertMockEndpointsSatisfied(); + + Exchange resultExchange = result.getExchanges().get(0); + assertEquals("This is my message text.", resultExchange.getIn().getBody()); + assertNotNull(resultExchange.getIn().getHeader(Sqs2Constants.RECEIPT_HANDLE)); + assertNotNull(resultExchange.getIn().getHeader(Sqs2Constants.MESSAGE_ID)); + assertEquals("6a1559560f67c5e7a7d5d838bf0272ee", resultExchange.getIn().getHeader(Sqs2Constants.MD5_OF_BODY)); + assertNotNull(resultExchange.getIn().getHeader(Sqs2Constants.ATTRIBUTES)); + assertNotNull(resultExchange.getIn().getHeader(Sqs2Constants.MESSAGE_ATTRIBUTES)); + + assertNotNull(exchange.getMessage().getHeader(Sqs2Constants.MESSAGE_ID)); + assertEquals("6a1559560f67c5e7a7d5d838bf0272ee", exchange.getMessage().getHeader(Sqs2Constants.MD5_OF_BODY)); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + final String sqsEndpointUri = String + .format("aws2-sqs://MyNewCamelQueue?messageRetentionPeriod=%s&maximumMessageSize=%s&visibilityTimeout=%s&policy=%s", + "1209600", "65536", "60", + "%7B%22Version%22%3A%222008-10-17%22%2C%22Id%22%3A%22%2F195004372649%2FMyNewCamelQueue%2FSQSDefaultPolicy%22%2C%22" + + "Statement%22%3A%5B%7B%22Sid%22%3A%22Queue1ReceiveMessage%22%2C%22Effect%22%3A%22Allow%22%2C%22Principal%22%3A%7B%22AWS%22%3A%22*%22%7D%2C%22" + + "Action%22%3A%22SQS%3AReceiveMessage%22%2C%22Resource%22%3A%22%2F195004372649%2FMyNewCamelQueue%22%7D%5D%7D"); + + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start").to(sqsEndpointUri); + + from(sqsEndpointUri).to("mock:result"); + } + }; + } +}