Skip to content

Commit

Permalink
Unit test: setOffset is called from user & sub executors
Browse files Browse the repository at this point in the history
Add coverage to test we're setting the offset after successful execution
  • Loading branch information
rgomezp committed Aug 14, 2024
1 parent 087f2a4 commit f54bc44
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,6 +41,7 @@ class SubscriptionOperationExecutorTests : FunSpec({
val mockConsistencyManager = mockk<IConsistencyManager>()

beforeTest {
clearMocks(mockConsistencyManager)
coEvery { mockConsistencyManager.setOffset(any(), any(), any()) } just runs
}

Expand Down Expand Up @@ -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<ISubscriptionBackendService>()
coEvery {
mockSubscriptionBackendService.updateSubscription(any(), any(), any())
} returns offset

val mockSubscriptionsModelStore = mockk<SubscriptionModelStore>()
val subscriptionModel1 = SubscriptionModel().apply {
id = remoteSubscriptionId
address = "pushToken1"
}
every { mockSubscriptionsModelStore.get(remoteSubscriptionId) } returns subscriptionModel1

val mockBuildUserService = mockk<IRebuildUserService>()

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)
}
}
})
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -30,6 +32,7 @@ class UpdateUserOperationExecutorTests : FunSpec({
val mockConsistencyManager = mockk<IConsistencyManager>()

beforeTest {
clearMocks(mockConsistencyManager)
coEvery { mockConsistencyManager.setOffset(any(), any(), any()) } just runs
}

Expand Down Expand Up @@ -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<IUserBackendService>()
coEvery {
mockUserBackendService.updateUser(any(), any(), any(), any(), any(), any())
} returns offset

val mockIdentityModelStore = MockHelper.identityModelStore()
val mockPropertiesModelStore = MockHelper.propertiesModelStore()
val mockBuildUserService = mockk<IRebuildUserService>()

val loginUserOperationExecutor =
UpdateUserOperationExecutor(
mockUserBackendService,
mockIdentityModelStore,
mockPropertiesModelStore,
mockBuildUserService,
getNewRecordState(),
mockConsistencyManager
)

val operations = listOf<Operation>(
TrackSessionStartOperation(appId, onesignalId = remoteOneSignalId)
)

// When
loginUserOperationExecutor.execute(operations)

// Then
coVerify(exactly = 1) {
mockConsistencyManager.setOffset(remoteOneSignalId, OffsetKey.USER_UPDATE, offset)
}
}
})

0 comments on commit f54bc44

Please sign in to comment.