From ce70ab8f0fde247da94873a6cd27aeacde2f2e52 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Mon, 3 Oct 2022 10:27:42 +0200 Subject: [PATCH] Adding unit tests for update client info use case --- .../v2/details/extended/NoDeviceIdError.kt | 19 +++ .../extended/SetMatrixClientInfoUseCase.kt | 2 +- .../extended/UpdateMatrixClientInfoUseCase.kt | 13 +- .../SetMatrixClientInfoUseCaseTest.kt | 2 +- .../UpdateMatrixClientInfoUseCaseTest.kt | 149 ++++++++++++++++++ 5 files changed, 177 insertions(+), 8 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/NoDeviceIdError.kt create mode 100644 vector/src/test/java/im/vector/app/features/settings/devices/v2/details/extended/UpdateMatrixClientInfoUseCaseTest.kt diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/NoDeviceIdError.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/NoDeviceIdError.kt new file mode 100644 index 0000000000..c021a6703a --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/NoDeviceIdError.kt @@ -0,0 +1,19 @@ +/* + * 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.features.settings.devices.v2.details.extended + +class NoDeviceIdError : IllegalStateException("device id is empty") diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/SetMatrixClientInfoUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/SetMatrixClientInfoUseCase.kt index ccc73e896b..b71e447ace 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/SetMatrixClientInfoUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/SetMatrixClientInfoUseCase.kt @@ -37,7 +37,7 @@ class SetMatrixClientInfoUseCase @Inject constructor( session.accountDataService() .updateUserAccountData(type, clientInfo.toContent()) } else { - throw IllegalStateException("device id is empty") + throw NoDeviceIdError() } } } diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/UpdateMatrixClientInfoUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/UpdateMatrixClientInfoUseCase.kt index f19503dcc5..c2c75e0409 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/UpdateMatrixClientInfoUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/UpdateMatrixClientInfoUseCase.kt @@ -33,18 +33,19 @@ class UpdateMatrixClientInfoUseCase @Inject constructor( ) { // TODO call the use case after signin + on app startup - // TODO add unit tests suspend fun execute(): Result = runCatching { val clientInfo = MatrixClientInfoContent( name = appNameProvider.getAppName(), version = buildMeta.versionName ) val deviceId = activeSessionHolder.getActiveSession().sessionParams.deviceId.orEmpty() - val storedClientInfo = deviceId - .takeUnless { it.isEmpty() } - ?.let { getMatrixClientInfoUseCase.execute(it) } - if (clientInfo != storedClientInfo) { - setMatrixClientInfoUseCase.execute(clientInfo) + if (deviceId.isNotEmpty()) { + val storedClientInfo = getMatrixClientInfoUseCase.execute(deviceId) + if (clientInfo != storedClientInfo) { + setMatrixClientInfoUseCase.execute(clientInfo) + } + } else { + throw NoDeviceIdError() } } } diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/details/extended/SetMatrixClientInfoUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/details/extended/SetMatrixClientInfoUseCaseTest.kt index 9e04d6984e..c7c8f562b1 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/details/extended/SetMatrixClientInfoUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/details/extended/SetMatrixClientInfoUseCaseTest.kt @@ -95,7 +95,7 @@ class SetMatrixClientInfoUseCaseTest { // Then result.isFailure shouldBe true - result.exceptionOrNull() shouldBeInstanceOf IllegalStateException::class + result.exceptionOrNull() shouldBeInstanceOf NoDeviceIdError::class fakeActiveSessionHolder.fakeSession .fakeSessionAccountDataService .verifyUpdateUserAccountDataEventSucceeds(type, content, inverse = true) diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/details/extended/UpdateMatrixClientInfoUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/details/extended/UpdateMatrixClientInfoUseCaseTest.kt new file mode 100644 index 0000000000..2151b15d27 --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/details/extended/UpdateMatrixClientInfoUseCaseTest.kt @@ -0,0 +1,149 @@ +/* + * 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.features.settings.devices.v2.details.extended + +import im.vector.app.core.resources.BuildMeta +import im.vector.app.test.fakes.FakeActiveSessionHolder +import im.vector.app.test.fakes.FakeAppNameProvider +import io.mockk.coEvery +import io.mockk.coVerify +import io.mockk.every +import io.mockk.mockk +import kotlinx.coroutines.test.runTest +import org.amshove.kluent.shouldBe +import org.amshove.kluent.shouldBeEqualTo +import org.amshove.kluent.shouldBeInstanceOf +import org.junit.Test + +private const val AN_APP_NAME_1 = "app_name_1" +private const val AN_APP_NAME_2 = "app_name_2" +private const val A_VERSION_NAME_1 = "version_name_1" +private const val A_VERSION_NAME_2 = "version_name_2" +private const val A_SESSION_ID = "session-id" + +class UpdateMatrixClientInfoUseCaseTest { + + private val fakeActiveSessionHolder = FakeActiveSessionHolder() + private val fakeAppNameProvider = FakeAppNameProvider() + private val fakeBuildMeta = mockk() + private val getMatrixClientInfoUseCase = mockk() + private val setMatrixClientInfoUseCase = mockk() + + private val updateMatrixClientInfoUseCase = UpdateMatrixClientInfoUseCase( + activeSessionHolder = fakeActiveSessionHolder.instance, + appNameProvider = fakeAppNameProvider, + buildMeta = fakeBuildMeta, + getMatrixClientInfoUseCase = getMatrixClientInfoUseCase, + setMatrixClientInfoUseCase = setMatrixClientInfoUseCase, + ) + + @Test + fun `given current client info is different than the stored one when trying to update then new client info is set and result is success`() = runTest { + // Given + givenCurrentAppName(AN_APP_NAME_1) + givenCurrentVersionName(A_VERSION_NAME_1) + givenStoredClientInfo(AN_APP_NAME_2, A_VERSION_NAME_2) + givenSetClientInfoSucceeds() + val expectedClientInfoToSet = MatrixClientInfoContent( + name = AN_APP_NAME_1, + version = A_VERSION_NAME_1, + ) + + // When + val result = updateMatrixClientInfoUseCase.execute() + + // Then + result.isSuccess shouldBe true + coVerify { setMatrixClientInfoUseCase.execute(match { it == expectedClientInfoToSet }) } + } + + @Test + fun `given current client info is equal to the stored one when trying to update then nothing is done and result is success`() = runTest { + // Given + givenCurrentAppName(AN_APP_NAME_1) + givenCurrentVersionName(A_VERSION_NAME_1) + givenStoredClientInfo(AN_APP_NAME_1, A_VERSION_NAME_1) + + // When + val result = updateMatrixClientInfoUseCase.execute() + + // Then + result.isSuccess shouldBe true + coVerify(inverse = true) { setMatrixClientInfoUseCase.execute(any()) } + } + + @Test + fun `given error during setting new client info when trying to update then result is failure`() = runTest { + // Given + givenCurrentAppName(AN_APP_NAME_1) + givenCurrentVersionName(A_VERSION_NAME_1) + givenStoredClientInfo(AN_APP_NAME_2, A_VERSION_NAME_2) + val error = Exception() + givenSetClientInfoFailsWithError(error) + val expectedClientInfoToSet = MatrixClientInfoContent( + name = AN_APP_NAME_1, + version = A_VERSION_NAME_1, + ) + + // When + val result = updateMatrixClientInfoUseCase.execute() + + // Then + result.isFailure shouldBe true + result.exceptionOrNull() shouldBeEqualTo error + coVerify { setMatrixClientInfoUseCase.execute(match { it == expectedClientInfoToSet }) } + } + + @Test + fun `given no session id for current session when trying to update then result is failure`() = runTest { + // Given + givenCurrentAppName(AN_APP_NAME_1) + givenCurrentVersionName(A_VERSION_NAME_1) + fakeActiveSessionHolder.fakeSession.givenSessionId(null) + + // When + val result = updateMatrixClientInfoUseCase.execute() + + // Then + result.isFailure shouldBe true + result.exceptionOrNull() shouldBeInstanceOf NoDeviceIdError::class + } + + private fun givenCurrentAppName(appName: String) { + fakeAppNameProvider.givenAppName(appName) + } + + private fun givenCurrentVersionName(versionName: String) { + every { fakeBuildMeta.versionName } returns versionName + } + + private fun givenStoredClientInfo(appName: String, versionName: String) { + fakeActiveSessionHolder.fakeSession.givenSessionId(A_SESSION_ID) + every { getMatrixClientInfoUseCase.execute(A_SESSION_ID) } returns MatrixClientInfoContent( + name = appName, + version = versionName, + ) + } + + private fun givenSetClientInfoSucceeds() { + coEvery { setMatrixClientInfoUseCase.execute(any()) } returns Result.success(Unit) + } + + private fun givenSetClientInfoFailsWithError(error: Exception) { + coEvery { setMatrixClientInfoUseCase.execute(any()) } throws error + } +}