Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.*;

public class TestTelegramClientIntegration {
class TestTelegramClientIntegration {
private MockWebServer webServer;

private static final String TOKEN = "testToken";
Expand Down Expand Up @@ -171,6 +172,20 @@ void testDownloadFileAsStream() throws Exception {
}
}

@Test
void testDownloadFileAsStreamFuture() {
client.downloadFileAsStreamAsync("someFile").thenAccept(is -> {
String text = null;
try {
text = new String(is.readAllBytes(), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException(e);
}
assertNotNull(text);
assertFalse(text.isEmpty());
});
}

@Test
void testSendMessageException() {
SendMessage method = new SendMessage("someChatId", "someText");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.telegram.telegrambots.client;

import org.apache.commons.io.IOUtils;
import org.telegram.telegrambots.meta.api.methods.botapimethods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.groupadministration.SetChatPhoto;
import org.telegram.telegrambots.meta.api.methods.send.*;
Expand All @@ -11,6 +12,7 @@
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.TelegramClient;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.List;
Expand Down Expand Up @@ -203,7 +205,13 @@ public java.io.File downloadFile(File file) throws TelegramApiException {
@Override
public InputStream downloadFileAsStream(File file) throws TelegramApiException {
try {
return downloadFileAsStreamAsync(file).get();
return downloadFileAsStreamAsync(file).thenApply(is -> {
try {
return IOUtils.toBufferedInputStream(is);
} catch (IOException e) {
throw new RuntimeException(e);
}
}).get();
} catch (Exception e) {
throw mapException(e, " download file ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import okhttp3.Callback;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.apache.commons.io.IOUtils;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.CompletableFuture;
Expand All @@ -25,7 +23,7 @@ public void onResponse(@NonNull Call call, @NonNull Response response) {
if (body == null) {
completeExceptionally(new TelegramApiException("Telegram api returned empty response"));
} else {
complete(new ByteArrayInputStream(IOUtils.toByteArray(body.byteStream())));
complete(body.byteStream());
}
} catch (Exception e) {
completeExceptionally(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.*;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestTelegramClientIntegration {
class TestTelegramClientIntegration {
private MockWebServer webServer;

private static final String TOKEN = "testToken";
Expand Down Expand Up @@ -168,6 +170,29 @@ void testSendAnimation() throws TelegramApiException {
assertEquals(responseMessage, parsedMessage);
}

@Test
void testDownloadFileAsStream() throws Exception {
try (InputStream is = client.downloadFileAsStream("someFile")) {
String text = new String(is.readAllBytes(), StandardCharsets.UTF_8);
assertNotNull(text);
assertFalse(text.isEmpty());
}
}

@Test
void testDownloadFileAsStreamFuture() throws Exception {
client.downloadFileAsStreamAsync("someFile").thenAccept(is -> {
String text = null;
try {
text = new String(is.readAllBytes(), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException(e);
}
assertNotNull(text);
assertFalse(text.isEmpty());
}).get();
}

@Test
void testSendMessageException() {
SendMessage method = new SendMessage("someChatId", "someText");
Expand Down