From cfd7bce2f3fdb662794cf08b3eb189acc8ae2e52 Mon Sep 17 00:00:00 2001 From: Atos1337 Date: Sat, 4 Dec 2021 00:35:12 +0300 Subject: [PATCH] Add some tests for encoding/decoding --- .../npm/xodus/serialization/json/encoder.kt | 2 + .../mipt/npm/xodus/serialization/json/main.kt | 3 +- .../serialization/json/EncoderDecoderTests.kt | 105 ++++++++++++++++++ 3 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 xodus-serialization/src/test/kotlin/ru/mipt/npm/xodus/serialization/json/EncoderDecoderTests.kt diff --git a/xodus-serialization/src/main/kotlin/ru/mipt/npm/xodus/serialization/json/encoder.kt b/xodus-serialization/src/main/kotlin/ru/mipt/npm/xodus/serialization/json/encoder.kt index e2dbaa3..3cd5b79 100644 --- a/xodus-serialization/src/main/kotlin/ru/mipt/npm/xodus/serialization/json/encoder.kt +++ b/xodus-serialization/src/main/kotlin/ru/mipt/npm/xodus/serialization/json/encoder.kt @@ -16,6 +16,7 @@ internal fun StoreTransaction.encodeToEntity(jsonElement: JsonElement, entity: E jsonElement.forEach { entry -> entry.value.let { value -> when(value) { + // не сможем десериализовать, если JsonNull (надо ли обрабатывать???) (можно сохранить в отдельный список ключи null-ов) is JsonPrimitive -> { if (value.isString) { entity.setProperty(entry.key, value.content) @@ -30,6 +31,7 @@ internal fun StoreTransaction.encodeToEntity(jsonElement: JsonElement, entity: E } // считаем, что все элементы массива - JsonObject, иначе не можем напрямую сериализовать (надо придывать костыли???) + // не сможем десериализовать, если массив пустой (надо ли обрабатывать???) (можно сохранять в отдельный список ключи пустых массивов) is JsonArray -> { value.forEach { element -> val childEntity = newEntity("${entity.type}.${entry.key}") diff --git a/xodus-serialization/src/main/kotlin/ru/mipt/npm/xodus/serialization/json/main.kt b/xodus-serialization/src/main/kotlin/ru/mipt/npm/xodus/serialization/json/main.kt index bec4b2d..60d9bb8 100644 --- a/xodus-serialization/src/main/kotlin/ru/mipt/npm/xodus/serialization/json/main.kt +++ b/xodus-serialization/src/main/kotlin/ru/mipt/npm/xodus/serialization/json/main.kt @@ -24,8 +24,7 @@ internal fun main() { Name.parse("magix-virtual-car"), time = Instant.fromEpochMilliseconds(1337) ), - "magix-virtual-car", - user = JsonObject(content = mapOf(Pair("name", JsonPrimitive("SCADA")))) + "magix-virtual-car" ) val entityStore = PersistentEntityStores.newInstance(Paths.get(".xodus_serialization").toString()) diff --git a/xodus-serialization/src/test/kotlin/ru/mipt/npm/xodus/serialization/json/EncoderDecoderTests.kt b/xodus-serialization/src/test/kotlin/ru/mipt/npm/xodus/serialization/json/EncoderDecoderTests.kt new file mode 100644 index 0000000..ad35069 --- /dev/null +++ b/xodus-serialization/src/test/kotlin/ru/mipt/npm/xodus/serialization/json/EncoderDecoderTests.kt @@ -0,0 +1,105 @@ +package ru.mipt.npm.xodus.serialization.json + +import jetbrains.exodus.entitystore.PersistentEntityStores +import kotlinx.datetime.Instant +import kotlinx.serialization.json.* +import org.junit.jupiter.api.AfterAll +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import ru.mipt.npm.controls.api.PropertyChangedMessage +import ru.mipt.npm.magix.api.MagixMessage +import space.kscience.dataforge.meta.Meta +import space.kscience.dataforge.names.Name +import java.nio.file.Paths +import kotlin.test.assertEquals + +internal class EncoderDecoderTests { + companion object { + private val storePath = Paths.get(".xodus_serialization_test") + private val entityStore = PersistentEntityStores.newInstance(storePath.toString()) + + @AfterAll + @JvmStatic + fun deleteDatabase() { + entityStore.close() + storePath.toFile().deleteRecursively() + } + + @AfterEach + fun clearDatabase() { + entityStore.clear() + } + + fun checkEncodingDecodingCorrectness(json: JsonObject) { + val id = entityStore.encodeToEntity(json, "JsonObject") + assertEquals(json, entityStore.decodeFromEntity(id)) + } + + fun checkEncodingDecodingCorrectness(jsons: List) = jsons.forEach { + checkEncodingDecodingCorrectness(it) + } + + } + + @Test + fun `encoder throw Illegal exception if input is not a JsonObject`() { + assertThrows { + val json = JsonPrimitive(0) + entityStore.encodeToEntity(json, "JsonPrimitive") + } + + assertThrows { + val json = buildJsonArray {} + entityStore.encodeToEntity(json, "JsonArray") + } + } + + @Test + fun `correctly work with underlying JsonPrimitive`() { + val jsonLong = buildJsonObject { put("value", 0) } + val jsonDouble = buildJsonObject { put("value", 0.0) } + val jsonBoolean = buildJsonObject { put("value", true) } + val jsonString = buildJsonObject { put("value", "") } + + checkEncodingDecodingCorrectness(listOf(jsonLong, jsonDouble, jsonBoolean, jsonString)) + } + + @Test + fun `correctly work with underlying JsonArray`() { + checkEncodingDecodingCorrectness(buildJsonObject { putJsonArray("value") { + add(buildJsonObject { put("value", 0) }) + add(buildJsonObject { put("value", 0.0) }) + } }) + } + + @Test + fun `correctly work with underlying JsonObject`() { + checkEncodingDecodingCorrectness(buildJsonObject { + putJsonObject("value", { put("value", true) }) + }) + } + + @Test + fun testMagixMessagePropertyChangedMessage() { + val expectedMessage = MagixMessage( + "dataforge", + "dataforge", + PropertyChangedMessage( + "acceleration", + Meta { + "x" put 3.0 + "y" put 9.0 + }, + Name.parse("virtual-car"), + Name.parse("magix-virtual-car"), + time = Instant.fromEpochMilliseconds(1337) + ), + "magix-virtual-car", + user = buildJsonObject { put("name", "SCADA") } + ) + + val id = entityStore.encodeToEntity(expectedMessage, "MagixMessage") + assertEquals(expectedMessage, entityStore.decodeFromEntity>(id)) + } +} \ No newline at end of file