From 187502c358ba1358ee7950321338bf59197de8e5 Mon Sep 17 00:00:00 2001 From: ericdecanini Date: Thu, 3 Mar 2022 11:32:23 +0100 Subject: [PATCH] Refactors SessionParamsMapperTest by adding fake moshi --- .../auth/db/SessionParamsMapperTest.kt | 88 ++++---------- .../fakes/FakeSessionParamsMapperMoshi.kt | 112 ++++++++++++++++++ 2 files changed, 133 insertions(+), 67 deletions(-) create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeSessionParamsMapperMoshi.kt diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/db/SessionParamsMapperTest.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/db/SessionParamsMapperTest.kt index ac01441588..26154e7d1a 100644 --- a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/db/SessionParamsMapperTest.kt +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/db/SessionParamsMapperTest.kt @@ -16,80 +16,52 @@ package org.matrix.android.sdk.internal.auth.db -import com.squareup.moshi.JsonAdapter -import com.squareup.moshi.Moshi import io.mockk.every -import io.mockk.mockk -import org.amshove.kluent.shouldBeEqualTo import org.amshove.kluent.shouldBeNull -import org.junit.Before import org.junit.Test -import org.matrix.android.sdk.api.auth.data.Credentials -import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig -import org.matrix.android.sdk.api.auth.data.SessionParams -import org.matrix.android.sdk.api.auth.data.sessionId -import org.matrix.android.sdk.internal.auth.login.LoginType -import org.matrix.android.sdk.test.fixtures.SessionParamsEntityFixture.aSessionParamsEntity -import org.matrix.android.sdk.test.fixtures.SessionParamsFixture.aSessionParams +import org.matrix.android.sdk.test.fakes.FakeSessionParamsMapperMoshi +import org.matrix.android.sdk.test.fakes.FakeSessionParamsMapperMoshi.Companion.nullSessionParams +import org.matrix.android.sdk.test.fakes.FakeSessionParamsMapperMoshi.Companion.nullSessionParamsEntity +import org.matrix.android.sdk.test.fakes.FakeSessionParamsMapperMoshi.Companion.sessionParams +import org.matrix.android.sdk.test.fakes.FakeSessionParamsMapperMoshi.Companion.sessionParamsEntity class SessionParamsMapperTest { - private val moshi: Moshi = mockk() - private lateinit var sessionParamsMapper: SessionParamsMapper - - private val credentialsAdapter: JsonAdapter = mockk() - private val homeServerConnectionAdapter: JsonAdapter = mockk() - - @Before - fun setup() { - every { moshi.adapter(Credentials::class.java) } returns mockk() - every { moshi.adapter(HomeServerConnectionConfig::class.java) } returns mockk() - sessionParamsMapper = SessionParamsMapper(moshi) - - every { credentialsAdapter.fromJson(sessionParamsEntity.credentialsJson) } returns credentials - every { homeServerConnectionAdapter.fromJson(sessionParamsEntity.homeServerConnectionConfigJson) } returns homeServerConnectionConfig - every { credentialsAdapter.toJson(sessionParams.credentials) } returns CREDENTIALS_JSON - every { homeServerConnectionAdapter.toJson(sessionParams.homeServerConnectionConfig) } returns HOME_SERVER_CONNECTION_CONFIG_JSON - } + private val fakeMoshi = FakeSessionParamsMapperMoshi() + private val sessionParamsMapper = SessionParamsMapper(fakeMoshi.instance) @Test fun `when mapping entity, then map as SessionParams`() { - val output = sessionParamsMapper.map(sessionParamsEntity)!! + val output = sessionParamsMapper.map(sessionParamsEntity) - output shouldBeEqualTo SessionParams( - credentials, - homeServerConnectionConfig, - sessionParamsEntity.isTokenValid, - LoginType.fromValue(sessionParamsEntity.loginType) - ) + fakeMoshi.assertSessionParamsWasMappedSuccessfully(output) } @Test fun `given null input, when mapping entity, then return null`() { - val nullEntity: SessionParamsEntity? = null - val output = sessionParamsMapper.map(nullEntity) + val output = sessionParamsMapper.map(nullSessionParamsEntity) - output.shouldBeNull() + fakeMoshi.assertSessionParamsIsNull(output) } @Test fun `given null credentials, when mapping entity, then return null`() { - every { credentialsAdapter.fromJson(sessionParamsEntity.credentialsJson) } returns null + fakeMoshi.givenCredentialsFromJsonIsNull() val output = sessionParamsMapper.map(sessionParamsEntity) - output.shouldBeNull() + fakeMoshi.assertSessionParamsIsNull(output) } @Test fun `given null homeServerConnectionConfig, when mapping entity, then return null`() { - every { homeServerConnectionAdapter.fromJson(sessionParamsEntity.homeServerConnectionConfigJson) } returns null + fakeMoshi.givenHomeServerConnectionConfigFromJsonIsNull() val output = sessionParamsMapper.map(sessionParamsEntity) - output.shouldBeNull() + fakeMoshi.assertSessionParamsIsNull(output) } @Test @@ -97,50 +69,32 @@ class SessionParamsMapperTest { val output = sessionParamsMapper.map(sessionParams) - output shouldBeEqualTo SessionParamsEntity( - sessionParams.credentials.sessionId(), - sessionParams.userId, - CREDENTIALS_JSON, - HOME_SERVER_CONNECTION_CONFIG_JSON, - sessionParams.isTokenValid, - sessionParams.loginType.value, - ) + fakeMoshi.assertSessionParamsEntityWasMappedSuccessfully(output) } @Test fun `given null input, when mapping sessionParams, then return null`() { - val nullSessionParams: SessionParams? = null val output = sessionParamsMapper.map(nullSessionParams) - output.shouldBeNull() + fakeMoshi.assertSessionParamsEntityWasMappedSuccessfully(output) } @Test fun `given null credentials json, when mapping sessionParams, then return null`() { - every { credentialsAdapter.toJson(credentials) } returns null + fakeMoshi.givenCredentialsToJsonIsNull() val output = sessionParamsMapper.map(sessionParams) - output.shouldBeNull() + fakeMoshi.assertSessionParamsEntityIsNull(output) } @Test fun `given null homeServerConnectionConfig json, when mapping sessionParams, then return null`() { - every { homeServerConnectionAdapter.toJson(homeServerConnectionConfig) } returns null + fakeMoshi.givenHomeServerConnectionConfigToJsonIsNull() val output = sessionParamsMapper.map(sessionParams) - output.shouldBeNull() - } - - companion object { - private val sessionParamsEntity = aSessionParamsEntity() - private val sessionParams = aSessionParams() - - private val credentials: Credentials = mockk() - private val homeServerConnectionConfig: HomeServerConnectionConfig = mockk() - private const val CREDENTIALS_JSON = "credentials_json" - private const val HOME_SERVER_CONNECTION_CONFIG_JSON = "home_server_connection_config_json" + fakeMoshi.assertSessionParamsEntityIsNull(output) } } diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeSessionParamsMapperMoshi.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeSessionParamsMapperMoshi.kt new file mode 100644 index 0000000000..2d5dc8222b --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeSessionParamsMapperMoshi.kt @@ -0,0 +1,112 @@ +/* + * 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 org.matrix.android.sdk.test.fakes + +import com.squareup.moshi.JsonAdapter +import com.squareup.moshi.Moshi +import io.mockk.every +import io.mockk.mockk +import org.amshove.kluent.shouldBeEqualTo +import org.amshove.kluent.shouldBeNull +import org.matrix.android.sdk.api.auth.data.Credentials +import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig +import org.matrix.android.sdk.api.auth.data.SessionParams +import org.matrix.android.sdk.api.auth.data.sessionId +import org.matrix.android.sdk.internal.auth.db.SessionParamsEntity +import org.matrix.android.sdk.internal.auth.login.LoginType +import org.matrix.android.sdk.test.fixtures.SessionParamsEntityFixture.aSessionParamsEntity +import org.matrix.android.sdk.test.fixtures.SessionParamsFixture.aSessionParams + +internal class FakeSessionParamsMapperMoshi { + + val instance: Moshi = mockk() + private val credentialsAdapter: JsonAdapter = mockk() + private val homeServerConnectionConfigAdapter: JsonAdapter = mockk() + + init { + stubAdapters() + stubJsonConversions() + } + + private fun stubAdapters() { + every { instance.adapter(Credentials::class.java) } returns credentialsAdapter + every { instance.adapter(HomeServerConnectionConfig::class.java) } returns homeServerConnectionConfigAdapter + } + + private fun stubJsonConversions() { + every { credentialsAdapter.fromJson(sessionParamsEntity.credentialsJson) } returns credentials + every { homeServerConnectionConfigAdapter.fromJson(sessionParamsEntity.homeServerConnectionConfigJson) } returns homeServerConnectionConfig + every { credentialsAdapter.toJson(sessionParams.credentials) } returns CREDENTIALS_JSON + every { homeServerConnectionConfigAdapter.toJson(sessionParams.homeServerConnectionConfig) } returns HOME_SERVER_CONNECTION_CONFIG_JSON + } + + fun givenCredentialsFromJsonIsNull() { + every { credentialsAdapter.fromJson(sessionParamsEntity.credentialsJson) } returns null + } + + fun givenHomeServerConnectionConfigFromJsonIsNull() { + every { homeServerConnectionConfigAdapter.fromJson(sessionParamsEntity.homeServerConnectionConfigJson) } returns null + } + + fun givenCredentialsToJsonIsNull() { + every { credentialsAdapter.toJson(credentials) } returns null + } + + fun givenHomeServerConnectionConfigToJsonIsNull() { + every { homeServerConnectionConfigAdapter.toJson(homeServerConnectionConfig) } returns null + } + + fun assertSessionParamsWasMappedSuccessfully(sessionParams: SessionParams?) { + sessionParams shouldBeEqualTo SessionParams( + credentials, + homeServerConnectionConfig, + sessionParamsEntity.isTokenValid, + LoginType.fromValue(sessionParamsEntity.loginType) + ) + } + + fun assertSessionParamsIsNull(sessionParams: SessionParams?) { + sessionParams.shouldBeNull() + } + + fun assertSessionParamsEntityWasMappedSuccessfully(sessionParamsEntity: SessionParamsEntity?) { + sessionParamsEntity shouldBeEqualTo SessionParamsEntity( + sessionParams.credentials.sessionId(), + sessionParams.userId, + CREDENTIALS_JSON, + HOME_SERVER_CONNECTION_CONFIG_JSON, + sessionParams.isTokenValid, + sessionParams.loginType.value, + ) + } + + fun assertSessionParamsEntityIsNull(sessionParamsEntity: SessionParamsEntity?) { + sessionParamsEntity.shouldBeNull() + } + + companion object { + val sessionParams = aSessionParams() + val sessionParamsEntity = aSessionParamsEntity() + val nullSessionParams: SessionParams? = null + val nullSessionParamsEntity: SessionParamsEntity? = null + + private val credentials: Credentials = mockk() + private val homeServerConnectionConfig: HomeServerConnectionConfig = mockk() + private const val CREDENTIALS_JSON = "credentials_json" + private const val HOME_SERVER_CONNECTION_CONFIG_JSON = "home_server_connection_config_json" + } +}