1+ package cz.muni.fi.rpg.model.firestore.jackson
2+
3+ import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
4+ import com.google.firebase.firestore.DocumentSnapshot
5+ import junit.framework.TestCase
6+ import org.mockito.Mockito.`when`
7+ import org.mockito.Mockito.mock
8+
9+ class JacksonAggregateMapperTest : TestCase (" JacksonAggregateMapperTest" ) {
10+ private class AggregateWithGetter {
11+ val field = " foo"
12+
13+ fun getGetter () = field
14+ }
15+
16+ private class AggregateWithIsGetter {
17+ private val field = " foo"
18+
19+ fun isGetter () = false
20+ }
21+
22+ private data class DataAggregate (val field : String )
23+
24+ private data class DataAggregateWithDefaultValue (
25+ val field : String ,
26+ val fieldWithDefaultValue : String = " default_value"
27+ )
28+
29+ fun testGettersAreIgnoredWhenMappingToDocumentData () {
30+ val mapper = JacksonAggregateMapper (AggregateWithGetter ::class , jacksonTypeRef())
31+
32+ assertEquals(
33+ mapOf (" field" to " foo" ),
34+ mapper.toDocumentData(AggregateWithGetter ())
35+ )
36+ }
37+
38+ fun testIsGettersAreIgnoredWhenMappingToDocumentData () {
39+ val mapper = JacksonAggregateMapper (AggregateWithIsGetter ::class , jacksonTypeRef())
40+
41+ assertEquals(
42+ mapOf (" field" to " foo" ),
43+ mapper.toDocumentData(AggregateWithIsGetter ())
44+ )
45+ }
46+
47+ fun testExtraFieldsAreIgnoredWhenHydratingAggregate () {
48+ val mapper = JacksonAggregateMapper (DataAggregate ::class , jacksonTypeRef())
49+
50+ val snapshot = mock(DocumentSnapshot ::class .java)
51+ `when `(snapshot.data).thenReturn(mapOf (" field" to " foo" , " extraField" to " bar" ))
52+
53+ assertEquals(
54+ DataAggregate (" foo" ),
55+ mapper.fromDocumentSnapshot(snapshot)
56+ )
57+ }
58+
59+ fun testIfFieldWithDefaultValueIsMissingDefaultValueIsUsed () {
60+ val mapper = JacksonAggregateMapper (DataAggregateWithDefaultValue ::class , jacksonTypeRef())
61+
62+ val snapshot = mock(DocumentSnapshot ::class .java)
63+ `when `(snapshot.data)
64+ .thenReturn(mapOf (
65+ " field" to " foo" ,
66+ " fieldWithDefaultValue" to " default_value"
67+ ))
68+
69+ assertEquals(
70+ DataAggregateWithDefaultValue (" foo" ),
71+ mapper.fromDocumentSnapshot(snapshot)
72+ )
73+ }
74+ }
0 commit comments