Updating unit tests

This commit is contained in:
Maxime NATUREL 2022-10-03 16:02:56 +02:00
parent c0e9d5124c
commit 34e37ea608
3 changed files with 53 additions and 83 deletions

View file

@ -16,7 +16,7 @@
package im.vector.app.core.session.clientinfo package im.vector.app.core.session.clientinfo
import im.vector.app.test.fakes.FakeActiveSessionHolder import im.vector.app.test.fakes.FakeSession
import org.amshove.kluent.shouldBe import org.amshove.kluent.shouldBe
import org.amshove.kluent.shouldBeEqualTo import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test import org.junit.Test
@ -28,11 +28,9 @@ private const val A_CLIENT_URL = "client-url"
class GetMatrixClientInfoUseCaseTest { class GetMatrixClientInfoUseCaseTest {
private val fakeActiveSessionHolder = FakeActiveSessionHolder() private val fakeSession = FakeSession()
private val getMatrixClientInfoUseCase = GetMatrixClientInfoUseCase( private val getMatrixClientInfoUseCase = GetMatrixClientInfoUseCase()
activeSessionHolder = fakeActiveSessionHolder.instance
)
@Test @Test
fun `given a device id and existing content when getting the info then result should contain that info`() { fun `given a device id and existing content when getting the info then result should contain that info`() {
@ -45,24 +43,12 @@ class GetMatrixClientInfoUseCaseTest {
) )
// When // When
val result = getMatrixClientInfoUseCase.execute(A_DEVICE_ID) val result = getMatrixClientInfoUseCase.execute(fakeSession, A_DEVICE_ID)
// Then // Then
result shouldBeEqualTo expectedClientInfo result shouldBeEqualTo expectedClientInfo
} }
@Test
fun `given no active session when getting the info then result should be null`() {
// Given
fakeActiveSessionHolder.givenGetSafeActiveSessionReturns(null)
// When
val result = getMatrixClientInfoUseCase.execute(A_DEVICE_ID)
// Then
result shouldBe null
}
private fun givenClientInfoContent(deviceId: String) { private fun givenClientInfoContent(deviceId: String) {
val type = MATRIX_CLIENT_INFO_KEY_PREFIX + deviceId val type = MATRIX_CLIENT_INFO_KEY_PREFIX + deviceId
val content = mapOf( val content = mapOf(
@ -70,7 +56,7 @@ class GetMatrixClientInfoUseCaseTest {
Pair("version", A_CLIENT_VERSION), Pair("version", A_CLIENT_VERSION),
Pair("url", A_CLIENT_URL), Pair("url", A_CLIENT_URL),
) )
fakeActiveSessionHolder.fakeSession fakeSession
.fakeSessionAccountDataService .fakeSessionAccountDataService
.givenGetUserAccountDataEventReturns(type, content) .givenGetUserAccountDataEventReturns(type, content)
} }

View file

@ -16,7 +16,7 @@
package im.vector.app.core.session.clientinfo package im.vector.app.core.session.clientinfo
import im.vector.app.test.fakes.FakeActiveSessionHolder import im.vector.app.test.fakes.FakeSession
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBe import org.amshove.kluent.shouldBe
import org.amshove.kluent.shouldBeEqualTo import org.amshove.kluent.shouldBeEqualTo
@ -28,11 +28,9 @@ private const val A_DEVICE_ID = "device-id"
class SetMatrixClientInfoUseCaseTest { class SetMatrixClientInfoUseCaseTest {
private val fakeActiveSessionHolder = FakeActiveSessionHolder() private val fakeSession = FakeSession()
private val setMatrixClientInfoUseCase = SetMatrixClientInfoUseCase( private val setMatrixClientInfoUseCase = SetMatrixClientInfoUseCase()
activeSessionHolder = fakeActiveSessionHolder.instance
)
@Test @Test
fun `given client info and no error when setting the info then account data is correctly updated`() = runTest { fun `given client info and no error when setting the info then account data is correctly updated`() = runTest {
@ -40,18 +38,18 @@ class SetMatrixClientInfoUseCaseTest {
val type = MATRIX_CLIENT_INFO_KEY_PREFIX + A_DEVICE_ID val type = MATRIX_CLIENT_INFO_KEY_PREFIX + A_DEVICE_ID
val clientInfo = givenClientInfo() val clientInfo = givenClientInfo()
val content = clientInfo.toContent() val content = clientInfo.toContent()
fakeActiveSessionHolder.fakeSession fakeSession
.givenSessionId(A_DEVICE_ID) .givenSessionId(A_DEVICE_ID)
fakeActiveSessionHolder.fakeSession fakeSession
.fakeSessionAccountDataService .fakeSessionAccountDataService
.givenUpdateUserAccountDataEventSucceeds() .givenUpdateUserAccountDataEventSucceeds()
// When // When
val result = setMatrixClientInfoUseCase.execute(clientInfo) val result = setMatrixClientInfoUseCase.execute(fakeSession, clientInfo)
// Then // Then
result.isSuccess shouldBe true result.isSuccess shouldBe true
fakeActiveSessionHolder.fakeSession fakeSession
.fakeSessionAccountDataService .fakeSessionAccountDataService
.verifyUpdateUserAccountDataEventSucceeds(type, content) .verifyUpdateUserAccountDataEventSucceeds(type, content)
} }
@ -62,20 +60,20 @@ class SetMatrixClientInfoUseCaseTest {
val type = MATRIX_CLIENT_INFO_KEY_PREFIX + A_DEVICE_ID val type = MATRIX_CLIENT_INFO_KEY_PREFIX + A_DEVICE_ID
val clientInfo = givenClientInfo() val clientInfo = givenClientInfo()
val content = clientInfo.toContent() val content = clientInfo.toContent()
fakeActiveSessionHolder.fakeSession fakeSession
.givenSessionId(A_DEVICE_ID) .givenSessionId(A_DEVICE_ID)
val error = Exception() val error = Exception()
fakeActiveSessionHolder.fakeSession fakeSession
.fakeSessionAccountDataService .fakeSessionAccountDataService
.givenUpdateUserAccountDataEventFailsWithError(error) .givenUpdateUserAccountDataEventFailsWithError(error)
// When // When
val result = setMatrixClientInfoUseCase.execute(clientInfo) val result = setMatrixClientInfoUseCase.execute(fakeSession, clientInfo)
// Then // Then
result.isFailure shouldBe true result.isFailure shouldBe true
result.exceptionOrNull() shouldBeEqualTo error result.exceptionOrNull() shouldBeEqualTo error
fakeActiveSessionHolder.fakeSession fakeSession
.fakeSessionAccountDataService .fakeSessionAccountDataService
.verifyUpdateUserAccountDataEventSucceeds(type, content) .verifyUpdateUserAccountDataEventSucceeds(type, content)
} }
@ -86,16 +84,16 @@ class SetMatrixClientInfoUseCaseTest {
val type = MATRIX_CLIENT_INFO_KEY_PREFIX + A_DEVICE_ID val type = MATRIX_CLIENT_INFO_KEY_PREFIX + A_DEVICE_ID
val clientInfo = givenClientInfo() val clientInfo = givenClientInfo()
val content = clientInfo.toContent() val content = clientInfo.toContent()
fakeActiveSessionHolder.fakeSession fakeSession
.givenSessionId(null) .givenSessionId(null)
// When // When
val result = setMatrixClientInfoUseCase.execute(clientInfo) val result = setMatrixClientInfoUseCase.execute(fakeSession, clientInfo)
// Then // Then
result.isFailure shouldBe true result.isFailure shouldBe true
result.exceptionOrNull() shouldBeInstanceOf NoDeviceIdError::class result.exceptionOrNull() shouldBeInstanceOf NoDeviceIdError::class
fakeActiveSessionHolder.fakeSession fakeSession
.fakeSessionAccountDataService .fakeSessionAccountDataService
.verifyUpdateUserAccountDataEventSucceeds(type, content, inverse = true) .verifyUpdateUserAccountDataEventSucceeds(type, content, inverse = true)
} }

View file

@ -16,17 +16,23 @@
package im.vector.app.core.session.clientinfo package im.vector.app.core.session.clientinfo
import android.os.SystemClock
import im.vector.app.core.resources.BuildMeta import im.vector.app.core.resources.BuildMeta
import im.vector.app.test.fakes.FakeActiveSessionHolder
import im.vector.app.test.fakes.FakeAppNameProvider import im.vector.app.test.fakes.FakeAppNameProvider
import im.vector.app.test.fakes.FakeSession
import im.vector.app.test.testDispatcher
import io.mockk.coEvery import io.mockk.coEvery
import io.mockk.coVerify import io.mockk.coVerify
import io.mockk.every import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBe import kotlinx.coroutines.test.setMain
import org.amshove.kluent.shouldBeEqualTo import org.junit.After
import org.amshove.kluent.shouldBeInstanceOf import org.junit.Before
import org.junit.Test import org.junit.Test
private const val AN_APP_NAME_1 = "app_name_1" private const val AN_APP_NAME_1 = "app_name_1"
@ -37,22 +43,31 @@ private const val A_SESSION_ID = "session-id"
class UpdateMatrixClientInfoUseCaseTest { class UpdateMatrixClientInfoUseCaseTest {
private val fakeActiveSessionHolder = FakeActiveSessionHolder() private val fakeSession = FakeSession()
private val fakeAppNameProvider = FakeAppNameProvider() private val fakeAppNameProvider = FakeAppNameProvider()
private val fakeBuildMeta = mockk<BuildMeta>() private val fakeBuildMeta = mockk<BuildMeta>()
private val getMatrixClientInfoUseCase = mockk<GetMatrixClientInfoUseCase>() private val getMatrixClientInfoUseCase = mockk<GetMatrixClientInfoUseCase>()
private val setMatrixClientInfoUseCase = mockk<SetMatrixClientInfoUseCase>() private val setMatrixClientInfoUseCase = mockk<SetMatrixClientInfoUseCase>()
private val updateMatrixClientInfoUseCase = UpdateMatrixClientInfoUseCase( private val updateMatrixClientInfoUseCase = UpdateMatrixClientInfoUseCase(
activeSessionHolder = fakeActiveSessionHolder.instance,
appNameProvider = fakeAppNameProvider, appNameProvider = fakeAppNameProvider,
buildMeta = fakeBuildMeta, buildMeta = fakeBuildMeta,
getMatrixClientInfoUseCase = getMatrixClientInfoUseCase, getMatrixClientInfoUseCase = getMatrixClientInfoUseCase,
setMatrixClientInfoUseCase = setMatrixClientInfoUseCase, setMatrixClientInfoUseCase = setMatrixClientInfoUseCase,
) )
@Before
fun setup() {
Dispatchers.setMain(testDispatcher)
}
@After
fun tearDown() {
Dispatchers.resetMain()
}
@Test @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 { fun `given current client info is different than the stored one when trying to update then new client info is set`() = runTest {
// Given // Given
givenCurrentAppName(AN_APP_NAME_1) givenCurrentAppName(AN_APP_NAME_1)
givenCurrentVersionName(A_VERSION_NAME_1) givenCurrentVersionName(A_VERSION_NAME_1)
@ -64,63 +79,38 @@ class UpdateMatrixClientInfoUseCaseTest {
) )
// When // When
val result = updateMatrixClientInfoUseCase.execute() updateMatrixClientInfoUseCase.execute(fakeSession)
// Then // Then
result.isSuccess shouldBe true coVerify { setMatrixClientInfoUseCase.execute(fakeSession, match { it == expectedClientInfoToSet }) }
coVerify { setMatrixClientInfoUseCase.execute(match { it == expectedClientInfoToSet }) }
} }
@Test @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 { fun `given current client info is equal to the stored one when trying to update then nothing is done`() = runTest {
// Given // Given
givenCurrentAppName(AN_APP_NAME_1) givenCurrentAppName(AN_APP_NAME_1)
givenCurrentVersionName(A_VERSION_NAME_1) givenCurrentVersionName(A_VERSION_NAME_1)
givenStoredClientInfo(AN_APP_NAME_1, A_VERSION_NAME_1) givenStoredClientInfo(AN_APP_NAME_1, A_VERSION_NAME_1)
// When // When
val result = updateMatrixClientInfoUseCase.execute() updateMatrixClientInfoUseCase.execute(fakeSession)
// Then // Then
result.isSuccess shouldBe true coVerify(inverse = true) { setMatrixClientInfoUseCase.execute(fakeSession, any()) }
coVerify(inverse = true) { setMatrixClientInfoUseCase.execute(any()) }
} }
@Test @Test
fun `given error during setting new client info when trying to update then result is failure`() = runTest { fun `given no session id for current session when trying to update then nothing is done`() = runTest {
// Given // Given
givenCurrentAppName(AN_APP_NAME_1) givenCurrentAppName(AN_APP_NAME_1)
givenCurrentVersionName(A_VERSION_NAME_1) givenCurrentVersionName(A_VERSION_NAME_1)
givenStoredClientInfo(AN_APP_NAME_2, A_VERSION_NAME_2) fakeSession.givenSessionId(null)
val error = Exception()
givenSetClientInfoFailsWithError(error)
val expectedClientInfoToSet = MatrixClientInfoContent(
name = AN_APP_NAME_1,
version = A_VERSION_NAME_1,
)
// When // When
val result = updateMatrixClientInfoUseCase.execute() updateMatrixClientInfoUseCase.execute(fakeSession)
// Then // Then
result.isFailure shouldBe true coVerify(inverse = true) { setMatrixClientInfoUseCase.execute(fakeSession, any()) }
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) { private fun givenCurrentAppName(appName: String) {
@ -132,18 +122,14 @@ class UpdateMatrixClientInfoUseCaseTest {
} }
private fun givenStoredClientInfo(appName: String, versionName: String) { private fun givenStoredClientInfo(appName: String, versionName: String) {
fakeActiveSessionHolder.fakeSession.givenSessionId(A_SESSION_ID) fakeSession.givenSessionId(A_SESSION_ID)
every { getMatrixClientInfoUseCase.execute(A_SESSION_ID) } returns MatrixClientInfoContent( every { getMatrixClientInfoUseCase.execute(fakeSession, A_SESSION_ID) } returns MatrixClientInfoContent(
name = appName, name = appName,
version = versionName, version = versionName,
) )
} }
private fun givenSetClientInfoSucceeds() { private fun givenSetClientInfoSucceeds() {
coEvery { setMatrixClientInfoUseCase.execute(any()) } returns Result.success(Unit) coEvery { setMatrixClientInfoUseCase.execute(any(), any()) } returns Result.success(Unit)
}
private fun givenSetClientInfoFailsWithError(error: Exception) {
coEvery { setMatrixClientInfoUseCase.execute(any()) } throws error
} }
} }