From 25e73e5bd083cd7c621740c4592fa1906bddada9 Mon Sep 17 00:00:00 2001 From: ericdecanini Date: Thu, 3 Mar 2022 10:56:07 +0100 Subject: [PATCH] Adds SessionParamsMapper tests --- .../auth/db/SessionParamsMapperTest.kt | 146 ++++++++++++++++++ .../sdk/test/fixtures/CredentialsFixture.kt | 38 +++++ .../fixtures/SessionParamsEntityFixture.kt | 37 +++++ .../sdk/test/fixtures/SessionParamsFixture.kt | 37 +++++ 4 files changed, 258 insertions(+) create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/db/SessionParamsMapperTest.kt create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/CredentialsFixture.kt create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/SessionParamsEntityFixture.kt create mode 100644 matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/SessionParamsFixture.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 new file mode 100644 index 0000000000..ac01441588 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/auth/db/SessionParamsMapperTest.kt @@ -0,0 +1,146 @@ +/* + * 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.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 + +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 + } + + @Test + fun `when mapping entity, then map as SessionParams`() { + + val output = sessionParamsMapper.map(sessionParamsEntity)!! + + output shouldBeEqualTo SessionParams( + credentials, + homeServerConnectionConfig, + sessionParamsEntity.isTokenValid, + LoginType.fromValue(sessionParamsEntity.loginType) + ) + } + + @Test + fun `given null input, when mapping entity, then return null`() { + val nullEntity: SessionParamsEntity? = null + + val output = sessionParamsMapper.map(nullEntity) + + output.shouldBeNull() + } + + @Test + fun `given null credentials, when mapping entity, then return null`() { + every { credentialsAdapter.fromJson(sessionParamsEntity.credentialsJson) } returns null + + val output = sessionParamsMapper.map(sessionParamsEntity) + + output.shouldBeNull() + } + + @Test + fun `given null homeServerConnectionConfig, when mapping entity, then return null`() { + every { homeServerConnectionAdapter.fromJson(sessionParamsEntity.homeServerConnectionConfigJson) } returns null + + val output = sessionParamsMapper.map(sessionParamsEntity) + + output.shouldBeNull() + } + + @Test + fun `when mapping sessionParams, then map as SessionParamsEntity`() { + + 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, + ) + } + + @Test + fun `given null input, when mapping sessionParams, then return null`() { + val nullSessionParams: SessionParams? = null + + val output = sessionParamsMapper.map(nullSessionParams) + + output.shouldBeNull() + } + + @Test + fun `given null credentials json, when mapping sessionParams, then return null`() { + every { credentialsAdapter.toJson(credentials) } returns null + + val output = sessionParamsMapper.map(sessionParams) + + output.shouldBeNull() + } + + @Test + fun `given null homeServerConnectionConfig json, when mapping sessionParams, then return null`() { + every { homeServerConnectionAdapter.toJson(homeServerConnectionConfig) } returns null + + 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" + } +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/CredentialsFixture.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/CredentialsFixture.kt new file mode 100644 index 0000000000..5002fe9153 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/CredentialsFixture.kt @@ -0,0 +1,38 @@ +/* + * 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.fixtures + +import org.matrix.android.sdk.api.auth.data.Credentials +import org.matrix.android.sdk.api.auth.data.DiscoveryInformation + +object CredentialsFixture { + fun aCredentials( + userId: String = "", + accessToken: String = "", + refreshToken: String? = null, + homeServer: String? = null, + deviceId: String? = null, + discoveryInformation: DiscoveryInformation? = null, + ) = Credentials( + userId, + accessToken, + refreshToken, + homeServer, + deviceId, + discoveryInformation, + ) +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/SessionParamsEntityFixture.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/SessionParamsEntityFixture.kt new file mode 100644 index 0000000000..29b5b1a7c9 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/SessionParamsEntityFixture.kt @@ -0,0 +1,37 @@ +/* + * 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.fixtures + +import org.matrix.android.sdk.internal.auth.db.SessionParamsEntity + +internal object SessionParamsEntityFixture { + fun aSessionParamsEntity( + sessionId: String = "", + userId: String = "", + credentialsJson: String = "", + homeServerConnectionConfigJson: String = "", + isTokenValid: Boolean = true, + loginType: String = "", + ) = SessionParamsEntity( + sessionId, + userId, + credentialsJson, + homeServerConnectionConfigJson, + isTokenValid, + loginType, + ) +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/SessionParamsFixture.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/SessionParamsFixture.kt new file mode 100644 index 0000000000..387727e8f0 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/SessionParamsFixture.kt @@ -0,0 +1,37 @@ +/* + * 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.fixtures + +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.internal.auth.login.LoginType +import org.matrix.android.sdk.test.fixtures.CredentialsFixture.aCredentials + +object SessionParamsFixture { + fun aSessionParams( + credentials: Credentials = aCredentials(), + homeServerConnectionConfig: HomeServerConnectionConfig = HomeServerConnectionConfig.Builder().build(), + isTokenValid: Boolean = false, + loginType: LoginType = LoginType.UNKNOWN, + ) = SessionParams( + credentials, + homeServerConnectionConfig, + isTokenValid, + loginType, + ) +}