refactor: 调整 checkJsonKey 的所在类, 以便于编写测试用例.

通过调整所在类, 可更好的在单元测试中获取方法对象, 进行测试调用.
This commit is contained in:
LamGC 2022-05-19 18:20:46 +08:00
parent 31366575a9
commit 24f34aa27f
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D

View File

@ -55,8 +55,8 @@ internal object ArtifactSerializer : JsonSerializer<Artifact>, JsonDeserializer<
return JsonPrimitive(gavBuilder.append(':').append(src.version).toString()) return JsonPrimitive(gavBuilder.append(':').append(src.version).toString())
} }
override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): Artifact { override fun deserialize(json: JsonElement, typeOfT: Type?, context: JsonDeserializationContext?): Artifact {
if (!json!!.isJsonPrimitive) { if (!json.isJsonPrimitive) {
throw JsonParseException("Wrong configuration value type.") throw JsonParseException("Wrong configuration value type.")
} }
return DefaultArtifact(json.asString.trim()) return DefaultArtifact(json.asString.trim())
@ -115,17 +115,24 @@ internal object AuthenticationSerializer : JsonDeserializer<Authentication> {
when (json.get(KEY_TYPE).asString.trim().lowercase()) { when (json.get(KEY_TYPE).asString.trim().lowercase()) {
"string" -> { "string" -> {
builder.addString(checkJsonKey(json, "key"), checkJsonKey(json, "value")) builder.addString(
SerializerUtils.checkJsonKey(json, "key"),
SerializerUtils.checkJsonKey(json, "value")
)
} }
"secret" -> { "secret" -> {
builder.addSecret(checkJsonKey(json, "key"), checkJsonKey(json, "value")) builder.addSecret(
SerializerUtils.checkJsonKey(json, "key"),
SerializerUtils.checkJsonKey(json, "value")
)
} }
} }
} }
} }
private fun checkJsonKey(json: JsonObject, key: String): String { private object SerializerUtils {
fun checkJsonKey(json: JsonObject, key: String): String {
if (!json.has(key)) { if (!json.has(key)) {
throw JsonParseException("Required field does not exist: $key") throw JsonParseException("Required field does not exist: $key")
} else if (!json.get(key).isJsonPrimitive) { } else if (!json.get(key).isJsonPrimitive) {
@ -133,6 +140,7 @@ private fun checkJsonKey(json: JsonObject, key: String): String {
} }
return json.get(key).asString return json.get(key).asString
} }
}
internal object MavenRepositoryConfigSerializer internal object MavenRepositoryConfigSerializer
: JsonDeserializer<MavenRepositoryConfig> { : JsonDeserializer<MavenRepositoryConfig> {
@ -146,7 +154,7 @@ internal object MavenRepositoryConfigSerializer
is JsonObject -> { is JsonObject -> {
MavenRepositoryConfig( MavenRepositoryConfig(
id = json.get("id")?.asString, id = json.get("id")?.asString,
url = URL(checkJsonKey(json, "url")), url = URL(SerializerUtils.checkJsonKey(json, "url")),
proxy = if (json.has("proxy") && json.get("proxy").isJsonObject) proxy = if (json.has("proxy") && json.get("proxy").isJsonObject)
context.deserialize<Proxy>( context.deserialize<Proxy>(
json.getAsJsonObject("proxy"), Proxy::class.java json.getAsJsonObject("proxy"), Proxy::class.java