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/23 07:34:36 UTC

[camel] 01/03: Camel-AWS2-Eventbridge: Added Localstack testcontainers support

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 cfab57cea4c1b93646222777bc8f4d0ad5f19a34
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Wed Sep 23 09:27:46 2020 +0200

    Camel-AWS2-Eventbridge: Added Localstack testcontainers support
---
 components/camel-aws2-eventbridge/pom.xml          | 70 +++++++++++++++++
 .../localstack/Aws2EventbridgeBaseTest.java        | 77 ++++++++++++++++++
 .../EventbridgePutRuleLocalstackTest.java          | 90 ++++++++++++++++++++++
 .../src/test/resources/log4j2.properties           |  2 +-
 4 files changed, 238 insertions(+), 1 deletion(-)

diff --git a/components/camel-aws2-eventbridge/pom.xml b/components/camel-aws2-eventbridge/pom.xml
index 0170ff2..36a3d4e 100644
--- a/components/camel-aws2-eventbridge/pom.xml
+++ b/components/camel-aws2-eventbridge/pom.xml
@@ -66,5 +66,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-eventbridge-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-eventbridge-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-eventbridge-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-eventbridge/src/test/java/org/apache/camel/component/aws2/eventbridge/localstack/Aws2EventbridgeBaseTest.java b/components/camel-aws2-eventbridge/src/test/java/org/apache/camel/component/aws2/eventbridge/localstack/Aws2EventbridgeBaseTest.java
new file mode 100644
index 0000000..2c0edd7
--- /dev/null
+++ b/components/camel-aws2-eventbridge/src/test/java/org/apache/camel/component/aws2/eventbridge/localstack/Aws2EventbridgeBaseTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.eventbridge.localstack;
+
+import java.net.URI;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.component.aws2.eventbridge.EventbridgeComponent;
+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.eventbridge.EventBridgeClient;
+import software.amazon.awssdk.services.eventbridge.model.CreateEventBusRequest;
+
+@TestInstance(TestInstance.Lifecycle.PER_CLASS)
+public class Aws2EventbridgeBaseTest extends ContainerAwareTestSupport {
+
+    public static final String CONTAINER_IMAGE = "localstack/localstack:0.11.4";
+    public static final String CONTAINER_NAME = "eventbridge";
+
+    @Override
+    protected GenericContainer<?> createContainer() {
+        return localstackContainer();
+    }
+
+    public static GenericContainer localstackContainer() {
+        return new GenericContainer(CONTAINER_IMAGE)
+                .withNetworkAliases(CONTAINER_NAME)
+                .withEnv("SERVICES", "events")
+                .withExposedPorts(4566)
+                .waitingFor(Wait.forListeningPort())
+                .waitingFor(Wait.forLogMessageContaining("Ready.", 1));
+    }
+
+    public String getEventbridgeUrl() {
+        return String.format(
+                "%s:%d",
+                getContainerHost(CONTAINER_NAME),
+                getContainerPort(CONTAINER_NAME, 4566));
+    }
+
+    public EventBridgeClient getEventbridgeClient() {
+    	EventBridgeClient eventbridgeClient = EventBridgeClient
+                .builder()
+                .endpointOverride(URI.create("http://" + getEventbridgeUrl()))
+                .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("xxx", "yyy")))
+                .region(Region.EU_WEST_1)
+                .build();
+        return eventbridgeClient;
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        EventbridgeComponent eventbridgeComponent = context.getComponent("aws2-eventbridge", EventbridgeComponent.class);
+        eventbridgeComponent.getConfiguration().setEventbridgeClient(getEventbridgeClient());
+        return context;
+    }
+}
diff --git a/components/camel-aws2-eventbridge/src/test/java/org/apache/camel/component/aws2/eventbridge/localstack/EventbridgePutRuleLocalstackTest.java b/components/camel-aws2-eventbridge/src/test/java/org/apache/camel/component/aws2/eventbridge/localstack/EventbridgePutRuleLocalstackTest.java
new file mode 100644
index 0000000..5a1bef1
--- /dev/null
+++ b/components/camel-aws2-eventbridge/src/test/java/org/apache/camel/component/aws2/eventbridge/localstack/EventbridgePutRuleLocalstackTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.eventbridge.localstack;
+
+import java.util.ArrayList;
+import java.util.List;
+
+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.eventbridge.EventbridgeConstants;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.BeforeClass;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import software.amazon.awssdk.services.eventbridge.model.CreateEventBusRequest;
+import software.amazon.awssdk.services.eventbridge.model.Target;
+
+public class EventbridgePutRuleLocalstackTest extends Aws2EventbridgeBaseTest {
+
+    @EndpointInject
+    private ProducerTemplate template;
+
+    @EndpointInject("mock:result")
+    private MockEndpoint result;
+
+    @EndpointInject("mock:result1")
+    private MockEndpoint result1;
+    
+
+    @Test
+    public void sendIn() throws Exception {
+        result.expectedMessageCount(1);
+        result1.expectedMessageCount(1);
+
+        template.send("direct:evs", new Processor() {
+
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(EventbridgeConstants.RULE_NAME, "firstrule");
+            }
+        });
+
+        template.send("direct:evs-targets", new Processor() {
+
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(EventbridgeConstants.RULE_NAME, "firstrule");
+                Target target = Target.builder().id("sqs-queue").arn("arn:aws:sqs:eu-west-1:780410022472:camel-connector-test")
+                        .build();
+                List<Target> targets = new ArrayList<Target>();
+                targets.add(target);
+                exchange.getIn().setHeader(EventbridgeConstants.TARGETS, targets);
+            }
+        });
+        assertMockEndpointsSatisfied();
+
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                String awsEndpoint
+                        = "aws2-eventbridge://default?operation=putRule&eventPatternFile=file:src/test/resources/eventpattern.json";
+                String target = "aws2-eventbridge://default?operation=putTargets";
+                from("direct:evs").to(awsEndpoint).log("${body}").to("mock:result");
+                from("direct:evs-targets").to(target).log("${body}").to("mock:result1");
+            }
+        };
+    }
+}
diff --git a/components/camel-aws2-eventbridge/src/test/resources/log4j2.properties b/components/camel-aws2-eventbridge/src/test/resources/log4j2.properties
index 668bde98..bea2995 100644
--- a/components/camel-aws2-eventbridge/src/test/resources/log4j2.properties
+++ b/components/camel-aws2-eventbridge/src/test/resources/log4j2.properties
@@ -24,5 +24,5 @@ appender.out.type = Console
 appender.out.name = out
 appender.out.layout.type = PatternLayout
 appender.out.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n
-rootLogger.level = INFO
+rootLogger.level = DEBUG
 rootLogger.appenderRef.file.ref = file