Add converters from Entity and ConvertersTest

This commit is contained in:
Atos1337 2021-11-19 22:15:50 +03:00
parent 1d8cc33c91
commit 53f9af3ba4
2 changed files with 97 additions and 0 deletions

View File

@ -2,8 +2,13 @@ package ru.mipt.npm.controls.xodus
import jetbrains.exodus.entitystore.Entity import jetbrains.exodus.entitystore.Entity
import jetbrains.exodus.entitystore.StoreTransaction 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.controls.api.PropertyChangedMessage
import ru.mipt.npm.magix.api.MagixMessage 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 { public fun PropertyChangedMessage.toEntity(transaction: StoreTransaction): Entity {
val entity = transaction.newEntity("PropertyChangedMessage") val entity = transaction.newEntity("PropertyChangedMessage")
@ -16,6 +21,21 @@ public fun PropertyChangedMessage.toEntity(transaction: StoreTransaction): Entit
return entity 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 <T> MagixMessage<T>.toEntity(transaction: StoreTransaction): Entity { public fun <T> MagixMessage<T>.toEntity(transaction: StoreTransaction): Entity {
val entity = transaction.newEntity("MagixMessage") val entity = transaction.newEntity("MagixMessage")
entity.setProperty("format", format) entity.setProperty("format", format)
@ -30,3 +50,21 @@ public fun <T> MagixMessage<T>.toEntity(transaction: StoreTransaction): Entity {
user?.let { entity.setProperty("user", it.toString()) } user?.let { entity.setProperty("user", it.toString()) }
return entity return entity
} }
public fun Entity.toMagixMessage(): MagixMessage<PropertyChangedMessage>? {
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) }
)
}
}

View File

@ -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()
}!!)
}
}