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.Before;
import org.junit.Test; import org.junit.Test;
import org.telegram.abilitybots.api.db.DBContext; 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.objects.MessageContext;
import org.telegram.abilitybots.api.sender.MessageSender; import org.telegram.abilitybots.api.sender.MessageSender;
import org.telegram.abilitybots.api.sender.SilentSender; import org.telegram.abilitybots.api.sender.SilentSender;
import org.telegram.telegrambots.api.objects.User;
import java.io.IOException; import java.io.IOException;
import static java.lang.Long.valueOf;
import static org.apache.commons.lang3.StringUtils.EMPTY; import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import static org.mockito.internal.verification.VerificationModeFactory.times; 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.db.MapDBContext.offlineInstance;
import static org.telegram.abilitybots.api.objects.EndUser.endUser;
public class AbilityBotI18nTest { public class AbilityBotI18nTest {
private static final EndUser NO_LANGUAGE_USER = endUser(1, "first", "last", "username"); private static final User NO_LANGUAGE_USER = newUser(1, "first", "last", "username", null);
private static final EndUser ITALIAN_USER = endUser(2, "first", "last", "username"); private static final User ITALIAN_USER = newUser(2, "first", "last", "username", "it-IT");
private DBContext db; private DBContext db;
private DefaultBot bot; private NoPublicCommandsBot bot;
private NoPublicCommandsBot noCommandsBot; private MessageSender sender;
private SilentSender silent;
private MessageSender sender; @Before
private SilentSender silent; public void setUp() {
db = offlineInstance("db");
bot = new NoPublicCommandsBot(EMPTY, EMPTY, db);
@Before sender = mock(MessageSender.class);
public void setUp() { silent = mock(SilentSender.class);
db = offlineInstance("db");
bot = new DefaultBot(EMPTY, EMPTY, db);
silent = mock(SilentSender.class); bot.sender = sender;
bot.silent = silent;
}
bot.sender = sender; @Test
bot.silent = silent; public void missingPublicCommandsLocalizedCorrectly1() {
} MessageContext context = mockContext(NO_LANGUAGE_USER);
@Test bot.reportCommands().action().accept(context);
public void missingPublicCommandsLocalizedCorrectly() {
NoPublicCommandsBot noCommandsBot = new NoPublicCommandsBot(EMPTY, EMPTY, db);
noCommandsBot.silent = silent;
MessageContext context = mock(MessageContext.class); verify(silent, times(1))
when(context.chatId()).thenReturn(Long.valueOf(NO_LANGUAGE_USER.id())); .send("No public commands found.", NO_LANGUAGE_USER.getId());
when(context.user()).thenReturn(NO_LANGUAGE_USER); }
noCommandsBot.reportCommands().action().accept(context); @Test
public void missingPublicCommandsLocalizedCorrectly2() {
MessageContext context1 = mockContext(ITALIAN_USER);
verify(silent, times(1)) bot.reportCommands().action().accept(context1);
.send("No public commands found.", NO_LANGUAGE_USER.id());
MessageContext context1 = mock(MessageContext.class); verify(silent, times(1))
when(context1.chatId()).thenReturn(Long.valueOf(ITALIAN_USER.id())); .send("Non sono presenti comandi pubblici.", ITALIAN_USER.getId());
when(context1.user()).thenReturn(ITALIAN_USER); }
noCommandsBot.reportCommands().action().accept(context1);
verify(silent, times(1))
.send("Non sono presenti comandi pubblici.", ITALIAN_USER.id());
}
@After @After
public void tearDown() throws IOException { public void tearDown() throws IOException {
db.clear(); db.clear();
db.close(); 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.bot.DefaultBot.getDefaultBuilder;
import static org.telegram.abilitybots.api.db.MapDBContext.offlineInstance; 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.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.DOCUMENT;
import static org.telegram.abilitybots.api.objects.Flag.MESSAGE; import static org.telegram.abilitybots.api.objects.Flag.MESSAGE;
import static org.telegram.abilitybots.api.objects.Locality.ALL; 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 TEST = "test";
private static final String[] TEXT = {TEST}; private static final String[] TEXT = {TEST};
public static final EndUser MUSER = endUser(1, "first", "last", "username"); 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 EndUser CREATOR = endUser(1337, "creatorFirst", "creatorLast", "creatorUsername");
public static final User TG_CREATOR = newUser(1337, "creatorFirst", "creatorLast", "creatorUsername", null);
private DefaultBot bot; private DefaultBot bot;
private DBContext db; private DBContext db;
@ -199,8 +202,7 @@ public class AbilityBotTest {
@NotNull @NotNull
private MessageContext defaultContext() { private MessageContext defaultContext() {
MessageContext context = mock(MessageContext.class); MessageContext context = mockContext(TG_CREATOR, GROUP_ID);
when(context.user()).thenReturn(CREATOR);
when(context.firstArg()).thenReturn(MUSER.username()); when(context.firstArg()).thenReturn(MUSER.username());
return context; return context;
} }
@ -208,8 +210,7 @@ public class AbilityBotTest {
@Test @Test
public void cannotBanCreator() { public void cannotBanCreator() {
addUsers(MUSER, CREATOR); addUsers(MUSER, CREATOR);
MessageContext context = mock(MessageContext.class); MessageContext context = mockContext(TG_USER, GROUP_ID);
when(context.user()).thenReturn(MUSER);
when(context.firstArg()).thenReturn(CREATOR.username()); when(context.firstArg()).thenReturn(CREATOR.username());
bot.banUser().action().accept(context); bot.banUser().action().accept(context);
@ -228,8 +229,7 @@ public class AbilityBotTest {
@Test @Test
public void creatorCanClaimBot() { public void creatorCanClaimBot() {
MessageContext context = mock(MessageContext.class); MessageContext context = mockContext(TG_CREATOR, GROUP_ID);
when(context.user()).thenReturn(CREATOR);
bot.claimCreator().action().accept(context); bot.claimCreator().action().accept(context);
@ -241,8 +241,7 @@ public class AbilityBotTest {
@Test @Test
public void userGetsBannedIfClaimsBot() { public void userGetsBannedIfClaimsBot() {
addUsers(MUSER); addUsers(MUSER);
MessageContext context = mock(MessageContext.class); MessageContext context = mockContext(TG_USER, GROUP_ID);
when(context.user()).thenReturn(MUSER);
bot.claimCreator().action().accept(context); bot.claimCreator().action().accept(context);
@ -550,21 +549,38 @@ public class AbilityBotTest {
@Test @Test
public void canReportCommands() { public void canReportCommands() {
Update update = mock(Update.class); MessageContext context = mockContext(TG_USER, GROUP_ID);
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);
bot.reportCommands().action().accept(context); bot.reportCommands().action().accept(context);
verify(silent, times(1)).send("default - dis iz default command", GROUP_ID); 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 @After
public void tearDown() throws IOException { public void tearDown() throws IOException {
db.clear(); db.clear();
@ -655,4 +671,17 @@ public class AbilityBotTest {
writer.close(); writer.close();
return backupFile; 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;
}
}