camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nferr...@apache.org
Subject [6/8] camel git commit: added unit tests for outgoing and incoming documents
Date Fri, 27 Jan 2017 12:56:38 GMT
added unit tests for outgoing and incoming documents


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/fc745de0
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/fc745de0
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/fc745de0

Branch: refs/heads/master
Commit: fc745de0c5a1b9423423fe6eb078ace9b76ee53c
Parents: 17a5a4c
Author: Neffez <mail@steffen-eitelmann.com>
Authored: Fri Jan 20 13:53:06 2017 +0100
Committer: Nicola Ferraro <ni.ferraro@gmail.com>
Committed: Fri Jan 27 13:56:19 2017 +0100

----------------------------------------------------------------------
 .../TelegramConsumerMediaDocumentTest.java      | 87 ++++++++++++++++++++
 .../telegram/TelegramProducerMediaTest.java     | 23 ++++++
 .../telegram/util/TelegramTestUtil.java         |  4 +
 .../messages/updates-media-document.json        | 35 ++++++++
 4 files changed, 149 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/fc745de0/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramConsumerMediaDocumentTest.java
----------------------------------------------------------------------
diff --git a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramConsumerMediaDocumentTest.java
b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramConsumerMediaDocumentTest.java
new file mode 100644
index 0000000..268e3ec
--- /dev/null
+++ b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramConsumerMediaDocumentTest.java
@@ -0,0 +1,87 @@
+/**
+ * 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.telegram;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.telegram.model.IncomingDocument;
+import org.apache.camel.component.telegram.model.IncomingMessage;
+import org.apache.camel.component.telegram.model.UpdateResult;
+import org.apache.camel.component.telegram.util.TelegramTestSupport;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.when;
+
+/**
+ * Tests the reception of messages without text having media content.
+ */
+public class TelegramConsumerMediaDocumentTest extends TelegramTestSupport {
+
+    @EndpointInject(uri = "mock:telegram")
+    private MockEndpoint endpoint;
+
+    @Before
+    public void mockAPIs() {
+        TelegramService api = mockTelegramService();
+
+        UpdateResult res = getJSONResource("messages/updates-media-document.json", UpdateResult.class);
+
+        UpdateResult defaultRes = getJSONResource("messages/updates-empty.json", UpdateResult.class);
+
+        when(api.getUpdates(any(), any(), any(), any()))
+                .thenReturn(res)
+                .thenAnswer((i) -> defaultRes);
+    }
+
+    @Test
+    public void testReceptionOfMessageWithADocument() throws Exception {
+        endpoint.expectedMinimumMessageCount(1);
+        endpoint.assertIsSatisfied();
+
+        Exchange mediaExchange = endpoint.getExchanges().get(0);
+        IncomingMessage msg = mediaExchange.getIn().getBody(IncomingMessage.class);
+
+        IncomingDocument document = msg.getDocument();
+
+        assertNotNull(document);
+        assertEquals("AgADBAADq6cxG0bQcwnUb4Cga-eXxnodQxkABLXiiSI1vzZK8XXXXXXX", document.getFileId());
+        assertEquals(Long.valueOf(12530), document.getFileSize());
+        assertEquals("file.png", document.getFileName());
+        assertEquals("image/png", document.getMimeType());
+        assertNotNull(document.getThumb());
+        assertEquals(Integer.valueOf(90), document.getThumb().getWidth());
+        assertEquals(Integer.valueOf(80), document.getThumb().getHeight());
+        assertEquals(Long.valueOf(1253), document.getThumb().getFileSize());
+        assertEquals("AgADBAADq6cxG0bQcwnUb4Cga-eXxnodQxkABLXiiSI1vzZK9XXXXXXX", document.getThumb().getFileId());
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("telegram:bots/mock-token")
+                        .to("mock:telegram");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/fc745de0/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramProducerMediaTest.java
----------------------------------------------------------------------
diff --git a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramProducerMediaTest.java
b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramProducerMediaTest.java
index 8831ea4..f4a6c02 100644
--- a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramProducerMediaTest.java
+++ b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramProducerMediaTest.java
@@ -22,6 +22,7 @@ import org.apache.camel.Exchange;
 import org.apache.camel.RoutesBuilder;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.telegram.model.OutgoingAudioMessage;
+import org.apache.camel.component.telegram.model.OutgoingDocumentMessage;
 import org.apache.camel.component.telegram.model.OutgoingPhotoMessage;
 import org.apache.camel.component.telegram.model.OutgoingVideoMessage;
 import org.apache.camel.component.telegram.util.TelegramTestSupport;
@@ -128,6 +129,28 @@ public class TelegramProducerMediaTest extends TelegramTestSupport {
         assertEquals("Video", captor.getValue().getCaption());
     }
 
+    @Test
+    public void testRouteWithDocument() throws Exception {
+
+        TelegramService service = mockTelegramService();
+
+        Exchange ex = endpoint.createExchange();
+        ex.getIn().setHeader(TelegramConstants.TELEGRAM_MEDIA_TITLE_CAPTION, "Document");
+        ex.getIn().setHeader(TelegramConstants.TELEGRAM_MEDIA_TYPE, TelegramMediaType.DOCUMENT.name());
+        byte[] document = TelegramTestUtil.createSampleDocument();
+        ex.getIn().setBody(document);
+
+        context().createProducerTemplate().send(endpoint, ex);
+
+        ArgumentCaptor<OutgoingDocumentMessage> captor = ArgumentCaptor.forClass(OutgoingDocumentMessage.class);
+
+        Mockito.verify(service).sendMessage(eq("mock-token"), captor.capture());
+        assertEquals("my-id", captor.getValue().getChatId());
+        assertEquals(document, captor.getValue().getDocument());
+        assertEquals("file", captor.getValue().getFilenameWithExtension());
+        assertEquals("Document", captor.getValue().getCaption());
+    }
+
     @Override
     protected RoutesBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {

http://git-wip-us.apache.org/repos/asf/camel/blob/fc745de0/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/util/TelegramTestUtil.java
----------------------------------------------------------------------
diff --git a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/util/TelegramTestUtil.java
b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/util/TelegramTestUtil.java
index 1457314..1479c9c 100644
--- a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/util/TelegramTestUtil.java
+++ b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/util/TelegramTestUtil.java
@@ -58,4 +58,8 @@ public final class TelegramTestUtil {
         return video;
     }
 
+    public static byte[] createSampleDocument() throws IOException {
+        byte[] document = IOUtils.readBytesFromStream(TelegramTestUtil.class.getResourceAsStream("/attachments/sample.png"));
+        return document;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/fc745de0/components/camel-telegram/src/test/resources/messages/updates-media-document.json
----------------------------------------------------------------------
diff --git a/components/camel-telegram/src/test/resources/messages/updates-media-document.json
b/components/camel-telegram/src/test/resources/messages/updates-media-document.json
new file mode 100644
index 0000000..31891b6
--- /dev/null
+++ b/components/camel-telegram/src/test/resources/messages/updates-media-document.json
@@ -0,0 +1,35 @@
+{
+  "ok": true,
+  "result": [
+    {
+      "update_id": 525704899,
+      "message": {
+        "message_id": 180,
+        "from": {
+          "id": 1585844777,
+          "first_name": "John",
+          "last_name": "Doe"
+        },
+        "chat": {
+          "id": 158584478,
+          "first_name": "John",
+          "last_name": "Doe",
+          "type": "private"
+        },
+        "date": 1463479642,
+        "document": {
+          "file_id": "AgADBAADq6cxG0bQcwnUb4Cga-eXxnodQxkABLXiiSI1vzZK8XXXXXXX",
+          "file_size": 12530,
+          "file_name": "file.png",
+          "mime_type": "image/png",
+          "thumb": {
+            "file_id": "AgADBAADq6cxG0bQcwnUb4Cga-eXxnodQxkABLXiiSI1vzZK9XXXXXXX",
+            "width": 90,
+            "height": 80,
+            "file_size": 1253
+          }
+        }
+      }
+    }
+  ]
+}
\ No newline at end of file


Mime
View raw message