diff --git a/app/src/main/java/com/tangem/tap/di/domain/WalletsDomainModule.kt b/app/src/main/java/com/tangem/tap/di/domain/WalletsDomainModule.kt index fa95151e3b..5e55ebdd54 100644 --- a/app/src/main/java/com/tangem/tap/di/domain/WalletsDomainModule.kt +++ b/app/src/main/java/com/tangem/tap/di/domain/WalletsDomainModule.kt @@ -1,9 +1,9 @@ package com.tangem.tap.di.domain import com.tangem.core.analytics.api.AnalyticsEventHandler +import com.tangem.domain.account.repository.AccountsCRUDRepository import com.tangem.domain.common.wallets.UserWalletsListRepository import com.tangem.domain.redux.ReduxStateHolder -import com.tangem.domain.tokens.repository.CurrenciesRepository import com.tangem.domain.transaction.WalletAddressServiceRepository import com.tangem.domain.transaction.usecase.ParseSharedAddressUseCase import com.tangem.domain.transaction.usecase.ValidateWalletAddressUseCase @@ -272,11 +272,11 @@ internal object WalletsDomainModule { @Singleton fun providesSetNotificationsEnabledUseCase( walletsRepository: WalletsRepository, - currenciesRepository: CurrenciesRepository, + accountsCRUDRepository: AccountsCRUDRepository, ): SetNotificationsEnabledUseCase { return SetNotificationsEnabledUseCase( walletsRepository = walletsRepository, - currenciesRepository = currenciesRepository, + accountsCRUDRepository = accountsCRUDRepository, ) } diff --git a/domain/wallets/build.gradle.kts b/domain/wallets/build.gradle.kts index b7b3798d96..f11102091a 100644 --- a/domain/wallets/build.gradle.kts +++ b/domain/wallets/build.gradle.kts @@ -25,6 +25,7 @@ dependencies { implementation(projects.domain.walletManager) implementation(projects.libs.blockchainSdk) implementation(projects.libs.tangemSdkApi) + implementation(projects.domain.account) implementation(projects.domain.models) implementation(projects.domain.tokens) implementation(projects.domain.card) diff --git a/domain/wallets/src/main/java/com/tangem/domain/wallets/usecase/SetNotificationsEnabledUseCase.kt b/domain/wallets/src/main/java/com/tangem/domain/wallets/usecase/SetNotificationsEnabledUseCase.kt index 5a66ceb5ab..c749d56049 100644 --- a/domain/wallets/src/main/java/com/tangem/domain/wallets/usecase/SetNotificationsEnabledUseCase.kt +++ b/domain/wallets/src/main/java/com/tangem/domain/wallets/usecase/SetNotificationsEnabledUseCase.kt @@ -1,13 +1,13 @@ package com.tangem.domain.wallets.usecase import arrow.core.Either -import com.tangem.domain.tokens.repository.CurrenciesRepository +import com.tangem.domain.account.repository.AccountsCRUDRepository import com.tangem.domain.models.wallet.UserWalletId import com.tangem.domain.wallets.repository.WalletsRepository class SetNotificationsEnabledUseCase( private val walletsRepository: WalletsRepository, - private val currenciesRepository: CurrenciesRepository, + private val accountsCRUDRepository: AccountsCRUDRepository, ) { suspend operator fun invoke(userWalletId: UserWalletId, isEnabled: Boolean): Either = @@ -16,7 +16,7 @@ class SetNotificationsEnabledUseCase( userWalletId = userWalletId, isEnabled = isEnabled, ) - currenciesRepository.syncTokens(userWalletId) + accountsCRUDRepository.syncTokens(userWalletId) }.onLeft { walletsRepository.setNotificationsEnabled( userWalletId = userWalletId, diff --git a/domain/wallets/src/test/java/com/tangem/domain/wallets/usecase/SetNotificationsEnabledUseCaseTest.kt b/domain/wallets/src/test/java/com/tangem/domain/wallets/usecase/SetNotificationsEnabledUseCaseTest.kt new file mode 100644 index 0000000000..73d291ba22 --- /dev/null +++ b/domain/wallets/src/test/java/com/tangem/domain/wallets/usecase/SetNotificationsEnabledUseCaseTest.kt @@ -0,0 +1,131 @@ +package com.tangem.domain.wallets.usecase + +import com.google.common.truth.Truth.assertThat +import com.tangem.domain.account.repository.AccountsCRUDRepository +import com.tangem.domain.models.wallet.UserWalletId +import com.tangem.domain.wallets.repository.WalletsRepository +import io.mockk.coEvery +import io.mockk.coVerify +import io.mockk.coVerifyOrder +import io.mockk.just +import io.mockk.mockk +import io.mockk.runs +import kotlinx.coroutines.test.runTest +import org.junit.Before +import org.junit.Test + +class SetNotificationsEnabledUseCaseTest { + + private lateinit var useCase: SetNotificationsEnabledUseCase + private lateinit var walletsRepository: WalletsRepository + private lateinit var accountsCRUDRepository: AccountsCRUDRepository + + @Before + fun setup() { + walletsRepository = mockk() + accountsCRUDRepository = mockk() + useCase = SetNotificationsEnabledUseCase( + walletsRepository = walletsRepository, + accountsCRUDRepository = accountsCRUDRepository, + ) + } + + @Test + fun `GIVEN notifications enabled successfully WHEN invoke THEN return Right with Unit`() = runTest { + // GIVEN + val userWalletId = UserWalletId("0A0B0C0D") + val isEnabled = true + coEvery { walletsRepository.setNotificationsEnabled(userWalletId, isEnabled) } just runs + coEvery { accountsCRUDRepository.syncTokens(userWalletId) } just runs + + // WHEN + val result = useCase(userWalletId, isEnabled) + + // THEN + assertThat(result.isRight()).isTrue() + coVerifyOrder { + walletsRepository.setNotificationsEnabled(userWalletId, isEnabled) + accountsCRUDRepository.syncTokens(userWalletId) + } + } + + @Test + fun `GIVEN notifications disabled successfully WHEN invoke THEN return Right with Unit`() = runTest { + // GIVEN + val userWalletId = UserWalletId("0A0B0C0D") + val isEnabled = false + coEvery { walletsRepository.setNotificationsEnabled(userWalletId, isEnabled) } just runs + coEvery { accountsCRUDRepository.syncTokens(userWalletId) } just runs + + // WHEN + val result = useCase(userWalletId, isEnabled) + + // THEN + assertThat(result.isRight()).isTrue() + coVerifyOrder { + walletsRepository.setNotificationsEnabled(userWalletId, isEnabled) + accountsCRUDRepository.syncTokens(userWalletId) + } + } + + @Test + fun `GIVEN setNotificationsEnabled throws exception WHEN invoke THEN return Left and revert notifications`() = runTest { + // GIVEN + val userWalletId = UserWalletId("0A0B0C0D") + val isEnabled = true + val exception = RuntimeException("Network error") + coEvery { walletsRepository.setNotificationsEnabled(userWalletId, isEnabled) } throws exception + coEvery { walletsRepository.setNotificationsEnabled(userWalletId, !isEnabled) } just runs + + // WHEN + val result = useCase(userWalletId, isEnabled) + + // THEN + assertThat(result.isLeft()).isTrue() + result.onLeft { throwable -> + assertThat(throwable).isEqualTo(exception) + } + coVerify { walletsRepository.setNotificationsEnabled(userWalletId, !isEnabled) } + } + + @Test + fun `GIVEN syncTokens throws exception WHEN invoke THEN return Left and revert notifications`() = runTest { + // GIVEN + val userWalletId = UserWalletId("0A0B0C0D") + val isEnabled = true + val exception = RuntimeException("Sync error") + coEvery { walletsRepository.setNotificationsEnabled(userWalletId, isEnabled) } just runs + coEvery { accountsCRUDRepository.syncTokens(userWalletId) } throws exception + coEvery { walletsRepository.setNotificationsEnabled(userWalletId, !isEnabled) } just runs + + // WHEN + val result = useCase(userWalletId, isEnabled) + + // THEN + assertThat(result.isLeft()).isTrue() + result.onLeft { throwable -> + assertThat(throwable).isEqualTo(exception) + } + coVerify { walletsRepository.setNotificationsEnabled(userWalletId, !isEnabled) } + } + + @Test + fun `GIVEN disabling notifications fails WHEN invoke THEN return Left and revert to enabled`() = runTest { + // GIVEN + val userWalletId = UserWalletId("0A0B0C0D") + val isEnabled = false + val exception = RuntimeException("Network error") + coEvery { walletsRepository.setNotificationsEnabled(userWalletId, isEnabled) } throws exception + coEvery { walletsRepository.setNotificationsEnabled(userWalletId, !isEnabled) } just runs + + // WHEN + val result = useCase(userWalletId, isEnabled) + + // THEN + assertThat(result.isLeft()).isTrue() + result.onLeft { throwable -> + assertThat(throwable).isEqualTo(exception) + } + coVerify { walletsRepository.setNotificationsEnabled(userWalletId, true) } + } +} \ No newline at end of file