From 0c7f04c4a1cf971b1abe6c53caeea3920ab2409c Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Wed, 12 Oct 2022 17:23:30 +0200 Subject: [PATCH] Adding unit tests for DeleteMatrixClientInfoUseCase --- .../DeleteMatrixClientInfoUseCase.kt | 3 +- .../DeleteMatrixClientInfoUseCaseTest.kt | 82 +++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 vector/src/test/java/im/vector/app/core/session/clientinfo/DeleteMatrixClientInfoUseCaseTest.kt diff --git a/vector/src/main/java/im/vector/app/core/session/clientinfo/DeleteMatrixClientInfoUseCase.kt b/vector/src/main/java/im/vector/app/core/session/clientinfo/DeleteMatrixClientInfoUseCase.kt index 3957c81997..bab3af9af5 100644 --- a/vector/src/main/java/im/vector/app/core/session/clientinfo/DeleteMatrixClientInfoUseCase.kt +++ b/vector/src/main/java/im/vector/app/core/session/clientinfo/DeleteMatrixClientInfoUseCase.kt @@ -28,7 +28,6 @@ class DeleteMatrixClientInfoUseCase @Inject constructor( private val setMatrixClientInfoUseCase: SetMatrixClientInfoUseCase, ) { - // TODO add unit tests suspend fun execute(): Result = runCatching { Timber.d("deleting recorded client info") val session = activeSessionHolder.getActiveSession() @@ -37,6 +36,6 @@ class DeleteMatrixClientInfoUseCase @Inject constructor( version = "", url = "", ) - setMatrixClientInfoUseCase.execute(session, emptyClientInfo) + return setMatrixClientInfoUseCase.execute(session, emptyClientInfo) } } diff --git a/vector/src/test/java/im/vector/app/core/session/clientinfo/DeleteMatrixClientInfoUseCaseTest.kt b/vector/src/test/java/im/vector/app/core/session/clientinfo/DeleteMatrixClientInfoUseCaseTest.kt new file mode 100644 index 0000000000..c89bd73979 --- /dev/null +++ b/vector/src/test/java/im/vector/app/core/session/clientinfo/DeleteMatrixClientInfoUseCaseTest.kt @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.core.session.clientinfo + +import im.vector.app.test.fakes.FakeActiveSessionHolder +import io.mockk.coEvery +import io.mockk.coVerify +import io.mockk.mockk +import kotlinx.coroutines.test.runTest +import org.amshove.kluent.shouldBe +import org.amshove.kluent.shouldBeEqualTo +import org.junit.Test + +class DeleteMatrixClientInfoUseCaseTest { + + private val fakeActiveSessionHolder = FakeActiveSessionHolder() + private val fakeSetMatrixClientInfoUseCase = mockk() + + private val deleteMatrixClientInfoUseCase = DeleteMatrixClientInfoUseCase( + activeSessionHolder = fakeActiveSessionHolder.instance, + setMatrixClientInfoUseCase = fakeSetMatrixClientInfoUseCase + ) + + @Test + fun `given current session when calling use case then empty client info is set and result is success`() = runTest { + // Given + givenSetMatrixClientInfoSucceeds() + val expectedClientInfoToBeSet = MatrixClientInfoContent( + name = "", + version = "", + url = "", + ) + + // When + val result = deleteMatrixClientInfoUseCase.execute() + + // Then + result.isSuccess shouldBe true + coVerify { + fakeSetMatrixClientInfoUseCase.execute( + fakeActiveSessionHolder.fakeSession, + expectedClientInfoToBeSet + ) + } + } + + @Test + fun `given current session and error during the process when calling use case then result is failure`() = runTest { + // Given + val error = Exception() + givenSetMatrixClientInfoFails(error) + + // When + val result = deleteMatrixClientInfoUseCase.execute() + + // Then + result.isFailure shouldBe true + result.exceptionOrNull() shouldBeEqualTo error + } + + private fun givenSetMatrixClientInfoSucceeds() { + coEvery { fakeSetMatrixClientInfoUseCase.execute(any(), any()) } returns Result.success(Unit) + } + + private fun givenSetMatrixClientInfoFails(error: Exception) { + coEvery { fakeSetMatrixClientInfoUseCase.execute(any(), any()) } returns Result.failure(error) + } +}