mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-02-10 09:21:31 +01:00
Refactored setowner command and added test class
This commit is contained in:
parent
3a43c40634
commit
cfad9879a5
@ -1,12 +1,9 @@
|
||||
package world.bentobox.bentobox.api.commands.island.team;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
import world.bentobox.bentobox.api.events.island.IslandEvent;
|
||||
@ -35,9 +32,13 @@ public class IslandTeamSetownerCommand extends CompositeCommand {
|
||||
public boolean execute(User user, String label, List<String> args) {
|
||||
UUID playerUUID = user.getUniqueId();
|
||||
// Can use if in a team
|
||||
boolean inTeam = getPlugin().getIslands().inTeam(getWorld(), playerUUID);
|
||||
boolean inTeam = getIslands().inTeam(getWorld(), playerUUID);
|
||||
if (!inTeam) {
|
||||
user.sendMessage("general.errors.no-team");
|
||||
return false;
|
||||
}
|
||||
UUID ownerUUID = getOwner(getWorld(), user);
|
||||
if (!(inTeam && ownerUUID.equals(playerUUID))) {
|
||||
if (ownerUUID == null || !ownerUUID.equals(playerUUID)) {
|
||||
user.sendMessage("general.errors.not-owner");
|
||||
return false;
|
||||
}
|
||||
@ -51,27 +52,23 @@ public class IslandTeamSetownerCommand extends CompositeCommand {
|
||||
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
|
||||
return false;
|
||||
}
|
||||
if (!getIslands().inTeam(getWorld(), playerUUID)) {
|
||||
user.sendMessage("general.errors.no-team");
|
||||
return false;
|
||||
}
|
||||
if (targetUUID.equals(playerUUID)) {
|
||||
user.sendMessage("commands.island.team.setowner.errors.cant-transfer-to-yourself");
|
||||
return false;
|
||||
}
|
||||
if (!getPlugin().getIslands().getMembers(getWorld(), playerUUID).contains(targetUUID)) {
|
||||
if (!getIslands().getMembers(getWorld(), playerUUID).contains(targetUUID)) {
|
||||
user.sendMessage("commands.island.team.setowner.errors.target-is-not-member");
|
||||
return false;
|
||||
}
|
||||
// Fire event so add-ons can run commands, etc.
|
||||
Island island = getIslands().getIsland(getWorld(), playerUUID);
|
||||
Island island = getIslands().getIsland(getWorld(), user);
|
||||
// Fire event so add-ons can run commands, etc.
|
||||
IslandBaseEvent e = TeamEvent.builder()
|
||||
.island(getIslands().getIsland(getWorld(), user.getUniqueId()))
|
||||
.island(island)
|
||||
.reason(TeamEvent.Reason.SETOWNER)
|
||||
.involvedPlayer(targetUUID)
|
||||
.build();
|
||||
if (e.getNewEvent().map(IslandBaseEvent::isCancelled).orElse(e.isCancelled())) {
|
||||
if (e.isCancelled()) {
|
||||
return false;
|
||||
}
|
||||
getIslands().setOwner(getWorld(), user, targetUUID);
|
||||
@ -97,12 +94,8 @@ public class IslandTeamSetownerCommand extends CompositeCommand {
|
||||
|
||||
@Override
|
||||
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
|
||||
List<String> options = new ArrayList<>();
|
||||
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
|
||||
for (UUID member : getPlugin().getIslands().getMembers(getWorld(), user.getUniqueId())) {
|
||||
options.add(Bukkit.getServer().getOfflinePlayer(member).getName());
|
||||
}
|
||||
return Optional.of(Util.tabLimit(options, lastArg));
|
||||
return Optional.of(Util.tabLimit(getIslands().getMembers(getWorld(), user.getUniqueId()).stream().map(getPlayers()::getName).toList(), lastArg));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,279 @@
|
||||
package world.bentobox.bentobox.api.commands.island.team;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
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.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Optional;
|
||||
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.eclipse.jdt.annotation.Nullable;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
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.localization.TextVariables;
|
||||
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.PlayersManager;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({Bukkit.class, BentoBox.class, User.class })
|
||||
public class IslandTeamSetownerCommandTest {
|
||||
|
||||
@Mock
|
||||
private CompositeCommand ic;
|
||||
private UUID uuid;
|
||||
@Mock
|
||||
private User user;
|
||||
@Mock
|
||||
private Settings s;
|
||||
@Mock
|
||||
private IslandsManager im;
|
||||
@Mock
|
||||
private IslandWorldManager iwm;
|
||||
@Mock
|
||||
private Player player;
|
||||
@Mock
|
||||
private CompositeCommand subCommand;
|
||||
@Mock
|
||||
private PlayersManager pm;
|
||||
@Mock
|
||||
private World world;
|
||||
private IslandTeamSetownerCommand its;
|
||||
|
||||
/**
|
||||
* @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
|
||||
when(s.getResetCooldown()).thenReturn(0);
|
||||
when(plugin.getSettings()).thenReturn(s);
|
||||
|
||||
// Player
|
||||
// Sometimes use Mockito.withSettings().verboseLogging()
|
||||
when(user.isOp()).thenReturn(false);
|
||||
uuid = UUID.randomUUID();
|
||||
when(user.getUniqueId()).thenReturn(uuid);
|
||||
when(user.getPlayer()).thenReturn(player);
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
|
||||
// Parent command has no aliases
|
||||
ic = mock(CompositeCommand.class);
|
||||
when(ic.getSubCommandAliases()).thenReturn(new HashMap<>());
|
||||
Optional<CompositeCommand> optionalCommand = Optional.of(subCommand);
|
||||
when(ic.getSubCommand(Mockito.anyString())).thenReturn(optionalCommand);
|
||||
when(ic.getWorld()).thenReturn(world);
|
||||
|
||||
// Player has island to begin with
|
||||
when(im.hasIsland(any(), Mockito.any(UUID.class))).thenReturn(true);
|
||||
when(im.isOwner(any(), any())).thenReturn(true);
|
||||
when(plugin.getIslands()).thenReturn(im);
|
||||
|
||||
// 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);
|
||||
|
||||
// Island World Manager
|
||||
when(plugin.getIWM()).thenReturn(iwm);
|
||||
|
||||
// Plugin Manager
|
||||
PluginManager pim = mock(PluginManager.class);
|
||||
when(Bukkit.getPluginManager()).thenReturn(pim);
|
||||
|
||||
// Island
|
||||
Island island = mock(Island.class);
|
||||
when(island.getUniqueId()).thenReturn("uniqueid");
|
||||
when(im.getIsland(any(), Mockito.any(User.class))).thenReturn(island);
|
||||
|
||||
// Class under test
|
||||
its = new IslandTeamSetownerCommand(ic);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#setup()}.
|
||||
*/
|
||||
@Test
|
||||
public void testSetup() {
|
||||
assertEquals("island.team.setowner", its.getPermission());
|
||||
assertTrue(its.isOnlyPlayer());
|
||||
assertEquals("commands.island.team.setowner.parameters", its.getParameters());
|
||||
assertEquals("commands.island.team.setowner.description", its.getDescription());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUserStringListOfStringNullOwner() {
|
||||
when(im.getOwner(any(), any())).thenReturn(null);
|
||||
assertFalse(its.execute(user, "", Collections.emptyList()));
|
||||
verify(user).sendMessage("general.errors.not-owner");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUserStringListOfStringNotInTeam() {
|
||||
when(im.inTeam(any(), any())).thenReturn(false);
|
||||
assertFalse(its.execute(user, "", Collections.emptyList()));
|
||||
verify(user).sendMessage("general.errors.no-team");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUserStringListOfStringNotOwner() {
|
||||
when(im.inTeam(any(), any())).thenReturn(true);
|
||||
when(im.getOwner(any(), any())).thenReturn(UUID.randomUUID());
|
||||
assertFalse(its.execute(user, "", Collections.emptyList()));
|
||||
verify(user).sendMessage("general.errors.not-owner");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUserStringListOfStringShowHelp() {
|
||||
when(im.inTeam(any(), any())).thenReturn(true);
|
||||
when(im.getOwner(any(), any())).thenReturn(uuid);
|
||||
assertFalse(its.execute(user, "", Collections.emptyList()));
|
||||
verify(user).sendMessage("commands.help.header","[label]", null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUserStringListOfStringUnknownPlayer() {
|
||||
when(im.inTeam(any(), any())).thenReturn(true);
|
||||
when(im.getOwner(any(), any())).thenReturn(uuid);
|
||||
when(pm.getUUID(anyString())).thenReturn(null);
|
||||
assertFalse(its.execute(user, "", Collections.singletonList("tastybento")));
|
||||
verify(user).sendMessage("general.errors.unknown-player", TextVariables.NAME, "tastybento");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUserStringListOfStringSamePlayer() {
|
||||
when(im.inTeam(any(), any())).thenReturn(true);
|
||||
when(im.getOwner(any(), any())).thenReturn(uuid);
|
||||
when(pm.getUUID(anyString())).thenReturn(uuid);
|
||||
assertFalse(its.execute(user, "", Collections.singletonList("tastybento")));
|
||||
verify(user).sendMessage("commands.island.team.setowner.errors.cant-transfer-to-yourself");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUserStringListOfStringTargetNotInTeam() {
|
||||
when(im.inTeam(any(), any())).thenReturn(true);
|
||||
when(im.getOwner(any(), any())).thenReturn(uuid);
|
||||
when(pm.getUUID(anyString())).thenReturn(UUID.randomUUID());
|
||||
when(im.getMembers(any(), any())).thenReturn(Collections.singleton(uuid));
|
||||
assertFalse(its.execute(user, "", Collections.singletonList("tastybento")));
|
||||
verify(user).sendMessage("commands.island.team.setowner.errors.target-is-not-member");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUserStringListOfStringSuccess() {
|
||||
when(im.inTeam(any(), any())).thenReturn(true);
|
||||
when(im.getOwner(any(), any())).thenReturn(uuid);
|
||||
UUID target = UUID.randomUUID();
|
||||
when(pm.getUUID(anyString())).thenReturn(target);
|
||||
when(im.getMembers(any(), any())).thenReturn(Collections.singleton(target));
|
||||
@Nullable
|
||||
Island island = mock(Island.class);
|
||||
when(im.getIsland(any(), any(User.class))).thenReturn(island);
|
||||
|
||||
assertTrue(its.execute(user, "", Collections.singletonList("tastybento")));
|
||||
verify(im).setOwner(any(), eq(user), eq(target));
|
||||
verify(im).save(island);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testTabCompleteUserStringListOfString() {
|
||||
assertTrue(its.tabComplete(user, "", Collections.emptyList()).get().isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testTabCompleteUserStringListOfStringUnknown() {
|
||||
assertTrue(its.tabComplete(user, "ta", Collections.emptyList()).get().isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testTabCompleteUserStringListOfStringMember() {
|
||||
UUID target = UUID.randomUUID();
|
||||
when(pm.getName(any())).thenReturn("tastybento");
|
||||
when(im.getMembers(any(), any())).thenReturn(Collections.singleton(target));
|
||||
assertEquals("tastybento", its.tabComplete(user, "", Collections.emptyList()).get().get(0));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user