From 69cea7ed545b41043789378af0bc463a7eec5942 Mon Sep 17 00:00:00 2001 From: Piotr Mionskowski Date: Mon, 26 Jun 2023 10:02:47 +0200 Subject: [PATCH] Test renames --- .../jackson/CodifiedJacksonModuleTest.kt | 60 +++++++++++++++++-- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/enums-jackson/src/test/kotlin/pl/brightinventions/codified/jackson/CodifiedJacksonModuleTest.kt b/enums-jackson/src/test/kotlin/pl/brightinventions/codified/jackson/CodifiedJacksonModuleTest.kt index 932bd1b..9f5bcec 100644 --- a/enums-jackson/src/test/kotlin/pl/brightinventions/codified/jackson/CodifiedJacksonModuleTest.kt +++ b/enums-jackson/src/test/kotlin/pl/brightinventions/codified/jackson/CodifiedJacksonModuleTest.kt @@ -12,7 +12,7 @@ class CodifiedJacksonModuleTest { val objectMapper = ObjectMapper().findAndRegisterModules() @Test - fun `can serialize and deserialize known value to codified enum`() { + fun `can serialize and deserialize known value to codified enum with string`() { // given val input = Colour.Blue @@ -23,6 +23,18 @@ class CodifiedJacksonModuleTest { output.knownOrNull().shouldEqual(input) } + @Test + fun `can serialize and deserialize known value to codified enum with int`() { + // given + val input = Weight.Heavy + + // when + val output = serdeCodifiedEnum(input) + + // then + output.knownOrNull().shouldEqual(input) + } + @Test fun `can serialize and deserialize not known value to codified enum`() { // given @@ -37,7 +49,7 @@ class CodifiedJacksonModuleTest { } @Test - fun `can serialize and deserialize known value to enum property`() { + fun `can serialize and deserialize known value to enum property with string`() { // given val input = HasColour(Colour.Blue) @@ -48,6 +60,18 @@ class CodifiedJacksonModuleTest { output.shouldEqual(input) } + @Test + fun `can serialize and deserialize known value to enum property with int`() { + // given + val input = HasWeight().apply { weight = Weight.Medium } + + // when + val output = serde(input) + + // then + output.shouldEqual(input) + } + @Test fun `can serialize and deserialize not known value to enum property`() { // given @@ -59,7 +83,7 @@ class CodifiedJacksonModuleTest { } @Test - fun `can serialize and deserialize known value to codified enum property`() { + fun `can deserialize known value to codified enum property`() { // given val input = HasCodifiedColour(Colour.Blue) @@ -71,7 +95,7 @@ class CodifiedJacksonModuleTest { } @Test - fun `can serialize and deserialize not known value to codified enum property`() { + fun `can deserialize not known value to codified enum property`() { // given val input = HasCodifiedColour("pink") // when @@ -86,6 +110,11 @@ class CodifiedJacksonModuleTest { return objectMapper.readValue(json, object : TypeReference>() {}) } + private fun serdeCodifiedEnum(input: Weight): CodifiedEnum { + val json = objectMapper.writer().writeValueAsString(input) + return objectMapper.readValue(json, object : TypeReference>() {}) + } + private fun serdeCodifiedEnum(input: String): CodifiedEnum { val json = objectMapper.writer().writeValueAsString(input) return objectMapper.readValue(json, object : TypeReference>() {}) @@ -122,8 +151,6 @@ class HasColour() { override fun hashCode(): Int { return colour?.hashCode() ?: 0 } - - } class HasCodifiedColour() { @@ -149,6 +176,27 @@ class HasCodifiedColour() { override fun hashCode(): Int { return colour?.hashCode() ?: 0 } +} +@JsonDeserialize(using = CodifiedDeserializer::class) +enum class Weight(override val code: Int) : Codified { + Light(200), Medium(400), Heavy(60); } + +class HasWeight { + var weight: Weight? = null + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as HasWeight + + return weight == other.weight + } + + override fun hashCode(): Int { + return weight?.hashCode() ?: 0 + } +} +