diff --git a/OneSignalSDK/onesignal/core/src/test/java/com/onesignal/user/internal/operations/SubscriptionOperationExecutorTests.kt b/OneSignalSDK/onesignal/core/src/test/java/com/onesignal/user/internal/operations/SubscriptionOperationExecutorTests.kt index 3f368b0ef..4d99ef32d 100644 --- a/OneSignalSDK/onesignal/core/src/test/java/com/onesignal/user/internal/operations/SubscriptionOperationExecutorTests.kt +++ b/OneSignalSDK/onesignal/core/src/test/java/com/onesignal/user/internal/operations/SubscriptionOperationExecutorTests.kt @@ -2,23 +2,27 @@ package com.onesignal.user.internal.operations import br.com.colman.kotest.android.extensions.robolectric.RobolectricTest import com.onesignal.common.IConsistencyManager +import com.onesignal.common.consistency.OffsetKey import com.onesignal.common.exceptions.BackendException import com.onesignal.core.internal.operations.ExecutionResult import com.onesignal.core.internal.operations.Operation import com.onesignal.mocks.AndroidMockHelper import com.onesignal.mocks.MockHelper import com.onesignal.user.internal.backend.ISubscriptionBackendService +import com.onesignal.user.internal.backend.IUserBackendService import com.onesignal.user.internal.backend.IdentityConstants import com.onesignal.user.internal.backend.SubscriptionObjectType import com.onesignal.user.internal.builduser.IRebuildUserService import com.onesignal.user.internal.operations.ExecutorMocks.Companion.getNewRecordState import com.onesignal.user.internal.operations.impl.executors.SubscriptionOperationExecutor +import com.onesignal.user.internal.operations.impl.executors.UpdateUserOperationExecutor import com.onesignal.user.internal.subscriptions.SubscriptionModel import com.onesignal.user.internal.subscriptions.SubscriptionModelStore import com.onesignal.user.internal.subscriptions.SubscriptionStatus import com.onesignal.user.internal.subscriptions.SubscriptionType import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.shouldBe +import io.mockk.clearMocks import io.mockk.coEvery import io.mockk.coVerify import io.mockk.every @@ -37,6 +41,7 @@ class SubscriptionOperationExecutorTests : FunSpec({ val mockConsistencyManager = mockk() beforeTest { + clearMocks(mockConsistencyManager) coEvery { mockConsistencyManager.setOffset(any(), any(), any()) } just runs } @@ -709,4 +714,54 @@ class SubscriptionOperationExecutorTests : FunSpec({ // Then response.result shouldBe ExecutionResult.FAIL_RETRY } + + test("setOffset is called after successful subscription update") { + // Given + val mockSubscriptionBackendService = mockk() + coEvery { + mockSubscriptionBackendService.updateSubscription(any(), any(), any()) + } returns offset + + val mockSubscriptionsModelStore = mockk() + val subscriptionModel1 = SubscriptionModel().apply { + id = remoteSubscriptionId + address = "pushToken1" + } + every { mockSubscriptionsModelStore.get(remoteSubscriptionId) } returns subscriptionModel1 + + val mockBuildUserService = mockk() + + val subscriptionOperationExecutor = + SubscriptionOperationExecutor( + mockSubscriptionBackendService, + MockHelper.deviceService(), + AndroidMockHelper.applicationService(), + mockSubscriptionsModelStore, + MockHelper.configModelStore(), + mockBuildUserService, + getNewRecordState(), + mockConsistencyManager + ) + + val operations = + listOf( + UpdateSubscriptionOperation( + appId, + remoteOneSignalId, + remoteSubscriptionId, + SubscriptionType.PUSH, + true, + "pushToken2", + SubscriptionStatus.SUBSCRIBED, + ), + ) + + // When + val response = subscriptionOperationExecutor.execute(operations) + + // Then + coVerify(exactly = 1) { + mockConsistencyManager.setOffset(remoteOneSignalId, OffsetKey.SUBSCRIPTION_UPDATE, offset) + } + } }) diff --git a/OneSignalSDK/onesignal/core/src/test/java/com/onesignal/user/internal/operations/UpdateUserOperationExecutorTests.kt b/OneSignalSDK/onesignal/core/src/test/java/com/onesignal/user/internal/operations/UpdateUserOperationExecutorTests.kt index 619811d0e..240123064 100644 --- a/OneSignalSDK/onesignal/core/src/test/java/com/onesignal/user/internal/operations/UpdateUserOperationExecutorTests.kt +++ b/OneSignalSDK/onesignal/core/src/test/java/com/onesignal/user/internal/operations/UpdateUserOperationExecutorTests.kt @@ -1,6 +1,7 @@ package com.onesignal.user.internal.operations import com.onesignal.common.IConsistencyManager +import com.onesignal.common.consistency.OffsetKey import com.onesignal.common.exceptions.BackendException import com.onesignal.core.internal.operations.ExecutionResult import com.onesignal.core.internal.operations.Operation @@ -14,6 +15,7 @@ import com.onesignal.user.internal.properties.PropertiesModel import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldNotBe +import io.mockk.clearMocks import io.mockk.coEvery import io.mockk.coVerify import io.mockk.every @@ -30,6 +32,7 @@ class UpdateUserOperationExecutorTests : FunSpec({ val mockConsistencyManager = mockk() beforeTest { + clearMocks(mockConsistencyManager) coEvery { mockConsistencyManager.setOffset(any(), any(), any()) } just runs } @@ -353,4 +356,38 @@ class UpdateUserOperationExecutorTests : FunSpec({ response.result shouldBe ExecutionResult.FAIL_RETRY response.retryAfterSeconds shouldBe 10 } + + test("setOffset is called after successful user update of session count") { + // Given + val mockUserBackendService = mockk() + coEvery { + mockUserBackendService.updateUser(any(), any(), any(), any(), any(), any()) + } returns offset + + val mockIdentityModelStore = MockHelper.identityModelStore() + val mockPropertiesModelStore = MockHelper.propertiesModelStore() + val mockBuildUserService = mockk() + + val loginUserOperationExecutor = + UpdateUserOperationExecutor( + mockUserBackendService, + mockIdentityModelStore, + mockPropertiesModelStore, + mockBuildUserService, + getNewRecordState(), + mockConsistencyManager + ) + + val operations = listOf( + TrackSessionStartOperation(appId, onesignalId = remoteOneSignalId) + ) + + // When + loginUserOperationExecutor.execute(operations) + + // Then + coVerify(exactly = 1) { + mockConsistencyManager.setOffset(remoteOneSignalId, OffsetKey.USER_UPDATE, offset) + } + } })