Skip to content

Commit

Permalink
More unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nossr50 committed Aug 27, 2023
1 parent c1a4cc2 commit 2d8bdbd
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 35 deletions.
25 changes: 21 additions & 4 deletions src/test/java/com/gmail/nossr50/MMOTestEnvironmentBasic.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent;
import com.gmail.nossr50.listeners.SelfListener;
import com.gmail.nossr50.util.*;
import com.gmail.nossr50.util.blockmeta.ChunkManager;
Expand All @@ -21,6 +22,8 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.plugin.PluginManager;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

Expand Down Expand Up @@ -50,7 +53,7 @@ public abstract class MMOTestEnvironmentBasic {
protected GeneralConfig generalConfig;
protected RankConfig rankConfig;
protected SkillTools skillTools;
protected Server server;
protected Server mockedServer;
protected PluginManager pluginManager;
protected World world;

Expand All @@ -69,6 +72,16 @@ public abstract class MMOTestEnvironmentBasic {

protected ConsoleCommandSender consoleCommandSender;

@BeforeEach
void setUp() {
mockBaseEnvironment();
}

@AfterEach
void tearDown() {
cleanupBaseEnvironment();
}

protected void mockBaseEnvironment() {
mockedMcMMO = Mockito.mockStatic(mcMMO.class);
mcMMO.p = mock(mcMMO.class);
Expand Down Expand Up @@ -113,12 +126,16 @@ protected void mockBaseEnvironment() {
mockedRankUtils = Mockito.mockStatic(RankUtils.class);

// wire server
this.server = mock(Server.class);
when(mcMMO.p.getServer()).thenReturn(server);
this.mockedServer = mock(Server.class);
when(mcMMO.p.getServer()).thenReturn(mockedServer);

// wire plugin manager
this.pluginManager = mock(PluginManager.class);
when(server.getPluginManager()).thenReturn(pluginManager);
when(mockedServer.getPluginManager()).thenReturn(pluginManager);
Mockito.doAnswer(invocation -> {
selfListener.onPlayerLevelUp(invocation.getArgument(0));
return null;
}).when(pluginManager).callEvent(any(McMMOPlayerLevelUpEvent.class));

// wire world
this.world = mock(World.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,128 @@
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent;
import com.gmail.nossr50.mcMMO;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import com.gmail.nossr50.util.EventUtils;
import org.bukkit.Bukkit;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.Set;
import java.util.function.BiPredicate;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

class LevelUpCommandTest extends MMOTestEnvironmentBasic {
private final PrimarySkillType skill = PrimarySkillType.MINING;
private final PrimarySkillType otherSkill = PrimarySkillType.WOODCUTTING;

@BeforeEach
void setUp() {
mockBaseEnvironment();
}
@Test
void levelUpShouldRunCommandFiveTimes() {
// GIVEN level up command for Mining should always execute for Mining level up
assert mcMMO.p.getLevelUpCommandManager().isEmpty();
final String commandStr = "say hello";
final LevelUpCommand levelUpCommand
= buildLevelUpCommand(commandStr, (s, ignored) -> s == skill);
mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);

// WHEN player gains 5 levels in mining via command
assertEquals(0, mmoPlayer.getSkillLevel(skill));
int levelsGained = 5;
EventUtils.tryLevelChangeEvent(
player,
skill,
levelsGained,
mmoPlayer.getProfile().getSkillXpLevelRaw(skill),
true,
XPGainReason.COMMAND);

@AfterEach
void tearDown() {
cleanupBaseEnvironment();
// THEN the command should be checked for execution
verify(levelUpCommandManager).apply(any(), any(), any());
verify(levelUpCommand).process(any(), any(), any());
// THEN the command should have executed
verify(levelUpCommand, times(levelsGained)).executeCommand(any(McMMOPlayer.class), any(PrimarySkillType.class), anyInt());
mockedBukkit.verify(() -> Bukkit.dispatchCommand(any(), any()), atLeast(5));
}

@Test
void levelInMiningShouldRunCommandFiveTimes() {
void levelUpShouldRunCommandFiveTimesWithPlaceholders() {
// GIVEN level up command for Mining should always execute for Mining level up
assert mcMMO.p.getLevelUpCommandManager().isEmpty();
final String commandStr = "say hello";
String playerName = "Momshroom";
when (player.getName()).thenReturn(playerName);
assertEquals(player.getName(), playerName);

final String commandStr = "say hello %player%";
final String expectedStr = "say hello " + playerName;
final LevelUpCommand levelUpCommand
= buildLevelUpCommand(commandStr, (skill, ignored) -> skill == PrimarySkillType.MINING);
= buildLevelUpCommand(commandStr, (s, ignored) -> s == skill);
mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
int levelsGained = 5;

// WHEN player gains 5 levels in mining
McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, skill, levelsGained, XPGainReason.PVE);
selfListener.onPlayerLevelUp(event);

// THEN the command should be checked for execution
verify(levelUpCommandManager).apply(any(), any(), any());
verify(levelUpCommand).process(any(), any(), any());
// THEN the command should have executed
verify(levelUpCommand, times(levelsGained)).executeCommand(any(McMMOPlayer.class), any(PrimarySkillType.class), anyInt());
// verify that Bukkit.dispatchCommand got executed at least 5 times with the correct injectedCommand
mockedBukkit.verify(() -> Bukkit.dispatchCommand(any(), eq(expectedStr)), atLeast(5));
}

@Test
void levelUpShouldRunCommandFiveTimesWithPlaceholdersForLevel() {
// GIVEN level up command for Mining should always execute for Mining level up
assert mcMMO.p.getLevelUpCommandManager().isEmpty();
String playerName = "Momshroom";
when (player.getName()).thenReturn(playerName);
assertEquals(player.getName(), playerName);

final String commandStr = "say hello %player%, you have reached level %level%";
final String expectedStr1 = "say hello " + playerName + ", you have reached level 1";
final String expectedStr2 = "say hello " + playerName + ", you have reached level 2";
final String expectedStr3 = "say hello " + playerName + ", you have reached level 3";
final String expectedStr4 = "say hello " + playerName + ", you have reached level 4";
final String expectedStr5 = "say hello " + playerName + ", you have reached level 5";

final LevelUpCommand levelUpCommand
= buildLevelUpCommand(commandStr, (s, ignored) -> s == skill);
mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
int levelsGained = 5;

// WHEN player gains 5 levels in mining
McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, PrimarySkillType.MINING, levelsGained, XPGainReason.PVE);
McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, skill, levelsGained, XPGainReason.PVE);
selfListener.onPlayerLevelUp(event);

// THEN the command should be checked for execution
verify(levelUpCommandManager).apply(any(), any(), any());
verify(levelUpCommand).process(any(), any(), any());
// THEN the command should have executed
verify(levelUpCommand, times(levelsGained)).executeCommand(any(McMMOPlayer.class), any(PrimarySkillType.class), anyInt());
// verify that Bukkit.dispatchCommand got executed at least 5 times with the correct injectedCommand
mockedBukkit.verify(() -> Bukkit.dispatchCommand(any(), eq(expectedStr1)));
mockedBukkit.verify(() -> Bukkit.dispatchCommand(any(), eq(expectedStr2)));
mockedBukkit.verify(() -> Bukkit.dispatchCommand(any(), eq(expectedStr3)));
mockedBukkit.verify(() -> Bukkit.dispatchCommand(any(), eq(expectedStr4)));
mockedBukkit.verify(() -> Bukkit.dispatchCommand(any(), eq(expectedStr5)));
}

@Test
void levelInMiningShouldRunCommandAtLeastOnce() {
void levelUpShouldRunCommandAtLeastOnce() {
// GIVEN level up command for Mining should always execute for Mining level up
assert mcMMO.p.getLevelUpCommandManager().isEmpty();
final String commandStr = "say hello";
final LevelUpCommand levelUpCommand
= buildLevelUpCommand(commandStr, (skill, ignored) -> skill == PrimarySkillType.MINING);
= buildLevelUpCommand(commandStr, (s, ignored) -> s == skill);
mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);

int levelsGained = 1;
// WHEN player gains 5 levels in mining
McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, PrimarySkillType.MINING, levelsGained, XPGainReason.PVE);
McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, skill, levelsGained, XPGainReason.PVE);
selfListener.onPlayerLevelUp(event);

// THEN the command should be checked for execution
Expand All @@ -72,18 +138,18 @@ void levelInMiningShouldRunCommandAtLeastOnce() {
}

@Test
void levelInMiningShouldNotRunCommand() {
void levelUpShouldNotRunCommand() {
// GIVEN level up command for Woodcutting should not execute for Mining level up
assert mcMMO.p.getLevelUpCommandManager().isEmpty();
final String commandStr = "say hello";
final LevelUpCommand levelUpCommand
= buildLevelUpCommand(commandStr, (skill, ignored) -> skill == PrimarySkillType.WOODCUTTING);
= buildLevelUpCommand(commandStr, (s, ignored) -> s == otherSkill);
mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);


int levelsGained = 5;
// WHEN player gains 5 levels in mining
McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, PrimarySkillType.MINING, levelsGained, XPGainReason.PVE);
McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, skill, levelsGained, XPGainReason.PVE);
selfListener.onPlayerLevelUp(event);

// THEN the command should be checked for execution
Expand All @@ -93,18 +159,6 @@ void levelInMiningShouldNotRunCommand() {
verify(levelUpCommand, never()).executeCommand(any(McMMOPlayer.class), any(PrimarySkillType.class), anyInt());
}

private LevelUpCommand buildLevelUpCommand(String commandStr, Set<Integer> levels, Set<PrimarySkillType> skillFilter) {
LevelUpCommand.LevelUpCommandBuilder builder = new LevelUpCommand.LevelUpCommandBuilder();
builder.command(commandStr)
.withLevels(levels)
.withLogInfo(true);
if (skillFilter != null) {
builder.withSkillFilter(skillFilter);
}
builder.withLogInfo(true);
return Mockito.spy(builder.build());
}

private LevelUpCommand buildLevelUpCommand(String commandStr, BiPredicate<PrimarySkillType, Integer> predicate) {
LevelUpCommand.LevelUpCommandBuilder builder = new LevelUpCommand.LevelUpCommandBuilder();
builder.command(commandStr)
Expand Down

0 comments on commit 2d8bdbd

Please sign in to comment.