From 78c66cedbc35845109fab9b09411fe4067252378 Mon Sep 17 00:00:00 2001 From: Shahroz Khan Date: Tue, 12 Mar 2024 18:10:29 +0500 Subject: [PATCH 1/3] updated name --- sdk/src/main/java/io/customer/sdk/data/request/Device.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/src/main/java/io/customer/sdk/data/request/Device.kt b/sdk/src/main/java/io/customer/sdk/data/request/Device.kt index 263f0eaa5..df6285411 100644 --- a/sdk/src/main/java/io/customer/sdk/data/request/Device.kt +++ b/sdk/src/main/java/io/customer/sdk/data/request/Device.kt @@ -9,7 +9,7 @@ import java.util.* data class Device( @field:Json(name = "id") val token: String, val platform: String = "android", - val lastUsed: Date, + @field:Json(name = "last_used") val lastUsed: Date, val attributes: CustomAttributes ) From 2aeead588ddbc021ffd8cd1147a81f0daac32c0b Mon Sep 17 00:00:00 2001 From: Shahroz Khan Date: Thu, 21 Mar 2024 18:29:33 +0500 Subject: [PATCH 2/3] added test --- .../io/customer/sdk/data/request/Device.kt | 2 +- .../sdk/data/moshi/model/DeviceTest.kt | 79 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 sdk/src/sharedTest/java/io/customer/sdk/data/moshi/model/DeviceTest.kt diff --git a/sdk/src/main/java/io/customer/sdk/data/request/Device.kt b/sdk/src/main/java/io/customer/sdk/data/request/Device.kt index df6285411..ef39e6d37 100644 --- a/sdk/src/main/java/io/customer/sdk/data/request/Device.kt +++ b/sdk/src/main/java/io/customer/sdk/data/request/Device.kt @@ -9,7 +9,7 @@ import java.util.* data class Device( @field:Json(name = "id") val token: String, val platform: String = "android", - @field:Json(name = "last_used") val lastUsed: Date, + @field:Json(name = "last_used") val lastUsed: Date?, val attributes: CustomAttributes ) diff --git a/sdk/src/sharedTest/java/io/customer/sdk/data/moshi/model/DeviceTest.kt b/sdk/src/sharedTest/java/io/customer/sdk/data/moshi/model/DeviceTest.kt new file mode 100644 index 000000000..929701ebf --- /dev/null +++ b/sdk/src/sharedTest/java/io/customer/sdk/data/moshi/model/DeviceTest.kt @@ -0,0 +1,79 @@ +package io.customer.sdk.data.moshi.model + +import androidx.test.ext.junit.runners.AndroidJUnit4 +import io.customer.commontest.BaseTest +import io.customer.sdk.data.request.Device +import io.customer.sdk.util.JsonAdapter +import java.util.Date +import org.amshove.kluent.shouldBe +import org.amshove.kluent.shouldBeEqualTo +import org.amshove.kluent.shouldContain +import org.amshove.kluent.shouldNotBe +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class DeviceTest : BaseTest() { + + private lateinit var adapter: JsonAdapter + + override fun setup() { + super.setup() + adapter = di.jsonAdapter + } + + @Test + fun parseDeviceJson_withInvalidLastUsedFormat_shouldStillDeserializeDevice() { + val givenTimestamp = 1683394080L + + val json = """ + { + "id": "123", + "platform": "android", + "lastUsed": $givenTimestamp, + "attributes": {} + } + """.trimIndent() + + val device = jsonAdapter.fromJson(json) + + device shouldNotBe null + device.lastUsed shouldBe null + } + + @Test + fun parseDeviceJson_givenValidLastUsedFormat_expectDeserializeDeviceCorrectly() { + val givenTimestamp = 1683394080L + + // Convert Unix timestamp to Date object for comparison + val expectedLastUsed = Date(givenTimestamp * 1000) // Multiply by 1000 to convert seconds to milliseconds + + val json = """ + { + "id": "123", + "platform": "android", + "last_used": $givenTimestamp, + "attributes": {} + } + """.trimIndent() + + val device = jsonAdapter.fromJson(json) + + device shouldNotBe null + device.lastUsed shouldBeEqualTo expectedLastUsed + } + + @Test + fun serializeJson_verifyCorrectLastUsed() { + val device = Device( + token = "123", + platform = "android", + lastUsed = Date(), + attributes = emptyMap() + ) + + val json = jsonAdapter.toJson(device) + + json shouldContain "\"last_used\"" + } +} From f887b798d574ca39786c387cbb8d799d193a55f9 Mon Sep 17 00:00:00 2001 From: Shahroz Khan Date: Thu, 21 Mar 2024 21:03:49 +0500 Subject: [PATCH 3/3] added comment --- sdk/src/main/java/io/customer/sdk/data/request/Device.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/src/main/java/io/customer/sdk/data/request/Device.kt b/sdk/src/main/java/io/customer/sdk/data/request/Device.kt index ef39e6d37..334120388 100644 --- a/sdk/src/main/java/io/customer/sdk/data/request/Device.kt +++ b/sdk/src/main/java/io/customer/sdk/data/request/Device.kt @@ -9,7 +9,7 @@ import java.util.* data class Device( @field:Json(name = "id") val token: String, val platform: String = "android", - @field:Json(name = "last_used") val lastUsed: Date?, + @field:Json(name = "last_used") val lastUsed: Date?, // nullable to cater for `lastUsed` field in older versions of the SDK val attributes: CustomAttributes )