diff --git a/build.gradle.kts b/build.gradle.kts index 0512d07..8f040df 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,6 +14,11 @@ dependencies { implementation(libs.adventurePlatformBukkit) implementation(libs.paper) + testImplementation(platform(libs.junit.bom)) + testImplementation(libs.junit.jupiter) + testImplementation(libs.junit.platform.launcher) + testRuntimeOnly(libs.junit.jupiter.engine) + testImplementation(libs.mockbukkit) } java { sourceCompatibility = JavaVersion.VERSION_21 @@ -37,6 +42,14 @@ tasks { archiveClassifier.set("") archiveFileName.set("labyrinth.jar") } + + test { + useJUnitPlatform() + jvmArgs("-Dlabyrinth.insideTest=true") + testLogging { + events("passed", "skipped", "failed") + } + } } publishing { diff --git a/settings.gradle.kts b/settings.gradle.kts index 6a6aadb..7f8e41f 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -4,11 +4,13 @@ dependencyResolutionManagement { versionCatalogs { create("libs") { - version("paper", "1.20.6-R0.1-SNAPSHOT") + version("paper", "1.21.8-R0.1-SNAPSHOT") version("plugin.yml", "0.6.0") version("run-paper", "3.0.0") version("publishdata", "1.4.0") version("shadow", "9.1.0") + version("junit-bom", "5.13.4") + version("mockbukit", "4.76.0") plugin("plugin.yml", "net.minecrell.plugin-yml.paper").versionRef("plugin.yml") plugin("run.paper", "xyz.jpenilla.run-paper").versionRef("run-paper") @@ -22,6 +24,12 @@ dependencyResolutionManagement { library("paper", "io.papermc.paper", "paper-api").versionRef("paper") + library("junit-bom", "org.junit", "junit-bom").versionRef("junit-bom") + library("junit-jupiter", "org.junit.jupiter", "junit-jupiter").withoutVersion() + library("junit-jupiter-engine", "org.junit.jupiter", "junit-jupiter-engine").withoutVersion() + library("junit.platform.launcher", "org.junit.platform", "junit-platform-launcher").withoutVersion() + library("mockbukkit", "org.mockbukkit.mockbukkit", "mockbukkit-v1.21").versionRef("mockbukit") + } } repositories { diff --git a/src/test/java/net/onelitefeather/labyrinth/commands/CenterCommandTest.java b/src/test/java/net/onelitefeather/labyrinth/commands/CenterCommandTest.java new file mode 100644 index 0000000..32a761c --- /dev/null +++ b/src/test/java/net/onelitefeather/labyrinth/commands/CenterCommandTest.java @@ -0,0 +1,98 @@ +package net.onelitefeather.labyrinth.commands; + +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.onelitefeather.labyrinth.service.api.ValidationService; +import net.onelitefeather.labyrinth.utils.Constants; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.*; +import org.mockbukkit.mockbukkit.world.WorldMock; + +class CenterCommandTest extends CommandPluginTestBase { + + private CenterCommand command; + private MockValidationService validationService; + + public static class MockValidationService implements ValidationService + { + private boolean isValid; + + @Override + public boolean validateZoneInput(@NotNull Player player, @NotNull String zone) { + return isValid; + } + + public void setValid(boolean valid) { + isValid = valid; + } + + public boolean isValid() { + return isValid; + } + } + + @Override + @BeforeEach + void setUp() + { + super.setUp(); + validationService = new MockValidationService(); + command = new CenterCommand(plugin, validationService); + } + + + @DisplayName("Test if the player location is not zero") + @Test + void testIsYLocationFromPlayerNotZero() + { + var player = server.addPlayer(); + var location = new Location(new WorldMock(Material.GRASS_BLOCK, 64), 120, 64, 120); + player.setLocation(location); + + command.centerCommand(player, "Test"); + Assertions.assertNotEquals(0, location.getY()); + } + + @Test + void testValidationWrong() + { + var player = server.addPlayer(); + var location = new Location(new WorldMock(Material.GRASS_BLOCK, 64), 120, 64, 120); + player.setLocation(location); + + validationService.setValid(false); + command.centerCommand(player, "Test"); + var playerMessage = player.nextComponentMessage(); + Assertions.assertNotNull(playerMessage); + var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.ZONE_INVALID_MESSAGE, + Placeholder.component("prefix", Constants.PREFIX)); + Assertions.assertEquals(expectedMessage, playerMessage); + Assertions.assertFalse(plugin.getConfig().contains(Constants.CONFIG_ZONE_CENTER_PATH.formatted("Test"))); + } + + @Test + void testValidationTrue() + { + var player = server.addPlayer(); + var location = new Location(new WorldMock(Material.GRASS_BLOCK, 64), 120, 64, 120); + var zoneName = "Test"; + var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.CENTER_COMMAND_MESSAGE_SUCCESS, + Placeholder.unparsed("zone", zoneName), + Placeholder.component("prefix", Constants.PREFIX)); + + player.setLocation(location); + validationService.setValid(true); + command.centerCommand(player, zoneName); + location.setY(0); + var playerMessage = player.nextComponentMessage(); + + Assertions.assertNotNull(playerMessage); + Assertions.assertEquals(expectedMessage, playerMessage); + Assertions.assertTrue(plugin.getConfig().contains(Constants.CONFIG_ZONE_CENTER_PATH.formatted(zoneName))); + Assertions.assertEquals(location, plugin.getConfig().getLocation(Constants.CONFIG_ZONE_CENTER_PATH.formatted(zoneName))); + } + +} \ No newline at end of file diff --git a/src/test/java/net/onelitefeather/labyrinth/commands/CommandPluginTestBase.java b/src/test/java/net/onelitefeather/labyrinth/commands/CommandPluginTestBase.java new file mode 100644 index 0000000..92841bd --- /dev/null +++ b/src/test/java/net/onelitefeather/labyrinth/commands/CommandPluginTestBase.java @@ -0,0 +1,38 @@ +package net.onelitefeather.labyrinth.commands; + +import net.onelitefeather.labyrinth.Labyrinth; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.mockbukkit.mockbukkit.MockBukkit; +import org.mockbukkit.mockbukkit.ServerMock; + +public abstract class CommandPluginTestBase { + protected @NotNull ServerMock server; + protected Labyrinth plugin; + + public static class MockLabyrinthPlugin extends Labyrinth { + @Override + public void onEnable() { + + } + + @Override + public void onDisable() { + + } + } + + @BeforeEach + void setUp() + { + server = MockBukkit.mock(); + plugin = MockBukkit.load(CommandPluginTestBase.MockLabyrinthPlugin.class); + } + + @AfterEach + void tearDown() + { + MockBukkit.unmock(); + } +} diff --git a/src/test/java/net/onelitefeather/labyrinth/commands/CreateZoneCommandTest.java b/src/test/java/net/onelitefeather/labyrinth/commands/CreateZoneCommandTest.java new file mode 100644 index 0000000..691231e --- /dev/null +++ b/src/test/java/net/onelitefeather/labyrinth/commands/CreateZoneCommandTest.java @@ -0,0 +1,49 @@ +package net.onelitefeather.labyrinth.commands; + +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.onelitefeather.labyrinth.utils.Constants; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class CreateZoneCommandTest extends CommandPluginTestBase { + + private CreateZoneCommand command; + + @Override + @BeforeEach + void setUp() + { + super.setUp(); + command = new CreateZoneCommand(plugin); + } + + @Test + void testZoneNameNotMatchesPattern() + { + var player = server.addPlayer(); + var zoneName = "Test%"; + var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.ZONE_INVALID_MESSAGE, + Placeholder.component("prefix", Constants.PREFIX)); + command.createZone(player, zoneName); + + assertFalse(plugin.getConfig().isSet(Constants.CONFIG_ZONE_PATH.formatted(zoneName))); + assertEquals(expectedMessage, player.nextComponentMessage()); + } + + @Test + void testZoneCreated() + { + var player = server.addPlayer(); + var zoneName = "Test"; + var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.CREATE_ZONE_MESSAGE_SUCCESS, + Placeholder.unparsed("zone", zoneName), + Placeholder.component("prefix", Constants.PREFIX)); + command.createZone(player, zoneName); + + assertTrue(plugin.getConfig().isSet(Constants.CONFIG_ZONE_PATH.formatted(zoneName))); + assertEquals(expectedMessage, player.nextComponentMessage()); + } +} \ No newline at end of file diff --git a/src/test/java/net/onelitefeather/labyrinth/commands/DeleteZoneCommandTest.java b/src/test/java/net/onelitefeather/labyrinth/commands/DeleteZoneCommandTest.java new file mode 100644 index 0000000..c6ab0fa --- /dev/null +++ b/src/test/java/net/onelitefeather/labyrinth/commands/DeleteZoneCommandTest.java @@ -0,0 +1,50 @@ +package net.onelitefeather.labyrinth.commands; + +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.onelitefeather.labyrinth.utils.Constants; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class DeleteZoneCommandTest extends CommandPluginTestBase{ + + private DeleteZoneCommand command; + + @Override + @BeforeEach + void setUp() + { + super.setUp(); + command = new DeleteZoneCommand(plugin); + } + + @Test + void testZoneDeleted() + { + var player = server.addPlayer(); + var zoneName = "Test"; + var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.DELETE_ZONE_MESSAGE_SUCCESS, + Placeholder.unparsed("zone", zoneName), + Placeholder.component("prefix", Constants.PREFIX)); + var configSectionName = Constants.CONFIG_ZONE_PATH.formatted(zoneName); + plugin.getConfig().createSection(configSectionName); + command.deleteZone(player, zoneName); + assertFalse(plugin.getConfig().isSet(configSectionName)); + assertEquals(expectedMessage, player.nextComponentMessage()); + } + + @Test + void testZoneNotDeleted() + { + var player = server.addPlayer(); + var zoneName = "Test"; + var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.ZONE_INVALID_MESSAGE, + Placeholder.component("prefix", Constants.PREFIX)); + command.deleteZone(player, zoneName); + assertEquals(expectedMessage, player.nextComponentMessage()); + + } + +} \ No newline at end of file diff --git a/src/test/java/net/onelitefeather/labyrinth/commands/SetRadiusCommandTest.java b/src/test/java/net/onelitefeather/labyrinth/commands/SetRadiusCommandTest.java new file mode 100644 index 0000000..46a7b20 --- /dev/null +++ b/src/test/java/net/onelitefeather/labyrinth/commands/SetRadiusCommandTest.java @@ -0,0 +1,95 @@ +package net.onelitefeather.labyrinth.commands; + +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.onelitefeather.labyrinth.service.api.ValidationService; +import net.onelitefeather.labyrinth.utils.Constants; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockbukkit.mockbukkit.world.WorldMock; + +import static org.junit.jupiter.api.Assertions.*; + +class SetRadiusCommandTest extends CommandPluginTestBase{ + + private SetRadiusCommand command; + private SetRadiusCommandTest.MockValidationService validationService; + + public static class MockValidationService implements ValidationService + { + private boolean isValid; + + @Override + public boolean validateZoneInput(@NotNull Player player, @NotNull String zone) { + return isValid; + } + + public void setValid(boolean valid) { + isValid = valid; + } + + public boolean isValid() { + return isValid; + } + } + + @Override + @BeforeEach + void setUp() + { + super.setUp(); + validationService = new SetRadiusCommandTest.MockValidationService(); + command = new SetRadiusCommand(plugin, validationService); + } + + @Test + void testValidationWrong() + { + var player = server.addPlayer(); + var zoneName = "Test"; + var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.ZONE_INVALID_MESSAGE, + Placeholder.component("prefix", Constants.PREFIX)); + validationService.setValid(false); + command.setRadius(player, zoneName); + assertEquals(expectedMessage, player.nextComponentMessage()); + } + + @Test + void testNoLocationSet() + { + var player = server.addPlayer(); + var zoneName = "Test"; + validationService.setValid(true); + command.setRadius(player, zoneName); + assertNull(player.nextComponentMessage()); + } + + @Test + void testRadiusSet() + { + var player = server.addPlayer(); + var zoneName = "Test"; + var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.SET_RADIUS_MESSAGE, + Placeholder.unparsed("zone", zoneName), + Placeholder.component("prefix", Constants.PREFIX)); + var world = new WorldMock(Material.GRASS_BLOCK, 64); + var centerLocation = new Location(world, 120, 0, 120); + var playerLocation = new Location(world, 240, 64, 240); + + validationService.setValid(true); + plugin.getConfig().set(Constants.CONFIG_ZONE_CENTER_PATH.formatted(zoneName), centerLocation); + player.setLocation(playerLocation); + + command.setRadius(player,zoneName); + + playerLocation.setY(0); + var expectedDistance = centerLocation.distance(playerLocation); + assertEquals(expectedDistance, plugin.getConfig().getDouble(Constants.CONFIG_ZONE_RADIUS_PATH.formatted(zoneName))); + assertEquals(expectedMessage, player.nextComponentMessage()); + } + +} \ No newline at end of file diff --git a/src/test/java/net/onelitefeather/labyrinth/commands/ToggleMobSpawnCommandTest.java b/src/test/java/net/onelitefeather/labyrinth/commands/ToggleMobSpawnCommandTest.java new file mode 100644 index 0000000..8228c62 --- /dev/null +++ b/src/test/java/net/onelitefeather/labyrinth/commands/ToggleMobSpawnCommandTest.java @@ -0,0 +1,109 @@ +package net.onelitefeather.labyrinth.commands; + +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.onelitefeather.labyrinth.service.api.ValidationService; +import net.onelitefeather.labyrinth.utils.Constants; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ToggleMobSpawnCommandTest extends CommandPluginTestBase{ + + private ToggleMobSpawnCommand command; + private ToggleMobSpawnCommandTest.MockValidationService validationService; + + public static class MockValidationService implements ValidationService + { + private boolean isValid; + + @Override + public boolean validateZoneInput(@NotNull Player player, @NotNull String zone) { + return isValid; + } + + public void setValid(boolean valid) { + isValid = valid; + } + + public boolean isValid() { + return isValid; + } + } + + @Override + @BeforeEach + void setUp() + { + super.setUp(); + validationService = new ToggleMobSpawnCommandTest.MockValidationService(); + command = new ToggleMobSpawnCommand(plugin, validationService); + } + + @Test + void testZoneNotValid() + { + var player = server.addPlayer(); + var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.ZONE_INVALID_MESSAGE, + Placeholder.component("prefix", Constants.PREFIX)); + + validationService.setValid(false); + + command.toggleMobSpawn(player, "Test"); + assertEquals(expectedMessage, player.nextComponentMessage()); + } + + @Test + void testActivateMobSpawningWithNotSetInConfig() + { + var player = server.addPlayer(); + var zoneName = "Test"; + var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.TOGGLE_MOB_SPAWN_COMMAND_MESSAGE_SUCCESS, + Placeholder.unparsed("zone", zoneName), + Placeholder.component("prefix", Constants.PREFIX), + Placeholder.unparsed("value", String.valueOf(true))); + validationService.setValid(true); + + command.toggleMobSpawn(player, zoneName); + assertEquals(expectedMessage, player.nextComponentMessage()); + assertTrue(plugin.getConfig().getBoolean(Constants.CONFIG_ZONE_MOBSPAWNING_PATH.formatted(zoneName))); + } + + @Test + void testMobSpawningChangedToTrue() + { + var player = server.addPlayer(); + var zoneName = "Test"; + var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.TOGGLE_MOB_SPAWN_COMMAND_MESSAGE_SUCCESS, + Placeholder.unparsed("zone", zoneName), + Placeholder.component("prefix", Constants.PREFIX), + Placeholder.unparsed("value", String.valueOf(true))); + plugin.getConfig().set(Constants.CONFIG_ZONE_MOBSPAWNING_PATH.formatted(zoneName), false); + validationService.setValid(true); + + command.toggleMobSpawn(player, zoneName); + assertEquals(expectedMessage, player.nextComponentMessage()); + assertTrue(plugin.getConfig().getBoolean(Constants.CONFIG_ZONE_MOBSPAWNING_PATH.formatted(zoneName))); + } + + @Test + void testMobSpawningChangedToFalse() + { + var player = server.addPlayer(); + var zoneName = "Test"; + var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.TOGGLE_MOB_SPAWN_COMMAND_MESSAGE_SUCCESS, + Placeholder.unparsed("zone", zoneName), + Placeholder.component("prefix", Constants.PREFIX), + Placeholder.unparsed("value", String.valueOf(false))); + plugin.getConfig().set(Constants.CONFIG_ZONE_MOBSPAWNING_PATH.formatted(zoneName), true); + validationService.setValid(true); + + command.toggleMobSpawn(player, zoneName); + assertEquals(expectedMessage, player.nextComponentMessage()); + assertFalse(plugin.getConfig().getBoolean(Constants.CONFIG_ZONE_MOBSPAWNING_PATH.formatted(zoneName))); + } + +} \ No newline at end of file diff --git a/src/test/java/net/onelitefeather/labyrinth/listener/MobspawnListenerTest.java b/src/test/java/net/onelitefeather/labyrinth/listener/MobspawnListenerTest.java new file mode 100644 index 0000000..6dbf543 --- /dev/null +++ b/src/test/java/net/onelitefeather/labyrinth/listener/MobspawnListenerTest.java @@ -0,0 +1,166 @@ +package net.onelitefeather.labyrinth.listener; + +import net.onelitefeather.labyrinth.Labyrinth; +import net.onelitefeather.labyrinth.utils.Constants; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.event.entity.EntitySpawnEvent; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockbukkit.mockbukkit.MockBukkit; +import org.mockbukkit.mockbukkit.ServerMock; +import org.mockbukkit.mockbukkit.entity.SheepMock; +import org.mockbukkit.mockbukkit.entity.ZombieMock; +import org.mockbukkit.mockbukkit.world.WorldMock; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +class MobspawnListenerTest { + + private @NotNull ServerMock server; + private Labyrinth plugin; + private MobspawnListener listener; + + public static class MockLabyrinthPlugin extends Labyrinth { + @Override + public void onEnable() { + + } + + @Override + public void onDisable() { + + } + } + + @BeforeEach + void setUp() + { + server = MockBukkit.mock(); + plugin = MockBukkit.load(MobspawnListenerTest.MockLabyrinthPlugin.class); + listener = new MobspawnListener(this.plugin); + } + + @AfterEach + void tearDown() + { + MockBukkit.unmock(); + } + + @Test + void testNoZone() + { + this.server.getPluginManager().registerEvents(listener, this.plugin); + var zombie = new ZombieMock(this.server, UUID.randomUUID()); + zombie.setLocation(new Location(new WorldMock(Material.GRASS_BLOCK, 64), 120, 64, 120)); + var event = new EntitySpawnEvent(zombie); + this.server.getPluginManager().callEvent(event); + assertFalse(event.isCancelled()); + } + + @Test + void testNoCenterLocation() + { + this.server.getPluginManager().registerEvents(listener, this.plugin); + var zombie = new ZombieMock(this.server, UUID.randomUUID()); + zombie.setLocation(new Location(new WorldMock(Material.GRASS_BLOCK, 64), 120, 64, 120)); + var zoneName = "Test"; + var configSectionName = Constants.CONFIG_ZONE_PATH.formatted(zoneName); + plugin.getConfig().createSection(configSectionName); + var event = new EntitySpawnEvent(zombie); + this.server.getPluginManager().callEvent(event); + assertFalse(event.isCancelled()); + } + + @Test + void testNotInRadius() + { + this.server.getPluginManager().registerEvents(listener, this.plugin); + + var world = new WorldMock(Material.GRASS_BLOCK, 64); + var zombie = new ZombieMock(this.server, UUID.randomUUID()); + zombie.setLocation(new Location(world, 10, 64, 10)); + var zoneName = "Test"; + var configSectionName = Constants.CONFIG_ZONE_PATH.formatted(zoneName); + plugin.getConfig().createSection(configSectionName); + + var centerLocation = new Location(world, 120, 0, 120); + plugin.getConfig().set(Constants.CONFIG_ZONE_CENTER_PATH.formatted(zoneName), centerLocation); + plugin.getConfig().set(Constants.CONFIG_ZONE_RADIUS_PATH.formatted(zoneName), 5d); + + var event = new EntitySpawnEvent(zombie); + this.server.getPluginManager().callEvent(event); + assertFalse(event.isCancelled()); + } + + @Test + void testNotDisabled() + { + this.server.getPluginManager().registerEvents(listener, this.plugin); + + var world = new WorldMock(Material.GRASS_BLOCK, 64); + var zombie = new ZombieMock(this.server, UUID.randomUUID()); + zombie.setLocation(new Location(world, 119, 64, 120)); + var zoneName = "Test"; + var configSectionName = Constants.CONFIG_ZONE_PATH.formatted(zoneName); + plugin.getConfig().createSection(configSectionName); + + var centerLocation = new Location(world, 120, 0, 120); + plugin.getConfig().set(Constants.CONFIG_ZONE_CENTER_PATH.formatted(zoneName), centerLocation); + plugin.getConfig().set(Constants.CONFIG_ZONE_RADIUS_PATH.formatted(zoneName), 5d); + plugin.getConfig().set(Constants.CONFIG_ZONE_MOBSPAWNING_PATH.formatted(zoneName), true); + + var event = new EntitySpawnEvent(zombie); + this.server.getPluginManager().callEvent(event); + assertFalse(event.isCancelled()); + } + + @Test + void testNoSpawning() + { + this.server.getPluginManager().registerEvents(listener, this.plugin); + + var world = new WorldMock(Material.GRASS_BLOCK, 64); + var zombie = new ZombieMock(this.server, UUID.randomUUID()); + zombie.setLocation(new Location(world, 119, 64, 120)); + var zoneName = "Test"; + var configSectionName = Constants.CONFIG_ZONE_PATH.formatted(zoneName); + plugin.getConfig().createSection(configSectionName); + + var centerLocation = new Location(world, 120, 0, 120); + plugin.getConfig().set(Constants.CONFIG_ZONE_CENTER_PATH.formatted(zoneName), centerLocation); + plugin.getConfig().set(Constants.CONFIG_ZONE_RADIUS_PATH.formatted(zoneName), 5d); + plugin.getConfig().set(Constants.CONFIG_ZONE_MOBSPAWNING_PATH.formatted(zoneName), false); + + var event = new EntitySpawnEvent(zombie); + this.server.getPluginManager().callEvent(event); + assertTrue(event.isCancelled()); + } + + @Test + void testNoMonster() + { + this.server.getPluginManager().registerEvents(listener, this.plugin); + + var world = new WorldMock(Material.GRASS_BLOCK, 64); + var sheep = new SheepMock(this.server, UUID.randomUUID()); + sheep.setLocation(new Location(world, 119, 64, 120)); + var zoneName = "Test"; + var configSectionName = Constants.CONFIG_ZONE_PATH.formatted(zoneName); + plugin.getConfig().createSection(configSectionName); + + var centerLocation = new Location(world, 120, 0, 120); + plugin.getConfig().set(Constants.CONFIG_ZONE_CENTER_PATH.formatted(zoneName), centerLocation); + plugin.getConfig().set(Constants.CONFIG_ZONE_RADIUS_PATH.formatted(zoneName), 5d); + plugin.getConfig().set(Constants.CONFIG_ZONE_MOBSPAWNING_PATH.formatted(zoneName), false); + + var event = new EntitySpawnEvent(sheep); + this.server.getPluginManager().callEvent(event); + assertFalse(event.isCancelled()); + } + +} \ No newline at end of file diff --git a/src/test/java/net/onelitefeather/labyrinth/service/impl/ValidationServiceImplTest.java b/src/test/java/net/onelitefeather/labyrinth/service/impl/ValidationServiceImplTest.java new file mode 100644 index 0000000..b298e8f --- /dev/null +++ b/src/test/java/net/onelitefeather/labyrinth/service/impl/ValidationServiceImplTest.java @@ -0,0 +1,84 @@ +package net.onelitefeather.labyrinth.service.impl; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.onelitefeather.labyrinth.Labyrinth; +import net.onelitefeather.labyrinth.utils.Constants; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockbukkit.mockbukkit.MockBukkit; +import org.mockbukkit.mockbukkit.ServerMock; + +import static org.junit.jupiter.api.Assertions.*; + +class ValidationServiceImplTest { + + private @NotNull ServerMock server; + private Labyrinth plugin; + private ValidationServiceImpl validationService; + + public static class MockLabyrinthPlugin extends Labyrinth + { + @Override + public void onEnable() { + + } + + @Override + public void onDisable() { + + } + } + + @BeforeEach + void setUp() + { + server = MockBukkit.mock(); + plugin = MockBukkit.load(ValidationServiceImplTest.MockLabyrinthPlugin.class); + validationService = new ValidationServiceImpl(plugin); + } + + @AfterEach + void tearDown() + { + MockBukkit.unmock(); + } + + @Test + void testZoneNotFound() + { + var player = server.addPlayer(); + var zoneName = "Test"; + var expectedMessage = MiniMessage.miniMessage().deserialize( + "Zone could not be found!", Placeholder.unparsed("zone", zoneName)); + var validationSuccessful = validationService.validateZoneInput(player, zoneName); + assertFalse(validationSuccessful); + assertEquals(expectedMessage, player.nextComponentMessage()); + } + + @Test + void testZoneValid() + { + var player = server.addPlayer(); + var zoneName = "Test"; + plugin.getConfig().createSection(Constants.CONFIG_ZONE_PATH.formatted(zoneName)); + var validationSuccessful = validationService.validateZoneInput(player, zoneName); + assertTrue(validationSuccessful); + } + + @Test + void testZoneNameNotMatchesPattern() + { + var player = server.addPlayer(); + var zoneName = "Test%"; + var expectedMessage = Component.text("Only characters without symbols are allowed"); + plugin.getConfig().createSection(Constants.CONFIG_ZONE_PATH.formatted(zoneName)); + var validationSuccessful = validationService.validateZoneInput(player, zoneName); + assertFalse(validationSuccessful); + assertEquals(expectedMessage, player.nextComponentMessage()); + } + +} \ No newline at end of file