Fixes IslandResetCommand test

This commit is contained in:
tastybento 2019-08-29 15:32:50 -07:00
parent 87168a5ea9
commit fa4c772572
2 changed files with 78 additions and 59 deletions

View File

@ -4,7 +4,6 @@ import java.io.IOException;
import java.util.List; import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.addons.GameModeAddon;
@ -43,15 +42,10 @@ public class IslandResetCommand extends ConfirmableCommand {
if (getSettings().getResetCooldown() > 0 && checkCooldown(user)) { if (getSettings().getResetCooldown() > 0 && checkCooldown(user)) {
return false; return false;
} }
if (!getIslands().hasIsland(getWorld(), user.getUniqueId())) { if (!getIslands().hasIsland(getWorld(), user.getUniqueId())) {
user.sendMessage("general.errors.no-island"); user.sendMessage("general.errors.no-island");
return false; return false;
} }
if (!getIslands().isOwner(getWorld(), user.getUniqueId())) {
user.sendMessage("general.errors.not-owner");
return false;
}
int resetsLeft = getPlayers().getResetsLeft(getWorld(), user.getUniqueId()); int resetsLeft = getPlayers().getResetsLeft(getWorld(), user.getUniqueId());
if (resetsLeft != -1) { if (resetsLeft != -1) {
// Resets are not unlimited here // Resets are not unlimited here
@ -110,10 +104,9 @@ public class IslandResetCommand extends ConfirmableCommand {
private boolean resetIsland(User user, String name) { private boolean resetIsland(User user, String name) {
// Reset the island // Reset the island
Player player = user.getPlayer();
user.sendMessage("commands.island.create.creating-island"); user.sendMessage("commands.island.create.creating-island");
// Get the player's old island // Get the player's old island
Island oldIsland = getIslands().getIsland(getWorld(), user.getUniqueId()); Island oldIsland = getIslands().getIsland(getWorld(), user);
// Kick all island members (including the owner) // Kick all island members (including the owner)
kickMembers(oldIsland); kickMembers(oldIsland);
@ -181,7 +174,7 @@ public class IslandResetCommand extends ConfirmableCommand {
.reason(TeamEvent.Reason.DELETE) .reason(TeamEvent.Reason.DELETE)
.involvedPlayer(memberUUID) .involvedPlayer(memberUUID)
.build(); .build();
Bukkit.getServer().getPluginManager().callEvent(e); Bukkit.getPluginManager().callEvent(e);
}); });
} }
} }

View File

@ -2,9 +2,10 @@ package world.bentobox.bentobox.api.commands.island;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -16,21 +17,27 @@ import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox; import org.powermock.reflect.Whitebox;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.events.IslandBaseEvent;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.BlueprintsManager; import world.bentobox.bentobox.managers.BlueprintsManager;
@ -48,16 +55,29 @@ import world.bentobox.bentobox.managers.island.NewIsland;
@PrepareForTest({Bukkit.class, BentoBox.class, NewIsland.class }) @PrepareForTest({Bukkit.class, BentoBox.class, NewIsland.class })
public class IslandResetCommandTest { public class IslandResetCommandTest {
@Mock
private CompositeCommand ic; private CompositeCommand ic;
private UUID uuid; @Mock
private User user; private User user;
@Mock
private Settings s; private Settings s;
@Mock
private IslandsManager im; private IslandsManager im;
@Mock
private PlayersManager pm; private PlayersManager pm;
@Mock
private World world; private World world;
@Mock
private IslandWorldManager iwm; private IslandWorldManager iwm;
@Mock @Mock
private BlueprintsManager bpm; private BlueprintsManager bpm;
@Mock
private @Nullable Island island;
@Mock
private PluginManager pim;
private IslandResetCommand irc;
private UUID uuid;
/** /**
* @throws java.lang.Exception * @throws java.lang.Exception
@ -73,36 +93,30 @@ public class IslandResetCommandTest {
when(plugin.getCommandsManager()).thenReturn(cm); when(plugin.getCommandsManager()).thenReturn(cm);
// Settings // Settings
s = mock(Settings.class);
when(s.getResetCooldown()).thenReturn(0); when(s.getResetCooldown()).thenReturn(0);
when(plugin.getSettings()).thenReturn(s); when(plugin.getSettings()).thenReturn(s);
// Player // Player
Player p = mock(Player.class); Player p = mock(Player.class);
// User, sometime use Mockito.withSettings().verboseLogging() // User
user = mock(User.class);
when(user.isOp()).thenReturn(false); when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID(); uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p); when(user.getPlayer()).thenReturn(p);
when(user.getTranslation(any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
// Parent command has no aliases // Parent command has no aliases
ic = mock(CompositeCommand.class);
when(ic.getSubCommandAliases()).thenReturn(new HashMap<>()); when(ic.getSubCommandAliases()).thenReturn(new HashMap<>());
when(ic.getTopLabel()).thenReturn("island"); when(ic.getTopLabel()).thenReturn("island");
// World // World
world = mock(World.class);
when(ic.getWorld()).thenReturn(world); when(ic.getWorld()).thenReturn(world);
// No island for player to begin with (set it later in the tests) // No island for player to begin with (set it later in the tests)
im = mock(IslandsManager.class);
when(im.hasIsland(any(), eq(uuid))).thenReturn(false); when(im.hasIsland(any(), eq(uuid))).thenReturn(false);
when(im.isOwner(any(), eq(uuid))).thenReturn(false); when(im.isOwner(any(), eq(uuid))).thenReturn(false);
when(plugin.getIslands()).thenReturn(im); when(plugin.getIslands()).thenReturn(im);
// Has team // Has team
pm = mock(PlayersManager.class);
when(im.inTeam(any(), eq(uuid))).thenReturn(true); when(im.inTeam(any(), eq(uuid))).thenReturn(true);
when(plugin.getPlayers()).thenReturn(pm); when(plugin.getPlayers()).thenReturn(pm);
@ -113,51 +127,50 @@ public class IslandResetCommandTest {
PowerMockito.mockStatic(Bukkit.class); PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getScheduler()).thenReturn(sch); when(Bukkit.getScheduler()).thenReturn(sch);
// Event
when(Bukkit.getPluginManager()).thenReturn(pim);
// IWM friendly name // IWM friendly name
iwm = mock(IslandWorldManager.class);
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
when(plugin.getIWM()).thenReturn(iwm); when(plugin.getIWM()).thenReturn(iwm);
// Bundles manager // Bundles manager
when(plugin.getBlueprintsManager()).thenReturn(bpm); when(plugin.getBlueprintsManager()).thenReturn(bpm);
when(bpm.validate(any(), any())).thenReturn("custom"); when(bpm.validate(any(), any())).thenReturn("custom");
// Give the user some resets
when(pm.getResetsLeft(eq(world), eq(uuid))).thenReturn(3);
// Island team members
when(im.getIsland(any(), any(User.class))).thenReturn(island);
Builder<UUID> members = new ImmutableSet.Builder<>();
members.add(uuid);
when(island.getMemberSet()).thenReturn(members.build());
// The command
irc = new IslandResetCommand(ic);
} }
/** /**
* Test method for . * Test method for {@link IslandResetCommand#canExecute(User, String, java.util.List)}
*/ */
@Test @Test
public void testNoIsland() { public void testNoIsland() {
IslandResetCommand irc = new IslandResetCommand(ic);
// Test the reset command // Test the reset command
// Does not have island // Does not have island
assertFalse(irc.canExecute(user, irc.getLabel(), Collections.emptyList())); assertFalse(irc.canExecute(user, irc.getLabel(), Collections.emptyList()));
verify(user).sendMessage("general.errors.no-island"); verify(user).sendMessage("general.errors.no-island");
} }
@Test /**
public void testNotOwner() { * Test method for {@link IslandResetCommand#canExecute(User, String, java.util.List)}
IslandResetCommand irc = new IslandResetCommand(ic); */
// Now has island, but is not the owner
when(im.hasIsland(any(), eq(uuid))).thenReturn(true);
assertFalse(irc.canExecute(user, irc.getLabel(), Collections.emptyList()));
verify(user).sendMessage("general.errors.not-owner");
}
@Test
public void testHasTeam() {
IslandResetCommand irc = new IslandResetCommand(ic);
// Now has island, but is not the owner
when(im.hasIsland(any(), eq(uuid))).thenReturn(true);
// Now is owner, but still has team
when(im.isOwner(any(), eq(uuid))).thenReturn(true);
}
@Test @Test
public void testNoResetsLeft() { public void testNoResetsLeft() {
IslandResetCommand irc = new IslandResetCommand(ic);
// Now has island, but is not the owner // Now has island, but is not the owner
when(im.hasIsland(any(), eq(uuid))).thenReturn(true); when(im.hasIsland(any(), eq(uuid))).thenReturn(true);
// Now is owner, but still has team // Now is owner, but still has team
@ -166,24 +179,21 @@ public class IslandResetCommandTest {
when(im.inTeam(any(), eq(uuid))).thenReturn(false); when(im.inTeam(any(), eq(uuid))).thenReturn(false);
// Block based on no resets left // Block based on no resets left
when(pm.getResets(eq(world),eq(uuid))).thenReturn(3); when(pm.getResetsLeft(eq(world), eq(uuid))).thenReturn(0);
assertFalse(irc.canExecute(user, irc.getLabel(), Collections.emptyList())); assertFalse(irc.canExecute(user, irc.getLabel(), Collections.emptyList()));
verify(user).sendMessage("commands.island.reset.none-left"); verify(user).sendMessage("commands.island.reset.none-left");
// Verify event
verify(pim, never()).callEvent(any(IslandBaseEvent.class));
} }
@Ignore("NPE") /**
* Test method for {@link IslandResetCommand#execute(User, String, java.util.List)}
*/
@Test @Test
public void testNoConfirmationRequired() throws IOException { public void testNoConfirmationRequired() throws IOException {
IslandResetCommand irc = new IslandResetCommand(ic);
// Now has island, but is not the owner // Now has island, but is not the owner
when(im.hasIsland(any(), eq(uuid))).thenReturn(true); when(im.hasIsland(any(), eq(uuid))).thenReturn(true);
// Now is owner, but still has team
when(im.isOwner(any(), eq(uuid))).thenReturn(true);
// Now has no team
when(im.inTeam(any(), eq(uuid))).thenReturn(false);
// Give the user some resets
when(pm.getResetsLeft(eq(world), eq(uuid))).thenReturn(2);
// Set so no confirmation required // Set so no confirmation required
when(s.isResetConfirmation()).thenReturn(false); when(s.isResetConfirmation()).thenReturn(false);
@ -206,11 +216,17 @@ public class IslandResetCommandTest {
assertTrue(irc.execute(user, irc.getLabel(), Collections.emptyList())); assertTrue(irc.execute(user, irc.getLabel(), Collections.emptyList()));
// TODO Verify that panel was shown // TODO Verify that panel was shown
// verify(bpm).showPanel(any(), eq(user), eq(irc.getLabel())); // verify(bpm).showPanel(any(), eq(user), eq(irc.getLabel()));
// Verify event
verify(pim).callEvent(any(IslandBaseEvent.class));
// Verify messaging
verify(user).sendMessage("commands.island.create.creating-island");
} }
/**
* Test method for {@link IslandResetCommand#canExecute(User, String, java.util.List)}
*/
@Test @Test
public void testUnlimitedResets() throws IOException { public void testUnlimitedResets() throws IOException {
IslandResetCommand irc = new IslandResetCommand(ic);
// Now has island, but is not the owner // Now has island, but is not the owner
when(im.hasIsland(any(), eq(uuid))).thenReturn(true); when(im.hasIsland(any(), eq(uuid))).thenReturn(true);
// Now is owner, but still has team // Now is owner, but still has team
@ -241,10 +257,11 @@ public class IslandResetCommandTest {
assertTrue(irc.canExecute(user, irc.getLabel(), Collections.emptyList())); assertTrue(irc.canExecute(user, irc.getLabel(), Collections.emptyList()));
} }
@Ignore("NPE") /**
* Test method for {@link IslandResetCommand#execute(User, String, java.util.List)}
*/
@Test @Test
public void testConfirmationRequired() throws IOException { public void testConfirmationRequired() throws IOException {
IslandResetCommand irc = new IslandResetCommand(ic);
// Now has island, but is not the owner // Now has island, but is not the owner
when(im.hasIsland(any(), eq(uuid))).thenReturn(true); when(im.hasIsland(any(), eq(uuid))).thenReturn(true);
// Now is owner, but still has team // Now is owner, but still has team
@ -280,11 +297,14 @@ public class IslandResetCommandTest {
// Send command again to confirm // Send command again to confirm
assertTrue(irc.execute(user, irc.getLabel(), Collections.emptyList())); assertTrue(irc.execute(user, irc.getLabel(), Collections.emptyList()));
// Some more checking can go here...
} }
/**
* Test method for {@link IslandResetCommand#execute(User, String, java.util.List)}
*/
@Test @Test
public void testNoConfirmationRequiredUnknownBlueprint() throws IOException { public void testNoConfirmationRequiredUnknownBlueprint() throws IOException {
IslandResetCommand irc = new IslandResetCommand(ic);
// No such bundle // No such bundle
when(bpm.validate(any(), any())).thenReturn(null); when(bpm.validate(any(), any())).thenReturn(null);
// Reset command, no confirmation required // Reset command, no confirmation required
@ -294,9 +314,11 @@ public class IslandResetCommandTest {
); );
} }
/**
* Test method for {@link IslandResetCommand#execute(User, String, java.util.List)}
*/
@Test @Test
public void testNoConfirmationRequiredBlueprintNoPerm() throws IOException { public void testNoConfirmationRequiredBlueprintNoPerm() throws IOException {
IslandResetCommand irc = new IslandResetCommand(ic);
// Bundle exists // Bundle exists
when(bpm.validate(any(), any())).thenReturn("custom"); when(bpm.validate(any(), any())).thenReturn("custom");
// No permission // No permission
@ -305,10 +327,11 @@ public class IslandResetCommandTest {
assertFalse(irc.execute(user, irc.getLabel(), Collections.singletonList("custom"))); assertFalse(irc.execute(user, irc.getLabel(), Collections.singletonList("custom")));
} }
@Ignore("NPE") /**
* Test method for {@link IslandResetCommand#execute(User, String, java.util.List)}
*/
@Test @Test
public void testNoConfirmationRequiredCustomSchemHasPermission() throws IOException { public void testNoConfirmationRequiredCustomSchemHasPermission() throws IOException {
IslandResetCommand irc = new IslandResetCommand(ic);
// Now has island, but is not the owner // Now has island, but is not the owner
when(im.hasIsland(any(), eq(uuid))).thenReturn(true); when(im.hasIsland(any(), eq(uuid))).thenReturn(true);
// Now is owner, but still has team // Now is owner, but still has team
@ -342,5 +365,8 @@ public class IslandResetCommandTest {
// Reset command, no confirmation required // Reset command, no confirmation required
assertTrue(irc.execute(user, irc.getLabel(), Collections.singletonList("custom"))); assertTrue(irc.execute(user, irc.getLabel(), Collections.singletonList("custom")));
verify(user).sendMessage("commands.island.create.creating-island"); verify(user).sendMessage("commands.island.create.creating-island");
// Verify event
verify(pim).callEvent(any(IslandBaseEvent.class));
} }
} }