bentobox/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminDeleteCommandTest.java

268 lines
9.4 KiB
Java
Raw Normal View History

2018-08-08 22:13:54 +02:00
package world.bentobox.bentobox.api.commands.admin;
import static org.junit.Assert.assertFalse;
2018-08-15 09:43:28 +02:00
import static org.junit.Assert.assertTrue;
2021-09-18 21:25:25 +02:00
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
2018-08-08 22:13:54 +02:00
import static org.mockito.Mockito.mock;
2021-09-18 21:25:25 +02:00
import static org.mockito.Mockito.verify;
2018-08-08 22:13:54 +02:00
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.Collections;
2018-08-08 22:13:54 +02:00
import java.util.HashMap;
2 0 0 multi island (#2185) * Multi world WIP - stashing * Initial work on supporting multiple islands per player The default allowed number is 5 for now, but will be set to 1 by default. Lots more work to do on this! * More work on multi island. Fixed tests so clean compile. * Remove unused imports * Updated island go and homes command to multi island Updated tests. * Do not reload addons anymore. * Add island name when entering or leaving own island * Remove unused import * Adds island names to /island go command. * Enables more homes to be set if player has more than one island * Switch to using a set for islands and explicit primary boolean in Island * WIP * Fix bugs with the go command. * Be able to delete multiple islands, e.g. when joining a team This is not fully tested. * Do not remove all islands when a player does reset. Players can reset just the island they are on. * More fixes for go command * Fix tests * Fix @NonNull annotation * Fix home syntax listing reference for IslandDeleteHome * Fixed deletehome for multiple islands. * Fix /island command teleport to current island default home. * Remove deprecated code. * Fix tag for concurrent island setting in config.yml * Improve error when trying to make additional islands over limit * Update config.yml * Correctly assign invites for islands. * Switch to canExecute API in prep for multi-island handling * Prevent players from obtaining more concurrent islands by owner transfer * Handle leaving and disbanding of teams * Fix tests * Fix minor bugs or code smells. * Restore the quarantine code from deprecation. This code can stay. It checks if islands can load, and if not puts them in a trash. It does no harm. * Remove unneeded eq()'s * Fix tests
2023-09-17 00:55:52 +02:00
import java.util.List;
2018-08-08 22:13:54 +02:00
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
2 0 0 multi island (#2185) * Multi world WIP - stashing * Initial work on supporting multiple islands per player The default allowed number is 5 for now, but will be set to 1 by default. Lots more work to do on this! * More work on multi island. Fixed tests so clean compile. * Remove unused imports * Updated island go and homes command to multi island Updated tests. * Do not reload addons anymore. * Add island name when entering or leaving own island * Remove unused import * Adds island names to /island go command. * Enables more homes to be set if player has more than one island * Switch to using a set for islands and explicit primary boolean in Island * WIP * Fix bugs with the go command. * Be able to delete multiple islands, e.g. when joining a team This is not fully tested. * Do not remove all islands when a player does reset. Players can reset just the island they are on. * More fixes for go command * Fix tests * Fix @NonNull annotation * Fix home syntax listing reference for IslandDeleteHome * Fixed deletehome for multiple islands. * Fix /island command teleport to current island default home. * Remove deprecated code. * Fix tag for concurrent island setting in config.yml * Improve error when trying to make additional islands over limit * Update config.yml * Correctly assign invites for islands. * Switch to canExecute API in prep for multi-island handling * Prevent players from obtaining more concurrent islands by owner transfer * Handle leaving and disbanding of teams * Fix tests * Fix minor bugs or code smells. * Restore the quarantine code from deprecation. This code can stay. It checks if islands can load, and if not puts them in a trash. It does no harm. * Remove unneeded eq()'s * Fix tests
2023-09-17 00:55:52 +02:00
import org.bukkit.World;
2018-08-08 22:13:54 +02:00
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.Vector;
2 0 0 multi island (#2185) * Multi world WIP - stashing * Initial work on supporting multiple islands per player The default allowed number is 5 for now, but will be set to 1 by default. Lots more work to do on this! * More work on multi island. Fixed tests so clean compile. * Remove unused imports * Updated island go and homes command to multi island Updated tests. * Do not reload addons anymore. * Add island name when entering or leaving own island * Remove unused import * Adds island names to /island go command. * Enables more homes to be set if player has more than one island * Switch to using a set for islands and explicit primary boolean in Island * WIP * Fix bugs with the go command. * Be able to delete multiple islands, e.g. when joining a team This is not fully tested. * Do not remove all islands when a player does reset. Players can reset just the island they are on. * More fixes for go command * Fix tests * Fix @NonNull annotation * Fix home syntax listing reference for IslandDeleteHome * Fixed deletehome for multiple islands. * Fix /island command teleport to current island default home. * Remove deprecated code. * Fix tag for concurrent island setting in config.yml * Improve error when trying to make additional islands over limit * Update config.yml * Correctly assign invites for islands. * Switch to canExecute API in prep for multi-island handling * Prevent players from obtaining more concurrent islands by owner transfer * Handle leaving and disbanding of teams * Fix tests * Fix minor bugs or code smells. * Restore the quarantine code from deprecation. This code can stay. It checks if islands can load, and if not puts them in a trash. It does no harm. * Remove unneeded eq()'s * Fix tests
2023-09-17 00:55:52 +02:00
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After;
2018-08-08 22:13:54 +02:00
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
2 0 0 multi island (#2185) * Multi world WIP - stashing * Initial work on supporting multiple islands per player The default allowed number is 5 for now, but will be set to 1 by default. Lots more work to do on this! * More work on multi island. Fixed tests so clean compile. * Remove unused imports * Updated island go and homes command to multi island Updated tests. * Do not reload addons anymore. * Add island name when entering or leaving own island * Remove unused import * Adds island names to /island go command. * Enables more homes to be set if player has more than one island * Switch to using a set for islands and explicit primary boolean in Island * WIP * Fix bugs with the go command. * Be able to delete multiple islands, e.g. when joining a team This is not fully tested. * Do not remove all islands when a player does reset. Players can reset just the island they are on. * More fixes for go command * Fix tests * Fix @NonNull annotation * Fix home syntax listing reference for IslandDeleteHome * Fixed deletehome for multiple islands. * Fix /island command teleport to current island default home. * Remove deprecated code. * Fix tag for concurrent island setting in config.yml * Improve error when trying to make additional islands over limit * Update config.yml * Correctly assign invites for islands. * Switch to canExecute API in prep for multi-island handling * Prevent players from obtaining more concurrent islands by owner transfer * Handle leaving and disbanding of teams * Fix tests * Fix minor bugs or code smells. * Restore the quarantine code from deprecation. This code can stay. It checks if islands can load, and if not puts them in a trash. It does no harm. * Remove unneeded eq()'s * Fix tests
2023-09-17 00:55:52 +02:00
import org.mockito.Mock;
2018-08-08 22:13:54 +02:00
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 world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.commands.CompositeCommand;
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.util.Util;
2018-08-08 22:13:54 +02:00
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class })
2018-08-08 22:13:54 +02:00
public class AdminDeleteCommandTest {
@Mock
private CompositeCommand ac;
@Mock
private User user;
@Mock
private IslandsManager im;
@Mock
private PlayersManager pm;
private UUID notUUID;
private UUID uuid;
@Mock
private World world;
@Mock
private @Nullable Island island;
/**
*/
@Before
public void setUp() throws Exception {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Util
Util.setPlugin(plugin);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Settings
Settings s = mock(Settings.class);
when(s.getResetCooldown()).thenReturn(0);
when(plugin.getSettings()).thenReturn(s);
// Player
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");
User.setPlugin(plugin);
// Parent command has no aliases
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
when(ac.getTopLabel()).thenReturn("admin");
when(ac.getWorld()).thenReturn(world);
// Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm);
// Player has island to begin with
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
when(im.hasIsland(any(), any(User.class))).thenReturn(true);
// when(im.isOwner(any(),any())).thenReturn(true);
// when(im.getOwner(any(),any())).thenReturn(uuid);
when(im.getIsland(world, user)).thenReturn(island);
when(plugin.getIslands()).thenReturn(im);
// Island
when(island.getOwner()).thenReturn(uuid);
when(island.hasTeam()).thenReturn(true);
// Has team
when(im.inTeam(any(), eq(uuid))).thenReturn(true);
when(plugin.getPlayers()).thenReturn(pm);
// Server & Scheduler
BukkitScheduler sch = mock(BukkitScheduler.class);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getScheduler()).thenReturn(sch);
BukkitTask task = mock(BukkitTask.class);
when(sch.runTaskLater(any(), any(Runnable.class), any(Long.class))).thenReturn(task);
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(lm.get(any(), any())).thenReturn("mock translation");
when(plugin.getLocalesManager()).thenReturn(lm);
}
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
/**
* Test method for
* {@link AdminDeleteCommand#canExecute(User, String, java.util.List)
*/
@Test
public void testExecuteNoTarget() {
AdminDeleteCommand itl = new AdminDeleteCommand(ac);
assertFalse(itl.canExecute(user, itl.getLabel(), Collections.emptyList()));
// Show help
}
/**
* Test method for
* {@link AdminDeleteCommand#canExecute(User, String, java.util.List)
*/
@Test
public void testExecuteUnknownPlayer() {
AdminDeleteCommand itl = new AdminDeleteCommand(ac);
String[] name = { "tastybento" };
when(pm.getUUID(any())).thenReturn(null);
assertFalse(itl.canExecute(user, itl.getLabel(), Arrays.asList(name)));
verify(user).sendMessage("general.errors.unknown-player", "[name]", name[0]);
}
/**
* Test method for
* {@link AdminDeleteCommand#canExecute(User, String, java.util.List)
*/
@Test
public void testExecutePlayerNoIsland() {
AdminDeleteCommand itl = new AdminDeleteCommand(ac);
when(pm.getUUID(any())).thenReturn(notUUID);
when(im.getIsland(world, user)).thenReturn(null);
assertFalse(itl.canExecute(user, "", List.of("tastybento")));
verify(user).sendMessage(eq("general.errors.player-has-no-island"));
}
/**
* Test method for {@link AdminDeleteCommand#canExecute(User, String, java.util.List)
2018-08-08 22:13:54 +02:00
*/
@Test
public void testExecuteOwner() {
2 0 0 multi island (#2185) * Multi world WIP - stashing * Initial work on supporting multiple islands per player The default allowed number is 5 for now, but will be set to 1 by default. Lots more work to do on this! * More work on multi island. Fixed tests so clean compile. * Remove unused imports * Updated island go and homes command to multi island Updated tests. * Do not reload addons anymore. * Add island name when entering or leaving own island * Remove unused import * Adds island names to /island go command. * Enables more homes to be set if player has more than one island * Switch to using a set for islands and explicit primary boolean in Island * WIP * Fix bugs with the go command. * Be able to delete multiple islands, e.g. when joining a team This is not fully tested. * Do not remove all islands when a player does reset. Players can reset just the island they are on. * More fixes for go command * Fix tests * Fix @NonNull annotation * Fix home syntax listing reference for IslandDeleteHome * Fixed deletehome for multiple islands. * Fix /island command teleport to current island default home. * Remove deprecated code. * Fix tag for concurrent island setting in config.yml * Improve error when trying to make additional islands over limit * Update config.yml * Correctly assign invites for islands. * Switch to canExecute API in prep for multi-island handling * Prevent players from obtaining more concurrent islands by owner transfer * Handle leaving and disbanding of teams * Fix tests * Fix minor bugs or code smells. * Restore the quarantine code from deprecation. This code can stay. It checks if islands can load, and if not puts them in a trash. It does no harm. * Remove unneeded eq()'s * Fix tests
2023-09-17 00:55:52 +02:00
2021-09-18 21:25:25 +02:00
when(im.inTeam(any(),any())).thenReturn(true);
when(island.inTeam(notUUID)).thenReturn(true);
//when(im.getOwner(any(), any())).thenReturn(notUUID);
2018-08-08 22:13:54 +02:00
String[] name = {"tastybento"};
2021-09-18 21:25:25 +02:00
when(pm.getUUID(any())).thenReturn(notUUID);
2018-08-08 22:13:54 +02:00
AdminDeleteCommand itl = new AdminDeleteCommand(ac);
assertFalse(itl.canExecute(user, itl.getLabel(), Arrays.asList(name)));
2021-09-18 21:25:25 +02:00
verify(user).sendMessage("commands.admin.delete.cannot-delete-owner");
2018-08-08 22:13:54 +02:00
}
/**
* Test method for {@link AdminDeleteCommand#canExecute(User, String, java.util.List)
*/
@Test
public void testcanExecuteSuccessUUID() {
when(island.hasTeam()).thenReturn(false);
2021-09-18 21:25:25 +02:00
when(im.inTeam(any(), any())).thenReturn(false);
//when(im.getOwner(any(), any())).thenReturn(uuid);
Island is = mock(Island.class);
Location loc = mock(Location.class);
when(loc.toVector()).thenReturn(new Vector(123,123,432));
when(is.getCenter()).thenReturn(loc);
2021-09-18 21:25:25 +02:00
when(im.getIsland(any(), any(UUID.class))).thenReturn(is);
// No such name
2021-09-18 21:25:25 +02:00
when(pm.getUUID(any())).thenReturn(null);
AdminDeleteCommand itl = new AdminDeleteCommand(ac);
// Success because it's a valid UUID
assertTrue(itl.canExecute(user, itl.getLabel(), Collections.singletonList(uuid.toString())));
}
/**
* Test method for {@link AdminDeleteCommand#canExecute(User, String, java.util.List)
*/
@Test
public void testExecuteFailUUID() {
2021-09-18 21:25:25 +02:00
when(im.inTeam(any(), any())).thenReturn(false);
//when(im.getOwner(any(), any())).thenReturn(uuid);
Island is = mock(Island.class);
Location loc = mock(Location.class);
when(loc.toVector()).thenReturn(new Vector(123,123,432));
when(is.getCenter()).thenReturn(loc);
2021-09-18 21:25:25 +02:00
when(im.getIsland(any(), any(UUID.class))).thenReturn(is);
// No such name
2021-09-18 21:25:25 +02:00
when(pm.getUUID(any())).thenReturn(null);
AdminDeleteCommand itl = new AdminDeleteCommand(ac);
// Fail because it's not a UUID
assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("not-A-UUID")));
}
/**
2018-08-08 22:13:54 +02:00
* Test method for {@link AdminDeleteCommand#execute(User, String, java.util.List)
*/
@Test
public void testCanExecuteSuccess() {
when(island.hasTeam()).thenReturn(false);
2021-09-18 21:25:25 +02:00
when(im.inTeam(any(), any())).thenReturn(false);
//when(im.getOwner(any(), any())).thenReturn(uuid);
2018-08-08 22:13:54 +02:00
Island is = mock(Island.class);
Location loc = mock(Location.class);
when(loc.toVector()).thenReturn(new Vector(123,123,432));
when(is.getCenter()).thenReturn(loc);
2021-09-18 21:25:25 +02:00
when(im.getIsland(any(), any(UUID.class))).thenReturn(is);
2018-08-08 22:13:54 +02:00
String[] name = {"tastybento"};
2021-09-18 21:25:25 +02:00
when(pm.getUUID(any())).thenReturn(notUUID);
2018-08-08 22:13:54 +02:00
AdminDeleteCommand itl = new AdminDeleteCommand(ac);
assertTrue(itl.canExecute(user, itl.getLabel(), Arrays.asList(name)));
2018-08-08 22:13:54 +02:00
// Confirm
itl.execute(user, itl.getLabel(), Arrays.asList(name));
2021-09-18 21:25:25 +02:00
verify(user).sendMessage("commands.confirmation.confirm", "[seconds]", "0");
2018-08-08 22:13:54 +02:00
}
}