You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by zb...@apache.org on 2023/03/01 09:41:43 UTC

[camel-quarkus] 02/02: Telegram: add integration test for the webhook endpoint

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

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

commit eda3bb96ccd51928c24598347d717ce5caa58757
Author: Zineb Bendhiba <be...@gmail.com>
AuthorDate: Tue Feb 28 17:45:34 2023 +0100

    Telegram: add integration test for the webhook endpoint
---
 integration-tests/telegram/pom.xml                 | 17 +++++++
 .../quarkus/component/telegram/it/Routes.java      |  3 +-
 .../component/telegram/it/TelegramResource.java    | 17 +++++++
 .../component/telegram/it/TelegramTest.java        | 55 ++++++++++++++++++++--
 4 files changed, 88 insertions(+), 4 deletions(-)

diff --git a/integration-tests/telegram/pom.xml b/integration-tests/telegram/pom.xml
index 2ef571f356..8cec6780ea 100644
--- a/integration-tests/telegram/pom.xml
+++ b/integration-tests/telegram/pom.xml
@@ -48,6 +48,10 @@
             <groupId>org.apache.camel.quarkus</groupId>
             <artifactId>camel-quarkus-rest</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-mock</artifactId>
+        </dependency>
         <!-- To serve the mock Telegram API -->
         <dependency>
             <groupId>org.apache.camel.quarkus</groupId>
@@ -138,6 +142,19 @@
                         </exclusion>
                     </exclusions>
                 </dependency>
+                <dependency>
+                    <groupId>org.apache.camel.quarkus</groupId>
+                    <artifactId>camel-quarkus-mock-deployment</artifactId>
+                    <version>${project.version}</version>
+                    <type>pom</type>
+                    <scope>test</scope>
+                    <exclusions>
+                        <exclusion>
+                            <groupId>*</groupId>
+                            <artifactId>*</artifactId>
+                        </exclusion>
+                    </exclusions>
+                </dependency>
                 <dependency>
                     <groupId>org.apache.camel.quarkus</groupId>
                     <artifactId>camel-quarkus-rest-deployment</artifactId>
diff --git a/integration-tests/telegram/src/main/java/org/apache/camel/quarkus/component/telegram/it/Routes.java b/integration-tests/telegram/src/main/java/org/apache/camel/quarkus/component/telegram/it/Routes.java
index 1dda144808..dab1578264 100644
--- a/integration-tests/telegram/src/main/java/org/apache/camel/quarkus/component/telegram/it/Routes.java
+++ b/integration-tests/telegram/src/main/java/org/apache/camel/quarkus/component/telegram/it/Routes.java
@@ -25,6 +25,7 @@ public class Routes extends RouteBuilder {
     @Override
     public void configure() throws Exception {
         from("webhook:telegram:bots?webhookPath=/my-test&webhook-external-url={{webhook-external-url}}&authorization-token={{webhook-authorization-token}}")
-                .log("webhook message : ${body}");
+                .log("webhook message : ${body}")
+                .to("mock:webhook");
     }
 }
diff --git a/integration-tests/telegram/src/main/java/org/apache/camel/quarkus/component/telegram/it/TelegramResource.java b/integration-tests/telegram/src/main/java/org/apache/camel/quarkus/component/telegram/it/TelegramResource.java
index cc7038fca3..86931098c4 100644
--- a/integration-tests/telegram/src/main/java/org/apache/camel/quarkus/component/telegram/it/TelegramResource.java
+++ b/integration-tests/telegram/src/main/java/org/apache/camel/quarkus/component/telegram/it/TelegramResource.java
@@ -29,8 +29,11 @@ import jakarta.ws.rs.Path;
 import jakarta.ws.rs.Produces;
 import jakarta.ws.rs.core.MediaType;
 import jakarta.ws.rs.core.Response;
+import org.apache.camel.CamelContext;
 import org.apache.camel.ConsumerTemplate;
+import org.apache.camel.Exchange;
 import org.apache.camel.ProducerTemplate;
+import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.component.telegram.TelegramConstants;
 import org.apache.camel.component.telegram.TelegramMediaType;
 import org.apache.camel.component.telegram.model.EditMessageLiveLocationMessage;
@@ -55,6 +58,9 @@ public class TelegramResource {
     @Inject
     ConsumerTemplate consumerTemplate;
 
+    @Inject
+    CamelContext context;
+
     @ConfigProperty(name = "telegram.chatId")
     String chatId;
 
@@ -180,4 +186,15 @@ public class TelegramResource {
                 .build();
     }
 
+    @Path("/webhook")
+    @Produces(MediaType.TEXT_PLAIN)
+    @GET
+    public String webhookMessages() {
+        final MockEndpoint mockEndpoint = context.getEndpoint("mock:webhook", MockEndpoint.class);
+        return mockEndpoint.getReceivedExchanges().stream()
+                .map(Exchange::getMessage)
+                .map(m -> m.getBody(String.class))
+                .findFirst().orElse("");
+    }
+
 }
diff --git a/integration-tests/telegram/src/test/java/org/apache/camel/quarkus/component/telegram/it/TelegramTest.java b/integration-tests/telegram/src/test/java/org/apache/camel/quarkus/component/telegram/it/TelegramTest.java
index 6c636f7dd6..8da35c824d 100644
--- a/integration-tests/telegram/src/test/java/org/apache/camel/quarkus/component/telegram/it/TelegramTest.java
+++ b/integration-tests/telegram/src/test/java/org/apache/camel/quarkus/component/telegram/it/TelegramTest.java
@@ -33,7 +33,6 @@ import org.apache.camel.component.telegram.model.SendVenueMessage;
 import org.apache.camel.component.telegram.model.StopMessageLiveLocationMessage;
 import org.apache.camel.quarkus.test.TrustStoreResource;
 import org.apache.camel.quarkus.test.wiremock.MockServer;
-import org.awaitility.Awaitility;
 import org.jboss.logging.Logger;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
@@ -42,6 +41,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
 import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
 import static com.github.tomakehurst.wiremock.client.WireMock.request;
 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
+import static org.awaitility.Awaitility.await;
 import static org.hamcrest.Matchers.both;
 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
 import static org.hamcrest.Matchers.is;
@@ -171,7 +171,7 @@ public class TelegramTest {
         edit.setChatId(result.getMessage().getChat().getId());
         edit.setMessageId(result.getMessage().getMessageId());
         /* The edit fails with various 400 errors unless we wait a bit */
-        Awaitility.await()
+        await()
                 .pollDelay(500, TimeUnit.MILLISECONDS)
                 .pollInterval(100, TimeUnit.MILLISECONDS)
                 .atMost(10, TimeUnit.SECONDS).until(() -> {
@@ -189,7 +189,7 @@ public class TelegramTest {
         stop.setChatId(result.getMessage().getChat().getId());
         stop.setMessageId(result.getMessage().getMessageId());
         // Poll until success as there's some potential for HTTP 400 responses to sometimes be returned
-        Awaitility.await()
+        await()
                 .pollDelay(500, TimeUnit.MILLISECONDS)
                 .pollInterval(100, TimeUnit.MILLISECONDS)
                 .atMost(10, TimeUnit.SECONDS).until(() -> {
@@ -215,4 +215,53 @@ public class TelegramTest {
                 .statusCode(201);
     }
 
+    @Test
+    void testWebhookEndpoint() {
+        //simulate POST messages from Telegram
+        final var message = "{\n" +
+                "   \"update_id\":123456789,\n" +
+                "   \"message\":{\n" +
+                "      \"message_id\":123,\n" +
+                "      \"from\":{\n" +
+                "         \"id\":123456789,\n" +
+                "         \"is_bot\":false,\n" +
+                "         \"first_name\":\"Apache\",\n" +
+                "\t\t \"last_name\":\"Camel\"\n" +
+                "      },\n" +
+                "      \"date\":1517384207,\n" +
+                "      \"chat\":{\n" +
+                "         \"id\":123456789,\n" +
+                "         \"type\":\"private\",\n" +
+                "         \"first_name\":\"Apache\",\n" +
+                "         \"all_members_are_administrators\":false\n" +
+                "      },\n" +
+                "      \"forward_from_message_id\":0,\n" +
+                "      \"text\":\"Hello World\",\n" +
+                "      \"delete_chat_photo\":false,\n" +
+                "      \"group_chat_created\":false,\n" +
+                "      \"supergroup_chat_created\":false,\n" +
+                "      \"channel_chat_created\":false,\n" +
+                "      \"migrate_to_chat_id\":0,\n" +
+                "      \"migrate_from_chat_id\":0\n" +
+                "   }\n" +
+                "}";
+        RestAssured.given()
+                .contentType(ContentType.JSON)
+                .body(message)
+                .post("/my-test")
+                .then()
+                .statusCode(204);
+
+        //make sure the telegram webhook message is consumed
+        await().atMost(5, TimeUnit.SECONDS).until(() -> {
+            return RestAssured.get("/telegram/webhook")
+                    .then()
+                    .statusCode(200)
+                    .extract()
+                    .body()
+                    .asString().equals("Hello World");
+        });
+
+    }
+
 }