|
| 1 | +package world.bentobox.magiccobblestonegenerator.commands.admin; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertNotNull; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | +import static org.mockito.ArgumentMatchers.any; |
| 8 | +import static org.mockito.ArgumentMatchers.anyString; |
| 9 | +import static org.mockito.Mockito.mock; |
| 10 | +import static org.mockito.Mockito.times; |
| 11 | +import static org.mockito.Mockito.verify; |
| 12 | +import static org.mockito.Mockito.when; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.io.IOException; |
| 16 | +import java.nio.file.Files; |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.util.Comparator; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Locale; |
| 22 | +import java.util.UUID; |
| 23 | + |
| 24 | +import org.bukkit.Bukkit; |
| 25 | +import org.bukkit.World; |
| 26 | +import org.bukkit.entity.Player; |
| 27 | +import org.bukkit.inventory.ItemFactory; |
| 28 | +import org.bukkit.inventory.meta.ItemMeta; |
| 29 | +import org.eclipse.jdt.annotation.Nullable; |
| 30 | +import org.junit.After; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.runner.RunWith; |
| 34 | +import org.mockito.Mock; |
| 35 | +import org.mockito.Mockito; |
| 36 | +import org.powermock.api.mockito.PowerMockito; |
| 37 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 38 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 39 | +import org.powermock.reflect.Whitebox; |
| 40 | + |
| 41 | +import world.bentobox.bentobox.BentoBox; |
| 42 | +import world.bentobox.bentobox.api.commands.CompositeCommand; |
| 43 | +import world.bentobox.bentobox.api.user.User; |
| 44 | +import world.bentobox.bentobox.database.DatabaseSetup; |
| 45 | +import world.bentobox.bentobox.database.objects.Island; |
| 46 | +import world.bentobox.bentobox.managers.IslandWorldManager; |
| 47 | +import world.bentobox.bentobox.managers.IslandsManager; |
| 48 | +import world.bentobox.bentobox.managers.LocalesManager; |
| 49 | +import world.bentobox.bentobox.managers.PlaceholdersManager; |
| 50 | +import world.bentobox.bentobox.managers.PlayersManager; |
| 51 | +import world.bentobox.bentobox.managers.RanksManager; |
| 52 | +import world.bentobox.magiccobblestonegenerator.StoneGeneratorAddon; |
| 53 | +import world.bentobox.magiccobblestonegenerator.config.Settings; |
| 54 | + |
| 55 | +@RunWith(PowerMockRunner.class) |
| 56 | +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, DatabaseSetup.class, RanksManager.class }) |
| 57 | +public class GeneratorAdminCommandTest { |
| 58 | + |
| 59 | + @Mock |
| 60 | + private BentoBox plugin; |
| 61 | + @Mock |
| 62 | + private CompositeCommand ac; |
| 63 | + @Mock |
| 64 | + private User user; |
| 65 | + @Mock |
| 66 | + private LocalesManager lm; |
| 67 | + @Mock |
| 68 | + private StoneGeneratorAddon addon; |
| 69 | + @Mock |
| 70 | + private World world; |
| 71 | + @Mock |
| 72 | + private IslandsManager im; |
| 73 | + @Mock |
| 74 | + private @Nullable Island island; |
| 75 | + @Mock |
| 76 | + private IslandWorldManager iwm; |
| 77 | + |
| 78 | + private GeneratorAdminCommand gac; |
| 79 | + private Settings settings; |
| 80 | + @Mock |
| 81 | + private ItemMeta itemMeta; |
| 82 | + @Mock |
| 83 | + private Player mockPlayer; |
| 84 | + @Mock |
| 85 | + private PlayersManager pm; |
| 86 | + private CompositeCommand ic; |
| 87 | + private CompositeCommand why; |
| 88 | + |
| 89 | + |
| 90 | + /** |
| 91 | + * @throws java.lang.Exception |
| 92 | + */ |
| 93 | + @Before |
| 94 | + public void setUp() throws Exception { |
| 95 | + // Set up plugin |
| 96 | + BentoBox plugin = mock(BentoBox.class); |
| 97 | + Whitebox.setInternalState(BentoBox.class, "instance", plugin); |
| 98 | + world.bentobox.bentobox.Settings bbSettings = new world.bentobox.bentobox.Settings(); |
| 99 | + when(plugin.getSettings()).thenReturn(bbSettings); |
| 100 | + when(plugin.getPlayers()).thenReturn(pm); |
| 101 | + // Parent command has no aliases |
| 102 | + when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); |
| 103 | + when(ac.getWorld()).thenReturn(world); |
| 104 | + when(ac.getAddon()).thenReturn(addon); |
| 105 | + // Addon |
| 106 | + settings = new Settings(); |
| 107 | + when(addon.getSettings()).thenReturn(settings); |
| 108 | + when(addon.getIslands()).thenReturn(im); |
| 109 | + // user |
| 110 | + when(user.getLocale()).thenReturn(Locale.ENGLISH); |
| 111 | + // Target bill - default target. Non Op, online, no ban prevention permission |
| 112 | + UUID uuid = UUID.randomUUID(); |
| 113 | + when(pm.getUUID(anyString())).thenReturn(uuid); |
| 114 | + when(mockPlayer.getName()).thenReturn("bill"); |
| 115 | + when(mockPlayer.getDisplayName()).thenReturn("&Cbill"); |
| 116 | + when(mockPlayer.getUniqueId()).thenReturn(uuid); |
| 117 | + when(mockPlayer.isOp()).thenReturn(false); |
| 118 | + when(mockPlayer.isOnline()).thenReturn(true); |
| 119 | + when(mockPlayer.hasPermission(anyString())).thenReturn(false); |
| 120 | + User.getInstance(mockPlayer); |
| 121 | + when(user.getPlayer()).thenReturn(mockPlayer); |
| 122 | + // Mock item factory (for itemstacks) |
| 123 | + PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); |
| 124 | + ItemFactory itemFactory = mock(ItemFactory.class); |
| 125 | + when(Bukkit.getItemFactory()).thenReturn(itemFactory); |
| 126 | + when(itemFactory.getItemMeta(any())).thenReturn(itemMeta); |
| 127 | + // Locales |
| 128 | + LocalesManager lm = mock(LocalesManager.class); |
| 129 | + when(lm.get(any(), any())).thenAnswer(invocation -> invocation.getArgument(1, String.class)); |
| 130 | + when(plugin.getLocalesManager()).thenReturn(lm); |
| 131 | + PlaceholdersManager phm = mock(PlaceholdersManager.class); |
| 132 | + when(phm.replacePlaceholders(any(), any())).thenAnswer(invocation -> invocation.getArgument(1, String.class)); |
| 133 | + // Placeholder manager |
| 134 | + when(plugin.getPlaceholdersManager()).thenReturn(phm); |
| 135 | + when(user.getTranslation(anyString())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); |
| 136 | + // IWM |
| 137 | + when(plugin.getIWM()).thenReturn(iwm); |
| 138 | + when(iwm.getFriendlyName(world)).thenReturn("BSkyBlock World"); |
| 139 | + |
| 140 | + gac = new GeneratorAdminCommand(addon, ac); |
| 141 | + ic = gac.getSubCommand("import").get(); |
| 142 | + why = gac.getSubCommand("why").get(); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @throws java.lang.Exception |
| 147 | + */ |
| 148 | + @After |
| 149 | + public void tearDown() throws Exception { |
| 150 | + User.clearUsers(); |
| 151 | + Mockito.framework().clearInlineMocks(); |
| 152 | + deleteAll(new File("database")); |
| 153 | + deleteAll(new File("database_backup")); |
| 154 | + } |
| 155 | + |
| 156 | + private void deleteAll(File file) throws IOException { |
| 157 | + if (file.exists()) { |
| 158 | + Files.walk(file.toPath()).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); |
| 159 | + } |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + /** |
| 165 | + * Test method for {@link world.bentobox.magiccobblestonegenerator.commands.admin.GeneratorAdminCommand#GeneratorAdminCommand(world.bentobox.magiccobblestonegenerator.StoneGeneratorAddon, world.bentobox.bentobox.api.commands.CompositeCommand)}. |
| 166 | + */ |
| 167 | + @Test |
| 168 | + public void testGeneratorAdminCommand() { |
| 169 | + assertNotNull(gac); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Test method for {@link world.bentobox.magiccobblestonegenerator.commands.admin.GeneratorAdminCommand#setup()}. |
| 174 | + */ |
| 175 | + @Test |
| 176 | + public void testSetup() { |
| 177 | + assertEquals("admin.stone-generator", gac.getPermission()); |
| 178 | + assertEquals("stone-generator.commands.admin.main.parameters", gac.getParameters()); |
| 179 | + assertEquals("stone-generator.commands.admin.main.description", gac.getDescription()); |
| 180 | + assertFalse(gac.isOnlyPlayer()); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Test method for {@link world.bentobox.magiccobblestonegenerator.commands.admin.GeneratorAdminCommand.GeneratorImportCommand#setup()}. |
| 185 | + */ |
| 186 | + @Test |
| 187 | + public void testSetupImport() { |
| 188 | + assertEquals("admin.stone-generator", ic.getPermission()); |
| 189 | + assertEquals("stone-generator.commands.admin.import.parameters", ic.getParameters()); |
| 190 | + assertEquals("stone-generator.commands.admin.import.description", ic.getDescription()); |
| 191 | + assertFalse(gac.isOnlyPlayer()); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Test method for {@link world.bentobox.magiccobblestonegenerator.commands.admin.GeneratorAdminCommand.GeneratorWhyCommand#setup()}. |
| 196 | + */ |
| 197 | + @Test |
| 198 | + public void testSetupWhy() { |
| 199 | + assertEquals("admin.stone-generator.why", why.getPermission()); |
| 200 | + assertEquals("stone-generator.commands.admin.why.parameters", why.getParameters()); |
| 201 | + assertEquals("stone-generator.commands.admin.why.description", why.getDescription()); |
| 202 | + assertFalse(gac.isOnlyPlayer()); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Test method for {@link world.bentobox.magiccobblestonegenerator.commands.admin.GeneratorAdminCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. |
| 207 | + */ |
| 208 | + @Test |
| 209 | + public void testExecuteUserStringListOfString() { |
| 210 | + assertTrue(gac.execute(user, "bskyblock", List.of())); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Test method for {@link world.bentobox.magiccobblestonegenerator.commands.admin.GeneratorAdminCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. |
| 215 | + */ |
| 216 | + @Test |
| 217 | + public void testExecuteUserStringListOfStringImport() { |
| 218 | + assertTrue(ic.execute(user, "bskyblock", List.of())); |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Test method for {@link world.bentobox.magiccobblestonegenerator.commands.admin.GeneratorAdminCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. |
| 223 | + */ |
| 224 | + @Test |
| 225 | + public void testExecuteUserStringListOfStringHelp() { |
| 226 | + assertTrue(gac.execute(user, "bskyblock", List.of("help"))); |
| 227 | + verify(user).sendMessage( |
| 228 | + "commands.help.header", |
| 229 | + "[label]", |
| 230 | + "BSkyBlock World"); |
| 231 | + verify(user).getTranslationOrNothing( |
| 232 | + "stone-generator.commands.admin.main.parameters" |
| 233 | + ); |
| 234 | + verify(user).getTranslation( |
| 235 | + "stone-generator.commands.admin.main.description" |
| 236 | + ); |
| 237 | + verify(user, times(4)).isPlayer(); |
| 238 | + verify(user).sendMessage( |
| 239 | + "commands.help.syntax-no-parameters", |
| 240 | + "[usage]", |
| 241 | + "/null generator", |
| 242 | + "[description]", |
| 243 | + "stone-generator.commands.admin.main.description" |
| 244 | + ); |
| 245 | + verify(user).sendMessage( |
| 246 | + "commands.help.syntax-no-parameters", |
| 247 | + "[usage]", |
| 248 | + "/null generator import", |
| 249 | + "[description]", |
| 250 | + "stone-generator.commands.admin.import.description" |
| 251 | + ); |
| 252 | + verify(user).sendMessage( |
| 253 | + "commands.help.syntax-no-parameters", |
| 254 | + "[usage]", |
| 255 | + "/null generator why", |
| 256 | + "[description]", |
| 257 | + "stone-generator.commands.admin.why.description" |
| 258 | + ); |
| 259 | + verify(user).sendMessage( |
| 260 | + "commands.help.syntax-no-parameters", |
| 261 | + "[usage]", |
| 262 | + "/null generator database", |
| 263 | + "[description]", |
| 264 | + "stone-generator.commands.admin.database.description" |
| 265 | + ); |
| 266 | + verify(user).sendMessage( |
| 267 | + "commands.help.end" |
| 268 | + ); |
| 269 | + } |
| 270 | + |
| 271 | + /** |
| 272 | + * Test method for {@link world.bentobox.magiccobblestonegenerator.commands.admin.GeneratorAdminCommand.GeneratorWhyCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. |
| 273 | + */ |
| 274 | + @Test |
| 275 | + public void testExecuteUserStringListOfStringWhy() { |
| 276 | + assertFalse(why.execute(user, "bskyblock", List.of())); |
| 277 | + verify(user).sendMessage( |
| 278 | + "commands.help.header", |
| 279 | + "[label]", |
| 280 | + "BSkyBlock World"); |
| 281 | + verify(user).getTranslationOrNothing( |
| 282 | + "stone-generator.commands.admin.why.parameters" |
| 283 | + ); |
| 284 | + verify(user).getTranslation( |
| 285 | + "stone-generator.commands.admin.why.description" |
| 286 | + ); |
| 287 | + verify(user).sendMessage( |
| 288 | + "commands.help.end" |
| 289 | + ); |
| 290 | + } |
| 291 | + |
| 292 | + /** |
| 293 | + * Test method for {@link world.bentobox.magiccobblestonegenerator.commands.admin.GeneratorAdminCommand.GeneratorWhyCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. |
| 294 | + */ |
| 295 | + @Test |
| 296 | + public void testExecuteUserStringListOfStringWhyPlayer() { |
| 297 | + assertFalse(why.execute(user, "bskyblock", List.of("tastybento"))); |
| 298 | + verify(user).sendMessage( |
| 299 | + "stone-generator.conversations.prefixgeneral.errors.player-is-not-owner" |
| 300 | + ); |
| 301 | + } |
| 302 | + |
| 303 | +} |
0 commit comments