Skip to content

Commit 4ad779d

Browse files
committed
Merge branch 'feat/data-forward-compat' into 'master'
Opravena kompatibilita mapperu See merge request fmasa/pv239-project!80
2 parents 51fa153 + 8cb0b04 commit 4ad779d

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,6 @@ dependencies {
139139
implementation "io.arrow-kt:arrow-core:0.10.4"
140140

141141
implementation 'com.jakewharton.timber:timber:4.7.1'
142+
143+
testImplementation 'org.mockito:mockito-core:2.7.22'
142144
}

app/src/main/java/cz/muni/fi/rpg/model/firestore/jackson/JacksonAggregateMapper.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cz.muni.fi.rpg.model.firestore.jackson
33
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility
44
import com.fasterxml.jackson.annotation.PropertyAccessor
55
import com.fasterxml.jackson.core.type.TypeReference
6+
import com.fasterxml.jackson.databind.DeserializationFeature
67
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
78
import com.google.firebase.firestore.DocumentSnapshot
89
import cz.muni.fi.rpg.model.firestore.AggregateMapper
@@ -19,6 +20,7 @@ internal class JacksonAggregateMapper<T : Any>(
1920
.setVisibility(PropertyAccessor.GETTER, Visibility.NONE)
2021
.setVisibility(PropertyAccessor.FIELD, Visibility.ANY)
2122
.setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE)
23+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
2224

2325
override fun fromDocumentSnapshot(snapshot: DocumentSnapshot): T {
2426
Timber.d("Mapping document $snapshot to ${aggregateType.simpleName}")
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)