mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-12-28 03:48:37 +03:00
Refactors SessionParamsMapperTest by adding fake moshi
This commit is contained in:
parent
25e73e5bd0
commit
187502c358
2 changed files with 133 additions and 67 deletions
|
@ -16,80 +16,52 @@
|
||||||
|
|
||||||
package org.matrix.android.sdk.internal.auth.db
|
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.every
|
||||||
import io.mockk.mockk
|
|
||||||
import org.amshove.kluent.shouldBeEqualTo
|
|
||||||
import org.amshove.kluent.shouldBeNull
|
import org.amshove.kluent.shouldBeNull
|
||||||
import org.junit.Before
|
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.matrix.android.sdk.api.auth.data.Credentials
|
import org.matrix.android.sdk.test.fakes.FakeSessionParamsMapperMoshi
|
||||||
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
|
import org.matrix.android.sdk.test.fakes.FakeSessionParamsMapperMoshi.Companion.nullSessionParams
|
||||||
import org.matrix.android.sdk.api.auth.data.SessionParams
|
import org.matrix.android.sdk.test.fakes.FakeSessionParamsMapperMoshi.Companion.nullSessionParamsEntity
|
||||||
import org.matrix.android.sdk.api.auth.data.sessionId
|
import org.matrix.android.sdk.test.fakes.FakeSessionParamsMapperMoshi.Companion.sessionParams
|
||||||
import org.matrix.android.sdk.internal.auth.login.LoginType
|
import org.matrix.android.sdk.test.fakes.FakeSessionParamsMapperMoshi.Companion.sessionParamsEntity
|
||||||
import org.matrix.android.sdk.test.fixtures.SessionParamsEntityFixture.aSessionParamsEntity
|
|
||||||
import org.matrix.android.sdk.test.fixtures.SessionParamsFixture.aSessionParams
|
|
||||||
|
|
||||||
class SessionParamsMapperTest {
|
class SessionParamsMapperTest {
|
||||||
|
|
||||||
private val moshi: Moshi = mockk()
|
private val fakeMoshi = FakeSessionParamsMapperMoshi()
|
||||||
private lateinit var sessionParamsMapper: SessionParamsMapper
|
private val sessionParamsMapper = SessionParamsMapper(fakeMoshi.instance)
|
||||||
|
|
||||||
private val credentialsAdapter: JsonAdapter<Credentials> = mockk()
|
|
||||||
private val homeServerConnectionAdapter: JsonAdapter<HomeServerConnectionConfig> = 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
|
@Test
|
||||||
fun `when mapping entity, then map as SessionParams`() {
|
fun `when mapping entity, then map as SessionParams`() {
|
||||||
|
|
||||||
val output = sessionParamsMapper.map(sessionParamsEntity)!!
|
val output = sessionParamsMapper.map(sessionParamsEntity)
|
||||||
|
|
||||||
output shouldBeEqualTo SessionParams(
|
fakeMoshi.assertSessionParamsWasMappedSuccessfully(output)
|
||||||
credentials,
|
|
||||||
homeServerConnectionConfig,
|
|
||||||
sessionParamsEntity.isTokenValid,
|
|
||||||
LoginType.fromValue(sessionParamsEntity.loginType)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `given null input, when mapping entity, then return null`() {
|
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
|
@Test
|
||||||
fun `given null credentials, when mapping entity, then return null`() {
|
fun `given null credentials, when mapping entity, then return null`() {
|
||||||
every { credentialsAdapter.fromJson(sessionParamsEntity.credentialsJson) } returns null
|
fakeMoshi.givenCredentialsFromJsonIsNull()
|
||||||
|
|
||||||
val output = sessionParamsMapper.map(sessionParamsEntity)
|
val output = sessionParamsMapper.map(sessionParamsEntity)
|
||||||
|
|
||||||
output.shouldBeNull()
|
fakeMoshi.assertSessionParamsIsNull(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `given null homeServerConnectionConfig, when mapping entity, then return null`() {
|
fun `given null homeServerConnectionConfig, when mapping entity, then return null`() {
|
||||||
every { homeServerConnectionAdapter.fromJson(sessionParamsEntity.homeServerConnectionConfigJson) } returns null
|
fakeMoshi.givenHomeServerConnectionConfigFromJsonIsNull()
|
||||||
|
|
||||||
val output = sessionParamsMapper.map(sessionParamsEntity)
|
val output = sessionParamsMapper.map(sessionParamsEntity)
|
||||||
|
|
||||||
output.shouldBeNull()
|
fakeMoshi.assertSessionParamsIsNull(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -97,50 +69,32 @@ class SessionParamsMapperTest {
|
||||||
|
|
||||||
val output = sessionParamsMapper.map(sessionParams)
|
val output = sessionParamsMapper.map(sessionParams)
|
||||||
|
|
||||||
output shouldBeEqualTo SessionParamsEntity(
|
fakeMoshi.assertSessionParamsEntityWasMappedSuccessfully(output)
|
||||||
sessionParams.credentials.sessionId(),
|
|
||||||
sessionParams.userId,
|
|
||||||
CREDENTIALS_JSON,
|
|
||||||
HOME_SERVER_CONNECTION_CONFIG_JSON,
|
|
||||||
sessionParams.isTokenValid,
|
|
||||||
sessionParams.loginType.value,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `given null input, when mapping sessionParams, then return null`() {
|
fun `given null input, when mapping sessionParams, then return null`() {
|
||||||
val nullSessionParams: SessionParams? = null
|
|
||||||
|
|
||||||
val output = sessionParamsMapper.map(nullSessionParams)
|
val output = sessionParamsMapper.map(nullSessionParams)
|
||||||
|
|
||||||
output.shouldBeNull()
|
fakeMoshi.assertSessionParamsEntityWasMappedSuccessfully(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `given null credentials json, when mapping sessionParams, then return null`() {
|
fun `given null credentials json, when mapping sessionParams, then return null`() {
|
||||||
every { credentialsAdapter.toJson(credentials) } returns null
|
fakeMoshi.givenCredentialsToJsonIsNull()
|
||||||
|
|
||||||
val output = sessionParamsMapper.map(sessionParams)
|
val output = sessionParamsMapper.map(sessionParams)
|
||||||
|
|
||||||
output.shouldBeNull()
|
fakeMoshi.assertSessionParamsEntityIsNull(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `given null homeServerConnectionConfig json, when mapping sessionParams, then return null`() {
|
fun `given null homeServerConnectionConfig json, when mapping sessionParams, then return null`() {
|
||||||
every { homeServerConnectionAdapter.toJson(homeServerConnectionConfig) } returns null
|
fakeMoshi.givenHomeServerConnectionConfigToJsonIsNull()
|
||||||
|
|
||||||
val output = sessionParamsMapper.map(sessionParams)
|
val output = sessionParamsMapper.map(sessionParams)
|
||||||
|
|
||||||
output.shouldBeNull()
|
fakeMoshi.assertSessionParamsEntityIsNull(output)
|
||||||
}
|
|
||||||
|
|
||||||
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"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<Credentials> = mockk()
|
||||||
|
private val homeServerConnectionConfigAdapter: JsonAdapter<HomeServerConnectionConfig> = 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"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue