From 212820e12a06de9dca7bed15e1b70c36e279aae4 Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 29 Oct 2019 16:05:04 -0700 Subject: [PATCH] Added new TryToComplete test class - WIP Covers inventory challenges. --- .../challenges/tasks/TryToComplete.java | 16 +- .../challenges/tasks/TryToCompleteTest.java | 601 ++++++++++++------ .../tasks/TryToCompleteTestOld.java | 233 +++++++ 3 files changed, 662 insertions(+), 188 deletions(-) create mode 100644 src/test/java/world/bentobox/challenges/tasks/TryToCompleteTestOld.java diff --git a/src/main/java/world/bentobox/challenges/tasks/TryToComplete.java b/src/main/java/world/bentobox/challenges/tasks/TryToComplete.java index dc8edb2..8c62808 100644 --- a/src/main/java/world/bentobox/challenges/tasks/TryToComplete.java +++ b/src/main/java/world/bentobox/challenges/tasks/TryToComplete.java @@ -1,6 +1,3 @@ -/** - * - */ package world.bentobox.challenges.tasks; @@ -16,6 +13,7 @@ import java.util.PriorityQueue; import java.util.Queue; import java.util.stream.Collectors; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.Material; @@ -178,7 +176,7 @@ public class TryToComplete this.permissionPrefix = permissionPrefix; this.user = user; this.manager = addon.getChallengesManager(); - // To avoid any modifications that may accure to challenges in current completion + // To avoid any modifications that may occur to challenges in current completion // just clone it. this.challenge = challenge.clone(); this.topLabel = topLabel; @@ -239,7 +237,7 @@ public class TryToComplete * This method checks if challenge can be done, and complete it, if it is possible. * @return ChallengeResult object, that contains completion status. */ - public ChallengeResult build(int maxTimes) + ChallengeResult build(int maxTimes) { // Check if can complete challenge ChallengeResult result = this.checkIfCanCompleteChallenge(maxTimes); @@ -304,7 +302,7 @@ public class TryToComplete if (this.addon.getChallengesSettings().isBroadcastMessages()) { - for (Player player : this.addon.getServer().getOnlinePlayers()) + for (Player player : Bukkit.getOnlinePlayers()) { // Only other players should see message. if (!player.getUniqueId().equals(this.user.getUniqueId())) @@ -521,7 +519,6 @@ public class TryToComplete ChallengeResult result; ChallengeType type = this.challenge.getChallengeType(); - // Check the world if (!this.challenge.isDeployed()) { @@ -609,7 +606,6 @@ public class TryToComplete { result.setCompleted(this.manager.isChallengeComplete(this.user, this.world, this.challenge)); } - // Everything fails till this point. return result; } @@ -1316,7 +1312,7 @@ public class TryToComplete // --------------------------------------------------------------------- -// Section: Private classes +// Section: Result classes // --------------------------------------------------------------------- @@ -1325,7 +1321,7 @@ public class TryToComplete * * @author tastybento */ - private class ChallengeResult + class ChallengeResult { /** * This method sets that challenge meets all requirements at least once. diff --git a/src/test/java/world/bentobox/challenges/tasks/TryToCompleteTest.java b/src/test/java/world/bentobox/challenges/tasks/TryToCompleteTest.java index aabe6fc..79c08a7 100644 --- a/src/test/java/world/bentobox/challenges/tasks/TryToCompleteTest.java +++ b/src/test/java/world/bentobox/challenges/tasks/TryToCompleteTest.java @@ -1,233 +1,478 @@ package world.bentobox.challenges.tasks; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; +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.List; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; import java.util.Map; -import java.util.logging.Logger; +import java.util.Optional; +import java.util.Set; +import java.util.UUID; import org.bukkit.Bukkit; +import org.bukkit.GameMode; +import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.Server; +import org.bukkit.World; +import org.bukkit.World.Environment; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemFactory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; +import org.eclipse.jdt.annotation.NonNull; +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.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.AddonDescription; +import world.bentobox.bentobox.api.addons.GameModeAddon; +import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.database.objects.Island; +import world.bentobox.bentobox.managers.AddonsManager; +import world.bentobox.bentobox.managers.IslandWorldManager; +import world.bentobox.bentobox.managers.IslandsManager; +import world.bentobox.bentobox.managers.LocalesManager; +import world.bentobox.bentobox.managers.PlaceholdersManager; +import world.bentobox.bentobox.util.Util; import world.bentobox.challenges.ChallengesAddon; +import world.bentobox.challenges.ChallengesManager; +import world.bentobox.challenges.config.Settings; +import world.bentobox.challenges.database.object.Challenge; +import world.bentobox.challenges.database.object.Challenge.ChallengeType; +import world.bentobox.challenges.database.object.ChallengeLevel; +import world.bentobox.challenges.database.object.requirements.InventoryRequirements; +import world.bentobox.challenges.tasks.TryToComplete.ChallengeResult; +import world.bentobox.challenges.utils.Utils; + /** * @author tastybento + * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class}) +@PrepareForTest({Bukkit.class, BentoBox.class, Util.class, Utils.class}) public class TryToCompleteTest { - private User user; - ItemStack[] stacks = { new ItemStack(Material.PAPER, 32), - new ItemStack(Material.ACACIA_BOAT), - null, - null, - new ItemStack(Material.CACTUS, 32), - new ItemStack(Material.CACTUS, 32), - new ItemStack(Material.CACTUS, 32), - new ItemStack(Material.BRICK_STAIRS, 64), - new ItemStack(Material.BRICK_STAIRS, 64), - new ItemStack(Material.BRICK_STAIRS, 5), - new ItemStack(Material.GOLD_BLOCK, 32) - }; - List required; - private ChallengesAddon addon; - private PlayerInventory inv; + // Constants + private static final String GAME_MODE_NAME = "BSkyBlock"; + private static final String[] NAMES = {"adam", "ben", "cara", "dave", "ed", "frank", "freddy", "george", "harry", "ian", "joe"}; - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - Server server = mock(Server.class); - PowerMockito.mockStatic(Bukkit.class); - when(Bukkit.getServer()).thenReturn(server); - when(Bukkit.getBukkitVersion()).thenReturn("1.13.2"); + private TryToComplete ttc; + private Challenge challenge; + private @NonNull ChallengeLevel level; + @Mock + private ChallengesAddon addon; + @Mock + private User user; + @Mock + private World world; + private String topLabel = "island"; + private String permissionPrefix = "perm."; + private String cName; + private String levelName; + @Mock + private ChallengesManager cm; + @Mock + private BentoBox plugin; + @Mock + private GameModeAddon gameMode; + @Mock + private AddonsManager am; + @Mock + private IslandsManager im; + @Mock + private Island island; + @Mock + private Player player; + @Mock + private Settings settings; + @Mock + private WorldSettings mySettings; + private Map map; + @Mock + private @Nullable PlayerInventory inv; + private ItemStack[] contents = {}; - user = mock(User.class); - inv = mock(PlayerInventory.class); - when(inv.getContents()).thenReturn(stacks); - when(user.getInventory()).thenReturn(inv); - addon = mock(ChallengesAddon.class); - required = new ArrayList<>(); + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + Whitebox.setInternalState(BentoBox.class, "instance", plugin); + // World + when(user.getWorld()).thenReturn(world); + when(world.getEnvironment()).thenReturn(Environment.NORMAL); - ItemFactory itemFactory = mock(ItemFactory.class); - when(server.getItemFactory()).thenReturn(itemFactory); + // Addons manager + when(plugin.getAddonsManager()).thenReturn(am); + // One game mode + when(am.getGameModeAddons()).thenReturn(Collections.singletonList(gameMode)); + AddonDescription desc2 = new AddonDescription.Builder("bentobox", GAME_MODE_NAME, "1.3").description("test").authors("tasty").build(); + when(gameMode.getDescription()).thenReturn(desc2); - // Test will not work with items that has meta data. - when(itemFactory.getItemMeta(any())).thenReturn(null); - when(itemFactory.equals(null, null)).thenReturn(true); + // Challenge Level + level = new ChallengeLevel(); + levelName = GAME_MODE_NAME + "_novice"; + level.setUniqueId(levelName); + level.setFriendlyName("Novice"); + // Set up challenge + String uuid = UUID.randomUUID().toString(); + challenge = new Challenge(); + challenge.setUniqueId(GAME_MODE_NAME + "_" + uuid); + challenge.setFriendlyName("name"); + challenge.setLevel(GAME_MODE_NAME + "_novice"); + challenge.setDescription(Collections.singletonList("A description")); + challenge.setChallengeType(ChallengeType.INVENTORY); + challenge.setDeployed(true); + challenge.setIcon(new ItemStack(Material.EMERALD)); + challenge.setEnvironment(Collections.singleton(World.Environment.NORMAL)); + challenge.setLevel(levelName); + challenge.setRepeatable(true); + challenge.setMaxTimes(10); + InventoryRequirements req = new InventoryRequirements(); + challenge.setRequirements(req); + // Util + PowerMockito.mockStatic(Util.class); + when(Util.getWorld(any())).thenReturn(world); + when(Util.prettifyText(anyString())).thenCallRealMethod(); - when(Bukkit.getItemFactory()).thenReturn(itemFactory); - when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger()); - } + // Island World Manager + IslandWorldManager iwm = mock(IslandWorldManager.class); + when(plugin.getIWM()).thenReturn(iwm); + Optional optionalGameMode = Optional.of(gameMode); + when(iwm.getAddon(any())).thenReturn(optionalGameMode); - /** - * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. - */ - @Test - public void testRemoveItemsSuccess() { - Material requiredMaterial = Material.PAPER; - int requiredQuantity = 21; + // Island Manager + when(addon.getIslands()).thenReturn(im); + Optional opIsland = Optional.of(island); + when(im.getIslandAt(any())).thenReturn(opIsland); + // Player is on island + when(im.locationIsOnIsland(any(), any())).thenReturn(true); + // Island flags - everything is allowed by default + when(island.isAllowed(any(), any())).thenReturn(true); - this.required.add(new ItemStack(requiredMaterial, requiredQuantity)); - TryToComplete x = new TryToComplete(this.addon); - x.user(this.user); - Map removed = x.removeItems(this.required, 1); + // Challenges Manager + when(addon.getChallengesManager()).thenReturn(cm); + // All levels unlocked by default + when(cm.isLevelUnlocked(any(), any(), any())).thenReturn(true); + // Player has done this challenge 3 times (default max is 10) + when(cm.getChallengeTimes(any(), any(), any(Challenge.class))).thenReturn(3L); - assertEquals((int) removed.getOrDefault(new ItemStack(requiredMaterial, 1), 0), requiredQuantity); - } + // User has all perms by default + when(user.hasPermission(anyString())).thenReturn(true); + when(user.getPlayer()).thenReturn(player); + when(user.getTranslation(Mockito.anyString())).thenAnswer((Answer) invocation -> invocation.getArgument(0, String.class)); + when(user.getName()).thenReturn("tastybento"); + when(user.getLocation()).thenReturn(mock(Location.class)); + when(user.getInventory()).thenReturn(inv); + when(inv.getContents()).thenReturn(contents); + // Locales + User.setPlugin(plugin); + LocalesManager lm = mock(LocalesManager.class); + when(plugin.getLocalesManager()).thenReturn(lm); + when(lm.get(any(), any())).thenAnswer((Answer) invocation -> invocation.getArgument(1, String.class)); + PlaceholdersManager phm = mock(PlaceholdersManager.class); + when(plugin.getPlaceholdersManager()).thenReturn(phm); + when(phm.replacePlaceholders(any(), any())).thenAnswer((Answer) invocation -> invocation.getArgument(1, String.class)); + + // Survival by default + when(player.getGameMode()).thenReturn(GameMode.SURVIVAL); + + // Addon + when(addon.getChallengesSettings()).thenReturn(settings); + when(settings.isBroadcastMessages()).thenReturn(true); + + // Bukkit - online players + Map online = new HashMap<>(); - /** - * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. - */ - @Test - public void testRemoveItemsMax() { - Material requiredMaterial = Material.PAPER; - int requiredQuantity = 50; + Set onlinePlayers = new HashSet<>(); + for (int j = 0; j < NAMES.length; j++) { + Player p1 = mock(Player.class); + UUID uuid2 = UUID.randomUUID(); + when(p1.getUniqueId()).thenReturn(uuid2); + when(p1.getName()).thenReturn(NAMES[j]); + online.put(uuid2, NAMES[j]); + onlinePlayers.add(p1); + } + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.getOnlinePlayers()).then((Answer>) invocation -> onlinePlayers); - this.required.add(new ItemStack(requiredMaterial, requiredQuantity)); - TryToComplete x = new TryToComplete(this.addon); - x.user(this.user); - Map removed = x.removeItems(this.required, 1); + // World settings + map = new HashMap<>(); + when(mySettings.getWorldFlags()).thenReturn(map); + when(iwm.getWorldSettings(any())).thenReturn(mySettings); + ChallengesAddon.CHALLENGES_WORLD_PROTECTION.setSetting(world, true); + + // ItemFsaxtory + ItemFactory itemFactory = mock(ItemFactory.class); + when(Bukkit.getItemFactory()).thenReturn(itemFactory); + + } - assertNotEquals((int) removed.getOrDefault(new ItemStack(requiredMaterial, 1), 0), requiredQuantity); - } + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } - /** - * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. - */ - @Test - public void testRemoveItemsZero() { - Material requiredMaterial = Material.PAPER; - int requiredQuantity = 0; + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#TryToComplete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}. + */ + @Test + public void testTryToCompleteChallengesAddonUserChallengeWorldStringString() { + ttc = new TryToComplete(addon, + user, + challenge, + world, + topLabel, + permissionPrefix); + verify(addon).getChallengesManager(); - this.required.add(new ItemStack(requiredMaterial, requiredQuantity)); - TryToComplete x = new TryToComplete(this.addon); - x.user(this.user); - Map removed = x.removeItems(this.required, 1); + } - assertTrue(removed.isEmpty()); - } + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}. + */ + @Test + public void testCompleteChallengesAddonUserChallengeWorldStringStringNotDeployed() { + challenge.setDeployed(false); + assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix)); + verify(user).sendMessage("challenges.errors.not-deployed"); + } + + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}. + */ + @Test + public void testCompleteChallengesAddonUserChallengeWorldStringStringWrongWorld() { + challenge.setUniqueId("test"); + assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix)); + verify(user).sendMessage("general.errors.wrong-world"); + } + + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}. + */ + @Test + public void testCompleteChallengesAddonUserChallengeWorldStringStringNotOnIsland() { + ChallengesAddon.CHALLENGES_WORLD_PROTECTION.setSetting(world, true); + when(im.locationIsOnIsland(any(Player.class), any(Location.class))).thenReturn(false); + assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix)); + verify(user).sendMessage("challenges.errors.not-on-island"); + } + + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}. + */ + @Test + public void testCompleteChallengesAddonUserChallengeWorldStringStringNotOnIslandButOk() { + ChallengesAddon.CHALLENGES_WORLD_PROTECTION.setSetting(world, false); + when(im.locationIsOnIsland(any(Player.class), any(Location.class))).thenReturn(false); + assertTrue(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix)); + verify(user).sendMessage("challenges.messages.you-completed-challenge", "[value]", "name"); + } + + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}. + */ + @Test + public void testCompleteChallengesAddonUserChallengeWorldStringStringLevelNotUnlocked() { + when(cm.isLevelUnlocked(any(), any(), any())).thenReturn(false); + assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix)); + verify(user).sendMessage("challenges.errors.challenge-level-not-available"); + } + + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}. + */ + @Test + public void testCompleteChallengesAddonUserChallengeWorldStringStringNotRepeatable() { + challenge.setRepeatable(false); + when(cm.isChallengeComplete(any(User.class), any(), any())).thenReturn(true); + assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix)); + verify(user).sendMessage("challenges.errors.not-repeatable"); + } + + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}. + */ + @Test + public void testCompleteChallengesAddonUserChallengeWorldStringStringNotRepeatableFirstTime() { + challenge.setRepeatable(false); + challenge.setMaxTimes(0); + when(cm.getChallengeTimes(any(), any(), any(Challenge.class))).thenReturn(0L); + assertTrue(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix)); + verify(user).sendMessage("challenges.messages.you-completed-challenge", "[value]", "name"); + } + + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}. + */ + @Test + public void testCompleteChallengesAddonUserChallengeWorldStringStringNoRank() { + when(island.isAllowed(any(), any())).thenReturn(false); + assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix)); + verify(user).sendMessage("challenges.errors.no-rank"); + } - /** - * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. - */ - @Test - public void testRemoveItemsSuccessMultiple() { - required.add(new ItemStack(Material.PAPER, 11)); - required.add(new ItemStack(Material.PAPER, 5)); - required.add(new ItemStack(Material.PAPER, 5)); - TryToComplete x = new TryToComplete(addon); - x.user(user); - Map removed = x.removeItems(required, 1); + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String, int)}. + */ + @Test + public void testCompleteChallengesAddonUserChallengeWorldStringStringIntZero() { + assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix, 0)); + verify(user).sendMessage("challenges.errors.not-valid-integer"); + } - assertEquals((int) removed.getOrDefault(new ItemStack(Material.PAPER, 1), 0), 21); - } + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String, int)}. + */ + @Test + public void testCompleteChallengesAddonUserChallengeWorldStringStringIntNegative() { + assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix, -10)); + verify(user).sendMessage("challenges.errors.not-valid-integer"); + } - /** - * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. - */ - @Test - public void testRemoveItemsSuccessMultipleOther() { - required.add(new ItemStack(Material.CACTUS, 5)); - required.add(new ItemStack(Material.PAPER, 11)); - required.add(new ItemStack(Material.PAPER, 5)); - required.add(new ItemStack(Material.PAPER, 5)); - required.add(new ItemStack(Material.CACTUS, 5)); - TryToComplete x = new TryToComplete(addon); - x.user(user); - Map removed = x.removeItems(required, 1); + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String, int)}. + */ + @Test + public void testCompleteChallengesAddonUserChallengeWorldStringStringIntPositiveWrongEnvinonment() { + challenge.setEnvironment(Collections.singleton(Environment.NETHER)); + assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix, 100)); + verify(user).sendMessage("challenges.errors.wrong-environment"); + } + + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String, int)}. + */ + @Test + public void testCompleteChallengesAddonUserChallengeWorldStringStringIntPositiveNoPerm() { + InventoryRequirements req = new InventoryRequirements(); + req.setRequiredPermissions(Collections.singleton("perm-you-dont-have")); + when(user.hasPermission(anyString())).thenReturn(false); + challenge.setRequirements(req); + assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix, 100)); + verify(user).sendMessage("general.errors.no-permission"); + } - assertEquals((int) removed.getOrDefault(new ItemStack(Material.PAPER, 1), 0), 21); - assertEquals((int) removed.getOrDefault(new ItemStack(Material.CACTUS, 1), 0), 10); - } + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}. + */ + @Test + public void testCompleteChallengesAddonUserChallengeWorldStringStringSuccess() { + assertTrue(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix)); + verify(user).sendMessage("challenges.messages.you-completed-challenge", "[value]", "name"); + } + + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}. + */ + @Test + public void testCompleteChallengesAddonUserChallengeWorldStringStringSuccessSingleReq() { + InventoryRequirements req = new InventoryRequirements(); + req.setRequiredItems(Collections.singletonList(new ItemStack(Material.EMERALD_BLOCK))); + challenge.setRequirements(req); + assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix)); + verify(user).sendMessage("challenges.errors.not-enough-items", "[items]", "Emerald Block"); + } + + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}. + */ + @Test + public void testCompleteChallengesAddonUserChallengeWorldStringStringSuccessMultipleReq() { + + InventoryRequirements req = new InventoryRequirements(); + ItemStack itemStackMock = mock(ItemStack.class); + when(itemStackMock.getAmount()).thenReturn(3); + when(itemStackMock.getType()).thenReturn(Material.EMERALD_BLOCK); + when(itemStackMock.clone()).thenReturn(itemStackMock); + + ItemStack itemStackMock2 = mock(ItemStack.class); + when(itemStackMock2.getType()).thenReturn(Material.ENCHANTED_BOOK); + when(itemStackMock2.getAmount()).thenReturn(10); + when(itemStackMock2.clone()).thenReturn(itemStackMock2); + + ItemStack itemStackMock3 = mock(ItemStack.class); + when(itemStackMock3.getType()).thenReturn(Material.EMERALD_BLOCK); + when(itemStackMock3.getAmount()).thenReturn(15); + when(itemStackMock3.clone()).thenReturn(itemStackMock3); + // itemStackMock and 3 are same type + when(itemStackMock3.isSimilar(eq(itemStackMock))).thenReturn(true); + when(itemStackMock.isSimilar(eq(itemStackMock3))).thenReturn(true); + + req.setRequiredItems(Arrays.asList(itemStackMock , itemStackMock2)); + challenge.setRequirements(req); + ItemStack[] newContents = {itemStackMock3}; + when(inv.getContents()).thenReturn(newContents); - /** - * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. - */ - @Test - public void testRemoveItemsMultipleOtherFail() { - required.add(new ItemStack(Material.ACACIA_FENCE, 5)); - required.add(new ItemStack(Material.ARROW, 11)); - required.add(new ItemStack(Material.STONE, 5)); - required.add(new ItemStack(Material.BAKED_POTATO, 5)); - required.add(new ItemStack(Material.GHAST_SPAWN_EGG, 5)); - TryToComplete x = new TryToComplete(addon); - x.user(user); - Map removed = x.removeItems(required, 1); - assertTrue(removed.isEmpty()); - } + assertFalse(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix)); + // Sufficient emerald blocks + verify(user, never()).sendMessage("challenges.errors.not-enough-items", "[items]", "Emerald Block"); + // Not enough apples + verify(user).sendMessage("challenges.errors.not-enough-items", "[items]", "Enchanted Book"); + } + + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}. + */ + @Test + public void testCompleteChallengesAddonUserChallengeWorldStringStringSuccessCreative() { + when(player.getGameMode()).thenReturn(GameMode.CREATIVE); + assertTrue(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix)); + verify(user).sendMessage("challenges.messages.you-completed-challenge", "[value]", "name"); + } - /** - * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. - */ - @Test - public void testRemoveItemsFail() { - ItemStack input = new ItemStack(Material.GOLD_BLOCK, 55); - required.add(input); - TryToComplete x = new TryToComplete(addon); - x.user(user); - Map removed = x.removeItems(required, 1); + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#complete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String, int)}. + */ + @Test + public void testCompleteChallengesAddonUserChallengeWorldStringStringIntMultipleTimesPositiveSuccess() { + // Try to complete 10 times. Already done 3 times, and max is 10, so it should be only done 7 times + assertTrue(TryToComplete.complete(addon, user, challenge, world, topLabel, permissionPrefix, 10)); + verify(user).sendMessage("challenges.messages.you-repeated-challenge-multiple", "[value]", "name", "[count]", "7"); + } - // It will remove 32, but not any more - assertEquals((int) removed.getOrDefault(new ItemStack(Material.GOLD_BLOCK, 1), 0), 32); + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#build(int)}. + */ + @Test + public void testBuild() { + this.testTryToCompleteChallengesAddonUserChallengeWorldStringString(); + ChallengeResult result = this.ttc.build(10); + } - // An error will be thrown - Mockito.verify(addon, Mockito.times(1)).logError(Mockito.anyString()); - } + /** + * Test method for {@link world.bentobox.challenges.tasks.TryToComplete#removeItems(java.util.List, int)}. + */ + @Test + public void testRemoveItems() { + this.testTryToCompleteChallengesAddonUserChallengeWorldStringString(); + ttc.removeItems(Collections.emptyList(), 1); + } - - - /** - * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. - */ - @Test - public void testRequireTwoStacks() { - required.add(new ItemStack(Material.BRICK_STAIRS, 64)); - required.add(new ItemStack(Material.BRICK_STAIRS, 64)); - - TryToComplete x = new TryToComplete(addon); - x.user(user); - Map removed = x.removeItems(required, 1); - - // It should remove both stacks - assertEquals((int) removed.getOrDefault(new ItemStack(Material.BRICK_STAIRS, 1), 0), 128); - } - - - /** - * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. - */ - @Test - public void testFactorStacks() { - required.add(new ItemStack(Material.BRICK_STAIRS, 32)); - - TryToComplete x = new TryToComplete(addon); - x.user(user); - Map removed = x.removeItems(required, 4); - - // It should remove both stacks - assertEquals((int) removed.getOrDefault(new ItemStack(Material.BRICK_STAIRS, 1), 0), 128); - } } - diff --git a/src/test/java/world/bentobox/challenges/tasks/TryToCompleteTestOld.java b/src/test/java/world/bentobox/challenges/tasks/TryToCompleteTestOld.java new file mode 100644 index 0000000..91d85de --- /dev/null +++ b/src/test/java/world/bentobox/challenges/tasks/TryToCompleteTestOld.java @@ -0,0 +1,233 @@ +package world.bentobox.challenges.tasks; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.logging.Logger; + +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.Server; +import org.bukkit.inventory.ItemFactory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import world.bentobox.bentobox.api.user.User; +import world.bentobox.challenges.ChallengesAddon; + +/** + * @author tastybento + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({ Bukkit.class}) +public class TryToCompleteTestOld { + + private User user; + ItemStack[] stacks = { new ItemStack(Material.PAPER, 32), + new ItemStack(Material.ACACIA_BOAT), + null, + null, + new ItemStack(Material.CACTUS, 32), + new ItemStack(Material.CACTUS, 32), + new ItemStack(Material.CACTUS, 32), + new ItemStack(Material.BRICK_STAIRS, 64), + new ItemStack(Material.BRICK_STAIRS, 64), + new ItemStack(Material.BRICK_STAIRS, 5), + new ItemStack(Material.GOLD_BLOCK, 32) + }; + List required; + private ChallengesAddon addon; + private PlayerInventory inv; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + Server server = mock(Server.class); + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.getServer()).thenReturn(server); + when(Bukkit.getBukkitVersion()).thenReturn("1.13.2"); + + user = mock(User.class); + inv = mock(PlayerInventory.class); + when(inv.getContents()).thenReturn(stacks); + when(user.getInventory()).thenReturn(inv); + addon = mock(ChallengesAddon.class); + required = new ArrayList<>(); + + ItemFactory itemFactory = mock(ItemFactory.class); + when(server.getItemFactory()).thenReturn(itemFactory); + + // Test will not work with items that has meta data. + when(itemFactory.getItemMeta(any())).thenReturn(null); + when(itemFactory.equals(null, null)).thenReturn(true); + + when(Bukkit.getItemFactory()).thenReturn(itemFactory); + when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger()); + } + + /** + * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. + */ + @Test + public void testRemoveItemsSuccess() { + Material requiredMaterial = Material.PAPER; + int requiredQuantity = 21; + + this.required.add(new ItemStack(requiredMaterial, requiredQuantity)); + TryToComplete x = new TryToComplete(this.addon); + x.user(this.user); + Map removed = x.removeItems(this.required, 1); + + assertEquals((int) removed.getOrDefault(new ItemStack(requiredMaterial, 1), 0), requiredQuantity); + } + + /** + * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. + */ + @Test + public void testRemoveItemsMax() { + Material requiredMaterial = Material.PAPER; + int requiredQuantity = 50; + + this.required.add(new ItemStack(requiredMaterial, requiredQuantity)); + TryToComplete x = new TryToComplete(this.addon); + x.user(this.user); + Map removed = x.removeItems(this.required, 1); + + assertNotEquals((int) removed.getOrDefault(new ItemStack(requiredMaterial, 1), 0), requiredQuantity); + } + + /** + * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. + */ + @Test + public void testRemoveItemsZero() { + Material requiredMaterial = Material.PAPER; + int requiredQuantity = 0; + + this.required.add(new ItemStack(requiredMaterial, requiredQuantity)); + TryToComplete x = new TryToComplete(this.addon); + x.user(this.user); + Map removed = x.removeItems(this.required, 1); + + assertTrue(removed.isEmpty()); + } + + /** + * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. + */ + @Test + public void testRemoveItemsSuccessMultiple() { + required.add(new ItemStack(Material.PAPER, 11)); + required.add(new ItemStack(Material.PAPER, 5)); + required.add(new ItemStack(Material.PAPER, 5)); + TryToComplete x = new TryToComplete(addon); + x.user(user); + Map removed = x.removeItems(required, 1); + + assertEquals((int) removed.getOrDefault(new ItemStack(Material.PAPER, 1), 0), 21); + } + + /** + * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. + */ + @Test + public void testRemoveItemsSuccessMultipleOther() { + required.add(new ItemStack(Material.CACTUS, 5)); + required.add(new ItemStack(Material.PAPER, 11)); + required.add(new ItemStack(Material.PAPER, 5)); + required.add(new ItemStack(Material.PAPER, 5)); + required.add(new ItemStack(Material.CACTUS, 5)); + TryToComplete x = new TryToComplete(addon); + x.user(user); + Map removed = x.removeItems(required, 1); + + assertEquals((int) removed.getOrDefault(new ItemStack(Material.PAPER, 1), 0), 21); + assertEquals((int) removed.getOrDefault(new ItemStack(Material.CACTUS, 1), 0), 10); + } + + /** + * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. + */ + @Test + public void testRemoveItemsMultipleOtherFail() { + required.add(new ItemStack(Material.ACACIA_FENCE, 5)); + required.add(new ItemStack(Material.ARROW, 11)); + required.add(new ItemStack(Material.STONE, 5)); + required.add(new ItemStack(Material.BAKED_POTATO, 5)); + required.add(new ItemStack(Material.GHAST_SPAWN_EGG, 5)); + TryToComplete x = new TryToComplete(addon); + x.user(user); + Map removed = x.removeItems(required, 1); + assertTrue(removed.isEmpty()); + } + + /** + * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. + */ + @Test + public void testRemoveItemsFail() { + ItemStack input = new ItemStack(Material.GOLD_BLOCK, 55); + required.add(input); + TryToComplete x = new TryToComplete(addon); + x.user(user); + Map removed = x.removeItems(required, 1); + + // It will remove 32, but not any more + assertEquals((int) removed.getOrDefault(new ItemStack(Material.GOLD_BLOCK, 1), 0), 32); + + // An error will be thrown + Mockito.verify(addon, Mockito.times(1)).logError(Mockito.anyString()); + } + + + + /** + * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. + */ + @Test + public void testRequireTwoStacks() { + required.add(new ItemStack(Material.BRICK_STAIRS, 64)); + required.add(new ItemStack(Material.BRICK_STAIRS, 64)); + + TryToComplete x = new TryToComplete(addon); + x.user(user); + Map removed = x.removeItems(required, 1); + + // It should remove both stacks + assertEquals((int) removed.getOrDefault(new ItemStack(Material.BRICK_STAIRS, 1), 0), 128); + } + + + /** + * Test method for {@link TryToComplete#removeItems(java.util.List, int)}. + */ + @Test + public void testFactorStacks() { + required.add(new ItemStack(Material.BRICK_STAIRS, 32)); + + TryToComplete x = new TryToComplete(addon); + x.user(user); + Map removed = x.removeItems(required, 4); + + // It should remove both stacks + assertEquals((int) removed.getOrDefault(new ItemStack(Material.BRICK_STAIRS, 1), 0), 128); + } +} +