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

bossenti pushed a commit to branch feat-ms-teams-sink
in repository https://gitbox.apache.org/repos/asf/streampipes.git

commit e02d63a355f9b7dcfc35c3457bcf55203537a0f8
Author: bossenti <[email protected]>
AuthorDate: Thu Nov 30 09:40:31 2023 +0100

    feat: implement sink for MS Teams
---
 .../streampipes-sinks-notifications-jvm/pom.xml    |   7 +
 .../jvm/NotificationsExtensionModuleExport.java    |   2 +
 .../notifications/jvm/msteams/MSTeamsSink.java     | 223 +++++++++++++++++++++
 .../documentation.md                               |  74 +++++++
 .../icon.png                                       | Bin 0 -> 126479 bytes
 .../strings.en                                     |  35 ++++
 .../notifications/jvm/msteams/TestMSTeamsSink.java | 123 ++++++++++++
 7 files changed, 464 insertions(+)

diff --git a/streampipes-extensions/streampipes-sinks-notifications-jvm/pom.xml 
b/streampipes-extensions/streampipes-sinks-notifications-jvm/pom.xml
index 78c44edca..d8713c5c8 100644
--- a/streampipes-extensions/streampipes-sinks-notifications-jvm/pom.xml
+++ b/streampipes-extensions/streampipes-sinks-notifications-jvm/pom.xml
@@ -54,6 +54,13 @@
             <groupId>com.squareup.okhttp3</groupId>
             <artifactId>okhttp</artifactId>
         </dependency>
+
+        <!-- Test dependencies -->
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
     <build>
         <plugins>
diff --git 
a/streampipes-extensions/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/NotificationsExtensionModuleExport.java
 
b/streampipes-extensions/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/NotificationsExtensionModuleExport.java
index 5b59a64ea..3c3d29360 100644
--- 
a/streampipes-extensions/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/NotificationsExtensionModuleExport.java
+++ 
b/streampipes-extensions/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/NotificationsExtensionModuleExport.java
@@ -23,6 +23,7 @@ import 
org.apache.streampipes.extensions.api.declarer.IExtensionModuleExport;
 import org.apache.streampipes.extensions.api.migration.IModelMigrator;
 import org.apache.streampipes.extensions.api.pe.IStreamPipesPipelineElement;
 import org.apache.streampipes.sinks.notifications.jvm.email.EmailSink;
+import org.apache.streampipes.sinks.notifications.jvm.msteams.MSTeamsSink;
 import org.apache.streampipes.sinks.notifications.jvm.onesignal.OneSignalSink;
 import 
org.apache.streampipes.sinks.notifications.jvm.slack.SlackNotificationSink;
 import org.apache.streampipes.sinks.notifications.jvm.telegram.TelegramSink;
@@ -40,6 +41,7 @@ public class NotificationsExtensionModuleExport implements 
IExtensionModuleExpor
   public List<IStreamPipesPipelineElement<?>> pipelineElements() {
     return List.of(
         new EmailSink(),
+        new MSTeamsSink(),
         new OneSignalSink(),
         new SlackNotificationSink(),
         new TelegramSink()
diff --git 
a/streampipes-extensions/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/msteams/MSTeamsSink.java
 
b/streampipes-extensions/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/msteams/MSTeamsSink.java
new file mode 100644
index 000000000..b2dd09203
--- /dev/null
+++ 
b/streampipes-extensions/streampipes-sinks-notifications-jvm/src/main/java/org/apache/streampipes/sinks/notifications/jvm/msteams/MSTeamsSink.java
@@ -0,0 +1,223 @@
+/*
+ * 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.streampipes.sinks.notifications.jvm.msteams;
+
+import org.apache.http.HttpStatus;
+import org.apache.streampipes.commons.exceptions.SpRuntimeException;
+import 
org.apache.streampipes.extensions.api.pe.context.EventSinkRuntimeContext;
+import org.apache.streampipes.model.DataSinkType;
+import org.apache.streampipes.model.graph.DataSinkDescription;
+import org.apache.streampipes.model.runtime.Event;
+import org.apache.streampipes.pe.shared.PlaceholderExtractor;
+import org.apache.streampipes.sdk.StaticProperties;
+import org.apache.streampipes.sdk.builder.DataSinkBuilder;
+import org.apache.streampipes.sdk.builder.StreamRequirementsBuilder;
+import org.apache.streampipes.sdk.helpers.Alternatives;
+import org.apache.streampipes.sdk.helpers.EpRequirements;
+import org.apache.streampipes.sdk.helpers.Labels;
+import org.apache.streampipes.sdk.helpers.Locales;
+import org.apache.streampipes.sdk.utils.Assets;
+import org.apache.streampipes.wrapper.params.compat.SinkParams;
+import org.apache.streampipes.wrapper.standalone.StreamPipesDataSink;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.HttpClients;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public class MSTeamsSink extends StreamPipesDataSink {
+
+  private static final String KEY_MESSAGE_ADVANCED = "messageAdvanced";
+  private static final String KEY_MESSAGE_ADVANCED_CONTENT = 
"messageContentAdvanced";
+  private static final String KEY_MESSAGE_SIMPLE = "messageSimple";
+  private static final String KEY_MESSAGE_SIMPLE_CONTENT = 
"messageContentSimple";
+  private static final String KEY_MESSAGE_TYPE_ALTERNATIVES = "messageType";
+  private static final String KEY_WEBHOOK_URL = "webhookUrl";
+  protected static final String SIMPLE_MESSAGE_TEMPLATE = "{\"text\": \"%s\"}";
+
+  private String messageContent;
+  private boolean isSimpleMessageMode;
+  private String webhookUrl;
+
+  @Override
+  public void onEvent(Event event) {
+
+    // This sink allows to use placeholders for event properties when defining 
the message content in the UI
+    // Therefore, we need to replace these placeholders based on the actual 
event before actually sending the message
+    var processedMessageContent = 
PlaceholderExtractor.replacePlaceholders(event, messageContent);
+
+    String teamsMessageContent;
+    if (isSimpleMessageMode) {
+      teamsMessageContent = 
createMessageFromSimpleContent(processedMessageContent);
+    } else {
+      try {
+        teamsMessageContent = 
createMessageFromAdvancedContent(processedMessageContent);
+      } catch (JsonProcessingException e) {
+        throw new RuntimeException(
+                "Advanced message content provided is not a valid JSON string: 
%s"
+                        .formatted(messageContent)
+        );
+      }
+    }
+
+    try {
+      sendPayloadToWebhook(HttpClients.createDefault(), teamsMessageContent, 
webhookUrl);
+    } catch (IOException e) {
+      throw new RuntimeException("Sending notification to MS Teams failed.", 
e);
+    }
+  }
+
+  @Override
+  public DataSinkDescription declareModel() {
+    return 
DataSinkBuilder.create("org.apache.streampipes.sinks.notifications.jvm.msteams")
+            .withLocales(Locales.EN)
+            .withAssets(Assets.DOCUMENTATION, Assets.ICON)
+            .category(DataSinkType.NOTIFICATION)
+            .requiredStream(
+                    StreamRequirementsBuilder
+                            .create()
+                            .requiredProperty(EpRequirements.anyProperty())
+                            .build()
+            )
+            .requiredSecret(Labels.withId(KEY_WEBHOOK_URL))
+            .requiredAlternatives(
+                    Labels.withId(KEY_MESSAGE_TYPE_ALTERNATIVES),
+                    Alternatives.from(
+                            Labels.withId(KEY_MESSAGE_SIMPLE),
+                            
StaticProperties.stringFreeTextProperty(Labels.withId(KEY_MESSAGE_SIMPLE_CONTENT),
 true, true),
+                            true),
+                    Alternatives.from(
+                            Labels.withId(KEY_MESSAGE_ADVANCED),
+                            StaticProperties.stringFreeTextProperty(
+                                    
Labels.withId(KEY_MESSAGE_ADVANCED_CONTENT),
+                                    true,
+                                    true
+                            )
+                    )
+            )
+            .build();
+  }
+
+  @Override
+  public void onInvocation(
+          SinkParams parameters,
+          EventSinkRuntimeContext runtimeContext
+  ) throws SpRuntimeException {
+    var extractor = parameters.extractor();
+    webhookUrl = extractor.secretValue(KEY_WEBHOOK_URL);
+
+    validateWebhookUrl(webhookUrl);
+
+    var selectedAlternative = 
extractor.selectedAlternativeInternalId(KEY_MESSAGE_TYPE_ALTERNATIVES);
+    if (selectedAlternative.equals(KEY_MESSAGE_ADVANCED)) {
+      isSimpleMessageMode = false;
+      messageContent = 
extractor.singleValueParameter(KEY_MESSAGE_ADVANCED_CONTENT, String.class);
+    } else {
+      isSimpleMessageMode = true;
+      messageContent = 
extractor.singleValueParameter(KEY_MESSAGE_SIMPLE_CONTENT, String.class);
+    }
+
+  }
+
+  @Override
+  public void onDetach() {
+    // nothing to do
+  }
+
+  /**
+   * Creates a JSON string intended for the MS Teams Webhook URL based on the 
provided plain message content.
+   * <p>
+   * This method utilizes a basic approach for constructing messages to be 
sent to MS Teams.
+   * If you intend to provide text in the form of Adaptive Cards, consider 
using
+   * {@link #createMessageFromAdvancedContent(String)} for a more advanced and 
interactive message format.
+   * </p>
+   *
+   * @param messageContent The plain message content to be included in the 
Teams message.
+   * @return A JSON string formatted using a predefined template with the 
provided message content.
+   */
+  protected String createMessageFromSimpleContent(String messageContent) {
+    return SIMPLE_MESSAGE_TEMPLATE.formatted(messageContent);
+  }
+
+  /**
+   * Creates a message for MS Teams from a JSON string, specifically designed 
for use with Adaptive Cards.
+   * <p>
+   * This method takes a JSON string as input, which is expected to represent 
the content of the message.
+   * The content is directly forwarded to MS Teams, allowing for the 
utilization of Adaptive Cards.
+   * Adaptive Cards provide a flexible and interactive way to present content 
in Microsoft Teams.
+   * Learn more about Adaptive Cards: <a 
href="https://learn.microsoft.com/en-us/adaptive-cards/";>here</a>
+   * </p>
+   *
+   * @param messageContent The JSON string representing the content of the 
message.
+   * @return The original JSON string, unchanged.
+   * @throws JsonProcessingException If the provided message is not a valid 
JSON string.
+   */
+  protected String createMessageFromAdvancedContent(String messageContent) 
throws JsonProcessingException {
+    new ObjectMapper().readValue(messageContent, Object.class);
+    return messageContent;
+  }
+
+  /**
+   * Sends a payload to a webhook using the provided HTTP client, payload, and 
webhook URL.
+   *
+   * @param httpClient The HTTP client used to send the payload.
+   * @param payload    The payload to be sent to the webhook.
+   * @param webhookUrl The URL of the webhook to which the payload will be 
sent.
+   * @throws IOException If an I/O error occurs while sending the payload to 
the webhook.
+   */
+  protected void sendPayloadToWebhook(HttpClient httpClient, String payload, 
String webhookUrl) throws IOException {
+    var contentEntity = new StringEntity(payload);
+    contentEntity.setContentType(ContentType.APPLICATION_JSON.toString());
+
+    var postRequest = new HttpPost(webhookUrl);
+    postRequest.setEntity(contentEntity);
+
+    var result  = httpClient.execute(postRequest);
+    if (result.getStatusLine().getStatusCode() == HttpStatus.SC_BAD_REQUEST) {
+      throw new RuntimeException(
+              "The provided message payload was not accepted by the MS Teams 
API: %s"
+                      .formatted(payload)
+      );
+    }
+  }
+
+  /**
+   * Validates a webhook URL to ensure it is not null, not empty, and has a 
valid URL format.
+   *
+   * @param webhookUrl The webhook URL to be validated.
+   * @throws RuntimeException If the webhook URL is null or empty, or if it is 
not a valid URL.
+   */
+  protected void validateWebhookUrl(String webhookUrl) {
+    if (webhookUrl == null || webhookUrl.isEmpty()) {
+      throw new RuntimeException("Given webhook URL is empty");
+    }
+    try {
+      new URL(webhookUrl);
+    } catch (MalformedURLException e) {
+      throw new RuntimeException("The given webhook is not a valid URL: 
%s".formatted(webhookUrl));
+    }
+  }
+}
diff --git 
a/streampipes-extensions/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.msteams/documentation.md
 
b/streampipes-extensions/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.msteams/documentation.md
new file mode 100644
index 000000000..b97aec9c4
--- /dev/null
+++ 
b/streampipes-extensions/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.msteams/documentation.md
@@ -0,0 +1,74 @@
+<!--
+  ~ 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.
+  ~
+  -->
+
+# MS Teams Sink
+
+<p align="center"> 
+    <img src="icon.png" width="150px;" class="pe-image-documentation"/>
+</p>
+
+---
+
+## Description
+
+The MS Teams Sink is a StreamPipes data sink that facilitates the sending of 
messages to a Microsoft Teams channel
+through a Webhook URL. Whether you need to convey simple text messages or 
employ more advanced formatting with [Adaptive
+Cards](https://adaptivecards.io/), this sink provides a versatile solution for 
integrating StreamPipes with Microsoft Teams.
+
+---
+
+## Required input
+
+The MS Teams Sink does not have any specific requirements for incoming event 
types. It is designed to work seamlessly
+with any type of incoming event, making it a versatile choice for various use 
cases.
+
+---
+
+## Configuration
+
+#### Webhook URL
+
+To configure the MS Teams Sink, you need to provide the Webhook URL that 
enables the sink to send messages to a specific
+MS Teams channel. If you don't have a Webhook URL, you can learn how to create
+one 
[here](https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook?tabs=dotnet#create-incoming-webhooks-1).
+
+#### Message Content Options
+
+You can choose between two message content formats:
+
+- **Simple Message Content:** Supports plain text and basic markdown 
formatting.
+- **Advanced Message Content:** Expects JSON input directly forwarded to Teams 
without modification. This format is
+  highly customizable and can be used for Adaptive Cards.
+
+Choose the format that best suits your messaging needs.
+
+---
+
+## Usage
+
+#### Simple Message Format
+
+In the simple message format, you can send plain text messages or utilize 
basic markdown formatting to convey
+information. This is ideal for straightforward communication needs.
+
+#### Advanced Message Format
+
+For more sophisticated messaging requirements, the advanced message format 
allows you to send JSON content directly to
+Microsoft Teams without modification. This feature is especially powerful when 
used
+with [Adaptive Cards](https://learn.microsoft.com/en-us/adaptive-cards/), 
enabling interactive and dynamic content in
+your Teams messages.
diff --git 
a/streampipes-extensions/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.msteams/icon.png
 
b/streampipes-extensions/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.msteams/icon.png
new file mode 100644
index 000000000..c8479db69
Binary files /dev/null and 
b/streampipes-extensions/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.msteams/icon.png
 differ
diff --git 
a/streampipes-extensions/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.msteams/strings.en
 
b/streampipes-extensions/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.msteams/strings.en
new file mode 100644
index 000000000..1457a3c77
--- /dev/null
+++ 
b/streampipes-extensions/streampipes-sinks-notifications-jvm/src/main/resources/org.apache.streampipes.sinks.notifications.jvm.msteams/strings.en
@@ -0,0 +1,35 @@
+#
+# 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.
+#
+
+org.apache.streampipes.sinks.notifications.jvm.msteams.title=MS Teams Sink
+org.apache.streampipes.sinks.notifications.jvm.msteams.description=Facilitates 
the sending of messages to a Microsoft Teams channel through a Webhook URL.
+
+messageType.title=Select the Message Type
+messageType.description=Messages sent to MS Teams can be provided in a simple 
or an advanced format
+
+messageContentSimple.title=Simple Message Format
+messageContentSimple.description=Provide plain text messages or utilize basic 
markdown formatting
+
+messageSimple.title=Simple Message Format
+
+messageContentAdvanced.title=Advanced Message Format
+messageContentAdvanced.description=This input mode allows you to freely define 
the message content sent to MS Teams. Therefore it is expected to be in JSON 
format. For more information about how messages are allowed to look like, 
please refer to 
https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using?tabs=cURL.
+
+messageAdvanced.title=Advanced Message Format
+
+webhookUrl.title=Webhook URL
+webhookUrl.description=URL of the Webhook that allows to notifications to a 
dedicated channel in MS Teams.
diff --git 
a/streampipes-extensions/streampipes-sinks-notifications-jvm/src/test/java/org/apache/streampipes/sinks/notifications/jvm/msteams/TestMSTeamsSink.java
 
b/streampipes-extensions/streampipes-sinks-notifications-jvm/src/test/java/org/apache/streampipes/sinks/notifications/jvm/msteams/TestMSTeamsSink.java
new file mode 100644
index 000000000..26f7ab992
--- /dev/null
+++ 
b/streampipes-extensions/streampipes-sinks-notifications-jvm/src/test/java/org/apache/streampipes/sinks/notifications/jvm/msteams/TestMSTeamsSink.java
@@ -0,0 +1,123 @@
+/*
+ * 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.streampipes.sinks.notifications.jvm.msteams;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import org.apache.http.HttpStatus;
+import org.apache.http.StatusLine;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentType;
+import org.apache.http.util.EntityUtils;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+
+import java.io.IOException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.assertArg;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class TestMSTeamsSink {
+
+  @Test
+  public void createMessageFromSimpleContent() {
+    var messageContent = "This is test";
+    var sink = new MSTeamsSink();
+
+    assertEquals(
+            MSTeamsSink.SIMPLE_MESSAGE_TEMPLATE.formatted(messageContent),
+            sink.createMessageFromSimpleContent(messageContent)
+    );
+  }
+
+  @Test
+  public void createMessageFromAdvancedContent() throws 
JsonProcessingException {
+    var messageContent = "{\"text\": \"Hi this is a message from Apache 
StreamPipes\"}";
+
+    var sink = new MSTeamsSink();
+
+    assertEquals(
+            messageContent,
+            sink.createMessageFromAdvancedContent(messageContent)
+    );
+  }
+
+  @Test
+  public void createMessageFromAdvancedContentCheckException() {
+    var messageContent = "invalid-complex-input";
+
+    var sink = new MSTeamsSink();
+
+    assertThrows(JsonProcessingException.class, () -> 
sink.createMessageFromAdvancedContent(messageContent));
+  }
+
+  @Test
+  public void sendPayloadToWebhook() throws IOException {
+
+    var mockedClient = mock(HttpClient.class);
+    var argumentCaptor = ArgumentCaptor.forClass(HttpPost.class);
+
+    var payload = "This is a test";
+    var webhook = "https://webhook.com";;
+    var sink = new MSTeamsSink();
+
+    sink.sendPayloadToWebhook(mockedClient, payload, webhook);
+    verify(mockedClient, times(1)).execute(argumentCaptor.capture());
+
+
+    var capturedPost = argumentCaptor.getValue();
+
+    assertNotNull(capturedPost);
+    assertEquals(webhook, capturedPost.getURI().toString());
+    assertEquals(ContentType.APPLICATION_JSON.toString(), 
capturedPost.getEntity().getContentType().getValue());
+    assertEquals(payload, EntityUtils.toString(capturedPost.getEntity()));
+  }
+
+  @Test
+  public void sendPayloadToWebhookBadResponse() throws  IOException {
+    var mockedClient = mock(HttpClient.class);
+    var mockedResponse = mock(CloseableHttpResponse.class);
+    var mockedStatusLine = mock(StatusLine.class);
+
+    
when(mockedStatusLine.getStatusCode()).thenReturn(HttpStatus.SC_BAD_REQUEST);
+    when(mockedResponse.getStatusLine()).thenReturn(mockedStatusLine);
+    when(mockedClient.execute(any())).thenReturn(mockedResponse);
+
+    var sink = new MSTeamsSink();
+    var payload = "<a>invalid</a>";
+    var url = "https://webhook.com";;
+
+    assertThrows(RuntimeException.class, () -> 
sink.sendPayloadToWebhook(mockedClient, payload, url));
+  }
+
+  @Test
+  public void validateWebhookUrl() {
+    var sink = new MSTeamsSink();
+    assertThrows(RuntimeException.class, () -> sink.validateWebhookUrl(""));
+    assertThrows(RuntimeException.class, () -> 
sink.validateWebhookUrl("some-string"));
+  }
+}

Reply via email to