Skip to content

Commit 8532e54

Browse files
committed
Add tests
1 parent d82a607 commit 8532e54

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package io.github.projectmapk.jackson.module.kogera.zIntegration.deser.valueClass.mapKey
2+
3+
import com.fasterxml.jackson.databind.DeserializationContext
4+
import com.fasterxml.jackson.databind.JsonMappingException
5+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException
6+
import com.fasterxml.jackson.databind.module.SimpleModule
7+
import io.github.projectmapk.jackson.module.kogera.defaultMapper
8+
import io.github.projectmapk.jackson.module.kogera.jacksonObjectMapper
9+
import io.github.projectmapk.jackson.module.kogera.readValue
10+
import io.github.projectmapk.jackson.module.kogera.zIntegration.deser.valueClass.NonNullObject
11+
import io.github.projectmapk.jackson.module.kogera.zIntegration.deser.valueClass.NullableObject
12+
import io.github.projectmapk.jackson.module.kogera.zIntegration.deser.valueClass.Primitive
13+
import org.junit.jupiter.api.Assertions.assertEquals
14+
import org.junit.jupiter.api.Assertions.assertTrue
15+
import org.junit.jupiter.api.Nested
16+
import org.junit.jupiter.api.Test
17+
import org.junit.jupiter.api.assertThrows
18+
import java.lang.reflect.InvocationTargetException
19+
import com.fasterxml.jackson.databind.KeyDeserializer as JacksonKeyDeserializer
20+
21+
class WithoutCustomDeserializeMethodTest {
22+
companion object {
23+
val throwable = IllegalArgumentException("test")
24+
}
25+
26+
@Nested
27+
inner class DirectDeserialize {
28+
@Test
29+
fun primitive() {
30+
val result = defaultMapper.readValue<Map<Primitive, String?>>("""{"1":null}""")
31+
assertEquals(mapOf(Primitive(1) to null), result)
32+
}
33+
34+
@Test
35+
fun nonNullObject() {
36+
val result = defaultMapper.readValue<Map<NonNullObject, String?>>("""{"foo":null}""")
37+
assertEquals(mapOf(NonNullObject("foo") to null), result)
38+
}
39+
40+
@Test
41+
fun nullableObject() {
42+
val result = defaultMapper.readValue<Map<NullableObject, String?>>("""{"bar":null}""")
43+
assertEquals(mapOf(NullableObject("bar") to null), result)
44+
}
45+
}
46+
47+
data class Dst(
48+
val p: Map<Primitive, String?>,
49+
val nn: Map<NonNullObject, String?>,
50+
val n: Map<NullableObject, String?>
51+
)
52+
53+
@Test
54+
fun wrapped() {
55+
val src = """
56+
{
57+
"p":{"1":null},
58+
"nn":{"foo":null},
59+
"n":{"bar":null}
60+
}
61+
""".trimIndent()
62+
val result = defaultMapper.readValue<Dst>(src)
63+
val expected = Dst(
64+
mapOf(Primitive(1) to null),
65+
mapOf(NonNullObject("foo") to null),
66+
mapOf(NullableObject("bar") to null)
67+
)
68+
69+
assertEquals(expected, result)
70+
}
71+
72+
@JvmInline
73+
value class HasCheckConstructor(val value: Int) {
74+
init {
75+
if (value < 0) throw throwable
76+
}
77+
}
78+
79+
@Test
80+
fun callConstructorCheckTest() {
81+
val e = assertThrows<InvocationTargetException> {
82+
defaultMapper.readValue<Map<HasCheckConstructor, String?>>("""{"-1":null}""")
83+
}
84+
assertTrue(e.cause === throwable)
85+
}
86+
87+
data class Wrapped(val first: String, val second: String) {
88+
class KeyDeserializer : JacksonKeyDeserializer() {
89+
override fun deserializeKey(key: String, ctxt: DeserializationContext) =
90+
key.split("-").let { Wrapped(it[0], it[1]) }
91+
}
92+
}
93+
94+
@JvmInline
95+
value class Wrapper(val w: Wrapped)
96+
97+
@Test
98+
fun wrappedCustomObject() {
99+
// If a type that cannot be deserialized is specified, the default is an error.
100+
val thrown = assertThrows<JsonMappingException> {
101+
defaultMapper.readValue<Map<Wrapper, String?>>("""{"foo-bar":null}""")
102+
}
103+
assertTrue(thrown.cause is InvalidDefinitionException)
104+
105+
val mapper = jacksonObjectMapper()
106+
.registerModule(
107+
object : SimpleModule() {
108+
init { addKeyDeserializer(Wrapped::class.java, Wrapped.KeyDeserializer()) }
109+
}
110+
)
111+
112+
val result = mapper.readValue<Map<Wrapper, String?>>("""{"foo-bar":null}""")
113+
val expected = mapOf(Wrapper(Wrapped("foo", "bar")) to null)
114+
115+
assertEquals(expected, result)
116+
}
117+
}

0 commit comments

Comments
 (0)