-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move to folder [WPB-14627] (#3213)
* feat: move conversation to folder * feat: move to folder use case * refactor and added tests * tests fix
- Loading branch information
Showing
18 changed files
with
593 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...tlin/com/wire/kalium/logic/feature/conversation/folder/MoveConversationToFolderUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
|
||
package com.wire.kalium.logic.feature.conversation.folder | ||
|
||
import com.wire.kalium.logic.CoreFailure | ||
import com.wire.kalium.logic.data.conversation.folders.ConversationFolderRepository | ||
import com.wire.kalium.logic.data.id.ConversationId | ||
import com.wire.kalium.logic.functional.Either | ||
import com.wire.kalium.logic.functional.flatMap | ||
import com.wire.kalium.logic.functional.fold | ||
import com.wire.kalium.util.KaliumDispatcher | ||
import com.wire.kalium.util.KaliumDispatcherImpl | ||
import kotlinx.coroutines.withContext | ||
|
||
/** | ||
* This use case will move a conversation to the selected folder. | ||
*/ | ||
interface MoveConversationToFolderUseCase { | ||
/** | ||
* @param conversationId the id of the conversation | ||
* @param folderId the id of the conversation folder | ||
* @param previousFolderId the id of the previous folder, if any | ||
* @return the [Result] indicating a successful operation, otherwise a [CoreFailure] | ||
*/ | ||
suspend operator fun invoke( | ||
conversationId: ConversationId, | ||
folderId: String, | ||
previousFolderId: String? | ||
): Result | ||
|
||
sealed interface Result { | ||
data object Success : Result | ||
data class Failure(val cause: CoreFailure) : Result | ||
} | ||
} | ||
|
||
internal class MoveConversationToFolderUseCaseImpl( | ||
private val conversationFolderRepository: ConversationFolderRepository, | ||
private val dispatchers: KaliumDispatcher = KaliumDispatcherImpl | ||
) : MoveConversationToFolderUseCase { | ||
override suspend fun invoke( | ||
conversationId: ConversationId, | ||
folderId: String, | ||
previousFolderId: String? | ||
): MoveConversationToFolderUseCase.Result = withContext(dispatchers.io) { | ||
( | ||
previousFolderId?.let { | ||
conversationFolderRepository.removeConversationFromFolder(conversationId, it) | ||
} ?: Either.Right(Unit) | ||
) | ||
.flatMap { | ||
conversationFolderRepository.addConversationToFolder( | ||
conversationId, | ||
folderId | ||
) | ||
} | ||
.flatMap { conversationFolderRepository.syncConversationFoldersFromLocal() } | ||
.fold({ | ||
MoveConversationToFolderUseCase.Result.Failure(it) | ||
}, { | ||
MoveConversationToFolderUseCase.Result.Success | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
.../com/wire/kalium/logic/feature/conversation/folder/MoveConversationToFolderUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2025 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.kalium.logic.feature.conversation.folder | ||
|
||
import com.wire.kalium.logic.CoreFailure | ||
import com.wire.kalium.logic.data.conversation.folders.ConversationFolderRepository | ||
import com.wire.kalium.logic.data.id.ConversationId | ||
import com.wire.kalium.logic.feature.conversation.folder.MoveConversationToFolderUseCase.Result | ||
import com.wire.kalium.logic.framework.TestConversation | ||
import com.wire.kalium.logic.functional.Either | ||
import io.mockative.Mock | ||
import io.mockative.coEvery | ||
import io.mockative.coVerify | ||
import io.mockative.mock | ||
import io.mockative.once | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertIs | ||
|
||
class MoveConversationToFolderUseCaseTest { | ||
|
||
@Test | ||
fun givenValidConversationAndFolder_WhenMoveIsSuccessful_ThenReturnSuccess() = runTest { | ||
val testConversationId = TestConversation.ID | ||
val testFolderId = "test-folder-id" | ||
val previousFolderId = "previous-folder-id" | ||
|
||
val (arrangement, moveConversationUseCase) = Arrangement() | ||
.withRemoveConversationFromFolder(testConversationId, previousFolderId, Either.Right(Unit)) | ||
.withAddConversationToFolder(testConversationId, testFolderId, Either.Right(Unit)) | ||
.withSyncFolders(Either.Right(Unit)) | ||
.arrange() | ||
|
||
val result = moveConversationUseCase(testConversationId, testFolderId, previousFolderId) | ||
|
||
assertIs<Result.Success>(result) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.removeConversationFromFolder(testConversationId, previousFolderId) | ||
}.wasInvoked(exactly = once) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.addConversationToFolder(testConversationId, testFolderId) | ||
}.wasInvoked(exactly = once) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.syncConversationFoldersFromLocal() | ||
}.wasInvoked(exactly = once) | ||
} | ||
|
||
@Test | ||
fun givenValidConversationAndFolder_WhenRemoveFails_ThenReturnFailure() = runTest { | ||
val testConversationId = TestConversation.ID | ||
val testFolderId = "test-folder-id" | ||
val previousFolderId = "previous-folder-id" | ||
|
||
val (arrangement, moveConversationUseCase) = Arrangement() | ||
.withRemoveConversationFromFolder(testConversationId, previousFolderId, Either.Left(CoreFailure.Unknown(null))) | ||
.arrange() | ||
|
||
val result = moveConversationUseCase(testConversationId, testFolderId, previousFolderId) | ||
|
||
assertIs<Result.Failure>(result) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.removeConversationFromFolder(testConversationId, previousFolderId) | ||
}.wasInvoked(exactly = once) | ||
} | ||
|
||
@Test | ||
fun givenValidConversationAndFolder_WhenAddFails_ThenReturnFailure() = runTest { | ||
val testConversationId = TestConversation.ID | ||
val testFolderId = "test-folder-id" | ||
val previousFolderId = "previous-folder-id" | ||
|
||
val (arrangement, moveConversationUseCase) = Arrangement() | ||
.withRemoveConversationFromFolder(testConversationId, previousFolderId, Either.Right(Unit)) | ||
.withAddConversationToFolder(testConversationId, testFolderId, Either.Left(CoreFailure.Unknown(null))) | ||
.arrange() | ||
|
||
val result = moveConversationUseCase(testConversationId, testFolderId, previousFolderId) | ||
|
||
assertIs<Result.Failure>(result) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.removeConversationFromFolder(testConversationId, previousFolderId) | ||
}.wasInvoked(exactly = once) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.addConversationToFolder(testConversationId, testFolderId) | ||
}.wasInvoked(exactly = once) | ||
} | ||
|
||
@Test | ||
fun givenValidConversationAndFolder_WhenSyncFails_ThenReturnFailure() = runTest { | ||
val testConversationId = TestConversation.ID | ||
val testFolderId = "test-folder-id" | ||
val previousFolderId = "previous-folder-id" | ||
|
||
val (arrangement, moveConversationUseCase) = Arrangement() | ||
.withRemoveConversationFromFolder(testConversationId, previousFolderId, Either.Right(Unit)) | ||
.withAddConversationToFolder(testConversationId, testFolderId, Either.Right(Unit)) | ||
.withSyncFolders(Either.Left(CoreFailure.Unknown(null))) | ||
.arrange() | ||
|
||
val result = moveConversationUseCase(testConversationId, testFolderId, previousFolderId) | ||
|
||
assertIs<Result.Failure>(result) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.removeConversationFromFolder(testConversationId, previousFolderId) | ||
}.wasInvoked(exactly = once) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.addConversationToFolder(testConversationId, testFolderId) | ||
}.wasInvoked(exactly = once) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.syncConversationFoldersFromLocal() | ||
}.wasInvoked(exactly = once) | ||
} | ||
|
||
private class Arrangement { | ||
@Mock | ||
val conversationFolderRepository = mock(ConversationFolderRepository::class) | ||
|
||
private val moveConversationToFolderUseCase = MoveConversationToFolderUseCaseImpl( | ||
conversationFolderRepository | ||
) | ||
|
||
suspend fun withRemoveConversationFromFolder( | ||
conversationId: ConversationId, | ||
folderId: String, | ||
either: Either<CoreFailure, Unit> | ||
) = apply { | ||
coEvery { | ||
conversationFolderRepository.removeConversationFromFolder(conversationId, folderId) | ||
}.returns(either) | ||
} | ||
|
||
suspend fun withAddConversationToFolder( | ||
conversationId: ConversationId, | ||
folderId: String, | ||
either: Either<CoreFailure, Unit> | ||
) = apply { | ||
coEvery { | ||
conversationFolderRepository.addConversationToFolder(conversationId, folderId) | ||
}.returns(either) | ||
} | ||
|
||
suspend fun withSyncFolders(either: Either<CoreFailure, Unit>) = apply { | ||
coEvery { | ||
conversationFolderRepository.syncConversationFoldersFromLocal() | ||
}.returns(either) | ||
} | ||
|
||
fun arrange(block: Arrangement.() -> Unit = { }) = apply(block).let { this to moveConversationToFolderUseCase } | ||
} | ||
} | ||
|
Oops, something went wrong.