From 985e08e0dc3fd63ce60cc0158b6935bcf6184748 Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 22 Oct 2019 18:47:10 -0700 Subject: [PATCH] Test coverage for Challenges Command --- pom.xml | 39 +-- .../commands/ChallengesCommand.java | 14 +- .../challenges/panel/user/ChallengesGUI.java | 2 +- .../challenges/ChallengesAddonTest.java | 3 - .../commands/ChallengesCommandTest.java | 277 ++++++++++++++++++ 5 files changed, 302 insertions(+), 33 deletions(-) create mode 100644 src/test/java/world/bentobox/challenges/commands/ChallengesCommandTest.java diff --git a/pom.xml b/pom.xml index 6ca353c..2cba69d 100644 --- a/pom.xml +++ b/pom.xml @@ -33,7 +33,7 @@ UTF-8 UTF-8 1.8 - 1.7.4 + 2.0.2 1.14.4-R0.1-SNAPSHOT 1.7.0 @@ -129,24 +129,25 @@ ${spigot.version} provided - - org.mockito - mockito-all - 1.10.19 - test - - - org.powermock - powermock-module-junit4 - ${powermock.version} - test - - - org.powermock - powermock-api-mockito - ${powermock.version} - test - + + + org.mockito + mockito-core + 3.0.0 + test + + + org.powermock + powermock-module-junit4 + ${powermock.version} + test + + + org.powermock + powermock-api-mockito2 + ${powermock.version} + test + world.bentobox bentobox diff --git a/src/main/java/world/bentobox/challenges/commands/ChallengesCommand.java b/src/main/java/world/bentobox/challenges/commands/ChallengesCommand.java index b26a194..b378e14 100644 --- a/src/main/java/world/bentobox/challenges/commands/ChallengesCommand.java +++ b/src/main/java/world/bentobox/challenges/commands/ChallengesCommand.java @@ -1,9 +1,7 @@ package world.bentobox.challenges.commands; 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.user.User; import world.bentobox.challenges.ChallengesAddon; @@ -27,10 +25,7 @@ public class ChallengesCommand extends CompositeCommand @Override public boolean canExecute(User user, String label, List args) { - Optional optionalAddon = this.getAddon().getPlugin().getIWM().getAddon(this.getWorld()); - - if (!optionalAddon.isPresent()) - { + if (!getIWM().inWorld(getWorld())) { // Not a GameMode world. user.sendMessage("general.errors.wrong-world"); return false; @@ -39,13 +34,12 @@ public class ChallengesCommand extends CompositeCommand if (!((ChallengesAddon) this.getAddon()).getChallengesManager().hasAnyChallengeData(this.getWorld())) { // Do not open gui if there is no challenges. - - this.getAddon().getLogger().severe("There are no challenges set up in " + this.getWorld() + "!"); + this.getAddon().logError("There are no challenges set up in " + this.getWorld() + "!"); // Show admin better explanation. 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"); } else @@ -56,7 +50,7 @@ public class ChallengesCommand extends CompositeCommand 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. user.sendMessage("general.errors.no-island"); diff --git a/src/main/java/world/bentobox/challenges/panel/user/ChallengesGUI.java b/src/main/java/world/bentobox/challenges/panel/user/ChallengesGUI.java index fd336ea..38005ad 100644 --- a/src/main/java/world/bentobox/challenges/panel/user/ChallengesGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/user/ChallengesGUI.java @@ -78,7 +78,7 @@ public class ChallengesGUI extends CommonGUI // Do not open gui if there is no challenges. 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"); return; } diff --git a/src/test/java/world/bentobox/challenges/ChallengesAddonTest.java b/src/test/java/world/bentobox/challenges/ChallengesAddonTest.java index b0d8992..60b88f9 100644 --- a/src/test/java/world/bentobox/challenges/ChallengesAddonTest.java +++ b/src/test/java/world/bentobox/challenges/ChallengesAddonTest.java @@ -1,6 +1,3 @@ -/** - * - */ package world.bentobox.challenges; import static org.mockito.Matchers.any; diff --git a/src/test/java/world/bentobox/challenges/commands/ChallengesCommandTest.java b/src/test/java/world/bentobox/challenges/commands/ChallengesCommandTest.java new file mode 100644 index 0000000..dcf1cdd --- /dev/null +++ b/src/test/java/world/bentobox/challenges/commands/ChallengesCommandTest.java @@ -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 optionalAddon = Optional.of(gameModeAddon); + when(iwm.getAddon(any())).thenReturn(optionalAddon); + when(plugin.getIWM()).thenReturn(iwm); + + // Game Mode Addon + @NonNull + Optional 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) 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()); + } + +}