|
| 1 | +/* |
| 2 | + * Wire |
| 3 | + * Copyright (C) 2024 Wire Swiss GmbH |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see http://www.gnu.org/licenses/. |
| 17 | + */ |
| 18 | +package com.wire.kalium.logic.feature.conversation |
| 19 | + |
| 20 | +import com.wire.kalium.logic.CoreFailure |
| 21 | +import com.wire.kalium.logic.data.asset.AssetRepository |
| 22 | +import com.wire.kalium.logic.data.id.ConversationId |
| 23 | +import com.wire.kalium.logic.data.message.MessageRepository |
| 24 | +import com.wire.kalium.logic.functional.Either |
| 25 | +import io.mockative.Mock |
| 26 | +import io.mockative.any |
| 27 | +import io.mockative.coEvery |
| 28 | +import io.mockative.coVerify |
| 29 | +import io.mockative.mock |
| 30 | +import kotlinx.coroutines.test.runTest |
| 31 | +import kotlin.test.Test |
| 32 | +import kotlin.test.assertIs |
| 33 | + |
| 34 | +class ClearConversationAssetsLocallyUseCaseTest { |
| 35 | + |
| 36 | + @Test |
| 37 | + fun givenConversationAssetIds_whenAllDeletionsAreSuccess_thenSuccessResultIsPropagated() = runTest { |
| 38 | + // given |
| 39 | + val ids = listOf("id_1", "id_2") |
| 40 | + val (arrangement, useCase) = Arrangement() |
| 41 | + .withAssetIdsResponse(ids) |
| 42 | + .withAssetClearSuccess("id_1") |
| 43 | + .withAssetClearSuccess("id_2") |
| 44 | + .arrange() |
| 45 | + |
| 46 | + // when |
| 47 | + val result = useCase(ConversationId("someValue", "someDomain")) |
| 48 | + |
| 49 | + // then |
| 50 | + assertIs<Either.Right<Unit>>(result) |
| 51 | + coVerify { arrangement.assetRepository.deleteAssetLocally(any()) }.wasInvoked(exactly = 2) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun givenConversationAssetIds_whenOneDeletionFailed_thenFailureResultIsPropagated() = runTest { |
| 56 | + // given |
| 57 | + val ids = listOf("id_1", "id_2") |
| 58 | + val (arrangement, useCase) = Arrangement() |
| 59 | + .withAssetIdsResponse(ids) |
| 60 | + .withAssetClearSuccess("id_1") |
| 61 | + .withAssetClearError("id_2") |
| 62 | + .arrange() |
| 63 | + |
| 64 | + // when |
| 65 | + val result = useCase(ConversationId("someValue", "someDomain")) |
| 66 | + |
| 67 | + // then |
| 68 | + assertIs<Either.Left<Unit>>(result) |
| 69 | + coVerify { arrangement.assetRepository.deleteAssetLocally(any()) }.wasInvoked(exactly = 2) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun givenEmptyConversationAssetIds_whenInvoked_thenDeletionsAreNotInvoked() = runTest { |
| 74 | + // given |
| 75 | + val (arrangement, useCase) = Arrangement() |
| 76 | + .withAssetIdsResponse(emptyList()) |
| 77 | + .arrange() |
| 78 | + |
| 79 | + // when |
| 80 | + val result = useCase(ConversationId("someValue", "someDomain")) |
| 81 | + |
| 82 | + // then |
| 83 | + assertIs<Either.Right<Unit>>(result) |
| 84 | + coVerify { arrangement.assetRepository.deleteAssetLocally(any()) }.wasNotInvoked() |
| 85 | + } |
| 86 | + |
| 87 | + private class Arrangement { |
| 88 | + @Mock |
| 89 | + val messageRepository = mock(MessageRepository::class) |
| 90 | + |
| 91 | + @Mock |
| 92 | + val assetRepository = mock(AssetRepository::class) |
| 93 | + |
| 94 | + suspend fun withAssetClearSuccess(id: String) = apply { |
| 95 | + coEvery { assetRepository.deleteAssetLocally(id) }.returns(Either.Right(Unit)) |
| 96 | + } |
| 97 | + |
| 98 | + suspend fun withAssetClearError(id: String) = apply { |
| 99 | + coEvery { assetRepository.deleteAssetLocally(id) }.returns(Either.Left(CoreFailure.Unknown(null))) |
| 100 | + } |
| 101 | + |
| 102 | + suspend fun withAssetIdsResponse(ids: List<String>) = apply { |
| 103 | + coEvery { messageRepository.getAllAssetIdsFromConversationId(any()) }.returns(Either.Right(ids)) |
| 104 | + } |
| 105 | + |
| 106 | + fun arrange() = this to ClearConversationAssetsLocallyUseCaseImpl( |
| 107 | + messageRepository = messageRepository, |
| 108 | + assetRepository = assetRepository |
| 109 | + ) |
| 110 | + } |
| 111 | +} |
0 commit comments