#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.api.objects.Update;
import org.telegram.telegrambots.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
import java.util.List;
/** /**
* @author Ruben Bermudez * @author Ruben Bermudez
* @version 1.0 * @version 1.0
@ -16,6 +18,15 @@ public interface LongPollingBot {
*/ */
void onUpdateReceived(Update update); 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 * 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.methods.updates.GetUpdates;
import org.telegram.telegrambots.api.objects.Update; import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.DefaultBotOptions; import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.TelegramBatchLongPollingBot;
import org.telegram.telegrambots.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.generics.*; import org.telegram.telegrambots.generics.*;
import org.telegram.telegrambots.logging.BotLogger; import org.telegram.telegrambots.logging.BotLogger;
@ -289,31 +288,17 @@ public class DefaultBotSession implements BotSession {
setPriority(Thread.MIN_PRIORITY); setPriority(Thread.MIN_PRIORITY);
while (running) { while (running) {
try { try {
if (callback instanceof TelegramBatchLongPollingBot) { List<Update> updates = getUpdateList();
List<Update> updates = getUpdateList(); if (updates.isEmpty()) {
if (updates.isEmpty()) { synchronized (receivedUpdates) {
synchronized (receivedUpdates) { receivedUpdates.wait();
receivedUpdates.wait(); updates = getUpdateList();
updates = getUpdateList(); if (updates.isEmpty()) {
if (updates.isEmpty()) { continue;
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) { } catch (InterruptedException e) {
BotLogger.debug(LOGTAG, e); BotLogger.debug(LOGTAG, e);
} catch (Exception 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.mockito.Mockito;
import org.telegram.telegrambots.api.objects.Update; import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.DefaultBotOptions; import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.TelegramBatchLongPollingBot;
import org.telegram.telegrambots.generics.LongPollingBot; import org.telegram.telegrambots.generics.LongPollingBot;
import org.telegram.telegrambots.test.Fakes.FakeBatchLongPollingBot;
import org.telegram.telegrambots.test.Fakes.FakeLongPollingBot; import org.telegram.telegrambots.test.Fakes.FakeLongPollingBot;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession; import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
@ -90,7 +88,7 @@ public class TestDefaultBotSession {
} }
@Test @Test
public void testUpdatesForStandardLongPollingBot() throws Exception { public void testUpdates() throws Exception {
LongPollingBot bot = Mockito.spy(new FakeLongPollingBot()); LongPollingBot bot = Mockito.spy(new FakeLongPollingBot());
session = getDefaultBotSession(bot); session = getDefaultBotSession(bot);
AtomicInteger flag = new AtomicInteger(); AtomicInteger flag = new AtomicInteger();
@ -118,8 +116,8 @@ public class TestDefaultBotSession {
} }
@Test @Test
public void testUpdatesForBatchLongPollingBot() throws Exception { public void testBatchUpdates() throws Exception {
TelegramBatchLongPollingBot bot = Mockito.spy(new FakeBatchLongPollingBot()); LongPollingBot bot = Mockito.spy(new FakeLongPollingBot());
session = getDefaultBotSession(bot); session = getDefaultBotSession(bot);
AtomicInteger flag = new AtomicInteger(); AtomicInteger flag = new AtomicInteger();
Update[] updates = createFakeUpdates(9); Update[] updates = createFakeUpdates(9);