-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
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
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/org/move/cli/runConfigurations/aptos/AptosExitStatus.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,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() |
40 changes: 40 additions & 0 deletions
40
src/test/kotlin/org/move/cli/runConfigurations/aptos/AptosExitStatusTest.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,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) | ||
} | ||
} |