diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/generics/LongPollingBot.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/generics/LongPollingBot.java index ce9bda79..32118616 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/generics/LongPollingBot.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/generics/LongPollingBot.java @@ -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 updates) { + updates.forEach(this::onUpdateReceived); + } + /** * Return bot username of this bot */ diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramBatchLongPollingBot.java b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramBatchLongPollingBot.java deleted file mode 100644 index 4b294868..00000000 --- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramBatchLongPollingBot.java +++ /dev/null @@ -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 updates); -} diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java index d968329c..23f5b618 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java @@ -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,31 +288,17 @@ public class DefaultBotSession implements BotSession { setPriority(Thread.MIN_PRIORITY); while (running) { try { - if (callback instanceof TelegramBatchLongPollingBot) { - List updates = getUpdateList(); - if (updates.isEmpty()) { - synchronized (receivedUpdates) { - receivedUpdates.wait(); - updates = getUpdateList(); - if (updates.isEmpty()) { - continue; - } + List updates = getUpdateList(); + if (updates.isEmpty()) { + synchronized (receivedUpdates) { + receivedUpdates.wait(); + updates = getUpdateList(); + if (updates.isEmpty()) { + continue; } } - ((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) { diff --git a/telegrambots/src/test/java/org/telegram/telegrambots/test/Fakes/FakeBatchLongPollingBot.java b/telegrambots/src/test/java/org/telegram/telegrambots/test/Fakes/FakeBatchLongPollingBot.java deleted file mode 100644 index 46f89377..00000000 --- a/telegrambots/src/test/java/org/telegram/telegrambots/test/Fakes/FakeBatchLongPollingBot.java +++ /dev/null @@ -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 updates) { - - } - - @Override - public String getBotUsername() { - return ""; - } - - @Override - public String getBotToken() { - return ""; - } - - @Override - public void clearWebhook() throws TelegramApiRequestException { - - } -} diff --git a/telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramBatchLongPollingBotTest.java b/telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramBatchLongPollingBotTest.java deleted file mode 100644 index 54a92057..00000000 --- a/telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramBatchLongPollingBotTest.java +++ /dev/null @@ -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)); - } -} \ No newline at end of file diff --git a/telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramLongPollingBotTest.java b/telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramLongPollingBotTest.java new file mode 100644 index 00000000..23dbfc6d --- /dev/null +++ b/telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramLongPollingBotTest.java @@ -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); + } +} \ No newline at end of file diff --git a/telegrambots/src/test/java/org/telegram/telegrambots/test/TestDefaultBotSession.java b/telegrambots/src/test/java/org/telegram/telegrambots/test/TestDefaultBotSession.java index 39c6a83f..17d4aa65 100644 --- a/telegrambots/src/test/java/org/telegram/telegrambots/test/TestDefaultBotSession.java +++ b/telegrambots/src/test/java/org/telegram/telegrambots/test/TestDefaultBotSession.java @@ -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);