Refactors SessionParamsMapperTest by adding fake json adapters

This commit is contained in:
ericdecanini 2022-03-03 11:47:31 +01:00
parent 187502c358
commit d33081c349
4 changed files with 114 additions and 54 deletions

View file

@ -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)

View file

@ -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<Credentials> = 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"
}
}

View file

@ -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<HomeServerConnectionConfig> = 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"
}
}

View file

@ -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<Credentials> = mockk()
private val homeServerConnectionConfigAdapter: JsonAdapter<HomeServerConnectionConfig> = 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"
}
}