From bee960d823de5c6b4352256abb7d618ffa4f5287 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 17 Oct 2019 20:43:01 -0700 Subject: [PATCH] Fixes invites. https://github.com/BentoBoxWorld/BentoBox/issues/988 Adds test classes for IslandTeam command and redoes IslandTeamInviteCommand test class. --- .../island/team/IslandTeamCommand.java | 34 +- .../island/team/IslandTeamInviteCommand.java | 24 +- .../island/team/IslandTeamCommandTest.java | 237 +++++++++++++ .../team/IslandTeamInviteCommandTest.java | 333 +++++++++++------- 4 files changed, 477 insertions(+), 151 deletions(-) create mode 100644 src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCommandTest.java diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCommand.java index ccfa57560..2ab379cfb 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCommand.java @@ -22,13 +22,13 @@ public class IslandTeamCommand extends CompositeCommand { * Invited list. Key is the invited party, value is the invite. * @since 1.8.0 */ - private Map inviteList; + private Map inviteMap; private IslandTeamInviteCommand inviteCommand; public IslandTeamCommand(CompositeCommand parent) { super(parent, "team"); - inviteList = new HashMap<>(); + inviteMap = new HashMap<>(); } @Override @@ -87,29 +87,29 @@ public class IslandTeamCommand extends CompositeCommand { .reason(TeamEvent.Reason.INFO) .involvedPlayer(user.getUniqueId()) .build(); - Bukkit.getServer().getPluginManager().callEvent(event); + Bukkit.getPluginManager().callEvent(event); return event.isCancelled(); } /** - * - * @param type - * @param inviter - * @param invitee + * Add an invite + * @param type - type of invite + * @param inviter - uuid of inviter + * @param invitee - uuid of invitee * @since 1.8.0 */ public void addInvite(Invite.Type type, @NonNull UUID inviter, @NonNull UUID invitee) { - inviteList.put(invitee, new Invite(type, inviter, invitee)); + inviteMap.put(invitee, new Invite(type, inviter, invitee)); } /** - * - * @param invitee - * @return + * Check if a player has been invited + * @param invitee - UUID of invitee to check + * @return true if invited, false if not * @since 1.8.0 */ public boolean isInvited(@NonNull UUID invitee) { - return inviteList.containsKey(invitee); + return inviteMap.containsKey(invitee); } /** @@ -120,7 +120,7 @@ public class IslandTeamCommand extends CompositeCommand { */ @Nullable public UUID getInviter(UUID invitee) { - return isInvited(invitee) ? inviteList.get(invitee).getInviter() : null; + return isInvited(invitee) ? inviteMap.get(invitee).getInviter() : null; } /** @@ -131,15 +131,15 @@ public class IslandTeamCommand extends CompositeCommand { */ @Nullable public Invite getInvite(UUID invitee) { - return inviteList.get(invitee); + return inviteMap.get(invitee); } /** * Removes a pending invite. - * @param uniqueId - UUID of invited user + * @param invitee - UUID of invited user * @since 1.8.0 */ - public void removeInvite(@NonNull UUID uniqueId) { - inviteList.remove(uniqueId); + public void removeInvite(@NonNull UUID invitee) { + inviteMap.remove(invitee); } } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommand.java index 0feeff109..1011475ee 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommand.java @@ -7,6 +7,7 @@ import java.util.Set; import java.util.UUID; import org.bukkit.Bukkit; +import org.eclipse.jdt.annotation.Nullable; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.island.team.Invite.Type; @@ -20,6 +21,7 @@ import world.bentobox.bentobox.util.Util; public class IslandTeamInviteCommand extends CompositeCommand { private IslandTeamCommand itc; + private @Nullable User invitedPlayer; public IslandTeamInviteCommand(IslandTeamCommand parent) { super(parent, "invite"); @@ -71,12 +73,6 @@ public class IslandTeamInviteCommand extends CompositeCommand { showHelp(this, user); return false; } - return true; - } - - @Override - public boolean execute(User user, String label, List args) { - UUID playerUUID = user.getUniqueId(); // Only online players can be invited UUID invitedPlayerUUID = getPlayers().getUUID(args.get(0)); @@ -84,7 +80,7 @@ public class IslandTeamInviteCommand extends CompositeCommand { user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0)); return false; } - User invitedPlayer = User.getInstance(invitedPlayerUUID); + invitedPlayer = User.getInstance(invitedPlayerUUID); if (!invitedPlayer.isOnline()) { user.sendMessage("general.errors.offline-player"); return false; @@ -108,19 +104,19 @@ public class IslandTeamInviteCommand extends CompositeCommand { user.sendMessage("commands.island.team.invite.errors.you-have-already-invited"); return false; } - return invite(user,invitedPlayer); - + return true; } - private boolean invite(User user, User invitedPlayer) { + @Override + public boolean execute(User user, String label, List args) { Set teamMembers = getMembers(getWorld(), user); // Check if player has space on their team int maxSize = getMaxTeamSize(user); if (teamMembers.size() < maxSize) { // If that player already has an invite out then retract it. // Players can only have one invite one at a time - interesting - if (itc.isInvited(user.getUniqueId())) { - itc.removeInvite(user.getUniqueId()); + if (itc.isInvited(invitedPlayer.getUniqueId())) { + itc.removeInvite(invitedPlayer.getUniqueId()); user.sendMessage("commands.island.team.invite.removing-invite"); } // Fire event so add-ons can run commands, etc. @@ -129,13 +125,13 @@ public class IslandTeamInviteCommand extends CompositeCommand { .reason(TeamEvent.Reason.INVITE) .involvedPlayer(invitedPlayer.getUniqueId()) .build(); - Bukkit.getServer().getPluginManager().callEvent(event); + Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) { return false; } // Put the invited player (key) onto the list with inviter (value) // If someone else has invited a player, then this invite will overwrite the previous invite! - itc.addInvite(Invite.Type.TEAM, invitedPlayer.getUniqueId(), user.getUniqueId()); + itc.addInvite(Invite.Type.TEAM, user.getUniqueId(), invitedPlayer.getUniqueId()); user.sendMessage("commands.island.team.invite.invitation-sent", TextVariables.NAME, invitedPlayer.getName()); // Send message to online player invitedPlayer.sendMessage("commands.island.team.invite.name-has-invited-you", TextVariables.NAME, user.getName()); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCommandTest.java new file mode 100644 index 000000000..0d2bdda7e --- /dev/null +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCommandTest.java @@ -0,0 +1,237 @@ +package world.bentobox.bentobox.api.commands.island.team; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Collections; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.plugin.PluginManager; +import org.eclipse.jdt.annotation.Nullable; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; + +import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.api.commands.CompositeCommand; +import world.bentobox.bentobox.api.commands.island.team.Invite.Type; +import world.bentobox.bentobox.api.localization.TextVariables; +import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.database.objects.Island; +import world.bentobox.bentobox.managers.CommandsManager; +import world.bentobox.bentobox.managers.IslandWorldManager; +import world.bentobox.bentobox.managers.IslandsManager; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) +public class IslandTeamCommandTest { + + @Mock + private CompositeCommand ic; + + + private IslandTeamCommand tc; + + private UUID uuid; + + private UUID invitee; + + @Mock + private IslandsManager im; + + @Mock + private User user; + + @Mock + private World world; + + @Mock + private PluginManager pim; + + @Mock + private IslandWorldManager iwm; + + @Mock + private @Nullable Island island; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + BentoBox plugin = mock(BentoBox.class); + Whitebox.setInternalState(BentoBox.class, "instance", plugin); + + // Command manager + CommandsManager cm = mock(CommandsManager.class); + when(plugin.getCommandsManager()).thenReturn(cm); + + // Parent command + when(ic.getPermissionPrefix()).thenReturn("bskyblock."); + when(ic.getWorld()).thenReturn(world); + + // user + uuid = UUID.randomUUID(); + invitee = UUID.randomUUID(); + when(user.getUniqueId()).thenReturn(uuid); + when(user.getPermissionValue(eq("bskyblock.team.maxsize"), anyInt())).thenReturn(3); + + // island Manager + when(plugin.getIslands()).thenReturn(im); + // is owner of island + when(im.getOwner(any(), any())).thenReturn(uuid); + // No team members + when(im.getMembers(any(), any(UUID.class))).thenReturn(Collections.emptySet()); + // island + when(im.getIsland(any(), eq(uuid))).thenReturn(island); + + // Bukkit + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.getPluginManager()).thenReturn(pim); + + // IWM + when(plugin.getIWM()).thenReturn(iwm); + when(iwm.getPermissionPrefix(any())).thenReturn("bskyblock."); + + // Command under test + tc = new IslandTeamCommand(ic); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCommand#IslandTeamCommand(world.bentobox.bentobox.api.commands.CompositeCommand)}. + */ + @Test + public void testIslandTeamCommand() { + assertEquals("team", tc.getLabel()); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCommand#setup()}. + */ + @Test + public void testSetup() { + assertEquals("bskyblock.island.team", tc.getPermission()); + assertTrue(tc.isOnlyPlayer()); + assertEquals("commands.island.team.description", tc.getDescription()); + + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteUserStringListOfStringNoIsland() { + when(im.getOwner(any(), any())).thenReturn(null); + assertFalse(tc.execute(user, "team", Collections.emptyList())); + verify(user).sendMessage(eq("general.errors.no-island")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteUserStringListOfStringIslandIsNotFull() { + assertTrue(tc.execute(user, "team", Collections.emptyList())); + verify(user).sendMessage(eq("commands.island.team.invite.you-can-invite"), eq(TextVariables.NUMBER), eq("3")); + verify(island).showMembers(eq(user)); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteUserStringListOfStringIslandIsFull() { + when(user.getPermissionValue(eq("bskyblock.team.maxsize"), anyInt())).thenReturn(0); + assertTrue(tc.execute(user, "team", Collections.emptyList())); + verify(user).sendMessage(eq("commands.island.team.invite.errors.island-is-full")); + verify(island).showMembers(eq(user)); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCommand#addInvite(world.bentobox.bentobox.api.commands.island.team.Invite.Type, java.util.UUID, java.util.UUID)}. + */ + @Test + public void testAddInvite() { + tc.addInvite(Invite.Type.TEAM, uuid, invitee); + assertTrue(tc.isInvited(invitee)); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCommand#isInvited(java.util.UUID)}. + */ + @Test + public void testIsInvited() { + assertFalse(tc.isInvited(invitee)); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCommand#getInviter(java.util.UUID)}. + */ + @Test + public void testGetInviter() { + tc.addInvite(Invite.Type.TEAM, uuid, invitee); + assertEquals(uuid, tc.getInviter(invitee)); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCommand#getInviter(java.util.UUID)}. + */ + @Test + public void testGetInviterNoInvite() { + assertNull(tc.getInviter(invitee)); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCommand#getInvite(java.util.UUID)}. + */ + @Test + public void testGetInvite() { + assertNull(tc.getInvite(invitee)); + tc.addInvite(Invite.Type.TEAM, uuid, invitee); + @Nullable + Invite invite = tc.getInvite(invitee); + assertEquals(invite.getInvitee(), invitee); + assertEquals(invite.getType(), Type.TEAM); + assertEquals(invite.getInviter(), uuid); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCommand#removeInvite(java.util.UUID)}. + */ + @Test + public void testRemoveInvite() { + assertNull(tc.getInvite(invitee)); + tc.addInvite(Invite.Type.TEAM, uuid, invitee); + tc.removeInvite(invitee); + assertNull(tc.getInvite(invitee)); + } + +} diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java index d53fe74de..6f38c72be 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java @@ -1,23 +1,28 @@ package world.bentobox.bentobox.api.commands.island.team; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.entity.Player; +import org.bukkit.plugin.PluginManager; import org.bukkit.scheduler.BukkitScheduler; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -25,6 +30,9 @@ import org.powermock.reflect.Whitebox; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; +import world.bentobox.bentobox.api.commands.island.team.Invite.Type; +import world.bentobox.bentobox.api.events.IslandBaseEvent; +import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; @@ -44,18 +52,26 @@ public class IslandTeamInviteCommandTest { @Mock private IslandTeamCommand ic; - private UUID uuid; - @Mock - private User user; @Mock private IslandsManager im; @Mock + private Island island; + @Mock + private PluginManager pim; + @Mock private PlayersManager pm; - private UUID notUUID; @Mock private Settings s; @Mock - private Island island; + private User target; + @Mock + private User user; + + private UUID uuid; + private UUID islandUUID; + private IslandTeamInviteCommand itl; + private UUID notUUID; + /** * @throws java.lang.Exception @@ -73,159 +89,236 @@ public class IslandTeamInviteCommandTest { // Settings when(plugin.getSettings()).thenReturn(s); - // Player + // Player & users + PowerMockito.mockStatic(User.class); + Player p = mock(Player.class); // Sometimes use Mockito.withSettings().verboseLogging() when(user.isOp()).thenReturn(false); uuid = UUID.randomUUID(); - notUUID = UUID.randomUUID(); - while(notUUID.equals(uuid)) { - notUUID = UUID.randomUUID(); - } when(user.getUniqueId()).thenReturn(uuid); when(user.getPlayer()).thenReturn(p); when(user.getName()).thenReturn("tastybento"); + when(user.isOnline()).thenReturn(true); + // Permission to invite 3 more players + when(user.getPermissionValue(anyString(), anyInt())).thenReturn(3); + when(User.getInstance(eq(uuid))).thenReturn(user); + User.setPlugin(plugin); + // Target + notUUID = UUID.randomUUID(); + when(target.getUniqueId()).thenReturn(notUUID); + when(target.getPlayer()).thenReturn(p); + when(target.isOnline()).thenReturn(true); + when(target.getName()).thenReturn("target"); + when(User.getInstance(eq(notUUID))).thenReturn(target); + // Parent command has no aliases when(ic.getSubCommandAliases()).thenReturn(new HashMap<>()); + // Island + islandUUID = UUID.randomUUID(); + when(island.getUniqueId()).thenReturn(islandUUID.toString()); + // Player has island to begin with - when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); - when(im.inTeam(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); - when(im.isOwner(Mockito.any(), Mockito.any())).thenReturn(true); - when(im.getOwner(Mockito.any(), Mockito.any())).thenReturn(uuid); - when(island.getRank(Mockito.any())).thenReturn(RanksManager.OWNER_RANK); - when(im.getIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(island); + when(im.hasIsland(any(), eq(uuid))).thenReturn(true); + when(im.isOwner(any(), eq(uuid))).thenReturn(true); + when(im.getOwner(any(), eq(uuid))).thenReturn(uuid); + when(island.getRank(any())).thenReturn(RanksManager.OWNER_RANK); + when(im.getIsland(any(), eq(user))).thenReturn(island); when(plugin.getIslands()).thenReturn(im); // Has team - when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + when(im.inTeam(any(), eq(uuid))).thenReturn(true); // Player Manager when(plugin.getPlayers()).thenReturn(pm); + when(pm.getUUID(eq("tastybento"))).thenReturn(uuid); + when(pm.getUUID(eq("target"))).thenReturn(notUUID); // Server & Scheduler BukkitScheduler sch = mock(BukkitScheduler.class); PowerMockito.mockStatic(Bukkit.class); when(Bukkit.getScheduler()).thenReturn(sch); + when(Bukkit.getPluginManager()).thenReturn(pim); // Locales LocalesManager lm = mock(LocalesManager.class); - when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation"); + when(lm.get(any(), any())).thenReturn("mock translation"); when(plugin.getLocalesManager()).thenReturn(lm); // IWM friendly name IslandWorldManager iwm = mock(IslandWorldManager.class); - when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock"); + when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); when(plugin.getIWM()).thenReturn(iwm); + + // Parent command + when(ic.getTopLabel()).thenReturn("island"); + + // Command under test + itl = new IslandTeamInviteCommand(ic); + } /** * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#canExecute(User, String, java.util.List)}. */ @Test - public void testExecuteNoIsland() { - when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); - when(im.inTeam(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); - IslandTeamInviteCommand itl = new IslandTeamInviteCommand(ic); - assertFalse(itl.canExecute(user, itl.getLabel(), new ArrayList<>())); - Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-island")); - } - - /** - * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#canExecute(User, String, java.util.List)}. - */ - @Test - public void testExecuteLowRank() { - when(island.getRank(Mockito.any())).thenReturn(RanksManager.MEMBER_RANK); - when(island.getRankCommand(anyString())).thenReturn(RanksManager.OWNER_RANK); - IslandTeamInviteCommand itl = new IslandTeamInviteCommand(ic); - assertFalse(itl.canExecute(user, itl.getLabel(), new ArrayList<>())); - Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-permission")); - } - - /** - * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#canExecute(User, String, java.util.List)}. - */ - @Test - public void testExecuteNoTarget() { - IslandTeamInviteCommand itl = new IslandTeamInviteCommand(ic); - assertFalse(itl.canExecute(user, itl.getLabel(), new ArrayList<>())); - // Show help - } - - /** - * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#execute(User, String, java.util.List)}. - */ - @Test - public void testExecuteUnknownPlayer() { - IslandTeamInviteCommand itl = new IslandTeamInviteCommand(ic); - String[] name = {"tastybento"}; - when(pm.getUUID(Mockito.any())).thenReturn(null); - assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); - Mockito.verify(user).sendMessage("general.errors.unknown-player", "[name]", name[0]); - } - - - /** - * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#execute(User, String, java.util.List)}. - */ - @Test - public void testExecuteOfflinePlayer() { - PowerMockito.mockStatic(User.class); - when(User.getInstance(Mockito.any(UUID.class))).thenReturn(user); - when(user.isOnline()).thenReturn(false); - IslandTeamInviteCommand itl = new IslandTeamInviteCommand(ic); - String[] name = {"tastybento"}; - when(pm.getUUID(Mockito.any())).thenReturn(uuid); - assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); - Mockito.verify(user).sendMessage(Mockito.eq("general.errors.offline-player")); - } - - /** - * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#execute(User, String, java.util.List)}. - */ - @Test - public void testExecuteSamePlayer() { - PowerMockito.mockStatic(User.class); - when(User.getInstance(Mockito.any(UUID.class))).thenReturn(user); - when(user.isOnline()).thenReturn(true); - IslandTeamInviteCommand itl = new IslandTeamInviteCommand(ic); - String[] name = {"tastybento"}; - when(pm.getUUID(Mockito.any())).thenReturn(uuid); - assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); - Mockito.verify(user).sendMessage(Mockito.eq("commands.island.team.invite.errors.cannot-invite-self")); - } - - - /** - * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#execute(User, String, java.util.List)}. - */ - @Test - public void testExecuteDifferentPlayerInTeam() { - PowerMockito.mockStatic(User.class); - when(User.getInstance(Mockito.any(UUID.class))).thenReturn(user); - when(user.isOnline()).thenReturn(true); - IslandTeamInviteCommand itl = new IslandTeamInviteCommand(ic); - String[] name = {"tastybento"}; - when(pm.getUUID(Mockito.any())).thenReturn(notUUID); - when(im.inTeam(Mockito.any(), Mockito.any())).thenReturn(true); - assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); - Mockito.verify(user).sendMessage(Mockito.eq("commands.island.team.invite.errors.already-on-team")); - } - - /** - * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#execute(User, String, java.util.List)}. - */ - @Test - public void testExecuteCoolDownActive() { + public void testCanExecuteCoolDownActive() { // 10 minutes = 600 seconds when(s.getInviteCooldown()).thenReturn(10); - IslandTeamInviteCommand itl = new IslandTeamInviteCommand(ic); - String[] name = {"tastybento"}; - assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); + itl.setCooldown(islandUUID, notUUID, 100); + assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("target"))); + verify(user).sendMessage(eq("general.errors.you-must-wait"), eq(TextVariables.NUMBER), anyString()); + } + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#canExecute(User, String, java.util.List)}. + */ + @Test + public void testCanExecuteDifferentPlayerInTeam() { + when(im.inTeam(any(), any())).thenReturn(true); + assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("target"))); + verify(user).sendMessage(eq("commands.island.team.invite.errors.already-on-team")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#canExecute(User, String, java.util.List)}. + */ + @Test + public void testCanExecuteLowRank() { + when(island.getRank(any())).thenReturn(RanksManager.MEMBER_RANK); + when(island.getRankCommand(anyString())).thenReturn(RanksManager.OWNER_RANK); + assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("target"))); + verify(user).sendMessage(eq("general.errors.no-permission")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#canExecute(User, String, java.util.List)}. + */ + @Test + public void testCanExecuteNoIsland() { + when(im.hasIsland(any(), any(UUID.class))).thenReturn(false); + when(im.inTeam(any(), any(UUID.class))).thenReturn(false); + assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("target"))); + verify(user).sendMessage(eq("general.errors.no-island")); + } + + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#canExecute(User, String, java.util.List)}. + */ + @Test + public void testCanExecuteNoTarget() { + assertFalse(itl.canExecute(user, itl.getLabel(), Collections.emptyList())); + // Show help + verify(user).sendMessage(eq("commands.help.header"),eq(TextVariables.LABEL),eq(null)); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#canExecute(User, String, java.util.List)}. + */ + @Test + public void testCanExecuteOfflinePlayer() { + when(target.isOnline()).thenReturn(false); + assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("target"))); + verify(user).sendMessage(eq("general.errors.offline-player")); + } + + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#canExecute(User, String, java.util.List)}. + */ + @Test + public void testCanExecuteSamePlayer() { + assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("tastybento"))); + verify(user).sendMessage(eq("commands.island.team.invite.errors.cannot-invite-self")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#canExecute(User, String, java.util.List)}. + */ + @Test + public void testCanExecuteSuccess() { + assertTrue(itl.canExecute(user, itl.getLabel(), Collections.singletonList("target"))); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#canExecute(User, String, java.util.List)}. + */ + @Test + public void testCanExecuteUnknownPlayer() { + when(pm.getUUID(eq("target"))).thenReturn(null); + assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("target"))); + verify(user).sendMessage(eq("general.errors.unknown-player"), eq(TextVariables.NAME), eq("target")); + } + + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#execute(User, String, java.util.List)}. + */ + @Test + public void testExecuteFullIsland() { + when(user.getPermissionValue(anyString(), anyInt())).thenReturn(0); + testCanExecuteSuccess(); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("target"))); + verify(user).sendMessage(eq("commands.island.team.invite.errors.island-is-full")); + verify(user).getPermissionValue(eq("nullteam.maxsize"), eq(0)); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#execute(User, String, java.util.List)}. + */ + @Test + public void testExecuteSuccessTargetHasIsland() { + when(im.hasIsland(any(), eq(notUUID))).thenReturn(true); + testCanExecuteSuccess(); + assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("target"))); + verify(pim).callEvent(any(IslandBaseEvent.class)); + verify(user, never()).sendMessage(eq("commands.island.team.invite.removing-invite")); + verify(ic).addInvite(eq(Invite.Type.TEAM), eq(uuid), eq(notUUID)); + verify(user).sendMessage(eq("commands.island.team.invite.invitation-sent"), eq(TextVariables.NAME), eq("target")); + verify(target).sendMessage(eq("commands.island.team.invite.name-has-invited-you"), eq(TextVariables.NAME), eq("tastybento")); + verify(target).sendMessage(eq("commands.island.team.invite.to-accept-or-reject"), eq(TextVariables.LABEL), eq("island")); + verify(target).sendMessage(eq("commands.island.team.invite.you-will-lose-your-island")); + + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#execute(User, String, java.util.List)}. + */ + @Test + public void testExecuteSuccessTargetHasNoIsland() { + testCanExecuteSuccess(); + assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("target"))); + verify(pim).callEvent(any(IslandBaseEvent.class)); + verify(user, never()).sendMessage(eq("commands.island.team.invite.removing-invite")); + verify(ic).addInvite(eq(Invite.Type.TEAM), eq(uuid), eq(notUUID)); + verify(user).sendMessage(eq("commands.island.team.invite.invitation-sent"), eq(TextVariables.NAME), eq("target")); + verify(target).sendMessage(eq("commands.island.team.invite.name-has-invited-you"), eq(TextVariables.NAME), eq("tastybento")); + verify(target).sendMessage(eq("commands.island.team.invite.to-accept-or-reject"), eq(TextVariables.LABEL), eq("island")); + verify(target, never()).sendMessage(eq("commands.island.team.invite.you-will-lose-your-island")); + + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#execute(User, String, java.util.List)}. + */ + @Test + public void testExecuteTargetAlreadyInvited() { + testCanExecuteSuccess(); + + when(ic.isInvited(eq(notUUID))).thenReturn(true); + // Set up invite + when(ic.getInviter(eq(notUUID))).thenReturn(uuid); + Invite invite = mock(Invite.class); + when(invite.getType()).thenReturn(Type.TEAM); + when(ic.getInvite(eq(notUUID))).thenReturn(invite); + assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("target"))); + verify(pim).callEvent(any(IslandBaseEvent.class)); + verify(ic).removeInvite(eq(notUUID)); + verify(user).sendMessage(eq("commands.island.team.invite.removing-invite")); } }