This is an automated email from the ASF dual-hosted git repository. ppalaga pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
commit dae964097807e4bfdf5f22f8c7f03d589c3c0f8c Author: Peter Palaga <[email protected]> AuthorDate: Thu Feb 4 17:49:32 2021 +0100 Prepare for multiple isolated AWS2 tests --- integration-tests-support/aws2/pom.xml | 68 ++++++++++++ .../test/support/aws2/Aws2TestResource.java | 116 +++++++++++++++++++++ integration-tests-support/pom.xml | 1 + integration-tests/aws2-s3/pom.xml | 16 +-- .../camel/quarkus/component/aws2/Aws2S3Test.java | 2 +- .../quarkus/component/aws2/Aws2S3TestResource.java | 66 ++++++++++++ .../quarkus/component/aws2/AwsTestResource.java | 106 ------------------- poms/bom-test/pom.xml | 5 + 8 files changed, 258 insertions(+), 122 deletions(-) diff --git a/integration-tests-support/aws2/pom.xml b/integration-tests-support/aws2/pom.xml new file mode 100644 index 0000000..2bcc8a6 --- /dev/null +++ b/integration-tests-support/aws2/pom.xml @@ -0,0 +1,68 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-integration-tests-support</artifactId> + <version>1.7.0-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>camel-quarkus-integration-tests-support-aws2</artifactId> + <name>Camel Quarkus :: Integration Tests :: Support :: AWS 2</name> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-bom-test</artifactId> + <version>${project.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-integration-testcontainers-support</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-integration-test-support-mock-backend</artifactId> + </dependency> + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>localstack</artifactId> + </dependency> + <dependency><!-- Workaround for https://github.com/testcontainers/testcontainers-java/issues/1442 --> + <groupId>com.amazonaws</groupId> + <artifactId>aws-java-sdk-core</artifactId> + <exclusions> + <exclusion> + <groupId>*</groupId> + <artifactId>*</artifactId> + </exclusion> + </exclusions> + </dependency> + </dependencies> +</project> diff --git a/integration-tests-support/aws2/src/main/java/org/apache/camel/quarkus/test/support/aws2/Aws2TestResource.java b/integration-tests-support/aws2/src/main/java/org/apache/camel/quarkus/test/support/aws2/Aws2TestResource.java new file mode 100644 index 0000000..54ab6c5 --- /dev/null +++ b/integration-tests-support/aws2/src/main/java/org/apache/camel/quarkus/test/support/aws2/Aws2TestResource.java @@ -0,0 +1,116 @@ +/* + * 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.quarkus.test.support.aws2; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.ListIterator; +import java.util.Locale; +import java.util.Map; + +import org.apache.camel.quarkus.test.mock.backend.MockBackendUtils; +import org.apache.camel.quarkus.testcontainers.ContainerResourceLifecycleManager; +import org.jboss.logging.Logger; +import org.testcontainers.containers.localstack.LocalStackContainer; +import org.testcontainers.containers.localstack.LocalStackContainer.Service; +import org.testcontainers.utility.DockerImageName; + +public abstract class Aws2TestResource implements ContainerResourceLifecycleManager { + + private static final Logger LOG = Logger.getLogger(Aws2TestResource.class); + + protected final ArrayList<AutoCloseable> closeables = new ArrayList<>(); + + private final Service[] services; + + protected LocalStackContainer localstack; + + protected boolean usingMockBackend; + + protected String accessKey; + protected String secretKey; + protected String region; + + public Aws2TestResource(Service first, Service... other) { + final Service[] s = new Service[other.length + 1]; + s[0] = first; + System.arraycopy(other, 0, s, 1, other.length); + this.services = s; + } + + @SuppressWarnings("resource") + @Override + public Map<String, String> start() { + final String realKey = System.getenv("AWS_ACCESS_KEY"); + final String realSecret = System.getenv("AWS_SECRET_KEY"); + final String realRegion = System.getenv("AWS_REGION"); + final boolean realCredentialsProvided = realKey != null && realSecret != null && realRegion != null; + final boolean startMockBackend = MockBackendUtils.startMockBackend(false); + final Map<String, String> result = new LinkedHashMap<>(); + usingMockBackend = startMockBackend && !realCredentialsProvided; + if (usingMockBackend) { + MockBackendUtils.logMockBackendUsed(); + this.localstack = new LocalStackContainer(DockerImageName.parse("localstack/localstack:0.11.3")) + .withServices(services); + closeables.add(localstack); + localstack.start(); + + this.accessKey = localstack.getAccessKey(); + this.secretKey = localstack.getSecretKey(); + this.region = localstack.getRegion(); + + for (Service service : services) { + String s = serviceKey(service); + result.put("camel.component.aws2-" + s + ".access-key", accessKey); + result.put("camel.component.aws2-" + s + ".secret-key", secretKey); + result.put("camel.component.aws2-" + s + ".override-endpoint", "true"); + result.put("camel.component.aws2-" + s + ".uri-endpoint-override", + localstack.getEndpointOverride(service).toString()); + result.put("camel.component.aws2-" + s + ".region", region); + } + + } else { + if (!startMockBackend && !realCredentialsProvided) { + throw new IllegalStateException( + "Set AWS_ACCESS_KEY, AWS_SECRET_KEY and AWS_REGION env vars if you set CAMEL_QUARKUS_START_MOCK_BACKEND=false"); + } + MockBackendUtils.logRealBackendUsed(); + this.accessKey = realKey; + this.secretKey = realSecret; + this.region = realRegion; + } + + return result; + } + + protected String serviceKey(Service service) { + return service.name().toLowerCase(Locale.ROOT); + } + + @Override + public void stop() { + ListIterator<AutoCloseable> it = closeables.listIterator(closeables.size()); + while (it.hasPrevious()) { + AutoCloseable c = it.previous(); + try { + c.close(); + } catch (Exception e) { + LOG.warnf(e, "Could not close %s", c); + } + } + } +} diff --git a/integration-tests-support/pom.xml b/integration-tests-support/pom.xml index 840b405..03a3395 100644 --- a/integration-tests-support/pom.xml +++ b/integration-tests-support/pom.xml @@ -36,6 +36,7 @@ </description> <modules> + <module>aws2</module> <module>azure</module> <module>custom-dataformat</module> <module>custom-log-component</module> diff --git a/integration-tests/aws2-s3/pom.xml b/integration-tests/aws2-s3/pom.xml index f7afa20..3430b7c 100644 --- a/integration-tests/aws2-s3/pom.xml +++ b/integration-tests/aws2-s3/pom.xml @@ -60,21 +60,7 @@ </dependency> <dependency> <groupId>org.apache.camel.quarkus</groupId> - <artifactId>camel-quarkus-integration-testcontainers-support</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.camel.quarkus</groupId> - <artifactId>camel-quarkus-integration-test-support-mock-backend</artifactId> - </dependency> - <dependency> - <groupId>org.testcontainers</groupId> - <artifactId>localstack</artifactId> - <scope>test</scope> - </dependency> - <dependency><!-- Workaround for https://github.com/testcontainers/testcontainers-java/issues/1442 --> - <groupId>com.amazonaws</groupId> - <artifactId>aws-java-sdk-core</artifactId> + <artifactId>camel-quarkus-integration-tests-support-aws2</artifactId> <scope>test</scope> </dependency> diff --git a/integration-tests/aws2-s3/src/test/java/org/apache/camel/quarkus/component/aws2/Aws2S3Test.java b/integration-tests/aws2-s3/src/test/java/org/apache/camel/quarkus/component/aws2/Aws2S3Test.java index 98d1233..ecff5ce 100644 --- a/integration-tests/aws2-s3/src/test/java/org/apache/camel/quarkus/component/aws2/Aws2S3Test.java +++ b/integration-tests/aws2-s3/src/test/java/org/apache/camel/quarkus/component/aws2/Aws2S3Test.java @@ -29,7 +29,7 @@ import org.junit.jupiter.api.Test; import static org.hamcrest.core.Is.is; @QuarkusTest -@QuarkusTestResource(AwsTestResource.class) +@QuarkusTestResource(Aws2S3TestResource.class) class Aws2S3Test { @Test diff --git a/integration-tests/aws2-s3/src/test/java/org/apache/camel/quarkus/component/aws2/Aws2S3TestResource.java b/integration-tests/aws2-s3/src/test/java/org/apache/camel/quarkus/component/aws2/Aws2S3TestResource.java new file mode 100644 index 0000000..37debcf --- /dev/null +++ b/integration-tests/aws2-s3/src/test/java/org/apache/camel/quarkus/component/aws2/Aws2S3TestResource.java @@ -0,0 +1,66 @@ +/* + * 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.quarkus.component.aws2; + +import java.util.Collections; +import java.util.Locale; +import java.util.Map; + +import org.apache.camel.quarkus.test.support.aws2.Aws2TestResource; +import org.apache.commons.lang3.RandomStringUtils; +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; +import software.amazon.awssdk.services.s3.S3ClientBuilder; +import software.amazon.awssdk.services.s3.model.CreateBucketRequest; +import software.amazon.awssdk.services.s3.model.DeleteBucketRequest; + +public class Aws2S3TestResource extends Aws2TestResource { + + public Aws2S3TestResource() { + super(Service.S3); + } + + @Override + public Map<String, String> start() { + Map<String, String> result = super.start(); + + final S3ClientBuilder clientBuilder = S3Client + .builder() + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create( + localstack.getAccessKey(), localstack.getSecretKey()))) + .region(Region.of(localstack.getRegion())); + if (usingMockBackend) { + clientBuilder.endpointOverride(localstack.getEndpointOverride(LocalStackContainer.Service.S3)); + } + final S3Client s3Client = clientBuilder.build(); + + final String bucketName = "camel-quarkus-" + RandomStringUtils.randomAlphanumeric(49).toLowerCase(Locale.ROOT); + s3Client.createBucket(CreateBucketRequest.builder().bucket(bucketName).build()); + result.put("aws-s3.bucket-name", bucketName); + closeables.add(() -> { + s3Client.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build()); + s3Client.close(); + }); + + return Collections.unmodifiableMap(result); + } + +} diff --git a/integration-tests/aws2-s3/src/test/java/org/apache/camel/quarkus/component/aws2/AwsTestResource.java b/integration-tests/aws2-s3/src/test/java/org/apache/camel/quarkus/component/aws2/AwsTestResource.java deleted file mode 100644 index ae20d3c..0000000 --- a/integration-tests/aws2-s3/src/test/java/org/apache/camel/quarkus/component/aws2/AwsTestResource.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * 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.quarkus.component.aws2; - -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.Locale; -import java.util.Map; - -import org.apache.camel.quarkus.test.mock.backend.MockBackendUtils; -import org.apache.camel.quarkus.testcontainers.ContainerResourceLifecycleManager; -import org.apache.commons.lang3.RandomStringUtils; -import org.testcontainers.containers.localstack.LocalStackContainer; -import org.testcontainers.containers.localstack.LocalStackContainer.Service; -import org.testcontainers.utility.DockerImageName; -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; -import software.amazon.awssdk.services.s3.model.CreateBucketRequest; -import software.amazon.awssdk.services.s3.model.DeleteBucketRequest; - -public class AwsTestResource implements ContainerResourceLifecycleManager { - - private LocalStackContainer localstack; - private S3Client s3Client; - private String bucketName; - - @SuppressWarnings("resource") - @Override - public Map<String, String> start() { - final String realKey = System.getenv("AWS_ACCESS_KEY"); - final String realSecret = System.getenv("AWS_SECRET_KEY"); - final String realRegion = System.getenv("AWS_REGION"); - final boolean realCredentialsProvided = realKey != null && realSecret != null && realRegion != null; - final boolean startMockBackend = MockBackendUtils.startMockBackend(false); - final Map<String, String> result = new LinkedHashMap<>(); - - bucketName = "camel-quarkus-" + RandomStringUtils.randomAlphanumeric(49).toLowerCase(Locale.ROOT); - - if (startMockBackend && !realCredentialsProvided) { - MockBackendUtils.logMockBackendUsed(); - localstack = new LocalStackContainer(DockerImageName.parse("localstack/localstack:0.11.3")) - .withServices(Service.S3); - localstack.start(); - - result.put("camel.component.aws2-s3.access-key", localstack.getAccessKey()); - result.put("camel.component.aws2-s3.secret-key", localstack.getSecretKey()); - result.put("camel.component.aws2-s3.override-endpoint", "true"); - result.put("camel.component.aws2-s3.uri-endpoint-override", localstack.getEndpointOverride(Service.S3).toString()); - result.put("camel.component.aws2-s3.region", localstack.getRegion()); - - s3Client = S3Client - .builder() - .endpointOverride(localstack.getEndpointOverride(LocalStackContainer.Service.S3)) - .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create( - localstack.getAccessKey(), localstack.getSecretKey()))) - .region(Region.of(localstack.getRegion())) - .build(); - - } else { - if (!startMockBackend && !realCredentialsProvided) { - throw new IllegalStateException( - "Set AWS_ACCESS_KEY, AWS_SECRET_KEY and AWS_REGION env vars if you set CAMEL_QUARKUS_START_MOCK_BACKEND=false"); - } - MockBackendUtils.logRealBackendUsed(); - s3Client = S3Client - .builder() - .credentialsProvider( - StaticCredentialsProvider.create(AwsBasicCredentials.create(realKey, realSecret))) - .region(Region.of(realRegion)) - .build(); - } - - s3Client.createBucket(CreateBucketRequest.builder().bucket(bucketName).build()); - - result.put("aws-s3.bucket-name", bucketName); - - return Collections.unmodifiableMap(result); - } - - @Override - public void stop() { - if (s3Client != null) { - s3Client.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build()); - s3Client.close(); - } - if (localstack != null && localstack.isRunning()) { - localstack.stop(); - } - } -} diff --git a/poms/bom-test/pom.xml b/poms/bom-test/pom.xml index 1eecbb0..09eed22 100644 --- a/poms/bom-test/pom.xml +++ b/poms/bom-test/pom.xml @@ -59,6 +59,11 @@ </dependency> <dependency> <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-integration-tests-support-aws2</artifactId> + <version>${camel-quarkus.version}</version> + </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> <artifactId>camel-quarkus-integration-tests-support-azure</artifactId> <version>${camel-quarkus.version}</version> </dependency>
