From d33081c349df58642db4cca4ade932b130de210b Mon Sep 17 00:00:00 2001 From: ericdecanini Date: Thu, 3 Mar 2022 11:47:31 +0100 Subject: [PATCH] Refactors SessionParamsMapperTest by adding fake json adapters --- .../auth/db/SessionParamsMapperTest.kt | 28 +++++------ .../FakeCredentialsJsonAdapter.kt | 45 +++++++++++++++++ ...keHomeServerConnectionConfigJsonAdapter.kt | 47 ++++++++++++++++++ .../FakeSessionParamsMapperMoshi.kt | 48 ++++--------------- 4 files changed, 114 insertions(+), 54 deletions(-) create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/sessionparams/FakeCredentialsJsonAdapter.kt create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/sessionparams/FakeHomeServerConnectionConfigJsonAdapter.kt rename matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/{ => sessionparams}/FakeSessionParamsMapperMoshi.kt (59%) 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 26154e7d1a..e496201d08 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,14 +16,12 @@ package org.matrix.android.sdk.internal.auth.db -import io.mockk.every -import org.amshove.kluent.shouldBeNull import org.junit.Test -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 +import org.matrix.android.sdk.test.fakes.sessionparams.FakeSessionParamsMapperMoshi +import org.matrix.android.sdk.test.fakes.sessionparams.FakeSessionParamsMapperMoshi.Companion.nullSessionParams +import org.matrix.android.sdk.test.fakes.sessionparams.FakeSessionParamsMapperMoshi.Companion.nullSessionParamsEntity +import org.matrix.android.sdk.test.fakes.sessionparams.FakeSessionParamsMapperMoshi.Companion.sessionParams +import org.matrix.android.sdk.test.fakes.sessionparams.FakeSessionParamsMapperMoshi.Companion.sessionParamsEntity class SessionParamsMapperTest { @@ -47,8 +45,8 @@ class SessionParamsMapperTest { } @Test - fun `given null credentials, when mapping entity, then return null`() { - fakeMoshi.givenCredentialsFromJsonIsNull() + fun `given null credentials json deserialization, when mapping entity, then return null`() { + fakeMoshi.credentialsJsonAdapter.givenNullDeserialization() val output = sessionParamsMapper.map(sessionParamsEntity) @@ -56,8 +54,8 @@ class SessionParamsMapperTest { } @Test - fun `given null homeServerConnectionConfig, when mapping entity, then return null`() { - fakeMoshi.givenHomeServerConnectionConfigFromJsonIsNull() + fun `given null homeServerConnectionConfig json deserialization, when mapping entity, then return null`() { + fakeMoshi.homeServerConnectionConfigAdapter.givenNullDeserialization() val output = sessionParamsMapper.map(sessionParamsEntity) @@ -81,8 +79,8 @@ class SessionParamsMapperTest { } @Test - fun `given null credentials json, when mapping sessionParams, then return null`() { - fakeMoshi.givenCredentialsToJsonIsNull() + fun `given null credentials json serialization, when mapping sessionParams, then return null`() { + fakeMoshi.credentialsJsonAdapter.givenNullSerialization() val output = sessionParamsMapper.map(sessionParams) @@ -90,8 +88,8 @@ class SessionParamsMapperTest { } @Test - fun `given null homeServerConnectionConfig json, when mapping sessionParams, then return null`() { - fakeMoshi.givenHomeServerConnectionConfigToJsonIsNull() + fun `given null homeServerConnectionConfig json serialization, when mapping sessionParams, then return null`() { + fakeMoshi.homeServerConnectionConfigAdapter.givenNullSerialization() val output = sessionParamsMapper.map(sessionParams) diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/sessionparams/FakeCredentialsJsonAdapter.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/sessionparams/FakeCredentialsJsonAdapter.kt new file mode 100644 index 0000000000..a7c5281687 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/sessionparams/FakeCredentialsJsonAdapter.kt @@ -0,0 +1,45 @@ +/* + * 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.sessionparams + +import com.squareup.moshi.JsonAdapter +import io.mockk.every +import io.mockk.mockk +import org.matrix.android.sdk.api.auth.data.Credentials + +internal class FakeCredentialsJsonAdapter { + + val instance: JsonAdapter = mockk() + + init { + every { instance.fromJson(FakeSessionParamsMapperMoshi.sessionParamsEntity.credentialsJson) } returns credentials + every { instance.toJson(FakeSessionParamsMapperMoshi.sessionParams.credentials) } returns CREDENTIALS_JSON + } + + fun givenNullDeserialization() { + every { instance.fromJson(FakeSessionParamsMapperMoshi.sessionParamsEntity.credentialsJson) } returns null + } + + fun givenNullSerialization() { + every { instance.toJson(credentials) } returns null + } + + companion object { + val credentials: Credentials = mockk() + const val CREDENTIALS_JSON = "credentials_json" + } +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/sessionparams/FakeHomeServerConnectionConfigJsonAdapter.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/sessionparams/FakeHomeServerConnectionConfigJsonAdapter.kt new file mode 100644 index 0000000000..50af9bd9ea --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/sessionparams/FakeHomeServerConnectionConfigJsonAdapter.kt @@ -0,0 +1,47 @@ +/* + * 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.sessionparams + +import com.squareup.moshi.JsonAdapter +import io.mockk.every +import io.mockk.mockk +import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig +import org.matrix.android.sdk.test.fakes.sessionparams.FakeSessionParamsMapperMoshi.Companion.sessionParams +import org.matrix.android.sdk.test.fakes.sessionparams.FakeSessionParamsMapperMoshi.Companion.sessionParamsEntity + +internal class FakeHomeServerConnectionConfigJsonAdapter { + + val instance: JsonAdapter = mockk() + + init { + every { instance.fromJson(sessionParamsEntity.homeServerConnectionConfigJson) } returns homeServerConnectionConfig + every { instance.toJson(sessionParams.homeServerConnectionConfig) } returns HOME_SERVER_CONNECTION_CONFIG_JSON + } + + fun givenNullDeserialization() { + every { instance.fromJson(sessionParamsEntity.credentialsJson) } returns null + } + + fun givenNullSerialization() { + every { instance.toJson(homeServerConnectionConfig) } returns null + } + + companion object { + val homeServerConnectionConfig: HomeServerConnectionConfig = mockk() + const val HOME_SERVER_CONNECTION_CONFIG_JSON = "home_server_connection_config_json" + } +} 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/sessionparams/FakeSessionParamsMapperMoshi.kt similarity index 59% rename from matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeSessionParamsMapperMoshi.kt rename to matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/sessionparams/FakeSessionParamsMapperMoshi.kt index 2d5dc8222b..7dc0df94b6 100644 --- 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/sessionparams/FakeSessionParamsMapperMoshi.kt @@ -14,9 +14,8 @@ * limitations under the License. */ -package org.matrix.android.sdk.test.fakes +package org.matrix.android.sdk.test.fakes.sessionparams -import com.squareup.moshi.JsonAdapter import com.squareup.moshi.Moshi import io.mockk.every import io.mockk.mockk @@ -28,46 +27,22 @@ 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.fakes.sessionparams.FakeCredentialsJsonAdapter.Companion.CREDENTIALS_JSON +import org.matrix.android.sdk.test.fakes.sessionparams.FakeCredentialsJsonAdapter.Companion.credentials +import org.matrix.android.sdk.test.fakes.sessionparams.FakeHomeServerConnectionConfigJsonAdapter.Companion.HOME_SERVER_CONNECTION_CONFIG_JSON +import org.matrix.android.sdk.test.fakes.sessionparams.FakeHomeServerConnectionConfigJsonAdapter.Companion.homeServerConnectionConfig 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() + val credentialsJsonAdapter = FakeCredentialsJsonAdapter() + val homeServerConnectionConfigAdapter = FakeHomeServerConnectionConfigJsonAdapter() 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 + every { instance.adapter(Credentials::class.java) } returns credentialsJsonAdapter.instance + every { instance.adapter(HomeServerConnectionConfig::class.java) } returns homeServerConnectionConfigAdapter.instance } fun assertSessionParamsWasMappedSuccessfully(sessionParams: SessionParams?) { @@ -103,10 +78,5 @@ internal class FakeSessionParamsMapperMoshi { 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" } }