From ef352d43a6d6c0b55c57ac1dda75612bb43a1f9a Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 3 Nov 2023 14:46:07 +0900 Subject: [PATCH] =?UTF-8?q?test:=20Object=E3=81=AE=E3=82=B7=E3=83=AA?= =?UTF-8?q?=E3=82=A2=E3=83=A9=E3=82=A4=E3=82=BA=E3=83=86=E3=82=B9=E3=83=88?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/model/objects/Object.kt | 14 +++- .../model/objects/ObjectSerializeTest.kt | 65 +++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/objects/ObjectSerializeTest.kt diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/objects/Object.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/objects/Object.kt index 703401ad..f3befd0f 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/objects/Object.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/objects/Object.kt @@ -9,18 +9,22 @@ import dev.usbharu.hideout.activitypub.domain.model.JsonLd open class Object : JsonLd { @JsonSerialize(using = TypeSerializer::class) var type: List = 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, 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 diff --git a/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/objects/ObjectSerializeTest.kt b/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/objects/ObjectSerializeTest.kt new file mode 100644 index 00000000..77f2579b --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/activitypub/domain/model/objects/ObjectSerializeTest.kt @@ -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(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(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(json) + + val expected = Object( + emptyList(), + null, + null, + null + ) + + assertEquals(expected, readValue) + } + +}