Adding tests to cover errors thrown during start/stop process

This commit is contained in:
Maxime NATUREL 2022-06-15 15:16:25 +02:00
parent 3e05431e6f
commit e1fc6fa727
6 changed files with 113 additions and 5 deletions

View File

@ -33,7 +33,6 @@ internal interface StartLiveLocationShareTask : Task<StartLiveLocationShareTask.
)
}
// TODO update unit test
internal class DefaultStartLiveLocationShareTask @Inject constructor(
@UserId private val userId: String,
private val clock: Clock,

View File

@ -36,7 +36,6 @@ internal interface StopLiveLocationShareTask : Task<StopLiveLocationShareTask.Pa
)
}
// TODO update unit tests
internal class DefaultStopLiveLocationShareTask @Inject constructor(
@UserId private val userId: String,
private val sendStateTask: SendStateTask,

View File

@ -20,6 +20,7 @@ import io.mockk.unmockkAll
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo
import org.amshove.kluent.shouldBeInstanceOf
import org.junit.After
import org.junit.Test
import org.matrix.android.sdk.api.session.events.model.EventType
@ -54,7 +55,7 @@ internal class DefaultStartLiveLocationShareTaskTest {
}
@Test
fun `given parameters an no error when calling the task then it is correctly executed`() = runTest {
fun `given parameters and no error when calling the task then result is success`() = runTest {
val params = StartLiveLocationShareTask.Params(
roomId = A_ROOM_ID,
timeoutMillis = A_TIMEOUT
@ -81,4 +82,33 @@ internal class DefaultStartLiveLocationShareTaskTest {
remainingRetry = 3
)
}
@Test
fun `given parameters and an empty returned event id when calling the task then result is failure`() = runTest {
val params = StartLiveLocationShareTask.Params(
roomId = A_ROOM_ID,
timeoutMillis = A_TIMEOUT
)
fakeClock.givenEpoch(AN_EPOCH)
fakeSendStateTask.givenExecuteRetryReturns("")
val result = defaultStartLiveLocationShareTask.execute(params)
result shouldBeInstanceOf UpdateLiveLocationShareResult.Failure::class
}
@Test
fun `given parameters and error during event sending when calling the task then result is failure`() = runTest {
val params = StartLiveLocationShareTask.Params(
roomId = A_ROOM_ID,
timeoutMillis = A_TIMEOUT
)
fakeClock.givenEpoch(AN_EPOCH)
val error = Throwable()
fakeSendStateTask.givenExecuteRetryThrows(error)
val result = defaultStartLiveLocationShareTask.execute(params)
result shouldBeEqualTo UpdateLiveLocationShareResult.Failure(error)
}
}

View File

@ -20,12 +20,14 @@ import io.mockk.unmockkAll
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo
import org.amshove.kluent.shouldBeInstanceOf
import org.junit.After
import org.junit.Test
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.events.model.EventType
import org.matrix.android.sdk.api.session.events.model.toContent
import org.matrix.android.sdk.api.session.room.location.UpdateLiveLocationShareResult
import org.matrix.android.sdk.api.session.room.model.message.MessageAudioContent
import org.matrix.android.sdk.api.session.room.model.message.MessageBeaconInfoContent
import org.matrix.android.sdk.internal.session.room.state.SendStateTask
import org.matrix.android.sdk.test.fakes.FakeSendStateTask
@ -55,7 +57,7 @@ class DefaultStopLiveLocationShareTaskTest {
}
@Test
fun `given parameters when calling the task then it is correctly executed`() = runTest {
fun `given parameters and no error when calling the task then result is success`() = runTest {
val params = StopLiveLocationShareTask.Params(roomId = A_ROOM_ID)
val currentStateEvent = Event(
stateKey = A_USER_ID,
@ -92,4 +94,78 @@ class DefaultStopLiveLocationShareTaskTest {
stateKey = A_USER_ID
)
}
@Test
fun `given parameters and an incorrect current state event when calling the task then result is failure`() = runTest {
val incorrectCurrentStateEvents = listOf(
// no event
null,
// no stateKey
Event(
stateKey = null,
content = MessageBeaconInfoContent(
timeout = A_TIMEOUT,
isLive = true,
unstableTimestampMillis = AN_EPOCH
).toContent()
),
// incorrect content
Event(
stateKey = A_USER_ID,
content = MessageAudioContent(
msgType = "",
body = ""
).toContent()
)
)
incorrectCurrentStateEvents.forEach { currentStateEvent ->
fakeStateEventDataSource.givenGetStateEventReturns(currentStateEvent)
fakeSendStateTask.givenExecuteRetryReturns(AN_EVENT_ID)
val params = StopLiveLocationShareTask.Params(roomId = A_ROOM_ID)
val result = defaultStopLiveLocationShareTask.execute(params)
result shouldBeInstanceOf UpdateLiveLocationShareResult.Failure::class
}
}
@Test
fun `given parameters and an empty returned event id when calling the task then result is failure`() = runTest {
val params = StopLiveLocationShareTask.Params(roomId = A_ROOM_ID)
val currentStateEvent = Event(
stateKey = A_USER_ID,
content = MessageBeaconInfoContent(
timeout = A_TIMEOUT,
isLive = true,
unstableTimestampMillis = AN_EPOCH
).toContent()
)
fakeStateEventDataSource.givenGetStateEventReturns(currentStateEvent)
fakeSendStateTask.givenExecuteRetryReturns("")
val result = defaultStopLiveLocationShareTask.execute(params)
result shouldBeInstanceOf UpdateLiveLocationShareResult.Failure::class
}
@Test
fun `given parameters and error during event sending when calling the task then result is failure`() = runTest {
val params = StopLiveLocationShareTask.Params(roomId = A_ROOM_ID)
val currentStateEvent = Event(
stateKey = A_USER_ID,
content = MessageBeaconInfoContent(
timeout = A_TIMEOUT,
isLive = true,
unstableTimestampMillis = AN_EPOCH
).toContent()
)
fakeStateEventDataSource.givenGetStateEventReturns(currentStateEvent)
val error = Throwable()
fakeSendStateTask.givenExecuteRetryThrows(error)
val result = defaultStopLiveLocationShareTask.execute(params)
result shouldBeEqualTo UpdateLiveLocationShareResult.Failure(error)
}
}

View File

@ -27,6 +27,10 @@ internal class FakeSendStateTask : SendStateTask by mockk() {
coEvery { executeRetry(any(), any()) } returns eventId
}
fun givenExecuteRetryThrows(error: Throwable) {
coEvery { executeRetry(any(), any()) } throws error
}
fun verifyExecuteRetry(params: SendStateTask.Params, remainingRetry: Int) {
coVerify { executeRetry(params, remainingRetry) }
}

View File

@ -27,7 +27,7 @@ internal class FakeStateEventDataSource {
val instance: StateEventDataSource = mockk()
fun givenGetStateEventReturns(event: Event) {
fun givenGetStateEventReturns(event: Event?) {
every {
instance.getStateEvent(
roomId = any(),