Challenges/src/test/java/world/bentobox/challenges/ChallengesAddonTest.java
BONNe 7ac1ab4a61
Prepare 1.0 Release (#287)
* Version 0.8.5

* Retranslated zh-CN.yml (#273)

* 50% completed.

* 60% completed.

* 63% completed.

* Completed.

* Change the indentation, some improvements.

Co-authored-by: zhangYi <apachezy@hotmail.com>

* Updated german language file (#278)

fixed double & and double whitespace

* Fixes a mistaken permission for completing multiple challenges at once.

* Fixes translated placeholders in PL translation.

Note: translation looks bad.

* Update CompleteChallengeCommandTest.java

* Upgrade to BentoBox 1.17 API changes.

Implement Pladdon functionality.
Compile against java 16 and Spigot 1.17

* Fix Java 16 compilation.

* Use BentoBox 1.17.0

* Update pom.xml

* Create Statistic Requirement for Challenges addon.

Statistic requirement is a new type of challenge that is based on Statistic page for clients.

* Switch to annotations instead of plugin.yml file.

* Move managers to a separate directory.

* Add mojang authLib instead of NMS.

* Rename classes to Selectors.

Split single and multiple item selectors for easier implementation.
Update proper locales.

* Implement customizable user panels.

Server owners can customize 3 panels:
- main panel
- gamemode selector
- multiple completions

Panel functions will be explained in docs later.

* Update all admin panels.

Admin panels will not contain better locales codding and easier-to-improve design.

Remove old and unused GUIs.

* Remove unused adapters.

Updates Challenges and ChallengesLevel objects.
Add TypeMigrationAdapter that will fix issue with renamed challenge type.

* Update commands.

Commands will now call correct GUI.

* Update Settings file.

Remove unused parts.

* Fixes ChallengesManager and Completer.

* Adds panel saving to the `/challenges/panels` directory.

* Updates locales file.

Complete rework of the locales file.
Very sorry translators :( no migration.

* Updates pom.xml

* Updates tests.

ChallengesGUITest is removed because GUI is removed.

* Update default.json

Split text into multiple lines.

* Create template YAML file.

This file format is for people who has an alergy with ingame GUI.

* Implements Template reading.

Add template loading via Admin Panel.
Improve LibraryPanel so it could find json and yml files.

* Improve coloring scheme a bit.

* Change settings file.

Add ability to change commands for addon.
Change default mode from player challenges to island challenges.

* Update Main addon class.

Move vault and level detection after everything is loaded.
Update command names.

* Update all commands.

Commands now will have an option to change their call values.

* Update default config value.

* Fixes #264

Challenges Menu will be opened only if player is in correct world.

* Changes User#sendMessage to Utils#sendMessage

This allows add "prefix" to all messages send from Challenges addon.

* Separate singe and multiple listings.

* Clean up Constants a bit.

* Add meta for items translations.

* Fix permission link.

* Translates color codes for database texts.

* Fixes a bug when global commands does not displays in tab-complete.

Remove DefaultsCommand.java as it is not used anymore.

* Fixes small bugs in translation.

* Remove unnecessary "admin" tag.

* Update default locale.

* Update latvian locale to the latest version.

* Implement multi-linguistic server support.

Now server owners can specify different name, description and reward text for each challenge and level via locales file.

Add showcase example.

* Comment out showcase translation.

* Update BentoBox version

* Update missing icons for blocks.

Some blocks cannot be displayed in GUI's, and were leaving empty spaces.
This replaces their icon with a close representative.

Fixes #286

* Add missing mob heads.

* Fixes illegal stack issues in default challenges.

#249

* Change from click-to-select to a proper next/previous page tooltip

* Add search field to the PagedSelectors.

Add missing tooltips.

* Change download icon from hopper to cobweb.

* Add missing tooltips to the CommonPagedPanel

* Add search button to the CommonPagedPanel.

Search button will allow to search elements if there are more than displayed elements.

* Add missing strings into locale.

* Reorder dependencies

The Mojang dependency was blocking out the needed Google common
packages.

* Prevent errors in TryToCompleteTest

Note - tests still fail.

* Fixed errors and tests for CompleteChallengeCommandTest

* Fixed ChallengesCommandTest tests

* Fixes tests

* Fix JavaDoc, Shade plugin settings

* Updated .gitignore

* Try different spigot API version

* Remove Vault repo because it is not needed.

* Excluded unnecessary files from shading.

* Fixes #253

Adds TeamKick and TeamLeave events to the reset check.
Do not reset challenges if data is stored per island. As in that case, they will already lose their data.

* Fixes #187

Add a new method that updates unlocked level list without changing active level. This method returns if last unlocked level was changed, and in that case it triggers whole gui rebuilding.

* Fixes #269

Disable waiver amount message for last challenge level.

* Add timeout for repeatable challenges.

Relates #71

* Implement timeout respecting in challenges completion.

Implement timeout in GUI's.

Relates #71

* Implement changing Timeout in the Challenge Edit GUI.

Relates #71

* Implement an option to set which item type will ignore metadata per challenge.

Fixes #261
Fixes #252

* Fixes failing unit-test

* Removed shade plugin from POM

* Replace GuiUtils and HeadLib to the PanelUtils library.

* Link templates to the docs.

* Remove unnecessary NMS dependency.

NMS code was used for Player Heads, but instead of NMS now it uses public mojang lib.

* Address some code quality reports from SonarCloud.

Most of the errors are just sanity checks, as the most of null-pointers were already checked in other ways.

* Fixes incorrect NEXT and PREVIOUS button descriptions.

Fixes #289

* Implement MetaData ignoring for rewards.

While required items had a metadata grouping, reward items did not have it. This will fix that.

Fixes #289

* Fix an issue when edit menu did not display item amount.

* Update lv translation.

* Fixes some small bugs with translation potion base effect.

There was an issue that it tried to translate extra effects and ignored main one.

Relates to #290

* Fix a bug with completion broadcasting

Reported via Discord.

* Update pom.xml

* Fixes a bug with `-1` repeat-times

There was a bug that prevented the challenge to be completed if negative numbers were set in the "max-repeats" value.

* Improve equal item listing.

Change when items should be grouped.
Instead of relaying strictly from ignoreMetaData set, now try to group equal elements without durability check, and use set only if that fails.

* Update German translation (#295)

* Translate de.yml via GitLocalize

* Translate de.yml via GitLocalize

* Translate de.yml via GitLocalize

Co-authored-by: Patrick <patrick.wassmuth@gmx.de>
Co-authored-by: Michael F <unhappyangel83@googlemail.com>
Co-authored-by: DAge030 <dage030@web.de>

* Fix NPEs when running tests.

Note that there are still test failures, but these are assertions and
not errors.

* Fix error in test class.

Note this does not fix the failing assertion.

* Fix failing test. Make player default to being on island.

* Fixed test failures.

* Avoid potential call with a null parameter to User.getInstance

* Check for null world

* Null check

* Added null check

* Require non-nulls.

getInventory never returns null.

* Remove various code smells.

Co-authored-by: tastybento <tastybento@wasteofplastic.com>
Co-authored-by: apachezy <50116371+apachezy@users.noreply.github.com>
Co-authored-by: zhangYi <apachezy@hotmail.com>
Co-authored-by: Qumoo <76853697+Qumoo@users.noreply.github.com>
Co-authored-by: tastybento <tastybento@users.noreply.github.com>
Co-authored-by: gitlocalize-app[bot] <55277160+gitlocalize-app[bot]@users.noreply.github.com>
Co-authored-by: Patrick <patrick.wassmuth@gmx.de>
Co-authored-by: Michael F <unhappyangel83@googlemail.com>
Co-authored-by: DAge030 <dage030@web.de>
2022-05-06 19:51:54 +03:00

429 lines
15 KiB
Java

package world.bentobox.challenges;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
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.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.UnsafeValues;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler;
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.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.Settings;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.addons.Addon.State;
import world.bentobox.bentobox.api.addons.AddonDescription;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.configuration.Config;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.DatabaseSetup.DatabaseType;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.AddonsManager;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
/**
* @author tastybento
*
*/
@SuppressWarnings("deprecation")
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class, Config.class })
public class ChallengesAddonTest {
@Mock
private User user;
@Mock
private IslandsManager im;
@Mock
private Island island;
private ChallengesAddon addon;
@Mock
private BentoBox plugin;
@Mock
private FlagsManager fm;
@Mock
private Settings settings;
@Mock
private GameModeAddon gameMode;
@Mock
private AddonsManager am;
@Mock
private BukkitScheduler scheduler;
@Mock
private PlaceholdersManager phm;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger());
//when(plugin.isEnabled()).thenReturn(true);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Placeholders manager
when(plugin.getPlaceholdersManager()).thenReturn(phm);
// Player
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
when(user.isOp()).thenReturn(false);
UUID uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
User.setPlugin(plugin);
// Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm);
// Player has island to begin with
island = mock(Island.class);
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island);
when(plugin.getIslands()).thenReturn(im);
// Locales
// Return the reference (USE THIS IN THE FUTURE)
when(user.getTranslation(Mockito.anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
// Server
PowerMockito.mockStatic(Bukkit.class);
Server server = mock(Server.class);
when(Bukkit.getServer()).thenReturn(server);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(Bukkit.getPluginManager()).thenReturn(mock(PluginManager.class));
// Addon
addon = new ChallengesAddon();
File jFile = new File("addon.jar");
List<String> lines = Arrays.asList("# ChallengesAddon Configuration", "uniqueId: config");
Path path = Paths.get("config.yml");
Files.write(path, lines, StandardCharsets.UTF_8);
try (JarOutputStream tempJarOutputStream = new JarOutputStream(new FileOutputStream(jFile))) {
addToJar(tempJarOutputStream, path);
addToJar(tempJarOutputStream, Paths.get("src/main/resources/panels/gamemode_panel.yml"));
addToJar(tempJarOutputStream, Paths.get("src/main/resources/panels/main_panel.yml"));
addToJar(tempJarOutputStream, Paths.get("src/main/resources/panels/multiple_panel.yml"));
addToJar(tempJarOutputStream, Paths.get("src/main/resources/template.yml"));
addToJar(tempJarOutputStream, Paths.get("src/main/resources/default.json"));
}
File dataFolder = new File("addons/Challenges");
addon.setDataFolder(dataFolder);
addon.setFile(jFile);
AddonDescription desc = new AddonDescription.Builder("bentobox", "challenges", "1.3").description("test").authors("BONNe").build();
addon.setDescription(desc);
// Addons manager
when(plugin.getAddonsManager()).thenReturn(am);
// One game mode
when(am.getGameModeAddons()).thenReturn(Collections.singletonList(gameMode));
AddonDescription desc2 = new AddonDescription.Builder("bentobox", "BSkyBlock", "1.3").description("test").authors("tasty").build();
when(gameMode.getDescription()).thenReturn(desc2);
// Player command
CompositeCommand cmd = mock(CompositeCommand.class);
@NonNull
Optional<CompositeCommand> opCmd = Optional.of(cmd);
when(gameMode.getPlayerCommand()).thenReturn(opCmd);
// Admin command
when(gameMode.getAdminCommand()).thenReturn(opCmd);
// Flags manager
when(plugin.getFlagsManager()).thenReturn(fm);
when(fm.getFlags()).thenReturn(Collections.emptyList());
// The database type has to be created one line before the thenReturn() to work!
when(plugin.getSettings()).thenReturn(settings);
DatabaseType value = DatabaseType.JSON;
when(settings.getDatabaseType()).thenReturn(value);
// Bukkit
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getScheduler()).thenReturn(scheduler);
ItemMeta meta = mock(ItemMeta.class);
ItemFactory itemFactory = mock(ItemFactory.class);
when(itemFactory.getItemMeta(any())).thenReturn(meta);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
UnsafeValues unsafe = mock(UnsafeValues.class);
when(unsafe.getDataVersion()).thenReturn(777);
when(Bukkit.getUnsafe()).thenReturn(unsafe);
}
private void addToJar(JarOutputStream tempJarOutputStream, Path path) throws IOException {
//Added the new files to the jar.
try (FileInputStream fis = new FileInputStream(path.toFile())) {
byte[] buffer = new byte[1024];
int bytesRead = 0;
JarEntry entry = new JarEntry(path.toString().replace("src/main/resources/", ""));
tempJarOutputStream.putNextEntry(entry);
while((bytesRead = fis.read(buffer)) != -1) {
tempJarOutputStream.write(buffer, 0, bytesRead);
}
}
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
new File("addon.jar").delete();
new File("config.yml").delete();
deleteAll(new File("addons"));
deleteAll(new File("database"));
deleteAll(new File("database_backup"));
}
private void deleteAll(File file) throws IOException {
if (file.exists()) {
Files.walk(file.toPath())
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
}
/**
* Test method for {@link world.bentobox.challenges.ChallengesAddon#onLoad()}.
*/
@Test
public void testOnLoad() {
addon.onLoad();
// Check that config.yml file has been saved
File check = new File("addons/Challenges","config.yml");
assertTrue(check.exists());
}
/**
* Test method for {@link world.bentobox.challenges.ChallengesAddon#onEnable()}.
*/
@Test
public void testOnEnableDisabledPlugin() {
when(plugin.isEnabled()).thenReturn(false);
addon.onEnable();
verify(plugin).logError("[challenges] BentoBox is not available or disabled!");
assertEquals(Addon.State.DISABLED, addon.getState());
}
/**
* Test method for {@link world.bentobox.challenges.ChallengesAddon#onEnable()}.
*/
@Test
public void testOnEnableDisabledAddon() {
when(plugin.isEnabled()).thenReturn(true);
addon.setState(State.DISABLED);
addon.onEnable();
verify(plugin).logError("[challenges] Challenges Addon is not available or disabled!");
}
/**
* Test method for {@link world.bentobox.challenges.ChallengesAddon#onEnable()}.
*/
@Test
public void testOnEnableIncompatibleDatabase() {
// The database type has to be created one line before the thenReturn() to work!
DatabaseType value = DatabaseType.YAML;
when(settings.getDatabaseType()).thenReturn(value);
when(plugin.isEnabled()).thenReturn(true);
addon.setState(State.LOADED);
addon.onEnable();
verify(plugin).logError("[challenges] BentoBox database is not compatible with Challenges Addon.");
verify(plugin).logError("[challenges] Please use JSON based database type.");
assertEquals(State.INCOMPATIBLE, addon.getState());
}
/**
* Test method for {@link world.bentobox.challenges.ChallengesAddon#onEnable()}.
*/
@Test
public void testOnEnableHooked() {
addon.onLoad();
when(plugin.isEnabled()).thenReturn(true);
addon.setState(State.LOADED);
addon.onEnable();
verify(plugin).log("[challenges] Loading challenges...");
verify(plugin, never()).logError("Challenges could not hook into AcidIsland or BSkyBlock so will not do anything!");
}
/**
* Test method for {@link world.bentobox.challenges.ChallengesAddon#onEnable()}.
*/
@Test
public void testOnEnableNotHooked() {
addon.onLoad();
when(am.getGameModeAddons()).thenReturn(Collections.emptyList());
when(plugin.isEnabled()).thenReturn(true);
addon.setState(State.LOADED);
addon.onEnable();
verify(plugin).log("[challenges] Loading challenges...");
verify(plugin).logError("[challenges] Challenges could not hook into AcidIsland or BSkyBlock so will not do anything!");
}
/**
* Test method for {@link world.bentobox.challenges.ChallengesAddon#onReload()}.
*/
@Test
public void testOnReloadNotHooked() {
addon.onReload();
verify(plugin, never()).log(anyString());
}
/**
* Test method for {@link world.bentobox.challenges.ChallengesAddon#onDisable()}.
*/
@Test
public void testOnDisable() {
this.testOnEnableHooked();
addon.onDisable();
// Verify database saved exists
File chDir = new File("database", "Challenge");
assertTrue(chDir.exists());
File lvDir = new File("database", "ChallengeLevel");
assertTrue(lvDir.exists());
}
/**
* Test method for {@link world.bentobox.challenges.ChallengesAddon#getChallengesManager()}.
*/
@Test
public void testGetChallengesManager() {
assertNull(addon.getChallengesManager());
this.testOnEnableHooked();
assertNotNull(addon.getChallengesManager());
}
/**
* Test method for {@link world.bentobox.challenges.ChallengesAddon#getPermissionPrefix()}.
*/
@Test
public void testGetPermissionPrefix() {
assertEquals("addon.", addon.getPermissionPrefix());
}
/**
* Test method for {@link world.bentobox.challenges.ChallengesAddon#getImportManager()}.
*/
@Test
public void testGetImportManager() {
assertNull(addon.getImportManager());
this.testOnEnableHooked();
assertNotNull(addon.getImportManager());
}
/**
* Test method for {@link world.bentobox.challenges.ChallengesAddon#getWebManager()}.
*/
@Test
public void testGetWebManager() {
assertNull(addon.getWebManager());
this.testOnEnableHooked();
assertNotNull(addon.getWebManager());
}
/**
* Test method for {@link world.bentobox.challenges.ChallengesAddon#getChallengesSettings()}.
*/
@Test
public void testGetChallengesSettings() {
assertNull(addon.getChallengesSettings());
addon.onLoad();
assertNotNull(addon.getChallengesSettings());
}
/**
* Test method for {@link world.bentobox.challenges.ChallengesAddon#isEconomyProvided()}.
*/
@Test
public void testIsEconomyProvided() {
assertFalse(addon.isEconomyProvided());
}
/**
* Test method for {@link world.bentobox.challenges.ChallengesAddon#getEconomyProvider()}.
*/
@Test
public void testGetEconomyProvider() {
assertNull(addon.getEconomyProvider());
}
/**
* Test method for {@link world.bentobox.challenges.ChallengesAddon#isLevelProvided()}.
*/
@Test
public void testIsLevelProvided() {
assertFalse(addon.isLevelProvided());
}
/**
* Test method for {@link world.bentobox.challenges.ChallengesAddon#getLevelAddon()}.
*/
@Test
public void testGetLevelAddon() {
assertNull(addon.getLevelAddon());
}
}