Replaces caught Exception with HttpException

This commit is contained in:
ericdecanini 2022-03-09 16:41:18 +01:00
parent 047e767f34
commit bbc6e8bbce
3 changed files with 22 additions and 13 deletions

View File

@ -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.GlobalErrorReceiver
import org.matrix.android.sdk.internal.network.executeRequest import org.matrix.android.sdk.internal.network.executeRequest
import org.matrix.android.sdk.internal.task.Task import org.matrix.android.sdk.internal.task.Task
import retrofit2.HttpException
import javax.inject.Inject import javax.inject.Inject
internal interface ResolveSpaceInfoTask : Task<ResolveSpaceInfoTask.Params, SpacesResponse> { internal interface ResolveSpaceInfoTask : Task<ResolveSpaceInfoTask.Params, SpacesResponse> {
@ -45,7 +46,7 @@ internal class DefaultResolveSpaceInfoTask @Inject constructor(
private suspend fun getSpaceHierarchy() = try { private suspend fun getSpaceHierarchy() = try {
getStableSpaceHierarchy() getStableSpaceHierarchy()
} catch (e: Throwable) { } catch (e: HttpException) {
getUnstableSpaceHierarchy() getUnstableSpaceHierarchy()
} }

View File

@ -18,13 +18,17 @@ package org.matrix.android.sdk.internal.session.space
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest import kotlinx.coroutines.test.runBlockingTest
import okhttp3.ResponseBody.Companion.toResponseBody
import org.amshove.kluent.shouldBeEqualTo import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test import org.junit.Test
import org.matrix.android.sdk.test.fakes.FakeGlobalErrorReceiver import org.matrix.android.sdk.test.fakes.FakeGlobalErrorReceiver
import org.matrix.android.sdk.test.fakes.FakeSpaceApi import org.matrix.android.sdk.test.fakes.FakeSpaceApi
import org.matrix.android.sdk.test.fixtures.SpacesResponseFixture.aSpacesResponse
import retrofit2.HttpException
import retrofit2.Response
@ExperimentalCoroutinesApi @ExperimentalCoroutinesApi
class DefaultResolveSpaceInfoTaskTest { internal class DefaultResolveSpaceInfoTaskTest {
private val spaceApi = FakeSpaceApi() private val spaceApi = FakeSpaceApi()
private val globalErrorReceiver = FakeGlobalErrorReceiver() private val globalErrorReceiver = FakeGlobalErrorReceiver()
@ -32,20 +36,25 @@ class DefaultResolveSpaceInfoTaskTest {
@Test @Test
fun `given stable endpoint works, when execute, then return stable api data`() = runBlockingTest { fun `given stable endpoint works, when execute, then return stable api data`() = runBlockingTest {
spaceApi.givenStableEndpointWorks() spaceApi.givenStableEndpointReturns(response)
val result = resolveSpaceInfoTask.execute(spaceApi.params) val result = resolveSpaceInfoTask.execute(spaceApi.params)
result shouldBeEqualTo spaceApi.response result shouldBeEqualTo response
} }
@Test @Test
fun `given stable endpoint fails, when execute, then fallback to unstable endpoint`() = runBlockingTest { fun `given stable endpoint fails, when execute, then fallback to unstable endpoint`() = runBlockingTest {
spaceApi.givenStableEndpointFails() spaceApi.givenStableEndpointThrows(httpException)
spaceApi.givenUnstableEndpointWorks() spaceApi.givenUnstableEndpointReturns(response)
val result = resolveSpaceInfoTask.execute(spaceApi.params) 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()))
} }
} }

View File

@ -19,24 +19,23 @@ package org.matrix.android.sdk.test.fakes
import io.mockk.coEvery import io.mockk.coEvery
import io.mockk.mockk import io.mockk.mockk
import org.matrix.android.sdk.internal.session.space.SpaceApi 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.ResolveSpaceInfoTaskParamsFixture
import org.matrix.android.sdk.test.fixtures.SpacesResponseFixture
internal class FakeSpaceApi { internal class FakeSpaceApi {
val instance: SpaceApi = mockk() val instance: SpaceApi = mockk()
val params = ResolveSpaceInfoTaskParamsFixture.aResolveSpaceInfoTaskParams() 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 coEvery { instance.getSpaceHierarchy(params.spaceId, params.suggestedOnly, params.limit, params.maxDepth, params.from) } returns response
} }
fun givenStableEndpointFails() { fun givenStableEndpointThrows(throwable: Throwable) {
coEvery { instance.getSpaceHierarchy(params.spaceId, params.suggestedOnly, params.limit, params.maxDepth, params.from) } throws Exception() 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 coEvery { instance.getSpaceHierarchyUnstable(params.spaceId, params.suggestedOnly, params.limit, params.maxDepth, params.from) } returns response
} }
} }