Fix executeAsync in SilentSender to properly call sender.executeAsync

This commit is contained in:
Abbas Abou Daya 2019-09-16 23:40:20 -07:00
parent 6f0247232f
commit ed7333a21e
2 changed files with 40 additions and 5 deletions

View File

@ -7,6 +7,7 @@ import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ForceReplyKeyboard;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.updateshandlers.SentCallback;
import java.io.Serializable;
import java.util.Optional;
@ -52,12 +53,12 @@ public class SilentSender {
}
}
public <T extends Serializable, Method extends BotApiMethod<T>> Optional<T> executeAsync(Method method) {
public <T extends Serializable, Method extends BotApiMethod<T>, Callback extends SentCallback<T>> void
executeAsync(Method method, Callback callable) {
try {
return Optional.ofNullable(sender.execute(method));
sender.executeAsync(method, callable);
} catch (TelegramApiException e) {
log.error("Could not execute bot API method", e);
return Optional.empty();
}
}

View File

@ -2,15 +2,19 @@ package org.telegram.abilitybots.api.sender;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.updateshandlers.SentCallback;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;
class SilentSenderTest {
private SilentSender silent;
@ -40,4 +44,34 @@ class SilentSenderTest {
assertEquals(data, execute.get(), "Silent execution resulted in a different object");
}
@Test
void callsAsyncVariantOfExecute() throws TelegramApiException {
SendMessage methodObject = new SendMessage();
NoOpCallback callback = new NoOpCallback();
silent.executeAsync(methodObject, callback);
verify(sender, only()).executeAsync(methodObject, callback);
}
private class NoOpCallback implements SentCallback<Message> {
@Override
public void onResult(BotApiMethod<Message> method, Message response) {
}
@Override
public void onError(BotApiMethod<Message> method, TelegramApiRequestException apiException) {
}
@Override
public void onException(BotApiMethod<Message> method, Exception exception) {
}
}
;
}