diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandResetCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandResetCommand.java index 0a8656dc4..877cf60ab 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandResetCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandResetCommand.java @@ -4,7 +4,6 @@ import java.io.IOException; import java.util.List; import org.bukkit.Bukkit; -import org.bukkit.entity.Player; import org.eclipse.jdt.annotation.NonNull; import world.bentobox.bentobox.api.addons.GameModeAddon; @@ -43,15 +42,10 @@ public class IslandResetCommand extends ConfirmableCommand { if (getSettings().getResetCooldown() > 0 && checkCooldown(user)) { return false; } - if (!getIslands().hasIsland(getWorld(), user.getUniqueId())) { user.sendMessage("general.errors.no-island"); return false; } - if (!getIslands().isOwner(getWorld(), user.getUniqueId())) { - user.sendMessage("general.errors.not-owner"); - return false; - } int resetsLeft = getPlayers().getResetsLeft(getWorld(), user.getUniqueId()); if (resetsLeft != -1) { // Resets are not unlimited here @@ -110,10 +104,9 @@ public class IslandResetCommand extends ConfirmableCommand { private boolean resetIsland(User user, String name) { // Reset the island - Player player = user.getPlayer(); user.sendMessage("commands.island.create.creating-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) kickMembers(oldIsland); @@ -181,7 +174,7 @@ public class IslandResetCommand extends ConfirmableCommand { .reason(TeamEvent.Reason.DELETE) .involvedPlayer(memberUUID) .build(); - Bukkit.getServer().getPluginManager().callEvent(e); + Bukkit.getPluginManager().callEvent(e); }); } } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java index dbb970a08..dbce6d442 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java @@ -2,9 +2,10 @@ package world.bentobox.bentobox.api.commands.island; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.eq; +import static org.mockito.ArgumentMatchers.any; +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; @@ -16,21 +17,27 @@ import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.entity.Player; +import org.bukkit.plugin.PluginManager; import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitTask; +import org.eclipse.jdt.annotation.Nullable; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.stubbing.Answer; 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.ImmutableSet; +import com.google.common.collect.ImmutableSet.Builder; + import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; 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.database.objects.Island; import world.bentobox.bentobox.managers.BlueprintsManager; @@ -48,16 +55,29 @@ import world.bentobox.bentobox.managers.island.NewIsland; @PrepareForTest({Bukkit.class, BentoBox.class, NewIsland.class }) public class IslandResetCommandTest { + @Mock private CompositeCommand ic; - private UUID uuid; + @Mock private User user; + @Mock private Settings s; + @Mock private IslandsManager im; + @Mock private PlayersManager pm; + @Mock private World world; + @Mock private IslandWorldManager iwm; @Mock private BlueprintsManager bpm; + @Mock + private @Nullable Island island; + @Mock + private PluginManager pim; + + private IslandResetCommand irc; + private UUID uuid; /** * @throws java.lang.Exception @@ -73,36 +93,30 @@ public class IslandResetCommandTest { when(plugin.getCommandsManager()).thenReturn(cm); // Settings - s = mock(Settings.class); when(s.getResetCooldown()).thenReturn(0); when(plugin.getSettings()).thenReturn(s); // Player Player p = mock(Player.class); - // User, sometime use Mockito.withSettings().verboseLogging() - user = mock(User.class); + // User when(user.isOp()).thenReturn(false); uuid = UUID.randomUUID(); when(user.getUniqueId()).thenReturn(uuid); when(user.getPlayer()).thenReturn(p); + when(user.getTranslation(any())).thenAnswer((Answer) invocation -> invocation.getArgument(0, String.class)); // Parent command has no aliases - ic = mock(CompositeCommand.class); when(ic.getSubCommandAliases()).thenReturn(new HashMap<>()); when(ic.getTopLabel()).thenReturn("island"); // World - world = mock(World.class); when(ic.getWorld()).thenReturn(world); // 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.isOwner(any(), eq(uuid))).thenReturn(false); when(plugin.getIslands()).thenReturn(im); - // Has team - pm = mock(PlayersManager.class); when(im.inTeam(any(), eq(uuid))).thenReturn(true); when(plugin.getPlayers()).thenReturn(pm); @@ -113,51 +127,50 @@ public class IslandResetCommandTest { PowerMockito.mockStatic(Bukkit.class); when(Bukkit.getScheduler()).thenReturn(sch); + // Event + when(Bukkit.getPluginManager()).thenReturn(pim); // IWM friendly name - iwm = mock(IslandWorldManager.class); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); when(plugin.getIWM()).thenReturn(iwm); // Bundles manager when(plugin.getBlueprintsManager()).thenReturn(bpm); 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 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 public void testNoIsland() { - IslandResetCommand irc = new IslandResetCommand(ic); // Test the reset command // Does not have island assertFalse(irc.canExecute(user, irc.getLabel(), Collections.emptyList())); verify(user).sendMessage("general.errors.no-island"); } - @Test - public void testNotOwner() { - 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 method for {@link IslandResetCommand#canExecute(User, String, java.util.List)} + */ @Test public void testNoResetsLeft() { - 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 @@ -166,24 +179,21 @@ public class IslandResetCommandTest { when(im.inTeam(any(), eq(uuid))).thenReturn(false); // 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())); 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 public void testNoConfirmationRequired() throws IOException { - 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); - // 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 when(s.isResetConfirmation()).thenReturn(false); @@ -206,11 +216,17 @@ public class IslandResetCommandTest { assertTrue(irc.execute(user, irc.getLabel(), Collections.emptyList())); // TODO Verify that panel was shown // 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 public void testUnlimitedResets() throws IOException { - 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 @@ -241,10 +257,11 @@ public class IslandResetCommandTest { assertTrue(irc.canExecute(user, irc.getLabel(), Collections.emptyList())); } - @Ignore("NPE") + /** + * Test method for {@link IslandResetCommand#execute(User, String, java.util.List)} + */ @Test public void testConfirmationRequired() throws IOException { - 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 @@ -280,11 +297,14 @@ public class IslandResetCommandTest { // Send command again to confirm 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 public void testNoConfirmationRequiredUnknownBlueprint() throws IOException { - IslandResetCommand irc = new IslandResetCommand(ic); // No such bundle when(bpm.validate(any(), any())).thenReturn(null); // Reset command, no confirmation required @@ -294,9 +314,11 @@ public class IslandResetCommandTest { ); } + /** + * Test method for {@link IslandResetCommand#execute(User, String, java.util.List)} + */ @Test public void testNoConfirmationRequiredBlueprintNoPerm() throws IOException { - IslandResetCommand irc = new IslandResetCommand(ic); // Bundle exists when(bpm.validate(any(), any())).thenReturn("custom"); // No permission @@ -305,10 +327,11 @@ public class IslandResetCommandTest { assertFalse(irc.execute(user, irc.getLabel(), Collections.singletonList("custom"))); } - @Ignore("NPE") + /** + * Test method for {@link IslandResetCommand#execute(User, String, java.util.List)} + */ @Test public void testNoConfirmationRequiredCustomSchemHasPermission() throws IOException { - 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 @@ -342,5 +365,8 @@ public class IslandResetCommandTest { // Reset command, no confirmation required assertTrue(irc.execute(user, irc.getLabel(), Collections.singletonList("custom"))); verify(user).sendMessage("commands.island.create.creating-island"); + // Verify event + verify(pim).callEvent(any(IslandBaseEvent.class)); + } }