2023-01-12 11:52:54 +03:00
|
|
|
package center.sciprog.attributes
|
|
|
|
|
2023-02-11 19:09:27 +03:00
|
|
|
import kotlinx.serialization.*
|
2023-01-12 11:52:54 +03:00
|
|
|
import kotlinx.serialization.json.Json
|
2023-02-11 19:09:27 +03:00
|
|
|
import kotlinx.serialization.modules.SerializersModule
|
|
|
|
import kotlinx.serialization.modules.contextual
|
|
|
|
import kotlinx.serialization.protobuf.ProtoBuf
|
|
|
|
import kotlin.test.Ignore
|
2023-01-12 11:52:54 +03:00
|
|
|
import kotlin.test.Test
|
|
|
|
import kotlin.test.assertEquals
|
|
|
|
|
|
|
|
internal class AttributesSerializationTest {
|
|
|
|
|
2023-02-11 19:09:27 +03:00
|
|
|
@Serializable
|
|
|
|
internal class Container(@Contextual val attributes: Attributes) {
|
|
|
|
override fun equals(other: Any?): Boolean = (other as? Container)?.attributes?.equals(attributes) ?: false
|
|
|
|
override fun hashCode(): Int = attributes.hashCode()
|
2023-02-11 17:14:00 +03:00
|
|
|
|
2023-02-11 19:09:27 +03:00
|
|
|
override fun toString(): String = attributes.toString()
|
|
|
|
}
|
2023-02-11 17:14:00 +03:00
|
|
|
|
2023-02-11 19:09:27 +03:00
|
|
|
internal object ContainerAttribute : SerializableAttribute<Container>("container", serializer()) {
|
|
|
|
override fun toString(): String = "container"
|
2023-01-12 11:52:54 +03:00
|
|
|
|
2023-02-11 19:09:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
internal object TestAttribute : SerializableAttribute<Map<String, String>>("test", serializer()) {
|
|
|
|
override fun toString(): String = "test"
|
|
|
|
}
|
2023-01-12 11:52:54 +03:00
|
|
|
|
2023-02-11 19:09:27 +03:00
|
|
|
@Test
|
|
|
|
fun restoreFromJson() {
|
|
|
|
val json = Json {
|
|
|
|
serializersModule = SerializersModule {
|
|
|
|
contextual(AttributesSerializer(setOf(NameAttribute, TestAttribute, ContainerAttribute)))
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 11:52:54 +03:00
|
|
|
|
|
|
|
val attributes = Attributes {
|
|
|
|
NameAttribute("myTest")
|
|
|
|
TestAttribute(mapOf("a" to "aa", "b" to "bb"))
|
2023-02-11 19:09:27 +03:00
|
|
|
ContainerAttribute(
|
|
|
|
Container(
|
|
|
|
Attributes {
|
|
|
|
TestAttribute(mapOf("a" to "aa", "b" to "bb"))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2023-01-12 11:52:54 +03:00
|
|
|
}
|
|
|
|
|
2023-02-11 19:09:27 +03:00
|
|
|
val serialized: String = json.encodeToString(attributes)
|
2023-01-12 11:52:54 +03:00
|
|
|
println(serialized)
|
|
|
|
|
2023-02-11 19:09:27 +03:00
|
|
|
val restored: Attributes = json.decodeFromString(serialized)
|
|
|
|
|
|
|
|
assertEquals(attributes, restored)
|
|
|
|
}
|
|
|
|
|
|
|
|
@OptIn(ExperimentalSerializationApi::class)
|
|
|
|
@Test
|
|
|
|
@Ignore
|
|
|
|
fun restoreFromProtoBuf() {
|
|
|
|
val protoBuf = ProtoBuf {
|
|
|
|
serializersModule = SerializersModule {
|
|
|
|
contextual(AttributesSerializer(setOf(NameAttribute, TestAttribute, ContainerAttribute)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val attributes = Attributes {
|
|
|
|
NameAttribute("myTest")
|
|
|
|
TestAttribute(mapOf("a" to "aa", "b" to "bb"))
|
|
|
|
ContainerAttribute(
|
|
|
|
Container(
|
|
|
|
Attributes {
|
|
|
|
TestAttribute(mapOf("a" to "aa", "b" to "bb"))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
val serialized = protoBuf.encodeToByteArray(attributes)
|
|
|
|
|
|
|
|
val restored: Attributes = protoBuf.decodeFromByteArray(serialized)
|
2023-01-12 11:52:54 +03:00
|
|
|
|
|
|
|
assertEquals(attributes, restored)
|
|
|
|
}
|
|
|
|
}
|