diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/space/ResolveSpaceInfoTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/space/ResolveSpaceInfoTask.kt index 003148cf17..47c3f199b8 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/space/ResolveSpaceInfoTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/space/ResolveSpaceInfoTask.kt @@ -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 { @@ -45,7 +46,7 @@ internal class DefaultResolveSpaceInfoTask @Inject constructor( private suspend fun getSpaceHierarchy() = try { getStableSpaceHierarchy() - } catch (e: Throwable) { + } catch (e: HttpException) { getUnstableSpaceHierarchy() } diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/session/space/DefaultResolveSpaceInfoTaskTest.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/session/space/DefaultResolveSpaceInfoTaskTest.kt index d23280121c..f80c0f06d0 100644 --- a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/session/space/DefaultResolveSpaceInfoTaskTest.kt +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/session/space/DefaultResolveSpaceInfoTaskTest.kt @@ -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(500, "".toResponseBody())) } } diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeSpaceApi.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeSpaceApi.kt index b851e15575..d4fc986791 100644 --- a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeSpaceApi.kt +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeSpaceApi.kt @@ -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 } }