Last active
April 14, 2020 18:19
-
-
Save diyan/bd928df06aa8af087a93e7f6c4f7ff0e to your computer and use it in GitHub Desktop.
Kotlin, Jackson, JUnit5, AssertJ
This file contains hidden or 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
package com.project.json | |
import com.fasterxml.jackson.databind.ObjectMapper | |
import com.fasterxml.jackson.databind.annotation.JsonDeserialize | |
import com.fasterxml.jackson.module.kotlin.KotlinModule | |
import com.fasterxml.jackson.module.kotlin.readValue | |
import org.assertj.core.api.Assertions.assertThat | |
import org.junit.jupiter.api.Test | |
import org.junit.jupiter.api.TestInstance | |
data class LooseMapSample ( | |
@JsonDeserialize(using=LooseMapDeserializer::class) | |
var value: Map<String, Any?> | |
) | |
class LooseMapDeserializerTest { | |
private val jsonMapper: ObjectMapper = ObjectMapper().registerModule(KotlinModule()) | |
private fun parseSample(jsonString: String): Map<String, Any?> { | |
return jsonMapper.readValue<LooseMapSample>(jsonString).value | |
} | |
@Test | |
fun `Return well-formed Map(String, String) as is`() { | |
assertThat(parseSample( | |
"""{ "value": { "foo": "foo1" } }""") | |
).isEqualTo(mapOf("foo" to "foo1")) | |
} | |
@Test | |
fun `Return well-formed Map(String, Int) as is`() { | |
assertThat(parseSample( | |
"""{ "value": { "foo": 1 } }""") | |
).isEqualTo(mapOf("foo" to 1)) | |
} | |
@Test | |
fun `Return well-formed Map(String, List(Int)) as is`() { | |
assertThat(parseSample( | |
"""{ "value": { "foo": [1, 2, 3] } }""") | |
).isEqualTo(mapOf("foo" to listOf(1, 2, 3))) | |
} | |
@Test | |
fun `Return well-formed Map(String, Map(String, Int)) as is`() { | |
assertThat(parseSample( | |
"""{ "value": { "foo": { "fooNested": 1 } } }""") | |
).isEqualTo(mapOf("foo" to mapOf("fooNested" to 1))) | |
} | |
@Test | |
fun `Return null as empty Map(String, Any)`() { | |
assertThat(parseSample( | |
"""{ "value": null }""") | |
).isEqualTo(emptyMap<String, Any>()) | |
} | |
@Test | |
fun `Covert malformed String to Map(String, String)`() { | |
assertThat(parseSample( | |
"""{ "value": "foo" }""") | |
).isEqualTo(mapOf("root" to "foo")) | |
} | |
@Test | |
fun `Covert malformed Int to Map(String, String)`() { | |
assertThat(parseSample( | |
"""{ "value": 1 }""") | |
).isEqualTo(mapOf("root" to "1")) | |
} | |
@Test | |
fun `Covert malformed Boolean to Map(String, String)`() { | |
assertThat(parseSample( | |
"""{ "value": true }""") | |
).isEqualTo(mapOf("root" to "true")) | |
} | |
@Test | |
fun `Covert malformed List(Int) to Map(String, List(Int))`() { | |
assertThat(parseSample( | |
"""{ "value": [1, 2, 3] }""") | |
).isEqualTo(mapOf("root" to listOf(1, 2, 3))) | |
} | |
} | |
/* | |
STDOUT: | |
com.project.json.LooseMapDeserializerTest | |
Test Return well-formed Map(String, String) as is() PASSED | |
Test Covert malformed Int to Map(String, String)() PASSED | |
Test Return well-formed Map(String, Map(String, Int)) as is() PASSED | |
Test Covert malformed Boolean to Map(String, String)() PASSED | |
Test Return well-formed Map(String, List(Int)) as is() PASSED | |
Test Covert malformed String to Map(String, String)() PASSED | |
Test Return null as empty Map(String, Any)() PASSED | |
Test Return well-formed Map(String, Int) as is() PASSED | |
Test Covert malformed List(Int) to Map(String, List(Int))() PASSED | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment