-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed return type of schemalist and implemented tests
- Loading branch information
Showing
3 changed files
with
167 additions
and
47 deletions.
There are no files selected for viewing
88 changes: 43 additions & 45 deletions
88
...ivr-engine-server/src/main/kotlin/org/vitrivr/engine/server/api/rest/KotlinxJsonMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,53 @@ | ||
import io.javalin.json.JsonMapper | ||
import kotlinx.serialization.* | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.serializer | ||
import kotlin.reflect.KType | ||
import java.lang.reflect.ParameterizedType | ||
import java.lang.reflect.Type | ||
import kotlin.reflect.KTypeProjection | ||
import kotlin.reflect.full.createType | ||
import io.javalin.json.JsonMapper | ||
import kotlinx.serialization.* | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.serializer | ||
import kotlin.reflect.KType | ||
import java.lang.reflect.ParameterizedType | ||
import java.lang.reflect.Type | ||
import kotlin.reflect.KTypeProjection | ||
import kotlin.reflect.full.createType | ||
|
||
object KotlinxJsonMapper : JsonMapper { | ||
object KotlinxJsonMapper : JsonMapper { | ||
|
||
private val json = Json { | ||
serializersModule = SerializersModule { } | ||
isLenient = true | ||
ignoreUnknownKeys = true | ||
} | ||
private val json = Json { | ||
isLenient = true | ||
ignoreUnknownKeys = true | ||
} | ||
|
||
override fun <T : Any> fromJsonString(jsonString: String, targetType: Type): T { | ||
return try { | ||
val kType : KType = targetType.toKType() | ||
val serializer = json.serializersModule.serializer(kType) | ||
json.decodeFromString(serializer, jsonString) as T | ||
} catch (e: SerializationException) { | ||
throw Exception("Error while deserializing JSON: ${e.message}") | ||
} catch (e: IllegalStateException) { | ||
throw Exception("Error state: ${e.message}") | ||
override fun <T : Any> fromJsonString(jsonString: String, targetType: Type): T { | ||
return try { | ||
val kType : KType = targetType.toKType() | ||
val serializer = json.serializersModule.serializer(kType) | ||
json.decodeFromString(serializer, jsonString) as T | ||
} catch (e: SerializationException) { | ||
throw Exception("Error while deserializing JSON: ${e.message}") | ||
} catch (e: IllegalStateException) { | ||
throw Exception("Error state: ${e.message}") | ||
} | ||
} | ||
} | ||
|
||
override fun toJsonString(obj: Any, type: Type): String { | ||
return try { | ||
val kType = type.toKType() | ||
val serializer = json.serializersModule.serializer(kType) | ||
json.encodeToString(serializer, obj) | ||
} catch (e: SerializationException) { | ||
throw Exception("Error while serializing JSON: ${e.message}") | ||
} catch (e: IllegalStateException) { | ||
throw Exception("Error state: ${e.message}") | ||
override fun toJsonString(obj: Any, type: Type): String { | ||
return try { | ||
val kType : KType = type.toKType() | ||
val serializer = json.serializersModule.serializer(kType) | ||
json.encodeToString(serializer, obj) | ||
} catch (e: SerializationException) { | ||
throw Exception("Error while serializing JSON: ${e.message}") | ||
} catch (e: IllegalStateException) { | ||
throw Exception("Error state: ${e.message}") | ||
} | ||
} | ||
} | ||
|
||
private fun Type.toKType(): KType { | ||
return when (this) { | ||
is ParameterizedType -> { | ||
val rawType = (this.rawType as Class<*>).kotlin | ||
val args = this.actualTypeArguments.map { it.toKType() } | ||
rawType.createType(args.map { KTypeProjection.invariant(it) }) | ||
private fun Type.toKType(): KType { | ||
return when (this) { | ||
is ParameterizedType -> { | ||
val rawType = (this.rawType as Class<*>).kotlin | ||
val args = this.actualTypeArguments.map { it.toKType() } | ||
rawType.createType(args.map { KTypeProjection.invariant(it) }) | ||
} | ||
is Class<*> -> this.kotlin.createType() | ||
else -> throw IllegalArgumentException("Unsupported Type: $this") | ||
} | ||
is Class<*> -> this.kotlin.createType() | ||
else -> throw IllegalArgumentException("Unsupported Type: $this") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
...engine-server/src/test/kotlin/org/vitrivr/engine/server/api/rest/KotlinxJsonMapperTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import io.javalin.json.JsonMapper | ||
import kotlinx.serialization.* | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.serializer | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.Test | ||
import org.vitrivr.engine.core.config.pipeline.execution.ExecutionStatus | ||
import org.vitrivr.engine.core.model.relationship.Relationship | ||
import org.vitrivr.engine.core.model.retrievable.RetrievableId | ||
import org.vitrivr.engine.core.model.retrievable.Retrieved | ||
import org.vitrivr.engine.query.model.api.result.QueryResult | ||
import org.vitrivr.engine.server.api.rest.handlers.SchemaList | ||
import org.vitrivr.engine.server.api.rest.model.ErrorStatus | ||
import org.vitrivr.engine.server.api.rest.model.IngestStatus | ||
import java.lang.reflect.Type | ||
import java.util.* | ||
|
||
class KotlinxJsonMapperTest { | ||
|
||
private val jsonMapper: JsonMapper = KotlinxJsonMapper | ||
private val schemaList = SchemaList(listOf("schema1", "schema2")) | ||
private val queryResult : QueryResult | ||
private val ingestStatus = IngestStatus(UUID.randomUUID().toString(), ExecutionStatus.RUNNING, System.currentTimeMillis()) | ||
private val errorStatus = ErrorStatus("Error") | ||
|
||
private val schemaListStr = """{"schemas":["schema1","schema2"]}""" | ||
private val ingestStatusStr = """{"jobId":"${ingestStatus.jobId}","executionStatus":"RUNNING","timestamp":${ingestStatus.timestamp}}""" | ||
private val errorStatusStr = """{"message":"Error"}""" | ||
|
||
init { | ||
val retrieveds = listOf(Retrieved(UUID.randomUUID(), "SOURCE:IMAGE", false), | ||
Retrieved(UUID.randomUUID(), "SOURCE:IMAGE", false)) | ||
|
||
retrieveds[0].addRelationship(Relationship.ByRef(retrieveds[0], "partOf", retrieveds[1], false)) | ||
|
||
queryResult = QueryResult(retrieveds) | ||
|
||
} | ||
|
||
@Test | ||
fun testFromJsonString_validSchemaList() { | ||
val result: SchemaList = jsonMapper.fromJsonString(schemaListStr, SchemaList::class.java) | ||
assertNotNull(result) | ||
assertEquals(2, result.schemas.size) | ||
assertTrue(result.schemas.contains("schema1")) | ||
assertTrue(result.schemas.contains("schema2")) | ||
} | ||
|
||
@Test | ||
fun testToJsonString_validSchemaList() { | ||
val jsonString = jsonMapper.toJsonString(schemaList, SchemaList::class.java) | ||
assert(jsonString == schemaListStr) | ||
} | ||
|
||
@Test | ||
fun testFromJsonString_validIngestStatus() { | ||
val result: IngestStatus = jsonMapper.fromJsonString(ingestStatusStr, IngestStatus::class.java) | ||
assertNotNull(result) | ||
assertEquals(ingestStatus.jobId, result.jobId) | ||
assertEquals(ingestStatus.executionStatus, result.executionStatus) | ||
assertEquals(ingestStatus.timestamp, result.timestamp) | ||
} | ||
|
||
@Test | ||
fun testToJsonString_validIngestStatus() { | ||
val jsonString = jsonMapper.toJsonString(ingestStatus, IngestStatus::class.java) | ||
assert(jsonString == ingestStatusStr) | ||
} | ||
|
||
|
||
@Test | ||
fun testFromJsonString_validErrorStatus() { | ||
val result: ErrorStatus = jsonMapper.fromJsonString(errorStatusStr, ErrorStatus::class.java) | ||
assertNotNull(result) | ||
assertEquals(errorStatus.message, result.message) | ||
} | ||
|
||
@Test | ||
fun testToJsonString_validErrorStatus() { | ||
val jsonString = jsonMapper.toJsonString(errorStatus, ErrorStatus::class.java) | ||
assert(jsonString == errorStatusStr) | ||
} | ||
|
||
@Serializable | ||
data class TestObject(val name: String, val age: Int) | ||
|
||
@Test | ||
fun testFromJsonString_validJson() { | ||
val jsonString = """{"name":"John Doe","age":30}""" | ||
val type: Type = TestObject::class.java | ||
val result: TestObject = jsonMapper.fromJsonString(jsonString, type) | ||
assertNotNull(result) | ||
assertEquals("John Doe", result.name) | ||
assertEquals(30, result.age) | ||
} | ||
|
||
@Test | ||
fun testFromJsonString_invalidJson() { | ||
val jsonString = """{"name":"John Doe","age":"thirty"}""" | ||
val type: Type = TestObject::class.java | ||
val exception = assertThrows(Exception::class.java) { | ||
jsonMapper.fromJsonString<TestObject>(jsonString, type) | ||
} | ||
assertTrue(exception.message!!.contains("Error while deserializing JSON")) | ||
} | ||
|
||
@Test | ||
fun testToJsonString_validObject() { | ||
val testObject = TestObject("John Doe", 30) | ||
val type: Type = TestObject::class.java | ||
val jsonString = jsonMapper.toJsonString(testObject, type) | ||
assertNotNull(jsonString) | ||
assertTrue(jsonString.contains("John Doe")) | ||
assertTrue(jsonString.contains("30")) | ||
} | ||
|
||
} |