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 ca08206e2a8a79a670db7af15f341531f74d997e Author: Peter Palaga <ppal...@redhat.com> AuthorDate: Tue Feb 9 23:19:52 2021 +0100 Aws2TestEnvCustomizer SPI to allow running AWS 2 tests both isolated and dynamically grouped into one module --- integration-tests-support/aws2/pom.xml | 12 ++ .../test/support/aws2/Aws2TestEnvContext.java | 150 +++++++++++++++++++++ .../test/support/aws2/Aws2TestEnvCustomizer.java | 38 ++++++ .../test/support/aws2/Aws2TestResource.java | 97 ++++--------- .../camel/quarkus/component/aws2/Aws2S3Test.java | 3 +- .../component/aws2/Aws2S3TestEnvCustomizer.java | 46 +++++++ .../quarkus/component/aws2/Aws2S3TestResource.java | 66 --------- ...quarkus.test.support.aws2.Aws2TestEnvCustomizer | 1 + .../quarkus/component/aws2/sqs/it/Aws2SqsTest.java | 3 +- ...Resource.java => Aws2SqsTestEnvCustomizer.java} | 66 +++------ ...quarkus.test.support.aws2.Aws2TestEnvCustomizer | 1 + 11 files changed, 299 insertions(+), 184 deletions(-) diff --git a/integration-tests-support/aws2/pom.xml b/integration-tests-support/aws2/pom.xml index 2bcc8a6..93c2d55 100644 --- a/integration-tests-support/aws2/pom.xml +++ b/integration-tests-support/aws2/pom.xml @@ -54,6 +54,18 @@ <groupId>org.testcontainers</groupId> <artifactId>localstack</artifactId> </dependency> + <dependency> + <groupId>software.amazon.awssdk</groupId> + <artifactId>auth</artifactId> + </dependency> + <dependency> + <groupId>software.amazon.awssdk</groupId> + <artifactId>aws-core</artifactId> + </dependency> + <dependency> + <groupId>software.amazon.awssdk</groupId> + <artifactId>sdk-core</artifactId> + </dependency> <dependency><!-- Workaround for https://github.com/testcontainers/testcontainers-java/issues/1442 --> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-core</artifactId> diff --git a/integration-tests-support/aws2/src/main/java/org/apache/camel/quarkus/test/support/aws2/Aws2TestEnvContext.java b/integration-tests-support/aws2/src/main/java/org/apache/camel/quarkus/test/support/aws2/Aws2TestEnvContext.java new file mode 100644 index 0000000..52477c9 --- /dev/null +++ b/integration-tests-support/aws2/src/main/java/org/apache/camel/quarkus/test/support/aws2/Aws2TestEnvContext.java @@ -0,0 +1,150 @@ +/* + * 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.Collections; +import java.util.LinkedHashMap; +import java.util.ListIterator; +import java.util.Locale; +import java.util.Map; +import java.util.Optional; +import java.util.function.Supplier; + +import org.jboss.logging.Logger; +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.awscore.client.builder.AwsClientBuilder; +import software.amazon.awssdk.core.SdkClient; +import software.amazon.awssdk.regions.Region; + +/** + * A context passed to {@link Aws2TestEnvCustomizer#customize(Aws2TestEnvContext)}. + */ +public class Aws2TestEnvContext { + private static final Logger LOG = Logger.getLogger(Aws2TestEnvContext.class); + private final ArrayList<AutoCloseable> closeables = new ArrayList<>(); + private final Map<String, String> properties = new LinkedHashMap<>(); + private final String accessKey; + private final String secretKey; + private final String region; + private final Optional<LocalStackContainer> localstack; + + public Aws2TestEnvContext(String accessKey, String secretKey, String region, Optional<LocalStackContainer> localstack, + Service... services) { + this.accessKey = accessKey; + this.secretKey = secretKey; + this.region = region; + this.localstack = localstack; + + localstack.ifPresent(ls -> { + for (Service service : services) { + String s = serviceKey(service); + properties.put("camel.component.aws2-" + s + ".access-key", accessKey); + properties.put("camel.component.aws2-" + s + ".secret-key", secretKey); + properties.put("camel.component.aws2-" + s + ".region", region); + + switch (service) { + case SQS: + case SNS: + // TODO https://github.com/apache/camel-quarkus/issues/2216 + break; + default: + properties.put("camel.component.aws2-" + s + ".override-endpoint", "true"); + properties.put("camel.component.aws2-" + s + ".uri-endpoint-override", + ls.getEndpointOverride(service).toString()); + break; + } + } + }); + } + + /** + * Add a key-value pair to the system properties seen by AWS 2 tests + * + * @param key + * @param value + * @return this {@link Aws2TestEnvContext} + */ + public Aws2TestEnvContext property(String key, String value) { + properties.put(key, value); + return this; + } + + /** + * Add an {@link AutoCloseable} to be closed after running AWS 2 tests + * + * @param closeable the {@link AutoCloseable} to add + * @return this {@link Aws2TestEnvContext} + */ + public Aws2TestEnvContext closeable(AutoCloseable closeable) { + closeables.add(closeable); + return this; + } + + /** + * @return a read-only view of {@link #properties} + */ + public Map<String, String> getProperies() { + return Collections.unmodifiableMap(properties); + } + + /** + * Close all {@link AutoCloseable}s registered via {@link #closeable(AutoCloseable)} + */ + public void close() { + 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); + } + } + } + + /** + * Create a new AWS 2 client and register it for closing after running AWS 2 tests. + * + * @param <B> + * @param <C> + * @param service + * @param builderSupplier + * @return a new client + */ + public <B extends AwsClientBuilder<B, C>, C extends SdkClient> C client( + Service service, + Supplier<B> builderSupplier) { + B builder = builderSupplier.get() + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create( + accessKey, secretKey))) + .region(Region.of(region)); + if (localstack.isPresent()) { + builder.endpointOverride(localstack.get().getEndpointOverride(service)); + } + final C client = builder.build(); + closeables.add(client); + return client; + } + + private static String serviceKey(Service service) { + return service.name().toLowerCase(Locale.ROOT); + } +} diff --git a/integration-tests-support/aws2/src/main/java/org/apache/camel/quarkus/test/support/aws2/Aws2TestEnvCustomizer.java b/integration-tests-support/aws2/src/main/java/org/apache/camel/quarkus/test/support/aws2/Aws2TestEnvCustomizer.java new file mode 100644 index 0000000..c479f00 --- /dev/null +++ b/integration-tests-support/aws2/src/main/java/org/apache/camel/quarkus/test/support/aws2/Aws2TestEnvCustomizer.java @@ -0,0 +1,38 @@ +/* + * 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 org.testcontainers.containers.localstack.LocalStackContainer.Service; + +/** + * An SPI to allow individual AWS 2 test modules to customize the {@link Aws2TestResource}. + * At the same time, this SPI should allow running the AWS 2 test modules both isolated and merged together. + */ +public interface Aws2TestEnvCustomizer { + + /** + * @return an array of services the Localstack container should expose + */ + Service[] localstackServices(); + + /** + * Customize the given {@link Aws2TestEnvContext} + * + * @param envContext the {@link Aws2TestEnvContext} to customize + */ + void customize(Aws2TestEnvContext envContext); +} 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 index 3fd5dc9..f7ae407 100644 --- 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 @@ -17,10 +17,11 @@ 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.List; import java.util.Map; +import java.util.Optional; +import java.util.ServiceLoader; +import java.util.stream.Stream; import org.apache.camel.quarkus.test.mock.backend.MockBackendUtils; import org.apache.camel.quarkus.testcontainers.ContainerResourceLifecycleManager; @@ -29,28 +30,10 @@ import org.testcontainers.containers.localstack.LocalStackContainer; import org.testcontainers.containers.localstack.LocalStackContainer.Service; import org.testcontainers.utility.DockerImageName; -public abstract class Aws2TestResource implements ContainerResourceLifecycleManager { - +public final class Aws2TestResource implements ContainerResourceLifecycleManager { private static final Logger LOG = Logger.getLogger(Aws2TestResource.class); - protected final ArrayList<AutoCloseable> closeables = new ArrayList<>(); - - protected 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; - } + private Aws2TestEnvContext envContext; @SuppressWarnings("resource") @Override @@ -60,20 +43,28 @@ public abstract class Aws2TestResource implements ContainerResourceLifecycleMana 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; + final boolean usingMockBackend = startMockBackend && !realCredentialsProvided; + + ServiceLoader<Aws2TestEnvCustomizer> loader = ServiceLoader.load(Aws2TestEnvCustomizer.class); + List<Aws2TestEnvCustomizer> customizers = new ArrayList<>(); + for (Aws2TestEnvCustomizer customizer : loader) { + LOG.info("Loaded Aws2TestEnvCustomizer " + customizer.getClass().getName()); + customizers.add(customizer); + } if (usingMockBackend) { MockBackendUtils.logMockBackendUsed(); - this.localstack = new LocalStackContainer(DockerImageName.parse("localstack/localstack:0.12.6")) + + final Service[] services = customizers.stream() + .map(Aws2TestEnvCustomizer::localstackServices) + .flatMap((Service[] ss) -> Stream.of(ss)) + .toArray(Service[]::new); + + LocalStackContainer localstack = new LocalStackContainer(DockerImageName.parse("localstack/localstack:0.12.6")) .withServices(services); - closeables.add(localstack); localstack.start(); - this.accessKey = localstack.getAccessKey(); - this.secretKey = localstack.getSecretKey(); - this.region = localstack.getRegion(); - - setMockProperties(result); + envContext = new Aws2TestEnvContext(localstack.getAccessKey(), localstack.getSecretKey(), localstack.getRegion(), + Optional.of(localstack), services); } else { if (!startMockBackend && !realCredentialsProvided) { @@ -81,49 +72,17 @@ public abstract class Aws2TestResource implements ContainerResourceLifecycleMana "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; + envContext = new Aws2TestEnvContext(realKey, realSecret, realRegion, Optional.empty()); } - return result; - } - - protected void setMockProperties(final Map<String, String> result) { - 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 + ".region", region); - - switch (service) { - case SQS: - case SNS: - // TODO https://github.com/apache/camel-quarkus/issues/2216 - break; - default: - result.put("camel.component.aws2-" + s + ".override-endpoint", "true"); - result.put("camel.component.aws2-" + s + ".uri-endpoint-override", - localstack.getEndpointOverride(service).toString()); - break; - } - } - } + customizers.forEach(customizer -> customizer.customize(envContext)); - protected String serviceKey(Service service) { - return service.name().toLowerCase(Locale.ROOT); + return envContext.getProperies(); } @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); - } - } + envContext.close(); } + } 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 ecff5ce..372818b 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 @@ -23,13 +23,14 @@ import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusTest; import io.restassured.RestAssured; import io.restassured.http.ContentType; +import org.apache.camel.quarkus.test.support.aws2.Aws2TestResource; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import static org.hamcrest.core.Is.is; @QuarkusTest -@QuarkusTestResource(Aws2S3TestResource.class) +@QuarkusTestResource(Aws2TestResource.class) class Aws2S3Test { @Test diff --git a/integration-tests/aws2-s3/src/test/java/org/apache/camel/quarkus/component/aws2/Aws2S3TestEnvCustomizer.java b/integration-tests/aws2-s3/src/test/java/org/apache/camel/quarkus/component/aws2/Aws2S3TestEnvCustomizer.java new file mode 100644 index 0000000..d4e3b68 --- /dev/null +++ b/integration-tests/aws2-s3/src/test/java/org/apache/camel/quarkus/component/aws2/Aws2S3TestEnvCustomizer.java @@ -0,0 +1,46 @@ +/* + * 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.Locale; + +import org.apache.camel.quarkus.test.support.aws2.Aws2TestEnvContext; +import org.apache.camel.quarkus.test.support.aws2.Aws2TestEnvCustomizer; +import org.apache.commons.lang3.RandomStringUtils; +import org.testcontainers.containers.localstack.LocalStackContainer.Service; +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 Aws2S3TestEnvCustomizer implements Aws2TestEnvCustomizer { + + @Override + public Service[] localstackServices() { + return new Service[] { Service.S3 }; + } + + @Override + public void customize(Aws2TestEnvContext envContext) { + final S3Client s3Client = envContext.client(Service.S3, S3Client::builder); + + final String bucketName = "camel-quarkus-" + RandomStringUtils.randomAlphanumeric(49).toLowerCase(Locale.ROOT); + s3Client.createBucket(CreateBucketRequest.builder().bucket(bucketName).build()); + envContext.property("aws-s3.bucket-name", bucketName); + envContext.closeable(() -> s3Client.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build())); + } + +} 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 deleted file mode 100644 index 37debcf..0000000 --- a/integration-tests/aws2-s3/src/test/java/org/apache/camel/quarkus/component/aws2/Aws2S3TestResource.java +++ /dev/null @@ -1,66 +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.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/resources/META-INF/services/org.apache.camel.quarkus.test.support.aws2.Aws2TestEnvCustomizer b/integration-tests/aws2-s3/src/test/resources/META-INF/services/org.apache.camel.quarkus.test.support.aws2.Aws2TestEnvCustomizer new file mode 100644 index 0000000..e024c2b --- /dev/null +++ b/integration-tests/aws2-s3/src/test/resources/META-INF/services/org.apache.camel.quarkus.test.support.aws2.Aws2TestEnvCustomizer @@ -0,0 +1 @@ +org.apache.camel.quarkus.component.aws2.Aws2S3TestEnvCustomizer \ No newline at end of file diff --git a/integration-tests/aws2-sqs-sns/src/test/java/org/apache/camel/quarkus/component/aws2/sqs/it/Aws2SqsTest.java b/integration-tests/aws2-sqs-sns/src/test/java/org/apache/camel/quarkus/component/aws2/sqs/it/Aws2SqsTest.java index c03d349..ddc912d 100644 --- a/integration-tests/aws2-sqs-sns/src/test/java/org/apache/camel/quarkus/component/aws2/sqs/it/Aws2SqsTest.java +++ b/integration-tests/aws2-sqs-sns/src/test/java/org/apache/camel/quarkus/component/aws2/sqs/it/Aws2SqsTest.java @@ -24,6 +24,7 @@ import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusTest; import io.restassured.RestAssured; import io.restassured.http.ContentType; +import org.apache.camel.quarkus.test.support.aws2.Aws2TestResource; import org.awaitility.Awaitility; import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.ConfigProvider; @@ -35,7 +36,7 @@ import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; import static org.hamcrest.core.Is.is; @QuarkusTest -@QuarkusTestResource(Aws2SqsTestResource.class) +@QuarkusTestResource(Aws2TestResource.class) @EnabledIfEnvironmentVariable(named = "AWS_ACCESS_KEY", matches = "[a-zA-Z0-9]+") // TODO // https://github.com/apache/camel-quarkus/issues/2216 class Aws2SqsTest { diff --git a/integration-tests/aws2-sqs-sns/src/test/java/org/apache/camel/quarkus/component/aws2/sqs/it/Aws2SqsTestResource.java b/integration-tests/aws2-sqs-sns/src/test/java/org/apache/camel/quarkus/component/aws2/sqs/it/Aws2SqsTestEnvCustomizer.java similarity index 67% rename from integration-tests/aws2-sqs-sns/src/test/java/org/apache/camel/quarkus/component/aws2/sqs/it/Aws2SqsTestResource.java rename to integration-tests/aws2-sqs-sns/src/test/java/org/apache/camel/quarkus/component/aws2/sqs/it/Aws2SqsTestEnvCustomizer.java index 83ce701..b4231b1 100644 --- a/integration-tests/aws2-sqs-sns/src/test/java/org/apache/camel/quarkus/component/aws2/sqs/it/Aws2SqsTestResource.java +++ b/integration-tests/aws2-sqs-sns/src/test/java/org/apache/camel/quarkus/component/aws2/sqs/it/Aws2SqsTestEnvCustomizer.java @@ -18,99 +18,75 @@ package org.apache.camel.quarkus.component.aws2.sqs.it; import java.util.Collections; import java.util.Locale; -import java.util.Map; -import org.apache.camel.quarkus.test.support.aws2.Aws2TestResource; +import org.apache.camel.quarkus.test.support.aws2.Aws2TestEnvContext; +import org.apache.camel.quarkus.test.support.aws2.Aws2TestEnvCustomizer; 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.sns.SnsClient; -import software.amazon.awssdk.services.sns.SnsClientBuilder; import software.amazon.awssdk.services.sns.model.CreateTopicRequest; import software.amazon.awssdk.services.sns.model.DeleteTopicRequest; import software.amazon.awssdk.services.sns.model.ListSubscriptionsByTopicRequest; import software.amazon.awssdk.services.sns.model.Subscription; import software.amazon.awssdk.services.sns.model.UnsubscribeRequest; import software.amazon.awssdk.services.sqs.SqsClient; -import software.amazon.awssdk.services.sqs.SqsClientBuilder; import software.amazon.awssdk.services.sqs.model.CreateQueueRequest; import software.amazon.awssdk.services.sqs.model.DeleteQueueRequest; import software.amazon.awssdk.services.sqs.model.GetQueueAttributesRequest; import software.amazon.awssdk.services.sqs.model.QueueAttributeName; import software.amazon.awssdk.services.sqs.model.SetQueueAttributesRequest; -public class Aws2SqsTestResource extends Aws2TestResource { +public class Aws2SqsTestEnvCustomizer implements Aws2TestEnvCustomizer { - public Aws2SqsTestResource() { - super(Service.SQS, Service.SNS); + @Override + public Service[] localstackServices() { + return new Service[] { Service.SQS, Service.SNS }; } @Override - public Map<String, String> start() { - Map<String, String> result = super.start(); + public void customize(Aws2TestEnvContext envContext) { /* SQS */ final String queueName = "camel-quarkus-" + RandomStringUtils.randomAlphanumeric(49).toLowerCase(Locale.ROOT); - result.put("aws-sqs.queue-name", queueName); - - final SqsClientBuilder clientBuilder = SqsClient - .builder() - .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey))) - .region(Region.of(region)); - if (usingMockBackend) { - clientBuilder.endpointOverride(localstack.getEndpointOverride(LocalStackContainer.Service.SQS)); - } - final SqsClient sqsClient = clientBuilder.build(); + envContext.property("aws-sqs.queue-name", queueName); + + final SqsClient sqsClient = envContext.client(Service.SQS, SqsClient::builder); { final String queueUrl = sqsClient.createQueue( CreateQueueRequest.builder() .queueName(queueName) .build()) .queueUrl(); - closeables.add(() -> { - sqsClient.deleteQueue(DeleteQueueRequest.builder().queueUrl(queueUrl).build()); - sqsClient.close(); - }); + envContext.closeable(() -> sqsClient.deleteQueue(DeleteQueueRequest.builder().queueUrl(queueUrl).build())); } /* SNS */ { final String topicName = "camel-quarkus-" + RandomStringUtils.randomAlphanumeric(49).toLowerCase(Locale.ROOT); - result.put("aws-sns.topic-name", topicName); + envContext.property("aws-sns.topic-name", topicName); - final SnsClientBuilder snsClientBuilder = SnsClient - .builder() - .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey))) - .region(Region.of(region)); - if (usingMockBackend) { - snsClientBuilder.endpointOverride(localstack.getEndpointOverride(LocalStackContainer.Service.SQS)); - } - final SnsClient snsClient = snsClientBuilder.build(); + final SnsClient snsClient = envContext.client(Service.SNS, SnsClient::builder); final String topicArn = snsClient.createTopic(CreateTopicRequest.builder().name(topicName).build()).topicArn(); - closeables.add(() -> { + envContext.closeable(() -> { snsClient.listSubscriptionsByTopic(ListSubscriptionsByTopicRequest.builder().topicArn(topicArn).build()) .subscriptions() .stream() .map(Subscription::subscriptionArn) .forEach(arn -> snsClient.unsubscribe(UnsubscribeRequest.builder().subscriptionArn(arn).build())); snsClient.deleteTopic(DeleteTopicRequest.builder().topicArn(topicArn).build()); - snsClient.close(); }); final String snsReceiverQueueName = "camel-quarkus-sns-receiver-" + RandomStringUtils.randomAlphanumeric(30).toLowerCase(Locale.ROOT); - result.put("aws-sqs.sns-receiver-queue-name", snsReceiverQueueName); + envContext.property("aws-sqs.sns-receiver-queue-name", snsReceiverQueueName); final String snsReceiverQueueUrl = sqsClient.createQueue( CreateQueueRequest.builder() .queueName(snsReceiverQueueName) .build()) .queueUrl(); - result.put("aws2-sqs.sns-receiver-queue-url", snsReceiverQueueUrl); + envContext.property("aws2-sqs.sns-receiver-queue-url", snsReceiverQueueUrl); /* * We need queue ARN instead of queue URL when creating a subscription of an SQS Queue to an SNS Topic @@ -123,7 +99,7 @@ public class Aws2SqsTestResource extends Aws2TestResource { .build()) .attributesAsStrings() .get("QueueArn"); - result.put("aws2-sqs.sns-receiver-queue-arn", snsReceiverQueueArn); + envContext.property("aws2-sqs.sns-receiver-queue-arn", snsReceiverQueueArn); final String policy = "{" + " \"Version\": \"2008-10-17\"," @@ -149,13 +125,9 @@ public class Aws2SqsTestResource extends Aws2TestResource { policy)) .build()); - closeables.add(() -> { - sqsClient.deleteQueue(DeleteQueueRequest.builder().queueUrl(snsReceiverQueueUrl).build()); - }); - + envContext + .closeable(() -> sqsClient.deleteQueue(DeleteQueueRequest.builder().queueUrl(snsReceiverQueueUrl).build())); } - return Collections.unmodifiableMap(result); } - } diff --git a/integration-tests/aws2-sqs-sns/src/test/resources/META-INF/services/org.apache.camel.quarkus.test.support.aws2.Aws2TestEnvCustomizer b/integration-tests/aws2-sqs-sns/src/test/resources/META-INF/services/org.apache.camel.quarkus.test.support.aws2.Aws2TestEnvCustomizer new file mode 100644 index 0000000..544add9 --- /dev/null +++ b/integration-tests/aws2-sqs-sns/src/test/resources/META-INF/services/org.apache.camel.quarkus.test.support.aws2.Aws2TestEnvCustomizer @@ -0,0 +1 @@ +org.apache.camel.quarkus.component.aws2.sqs.it.Aws2SqsTestEnvCustomizer \ No newline at end of file