Replaces caught Exception with HttpException
This commit is contained in:
parent
047e767f34
commit
bbc6e8bbce
|
@ -19,6 +19,7 @@ package org.matrix.android.sdk.internal.session.space
|
|||
import org.matrix.android.sdk.internal.network.GlobalErrorReceiver
|
||||
import org.matrix.android.sdk.internal.network.executeRequest
|
||||
import org.matrix.android.sdk.internal.task.Task
|
||||
import retrofit2.HttpException
|
||||
import javax.inject.Inject
|
||||
|
||||
internal interface ResolveSpaceInfoTask : Task<ResolveSpaceInfoTask.Params, SpacesResponse> {
|
||||
|
@ -45,7 +46,7 @@ internal class DefaultResolveSpaceInfoTask @Inject constructor(
|
|||
|
||||
private suspend fun getSpaceHierarchy() = try {
|
||||
getStableSpaceHierarchy()
|
||||
} catch (e: Throwable) {
|
||||
} catch (e: HttpException) {
|
||||
getUnstableSpaceHierarchy()
|
||||
}
|
||||
|
||||
|
|
|
@ -18,13 +18,17 @@ package org.matrix.android.sdk.internal.session.space
|
|||
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.runBlockingTest
|
||||
import okhttp3.ResponseBody.Companion.toResponseBody
|
||||
import org.amshove.kluent.shouldBeEqualTo
|
||||
import org.junit.Test
|
||||
import org.matrix.android.sdk.test.fakes.FakeGlobalErrorReceiver
|
||||
import org.matrix.android.sdk.test.fakes.FakeSpaceApi
|
||||
import org.matrix.android.sdk.test.fixtures.SpacesResponseFixture.aSpacesResponse
|
||||
import retrofit2.HttpException
|
||||
import retrofit2.Response
|
||||
|
||||
@ExperimentalCoroutinesApi
|
||||
class DefaultResolveSpaceInfoTaskTest {
|
||||
internal class DefaultResolveSpaceInfoTaskTest {
|
||||
|
||||
private val spaceApi = FakeSpaceApi()
|
||||
private val globalErrorReceiver = FakeGlobalErrorReceiver()
|
||||
|
@ -32,20 +36,25 @@ class DefaultResolveSpaceInfoTaskTest {
|
|||
|
||||
@Test
|
||||
fun `given stable endpoint works, when execute, then return stable api data`() = runBlockingTest {
|
||||
spaceApi.givenStableEndpointWorks()
|
||||
spaceApi.givenStableEndpointReturns(response)
|
||||
|
||||
val result = resolveSpaceInfoTask.execute(spaceApi.params)
|
||||
|
||||
result shouldBeEqualTo spaceApi.response
|
||||
result shouldBeEqualTo response
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given stable endpoint fails, when execute, then fallback to unstable endpoint`() = runBlockingTest {
|
||||
spaceApi.givenStableEndpointFails()
|
||||
spaceApi.givenUnstableEndpointWorks()
|
||||
spaceApi.givenStableEndpointThrows(httpException)
|
||||
spaceApi.givenUnstableEndpointReturns(response)
|
||||
|
||||
val result = resolveSpaceInfoTask.execute(spaceApi.params)
|
||||
|
||||
result shouldBeEqualTo spaceApi.response
|
||||
result shouldBeEqualTo response
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val response = aSpacesResponse()
|
||||
private val httpException = HttpException(Response.error<SpacesResponse>(500, "".toResponseBody()))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,24 +19,23 @@ package org.matrix.android.sdk.test.fakes
|
|||
import io.mockk.coEvery
|
||||
import io.mockk.mockk
|
||||
import org.matrix.android.sdk.internal.session.space.SpaceApi
|
||||
import org.matrix.android.sdk.internal.session.space.SpacesResponse
|
||||
import org.matrix.android.sdk.test.fixtures.ResolveSpaceInfoTaskParamsFixture
|
||||
import org.matrix.android.sdk.test.fixtures.SpacesResponseFixture
|
||||
|
||||
internal class FakeSpaceApi {
|
||||
|
||||
val instance: SpaceApi = mockk()
|
||||
val params = ResolveSpaceInfoTaskParamsFixture.aResolveSpaceInfoTaskParams()
|
||||
val response = SpacesResponseFixture.aSpacesResponse()
|
||||
|
||||
fun givenStableEndpointWorks() {
|
||||
fun givenStableEndpointReturns(response: SpacesResponse) {
|
||||
coEvery { instance.getSpaceHierarchy(params.spaceId, params.suggestedOnly, params.limit, params.maxDepth, params.from) } returns response
|
||||
}
|
||||
|
||||
fun givenStableEndpointFails() {
|
||||
coEvery { instance.getSpaceHierarchy(params.spaceId, params.suggestedOnly, params.limit, params.maxDepth, params.from) } throws Exception()
|
||||
fun givenStableEndpointThrows(throwable: Throwable) {
|
||||
coEvery { instance.getSpaceHierarchy(params.spaceId, params.suggestedOnly, params.limit, params.maxDepth, params.from) } throws throwable
|
||||
}
|
||||
|
||||
fun givenUnstableEndpointWorks() {
|
||||
fun givenUnstableEndpointReturns(response: SpacesResponse) {
|
||||
coEvery { instance.getSpaceHierarchyUnstable(params.spaceId, params.suggestedOnly, params.limit, params.maxDepth, params.from) } returns response
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue