mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-25 20:25:16 +01:00
Added tests
This commit is contained in:
parent
87b147ace4
commit
95b7ed137f
@ -2,11 +2,13 @@ package world.bentobox.bentobox.api.commands.admin;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
|
||||
@ -19,6 +21,10 @@ import world.bentobox.bentobox.util.Util;
|
||||
|
||||
public class AdminRegisterCommand extends ConfirmableCommand {
|
||||
|
||||
private Island island;
|
||||
private Location closestIsland;
|
||||
private @Nullable UUID targetUUID;
|
||||
|
||||
public AdminRegisterCommand(CompositeCommand parent) {
|
||||
super(parent, "register");
|
||||
}
|
||||
@ -32,20 +38,20 @@ public class AdminRegisterCommand extends ConfirmableCommand {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(User user, String label, List<String> args) {
|
||||
public boolean canExecute(User user, String label, List<String> args) {
|
||||
// If args are not right, show help
|
||||
if (args.size() != 1) {
|
||||
showHelp(this, user);
|
||||
return false;
|
||||
}
|
||||
// Get target
|
||||
UUID targetUUID = Util.getUUID(args.get(0));
|
||||
targetUUID = Util.getUUID(args.get(0));
|
||||
if (targetUUID == null) {
|
||||
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
|
||||
return false;
|
||||
}
|
||||
// Check if this spot is still being deleted
|
||||
Location closestIsland = Util.getClosestIsland(user.getLocation());
|
||||
closestIsland = Util.getClosestIsland(user.getLocation());
|
||||
if (getPlugin().getIslandDeletionManager().inDeletion(closestIsland)) {
|
||||
user.sendMessage("commands.admin.register.in-deletion");
|
||||
return false;
|
||||
@ -55,25 +61,38 @@ public class AdminRegisterCommand extends ConfirmableCommand {
|
||||
if (opIsland.isEmpty()) {
|
||||
// Reserve spot
|
||||
this.askConfirmation(user, user.getTranslation("commands.admin.register.no-island-here"),
|
||||
() -> reserve(user, args.get(0), targetUUID, closestIsland));
|
||||
() -> reserve(user, args.get(0)));
|
||||
return false;
|
||||
}
|
||||
Island island = opIsland.get();
|
||||
island = opIsland.get();
|
||||
if (targetUUID.equals(island.getOwner())) {
|
||||
user.sendMessage("commands.admin.register.already-owned");
|
||||
return false;
|
||||
}
|
||||
// Check if island is spawn
|
||||
if (island.isSpawn()) {
|
||||
askConfirmation(user, user.getTranslation("commands.admin.register.island-is-spawn"), () -> register(user, args.get(0), targetUUID, island, closestIsland));
|
||||
askConfirmation(user, user.getTranslation("commands.admin.register.island-is-spawn"),
|
||||
() -> register(user, args.get(0)));
|
||||
return false;
|
||||
}
|
||||
|
||||
register(user, args.get(0), targetUUID, island, closestIsland);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void reserve(User user, String targetName, UUID targetUUID, Location closestIsland) {
|
||||
@Override
|
||||
public boolean execute(User user, String label, List<String> args) {
|
||||
register(user, args.get(0));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reserve a spot for a target
|
||||
* @param user user doing the reserving
|
||||
* @param targetName target name
|
||||
*/
|
||||
void reserve(User user, String targetName) {
|
||||
Objects.requireNonNull(closestIsland);
|
||||
Objects.requireNonNull(targetUUID);
|
||||
// Island does not exist - this is a reservation
|
||||
// Make island here
|
||||
Island i = getIslands().createIsland(closestIsland, targetUUID);
|
||||
@ -91,20 +110,29 @@ public class AdminRegisterCommand extends ConfirmableCommand {
|
||||
.involvedPlayer(targetUUID).admin(true).build();
|
||||
}
|
||||
|
||||
private void register(User user, String targetName, UUID targetUUID, Island i, Location closestIsland) {
|
||||
/**
|
||||
* Register the island to a target
|
||||
* @param user user doing the registering
|
||||
* @param targetName name of target
|
||||
*/
|
||||
void register(User user, String targetName) {
|
||||
Objects.requireNonNull(closestIsland);
|
||||
Objects.requireNonNull(targetUUID);
|
||||
Objects.requireNonNull(island);
|
||||
// Island exists
|
||||
getIslands().setOwner(user, targetUUID, i, RanksManager.VISITOR_RANK);
|
||||
if (i.isSpawn()) {
|
||||
getIslands().clearSpawn(i.getWorld());
|
||||
getIslands().setOwner(user, targetUUID, island, RanksManager.VISITOR_RANK);
|
||||
if (island.isSpawn()) {
|
||||
getIslands().clearSpawn(island.getWorld());
|
||||
}
|
||||
user.sendMessage("commands.admin.register.registered-island", TextVariables.XYZ,
|
||||
Util.xyz(i.getCenter().toVector()), TextVariables.NAME, targetName);
|
||||
Util.xyz(island.getCenter().toVector()), TextVariables.NAME, targetName);
|
||||
user.sendMessage("general.success");
|
||||
// Build and call event
|
||||
IslandEvent.builder().island(i).location(i.getCenter()).reason(IslandEvent.Reason.REGISTERED)
|
||||
IslandEvent.builder().island(island).location(island.getCenter()).reason(IslandEvent.Reason.REGISTERED)
|
||||
.involvedPlayer(targetUUID).admin(true).build();
|
||||
IslandEvent.builder().island(i).involvedPlayer(targetUUID).admin(true).reason(IslandEvent.Reason.RANK_CHANGE)
|
||||
.rankChange(RanksManager.VISITOR_RANK, RanksManager.OWNER_RANK).build();
|
||||
IslandEvent.builder().island(island).involvedPlayer(targetUUID).admin(true)
|
||||
.reason(IslandEvent.Reason.RANK_CHANGE).rankChange(RanksManager.VISITOR_RANK, RanksManager.OWNER_RANK)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -3,19 +3,24 @@ package world.bentobox.bentobox.api.commands.admin;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
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.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
@ -26,12 +31,14 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
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 world.bentobox.bentobox.BentoBox;
|
||||
import world.bentobox.bentobox.Settings;
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
@ -42,6 +49,7 @@ 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;
|
||||
import world.bentobox.bentobox.util.Util;
|
||||
|
||||
/**
|
||||
@ -49,7 +57,7 @@ import world.bentobox.bentobox.util.Util;
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class })
|
||||
@PrepareForTest({ Bukkit.class, BentoBox.class, Util.class })
|
||||
public class AdminRegisterCommandTest {
|
||||
|
||||
@Mock
|
||||
@ -61,10 +69,19 @@ public class AdminRegisterCommandTest {
|
||||
private IslandsManager im;
|
||||
@Mock
|
||||
private PlayersManager pm;
|
||||
@Mock
|
||||
private Island is;
|
||||
@Mock
|
||||
private Location loc;
|
||||
|
||||
private UUID notUUID;
|
||||
|
||||
private IslandDeletionManager idm;
|
||||
private AdminRegisterCommand itl;
|
||||
@Mock
|
||||
private World world;
|
||||
@Mock
|
||||
private Block block;
|
||||
|
||||
/**
|
||||
*/
|
||||
@ -75,6 +92,10 @@ public class AdminRegisterCommandTest {
|
||||
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
||||
Util.setPlugin(plugin);
|
||||
|
||||
Settings settings = new Settings();
|
||||
// Settings
|
||||
when(plugin.getSettings()).thenReturn(settings);
|
||||
|
||||
// Command manager
|
||||
CommandsManager cm = mock(CommandsManager.class);
|
||||
when(plugin.getCommandsManager()).thenReturn(cm);
|
||||
@ -91,8 +112,15 @@ public class AdminRegisterCommandTest {
|
||||
when(user.getUniqueId()).thenReturn(uuid);
|
||||
when(user.getPlayer()).thenReturn(p);
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
when(user.getTranslation(anyString()))
|
||||
.thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
|
||||
User.getInstance(p);
|
||||
User.setPlugin(plugin);
|
||||
|
||||
// Util
|
||||
PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS);
|
||||
when(Util.getUUID("tastybento")).thenReturn(uuid);
|
||||
|
||||
// Parent command has no aliases
|
||||
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
|
||||
|
||||
@ -131,6 +159,14 @@ public class AdminRegisterCommandTest {
|
||||
PluginManager pim = mock(PluginManager.class);
|
||||
when(Bukkit.getPluginManager()).thenReturn(pim);
|
||||
|
||||
// Island
|
||||
when(is.getWorld()).thenReturn(world);
|
||||
when(is.getCenter()).thenReturn(loc);
|
||||
when(im.createIsland(any(), eq(uuid))).thenReturn(is);
|
||||
when(loc.getBlock()).thenReturn(block);
|
||||
|
||||
// DUT
|
||||
itl = new AdminRegisterCommand(ac);
|
||||
}
|
||||
|
||||
@After
|
||||
@ -141,81 +177,64 @@ public class AdminRegisterCommandTest {
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link AdminRegisterCommand#execute(org.bukkit.command.CommandSender, String, String[])}.
|
||||
* {@link AdminRegisterCommand#canExecute(org.bukkit.command.CommandSender, String, String[])}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteNoTarget() {
|
||||
AdminRegisterCommand itl = new AdminRegisterCommand(ac);
|
||||
assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>()));
|
||||
public void testCanExecuteNoTarget() {
|
||||
assertFalse(itl.canExecute(user, itl.getLabel(), new ArrayList<>()));
|
||||
// Show help
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link AdminRegisterCommand#execute(org.bukkit.command.CommandSender, String, String[])}.
|
||||
* {@link AdminRegisterCommand#canExecute(org.bukkit.command.CommandSender, String, String[])}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUnknownPlayer() {
|
||||
AdminRegisterCommand itl = new AdminRegisterCommand(ac);
|
||||
public void testCanExecuteUnknownPlayer() {
|
||||
when(pm.getUUID(any())).thenReturn(null);
|
||||
assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento")));
|
||||
verify(user).sendMessage(eq("general.errors.unknown-player"), eq("[name]"), eq("tastybento"));
|
||||
assertFalse(itl.canExecute(user, itl.getLabel(), List.of("tastybento2")));
|
||||
verify(user).sendMessage("general.errors.unknown-player", TextVariables.NAME, "tastybento2");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link AdminRegisterCommand#execute(org.bukkit.command.CommandSender, String, String[])}.
|
||||
* {@link AdminRegisterCommand#canExecute(org.bukkit.command.CommandSender, String, String[])}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecutePlayerHasIsland() {
|
||||
AdminRegisterCommand itl = new AdminRegisterCommand(ac);
|
||||
when(pm.getUUID(any())).thenReturn(notUUID);
|
||||
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
|
||||
when(im.inTeam(any(), any())).thenReturn(false);
|
||||
assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento")));
|
||||
verify(user).sendMessage(eq("general.errors.player-has-island"));
|
||||
public void testCanExecuteNoIsland() {
|
||||
when(im.getIslandAt(any())).thenReturn(Optional.empty());
|
||||
assertFalse(itl.canExecute(user, itl.getLabel(), List.of("tastybento")));
|
||||
verify(user).getTranslation("commands.admin.register.no-island-here");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link AdminRegisterCommand#execute(org.bukkit.command.CommandSender, String, String[])}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteInTeam() {
|
||||
when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
|
||||
when(im.inTeam(any(), any())).thenReturn(true);
|
||||
when(pm.getUUID(any())).thenReturn(notUUID);
|
||||
AdminRegisterCommand itl = new AdminRegisterCommand(ac);
|
||||
assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento")));
|
||||
verify(user).sendMessage("commands.admin.register.cannot-register-team-player");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link AdminRegisterCommand#execute(org.bukkit.command.CommandSender, String, String[])}.
|
||||
* Test method for {@link AdminRegisterCommand#canExecute(org.bukkit.command.CommandSender, String, String[])}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteAlreadyOwnedIsland() {
|
||||
public void testCanExecuteAlreadyOwnedIsland() {
|
||||
when(im.inTeam(any(), any())).thenReturn(false);
|
||||
when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
|
||||
when(pm.getUUID(any())).thenReturn(notUUID);
|
||||
Location loc = mock(Location.class);
|
||||
|
||||
when(loc.toVector()).thenReturn(new Vector(1, 2, 3));
|
||||
// Island has owner
|
||||
Island is = mock(Island.class);
|
||||
when(is.getOwner()).thenReturn(uuid);
|
||||
when(is.isOwned()).thenReturn(true);
|
||||
when(is.getCenter()).thenReturn(loc);
|
||||
Optional<Island> opi = Optional.of(is);
|
||||
when(im.getIslandAt(any())).thenReturn(opi);
|
||||
when(user.getLocation()).thenReturn(loc);
|
||||
AdminRegisterCommand itl = new AdminRegisterCommand(ac);
|
||||
assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento")));
|
||||
|
||||
assertFalse(itl.canExecute(user, itl.getLabel(), List.of("tastybento")));
|
||||
verify(user).sendMessage("commands.admin.register.already-owned");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link AdminRegisterCommand#execute(org.bukkit.command.CommandSender, String, String[])}.
|
||||
* Test method for {@link AdminRegisterCommand#canExecute(org.bukkit.command.CommandSender, String, String[])}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteInDeletionIsland() {
|
||||
public void testCanExecuteInDeletionIsland() {
|
||||
when(idm.inDeletion(any())).thenReturn(true);
|
||||
when(im.inTeam(any(), any())).thenReturn(false);
|
||||
when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
|
||||
@ -223,25 +242,20 @@ public class AdminRegisterCommandTest {
|
||||
Location loc = mock(Location.class);
|
||||
|
||||
// Island has owner
|
||||
Island is = mock(Island.class);
|
||||
when(is.getOwner()).thenReturn(uuid);
|
||||
Optional<Island> opi = Optional.of(is);
|
||||
when(im.getIslandAt(any())).thenReturn(opi);
|
||||
when(user.getLocation()).thenReturn(loc);
|
||||
AdminRegisterCommand itl = new AdminRegisterCommand(ac);
|
||||
assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento")));
|
||||
|
||||
assertFalse(itl.canExecute(user, itl.getLabel(), List.of("tastybento")));
|
||||
verify(user).sendMessage("commands.admin.register.in-deletion");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link AdminRegisterCommand#execute(org.bukkit.command.CommandSender, String, String[])}.
|
||||
* Test method for {@link AdminRegisterCommand#canExecute(org.bukkit.command.CommandSender, String, String[])}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteSuccess() {
|
||||
when(im.inTeam(any(), any())).thenReturn(false);
|
||||
when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
|
||||
Island is = mock(Island.class);
|
||||
Location loc = mock(Location.class);
|
||||
public void testCanExecuteSuccess() {
|
||||
when(loc.toVector()).thenReturn(new Vector(123,123,432));
|
||||
when(is.getCenter()).thenReturn(loc);
|
||||
when(im.getIsland(any(), any(UUID.class))).thenReturn(is);
|
||||
@ -250,11 +264,48 @@ public class AdminRegisterCommandTest {
|
||||
when(user.getLocation()).thenReturn(loc);
|
||||
when(pm.getUUID(any())).thenReturn(notUUID);
|
||||
|
||||
AdminRegisterCommand itl = new AdminRegisterCommand(ac);
|
||||
assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento")));
|
||||
// Add other verifications
|
||||
verify(user).sendMessage(eq("commands.admin.register.registered-island"), eq(TextVariables.XYZ), eq("123,123,432"), eq("[name]"), eq("tastybento"));
|
||||
verify(user).sendMessage(eq("general.success"));
|
||||
assertTrue(itl.canExecute(user, itl.getLabel(), List.of("tastybento")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link AdminRegisterCommand#register(User, String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRegister() {
|
||||
testCanExecuteSuccess();
|
||||
when(is.isSpawn()).thenReturn(true);
|
||||
itl.register(user, "tastybento");
|
||||
verify(im).setOwner(user, uuid, is, RanksManager.VISITOR_RANK);
|
||||
verify(im).clearSpawn(world);
|
||||
verify(user).sendMessage("commands.admin.register.registered-island", TextVariables.XYZ, "", TextVariables.NAME,
|
||||
"tastybento");
|
||||
verify(user).sendMessage("general.success");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link AdminRegisterCommand#reserve(User, String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testReserveCannotMakeIsland() {
|
||||
when(im.createIsland(any(), eq(uuid))).thenReturn(null);
|
||||
testCanExecuteNoIsland();
|
||||
itl.reserve(user, "tastybento");
|
||||
verify(im).createIsland(any(), eq(uuid));
|
||||
verify(user).sendMessage("commands.admin.register.cannot-make-island");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link AdminRegisterCommand#reserve(User, String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testReserveCanMakeIsland() {
|
||||
testCanExecuteNoIsland();
|
||||
itl.reserve(user, "tastybento");
|
||||
verify(im).createIsland(any(), eq(uuid));
|
||||
verify(user, never()).sendMessage("commands.admin.register.cannot-make-island");
|
||||
verify(block).setType(Material.BEDROCK);
|
||||
verify(user).sendMessage("commands.admin.register.reserved-island", TextVariables.XYZ, "", TextVariables.NAME,
|
||||
"tastybento");
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user