From fede7048ad01b09783a5ec2349449635eec53010 Mon Sep 17 00:00:00 2001 From: mh-northlander Date: Tue, 21 May 2024 15:39:42 +0900 Subject: [PATCH] test xcontent serialization --- build.gradle | 2 + .../attributes/MorphemeAttributeImplTest.kt | 47 ++++++++++++++++--- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/build.gradle b/build.gradle index 0250380..484df01 100644 --- a/build.gradle +++ b/build.gradle @@ -3,6 +3,7 @@ import org.jetbrains.kotlin.gradle.dsl.JvmTarget plugins { id 'java-library' id 'org.jetbrains.kotlin.jvm' version '1.8.0' + id "org.jetbrains.kotlin.plugin.serialization" version "1.8.0" id 'com.diffplug.spotless' version '6.16.0' id 'org.sonarqube' version '4.0.0.2929' id("org.jetbrains.kotlinx.kover") version "0.7.0" @@ -44,6 +45,7 @@ dependencies { testImplementation('org.jetbrains.kotlin:kotlin-test-junit') { exclude(group: 'org.hamcrest') } + testImplementation('org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3') kover(project(':integration')) kover(project(':testlib')) } diff --git a/src/test/java/com/worksap/nlp/lucene/sudachi/ja/attributes/MorphemeAttributeImplTest.kt b/src/test/java/com/worksap/nlp/lucene/sudachi/ja/attributes/MorphemeAttributeImplTest.kt index ab14082..1247f9c 100644 --- a/src/test/java/com/worksap/nlp/lucene/sudachi/ja/attributes/MorphemeAttributeImplTest.kt +++ b/src/test/java/com/worksap/nlp/lucene/sudachi/ja/attributes/MorphemeAttributeImplTest.kt @@ -17,14 +17,19 @@ package com.worksap.nlp.lucene.sudachi.ja.attributes import com.worksap.nlp.lucene.aliases.ToXContent +import com.worksap.nlp.lucene.aliases.XContentBuilder +import com.worksap.nlp.search.aliases.XContentType import com.worksap.nlp.sudachi.Config import com.worksap.nlp.sudachi.DictionaryFactory import com.worksap.nlp.sudachi.Morpheme import com.worksap.nlp.test.TestDictionary import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertNotNull import kotlin.test.assertNull import kotlin.test.assertTrue +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.Json import org.junit.Before import org.junit.Rule @@ -52,22 +57,52 @@ class MorphemeAttributeImplTest { var morphemeAtt = MorphemeAttributeImpl() assertNull(morphemeAtt.getMorpheme()) - val morph = getFirstMorpheme("東京都")!! - morphemeAtt.setMorpheme(morph) - assertEquals(morph, morphemeAtt.getMorpheme()) + val morpheme = getFirstMorpheme("東京都")!! + morphemeAtt.setMorpheme(morpheme) + assertEquals(morpheme, morphemeAtt.getMorpheme()) + + morphemeAtt.setMorpheme(null) + assertNull(morphemeAtt.getMorpheme()) } @Test - fun reflectMorpheme() { + fun toXContent() { var morphemeAtt = MorphemeAttributeImpl() - val morph = getFirstMorpheme("東京都")!! - morphemeAtt.setMorpheme(morph) + val morpheme = getFirstMorpheme("東京都")!! + morphemeAtt.setMorpheme(morpheme) + val builder = XContentBuilder.builder(XContentType.JSON.xContent()) + builder.startObject() morphemeAtt.reflectWith( fun(attClass, key, value) { assertEquals(MorphemeAttribute::class.java, attClass) assertEquals("morpheme", key) assertTrue(value is ToXContent) + + builder.field(key, value) }) + builder.endObject() + builder.flush() + + val serialized = builder.getOutputStream().toString() + val deserialized = Json.decodeFromString(serialized) + + assertNotNull(deserialized.morpheme) + assertEquals(morpheme.surface(), deserialized.morpheme.surface) + assertEquals(morpheme.dictionaryForm(), deserialized.morpheme.dictionaryForm) + assertEquals(morpheme.normalizedForm(), deserialized.morpheme.normalizedForm) + assertEquals(morpheme.readingForm(), deserialized.morpheme.readingForm) + assertEquals(morpheme.partOfSpeech(), deserialized.morpheme.partOfSpeech) } } + +@Serializable data class MorphemeHolder(val morpheme: MorphemeAttributeHolder) + +@Serializable +data class MorphemeAttributeHolder( + val surface: String, + val dictionaryForm: String, + val normalizedForm: String, + val readingForm: String, + val partOfSpeech: List, +)