diff --git a/android/nativebrik/build.gradle.kts b/android/nativebrik/build.gradle.kts index 9b08be6..eda0cfe 100644 --- a/android/nativebrik/build.gradle.kts +++ b/android/nativebrik/build.gradle.kts @@ -71,6 +71,7 @@ dependencies { implementation("androidx.core:core-ktx:1.12.0") implementation("androidx.appcompat:appcompat:1.6.1") testImplementation("junit:junit:4.13.2") + testImplementation("org.mockito:mockito-core:5.14.2") androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") } diff --git a/android/nativebrik/src/main/java/com/nativebrik/sdk/data/user/user.kt b/android/nativebrik/src/main/java/com/nativebrik/sdk/data/user/user.kt index f3c271e..a643ab8 100644 --- a/android/nativebrik/src/main/java/com/nativebrik/sdk/data/user/user.kt +++ b/android/nativebrik/src/main/java/com/nativebrik/sdk/data/user/user.kt @@ -28,6 +28,7 @@ internal const val USER_SEED_MAX = 100000000 internal const val USER_SEED_KEY = "NATIVEBRIK_USER_SEED" internal var DATETIME_OFFSET: Long = 0 + internal fun getCurrentDate(): ZonedDateTime { val currentMillis = ZonedDateTime.now().toInstant().toEpochMilli() return ZonedDateTime.ofInstant( @@ -39,7 +40,7 @@ internal fun getCurrentDate(): ZonedDateTime { internal fun syncDateFromHttpResponse(t0: Long, connection: HttpURLConnection) { val t1 = System.currentTimeMillis() - val serverDateStr = connection.headerFields["Date"]?.firstOrNull() ?: return + val serverDateStr = connection.getHeaderField("Date") ?: return val serverTime = try { val formatter = SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US) diff --git a/android/nativebrik/src/test/java/com/nativebrik/sdk/data/user/user.kt b/android/nativebrik/src/test/java/com/nativebrik/sdk/data/user/user.kt new file mode 100644 index 0000000..e28da81 --- /dev/null +++ b/android/nativebrik/src/test/java/com/nativebrik/sdk/data/user/user.kt @@ -0,0 +1,44 @@ +package com.nativebrik.sdk.data.user + +import org.junit.Assert.assertTrue +import org.junit.Test +import org.mockito.Mockito.mock +import org.mockito.Mockito.`when` +import java.net.HttpURLConnection +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale +import java.util.TimeZone +import kotlin.math.abs + +class UtilsUnitTest { + @Test + fun testSyncDateFromHttpResponse_shouldWork() { + com.nativebrik.sdk.data.user.DATETIME_OFFSET = 0 + val now = System.currentTimeMillis() + val tomorrow = Date(now + (24 * 60 * 60 * 1000)) + val formatter = SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US).apply { + timeZone = TimeZone.getTimeZone("GMT") + } + val formattedDate = formatter.format(tomorrow) + val connection = mock(HttpURLConnection::class.java) + `when`(connection.responseCode).thenReturn(400) + `when`(connection.getHeaderField("Date")).thenReturn(formattedDate) + + syncDateFromHttpResponse(now, connection) + val offset = com.nativebrik.sdk.data.user.DATETIME_OFFSET + val diff = abs(offset - 24 * 60 * 60 * 1000) + + assertTrue("time offset should be around 24 hours", diff < 5000) + } + + @Test + fun testGetCurrentDate() { + com.nativebrik.sdk.data.user.DATETIME_OFFSET = 24 * 60 * 60 * 1000 + val deviceCurrent = System.currentTimeMillis() + val syncedCurrent = getCurrentDate().toInstant().toEpochMilli() + val diff = (syncedCurrent - deviceCurrent) / 1000 + + assertTrue("(diff - 24 hours) should be around 2 sec", abs(diff - 24 * 60 * 60) < 2) + } +} \ No newline at end of file