This is an automated email from the ASF dual-hosted git repository.

jamesnetherton pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git

commit 06d0b0acf942943f37f4d2fbdaee25a4df88bf4d
Author: James Netherton <[email protected]>
AuthorDate: Mon Nov 2 10:00:06 2020 +0000

    Stub Slack tests with WireMock
---
 integration-tests/slack/README.adoc                | 24 ++++++++++
 integration-tests/slack/pom.xml                    |  4 ++
 .../quarkus/component/slack/it/SlackResource.java  |  2 +-
 .../quarkus/component/slack/it/SlackRoutes.java    | 48 -------------------
 .../src/main/resources/application.properties      | 21 ---------
 .../quarkus/component/slack/it/SlackTest.java      |  7 ++-
 .../component/slack/it/SlackTestResource.java      | 55 ++++++++++++++++++++++
 .../test/resources/mappings/slackGetChannels.json  | 37 +++++++++++++++
 .../mappings/slackGetConversationHistory.json      | 37 +++++++++++++++
 .../resources/mappings/slackWebhookMesagePost.json | 30 ++++++++++++
 10 files changed, 191 insertions(+), 74 deletions(-)

diff --git a/integration-tests/slack/README.adoc 
b/integration-tests/slack/README.adoc
new file mode 100644
index 0000000..4a4dd49
--- /dev/null
+++ b/integration-tests/slack/README.adoc
@@ -0,0 +1,24 @@
+== Camel Quarkus Slack Integration Tests
+
+By default the Slack integration tests use WireMock to stub the API 
interactions.
+
+To run the `camel-quarkus-Slack` integration tests against the real API, you 
must first create a Slack account and workspace 
https://slack.com/get-started#/create.
+
+Then configure Slack custom configurations for 'Incoming Webhooks' & the 
'Slack API Tester' on your workspace.
+
+Next set the following environment variables:
+
+[source,shell]
+----
+export SLACK_TOKEN=your-slack-token
+export SLACK_SERVER_URL=https://slack.com
+export SLACK_WEBHOOK_URL=https://hooks.slack.com/services/some/webhook/path
+----
+
+If the WireMock stub recordings need updating, then remove the existing files 
from `src/test/resources/mappings` and run tests with either:
+
+System property `-Dwiremock.record=true`
+
+Or
+
+Set environment variable `WIREMOCK_RECORD=true`
diff --git a/integration-tests/slack/pom.xml b/integration-tests/slack/pom.xml
index aef247a..09fc268 100644
--- a/integration-tests/slack/pom.xml
+++ b/integration-tests/slack/pom.xml
@@ -62,6 +62,10 @@
             <artifactId>rest-assured</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-integration-wiremock-support</artifactId>
+        </dependency>
 
         <!-- The following dependencies guarantee that this module is built 
after them. You can update them by running `mvn process-resources -Pformat -N` 
from the source tree root directory -->
         <dependency>
diff --git 
a/integration-tests/slack/src/main/java/org/apache/camel/quarkus/component/slack/it/SlackResource.java
 
b/integration-tests/slack/src/main/java/org/apache/camel/quarkus/component/slack/it/SlackResource.java
index 38ff208..a8a1556 100644
--- 
a/integration-tests/slack/src/main/java/org/apache/camel/quarkus/component/slack/it/SlackResource.java
+++ 
b/integration-tests/slack/src/main/java/org/apache/camel/quarkus/component/slack/it/SlackResource.java
@@ -36,7 +36,7 @@ import org.apache.camel.component.slack.helper.SlackMessage;
 @ApplicationScoped
 public class SlackResource {
 
-    private static final String SLACK_AUTH_PARAMS = 
"serverUrl={{env:SLACK_SERVER_URL:http://localhost:8099/slack}}&token={{env:SLACK_TOKEN:a-fake-token-for-testing}}";;
+    private static final String SLACK_AUTH_PARAMS = 
"serverUrl={{sys:slack.server-url}}&token={{sys:slack.token}}";
 
     @Inject
     ProducerTemplate producerTemplate;
diff --git 
a/integration-tests/slack/src/main/java/org/apache/camel/quarkus/component/slack/it/SlackRoutes.java
 
b/integration-tests/slack/src/main/java/org/apache/camel/quarkus/component/slack/it/SlackRoutes.java
deleted file mode 100644
index 02d8d1e..0000000
--- 
a/integration-tests/slack/src/main/java/org/apache/camel/quarkus/component/slack/it/SlackRoutes.java
+++ /dev/null
@@ -1,48 +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.slack.it;
-
-import org.apache.camel.builder.RouteBuilder;
-
-public class SlackRoutes extends RouteBuilder {
-    @Override
-    public void configure() throws Exception {
-
-        // Stubbed API endpoints for testing without slack API credentials
-        restConfiguration()
-                .component("netty-http")
-                .host("0.0.0.0")
-                .port(8099);
-
-        rest()
-                .post("/webhook")
-                .route()
-                .setBody(constant("{\"ok\": true}"))
-                .endRest()
-                .post("/slack/api/conversations.list")
-                .route()
-                .setBody(constant(
-                        
"{\"ok\":true,\"channels\":[{\"id\":\"ABC12345\",\"name\":\"general\",\"is_channel\":true,\"created\":1571904169}]}"))
-                .endRest()
-                .post("/slack/api/conversations.history")
-                .route()
-                .setBody(constant(
-                        
"{\"ok\":true,\"messages\":[{\"type\":\"message\",\"subtype\":\"bot_message\",\"text\":\"Hello
 Camel Quarkus Slack\""
-                                + 
",\"ts\":\"1571912155.001300\",\"bot_id\":\"ABC12345C\"}],\"has_more\":true"
-                                + 
",\"channel_actions_ts\":null,\"channel_actions_count\":0}"));
-    }
-}
diff --git a/integration-tests/slack/src/main/resources/application.properties 
b/integration-tests/slack/src/main/resources/application.properties
deleted file mode 100644
index cdbbe3f..0000000
--- a/integration-tests/slack/src/main/resources/application.properties
+++ /dev/null
@@ -1,21 +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.
-## ---------------------------------------------------------------------------
-
-#
-# Camel :: Slack
-#
-camel.component.slack.webhook-url={{env:SLACK_WEBHOOK_URL:http://localhost:8099/webhook}}
diff --git 
a/integration-tests/slack/src/test/java/org/apache/camel/quarkus/component/slack/it/SlackTest.java
 
b/integration-tests/slack/src/test/java/org/apache/camel/quarkus/component/slack/it/SlackTest.java
index 77877af..c7b23b9 100644
--- 
a/integration-tests/slack/src/test/java/org/apache/camel/quarkus/component/slack/it/SlackTest.java
+++ 
b/integration-tests/slack/src/test/java/org/apache/camel/quarkus/component/slack/it/SlackTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.quarkus.component.slack.it;
 
 import java.util.UUID;
 
+import io.quarkus.test.common.QuarkusTestResource;
 import io.quarkus.test.junit.QuarkusTest;
 import io.restassured.RestAssured;
 import io.restassured.http.ContentType;
@@ -28,9 +29,6 @@ import static org.hamcrest.CoreMatchers.equalTo;
 /**
  * Camel Slack component tests.
  *
- * By default tests configure the Slack component to use stubbed Slack API 
responses
- * that are configured in {@link SlackRoutes}
- *
  * To test against a real Slack instance. Set up environment variables like 
the following:
  *
  * SLACK_WEBHOOK_URL=https://hooks.slack.com/services/unique/hook/path
@@ -38,6 +36,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
  * SLACK_TOKEN=your-slack-api-access-token
  */
 @QuarkusTest
+@QuarkusTestResource(SlackTestResource.class)
 class SlackTest {
 
     @Test
@@ -57,6 +56,6 @@ class SlackTest {
     }
 
     boolean externalSlackEnabled() {
-        return System.getenv("SLACK_WEBHOOK_URL") != null;
+        return System.getProperty("wiremock.url") == null;
     }
 }
diff --git 
a/integration-tests/slack/src/test/java/org/apache/camel/quarkus/component/slack/it/SlackTestResource.java
 
b/integration-tests/slack/src/test/java/org/apache/camel/quarkus/component/slack/it/SlackTestResource.java
new file mode 100644
index 0000000..1ce4ffc
--- /dev/null
+++ 
b/integration-tests/slack/src/test/java/org/apache/camel/quarkus/component/slack/it/SlackTestResource.java
@@ -0,0 +1,55 @@
+/*
+ * 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.slack.it;
+
+import java.util.Map;
+
+import 
org.apache.camel.quarkus.test.wiremock.WireMockTestResourceLifecycleManager;
+import org.apache.camel.util.CollectionHelper;
+
+public class SlackTestResource extends WireMockTestResourceLifecycleManager {
+
+    private static final String SLACK_API_BASE_URL = "https://api.slack.com/";;
+    private static final String SLACK_ENV_WEBHOOK_URL = "SLACK_WEBHOOK_URL";
+    private static final String SLACK_ENV_SERVER_URL = "SLACK_SERVER_URL";
+    private static final String SLACK_ENV_TOKEN = "SLACK_TOKEN";
+
+    @Override
+    public Map<String, String> start() {
+        Map<String, String> properties = super.start();
+        String wiremockUrl = properties.get("wiremock.url");
+        String serverUrl = wiremockUrl != null ? wiremockUrl : 
System.getenv(SLACK_ENV_SERVER_URL);
+        String webhookUrl = wiremockUrl != null ? wiremockUrl + 
"/services/webhook" : System.getenv(SLACK_ENV_WEBHOOK_URL);
+        return CollectionHelper.mergeMaps(properties, CollectionHelper.mapOf(
+                "camel.component.slack.webhook-url", webhookUrl,
+                "slack.server-url", serverUrl,
+                "slack.token", envOrDefault(SLACK_ENV_TOKEN, "test-token")));
+    }
+
+    @Override
+    protected String getRecordTargetBaseUrl() {
+        return SLACK_API_BASE_URL;
+    }
+
+    @Override
+    protected boolean isMockingEnabled() {
+        return !envVarsPresent(
+                SLACK_ENV_WEBHOOK_URL,
+                SLACK_ENV_SERVER_URL,
+                SLACK_ENV_TOKEN);
+    }
+}
diff --git 
a/integration-tests/slack/src/test/resources/mappings/slackGetChannels.json 
b/integration-tests/slack/src/test/resources/mappings/slackGetChannels.json
new file mode 100644
index 0000000..ec06cbd
--- /dev/null
+++ b/integration-tests/slack/src/test/resources/mappings/slackGetChannels.json
@@ -0,0 +1,37 @@
+{
+  "id" : "2a011f82-a594-4f21-91f6-7be02c035fa5",
+  "name" : "api_conversationslist",
+  "request" : {
+    "url" : "/api/conversations.list",
+    "method" : "POST",
+    "bodyPatterns" : [ {
+      "equalTo" : "token=test-token",
+      "caseInsensitive" : false
+    } ]
+  },
+  "response" : {
+    "status" : 200,
+    "body" : 
"{\"ok\":true,\"channels\":[{\"id\":\"CPT2U8YSK\",\"name\":\"random\",\"is_channel\":true,\"is_group\":false,\"is_im\":false,\"created\":1571904169,\"is_archived\":false,\"is_general\":false,\"unlinked\":0,\"name_normalized\":\"random\",\"is_shared\":false,\"parent_conversation\":null,\"creator\":\"UPJPHCRUH\",\"is_ext_shared\":false,\"is_org_shared\":false,\"shared_team_ids\":[\"TPR71CS7J\"],\"pending_shared\":[],\"pending_connected_team_ids\":[],\"is_pending_ext_shared\":f
 [...]
+    "headers" : {
+      "date" : "Mon, 02 Nov 2020 09:53:19 GMT",
+      "server" : "Apache",
+      "x-xss-protection" : "0",
+      "pragma" : "no-cache",
+      "cache-control" : "private, no-cache, no-store, must-revalidate",
+      "strict-transport-security" : "max-age=31536000; includeSubDomains; 
preload",
+      "x-slack-req-id" : "8500fbbc3969104df3a1a00fcf57ca3c",
+      "x-content-type-options" : "nosniff",
+      "referrer-policy" : "no-referrer",
+      "x-slack-backend" : "r",
+      "x-oauth-scopes" : "read,client,admin,identify,post,apps",
+      "x-accepted-oauth-scopes" : 
"channels:read,groups:read,mpim:read,im:read,read",
+      "expires" : "Mon, 26 Jul 1997 05:00:00 GMT",
+      "vary" : "Accept-Encoding",
+      "content-type" : "application/json; charset=utf-8",
+      "x-via" : "haproxy-www-0tj6,haproxy-edge-lhr-fchm"
+    }
+  },
+  "uuid" : "2a011f82-a594-4f21-91f6-7be02c035fa5",
+  "persistent" : true,
+  "insertionIndex" : 2
+}
\ No newline at end of file
diff --git 
a/integration-tests/slack/src/test/resources/mappings/slackGetConversationHistory.json
 
b/integration-tests/slack/src/test/resources/mappings/slackGetConversationHistory.json
new file mode 100644
index 0000000..1d9fbff
--- /dev/null
+++ 
b/integration-tests/slack/src/test/resources/mappings/slackGetConversationHistory.json
@@ -0,0 +1,37 @@
+{
+  "id" : "6e638406-ee78-4a8d-baff-bf20096039ca",
+  "name" : "api_conversationshistory",
+  "request" : {
+    "url" : "/api/conversations.history",
+    "method" : "POST",
+    "bodyPatterns" : [ {
+      "equalTo" : "channel=CPTD6F086&count=1&token=test-token",
+      "caseInsensitive" : false
+    } ]
+  },
+  "response" : {
+    "status" : 200,
+    "body" : 
"{\"ok\":true,\"messages\":[{\"type\":\"message\",\"subtype\":\"bot_message\",\"text\":\"Hello
 Camel Quarkus 
Slack\",\"ts\":\"1604310723.000600\",\"bot_id\":\"BTWSZ6PGX\"}],\"has_more\":true,\"pin_count\":0,\"channel_actions_ts\":null,\"channel_actions_count\":0,\"response_metadata\":{\"next_cursor\":\"bmV4dF90czoxNjA0MzEwNDg4MDAwNTAw\"}}",
+    "headers" : {
+      "date" : "Mon, 02 Nov 2020 09:53:20 GMT",
+      "server" : "Apache",
+      "x-xss-protection" : "0",
+      "pragma" : "no-cache",
+      "cache-control" : "private, no-cache, no-store, must-revalidate",
+      "strict-transport-security" : "max-age=31536000; includeSubDomains; 
preload",
+      "x-slack-req-id" : "4c02fef93a98415344290ac4dbc10bde",
+      "x-content-type-options" : "nosniff",
+      "referrer-policy" : "no-referrer",
+      "x-slack-backend" : "r",
+      "x-oauth-scopes" : "read,client,admin,identify,post,apps",
+      "x-accepted-oauth-scopes" : 
"channels:history,groups:history,mpim:history,im:history,read",
+      "expires" : "Mon, 26 Jul 1997 05:00:00 GMT",
+      "vary" : "Accept-Encoding",
+      "content-type" : "application/json; charset=utf-8",
+      "x-via" : "haproxy-www-6ya7,haproxy-edge-lhr-fchm"
+    }
+  },
+  "uuid" : "6e638406-ee78-4a8d-baff-bf20096039ca",
+  "persistent" : true,
+  "insertionIndex" : 3
+}
\ No newline at end of file
diff --git 
a/integration-tests/slack/src/test/resources/mappings/slackWebhookMesagePost.json
 
b/integration-tests/slack/src/test/resources/mappings/slackWebhookMesagePost.json
new file mode 100644
index 0000000..621acc3
--- /dev/null
+++ 
b/integration-tests/slack/src/test/resources/mappings/slackWebhookMesagePost.json
@@ -0,0 +1,30 @@
+{
+  "id" : "b83207ea-d573-449e-bf3f-6e55660166bd",
+  "name" : "services_webhook",
+  "request" : {
+    "url" : "/services/webhook",
+    "method" : "POST",
+    "bodyPatterns" : [ {
+      "equalTo" : 
"{\"icon_url\":null,\"icon_emoji\":null,\"channel\":\"general\",\"text\":\"Hello
 Camel Quarkus Slack\",\"username\":null}",
+      "caseInsensitive" : false
+    } ]
+  },
+  "response" : {
+    "status" : 200,
+    "body" : "ok",
+    "headers" : {
+      "date" : "Mon, 02 Nov 2020 09:52:03 GMT",
+      "server" : "Apache",
+      "strict-transport-security" : "max-age=31536000; includeSubDomains; 
preload",
+      "x-slack-backend" : "r",
+      "x-frame-options" : "SAMEORIGIN",
+      "vary" : "Accept-Encoding",
+      "referrer-policy" : "no-referrer",
+      "content-type" : "text/html",
+      "x-via" : "haproxy-www-plhy,haproxy-edge-lhr-s8de"
+    }
+  },
+  "uuid" : "b83207ea-d573-449e-bf3f-6e55660166bd",
+  "persistent" : true,
+  "insertionIndex" : 1
+}
\ No newline at end of file

Reply via email to