Browse Source

refactor PlayerService tests by restructuring theme and updating the mocking validation plus update test in general

merge-requests/6/merge
Augusto Dwenger 2 years ago
parent
commit
e130455009
  1. 8
      build.gradle.kts
  2. 19
      src/main/kotlin/Main.kt
  3. 39
      src/main/kotlin/PlayerService.kt
  4. 12
      src/main/kotlin/commands/PlayTime.kt
  5. 4
      src/main/kotlin/commands/TestCommand.kt
  6. 5
      src/main/kotlin/commands/UpTime.kt
  7. 30
      src/main/kotlin/db/FilePlayerTimeDB.kt
  8. 20
      src/main/kotlin/db/PlayerTimeDB.kt
  9. 5
      src/main/kotlin/listener/JoinListener.kt
  10. 5
      src/main/kotlin/listener/QuitListener.kt
  11. 17
      src/main/kotlin/models/Player.kt
  12. 233
      src/test/kotlin/PlayerServiceTest.kt
  13. 18
      src/test/kotlin/db/FilePlayerTimeDBTest.kt
  14. 35
      src/test/kotlin/listener/JoinListenerTest.kt
  15. 35
      src/test/kotlin/listener/QuitListenerTest.kt

8
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"
}
}

19
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

39
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<UUID, Player>()
/**
@ -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")
}
}

12
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<out String>): 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
}
}

4
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 { }

5
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 {
/**

30
src/main/kotlin/db/FileDB.kt → 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
*/

20
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
*/

5
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 {
/**

5
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 {
/**

17
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
)

233
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<PlayerTimeDB>()
every { mockDB.findUUID(uuid) } returns false
every { mockDB.createPlayer(any()) } just Runs
val playerService = PlayerService(mockDB)
assertTrue(playerService.isEmpty())
val mockDB = mockk<PlayerTimeDB>()
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<PlayerTimeDB>()
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<PlayerTimeDB>()
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<PlayerTimeDB>()
every { mockDB.findUUID(uuid) } returns false
every { mockDB.createPlayer(any()) } just Runs
val mockDB = mockk<PlayerTimeDB>()
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<PlayerTimeDB>()
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<PlayerTimeDB>()
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<PlayerTimeDB>()
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<Player>()
val uuid = UUID.randomUUID()
val player = Player(uuid, "playerName")
val mockDB = mockk<PlayerTimeDB>()
every { mockDB.findUUID(uuid) } returns true
every { mockDB.readPlayer(uuid) } returns player
every { mockDB.writePlayer(player) } just Runs
val mockDB = mockk<PlayerTimeDB>()
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<PlayerTimeDB>()
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<PlayerTimeDB>()
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<PlayerTimeDB>()
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<PlayerTimeDB>()
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())
}
}

18
src/test/kotlin/db/FileDBTest.kt → 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())

35
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<Player>()
every { mockPlayer.uniqueId } returns UUID.randomUUID()
every { mockPlayer.name } returns ""
val mockPlayerJoinEvent = mockk<PlayerJoinEvent>()
every { mockPlayerJoinEvent.player } returns mockPlayer
val mockPlayerService = mockk<PlayerService>()
every { mockPlayerService.addPlayer(any(), any()) } just Runs
val joinListener = JoinListener(mockPlayerService)
joinListener.onPlayerJoin(mockPlayerJoinEvent)
verify(exactly = 1) { mockPlayerService.addPlayer(any(), any()) }
confirmVerified(mockPlayerService)
}
}

35
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<Player>()
every { mockPlayer.uniqueId } returns UUID.randomUUID()
every { mockPlayer.name } returns ""
val mockPlayerQuitEvent = mockk<PlayerQuitEvent>()
every { mockPlayerQuitEvent.player } returns mockPlayer
val mockPlayerService = mockk<PlayerService>()
every { mockPlayerService.removePlayer(any()) } just Runs
val quitListener = QuitListener(mockPlayerService)
quitListener.onPlayerQuit(mockPlayerQuitEvent)
verify(exactly = 1) { mockPlayerService.removePlayer(any()) }
confirmVerified(mockPlayerService)
}
}
Loading…
Cancel
Save