From 11fbc3d4808824c139ee7f90bd6bebd780e851b4 Mon Sep 17 00:00:00 2001 From: Maksim Kurnikov Date: Sun, 10 Nov 2024 13:13:50 +0100 Subject: [PATCH] add aptos result json parser --- build.gradle.kts | 1 + .../aptos/AptosExitStatus.kt | 35 ++++++++++++++++ .../aptos/AptosExitStatusTest.kt | 40 +++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 src/main/kotlin/org/move/cli/runConfigurations/aptos/AptosExitStatus.kt create mode 100644 src/test/kotlin/org/move/cli/runConfigurations/aptos/AptosExitStatusTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index 1f30c9d32..f2db5dae7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -100,6 +100,7 @@ allprojects { } // cannot be updated further, problems with underlying library implementation("com.github.ajalt.clikt:clikt:3.5.4") + implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.18.+") testImplementation("junit:junit:4.13.2") testImplementation("org.opentest4j:opentest4j:1.3.0") diff --git a/src/main/kotlin/org/move/cli/runConfigurations/aptos/AptosExitStatus.kt b/src/main/kotlin/org/move/cli/runConfigurations/aptos/AptosExitStatus.kt new file mode 100644 index 000000000..afae68d31 --- /dev/null +++ b/src/main/kotlin/org/move/cli/runConfigurations/aptos/AptosExitStatus.kt @@ -0,0 +1,35 @@ +package org.move.cli.runConfigurations.aptos + +import com.fasterxml.jackson.core.JacksonException +import com.fasterxml.jackson.databind.DeserializationFeature +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.registerKotlinModule +import org.intellij.lang.annotations.Language + +sealed class AptosExitStatus(val message: String) { + class Result(message: String): AptosExitStatus(message) + class Error(message: String): AptosExitStatus(message) + class Malformed(jsonText: String): AptosExitStatus(jsonText) + + companion object { + @Throws(JacksonException::class) + fun fromJson(@Language("JSON") json: String): AptosExitStatus { + val parsedResult = JSON_MAPPER.readValue(json, AptosJsonResult::class.java) + return when { + parsedResult.Error != null -> Error(parsedResult.Error) + parsedResult.Result != null -> Result(parsedResult.Result) + else -> Malformed(json) + } + } + } +} + +@Suppress("PropertyName") +private data class AptosJsonResult( + val Result: String?, + val Error: String? +) + +private val JSON_MAPPER: ObjectMapper = ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .registerKotlinModule() diff --git a/src/test/kotlin/org/move/cli/runConfigurations/aptos/AptosExitStatusTest.kt b/src/test/kotlin/org/move/cli/runConfigurations/aptos/AptosExitStatusTest.kt new file mode 100644 index 000000000..1da6c1a13 --- /dev/null +++ b/src/test/kotlin/org/move/cli/runConfigurations/aptos/AptosExitStatusTest.kt @@ -0,0 +1,40 @@ +package org.move.cli.runConfigurations.aptos + +import org.move.utils.tests.MvTestBase + +class AptosExitStatusTest: MvTestBase() { + fun `test parse result`() { + val status = AptosExitStatus.fromJson( + """ + { + "Result": "my result message" + } + """ + ) + check(status is AptosExitStatus.Result) + check(status.message == "my result message") + } + + fun `test parse error`() { + val status = AptosExitStatus.fromJson( + """ + { + "Error": "my error message" + } + """ + ) + check(status is AptosExitStatus.Error) + check(status.message == "my error message") + } + + fun `test parse malformed`() { + val status = AptosExitStatus.fromJson( + """ + { + "Unknown": "unknown" + } + """ + ) + check(status is AptosExitStatus.Malformed) + } +} \ No newline at end of file