Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: last used format for device #287

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/src/main/java/io/customer/sdk/data/request/Device.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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?, // nullable to cater for `lastUsed` field in older versions of the SDK
val attributes: CustomAttributes
)

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Device>(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<Device>(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\""
}
}
Loading