Fix tests

This commit is contained in:
Abbas Abou Daya 2018-05-18 04:40:27 -04:00
parent 9347d064c1
commit 8729271d34
3 changed files with 96 additions and 73 deletions

View File

@ -4,70 +4,79 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.telegram.abilitybots.api.db.DBContext;
import org.telegram.abilitybots.api.objects.EndUser;
import org.telegram.abilitybots.api.objects.MessageContext;
import org.telegram.abilitybots.api.sender.MessageSender;
import org.telegram.abilitybots.api.sender.SilentSender;
import org.telegram.telegrambots.api.objects.User;
import java.io.IOException;
import static java.lang.Long.valueOf;
import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.mockito.Mockito.*;
import static org.mockito.internal.verification.VerificationModeFactory.times;
import static org.telegram.abilitybots.api.bot.AbilityBotTest.mockContext;
import static org.telegram.abilitybots.api.bot.AbilityBotTest.newUser;
import static org.telegram.abilitybots.api.db.MapDBContext.offlineInstance;
import static org.telegram.abilitybots.api.objects.EndUser.endUser;
public class AbilityBotI18nTest {
private static final EndUser NO_LANGUAGE_USER = endUser(1, "first", "last", "username");
private static final EndUser ITALIAN_USER = endUser(2, "first", "last", "username");
private static final User NO_LANGUAGE_USER = newUser(1, "first", "last", "username", null);
private static final User ITALIAN_USER = newUser(2, "first", "last", "username", "it-IT");
private DBContext db;
private DefaultBot bot;
private DBContext db;
private NoPublicCommandsBot bot;
private NoPublicCommandsBot noCommandsBot;
private MessageSender sender;
private SilentSender silent;
private MessageSender sender;
private SilentSender silent;
@Before
public void setUp() {
db = offlineInstance("db");
bot = new NoPublicCommandsBot(EMPTY, EMPTY, db);
@Before
public void setUp() {
db = offlineInstance("db");
bot = new DefaultBot(EMPTY, EMPTY, db);
sender = mock(MessageSender.class);
silent = mock(SilentSender.class);
silent = mock(SilentSender.class);
bot.sender = sender;
bot.silent = silent;
}
bot.sender = sender;
bot.silent = silent;
}
@Test
public void missingPublicCommandsLocalizedCorrectly1() {
MessageContext context = mockContext(NO_LANGUAGE_USER);
@Test
public void missingPublicCommandsLocalizedCorrectly() {
NoPublicCommandsBot noCommandsBot = new NoPublicCommandsBot(EMPTY, EMPTY, db);
noCommandsBot.silent = silent;
bot.reportCommands().action().accept(context);
MessageContext context = mock(MessageContext.class);
when(context.chatId()).thenReturn(Long.valueOf(NO_LANGUAGE_USER.id()));
when(context.user()).thenReturn(NO_LANGUAGE_USER);
verify(silent, times(1))
.send("No public commands found.", NO_LANGUAGE_USER.getId());
}
noCommandsBot.reportCommands().action().accept(context);
@Test
public void missingPublicCommandsLocalizedCorrectly2() {
MessageContext context1 = mockContext(ITALIAN_USER);
verify(silent, times(1))
.send("No public commands found.", NO_LANGUAGE_USER.id());
bot.reportCommands().action().accept(context1);
MessageContext context1 = mock(MessageContext.class);
when(context1.chatId()).thenReturn(Long.valueOf(ITALIAN_USER.id()));
when(context1.user()).thenReturn(ITALIAN_USER);
noCommandsBot.reportCommands().action().accept(context1);
verify(silent, times(1))
.send("Non sono presenti comandi pubblici.", ITALIAN_USER.id());
}
verify(silent, times(1))
.send("Non sono presenti comandi pubblici.", ITALIAN_USER.getId());
}
@After
public void tearDown() throws IOException {
db.clear();
db.close();
}
@After
public void tearDown() throws IOException {
db.clear();
db.close();
}
public static class NoPublicCommandsBot extends AbilityBot {
protected NoPublicCommandsBot(String botToken, String botUsername, DBContext db) {
super(botToken, botUsername, db);
}
@Override
public int creatorId() {
return 0;
}
}
}

View File

@ -39,6 +39,7 @@ import static org.mockito.internal.verification.VerificationModeFactory.times;
import static org.telegram.abilitybots.api.bot.DefaultBot.getDefaultBuilder;
import static org.telegram.abilitybots.api.db.MapDBContext.offlineInstance;
import static org.telegram.abilitybots.api.objects.EndUser.endUser;
import static org.telegram.abilitybots.api.objects.EndUser.fromUser;
import static org.telegram.abilitybots.api.objects.Flag.DOCUMENT;
import static org.telegram.abilitybots.api.objects.Flag.MESSAGE;
import static org.telegram.abilitybots.api.objects.Locality.ALL;
@ -56,7 +57,9 @@ public class AbilityBotTest {
private static final String TEST = "test";
private static final String[] TEXT = {TEST};
public static final EndUser MUSER = endUser(1, "first", "last", "username");
public static final User TG_USER = newUser(1, "first", "last", "username", null);
public static final EndUser CREATOR = endUser(1337, "creatorFirst", "creatorLast", "creatorUsername");
public static final User TG_CREATOR = newUser(1337, "creatorFirst", "creatorLast", "creatorUsername", null);
private DefaultBot bot;
private DBContext db;
@ -199,8 +202,7 @@ public class AbilityBotTest {
@NotNull
private MessageContext defaultContext() {
MessageContext context = mock(MessageContext.class);
when(context.user()).thenReturn(CREATOR);
MessageContext context = mockContext(TG_CREATOR, GROUP_ID);
when(context.firstArg()).thenReturn(MUSER.username());
return context;
}
@ -208,8 +210,7 @@ public class AbilityBotTest {
@Test
public void cannotBanCreator() {
addUsers(MUSER, CREATOR);
MessageContext context = mock(MessageContext.class);
when(context.user()).thenReturn(MUSER);
MessageContext context = mockContext(TG_USER, GROUP_ID);
when(context.firstArg()).thenReturn(CREATOR.username());
bot.banUser().action().accept(context);
@ -228,8 +229,7 @@ public class AbilityBotTest {
@Test
public void creatorCanClaimBot() {
MessageContext context = mock(MessageContext.class);
when(context.user()).thenReturn(CREATOR);
MessageContext context = mockContext(TG_CREATOR, GROUP_ID);
bot.claimCreator().action().accept(context);
@ -241,8 +241,7 @@ public class AbilityBotTest {
@Test
public void userGetsBannedIfClaimsBot() {
addUsers(MUSER);
MessageContext context = mock(MessageContext.class);
when(context.user()).thenReturn(MUSER);
MessageContext context = mockContext(TG_USER, GROUP_ID);
bot.claimCreator().action().accept(context);
@ -550,21 +549,38 @@ public class AbilityBotTest {
@Test
public void canReportCommands() {
Update update = mock(Update.class);
Message message = mock(Message.class);
when(update.hasMessage()).thenReturn(true);
when(update.getMessage()).thenReturn(message);
when(message.hasText()).thenReturn(true);
MessageContext context = mock(MessageContext.class);
when(context.chatId()).thenReturn(GROUP_ID);
when(context.user()).thenReturn(MUSER);
MessageContext context = mockContext(TG_USER, GROUP_ID);
bot.reportCommands().action().accept(context);
verify(silent, times(1)).send("default - dis iz default command", GROUP_ID);
}
@NotNull
public static MessageContext mockContext(User user) {
return mockContext(user, user.getId());
}
@NotNull
public static MessageContext mockContext(User user, long groupId) {
Update update = mock(Update.class);
Message message = mock(Message.class);
EndUser endUser = fromUser(user);
when(update.hasMessage()).thenReturn(true);
when(update.getMessage()).thenReturn(message);
when(message.getFrom()).thenReturn(user);
when(message.hasText()).thenReturn(true);
MessageContext context = mock(MessageContext.class);
when(context.update()).thenReturn(update);
when(context.chatId()).thenReturn(groupId);
when(context.user()).thenReturn(endUser);
return context;
}
@After
public void tearDown() throws IOException {
db.clear();
@ -655,4 +671,17 @@ public class AbilityBotTest {
writer.close();
return backupFile;
}
public static User newUser(Integer id, String firstname, String lastname, String username, String languageCode) {
User user = mock(User.class);
when(user.getBot()).thenReturn(false);
when(user.getFirstName()).thenReturn(firstname);
when(user.getId()).thenReturn(id);
when(user.getLastName()).thenReturn(lastname);
when(user.getUserName()).thenReturn(username);
when(user.getLanguageCode()).thenReturn(languageCode);
return user;
}
}

View File

@ -1,15 +0,0 @@
package org.telegram.abilitybots.api.bot;
import org.telegram.abilitybots.api.db.DBContext;
public class NoPublicCommandsBot extends AbilityBot {
protected NoPublicCommandsBot(String botToken, String botUsername, DBContext db) {
super(botToken, botUsername, db);
}
@Override
public int creatorId() {
return 0;
}
}