Merge pull request #666 from addo37/fix-execute-async

Fix executeAsync in SilentSender to properly call sender.executeAsync
This commit is contained in:
Ruben Bermudez 2019-10-08 23:07:25 +01:00 committed by GitHub
commit 51df90464e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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.Message;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ForceReplyKeyboard; import org.telegram.telegrambots.meta.api.objects.replykeyboard.ForceReplyKeyboard;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.updateshandlers.SentCallback;
import java.io.Serializable; import java.io.Serializable;
import java.util.Optional; 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 { try {
return Optional.ofNullable(sender.execute(method)); sender.executeAsync(method, callable);
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
log.error("Could not execute bot API method", 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.BeforeEach;
import org.junit.jupiter.api.Test; 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.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.updateshandlers.SentCallback;
import java.util.Optional; import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.*;
import static org.mockito.Mockito.when;
class SilentSenderTest { class SilentSenderTest {
private SilentSender silent; private SilentSender silent;
@ -40,4 +44,34 @@ class SilentSenderTest {
assertEquals(data, execute.get(), "Silent execution resulted in a different object"); 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) {
}
}
;
} }