BIT-636: Rectify sync api response model (#145)

This commit is contained in:
Ramsey Smith
2023-10-23 12:01:50 -06:00
committed by GitHub
parent 98c09e5bf4
commit 21424f52dd
12 changed files with 633 additions and 41 deletions

View File

@@ -0,0 +1,96 @@
package com.x8bit.bitwarden.data.platform.datasource.network.serializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.modules.SerializersModule
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.time.LocalDateTime
class LocalDateTimeSerializerTest {
private val json = Json {
serializersModule = SerializersModule {
contextual(LocalDateTime::class, LocalDateTimeSerializer())
}
}
@Test
fun `properly deserializes raw JSON to LocalDate`() {
assertEquals(
LocalDateTimeData(
dataAsLocalDateTime = LocalDateTime.of(
2023,
10,
6,
17,
22,
28,
440000000,
),
),
json.decodeFromString<LocalDateTimeData>(
"""
{
"dataAsLocalDateTime": "2023-10-06T17:22:28.44Z"
}
""",
),
)
}
@Test
fun `properly deserializes raw JSON with nano seconds to LocalDate`() {
assertEquals(
LocalDateTimeData(
dataAsLocalDateTime = LocalDateTime.of(
2023,
10,
6,
17,
22,
28,
446666700,
),
),
json.decodeFromString<LocalDateTimeData>(
"""
{
"dataAsLocalDateTime": "2023-10-06T17:22:28.4466667Z"
}
""",
),
)
}
@Test
fun `properly serializes external model back to raw JSON`() {
assertEquals(
json.parseToJsonElement(
"""
{
"dataAsLocalDateTime": "2023-10-06T17:22:28.44Z"
}
""",
),
json.encodeToJsonElement(
LocalDateTimeData(
dataAsLocalDateTime = LocalDateTime.of(
2023,
10,
6,
17,
22,
28,
440000000,
),
),
),
)
}
}
@Serializable
private data class LocalDateTimeData(
@Serializable(LocalDateTimeSerializer::class)
val dataAsLocalDateTime: LocalDateTime,
)