mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-04-29 22:27:31 +00:00
refactor(config): 调整序列化工具类, 便于进行测试.
修正序列化工具类的类名, 并调整访问权为 internal. 同时将 `checkJsonKey` 改成更方便的 `JsonObject.getPrimitiveValueOrThrow`.
This commit is contained in:
parent
9b7fc30512
commit
9ed55204c0
@ -3,6 +3,7 @@ package net.lamgc.scalabot.config.serializer
|
|||||||
import com.google.gson.*
|
import com.google.gson.*
|
||||||
import com.google.gson.reflect.TypeToken
|
import com.google.gson.reflect.TypeToken
|
||||||
import net.lamgc.scalabot.config.*
|
import net.lamgc.scalabot.config.*
|
||||||
|
import net.lamgc.scalabot.config.serializer.SerializeUtils.getPrimitiveValueOrThrow
|
||||||
import org.eclipse.aether.artifact.AbstractArtifact
|
import org.eclipse.aether.artifact.AbstractArtifact
|
||||||
import org.eclipse.aether.artifact.Artifact
|
import org.eclipse.aether.artifact.Artifact
|
||||||
import org.eclipse.aether.artifact.DefaultArtifact
|
import org.eclipse.aether.artifact.DefaultArtifact
|
||||||
@ -81,8 +82,8 @@ object AuthenticationSerializer : JsonDeserializer<Authentication> {
|
|||||||
if (json !is JsonObject) {
|
if (json !is JsonObject) {
|
||||||
throw JsonParseException("Unsupported JSON type.")
|
throw JsonParseException("Unsupported JSON type.")
|
||||||
}
|
}
|
||||||
val username = SerializerUtils.checkJsonKey(json, "username")
|
val username = json.getPrimitiveValueOrThrow("username").asString
|
||||||
val password = SerializerUtils.checkJsonKey(json, "password")
|
val password = json.getPrimitiveValueOrThrow("password").asString
|
||||||
val builder = AuthenticationBuilder()
|
val builder = AuthenticationBuilder()
|
||||||
builder.addUsername(username)
|
builder.addUsername(username)
|
||||||
builder.addPassword(password)
|
builder.addPassword(password)
|
||||||
@ -91,14 +92,14 @@ object AuthenticationSerializer : JsonDeserializer<Authentication> {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private object SerializerUtils {
|
internal object SerializeUtils {
|
||||||
fun checkJsonKey(json: JsonObject, key: String): String {
|
|
||||||
if (!json.has(key)) {
|
fun JsonObject.getPrimitiveValueOrThrow(fieldName: String): JsonPrimitive {
|
||||||
throw JsonParseException("Required field does not exist: $key")
|
val value = get(fieldName) ?: throw JsonParseException("Missing `$fieldName` field.")
|
||||||
} else if (!json.get(key).isJsonPrimitive) {
|
if (value !is JsonPrimitive) {
|
||||||
throw JsonParseException("Wrong field `$key` type: ${json.get(key)::class.java}")
|
throw JsonParseException("Invalid `account` field type.")
|
||||||
}
|
}
|
||||||
return json.get(key).asString
|
return value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,7 +115,7 @@ object MavenRepositoryConfigSerializer
|
|||||||
is JsonObject -> {
|
is JsonObject -> {
|
||||||
MavenRepositoryConfig(
|
MavenRepositoryConfig(
|
||||||
id = json.get("id")?.asString,
|
id = json.get("id")?.asString,
|
||||||
url = URL(SerializerUtils.checkJsonKey(json, "url")),
|
url = URL(json.getPrimitiveValueOrThrow("url").asString),
|
||||||
proxy = if (json.has("proxy"))
|
proxy = if (json.has("proxy"))
|
||||||
context.deserialize<Proxy>(
|
context.deserialize<Proxy>(
|
||||||
json.get("proxy"), Proxy::class.java
|
json.get("proxy"), Proxy::class.java
|
||||||
|
@ -5,6 +5,7 @@ import io.mockk.every
|
|||||||
import io.mockk.mockk
|
import io.mockk.mockk
|
||||||
import io.mockk.verify
|
import io.mockk.verify
|
||||||
import net.lamgc.scalabot.config.*
|
import net.lamgc.scalabot.config.*
|
||||||
|
import net.lamgc.scalabot.config.serializer.SerializeUtils.getPrimitiveValueOrThrow
|
||||||
import org.eclipse.aether.artifact.Artifact
|
import org.eclipse.aether.artifact.Artifact
|
||||||
import org.eclipse.aether.artifact.DefaultArtifact
|
import org.eclipse.aether.artifact.DefaultArtifact
|
||||||
import org.eclipse.aether.repository.Authentication
|
import org.eclipse.aether.repository.Authentication
|
||||||
@ -12,54 +13,39 @@ import org.eclipse.aether.repository.AuthenticationContext
|
|||||||
import org.eclipse.aether.repository.Proxy
|
import org.eclipse.aether.repository.Proxy
|
||||||
import org.intellij.lang.annotations.Language
|
import org.intellij.lang.annotations.Language
|
||||||
import org.junit.jupiter.api.Assertions.assertThrows
|
import org.junit.jupiter.api.Assertions.assertThrows
|
||||||
import java.lang.reflect.InvocationTargetException
|
|
||||||
import java.lang.reflect.Method
|
|
||||||
import java.lang.reflect.Type
|
import java.lang.reflect.Type
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import kotlin.test.*
|
import kotlin.test.*
|
||||||
|
|
||||||
internal class SerializersKtTest {
|
internal class SerializeUtilsTest {
|
||||||
|
|
||||||
private val instance: Any
|
|
||||||
private val method: Method
|
|
||||||
|
|
||||||
init {
|
|
||||||
val clazz = Class.forName("net.lamgc.scalabot.config.serializer.SerializerUtils")
|
|
||||||
method = clazz.getDeclaredMethod("checkJsonKey", JsonObject::class.java, String::class.java)
|
|
||||||
method.isAccessible = true
|
|
||||||
instance = clazz.getDeclaredField("INSTANCE").apply {
|
|
||||||
isAccessible = true
|
|
||||||
}.get(null)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun invoke(json: JsonObject, key: String): String {
|
|
||||||
try {
|
|
||||||
return method.invoke(instance, json, key) as String
|
|
||||||
} catch (e: InvocationTargetException) {
|
|
||||||
throw e.targetException
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Json key checker test`() {
|
fun `getPrimitiveValueOrThrow test`() {
|
||||||
assertThrows(JsonParseException::class.java) {
|
assertThrows(JsonParseException::class.java) {
|
||||||
invoke(JsonObject(), "NOT_EXIST_KEY")
|
JsonObject().getPrimitiveValueOrThrow("NOT_EXIST_KEY")
|
||||||
}
|
|
||||||
assertThrows(JsonParseException::class.java) {
|
|
||||||
invoke(JsonObject().apply { add("NULL_KEY", JsonNull.INSTANCE) }, "NULL_KEY")
|
|
||||||
}
|
|
||||||
assertThrows(JsonParseException::class.java) {
|
|
||||||
invoke(JsonObject().apply { add("ARRAY_KEY", JsonArray()) }, "ARRAY_KEY")
|
|
||||||
}
|
|
||||||
assertThrows(JsonParseException::class.java) {
|
|
||||||
invoke(JsonObject().apply { add("OBJECT_KEY", JsonObject()) }, "OBJECT_KEY")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val expectKey = "TEST"
|
assertThrows(JsonParseException::class.java) {
|
||||||
val expectString = "testString"
|
JsonObject().apply {
|
||||||
val json = JsonObject().apply { addProperty(expectKey, expectString) }
|
add("testKey", JsonArray())
|
||||||
|
}.getPrimitiveValueOrThrow("testKey")
|
||||||
|
}
|
||||||
|
assertThrows(JsonParseException::class.java) {
|
||||||
|
JsonObject().apply {
|
||||||
|
add("testKey", JsonObject())
|
||||||
|
}.getPrimitiveValueOrThrow("testKey")
|
||||||
|
}
|
||||||
|
assertThrows(JsonParseException::class.java) {
|
||||||
|
JsonObject().apply {
|
||||||
|
add("testKey", JsonNull.INSTANCE)
|
||||||
|
}.getPrimitiveValueOrThrow("testKey")
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals(expectString, invoke(json, expectKey))
|
val expectKey = "STRING_KEY"
|
||||||
|
val expectValue = JsonPrimitive("A STRING")
|
||||||
|
assertEquals(expectValue, JsonObject()
|
||||||
|
.apply { add(expectKey, expectValue) }
|
||||||
|
.getPrimitiveValueOrThrow(expectKey))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user