Improving mapper tests

This commit is contained in:
Maxime NATUREL 2022-05-30 10:03:27 +02:00
parent 03a8289a13
commit f707f177d9

View file

@ -16,57 +16,41 @@
package org.matrix.android.sdk.internal.database.mapper package org.matrix.android.sdk.internal.database.mapper
import io.mockk.every import com.squareup.moshi.Moshi
import io.mockk.mockk import org.amshove.kluent.shouldBeEqualTo
import io.mockk.mockkObject
import io.mockk.unmockkObject
import org.amshove.kluent.internal.assertEquals
import org.junit.After
import org.junit.Before
import org.junit.Test import org.junit.Test
import org.matrix.android.sdk.api.session.events.model.Content
import org.matrix.android.sdk.api.session.room.model.livelocation.LiveLocationShareAggregatedSummary import org.matrix.android.sdk.api.session.room.model.livelocation.LiveLocationShareAggregatedSummary
import org.matrix.android.sdk.api.session.room.model.message.LocationInfo
import org.matrix.android.sdk.api.session.room.model.message.MessageBeaconLocationDataContent
import org.matrix.android.sdk.internal.database.model.livelocation.LiveLocationShareAggregatedSummaryEntity import org.matrix.android.sdk.internal.database.model.livelocation.LiveLocationShareAggregatedSummaryEntity
private const val ANY_USER_ID = "a-user-id"
private const val ANY_ACTIVE_STATE = true
private const val ANY_TIMEOUT = 123L
private val A_LOCATION_INFO = LocationInfo("a-geo-uri")
class LiveLocationShareAggregatedSummaryMapperTest { class LiveLocationShareAggregatedSummaryMapperTest {
private val mapper = LiveLocationShareAggregatedSummaryMapper() private val mapper = LiveLocationShareAggregatedSummaryMapper()
@Before
fun setUp() {
mockkObject(ContentMapper)
}
@After
fun tearDown() {
unmockkObject(ContentMapper)
}
@Test @Test
fun `given an entity then result should be mapped correctly`() { fun `given an entity then result should be mapped correctly`() {
val userId = "userId" val entity = anEntity(content = MessageBeaconLocationDataContent(locationInfo = A_LOCATION_INFO))
val timeout = 123L
val isActive = true
val lastKnownLocationContent = "lastKnownLocationContent"
val entity = LiveLocationShareAggregatedSummaryEntity(
userId = userId,
isActive = isActive,
endOfLiveTimestampMillis = timeout,
lastLocationContent = lastKnownLocationContent
)
val content = mockk<Content>()
every { ContentMapper.map(lastKnownLocationContent) } returns content
val summary = mapper.map(entity) val summary = mapper.map(entity)
// note: unfortunately the implementation relies on an inline method to map the lastLocationDataContent summary shouldBeEqualTo LiveLocationShareAggregatedSummary(
// since inline methods do not produce bytecode, it is not mockable and the verification on this field cannot be done userId = ANY_USER_ID,
val expectedSummary = LiveLocationShareAggregatedSummary( isActive = ANY_ACTIVE_STATE,
userId = userId, endOfLiveTimestampMillis = ANY_TIMEOUT,
isActive = isActive, lastLocationDataContent = MessageBeaconLocationDataContent(locationInfo = A_LOCATION_INFO)
endOfLiveTimestampMillis = timeout,
lastLocationDataContent = null
) )
assertEquals(expectedSummary, summary)
} }
private fun anEntity(content: MessageBeaconLocationDataContent) = LiveLocationShareAggregatedSummaryEntity(
userId = ANY_USER_ID,
isActive = ANY_ACTIVE_STATE,
endOfLiveTimestampMillis = ANY_TIMEOUT,
lastLocationContent = Moshi.Builder().build().adapter(MessageBeaconLocationDataContent::class.java).toJson(content)
)
} }