mirror of
https://github.com/bitwarden/android.git
synced 2026-06-03 03:06:21 -05:00
BIT-636: Rectify sync api response model (#145)
This commit is contained in:
@@ -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,
|
||||
)
|
||||
Reference in New Issue
Block a user