From e130455009d3b6d0e93eb4cc89c192d20885944c Mon Sep 17 00:00:00 2001 From: Augusto Dwenger Date: Sun, 29 Dec 2019 01:50:04 +0000 Subject: [PATCH] refactor PlayerService tests by restructuring theme and updating the mocking validation plus update test in general --- build.gradle.kts | 8 + src/main/kotlin/Main.kt | 19 +- src/main/kotlin/PlayerService.kt | 39 +-- src/main/kotlin/commands/PlayTime.kt | 12 +- src/main/kotlin/commands/TestCommand.kt | 4 + src/main/kotlin/commands/UpTime.kt | 5 + .../db/{FileDB.kt => FilePlayerTimeDB.kt} | 30 ++- src/main/kotlin/db/PlayerTimeDB.kt | 20 +- src/main/kotlin/listener/JoinListener.kt | 5 + src/main/kotlin/listener/QuitListener.kt | 5 + src/main/kotlin/models/Player.kt | 17 +- src/test/kotlin/PlayerServiceTest.kt | 233 ++++++++++-------- ...{FileDBTest.kt => FilePlayerTimeDBTest.kt} | 18 +- src/test/kotlin/listener/JoinListenerTest.kt | 35 +++ src/test/kotlin/listener/QuitListenerTest.kt | 35 +++ 15 files changed, 295 insertions(+), 190 deletions(-) rename src/main/kotlin/db/{FileDB.kt => FilePlayerTimeDB.kt} (59%) rename src/test/kotlin/db/{FileDBTest.kt => FilePlayerTimeDBTest.kt} (80%) create mode 100644 src/test/kotlin/listener/JoinListenerTest.kt create mode 100644 src/test/kotlin/listener/QuitListenerTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index 276454b..73e74a4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,6 +2,7 @@ plugins { kotlin("jvm") version "1.3.61" id("org.jlleitschuh.gradle.ktlint") version "9.0.0" id("com.github.johnrengelman.shadow") version "5.2.0" + id("org.jetbrains.dokka") version "0.10.0" } group = "de.augustodwenger" @@ -15,6 +16,9 @@ repositories { maven { url = uri("https://oss.sonatype.org/content/repositories/snapshots/") } + maven { + url = uri("https://dl.bintray.com/kotlin/dokka") + } } dependencies { @@ -44,4 +48,8 @@ tasks { events("passed", "skipped", "failed") } } + dokka { + outputFormat = "html" + outputDirectory = "$buildDir/dokka" + } } diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 00b5996..79ea911 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,24 +1,19 @@ import commands.PlayTime import commands.TestCommand import commands.UpTime -import db.FileDB -import java.io.File +import db.FilePlayerTimeDB import listener.JoinListener import listener.QuitListener import org.bukkit.plugin.java.JavaPlugin +/** + * Entrypoint of the plugin + * + * @property playerService A singleton of the playerService with FilePlayerTimeDB + */ class Main : JavaPlugin() { - private val storageDir = File("./plugins/PlayTime/") - private val playerService: PlayerService - - /** - * Ensures that the plugin directory exists and initializes the service - */ - init { - storageDir.mkdirs() - playerService = object : PlayerService(FileDB(storageDir)) {} - } + private val playerService = PlayerService(FilePlayerTimeDB()) /** * Gets executed when ever the server starts diff --git a/src/main/kotlin/PlayerService.kt b/src/main/kotlin/PlayerService.kt index c4fb088..bacc05f 100644 --- a/src/main/kotlin/PlayerService.kt +++ b/src/main/kotlin/PlayerService.kt @@ -8,13 +8,11 @@ import models.Player * A service to hold the all player that are currently playing * It should only be used as Singleton @see Kotlin objects * - * @param db the DB implementation which should be used to persist the state + * @property db the DB implementation which should be used to persist the state + * @property playerMap Holds all online players */ open class PlayerService(private val db: PlayerTimeDB) { - /** - * Holds all online players - */ private val playerMap = mutableMapOf() /** @@ -23,12 +21,9 @@ open class PlayerService(private val db: PlayerTimeDB) { * * @param uuid the actual uuid of a player * @param playerName for debugging because with the new api playerNames are not longer unique - * - * @exception UnsupportedOperationException if the player is already in the map. To prevent this check before adding if it's already present */ fun addPlayer(uuid: UUID, playerName: String) { - if (playerMap.contains(uuid)) throw UnsupportedOperationException("""Trying to add $playerName to the instance that already exists""") - val player: Player = if (db.findUUID(uuid)) { + val player: Player = if (db.findByID(uuid)) { val player = db.readPlayer(uuid) player.joinTime = LocalDateTime.now() player @@ -44,12 +39,8 @@ open class PlayerService(private val db: PlayerTimeDB) { * Removes a player from the map and persists its status * *@param uuid players uuid - * - * @exception IndexOutOfBoundsException if the map is empty - * @exception NoSuchElementException if the map does not contain the key */ fun removePlayer(uuid: UUID) { - validateLookUp(uuid) persistPlayer(uuid) playerMap.remove(uuid) } @@ -76,9 +67,6 @@ open class PlayerService(private val db: PlayerTimeDB) { * Updates and writes the current status to the db * * @param uuid player uuid - * - * @exception IndexOutOfBoundsException if the map is empty - * @exception NoSuchElementException if the map does not contain the key */ fun persistPlayer(uuid: UUID) { val duration = timePlayed(uuid) @@ -100,12 +88,8 @@ open class PlayerService(private val db: PlayerTimeDB) { * Returns the player with that uuid * * @param uuid player uuid - * - * @exception IndexOutOfBoundsException if the map is empty - * @exception NoSuchElementException if the map does not contain the key */ fun getPlayer(uuid: UUID): Player { - validateLookUp(uuid) return playerMap[uuid]!! } @@ -113,27 +97,10 @@ open class PlayerService(private val db: PlayerTimeDB) { * Returns the player time including the old time * * @param uuid player uuid - * - * @exception IndexOutOfBoundsException if the map is empty - * @exception NoSuchElementException if the map does not contain the key */ fun timePlayed(uuid: UUID): Duration { - validateLookUp(uuid) val player = getPlayer(uuid) val duration = Duration.between(player.joinTime, LocalDateTime.now()) return Duration.from(player.playTime).plus(duration) } - - /** - * It validates if the lookup is possible - * - * @param uuid the player uuid - * - * @exception IndexOutOfBoundsException if the map is empty - * @exception NoSuchElementException if the map does not contain the key - */ - private fun validateLookUp(uuid: UUID) { - if (playerMap.isEmpty()) throw IndexOutOfBoundsException("The map is empty") - if (!playerMap.containsKey(uuid)) throw NoSuchElementException("There is no entry for the key $uuid inside of the playerMap") - } } diff --git a/src/main/kotlin/commands/PlayTime.kt b/src/main/kotlin/commands/PlayTime.kt index 4d7df2a..e3e76a3 100644 --- a/src/main/kotlin/commands/PlayTime.kt +++ b/src/main/kotlin/commands/PlayTime.kt @@ -9,7 +9,7 @@ import org.bukkit.entity.Player /** * The command executor for the playtime * - * @param playerService the service which holds the current status + * @property playerService the service which holds the current status */ class PlayTime(private val playerService: PlayerService) : CommandExecutor { @@ -22,7 +22,10 @@ class PlayTime(private val playerService: PlayerService) : CommandExecutor { */ override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean { - if (sender is Player) { + return if (sender !is Player) { + sender.sendMessage("This command can only be executed as a player") + false + } else { val duration = playerService.timePlayed(sender.uniqueId) val days: String = @@ -31,10 +34,9 @@ class PlayTime(private val playerService: PlayerService) : CommandExecutor { if (duration.toHoursPart() < 10) """0${duration.toHoursPart()}""" else """${duration.toHoursPart()}""" val minutes: String = if (duration.toMinutesPart() < 10) """0${duration.toMinutesPart()}""" else """${duration.toMinutesPart()}""" + sender.sendMessage("""Your play time: $days:$hours:$minutes""") - return true + true } - sender.sendMessage("This command can only be executed as a player") - return false } } diff --git a/src/main/kotlin/commands/TestCommand.kt b/src/main/kotlin/commands/TestCommand.kt index deb89cd..a8ca65e 100644 --- a/src/main/kotlin/commands/TestCommand.kt +++ b/src/main/kotlin/commands/TestCommand.kt @@ -6,6 +6,10 @@ import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandSender import org.bukkit.entity.Player +/** + * Playground for testing + * Pls ignore + */ class TestCommand : CommandExecutor { private val log = KotlinLogging.logger { } diff --git a/src/main/kotlin/commands/UpTime.kt b/src/main/kotlin/commands/UpTime.kt index 4217e0d..0f5e49b 100644 --- a/src/main/kotlin/commands/UpTime.kt +++ b/src/main/kotlin/commands/UpTime.kt @@ -5,6 +5,11 @@ import org.bukkit.command.Command import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandSender +/** + * This is a command executor to print time the server is up and running. + * + * @property startTime the time the server starts. The default value is the object initialization but it's possible to change for example for testing + */ class UpTime(val startTime: LocalDateTime = LocalDateTime.now()) : CommandExecutor { /** diff --git a/src/main/kotlin/db/FileDB.kt b/src/main/kotlin/db/FilePlayerTimeDB.kt similarity index 59% rename from src/main/kotlin/db/FileDB.kt rename to src/main/kotlin/db/FilePlayerTimeDB.kt index c56e056..0036861 100644 --- a/src/main/kotlin/db/FileDB.kt +++ b/src/main/kotlin/db/FilePlayerTimeDB.kt @@ -7,40 +7,48 @@ import models.Player /** * FileDB manages the file based PlayerTime DB and provides basic functionality to interact with it + * + * @property path optional path for the storage of the data + * @property gson gson object to work with json */ -class FileDB(private val path: File) : PlayerTimeDB { +class FilePlayerTimeDB(private val path: File = File("./plugins/PlayTime/")) : PlayerTimeDB { private val gson = Gson() + /** + * Ensures that the directory exists + */ + init { + path.mkdirs() + } + /** * It looks if a certain file already exits * - * @param uuid the uuid as id for the file tha should be found + * @param uuid the id for the file that should be found * * @return if the file is present */ - override fun findUUID(uuid: UUID): Boolean { + override fun findByID(uuid: UUID): Boolean { return File(path, uuid.toString()).exists() } /** - * Creates a playerTime file + * Creates a player file * - * @param player the player for which the file should be created + * @param player the player that should be saved to the new file * - * @exception FileAlreadyExistsException is thrown if the playerTime file already exists. To prevent it check it with the findUUID method */ override fun createPlayer(player: Player) { val file = File(path, player.uuid.toString()) - if (file.exists()) throw FileAlreadyExistsException(file) - file.createNewFile() + if (!file.exists()) file.createNewFile() writePlayer(player) } /** - * reads the player out of the file + * Reads the player out of a file * - * @param uuid the uuid of the player that should be read + * @param uuid the id of the player that should be read * * @return Player */ @@ -51,7 +59,7 @@ class FileDB(private val path: File) : PlayerTimeDB { /** * Writs a player into an existing file - * To create a new player file @see createPlayer + * To create a new player file @see createPlayer(player: Player) * * @param player that should be written */ diff --git a/src/main/kotlin/db/PlayerTimeDB.kt b/src/main/kotlin/db/PlayerTimeDB.kt index 35796b0..4468e3e 100644 --- a/src/main/kotlin/db/PlayerTimeDB.kt +++ b/src/main/kotlin/db/PlayerTimeDB.kt @@ -6,35 +6,33 @@ import models.Player interface PlayerTimeDB { /** - * It looks if a certain file already exits + * It should look if a player exists * - * @param uuid the uuid as id for the file tha should be found + * @param uuid the id of the player * * @return if the file is present */ - fun findUUID(uuid: UUID): Boolean + fun findByID(uuid: UUID): Boolean /** - * Creates a playerTime file + * Should create a player record for persisting * - * @param player the player for which the file should be created - * - * @exception FileAlreadyExistsException is thrown if the playerTime file already exists. To prevent it check it with the findUUID method + * @param player the player that should be save */ fun createPlayer(player: Player) /** - * reads the player out of the file + * Should read the player * * @param uuid the uuid of the player that should be read * - * @return Player + * @return the actual player */ fun readPlayer(uuid: UUID): Player /** - * Writs a player into an existing file - * To create a new player file @see createPlayer + * Should write a player into persistence + * To create a new player file @see createPlayer(player: Player) * * @param player that should be written */ diff --git a/src/main/kotlin/listener/JoinListener.kt b/src/main/kotlin/listener/JoinListener.kt index c2946b9..8dab8b0 100644 --- a/src/main/kotlin/listener/JoinListener.kt +++ b/src/main/kotlin/listener/JoinListener.kt @@ -5,6 +5,11 @@ import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.player.PlayerJoinEvent +/** + * The listener for the PlayerJoinEvent + * + * @property playerService the playService to add player to it + */ class JoinListener(private val playerService: PlayerService) : Listener { /** diff --git a/src/main/kotlin/listener/QuitListener.kt b/src/main/kotlin/listener/QuitListener.kt index a0641b2..b41cb33 100644 --- a/src/main/kotlin/listener/QuitListener.kt +++ b/src/main/kotlin/listener/QuitListener.kt @@ -5,6 +5,11 @@ import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.player.PlayerQuitEvent +/** + * The listener for the PlayerQuitEvent + * + * @property playerService the playService to remove player from it + */ class QuitListener(private val playerService: PlayerService) : Listener { /** diff --git a/src/main/kotlin/models/Player.kt b/src/main/kotlin/models/Player.kt index 0665a0e..af7ac0e 100644 --- a/src/main/kotlin/models/Player.kt +++ b/src/main/kotlin/models/Player.kt @@ -4,4 +4,19 @@ import java.time.Duration import java.time.LocalDateTime import java.util.UUID -data class Player(val uuid: UUID, val playerName: String, var playTime: Duration = Duration.ZERO, var joinTime: LocalDateTime = LocalDateTime.now(), var lastSave: LocalDateTime? = null) +/** + * The representation of a in game player for the online time + * + * @property uuid the unique identify of the player given by Mojang + * @property playerName the username of the player which is not that important due to the fact that a player can change it. It should be used only for debugging purposes + * @property playTime the actual playtime. The default value for initialization is zero + * @property joinTime the last time when the player join the server# + * @property lastSave should hold the time when it was last saved if it wasn't it should be null + */ +data class Player( + val uuid: UUID, + val playerName: String, + var playTime: Duration = Duration.ZERO, + var joinTime: LocalDateTime = LocalDateTime.now(), + var lastSave: LocalDateTime? = null +) diff --git a/src/test/kotlin/PlayerServiceTest.kt b/src/test/kotlin/PlayerServiceTest.kt index a4c0fbb..abb9bd2 100644 --- a/src/test/kotlin/PlayerServiceTest.kt +++ b/src/test/kotlin/PlayerServiceTest.kt @@ -12,76 +12,76 @@ import kotlin.test.assertEquals import kotlin.test.assertNotEquals import models.Player import org.junit.jupiter.api.Assertions.assertFalse -import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test class PlayerServiceTest { - @Test - fun `should add new player`() { - val uuid = UUID.randomUUID() + @Nested + inner class AddPlayerTest { + @Test + fun `should add new player`() { + val uuid = UUID.randomUUID() - val mockDB = mockk() - every { mockDB.findUUID(uuid) } returns false - every { mockDB.createPlayer(any()) } just Runs - - val playerService = PlayerService(mockDB) - assertTrue(playerService.isEmpty()) + val mockDB = mockk() + every { mockDB.findByID(uuid) } returns false + every { mockDB.createPlayer(any()) } just Runs - playerService.addPlayer(uuid, "playerName") + val playerService = PlayerService(mockDB) + assertTrue(playerService.isEmpty()) - assertFalse(playerService.isEmpty()) - assertTrue(playerService.containsPlayer(uuid)) + playerService.addPlayer(uuid, "playerName") - verify(exactly = 1) { mockDB.findUUID(uuid) } - verify(exactly = 1) { mockDB.createPlayer(any()) } - confirmVerified(mockDB) - } + assertFalse(playerService.isEmpty()) + assertTrue(playerService.containsPlayer(uuid)) - @Test - fun `should add existing player`() { - val uuid = UUID.randomUUID() - val player = Player(uuid, "playerName") - player.joinTime = LocalDateTime.now().minusDays(1) - val originalPlayer = player.copy() + verify(exactly = 1) { mockDB.findByID(uuid) } + verify(exactly = 1) { mockDB.createPlayer(any()) } + confirmVerified(mockDB) + } - val mockDB = mockk() - every { mockDB.findUUID(uuid) } returns true - every { mockDB.readPlayer(uuid) } returns player + @Test + fun `should add existing player`() { + val uuid = UUID.randomUUID() + val player = Player(uuid, "playerName") - val playerService = PlayerService(mockDB) + val mockDB = mockk() + every { mockDB.findByID(any()) } returns true + every { mockDB.readPlayer(any()) } returns player - assertTrue(playerService.isEmpty()) - playerService.addPlayer(uuid, "playerName") + val playerService = PlayerService(mockDB) - assertFalse(playerService.isEmpty()) - assertTrue(playerService.containsPlayer(uuid)) - val extract = playerService.getPlayer(uuid) + assertTrue(playerService.isEmpty()) + playerService.addPlayer(uuid, "playerName") - assertNotEquals(originalPlayer.joinTime, extract.joinTime) + assertFalse(playerService.isEmpty()) + assertTrue(playerService.containsPlayer(uuid)) - verify(exactly = 1) { mockDB.findUUID(uuid) } - verify(exactly = 1) { mockDB.readPlayer(uuid) } - confirmVerified(mockDB) - } + verify(exactly = 1) { mockDB.findByID(any()) } + verify(exactly = 1) { mockDB.readPlayer(any()) } + confirmVerified(mockDB) + } - @Test - fun `throws exception if the uuid is already in the map`() { - val uuid = UUID.randomUUID() + @Test + fun `should change join time by existing player`() { + val uuid = UUID.randomUUID() + val player = Player(uuid, "playerName") + player.joinTime = LocalDateTime.now().minusDays(1) + val originalPlayer = player.copy() - val mockDB = mockk() - every { mockDB.findUUID(uuid) } returns false - every { mockDB.createPlayer(any()) } just Runs + val mockDB = mockk() + every { mockDB.findByID(any()) } returns true + every { mockDB.readPlayer(any()) } returns player - val playerService = PlayerService(mockDB) - playerService.addPlayer(uuid, "playerName") + val playerService = PlayerService(mockDB) - val exception: Exception = assertThrows(UnsupportedOperationException::class.java) { playerService.addPlayer(uuid, "playerName") - } - assertEquals("Trying to add playerName to the instance that already exists", exception.message) + val extract = playerService.getPlayer(uuid) + + assertNotEquals(originalPlayer.joinTime, extract.joinTime) + } } @Test @@ -90,9 +90,9 @@ class PlayerServiceTest { val player = Player(uuid, "playerName") val mockDB = mockk() - every { mockDB.findUUID(uuid) } returns true - every { mockDB.readPlayer(uuid) } returns player - every { mockDB.writePlayer(player) } just Runs + every { mockDB.findByID(any()) } returns true + every { mockDB.readPlayer(any()) } returns player + every { mockDB.writePlayer(any()) } just Runs val playerService = PlayerService(mockDB) playerService.addPlayer(uuid, "playerName") @@ -102,71 +102,101 @@ class PlayerServiceTest { playerService.removePlayer(uuid) assertTrue(playerService.isEmpty()) - - verify(exactly = 1) { mockDB.writePlayer(player) } } - @Test - fun `persist a player from the map`() { - val uuid = UUID.randomUUID() - val player = Player(uuid, "playerName") + @Nested + inner class PersistPlayerTest { - val mockDB = mockk() - every { mockDB.findUUID(uuid) } returns true - every { mockDB.readPlayer(uuid) } returns player - every { mockDB.writePlayer(player) } just Runs + @Test + fun `persist a player from the map to the db`() { + val uuid = UUID.randomUUID() + val player = Player(uuid, "playerName") - val playerService = PlayerService(mockDB) - playerService.addPlayer(uuid, "playerName") + val mockDB = mockk() + every { mockDB.findByID(any()) } returns true + every { mockDB.readPlayer(any()) } returns player + every { mockDB.writePlayer(any()) } just Runs - assertFalse(playerService.isEmpty()) + val playerService = PlayerService(mockDB) + playerService.addPlayer(uuid, "playerName") - playerService.persistPlayer(uuid) + playerService.persistPlayer(uuid) - verify(exactly = 1) { mockDB.writePlayer(player) } - } + verify(exactly = 1) { mockDB.writePlayer(any()) } + } - @Test - fun `persist all player from the map`() { - val uuid = UUID.randomUUID() - val player = Player(uuid, "playerName") + @Test + fun `persist a player from the map to the db with updated playtime`() { + val captureList = mutableListOf() + val uuid = UUID.randomUUID() + val player = Player(uuid, "playerName") - val mockDB = mockk() - every { mockDB.findUUID(uuid) } returns true - every { mockDB.readPlayer(uuid) } returns player - every { mockDB.writePlayer(player) } just Runs + val mockDB = mockk() + every { mockDB.findByID(any()) } returns true + every { mockDB.readPlayer(any()) } returns player + every { mockDB.writePlayer(capture(captureList)) } just Runs - val playerService = PlayerService(mockDB) - playerService.addPlayer(uuid, "playerName") + val playerService = PlayerService(mockDB) + playerService.addPlayer(uuid, "playerName") - assertFalse(playerService.isEmpty()) + playerService.persistPlayer(uuid) + + assertEquals(1, captureList.size) + assertNotEquals(Duration.ZERO, captureList.first().playTime) + + verify(exactly = 1) { mockDB.writePlayer(any()) } + } + + @Test + fun `persist all player from the map`() { + val uuid = UUID.randomUUID() + val uuid2 = UUID.randomUUID() + + val player = Player(uuid, "playerName") + val player2 = Player(uuid, "playerName") + + val mockDB = mockk() + every { mockDB.findByID(any()) } returns true + every { mockDB.readPlayer(uuid) } returns player + every { mockDB.readPlayer(uuid2) } returns player2 + every { mockDB.writePlayer(any()) } just Runs - playerService.persistAllPlayer() + val playerService = PlayerService(mockDB) + playerService.addPlayer(uuid, "playerName") + playerService.addPlayer(uuid2, "playerName") + + assertFalse(playerService.isEmpty()) + + playerService.persistAllPlayer() - verify(exactly = 1) { mockDB.writePlayer(player) } + verify(exactly = 2) { mockDB.writePlayer(any()) } + } } - @Test - fun `get player`() { - val uuid = UUID.randomUUID() - val player = Player(uuid, "playerName") + @Nested + inner class GetPlayerTest { - val mockDB = mockk() - every { mockDB.findUUID(uuid) } returns true - every { mockDB.readPlayer(uuid) } returns player - every { mockDB.writePlayer(player) } just Runs + @Test + fun `get player`() { + val uuid = UUID.randomUUID() + val player = Player(uuid, "playerName") - val playerService = PlayerService(mockDB) + val mockDB = mockk() + every { mockDB.findByID(any()) } returns true + every { mockDB.readPlayer(any()) } returns player - playerService.addPlayer(uuid, "playerName") + val playerService = PlayerService(mockDB) - assertFalse(playerService.isEmpty()) + playerService.addPlayer(uuid, "playerName") - val extractedPlayer = playerService.getPlayer(uuid) + assertFalse(playerService.isEmpty()) - assertEquals(player.uuid, extractedPlayer.uuid) - assertEquals(player.playerName, extractedPlayer.playerName) - assertEquals(player.playTime, extractedPlayer.playTime) + val extractedPlayer = playerService.getPlayer(uuid) + + assertEquals(player.uuid, extractedPlayer.uuid) + assertEquals(player.playerName, extractedPlayer.playerName) + assertEquals(player.playTime, extractedPlayer.playTime) + } } @Test @@ -175,8 +205,8 @@ class PlayerServiceTest { val player = Player(uuid, "playerName") val mockDB = mockk() - every { mockDB.findUUID(uuid) } returns true - every { mockDB.readPlayer(uuid) } returns player + every { mockDB.findByID(any()) } returns true + every { mockDB.readPlayer(any()) } returns player val playerService = PlayerService(mockDB) @@ -184,14 +214,7 @@ class PlayerServiceTest { assertFalse(playerService.isEmpty()) - assertNotEquals(0, playerService.timePlayed(uuid).toNanos()) + assertNotEquals(Duration.ZERO, playerService.timePlayed(uuid)) assertEquals(Duration.ZERO, playerService.getPlayer(uuid).playTime) } - - @Test - fun `duration`() { - - val duration = Duration.ZERO.plusSeconds(100) - assertEquals(200, duration.plus(Duration.ZERO.plusSeconds(100)).toSeconds()) - } } diff --git a/src/test/kotlin/db/FileDBTest.kt b/src/test/kotlin/db/FilePlayerTimeDBTest.kt similarity index 80% rename from src/test/kotlin/db/FileDBTest.kt rename to src/test/kotlin/db/FilePlayerTimeDBTest.kt index 33a04d1..2312b58 100644 --- a/src/test/kotlin/db/FileDBTest.kt +++ b/src/test/kotlin/db/FilePlayerTimeDBTest.kt @@ -11,7 +11,7 @@ import models.Player import org.junit.jupiter.api.Assertions.assertFalse import org.junit.jupiter.api.Assertions.assertTrue -internal class FileDBTest { +internal class FilePlayerTimeDBTest { private var dbDir = createTempDir() @@ -28,25 +28,25 @@ internal class FileDBTest { @Test fun `find user over uuid file name`() { val uuid = UUID.randomUUID() - val fileDB = FileDB(dbDir) + val fileDB = FilePlayerTimeDB(dbDir) - assertFalse(fileDB.findUUID(uuid)) + assertFalse(fileDB.findByID(uuid)) File(dbDir, uuid.toString()).createNewFile() - assertTrue(fileDB.findUUID(uuid)) + assertTrue(fileDB.findByID(uuid)) } @Test fun `create new player file`() { val uuid = UUID.randomUUID() - val fileDB = FileDB(dbDir) + val fileDB = FilePlayerTimeDB(dbDir) val player = Player(uuid, "username") - assertFalse(fileDB.findUUID(uuid)) + assertFalse(fileDB.findByID(uuid)) fileDB.createPlayer(player) - assertTrue(fileDB.findUUID(uuid)) + assertTrue(fileDB.findByID(uuid)) val writtenFile = File(dbDir, uuid.toString()) assertEquals(player, Gson().fromJson(writtenFile.readText(), Player::class.java)) @@ -55,7 +55,7 @@ internal class FileDBTest { @Test fun `read player from file`() { val uuid = UUID.randomUUID() - val fileDB = FileDB(dbDir) + val fileDB = FilePlayerTimeDB(dbDir) val player = Player(uuid, "username") File(dbDir, uuid.toString()).writeText(Gson().toJson(player)) @@ -66,7 +66,7 @@ internal class FileDBTest { @Test fun `write player to file`() { val uuid = UUID.randomUUID() - val fileDB = FileDB(dbDir) + val fileDB = FilePlayerTimeDB(dbDir) val player = Player(uuid, "username") val file = File(dbDir, uuid.toString()) diff --git a/src/test/kotlin/listener/JoinListenerTest.kt b/src/test/kotlin/listener/JoinListenerTest.kt new file mode 100644 index 0000000..4b66e60 --- /dev/null +++ b/src/test/kotlin/listener/JoinListenerTest.kt @@ -0,0 +1,35 @@ +package listener + +import PlayerService +import io.mockk.Runs +import io.mockk.confirmVerified +import io.mockk.every +import io.mockk.just +import io.mockk.mockk +import io.mockk.verify +import java.util.UUID +import org.bukkit.entity.Player +import org.bukkit.event.player.PlayerJoinEvent +import org.junit.jupiter.api.Test + +class JoinListenerTest { + + @Test + fun `should call to add a player`() { + val mockPlayer = mockk() + every { mockPlayer.uniqueId } returns UUID.randomUUID() + every { mockPlayer.name } returns "" + + val mockPlayerJoinEvent = mockk() + every { mockPlayerJoinEvent.player } returns mockPlayer + + val mockPlayerService = mockk() + every { mockPlayerService.addPlayer(any(), any()) } just Runs + + val joinListener = JoinListener(mockPlayerService) + joinListener.onPlayerJoin(mockPlayerJoinEvent) + + verify(exactly = 1) { mockPlayerService.addPlayer(any(), any()) } + confirmVerified(mockPlayerService) + } +} diff --git a/src/test/kotlin/listener/QuitListenerTest.kt b/src/test/kotlin/listener/QuitListenerTest.kt new file mode 100644 index 0000000..a95303c --- /dev/null +++ b/src/test/kotlin/listener/QuitListenerTest.kt @@ -0,0 +1,35 @@ +package listener + +import PlayerService +import io.mockk.Runs +import io.mockk.confirmVerified +import io.mockk.every +import io.mockk.just +import io.mockk.mockk +import io.mockk.verify +import java.util.UUID +import org.bukkit.entity.Player +import org.bukkit.event.player.PlayerQuitEvent +import org.junit.jupiter.api.Test + +class QuitListenerTest { + + @Test + fun `should call to add a player`() { + val mockPlayer = mockk() + every { mockPlayer.uniqueId } returns UUID.randomUUID() + every { mockPlayer.name } returns "" + + val mockPlayerQuitEvent = mockk() + every { mockPlayerQuitEvent.player } returns mockPlayer + + val mockPlayerService = mockk() + every { mockPlayerService.removePlayer(any()) } just Runs + + val quitListener = QuitListener(mockPlayerService) + quitListener.onPlayerQuit(mockPlayerQuitEvent) + + verify(exactly = 1) { mockPlayerService.removePlayer(any()) } + confirmVerified(mockPlayerService) + } +}