#269: Deleted TelegramBatchLongPollingBot, added onUpdatesReceived method to basic LongPollingBot interface

This commit is contained in:
vantuz-subhuman 2017-09-21 00:46:41 +03:00
parent 0fb556518a
commit cb92eca883
7 changed files with 46 additions and 97 deletions

View File

@ -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
*/

View File

@ -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);
}

View File

@ -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<Update> updates = getUpdateList();
if (updates.isEmpty()) {
synchronized (receivedUpdates) {
receivedUpdates.wait();
updates = getUpdateList();
if (updates.isEmpty()) {
continue;
}
List<Update> 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) {

View File

@ -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 {
}
}

View File

@ -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));
}
}

View File

@ -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);
}
}

View File

@ -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);