From 96e105d6afee58492e45403cd551dcec58157357 Mon Sep 17 00:00:00 2001 From: Rahul Raveendran Date: Mon, 5 Aug 2024 13:12:16 +0530 Subject: [PATCH] Added more tests for the BlueshiftNetworkRequestRepositoryImpl class --- ...ueshiftNetworkRequestRepositoryImplTest.kt | 97 ++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) diff --git a/android-sdk/src/androidTest/java/com/blueshift/core/network/BlueshiftNetworkRequestRepositoryImplTest.kt b/android-sdk/src/androidTest/java/com/blueshift/core/network/BlueshiftNetworkRequestRepositoryImplTest.kt index a2595eac..a948e196 100644 --- a/android-sdk/src/androidTest/java/com/blueshift/core/network/BlueshiftNetworkRequestRepositoryImplTest.kt +++ b/android-sdk/src/androidTest/java/com/blueshift/core/network/BlueshiftNetworkRequestRepositoryImplTest.kt @@ -136,4 +136,99 @@ class BlueshiftNetworkRequestRepositoryImplTest { assert(request3 == null) } -} \ No newline at end of file + @Test + fun readNextRequest_shouldReturnTheFirstRequestWhenAllRequestsInTheQueueRetryAttemptBalanceGreaterThanZero(): Unit = + runBlocking { + for (i in 1..3) { + val url = "https://api.com/$i" + val method = BlueshiftNetworkRequest.Method.GET + val header = JSONObject(mapOf("Content-Type" to "application/json")) + val body = JSONObject() + val authRequired = true + val retryBalance = 3 + val retryTimestamp = 0L + val timestamp = System.currentTimeMillis() + val request = BlueshiftNetworkRequest( + url = url, + method = method, + header = header, + body = body, + authorizationRequired = authRequired, + retryAttemptBalance = retryBalance, + retryAttemptTimestamp = retryTimestamp, + timestamp = timestamp + ) + repository.insertRequest(request) + } + + val request = repository.readNextRequest() + assert(request != null) + request?.let { + assert(it.url == "https://api.com/1") + } + } + + @Test + fun readNextRequest_shouldReturnNullWhenAllRequestsInTheQueueHasRetryAttemptBalanceEqualToZero(): Unit = + runBlocking { + for (i in 1..3) { + val url = "https://api.com/$i" + val method = BlueshiftNetworkRequest.Method.GET + val header = JSONObject(mapOf("Content-Type" to "application/json")) + val body = JSONObject() + val authRequired = true + val retryBalance = 0 + val retryTimestamp = 0L + val timestamp = System.currentTimeMillis() + val request = BlueshiftNetworkRequest( + url = url, + method = method, + header = header, + body = body, + authorizationRequired = authRequired, + retryAttemptBalance = retryBalance, + retryAttemptTimestamp = retryTimestamp, + timestamp = timestamp + ) + repository.insertRequest(request) + } + + val request = repository.readNextRequest() + assert(request == null) + } + + @Test + fun readNextRequest_shouldReturnTheRequestWithRetryAttemptTimestampLessThanCurrentTime(): Unit = + runBlocking { + for (i in 1..2) { + val fiveMinutes = 5 * 60 * 1000 + val url = "https://api.com/$i" + val method = BlueshiftNetworkRequest.Method.GET + val header = JSONObject(mapOf("Content-Type" to "application/json")) + val body = JSONObject() + val authRequired = true + val retryBalance = 1 + val retryTimestamp = if (i % 2 == 0) System.currentTimeMillis() + fiveMinutes else System.currentTimeMillis() - fiveMinutes + val timestamp = System.currentTimeMillis() + val request = BlueshiftNetworkRequest( + url = url, + method = method, + header = header, + body = body, + authorizationRequired = authRequired, + retryAttemptBalance = retryBalance, + retryAttemptTimestamp = retryTimestamp, + timestamp = timestamp + ) + repository.insertRequest(request) + } + + // i = 1 -> current time - 5min + // i = 2 -> current time + 5min + val request = repository.readNextRequest() + assert(request != null) + request?.let { + assert(it.url == "https://api.com/1") + } + } +}