Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slack : fix native support for Webhook URL + add test coverage #3643

Merged
merged 1 commit into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.IndexDependencyBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import org.apache.camel.component.slack.helper.SlackMessage;
import org.jboss.jandex.IndexView;

class SlackProcessor {
Expand Down Expand Up @@ -56,4 +57,9 @@ ReflectiveClassBuildItem registerForReflection(CombinedIndexBuildItem combinedIn
.toArray(String[]::new);
return new ReflectiveClassBuildItem(false, true, slackApiClasses);
}

@BuildStep
ReflectiveClassBuildItem registerForReflection() {
return new ReflectiveClassBuildItem(false, true, SlackMessage.class);
}
}
2 changes: 1 addition & 1 deletion integration-tests/slack/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>

<!-- test dependencies -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package org.apache.camel.quarkus.component.slack.it;

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
Expand All @@ -29,8 +32,13 @@
import javax.ws.rs.core.Response;

import com.slack.api.model.Message;
import com.slack.api.model.block.DividerBlock;
import com.slack.api.model.block.LayoutBlock;
import com.slack.api.model.block.SectionBlock;
import com.slack.api.model.block.composition.MarkdownTextObject;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.quarkus.component.slack.it.model.SlackMessageResponse;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@Path("/slack")
Expand All @@ -48,19 +56,70 @@ public class SlackResource {
@ConfigProperty(name = "slack.token")
String slackToken;

@ConfigProperty(name = "slack.webhook.url")
String slackWebHookUrl;

@Path("/messages")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getSlackMessages() throws Exception {
@Produces(MediaType.APPLICATION_JSON)
public SlackMessageResponse getSlackMessages() throws Exception {
Message message = consumerTemplate.receiveBody("slack://test-channel?maxResults=1&" + getSlackAuthParams(),
5000L, Message.class);
return message.getText();
return new SlackMessageResponse(message.getText(), message.getBlocks() != null ? message.getBlocks().size() : 0);
}

@Path("/message/token")
@POST
@Consumes(MediaType.TEXT_PLAIN)
public Response createSlackMessageWithToken(String message) throws Exception {
producerTemplate.requestBody("slack://test-channel?" + getSlackAuthParams(), message);
return Response
.created(new URI("https://camel.apache.org/"))
.build();
}

@Path("/message")
@Path("/message/webhook")
@POST
@Consumes(MediaType.TEXT_PLAIN)
public Response createSlackMessage(String message) throws Exception {
public Response createSlackMessageWithWebhook(String message) throws Exception {
producerTemplate.requestBody(String.format("slack://test-channel?webhookUrl=%s", slackWebHookUrl), message);
return Response
.created(new URI("https://camel.apache.org/"))
.build();
}

@Path("/message/block")
@POST
@Consumes(MediaType.TEXT_PLAIN)
public Response createBlockMessage(String text) throws Exception {
List<LayoutBlock> blocks = new ArrayList();
blocks.add(SectionBlock
.builder()
.text(MarkdownTextObject
.builder()
.text(text)
.build())
.build());
blocks.add(SectionBlock
.builder()
.fields(Arrays.asList(
MarkdownTextObject
.builder()
.text("*Testing Camel Quarkus blocks*")
.build(),
MarkdownTextObject
.builder()
.text("*You should be able to see these blocks")
.build()))
.build());
blocks.add(DividerBlock
.builder()
.build());

Message message = new Message();
message.setText(text);
message.setBlocks(blocks);

producerTemplate.requestBody("slack://test-channel?" + getSlackAuthParams(), message);
return Response
.created(new URI("https://camel.apache.org/"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.model;

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection
public class SlackMessageResponse {
String text;
int nbBlocks;

public SlackMessageResponse() {
}

public SlackMessageResponse(String text, int nbBlocks) {
this.text = text;
this.nbBlocks = nbBlocks;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public int getNbBlocks() {
return nbBlocks;
}

public void setNbBlocks(int nbBlocks) {
this.nbBlocks = nbBlocks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import com.github.tomakehurst.wiremock.WireMockServer;
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.wiremock.MockServer;
import org.eclipse.microprofile.config.ConfigProvider;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;

/**
Expand All @@ -47,21 +47,62 @@ class SlackTest {

@Test
public void testSlackProduceConsumeMessages() {
final String message = "Hello Camel Quarkus Slack" + (externalSlackEnabled() ? " " + UUID.randomUUID() : "");
RestAssured.given()
// sending a message using Token
String message = "Hello Camel Quarkus Slack using Token" + (externalSlackEnabled() ? " " + UUID.randomUUID() : "");
given()
.contentType(ContentType.TEXT)
.body(message)
.post("/slack/message")
.post("/slack/message/token")
.then()
.statusCode(201);

RestAssured.get("/slack/messages")
given()
.contentType(ContentType.JSON)
.get("/slack/messages")
.then()
.statusCode(200)
.body(equalTo(message));
.body(equalTo(getExpectedResponse(message, 0)));

// sending a message using Webhook URL
message = "Hello Camel Quarkus Slack using Webhook URL" + (externalSlackEnabled() ? " " + UUID.randomUUID() : "");

given()
.contentType(ContentType.TEXT)
.body(message)
.post("/slack/message/webhook")
.then()
.statusCode(201);

given()
.contentType(ContentType.JSON)
.get("/slack/messages")
.then()
.statusCode(200)
.body(equalTo(getExpectedResponse(message, 0)));

message = "Hello Camel Quarkus Slack using Blocks" + (externalSlackEnabled() ? " " + UUID.randomUUID() : "");

// sending message with blocks
given()
.contentType(ContentType.TEXT)
.body(message)
.post("/slack/message/block")
.then()
.statusCode(201);

given()
.contentType(ContentType.JSON)
.get("/slack/messages")
.then()
.statusCode(200)
.body(equalTo(getExpectedResponse(message, 3)));
}

boolean externalSlackEnabled() {
return !ConfigProvider.getConfig().getOptionalValue("wiremock.url", String.class).isPresent();
}

String getExpectedResponse(String message, int nbBlocks) {
return String.format("{\"text\":\"%s\",\"nbBlocks\":%s}", message, nbBlocks);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Map<String, String> start() {
String webhookUrl = wiremockUrl != null ? wiremockUrl + "/services/webhook"
: ConfigProvider.getConfig().getValue(SLACK_ENV_WEBHOOK_URL, String.class);
return CollectionHelper.mergeMaps(properties, CollectionHelper.mapOf(
"camel.component.slack.webhook-url", webhookUrl,
"slack.webhook.url", webhookUrl,
"slack.server-url", serverUrl,
"slack.token", envOrDefault(SLACK_ENV_TOKEN, "test-token")));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"id" : "c86380a0-2c6e-41ec-8183-467013109f96",
"name" : "chatpostmessage",
"request" : {
"url" : "/api/chat.postMessage",
"method" : "POST",
"bodyPatterns" : [ {
"equalTo" : "channel=test-channel&text=Hello%20Camel%20Quarkus%20Slack%20using%20Blocks&link_names=0&mrkdwn=1&blocks=%5B%7B%22type%22%3A%22section%22%2C%22text%22%3A%7B%22type%22%3A%22mrkdwn%22%2C%22text%22%3A%22Hello%20Camel%20Quarkus%20Slack%20using%20Blocks%22%7D%7D%2C%7B%22type%22%3A%22section%22%2C%22fields%22%3A%5B%7B%22type%22%3A%22mrkdwn%22%2C%22text%22%3A%22*Testing%20Camel%20Quarkus%20blocks*%22%7D%2C%7B%22type%22%3A%22mrkdwn%22%2C%22text%22%3A%22*You%20should%20be%20able%20to%20see%20these%20blocks%22%7D%5D%7D%2C%7B%22type%22%3A%22divider%22%7D%5D&unfurl_links=0&unfurl_media=0&reply_broadcast=0",
"caseInsensitive" : false
} ]
},
"response" : {
"status" : 200,
"body" : "{\"ok\":true,\"channel\":\"test\",\"ts\":\"1615377778.002900\",\"message\":{\"bot_id\":\"test\",\"type\":\"message\",\"text\":\"Hello Camel Quarkus Slack\",\"user\":\"test\",\"ts\":\"1615377778.002900\",\"team\":\"test\",\"bot_profile\":{\"id\":\"test\",\"deleted\":false,\"name\":\"API Test\",\"updated\":1615375056,\"app_id\":\"test\",\"icons\":{\"image_36\":\"https:\\/\\/a.slack-edge.com\\/80588\\/img\\/plugins\\/app\\/bot_36.png\",\"image_48\":\"https:\\/\\/a.slack-edge.com\\/80588\\/img\\/plugins\\/app\\/bot_48.png\",\"image_72\":\"https:\\/\\/a.slack-edge.com\\/80588\\/img\\/plugins\\/app\\/service_72.png\"},\"team_id\":\"test\"}}}",
"headers" : {
"date" : "Wed, 10 Mar 2021 12:02:58 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" : "f353e4e5fc6450a0ca4a32ed0ad4e203",
"x-content-type-options" : "nosniff",
"referrer-policy" : "no-referrer",
"x-slack-backend" : "r",
"x-oauth-scopes" : "incoming-webhook,chat:write,channels:read,groups:read,mpim:read,im:read,channels:history,groups:history,mpim:history,im:history",
"x-accepted-oauth-scopes" : "chat:write",
"expires" : "Mon, 26 Jul 1997 05:00:00 GMT",
"vary" : "Accept-Encoding",
"content-type" : "application/json; charset=utf-8",
"x-envoy-upstream-service-time" : "61",
"x-backend" : "main_normal main_canary_with_overflow main_control_with_overflow",
"x-server" : "slack-www-hhvm-main-iad-au28",
"x-via" : "envoy-www-iad-yrq3, haproxy-edge-lhr-ubwz",
"x-slack-shared-secret-outcome" : "shared-secret",
"via" : "envoy-www-iad-yrq3"
}
},
"uuid" : "c86380a0-2c6e-41ec-8183-467013109f96",
"persistent" : true,
"insertionIndex" : 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"url" : "/api/chat.postMessage",
"method" : "POST",
"bodyPatterns" : [ {
"equalTo" : "channel=test-channel&text=Hello%20Camel%20Quarkus%20Slack&link_names=0&mrkdwn=1&unfurl_links=0&unfurl_media=0&reply_broadcast=0",
"equalTo" : "channel=test-channel&text=Hello%20Camel%20Quarkus%20Slack%20using%20Token&link_names=0&mrkdwn=1&unfurl_links=0&unfurl_media=0&reply_broadcast=0",
"caseInsensitive" : false
} ]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"id" : "c86380a0-2c6e-41ec-8183-467013109f96",
"name" : "chatpostmessage",
"request" : {
"url" : "/services/webhook",
"method" : "POST",
"bodyPatterns" : [ {
"equalTo" : "{\"text\":\"Hello Camel Quarkus Slack using Webhook URL\",\"channel\":\"test-channel\"}",
"caseInsensitive" : false
} ]
},
"response" : {
"status" : 200,
"body" : "{\"ok\":true,\"channel\":\"test\",\"ts\":\"1615377778.002900\",\"message\":{\"bot_id\":\"test\",\"type\":\"message\",\"text\":\"Hello Camel Quarkus Slack\",\"user\":\"test\",\"ts\":\"1615377778.002900\",\"team\":\"test\",\"bot_profile\":{\"id\":\"test\",\"deleted\":false,\"name\":\"API Test\",\"updated\":1615375056,\"app_id\":\"test\",\"icons\":{\"image_36\":\"https:\\/\\/a.slack-edge.com\\/80588\\/img\\/plugins\\/app\\/bot_36.png\",\"image_48\":\"https:\\/\\/a.slack-edge.com\\/80588\\/img\\/plugins\\/app\\/bot_48.png\",\"image_72\":\"https:\\/\\/a.slack-edge.com\\/80588\\/img\\/plugins\\/app\\/service_72.png\"},\"team_id\":\"test\"}}}",
"headers" : {
"date" : "Wed, 10 Mar 2021 12:02:58 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" : "f353e4e5fc6450a0ca4a32ed0ad4e203",
"x-content-type-options" : "nosniff",
"referrer-policy" : "no-referrer",
"x-slack-backend" : "r",
"x-oauth-scopes" : "incoming-webhook,chat:write,channels:read,groups:read,mpim:read,im:read,channels:history,groups:history,mpim:history,im:history",
"x-accepted-oauth-scopes" : "chat:write",
"expires" : "Mon, 26 Jul 1997 05:00:00 GMT",
"vary" : "Accept-Encoding",
"content-type" : "application/json; charset=utf-8",
"x-envoy-upstream-service-time" : "61",
"x-backend" : "main_normal main_canary_with_overflow main_control_with_overflow",
"x-server" : "slack-www-hhvm-main-iad-au28",
"x-via" : "envoy-www-iad-yrq3, haproxy-edge-lhr-ubwz",
"x-slack-shared-secret-outcome" : "shared-secret",
"via" : "envoy-www-iad-yrq3"
}
},
"uuid" : "c86380a0-2c6e-41ec-8183-467013109f96",
"persistent" : true,
"insertionIndex" : 2
}