test: Objectのシリアライズテストを追加

This commit is contained in:
usbharu 2023-11-03 14:46:07 +09:00
parent 00d24b5030
commit ef352d43a6
2 changed files with 76 additions and 3 deletions

View File

@ -9,18 +9,22 @@ import dev.usbharu.hideout.activitypub.domain.model.JsonLd
open class Object : JsonLd {
@JsonSerialize(using = TypeSerializer::class)
var type: List<String> = emptyList()
set(value) {
field = value.filter { it.isNotBlank() }
}
var name: String? = null
var actor: String? = null
var id: String? = null
protected constructor()
constructor(type: List<String>, name: String? = null, actor: String? = null, id: String? = null) : super() {
this.type = type
this.type = type.filter { it.isNotBlank() }
this.name = name
this.actor = actor
this.id = id
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Object) return false
@ -29,7 +33,9 @@ open class Object : JsonLd {
if (type != other.type) return false
if (name != other.name) return false
if (actor != other.actor) return false
return id == other.id
if (id != other.id) return false
return true
}
override fun hashCode(): Int {
@ -41,7 +47,9 @@ open class Object : JsonLd {
return result
}
override fun toString(): String = "Object(type=$type, name=$name, actor=$actor, id=$id) ${super.toString()}"
override fun toString(): String {
return "Object(type=$type, name=$name, actor=$actor, id=$id) ${super.toString()}"
}
companion object {
@JvmStatic

View File

@ -0,0 +1,65 @@
package dev.usbharu.hideout.activitypub.domain.model.objects
import com.fasterxml.jackson.module.kotlin.readValue
import dev.usbharu.hideout.application.config.ActivityPubConfig
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
class ObjectSerializeTest {
@Test
fun typeが文字列のときデシリアライズできる() {
//language=JSON
val json = """{"type": "Object"}"""
val objectMapper = ActivityPubConfig().objectMapper()
val readValue = objectMapper.readValue<Object>(json)
val expected = Object(
listOf("Object"),
null,
null,
null
)
assertEquals(expected, readValue)
}
@Test
fun typeが文字列の配列のときデシリアライズできる() {
//language=JSON
val json = """{"type": ["Hoge","Object"]}"""
val objectMapper = ActivityPubConfig().objectMapper()
val readValue = objectMapper.readValue<Object>(json)
val expected = Object(
listOf("Hoge", "Object"),
null,
null,
null
)
assertEquals(expected, readValue)
}
@Test
fun typeが空のとき無視してデシリアライズする() {
//language=JSON
val json = """{"type": ""}"""
val objectMapper = ActivityPubConfig().objectMapper()
val readValue = objectMapper.readValue<Object>(json)
val expected = Object(
emptyList(),
null,
null,
null
)
assertEquals(expected, readValue)
}
}