-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test class for island team invite accept command.
Reduced complexity of command.
- Loading branch information
1 parent
a397945
commit d4d1b77
Showing
2 changed files
with
258 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
import org.bukkit.GameMode; | ||
import org.bukkit.Location; | ||
|
||
import com.google.common.collect.BiMap; | ||
|
||
import world.bentobox.bentobox.api.commands.ConfirmableCommand; | ||
import world.bentobox.bentobox.api.events.IslandBaseEvent; | ||
import world.bentobox.bentobox.api.events.team.TeamEvent; | ||
|
@@ -20,6 +22,8 @@ | |
public class IslandTeamInviteAcceptCommand extends ConfirmableCommand { | ||
|
||
private IslandTeamCommand itc; | ||
private UUID playerUUID; | ||
private UUID prospectiveOwnerUUID; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
tastybento
Author
Member
|
||
|
||
public IslandTeamInviteAcceptCommand(IslandTeamCommand islandTeamCommand) { | ||
super(islandTeamCommand, "accept"); | ||
|
@@ -34,10 +38,11 @@ public void setup() { | |
} | ||
|
||
@Override | ||
public boolean execute(User user, String label, List<String> args) { | ||
UUID playerUUID = user.getUniqueId(); | ||
public boolean canExecute(User user, String label, List<String> args) { | ||
playerUUID = user.getUniqueId(); | ||
// Check if player has been invited | ||
if (!itc.getInviteCommand().getInviteList().containsKey(playerUUID)) { | ||
BiMap<UUID, UUID> inviteList = itc.getInviteCommand().getInviteList(); | ||
if (!inviteList.containsKey(playerUUID)) { | ||
user.sendMessage("commands.island.team.invite.errors.none-invited-you"); | ||
return false; | ||
} | ||
|
@@ -47,24 +52,24 @@ public boolean execute(User user, String label, List<String> args) { | |
return false; | ||
} | ||
// Get the island owner | ||
UUID prospectiveOwnerUUID = itc.getInviteCommand().getInviteList().get(playerUUID); | ||
prospectiveOwnerUUID = inviteList.get(playerUUID); | ||
if (!getIslands().hasIsland(getWorld(), prospectiveOwnerUUID)) { | ||
user.sendMessage("commands.island.team.invite.errors.invalid-invite"); | ||
itc.getInviteCommand().getInviteList().remove(playerUUID); | ||
inviteList.remove(playerUUID); | ||
return false; | ||
} | ||
// Fire event so add-ons can run commands, etc. | ||
IslandBaseEvent event = TeamEvent.builder() | ||
.island(getIslands() | ||
.getIsland(getWorld(), prospectiveOwnerUUID)) | ||
.island(getIslands().getIsland(getWorld(), prospectiveOwnerUUID)) | ||
.reason(TeamEvent.Reason.JOIN) | ||
.involvedPlayer(playerUUID) | ||
.build(); | ||
Bukkit.getServer().getPluginManager().callEvent(event); | ||
if (event.isCancelled()) { | ||
return true; | ||
} | ||
Bukkit.getPluginManager().callEvent(event); | ||
return !event.isCancelled(); | ||
} | ||
|
||
@Override | ||
public boolean execute(User user, String label, List<String> args) { | ||
askConfirmation(user, user.getTranslation("commands.island.team.invite.accept.confirmation"), () -> { | ||
// Remove the invite | ||
itc.getInviteCommand().getInviteList().remove(playerUUID); | ||
|
@@ -81,15 +86,7 @@ public boolean execute(User user, String label, List<String> args) { | |
// Remove player as owner of the old island | ||
getIslands().removePlayer(getWorld(), playerUUID); | ||
// Remove money inventory etc. for leaving | ||
if (getIWM().isOnLeaveResetEnderChest(getWorld()) || getIWM().isOnJoinResetEnderChest(getWorld())) { | ||
user.getPlayer().getEnderChest().clear(); | ||
} | ||
if (getIWM().isOnLeaveResetInventory(getWorld()) || getIWM().isOnJoinResetInventory(getWorld())) { | ||
user.getPlayer().getInventory().clear(); | ||
} | ||
if (getSettings().isUseEconomy() && (getIWM().isOnLeaveResetMoney(getWorld()) || getIWM().isOnJoinResetMoney(getWorld()))) { | ||
getPlugin().getVault().ifPresent(vault -> vault.withdraw(user, vault.getBalance(user))); | ||
} | ||
cleanPlayer(user); | ||
// Add the player as a team member of the new island | ||
getIslands().setJoinTeam(teamIsland, playerUUID); | ||
// Set the player's home | ||
|
@@ -114,8 +111,7 @@ public boolean execute(User user, String label, List<String> args) { | |
getIslands().save(teamIsland); | ||
// Fire event | ||
IslandBaseEvent e = TeamEvent.builder() | ||
.island(getIslands() | ||
.getIsland(getWorld(), prospectiveOwnerUUID)) | ||
.island(getIslands().getIsland(getWorld(), prospectiveOwnerUUID)) | ||
.reason(TeamEvent.Reason.JOINED) | ||
.involvedPlayer(playerUUID) | ||
.build(); | ||
|
@@ -125,4 +121,16 @@ public boolean execute(User user, String label, List<String> args) { | |
return true; | ||
} | ||
|
||
private void cleanPlayer(User user) { | ||
if (getIWM().isOnLeaveResetEnderChest(getWorld()) || getIWM().isOnJoinResetEnderChest(getWorld())) { | ||
user.getPlayer().getEnderChest().clear(); | ||
} | ||
if (getIWM().isOnLeaveResetInventory(getWorld()) || getIWM().isOnJoinResetInventory(getWorld())) { | ||
user.getPlayer().getInventory().clear(); | ||
} | ||
if (getSettings().isUseEconomy() && (getIWM().isOnLeaveResetMoney(getWorld()) || getIWM().isOnJoinResetMoney(getWorld()))) { | ||
getPlugin().getVault().ifPresent(vault -> vault.withdraw(user, vault.getBalance(user))); | ||
} | ||
} | ||
|
||
} |
228 changes: 228 additions & 0 deletions
228
...a/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteAcceptCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
/** | ||
* | ||
*/ | ||
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.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
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.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mockito; | ||
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 com.google.common.collect.BiMap; | ||
import com.google.common.collect.HashBiMap; | ||
|
||
import world.bentobox.bentobox.BentoBox; | ||
import world.bentobox.bentobox.Settings; | ||
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; | ||
import world.bentobox.bentobox.managers.LocalesManager; | ||
import world.bentobox.bentobox.managers.PlayersManager; | ||
import world.bentobox.bentobox.managers.RanksManager; | ||
|
||
/** | ||
* @author tastybento | ||
* | ||
*/ | ||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) | ||
public class IslandTeamInviteAcceptCommandTest { | ||
|
||
private IslandTeamCommand ic; | ||
private UUID uuid; | ||
private User user; | ||
private IslandsManager im; | ||
private PlayersManager pm; | ||
private UUID notUUID; | ||
private Settings s; | ||
private Island island; | ||
private IslandTeamInviteAcceptCommand c; | ||
private BiMap<UUID, UUID> biMap; | ||
private IslandTeamInviteCommand inviteCommand; | ||
private PluginManager pim; | ||
|
||
/** | ||
* @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); | ||
|
||
// Settings | ||
s = mock(Settings.class); | ||
when(s.getRankCommand(Mockito.anyString())).thenReturn(RanksManager.OWNER_RANK); | ||
when(plugin.getSettings()).thenReturn(s); | ||
|
||
// Player | ||
Player p = mock(Player.class); | ||
// Sometimes use Mockito.withSettings().verboseLogging() | ||
user = mock(User.class); | ||
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"); | ||
User.setPlugin(plugin); | ||
|
||
// Parent command has no aliases | ||
ic = mock(IslandTeamCommand.class); | ||
when(ic.getSubCommandAliases()).thenReturn(new HashMap<>()); | ||
when(ic.getPermissionPrefix()).thenReturn("bskyblock."); | ||
inviteCommand = mock(IslandTeamInviteCommand.class); | ||
biMap = HashBiMap.create(); | ||
when(inviteCommand.getInviteList()).thenReturn(biMap); | ||
when(ic.getInviteCommand()).thenReturn(inviteCommand); | ||
|
||
// Player has island to begin with | ||
im = mock(IslandsManager.class); | ||
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); | ||
island = mock(Island.class); | ||
when(island.getRank(Mockito.any())).thenReturn(RanksManager.OWNER_RANK); | ||
when(im.getIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(island); | ||
when(plugin.getIslands()).thenReturn(im); | ||
|
||
// Has team | ||
when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); | ||
|
||
// Player Manager | ||
pm = mock(PlayersManager.class); | ||
|
||
when(plugin.getPlayers()).thenReturn(pm); | ||
|
||
// Server & Scheduler | ||
BukkitScheduler sch = mock(BukkitScheduler.class); | ||
PowerMockito.mockStatic(Bukkit.class); | ||
when(Bukkit.getScheduler()).thenReturn(sch); | ||
pim = mock(PluginManager.class); | ||
when(Bukkit.getPluginManager()).thenReturn(pim); | ||
|
||
// Locales | ||
LocalesManager lm = mock(LocalesManager.class); | ||
when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation"); | ||
when(plugin.getLocalesManager()).thenReturn(lm); | ||
when(user.getTranslation(Mockito.anyString())).thenReturn("mock translation2"); | ||
|
||
// IWM friendly name | ||
IslandWorldManager iwm = mock(IslandWorldManager.class); | ||
when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock"); | ||
when(plugin.getIWM()).thenReturn(iwm); | ||
|
||
// Team invite accept command | ||
c = new IslandTeamInviteAcceptCommand(ic); | ||
} | ||
|
||
/** | ||
* @throws java.lang.Exception | ||
*/ | ||
@After | ||
public void tearDown() throws Exception { | ||
User.clearUsers(); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteAcceptCommand#IslandTeamInviteAcceptCommand(world.bentobox.bentobox.api.commands.island.team.IslandTeamCommand)}. | ||
*/ | ||
@Test | ||
public void testIslandTeamInviteAcceptCommand() { | ||
assertEquals("accept", c.getLabel()); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteAcceptCommand#setup()}. | ||
*/ | ||
@Test | ||
public void testSetup() { | ||
assertEquals("bskyblock.island.team", c.getPermission()); | ||
assertTrue(c.isOnlyPlayer()); | ||
assertEquals("commands.island.team.invite.accept.description", c.getDescription()); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteAcceptCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. | ||
*/ | ||
@Test | ||
public void testCanExecuteNoInvite() { | ||
assertFalse(c.canExecute(user, "accept", Collections.emptyList())); | ||
Mockito.verify(user).sendMessage("commands.island.team.invite.errors.none-invited-you"); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteAcceptCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. | ||
*/ | ||
@Test | ||
public void testCanExecuteInTeam() { | ||
biMap.put(uuid, notUUID); | ||
when(im.inTeam(Mockito.any(), Mockito.any())).thenReturn(true); | ||
assertFalse(c.canExecute(user, "accept", Collections.emptyList())); | ||
Mockito.verify(user).sendMessage("commands.island.team.invite.errors.you-already-are-in-team"); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteAcceptCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. | ||
*/ | ||
@Test | ||
public void testCanExecuteInvalidInvite() { | ||
biMap.put(uuid, notUUID); | ||
when(im.inTeam(Mockito.any(), Mockito.any())).thenReturn(false); | ||
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); | ||
assertFalse(c.canExecute(user, "accept", Collections.emptyList())); | ||
Mockito.verify(user).sendMessage("commands.island.team.invite.errors.invalid-invite"); | ||
Mockito.verify(inviteCommand).getInviteList(); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteAcceptCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. | ||
*/ | ||
@Test | ||
public void testCanExecuteOkay() { | ||
biMap.put(uuid, notUUID); | ||
when(im.inTeam(Mockito.any(), Mockito.any())).thenReturn(false); | ||
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); | ||
assertTrue(c.canExecute(user, "accept", Collections.emptyList())); | ||
Mockito.verify(pim).callEvent(Mockito.any()); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteAcceptCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. | ||
*/ | ||
@Test | ||
public void testExecuteUserStringListOfString() { | ||
assertTrue(c.execute(user, "accept", Collections.emptyList())); | ||
Mockito.verify(user).getTranslation("commands.island.team.invite.accept.confirmation"); | ||
} | ||
|
||
} |
Don't we risk some concurrency here?