Skip to content

Commit

Permalink
Fixed ChallengesCommandTest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Sep 24, 2021
1 parent c21cae9 commit 7b8cab3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class ChallengesPlayerCommand extends CompositeCommand
public ChallengesPlayerCommand(ChallengesAddon addon, CompositeCommand cmd)
{
super(addon,
cmd,
addon.getChallengesSettings().getPlayerMainCommand().split(" ")[0],
addon.getChallengesSettings().getPlayerMainCommand().split(" "));
cmd,
addon.getChallengesSettings().getPlayerMainCommand().split(" ")[0],
addon.getChallengesSettings().getPlayerMainCommand().split(" "));
}


Expand All @@ -29,7 +29,7 @@ public ChallengesPlayerCommand(ChallengesAddon addon, CompositeCommand cmd)
public boolean canExecute(User user, String label, List<String> args)
{
if (!this.getIWM().inWorld(user.getWorld()) ||
Util.getWorld(this.getWorld()) != Util.getWorld(user.getWorld())) {
!Util.sameWorld(this.getWorld(), user.getWorld())) {
// Not a GameMode world.
Utils.sendMessage(user, user.getTranslation("general.errors.wrong-world"));
return false;
Expand All @@ -44,12 +44,12 @@ public boolean canExecute(User user, String label, List<String> args)
if (user.isOp() || user.hasPermission(this.getPermissionPrefix() + "admin.challenges"))
{
String topLabel = this.getIWM().getAddon(this.getWorld()).
map(GameModeAddon::getAdminCommand).
map(optionalAdminCommand -> optionalAdminCommand.map(CompositeCommand::getTopLabel).orElse(this.getTopLabel())).
orElse(this.getTopLabel());
map(GameModeAddon::getAdminCommand).
map(optionalAdminCommand -> optionalAdminCommand.map(CompositeCommand::getTopLabel).orElse(this.getTopLabel())).
orElse(this.getTopLabel());
Utils.sendMessage(user, user.getTranslation("challenges.errors.no-challenges-admin",
"[command]",
topLabel + " " + this.<ChallengesAddon>getAddon().getChallengesSettings().getAdminMainCommand().split(" ")[0]));
"[command]",
topLabel + " " + this.<ChallengesAddon>getAddon().getChallengesSettings().getAdminMainCommand().split(" ")[0]));

}
else
Expand All @@ -66,7 +66,7 @@ public boolean canExecute(User user, String label, List<String> args)
Utils.sendMessage(user, user.getTranslation("general.errors.no-island"));
return false;
} else if (ChallengesAddon.CHALLENGES_WORLD_PROTECTION.isSetForWorld(this.getWorld()) &&
!this.getIslands().locationIsOnIsland(user.getPlayer(), user.getLocation()))
!this.getIslands().locationIsOnIsland(user.getPlayer(), user.getLocation()))
{
// Do not open gui if player is not on the island, but challenges requires island for
// completion.
Expand All @@ -85,10 +85,10 @@ public boolean execute(User user, String label, List<String> args)
if (user.isPlayer())
{
ChallengesPanel.open(this.getAddon(),
this.getWorld(),
user,
this.getTopLabel(),
this.getPermissionPrefix());
this.getWorld(),
user,
this.getTopLabel(),
this.getPermissionPrefix());

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;
Expand All @@ -42,6 +43,7 @@
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.util.Util;
import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.managers.ChallengesManager;
import world.bentobox.challenges.config.Settings;
Expand All @@ -52,7 +54,7 @@
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, ChatColor.class})
@PrepareForTest({Bukkit.class, BentoBox.class, ChatColor.class, Util.class})
public class ChallengesCommandTest {

@Mock
Expand All @@ -75,7 +77,7 @@ public class ChallengesCommandTest {
private IslandWorldManager iwm;
@Mock
private GameModeAddon gameModeAddon;
@Mock

private Settings settings;

/**
Expand Down Expand Up @@ -106,7 +108,7 @@ public void setUp() throws Exception {
when(iwm.getAddon(any())).thenReturn(optionalAddon);
when(plugin.getIWM()).thenReturn(iwm);

// Game Mode Addon
// Game Mode Addon
@NonNull
Optional<CompositeCommand> optionalAdmin = Optional.of(ic);
when(gameModeAddon.getAdminCommand()).thenReturn(optionalAdmin);
Expand All @@ -124,6 +126,8 @@ public void setUp() throws Exception {
when(user.getName()).thenReturn("tastybento");
when(user.getPermissionValue(anyString(), anyInt())).thenReturn(-1);
when(user.isPlayer()).thenReturn(true);
when(user.getTranslationOrNothing(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
when(user.getWorld()).thenReturn(world);

// Mock item factory (for itemstacks)
PowerMockito.mockStatic(Bukkit.class);
Expand All @@ -143,13 +147,17 @@ public void setUp() throws Exception {
when(ChatColor.translateAlternateColorCodes(any(char.class), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));

// Settings
settings = new Settings();
when(addon.getChallengesSettings()).thenReturn(settings);
when(settings.getVisibilityMode()).thenReturn(VisibilityMode.VISIBLE);
settings.setVisibilityMode(VisibilityMode.VISIBLE);

// Island
when(plugin.getIslands()).thenReturn(im);
when(im.getIsland(any(), any(User.class))).thenReturn(island);

// Util
PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS);
when(Util.sameWorld(any(), any())).thenReturn(true);
// Command under test
cc = new ChallengesPlayerCommand(addon, ic);
}
Expand All @@ -161,18 +169,19 @@ public void setUp() throws Exception {
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");
verify(user).getTranslation("general.errors.wrong-world");
}

/**
* Test method for {@link ChallengesPlayerCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteNoChallenges() {
when(iwm.inWorld(any(World.class))).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");
verify(user).getTranslation("challenges.errors.no-challenges");
}

/**
Expand All @@ -184,8 +193,8 @@ public void testCanExecuteNoChallengesOp() {
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");
verify(user).getTranslation("challenges.errors.no-challenges-admin", "[command]", "bsb challenges");
verify(user, never()).getTranslation("challenges.errors.no-challenges");
}

/**
Expand All @@ -197,8 +206,8 @@ public void testCanExecuteNoChallengesHasPerm() {
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");
verify(user).getTranslation("challenges.errors.no-challenges-admin", "[command]", "bsb challenges");
verify(user, never()).getTranslation("challenges.errors.no-challenges");
}

/**
Expand All @@ -211,8 +220,8 @@ public void testCanExecuteNoAdminCommand() {
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");
verify(user).getTranslation("challenges.errors.no-challenges-admin", "[command]", "bsb challenges");
verify(user, never()).getTranslation("challenges.errors.no-challenges");
}

/**
Expand All @@ -222,16 +231,16 @@ public void testCanExecuteNoAdminCommand() {
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");
verify(user).getTranslation("general.errors.no-island");
}

/**
* Test method for {@link ChallengesPlayerCommand#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());
verify(user, never()).sendMessage(anyString());
}

/**
Expand All @@ -248,7 +257,7 @@ public void testExecuteUserStringListOfStringConsole() {
* Test method for {@link ChallengesPlayerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringUser() {
public void testExecuteUserStringListOfStringUser() {
assertTrue(cc.execute(user, "challenges", Collections.emptyList()));
}

Expand Down

0 comments on commit 7b8cab3

Please sign in to comment.