From 53f9af3ba49700a0d1c9fd95d8449ceb63b7b590 Mon Sep 17 00:00:00 2001 From: Atos1337 Date: Fri, 19 Nov 2021 22:15:50 +0300 Subject: [PATCH] Add converters from Entity and ConvertersTest --- .../ru/mipt/npm/controls/xodus/converters.kt | 38 ++++++++++++ .../mipt/npm/controls/xodus/ConvertersTest.kt | 59 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 controls-xodus/src/test/kotlin/ru/mipt/npm/controls/xodus/ConvertersTest.kt diff --git a/controls-xodus/src/main/kotlin/ru/mipt/npm/controls/xodus/converters.kt b/controls-xodus/src/main/kotlin/ru/mipt/npm/controls/xodus/converters.kt index bb9e070..83b3035 100644 --- a/controls-xodus/src/main/kotlin/ru/mipt/npm/controls/xodus/converters.kt +++ b/controls-xodus/src/main/kotlin/ru/mipt/npm/controls/xodus/converters.kt @@ -2,8 +2,13 @@ package ru.mipt.npm.controls.xodus import jetbrains.exodus.entitystore.Entity import jetbrains.exodus.entitystore.StoreTransaction +import kotlinx.datetime.Instant +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonElement import ru.mipt.npm.controls.api.PropertyChangedMessage import ru.mipt.npm.magix.api.MagixMessage +import space.kscience.dataforge.meta.MetaSerializer +import space.kscience.dataforge.names.Name public fun PropertyChangedMessage.toEntity(transaction: StoreTransaction): Entity { val entity = transaction.newEntity("PropertyChangedMessage") @@ -16,6 +21,21 @@ public fun PropertyChangedMessage.toEntity(transaction: StoreTransaction): Entit return entity } +public fun Entity.toPropertyChangedMessage(): PropertyChangedMessage? { + if (getProperty("property") == null || getProperty("value") == null || getProperty("sourceDevice") == null) { + return null + } + + return PropertyChangedMessage( + getProperty("property") as String, + Json.decodeFromString(MetaSerializer, getProperty("value") as String), + Name.parse(getProperty("sourceDevice") as String), + getProperty("targetDevice")?.let { Name.parse(it as String) }, + getProperty("comment")?.let { it as String }, + getProperty("time")?.let { Instant.fromEpochMilliseconds(it as Long) } + ) +} + public fun MagixMessage.toEntity(transaction: StoreTransaction): Entity { val entity = transaction.newEntity("MagixMessage") entity.setProperty("format", format) @@ -30,3 +50,21 @@ public fun MagixMessage.toEntity(transaction: StoreTransaction): Entity { user?.let { entity.setProperty("user", it.toString()) } return entity } + +public fun Entity.toMagixMessage(): MagixMessage? { + if (getProperty("format") == null || getProperty("origin") == null) { + return null + } + + return getLink("payload")?.toPropertyChangedMessage()?.let { propertyChangedMessage -> + MagixMessage( + getProperty("format") as String, + getProperty("origin") as String, + propertyChangedMessage, + getProperty("target")?.let { it as String }, + getProperty("id")?.let { it as String }, + getProperty("parentId")?.let { it as String }, + getProperty("user")?.let { Json.decodeFromString(JsonElement.serializer(), it as String) } + ) + } +} diff --git a/controls-xodus/src/test/kotlin/ru/mipt/npm/controls/xodus/ConvertersTest.kt b/controls-xodus/src/test/kotlin/ru/mipt/npm/controls/xodus/ConvertersTest.kt new file mode 100644 index 0000000..85e8ba0 --- /dev/null +++ b/controls-xodus/src/test/kotlin/ru/mipt/npm/controls/xodus/ConvertersTest.kt @@ -0,0 +1,59 @@ +package ru.mipt.npm.controls.xodus + +import jetbrains.exodus.entitystore.PersistentEntityStores +import kotlinx.datetime.Instant +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.JsonPrimitive +import org.junit.jupiter.api.AfterAll +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test +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.io.File +import kotlin.test.assertEquals + +internal class ConvertersTest { + companion object { + private val entityStore = PersistentEntityStores.newInstance(".test") + private 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 = JsonObject(content = mapOf(Pair("name", JsonPrimitive("SCADA")))) + ) + + @BeforeAll + @JvmStatic + fun createEntities() { + entityStore.executeInTransaction { + expectedMessage.toEntity(it) + } + } + + @AfterAll + @JvmStatic + fun deleteDatabase() { + entityStore.close() + File(".test").deleteRecursively() + } + } + + @Test + fun testMagixMessageAndPropertyChangedMessageConverters() { + assertEquals(expectedMessage, entityStore.computeInReadonlyTransaction { + it.getAll("MagixMessage").first?.toMagixMessage() + }!!) + } +}