-
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: GetNextAudioMessageInConversationUseCase WPB-11725 (#3191)
* feat/add_get_sender_name_by_message_id_use_case * Added unit test for new DB query * Fixed test * feat: GetNextAudioMessageInConversationUseCase * Fixed code style
- Loading branch information
1 parent
3c7855a
commit ecdca03
Showing
8 changed files
with
262 additions
and
16 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
53 changes: 53 additions & 0 deletions
53
.../kotlin/com/wire/kalium/logic/feature/message/GetNextAudioMessageInConversationUseCase.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,53 @@ | ||
/* | ||
* 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.message | ||
|
||
import com.wire.kalium.logic.CoreFailure | ||
import com.wire.kalium.logic.StorageFailure | ||
import com.wire.kalium.logic.data.id.ConversationId | ||
import com.wire.kalium.logic.data.message.MessageRepository | ||
import com.wire.kalium.logic.functional.fold | ||
import com.wire.kalium.util.KaliumDispatcher | ||
import com.wire.kalium.util.KaliumDispatcherImpl | ||
import kotlinx.coroutines.withContext | ||
|
||
/** | ||
* Provides a way to get a messageId of next AudioMessage after [messageId] in [ConversationId] conversation. | ||
*/ | ||
class GetNextAudioMessageInConversationUseCase internal constructor( | ||
private val messageRepository: MessageRepository, | ||
private val dispatchers: KaliumDispatcher = KaliumDispatcherImpl | ||
) { | ||
suspend operator fun invoke( | ||
conversationId: ConversationId, | ||
messageId: String | ||
): Result = withContext(dispatchers.io) { | ||
messageRepository.getNextAudioMessageInConversation(conversationId, messageId) | ||
.fold({ Result.Failure(it) }, { Result.Success(it) }) | ||
} | ||
|
||
sealed interface Result { | ||
|
||
data class Success(val messageId: String) : Result | ||
|
||
/** | ||
* [StorageFailure.DataNotFound] in case there is no AudioMessage or some other generic error. | ||
*/ | ||
data class Failure(val cause: CoreFailure) : Result | ||
} | ||
} |
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
109 changes: 109 additions & 0 deletions
109
...lin/com/wire/kalium/logic/feature/message/GetNextAudioMessageInConversationUseCaseTest.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,109 @@ | ||
/* | ||
* 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.message | ||
|
||
import com.wire.kalium.logic.StorageFailure | ||
import com.wire.kalium.logic.data.id.ConversationId | ||
import com.wire.kalium.logic.data.message.Message | ||
import com.wire.kalium.logic.data.message.MessageRepository | ||
import com.wire.kalium.logic.framework.TestConversation | ||
import com.wire.kalium.logic.framework.TestMessage | ||
import com.wire.kalium.logic.functional.Either | ||
import com.wire.kalium.logic.test_util.TestKaliumDispatcher | ||
import com.wire.kalium.util.KaliumDispatcher | ||
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.assertEquals | ||
import kotlin.test.assertIs | ||
|
||
class GetNextAudioMessageInConversationUseCaseTest { | ||
|
||
private val testDispatchers: KaliumDispatcher = TestKaliumDispatcher | ||
|
||
@Test | ||
fun givenMessageAndConversationId_whenInvokingUseCase_thenShouldCallMessageRepository() = runTest(testDispatchers.io) { | ||
val (arrangement, getMessageByIdUseCase) = Arrangement() | ||
.withRepositoryMessageByIdReturning(CONVERSATION_ID, MESSAGE_ID, Either.Left(StorageFailure.DataNotFound)) | ||
.arrange() | ||
|
||
getMessageByIdUseCase(CONVERSATION_ID, MESSAGE_ID) | ||
|
||
coVerify { | ||
arrangement.messageRepository.getMessageById(CONVERSATION_ID, MESSAGE_ID) | ||
}.wasInvoked(exactly = once) | ||
} | ||
|
||
@Test | ||
fun givenRepositoryFails_whenInvokingUseCase_thenShouldPropagateTheFailure() = runTest(testDispatchers.io) { | ||
val cause = StorageFailure.DataNotFound | ||
val (_, getMessageByIdUseCase) = Arrangement() | ||
.withRepositoryMessageByIdReturning(CONVERSATION_ID, MESSAGE_ID, Either.Left(cause)) | ||
.arrange() | ||
|
||
val result = getMessageByIdUseCase(CONVERSATION_ID, MESSAGE_ID) | ||
|
||
assertIs<GetMessageByIdUseCase.Result.Failure>(result) | ||
assertEquals(cause, result.cause) | ||
} | ||
|
||
@Test | ||
fun givenRepositorySucceeds_whenInvokingUseCase_thenShouldPropagateTheSuccess() = runTest(testDispatchers.io) { | ||
val (_, getMessageByIdUseCase) = Arrangement() | ||
.withRepositoryMessageByIdReturning(CONVERSATION_ID, MESSAGE_ID, Either.Right(MESSAGE)) | ||
.arrange() | ||
|
||
val result = getMessageByIdUseCase(CONVERSATION_ID, MESSAGE_ID) | ||
|
||
assertIs<GetMessageByIdUseCase.Result.Success>(result) | ||
assertEquals(MESSAGE, result.message) | ||
} | ||
|
||
private inner class Arrangement { | ||
|
||
@Mock | ||
val messageRepository: MessageRepository = mock(MessageRepository::class) | ||
|
||
private val getMessageById by lazy { | ||
GetMessageByIdUseCase(messageRepository, testDispatchers) | ||
} | ||
|
||
suspend fun withRepositoryMessageByIdReturning( | ||
conversationId: ConversationId, | ||
messageId: String, | ||
response: Either<StorageFailure, Message> | ||
) = apply { | ||
coEvery { | ||
messageRepository.getMessageById(conversationId, messageId) | ||
}.returns(response) | ||
} | ||
|
||
fun arrange() = this to getMessageById | ||
} | ||
|
||
private companion object { | ||
const val MESSAGE_ID = TestMessage.TEST_MESSAGE_ID | ||
val MESSAGE = TestMessage.TEXT_MESSAGE | ||
val CONVERSATION_ID = TestConversation.ID | ||
} | ||
} |
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