From ed18c2c2650adbb6d35cfc6354be15de40e03649 Mon Sep 17 00:00:00 2001 From: chase Date: Tue, 26 Mar 2019 22:39:54 +0100 Subject: [PATCH] Add test for TelegramFileDownloader --- .../test/TelegramFileDownloaderTest.java | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramFileDownloaderTest.java diff --git a/telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramFileDownloaderTest.java b/telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramFileDownloaderTest.java new file mode 100644 index 00000000..d185f869 --- /dev/null +++ b/telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramFileDownloaderTest.java @@ -0,0 +1,112 @@ +package org.telegram.telegrambots.test; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.message.BasicStatusLine; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.telegram.telegrambots.facilities.filedownloader.TelegramFileDownloader; +import org.telegram.telegrambots.meta.exceptions.TelegramApiException; +import org.telegram.telegrambots.meta.updateshandlers.DownloadFileCallback; + +import java.io.File; +import java.io.IOException; +import java.util.function.Supplier; + +import static java.nio.charset.Charset.defaultCharset; +import static org.apache.commons.io.FileUtils.readFileToString; +import static org.apache.commons.io.IOUtils.toInputStream; +import static org.apache.http.HttpVersion.HTTP_1_1; +import static org.hamcrest.core.IsInstanceOf.instanceOf; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; + +@RunWith(MockitoJUnitRunner.class) +public class TelegramFileDownloaderTest { + + private TelegramFileDownloader telegramFileDownloader; + + @Mock + private DownloadFileCallback downloadFileCallbackMock; + + @Mock + private HttpClient httpClientMock; + + @Mock + private HttpResponse httpResponseMock; + + @Mock + private HttpEntity httpEntityMock; + + private Supplier tokenSupplierMock = () -> "someToken"; + + + @Before + public void setup() throws IOException { + + + when(httpResponseMock.getStatusLine()).thenReturn(new BasicStatusLine(HTTP_1_1, 200, "emptyString")); + when(httpResponseMock.getEntity()).thenReturn(httpEntityMock); + + when(httpEntityMock.getContent()).thenReturn(toInputStream("Some File Content", defaultCharset())); + + when(httpClientMock.execute(any(HttpUriRequest.class))).thenReturn(httpResponseMock); + + telegramFileDownloader = new TelegramFileDownloader(httpClientMock, tokenSupplierMock); + } + + @Test + public void testFileDownload() throws TelegramApiException, IOException { + File returnFile = telegramFileDownloader.downloadFile("someFilePath"); + String content = readFileToString(returnFile, defaultCharset()); + + assertEquals("Some File Content", content); + } + + @Test(expected = TelegramApiException.class) + public void testDownloadException() throws TelegramApiException { + when(httpResponseMock.getStatusLine()).thenReturn(new BasicStatusLine(HTTP_1_1, 500, "emptyString")); + + telegramFileDownloader.downloadFile("someFilePath"); + } + + @Test + public void testAsyncDownload() throws TelegramApiException, IOException { + final ArgumentCaptor fileArgumentCaptor = ArgumentCaptor.forClass(File.class); + + telegramFileDownloader.downloadFileAsync("someFilePath", downloadFileCallbackMock); + + verify(downloadFileCallbackMock, timeout(100) + .times(1)) + .onResult(any(), fileArgumentCaptor.capture()); + + String content = readFileToString(fileArgumentCaptor.getValue(), defaultCharset()); + assertEquals("Some File Content", content); + } + + @Test + public void testAsyncException() throws TelegramApiException { + final ArgumentCaptor exceptionArgumentCaptor = ArgumentCaptor.forClass(Exception.class); + + when(httpResponseMock.getStatusLine()).thenReturn(new BasicStatusLine(HTTP_1_1, 500, "emptyString")); + + telegramFileDownloader.downloadFileAsync("someFilePath", downloadFileCallbackMock); + + verify(downloadFileCallbackMock, timeout(100) + .times(1)) + .onException(any(), exceptionArgumentCaptor.capture()); + + Exception e = exceptionArgumentCaptor.getValue(); + assertThat(e, instanceOf(TelegramApiException.class)); + assertEquals(e.getCause().getCause().getMessage(), "Unexpected Status code while downloading file. Expected 200 got 500"); + } + +}