Test coverage for Challenges Command

This commit is contained in:
tastybento 2019-10-22 18:47:10 -07:00
parent 250d8cf639
commit 985e08e0dc
5 changed files with 302 additions and 33 deletions

39
pom.xml
View File

@ -33,7 +33,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<powermock.version>1.7.4</powermock.version> <powermock.version>2.0.2</powermock.version>
<!-- More visible way how to change dependency versions --> <!-- More visible way how to change dependency versions -->
<spigot.version>1.14.4-R0.1-SNAPSHOT</spigot.version> <spigot.version>1.14.4-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>1.7.0</bentobox.version> <bentobox.version>1.7.0</bentobox.version>
@ -129,24 +129,25 @@
<version>${spigot.version}</version> <version>${spigot.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <!-- Mockito (Unit testing) -->
<groupId>org.mockito</groupId> <dependency>
<artifactId>mockito-all</artifactId> <groupId>org.mockito</groupId>
<version>1.10.19</version> <artifactId>mockito-core</artifactId>
<scope>test</scope> <version>3.0.0</version>
</dependency> <scope>test</scope>
<dependency> </dependency>
<groupId>org.powermock</groupId> <dependency>
<artifactId>powermock-module-junit4</artifactId> <groupId>org.powermock</groupId>
<version>${powermock.version}</version> <artifactId>powermock-module-junit4</artifactId>
<scope>test</scope> <version>${powermock.version}</version>
</dependency> <scope>test</scope>
<dependency> </dependency>
<groupId>org.powermock</groupId> <dependency>
<artifactId>powermock-api-mockito</artifactId> <groupId>org.powermock</groupId>
<version>${powermock.version}</version> <artifactId>powermock-api-mockito2</artifactId>
<scope>test</scope> <version>${powermock.version}</version>
</dependency> <scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>world.bentobox</groupId> <groupId>world.bentobox</groupId>
<artifactId>bentobox</artifactId> <artifactId>bentobox</artifactId>

View File

@ -1,9 +1,7 @@
package world.bentobox.challenges.commands; package world.bentobox.challenges.commands;
import java.util.List; import java.util.List;
import java.util.Optional;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.ChallengesAddon;
@ -27,10 +25,7 @@ public class ChallengesCommand extends CompositeCommand
@Override @Override
public boolean canExecute(User user, String label, List<String> args) public boolean canExecute(User user, String label, List<String> args)
{ {
Optional<GameModeAddon> optionalAddon = this.getAddon().getPlugin().getIWM().getAddon(this.getWorld()); if (!getIWM().inWorld(getWorld())) {
if (!optionalAddon.isPresent())
{
// Not a GameMode world. // Not a GameMode world.
user.sendMessage("general.errors.wrong-world"); user.sendMessage("general.errors.wrong-world");
return false; return false;
@ -39,13 +34,12 @@ public class ChallengesCommand extends CompositeCommand
if (!((ChallengesAddon) this.getAddon()).getChallengesManager().hasAnyChallengeData(this.getWorld())) if (!((ChallengesAddon) this.getAddon()).getChallengesManager().hasAnyChallengeData(this.getWorld()))
{ {
// Do not open gui if there is no challenges. // Do not open gui if there is no challenges.
this.getAddon().logError("There are no challenges set up in " + this.getWorld() + "!");
this.getAddon().getLogger().severe("There are no challenges set up in " + this.getWorld() + "!");
// Show admin better explanation. // Show admin better explanation.
if (user.isOp() || user.hasPermission(this.getPermissionPrefix() + "admin.challenges")) if (user.isOp() || user.hasPermission(this.getPermissionPrefix() + "admin.challenges"))
{ {
String topLabel = optionalAddon.get().getAdminCommand().orElseGet(this::getParent).getTopLabel(); String topLabel = getIWM().getAddon(this.getWorld()).get().getAdminCommand().orElseGet(this::getParent).getTopLabel();
user.sendMessage("challenges.errors.no-challenges-admin", "[command]", topLabel + " challenges"); user.sendMessage("challenges.errors.no-challenges-admin", "[command]", topLabel + " challenges");
} }
else else
@ -56,7 +50,7 @@ public class ChallengesCommand extends CompositeCommand
return false; return false;
} }
if (this.getPlugin().getIslands().getIsland(this.getWorld(), user.getUniqueId()) == null) if (this.getIslands().getIsland(this.getWorld(), user) == null)
{ {
// Do not open gui if there is no island for this player. // Do not open gui if there is no island for this player.
user.sendMessage("general.errors.no-island"); user.sendMessage("general.errors.no-island");

View File

@ -78,7 +78,7 @@ public class ChallengesGUI extends CommonGUI
// Do not open gui if there is no challenges. // Do not open gui if there is no challenges.
if (!this.challengesManager.hasAnyChallengeData(this.world)) if (!this.challengesManager.hasAnyChallengeData(this.world))
{ {
this.addon.getLogger().severe("There are no challenges set up!"); this.addon.logError("There are no challenges set up!");
this.user.sendMessage("challenges.errors.no-challenges"); this.user.sendMessage("challenges.errors.no-challenges");
return; return;
} }

View File

@ -1,6 +1,3 @@
/**
*
*/
package world.bentobox.challenges; package world.bentobox.challenges;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;

View File

@ -0,0 +1,277 @@
package world.bentobox.challenges.commands;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
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.Collections;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.ItemMeta;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.After;
import org.junit.Before;
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 world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
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.challenges.ChallengesAddon;
import world.bentobox.challenges.ChallengesManager;
import world.bentobox.challenges.config.Settings;
import world.bentobox.challenges.config.SettingsUtils.VisibilityMode;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, ChatColor.class})
public class ChallengesCommandTest {
@Mock
private CompositeCommand ic;
private UUID uuid;
@Mock
private User user;
@Mock
private IslandsManager im;
@Mock
private Island island;
@Mock
private ChallengesAddon addon;
private ChallengesCommand cc;
@Mock
private World world;
@Mock
private ChallengesManager chm;
@Mock
private IslandWorldManager iwm;
@Mock
private GameModeAddon gameModeAddon;
@Mock
private Settings settings;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
User.setPlugin(plugin);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Addon
when(ic.getAddon()).thenReturn(addon);
when(ic.getPermissionPrefix()).thenReturn("bskyblock.");
when(ic.getLabel()).thenReturn("island");
when(ic.getTopLabel()).thenReturn("island");
when(ic.getWorld()).thenReturn(world);
when(ic.getTopLabel()).thenReturn("bsb");
// IWM friendly name
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
when(iwm.inWorld(any(World.class))).thenReturn(true);
Optional<GameModeAddon> optionalAddon = Optional.of(gameModeAddon);
when(iwm.getAddon(any())).thenReturn(optionalAddon);
when(plugin.getIWM()).thenReturn(iwm);
// Game Mode Addon
@NonNull
Optional<CompositeCommand> optionalAdmin = Optional.of(ic);
when(gameModeAddon.getAdminCommand()).thenReturn(optionalAdmin);
// World
when(world.toString()).thenReturn("world");
// Player
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
when(user.getPermissionValue(anyString(), anyInt())).thenReturn(-1);
when(user.isPlayer()).thenReturn(true);
// Mock item factory (for itemstacks)
PowerMockito.mockStatic(Bukkit.class);
ItemFactory itemFactory = mock(ItemFactory.class);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
ItemMeta itemMeta = mock(ItemMeta.class);
when(itemFactory.getItemMeta(any())).thenReturn(itemMeta);
// Addon
when(addon.getChallengesManager()).thenReturn(chm);
when(chm.getAllChallengeLevelStatus(any(), any())).thenReturn(Collections.emptyList());
// Challenges exist
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(true);
// ChatColor
PowerMockito.mockStatic(ChatColor.class);
when(ChatColor.translateAlternateColorCodes(any(char.class), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
// Settings
when(addon.getChallengesSettings()).thenReturn(settings);
when(settings.getVisibilityMode()).thenReturn(VisibilityMode.VISIBLE);
// Island
when(plugin.getIslands()).thenReturn(im);
when(im.getIsland(any(), any(User.class))).thenReturn(island);
// Command under test
cc = new ChallengesCommand(addon, ic);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
/**
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteWrongWorld() {
when(iwm.inWorld(any(World.class))).thenReturn(false);
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
verify(user).sendMessage("general.errors.wrong-world");
}
/**
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteNoChallenges() {
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(false);
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
verify(addon).logError("There are no challenges set up in world!");
verify(user).sendMessage("challenges.errors.no-challenges");
}
/**
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteNoChallengesOp() {
when(user.isOp()).thenReturn(true);
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(false);
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
verify(addon).logError("There are no challenges set up in world!");
verify(user).sendMessage("challenges.errors.no-challenges-admin", "[command]", "bsb challenges");
verify(user, never()).sendMessage("challenges.errors.no-challenges");
}
/**
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteNoChallengesHasPerm() {
when(user.hasPermission(anyString())).thenReturn(true);
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(false);
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
verify(addon).logError("There are no challenges set up in world!");
verify(user).sendMessage("challenges.errors.no-challenges-admin", "[command]", "bsb challenges");
verify(user, never()).sendMessage("challenges.errors.no-challenges");
}
/**
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteNoAdminCommand() {
when(gameModeAddon.getAdminCommand()).thenReturn(Optional.empty());
when(user.isOp()).thenReturn(true);
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(false);
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
verify(addon).logError("There are no challenges set up in world!");
verify(user).sendMessage("challenges.errors.no-challenges-admin", "[command]", "bsb challenges");
verify(user, never()).sendMessage("challenges.errors.no-challenges");
}
/**
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteNoIsland() {
when(im.getIsland(any(), any(User.class))).thenReturn(null);
assertFalse(cc.canExecute(user, "challenges", Collections.emptyList()));
verify(user).sendMessage("general.errors.no-island");
}
/**
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteSuccess() {
assertTrue(cc.canExecute(user, "challenges", Collections.emptyList()));
verify(user, never()).sendMessage(anyString());
}
/**
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringConsole() {
User console = mock(User.class);
assertFalse(cc.execute(console, "challenges", Collections.emptyList()));
verify(console).sendMessage(eq("commands.help.header"), eq(TextVariables.LABEL), eq("BSkyBlock"));
}
/**
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringUser() {
assertTrue(cc.execute(user, "challenges", Collections.emptyList()));
}
/**
* Test method for {@link world.bentobox.challenges.commands.ChallengesCommand#setup()}.
*/
@Test
public void testSetup() {
assertEquals("bskyblock." + ChallengesCommand.CHALLENGE_COMMAND, cc.getPermission());
assertEquals("challenges.commands.user.main.parameters", cc.getParameters());
assertEquals("challenges.commands.user.main.description", cc.getDescription());
assertTrue(cc.isOnlyPlayer());
// CompleteChallengeCommand
assertEquals(1, cc.getSubCommands(true).size());
}
}