#269: Deleted TelegramBatchLongPollingBot
, added onUpdatesReceived
method to basic LongPollingBot
interface
This commit is contained in:
parent
0fb556518a
commit
cb92eca883
@ -3,6 +3,8 @@ package org.telegram.telegrambots.generics;
|
||||
import org.telegram.telegrambots.api.objects.Update;
|
||||
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Ruben Bermudez
|
||||
* @version 1.0
|
||||
@ -16,6 +18,15 @@ public interface LongPollingBot {
|
||||
*/
|
||||
void onUpdateReceived(Update update);
|
||||
|
||||
/**
|
||||
* This method is called when receiving updates via GetUpdates method.
|
||||
* If not reimplemented - it just sends updates by one into {@link #onUpdateReceived(Update)}
|
||||
* @param updates list of Update received
|
||||
*/
|
||||
default void onUpdatesReceived(List<Update> updates) {
|
||||
updates.forEach(this::onUpdateReceived);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return bot username of this bot
|
||||
*/
|
||||
|
@ -1,16 +0,0 @@
|
||||
package org.telegram.telegrambots.bots;
|
||||
|
||||
import org.telegram.telegrambots.api.objects.Update;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class TelegramBatchLongPollingBot extends TelegramLongPollingBot {
|
||||
|
||||
@Override
|
||||
public final void onUpdateReceived(Update update) {
|
||||
onUpdatesReceived(Collections.singletonList(update));
|
||||
}
|
||||
|
||||
public abstract void onUpdatesReceived(List<Update> updates);
|
||||
}
|
@ -18,7 +18,6 @@ import org.telegram.telegrambots.ApiConstants;
|
||||
import org.telegram.telegrambots.api.methods.updates.GetUpdates;
|
||||
import org.telegram.telegrambots.api.objects.Update;
|
||||
import org.telegram.telegrambots.bots.DefaultBotOptions;
|
||||
import org.telegram.telegrambots.bots.TelegramBatchLongPollingBot;
|
||||
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
|
||||
import org.telegram.telegrambots.generics.*;
|
||||
import org.telegram.telegrambots.logging.BotLogger;
|
||||
@ -289,7 +288,6 @@ public class DefaultBotSession implements BotSession {
|
||||
setPriority(Thread.MIN_PRIORITY);
|
||||
while (running) {
|
||||
try {
|
||||
if (callback instanceof TelegramBatchLongPollingBot) {
|
||||
List<Update> updates = getUpdateList();
|
||||
if (updates.isEmpty()) {
|
||||
synchronized (receivedUpdates) {
|
||||
@ -300,20 +298,7 @@ public class DefaultBotSession implements BotSession {
|
||||
}
|
||||
}
|
||||
}
|
||||
((TelegramBatchLongPollingBot) callback).onUpdatesReceived(updates);
|
||||
} else {
|
||||
Update update = receivedUpdates.pollFirst();
|
||||
if (update == null) {
|
||||
synchronized (receivedUpdates) {
|
||||
receivedUpdates.wait();
|
||||
update = receivedUpdates.pollFirst();
|
||||
if (update == null) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
callback.onUpdateReceived(update);
|
||||
}
|
||||
callback.onUpdatesReceived(updates);
|
||||
} catch (InterruptedException e) {
|
||||
BotLogger.debug(LOGTAG, e);
|
||||
} catch (Exception e) {
|
||||
|
@ -1,34 +0,0 @@
|
||||
package org.telegram.telegrambots.test.Fakes;
|
||||
|
||||
import org.telegram.telegrambots.api.objects.Update;
|
||||
import org.telegram.telegrambots.bots.DefaultBotOptions;
|
||||
import org.telegram.telegrambots.bots.TelegramBatchLongPollingBot;
|
||||
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
|
||||
import org.telegram.telegrambots.generics.BotOptions;
|
||||
import org.telegram.telegrambots.generics.LongPollingBot;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class FakeBatchLongPollingBot extends TelegramBatchLongPollingBot
|
||||
{
|
||||
|
||||
@Override
|
||||
public void onUpdatesReceived(List<Update> updates) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBotUsername() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBotToken() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearWebhook() throws TelegramApiRequestException {
|
||||
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package org.telegram.telegrambots.test;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.telegram.telegrambots.api.objects.Update;
|
||||
import org.telegram.telegrambots.bots.TelegramBatchLongPollingBot;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
public class TelegramBatchLongPollingBotTest {
|
||||
|
||||
@Test
|
||||
public void testOnUpdateReceived() throws Exception {
|
||||
TelegramBatchLongPollingBot bot = Mockito.mock(TelegramBatchLongPollingBot.class);
|
||||
Update update = new Update();
|
||||
bot.onUpdateReceived(update);
|
||||
Mockito.verify(bot).onUpdatesReceived(Collections.singletonList(update));
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package org.telegram.telegrambots.test;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.telegram.telegrambots.api.objects.Update;
|
||||
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyList;
|
||||
|
||||
public class TelegramLongPollingBotTest {
|
||||
|
||||
@Test
|
||||
public void testOnUpdateReceived() throws Exception {
|
||||
TelegramLongPollingBot bot = Mockito.mock(TelegramLongPollingBot.class);
|
||||
Mockito.doCallRealMethod().when(bot).onUpdatesReceived(any());
|
||||
Update update1 = new Update();
|
||||
Update update2 = new Update();
|
||||
bot.onUpdatesReceived(asList(update1, update2));
|
||||
Mockito.verify(bot).onUpdateReceived(update1);
|
||||
Mockito.verify(bot).onUpdateReceived(update2);
|
||||
}
|
||||
}
|
@ -15,9 +15,7 @@ import org.mockito.Matchers;
|
||||
import org.mockito.Mockito;
|
||||
import org.telegram.telegrambots.api.objects.Update;
|
||||
import org.telegram.telegrambots.bots.DefaultBotOptions;
|
||||
import org.telegram.telegrambots.bots.TelegramBatchLongPollingBot;
|
||||
import org.telegram.telegrambots.generics.LongPollingBot;
|
||||
import org.telegram.telegrambots.test.Fakes.FakeBatchLongPollingBot;
|
||||
import org.telegram.telegrambots.test.Fakes.FakeLongPollingBot;
|
||||
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
|
||||
|
||||
@ -90,7 +88,7 @@ public class TestDefaultBotSession {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatesForStandardLongPollingBot() throws Exception {
|
||||
public void testUpdates() throws Exception {
|
||||
LongPollingBot bot = Mockito.spy(new FakeLongPollingBot());
|
||||
session = getDefaultBotSession(bot);
|
||||
AtomicInteger flag = new AtomicInteger();
|
||||
@ -118,8 +116,8 @@ public class TestDefaultBotSession {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatesForBatchLongPollingBot() throws Exception {
|
||||
TelegramBatchLongPollingBot bot = Mockito.spy(new FakeBatchLongPollingBot());
|
||||
public void testBatchUpdates() throws Exception {
|
||||
LongPollingBot bot = Mockito.spy(new FakeLongPollingBot());
|
||||
session = getDefaultBotSession(bot);
|
||||
AtomicInteger flag = new AtomicInteger();
|
||||
Update[] updates = createFakeUpdates(9);
|
||||
|
Loading…
Reference in New Issue
Block a user