|
| 1 | +package world.bentobox.level.commands; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | +import static org.mockito.ArgumentMatchers.any; |
| 7 | +import static org.mockito.ArgumentMatchers.anyString; |
| 8 | +import static org.mockito.ArgumentMatchers.eq; |
| 9 | +import static org.mockito.Mockito.mock; |
| 10 | +import static org.mockito.Mockito.verify; |
| 11 | +import static org.mockito.Mockito.when; |
| 12 | + |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.UUID; |
| 15 | + |
| 16 | +import org.bukkit.Bukkit; |
| 17 | +import org.bukkit.Server; |
| 18 | +import org.bukkit.World; |
| 19 | +import org.bukkit.entity.Player; |
| 20 | +import org.bukkit.inventory.ItemFactory; |
| 21 | +import org.bukkit.inventory.meta.ItemMeta; |
| 22 | +import org.junit.After; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runner.RunWith; |
| 26 | +import org.mockito.Mock; |
| 27 | +import org.powermock.api.mockito.PowerMockito; |
| 28 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 29 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 30 | +import org.powermock.reflect.Whitebox; |
| 31 | + |
| 32 | +import world.bentobox.bentobox.BentoBox; |
| 33 | +import world.bentobox.bentobox.api.addons.GameModeAddon; |
| 34 | +import world.bentobox.bentobox.api.commands.CompositeCommand; |
| 35 | +import world.bentobox.bentobox.api.localization.TextVariables; |
| 36 | +import world.bentobox.bentobox.api.user.User; |
| 37 | +import world.bentobox.bentobox.database.objects.Island; |
| 38 | +import world.bentobox.bentobox.managers.IslandWorldManager; |
| 39 | +import world.bentobox.bentobox.managers.IslandsManager; |
| 40 | +import world.bentobox.bentobox.managers.LocalesManager; |
| 41 | +import world.bentobox.bentobox.managers.PlayersManager; |
| 42 | +import world.bentobox.level.Level; |
| 43 | +import world.bentobox.level.LevelsManager; |
| 44 | +import world.bentobox.level.objects.TopTenData; |
| 45 | + |
| 46 | +/** |
| 47 | + * @author tastybento |
| 48 | + * |
| 49 | + */ |
| 50 | +@RunWith(PowerMockRunner.class) |
| 51 | +@PrepareForTest({ Bukkit.class, BentoBox.class }) |
| 52 | +public class AdminTopRemoveCommandTest { |
| 53 | + |
| 54 | + @Mock |
| 55 | + private CompositeCommand ic; |
| 56 | + private UUID uuid; |
| 57 | + @Mock |
| 58 | + private User user; |
| 59 | + @Mock |
| 60 | + private IslandsManager im; |
| 61 | + @Mock |
| 62 | + private Island island; |
| 63 | + @Mock |
| 64 | + private Level addon; |
| 65 | + @Mock |
| 66 | + private World world; |
| 67 | + @Mock |
| 68 | + private IslandWorldManager iwm; |
| 69 | + @Mock |
| 70 | + private GameModeAddon gameModeAddon; |
| 71 | + @Mock |
| 72 | + private Player p; |
| 73 | + @Mock |
| 74 | + private LocalesManager lm; |
| 75 | + @Mock |
| 76 | + private PlayersManager pm; |
| 77 | + |
| 78 | + private AdminTopRemoveCommand atrc; |
| 79 | + @Mock |
| 80 | + private TopTenData ttd; |
| 81 | + @Mock |
| 82 | + private LevelsManager manager; |
| 83 | + @Mock |
| 84 | + private Server server; |
| 85 | + |
| 86 | + @Before |
| 87 | + public void setUp() { |
| 88 | + // Set up plugin |
| 89 | + BentoBox plugin = mock(BentoBox.class); |
| 90 | + Whitebox.setInternalState(BentoBox.class, "instance", plugin); |
| 91 | + User.setPlugin(plugin); |
| 92 | + // Addon |
| 93 | + when(ic.getAddon()).thenReturn(addon); |
| 94 | + when(ic.getPermissionPrefix()).thenReturn("bskyblock."); |
| 95 | + when(ic.getLabel()).thenReturn("island"); |
| 96 | + when(ic.getTopLabel()).thenReturn("island"); |
| 97 | + when(ic.getWorld()).thenReturn(world); |
| 98 | + when(ic.getTopLabel()).thenReturn("bsb"); |
| 99 | + |
| 100 | + // IWM friendly name |
| 101 | + when(plugin.getIWM()).thenReturn(iwm); |
| 102 | + when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); |
| 103 | + |
| 104 | + // World |
| 105 | + when(world.toString()).thenReturn("world"); |
| 106 | + when(world.getName()).thenReturn("BSkyBlock_world"); |
| 107 | + |
| 108 | + // Player manager |
| 109 | + when(plugin.getPlayers()).thenReturn(pm); |
| 110 | + when(pm.getUser(anyString())).thenReturn(user); |
| 111 | + // topTen |
| 112 | + when(addon.getManager()).thenReturn(manager); |
| 113 | + // User |
| 114 | + uuid = UUID.randomUUID(); |
| 115 | + when(user.getUniqueId()).thenReturn(uuid); |
| 116 | + when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); |
| 117 | + |
| 118 | + // Bukkit |
| 119 | + PowerMockito.mockStatic(Bukkit.class); |
| 120 | + when(Bukkit.getServer()).thenReturn(server); |
| 121 | + // Mock item factory (for itemstacks) |
| 122 | + ItemFactory itemFactory = mock(ItemFactory.class); |
| 123 | + ItemMeta itemMeta = mock(ItemMeta.class); |
| 124 | + when(itemFactory.getItemMeta(any())).thenReturn(itemMeta); |
| 125 | + when(server.getItemFactory()).thenReturn(itemFactory); |
| 126 | + when(Bukkit.getItemFactory()).thenReturn(itemFactory); |
| 127 | + |
| 128 | + atrc = new AdminTopRemoveCommand(addon, ic); |
| 129 | + } |
| 130 | + |
| 131 | + @After |
| 132 | + public void tearDown() { |
| 133 | + User.clearUsers(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Test method for |
| 138 | + * {@link world.bentobox.level.commands.admin.AdminTopRemoveCommand#AdminTopRemoveCommand(world.bentobox.level.Level, world.bentobox.bentobox.api.commands.CompositeCommand)}. |
| 139 | + */ |
| 140 | + @Test |
| 141 | + public void testAdminTopRemoveCommand() { |
| 142 | + assertEquals("remove", atrc.getLabel()); |
| 143 | + assertEquals("delete", atrc.getAliases().get(0)); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Test method for |
| 148 | + * {@link world.bentobox.level.commands.admin.AdminTopRemoveCommand#setup()}. |
| 149 | + */ |
| 150 | + @Test |
| 151 | + public void testSetup() { |
| 152 | + assertEquals("bskyblock.admin.top.remove", atrc.getPermission()); |
| 153 | + assertEquals("admin.top.remove.parameters", atrc.getParameters()); |
| 154 | + assertEquals("admin.top.remove.description", atrc.getDescription()); |
| 155 | + assertFalse(atrc.isOnlyPlayer()); |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Test method for |
| 161 | + * {@link world.bentobox.level.commands.admin.AdminTopRemoveCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. |
| 162 | + */ |
| 163 | + @Test |
| 164 | + public void testCanExecuteWrongArgs() { |
| 165 | + assertFalse(atrc.canExecute(user, "delete", Collections.emptyList())); |
| 166 | + verify(user).sendMessage("commands.help.header", TextVariables.LABEL, "BSkyBlock"); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Test method for {@link world.bentobox.level.commands.admin.AdminTopRemoveCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. |
| 171 | + */ |
| 172 | + @Test |
| 173 | + public void testCanExecuteUnknown() { |
| 174 | + when(pm.getUser(anyString())).thenReturn(null); |
| 175 | + assertFalse(atrc.canExecute(user, "delete", Collections.singletonList("tastybento"))); |
| 176 | + verify(user).sendMessage("general.errors.unknown-player", TextVariables.NAME, "tastybento"); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Test method for |
| 181 | + * {@link world.bentobox.level.commands.admin.AdminTopRemoveCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. |
| 182 | + */ |
| 183 | + @Test |
| 184 | + public void testCanExecuteKnown() { |
| 185 | + assertTrue(atrc.canExecute(user, "delete", Collections.singletonList("tastybento"))); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Test method for |
| 190 | + * {@link world.bentobox.level.commands.admin.AdminTopRemoveCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. |
| 191 | + */ |
| 192 | + @Test |
| 193 | + public void testExecuteUserStringListOfString() { |
| 194 | + testCanExecuteKnown(); |
| 195 | + assertTrue(atrc.execute(user, "delete", Collections.singletonList("tastybento"))); |
| 196 | + verify(manager).removeEntry(any(World.class), eq(uuid)); |
| 197 | + verify(user).sendMessage("general.success"); |
| 198 | + } |
| 199 | + |
| 200 | +} |
0 commit comments