Skip to content

Commit

Permalink
update krate dependency, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
makeevrserg committed May 9, 2024
1 parent e06cbc8 commit 1d0fef9
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 26 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ makeevrserg.java.ktarget=17
# Project
makeevrserg.project.name=AspeKt
makeevrserg.project.group=ru.astrainteractive.aspekt
makeevrserg.project.version.string=2.19.5
makeevrserg.project.version.string=2.19.6
makeevrserg.project.description=Essentials plugin for EmpireProjekt
makeevrserg.project.developers=makeevrserg|Makeev Roman|[email protected]
makeevrserg.project.url=https://empireprojekt.ru
Expand Down
13 changes: 11 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[versions]
# Kotlin
kotlin-version = "1.9.23" # https://github.com/JetBrains/kotlin
kotlin-benchmark = "0.4.10" # https://github.com/Kotlin/kotlinx-benchmark
kotlin-coroutines = "1.8.0" # https://github.com/Kotlin/kotlinx.coroutines
kotlin-json = "1.6.3" # https://github.com/Kotlin/kotlinx.serialization
kotlin-kaml = "0.58.0" # https://github.com/charleskorn/kaml
Expand All @@ -21,7 +22,7 @@ minecraft-spigot = "1.20.4-R0.1-SNAPSHOT" # https://github.com/PaperMC/Paper
minecraft-papi = "2.11.5" # https://github.com/PlaceholderAPI/PlaceholderAPI
minecraft-protocollib = "4.8.0" # https://github.com/dmulloy2/ProtocolLib
minecraft-vault = "1.7" # https://github.com/MilkBowl/VaultAPI
minecraft-astralibs = "3.3.1-alpha" # https://github.com/Astra-Interactive/AstraLibs
minecraft-astralibs = "3.3.2-alpha" # https://github.com/Astra-Interactive/AstraLibs
minecraft-bstats = "3.0.0" # https://github.com/Bastian/bStats
minecraft-mockbukkit = "v1.19-SNAPSHOT" #https://github.com/MockBukkit/MockBukkit

Expand All @@ -31,21 +32,26 @@ gradle-shadow = "7.1.2" # https://github.com/johnrengelman/shadow
# BuildConfig
gradle-buildconfig = "3.1.0" # https://github.com/gmazzo/gradle-buildconfig-plugin


# Exposed
exposed = "0.44.0" # https://github.com/JetBrains/Exposed

# DisordSrv
discordsrv = "1.27.0" # https://github.com/DiscordSRV/DiscordSRV

# Mockito
mockito = "5.11.0" # https://github.com/mockito/mockito

[libraries]
# Kotlin
kotlin-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlin-coroutines" }
kotlin-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlin-coroutines" }
kotlin-coroutines-coreJvm = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm", version.ref = "kotlin-coroutines" }
kotlin-serialization = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kotlin-version" }
kotlin-serialization-protobuf = { module = "org.jetbrains.kotlinx:kotlinx-serialization-protobuf", version.ref = "kotlin-json" }
kotlin-serializationJson = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlin-json" }
kotlin-serializationKaml = { module = "com.charleskorn.kaml:kaml", version.ref = "kotlin-kaml" }
kotlin-gradle = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin-version" }
kotlin-benchmark-runtime = { module = "org.jetbrains.kotlinx:kotlinx-benchmark-runtime", version.ref = "kotlin-benchmark" }

# Shadow
gradle-shadow = { module = "gradle.plugin.com.github.johnrengelman:shadow", version.ref = "gradle-shadow" }
Expand All @@ -56,6 +62,7 @@ driver-mysql = { module = "mysql:mysql-connector-java", version.ref = "driver-my

# Testing
tests-kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test" }
mockito-core = { module = "org.mockito:mockito-core", version.ref = "mockito" }

# Exposed
exposed-java-time = { module = "org.jetbrains.exposed:exposed-java-time", version.ref = "exposed" }
Expand Down Expand Up @@ -103,6 +110,8 @@ kotlin = ["kotlin-coroutines-core", "kotlin-coroutines-coreJvm", "kotlin-seriali
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin-version" }
kotlin-gradle = { id = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin-version" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin-version" }
kotlin-benchmark = { id = "org.jetbrains.kotlinx.benchmark", version.ref = "kotlin-benchmark" }
kotlin-allopen = { id = "org.jetbrains.kotlin.plugin.allopen", version.ref = "kotlin-version" }

# Gradle
gradle-shadow = { id = "com.github.johnrengelman.shadow", version.ref = "gradle-shadow" }
Expand Down
7 changes: 7 additions & 0 deletions modules/antiswear/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ dependencies {
implementation(libs.minecraft.astralibs.core.bukkit)
implementation(libs.minecraft.astralibs.command)
implementation(libs.minecraft.astralibs.command.bukkit)
// Test
testImplementation(libs.bundles.testing.kotlin)
testImplementation(libs.tests.kotlin.test)
testImplementation(libs.minecraft.mockbukkit)
testImplementation(libs.mockito.core)
testImplementation(libs.minecraft.paper.api)
testImplementation(libs.kotlin.coroutines.test)
// Local
implementation(projects.modules.core)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.astrainteractive.aspekt.module.antiswear.data

import kotlinx.serialization.StringFormat
import org.bukkit.entity.Player
import ru.astrainteractive.aspekt.module.antiswear.model.AntiSwearStorage
import ru.astrainteractive.astralibs.krate.core.FileKrate
import ru.astrainteractive.astralibs.krate.core.StringFormatKrate
import java.io.File

internal class AntiSwearKrate(
player: Player,
stringFormat: StringFormat,
) : FileKrate<AntiSwearStorage> by StringFormatKrate(
default = AntiSwearStorage(
playerName = player.name,
uuid = player.uniqueId.toString()
),
fileName = "${player.uniqueId}.json",
stringFormat = stringFormat,
kSerializer = AntiSwearStorage.serializer(),
folder = File("./.temp/antiswear")
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
package ru.astrainteractive.aspekt.module.antiswear.data

import kotlinx.coroutines.withContext
import kotlinx.serialization.StringFormat
import org.bukkit.entity.Player
import ru.astrainteractive.aspekt.module.antiswear.model.AntiSwearStorage
import ru.astrainteractive.astralibs.krate.Krate
import ru.astrainteractive.astralibs.krate.KrateExt.create
import ru.astrainteractive.klibs.kstorage.api.MutableStorageValue
import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers
import java.util.UUID

internal class SwearRepositoryImpl(
private val dispatchers: KotlinDispatchers,
private val tempFileStringFormat: StringFormat,
) : SwearRepository {
private val swearFilterMap = mutableMapOf<UUID, Boolean>()

private suspend fun getAntiSwearStorageValue(
private suspend fun getAntiSwearKrate(
player: Player
): Krate<AntiSwearStorage> = withContext(dispatchers.IO) {
AntiSwearKrateFactory.create(
key = player.uniqueId.toString(),
default = {
AntiSwearStorage(
playerName = player.name,
uuid = player.uniqueId.toString(),
)
}
)
): MutableStorageValue<AntiSwearStorage> = withContext(dispatchers.IO) {
AntiSwearKrate(player = player, stringFormat = tempFileStringFormat)
}

override suspend fun rememberPlayer(player: Player) = withContext(dispatchers.IO) {
swearFilterMap[player.uniqueId] = getAntiSwearStorageValue(player).value.isSwearFilterEnabled
swearFilterMap[player.uniqueId] = getAntiSwearKrate(player).value.isSwearFilterEnabled
}

override suspend fun forgetPlayer(player: Player): Unit = withContext(dispatchers.IO) {
Expand All @@ -41,7 +34,7 @@ internal class SwearRepositoryImpl(

override suspend fun setSwearFilterEnabled(player: Player, isEnabled: Boolean) = withContext(dispatchers.IO) {
swearFilterMap[player.uniqueId] = isEnabled
getAntiSwearStorageValue(player).update { value ->
getAntiSwearKrate(player).update { value ->
value.copy(isSwearFilterEnabled = isEnabled)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ interface AntiSwearModule {

class Default(coreModule: CoreModule) : AntiSwearModule {
private val swearRepository = SwearRepositoryImpl(
dispatchers = coreModule.dispatchers.value
dispatchers = coreModule.dispatchers.value,
tempFileStringFormat = coreModule.tempFileStringFormat
)
private val antiSwearEventListener = AntiSwearEventListener(
swearRepository = swearRepository,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ru.astrainteractive.aspekt.module.antiswear.data

import kotlinx.coroutines.test.runTest
import kotlinx.serialization.json.Json
import org.bukkit.entity.Player
import org.mockito.Mockito
import ru.astrainteractive.klibs.mikro.core.dispatchers.DefaultKotlinDispatchers
import java.util.UUID
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class SwearRepositoryTest {
@Test
fun test(): Unit = runTest {
val repository = SwearRepositoryImpl(
dispatchers = DefaultKotlinDispatchers,
tempFileStringFormat = Json
)
val player = Mockito.mock(Player::class.java)
val uuid = UUID.randomUUID()
Mockito.`when`(player.name).then { "Name" }
Mockito.`when`(player.uniqueId).then { uuid }
assertTrue(repository.isSwearFilterEnabled(player))
repository.setSwearFilterEnabled(player, false)
assertFalse(repository.isSwearFilterEnabled(player))
repository.setSwearFilterEnabled(player, true)
assertTrue(repository.isSwearFilterEnabled(player))
repository.forgetPlayer(player)
repository.rememberPlayer(player)
assertTrue(repository.isSwearFilterEnabled(player))
repository.setSwearFilterEnabled(player, false)
assertFalse(repository.isSwearFilterEnabled(player))
repository.forgetPlayer(player)
repository.rememberPlayer(player)
assertFalse(repository.isSwearFilterEnabled(player))
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ru.astrainteractive.aspekt.di

import kotlinx.serialization.StringFormat
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.bukkit.plugin.java.JavaPlugin
import ru.astrainteractive.aspekt.plugin.PluginConfiguration
import ru.astrainteractive.aspekt.plugin.PluginTranslation
Expand Down Expand Up @@ -44,6 +46,8 @@ interface CoreModule : Lifecycle {
val kyoriComponentSerializer: Reloadable<KyoriComponentSerializer>
val inventoryClickEventListener: Single<DefaultInventoryClickEvent>

val tempFileStringFormat: StringFormat

class Default : CoreModule {

// Core
Expand Down Expand Up @@ -109,6 +113,14 @@ interface CoreModule : Lifecycle {
DefaultInventoryClickEvent()
}

override val tempFileStringFormat: StringFormat by lazy {
Json {
isLenient = true
ignoreUnknownKeys = true
prettyPrint = false
}
}

override fun onDisable() {
inventoryClickEventListener.value.onDisable()
eventListener.value.onDisable()
Expand Down

0 comments on commit 1d0fef9

Please sign in to comment.