Handle the poll flag in the getChatId method

This commit is contained in:
Abbas Abou Daya 2020-05-09 14:02:36 -07:00
parent e4dbf27c55
commit 202fb72c3f
3 changed files with 53 additions and 1 deletions

View File

@ -156,6 +156,8 @@ public final class AbilityUtils {
return (long) update.getPreCheckoutQuery().getFrom().getId();
} else if (POLL_ANSWER.test(update)) {
return (long) update.getPollAnswer().getUser().getId();
} else if (POLL.test(update)) {
return (long) EMPTY_USER.getId();
} else {
throw new IllegalStateException("Could not retrieve originating chat ID from update");
}

View File

@ -26,6 +26,7 @@ import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Sets.newHashSet;
@ -154,6 +155,16 @@ public class AbilityBotTest {
});
}
@Test
void getChatIdCanHandleAllKindsOfUpdates() {
handlesAllUpdates(AbilityUtils::getUser);
}
@Test
void getUserCanHandleAllKindsOfUpdates() {
handlesAllUpdates(AbilityUtils::getChatId);
}
@Test
void canBackupDB() throws TelegramApiException {
MessageContext context = defaultContext();
@ -608,6 +619,29 @@ public class AbilityBotTest {
verify(silent, times(1)).send(expected, GROUP_ID);
}
private void handlesAllUpdates(Consumer<Update> utilMethod) {
Arrays.stream(Update.class.getMethods())
// filter to all these methods of hasXXX (hasPoll, hasMessage, etc...)
.filter(method -> method.getName().startsWith("has"))
// Gotta filter out hashCode
.filter(method -> method.getReturnType().getName().equals("boolean"))
.forEach(method -> {
Update update = mock(Update.class);
try {
// Mock the method and make sure it returns true so that it gets processed by the following method
when(method.invoke(update)).thenReturn(true);
// Call the function, throws an IllegalStateException if there's an update that can't be processed
utilMethod.accept(update);
} catch (IllegalStateException e) {
throw new RuntimeException(
format("Found an update variation that is not handled by the getChatId util method [%s]", method.getName()), e);
} catch (NullPointerException | ReflectiveOperationException e) {
// This is fine, the mock isn't complete and we're only
// looking for IllegalStateExceptions thrown by the method
}
});
}
private void mockUser(Update update, Message message, User user) {
when(update.hasMessage()).thenReturn(true);
when(update.getMessage()).thenReturn(message);

View File

@ -5,11 +5,13 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.telegram.abilitybots.api.db.DBContext;
import org.telegram.abilitybots.api.objects.Flag;
import org.telegram.abilitybots.api.objects.Reply;
import org.telegram.abilitybots.api.objects.ReplyFlow;
import org.telegram.abilitybots.api.sender.MessageSender;
import org.telegram.abilitybots.api.sender.SilentSender;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.polls.Poll;
import java.io.IOException;
import java.util.function.Predicate;
@ -106,6 +108,20 @@ public class ReplyFlowTest {
assertFalse(db.<Long, Integer>getMap(STATES).containsKey(chatId), "User still has state after terminal reply");
}
@Test
void repliesHandlePollResponse() {
Update update = mock(Update.class);
when(update.hasPoll()).thenReturn(true);
when(update.hasMessage()).thenReturn(false);
Poll poll = mock(Poll.class);
when(poll.getId()).thenReturn("1");
when(update.getPoll()).thenReturn(poll);
// This should not be processed as a reply, so we wouldn't filter out (true)
assertTrue(bot.filterReply(update));
}
public static class ReplyFlowBot extends AbilityBot {
private ReplyFlowBot(String botToken, String botUsername, DBContext db) {
@ -139,7 +155,7 @@ public class ReplyFlowTest {
@NotNull
private Predicate<Update> hasMessageWith(String msg) {
return upd -> upd.getMessage().getText().equalsIgnoreCase(msg);
return upd -> Flag.MESSAGE.test(upd) && upd.getMessage().getText().equalsIgnoreCase(msg);
}
}
}