mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-02-03 20:47:32 +01:00
Adds logic for using stable and unstable hierarchy endpoints
This commit is contained in:
parent
bc3b8d0a16
commit
0af6ae6075
@ -19,7 +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 timber.log.Timber
|
||||
import retrofit2.HttpException
|
||||
import javax.inject.Inject
|
||||
|
||||
internal interface ResolveSpaceInfoTask : Task<ResolveSpaceInfoTask.Params, SpacesResponse> {
|
||||
@ -29,7 +29,6 @@ internal interface ResolveSpaceInfoTask : Task<ResolveSpaceInfoTask.Params, Spac
|
||||
val maxDepth: Int?,
|
||||
val from: String?,
|
||||
val suggestedOnly: Boolean?
|
||||
// val autoJoinOnly: Boolean?
|
||||
)
|
||||
}
|
||||
|
||||
@ -37,19 +36,35 @@ internal class DefaultResolveSpaceInfoTask @Inject constructor(
|
||||
private val spaceApi: SpaceApi,
|
||||
private val globalErrorReceiver: GlobalErrorReceiver
|
||||
) : ResolveSpaceInfoTask {
|
||||
|
||||
private lateinit var params: ResolveSpaceInfoTask.Params
|
||||
|
||||
override suspend fun execute(params: ResolveSpaceInfoTask.Params): SpacesResponse {
|
||||
return executeRequest(globalErrorReceiver) {
|
||||
try {
|
||||
throw RuntimeException("Test space task exception")
|
||||
} catch (e: Throwable) {
|
||||
Timber.i("Test fall back api")
|
||||
spaceApi.getSpaceHierarchy(
|
||||
spaceId = params.spaceId,
|
||||
suggestedOnly = params.suggestedOnly,
|
||||
limit = params.limit,
|
||||
maxDepth = params.maxDepth,
|
||||
from = params.from)
|
||||
}
|
||||
this.params = params
|
||||
getSpaceHierarchy()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun getSpaceHierarchy() = try {
|
||||
getStableSpaceHierarchy()
|
||||
} catch (e: HttpException) {
|
||||
getUnstableSpaceHierarchy()
|
||||
}
|
||||
|
||||
private suspend fun getStableSpaceHierarchy() =
|
||||
spaceApi.getSpaceHierarchy(
|
||||
spaceId = params.spaceId,
|
||||
suggestedOnly = params.suggestedOnly,
|
||||
limit = params.limit,
|
||||
maxDepth = params.maxDepth,
|
||||
from = params.from)
|
||||
|
||||
private suspend fun getUnstableSpaceHierarchy() =
|
||||
spaceApi.getSpaceHierarchyUnstable(
|
||||
spaceId = params.spaceId,
|
||||
suggestedOnly = params.suggestedOnly,
|
||||
limit = params.limit,
|
||||
maxDepth = params.maxDepth,
|
||||
from = params.from)
|
||||
}
|
||||
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2022 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.matrix.android.sdk.internal.session.space
|
||||
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.runBlockingTest
|
||||
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
|
||||
|
||||
@ExperimentalCoroutinesApi
|
||||
class DefaultResolveSpaceInfoTaskTest {
|
||||
|
||||
private val spaceApi = FakeSpaceApi()
|
||||
private val globalErrorReceiver = FakeGlobalErrorReceiver()
|
||||
private val resolveSpaceInfoTask = DefaultResolveSpaceInfoTask(spaceApi.instance, globalErrorReceiver)
|
||||
|
||||
@Test
|
||||
fun `given stable endpoint works, when execute, then return stable api data`() = runBlockingTest {
|
||||
spaceApi.givenStableEndpointWorks()
|
||||
|
||||
val result = resolveSpaceInfoTask.execute(spaceApi.params)
|
||||
|
||||
result shouldBeEqualTo spaceApi.response
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given stable endpoint fails, when execute, then fallback to unstable endpoint`() = runBlockingTest {
|
||||
spaceApi.givenStableEndpointFails()
|
||||
spaceApi.givenUnstableEndpointWorks()
|
||||
|
||||
val result = resolveSpaceInfoTask.execute(spaceApi.params)
|
||||
|
||||
result shouldBeEqualTo spaceApi.response
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (c) 2022 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.matrix.android.sdk.test.fakes
|
||||
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.mockk
|
||||
import okhttp3.ResponseBody.Companion.toResponseBody
|
||||
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
|
||||
import retrofit2.HttpException
|
||||
import retrofit2.Response
|
||||
|
||||
internal class FakeSpaceApi {
|
||||
|
||||
val instance: SpaceApi = mockk()
|
||||
val params = ResolveSpaceInfoTaskParamsFixture.aResolveSpaceInfoTaskParams()
|
||||
val response = SpacesResponseFixture.aSpacesResponse()
|
||||
|
||||
fun givenStableEndpointWorks() {
|
||||
coEvery { instance.getSpaceHierarchy(params.spaceId, params.suggestedOnly, params.limit, params.maxDepth, params.from) } returns response
|
||||
}
|
||||
|
||||
fun givenStableEndpointFails() {
|
||||
val errorResponse = Response.error<SpacesResponse>(500, "".toResponseBody())
|
||||
coEvery { instance.getSpaceHierarchy(params.spaceId, params.suggestedOnly, params.limit, params.maxDepth, params.from) } throws HttpException(errorResponse)
|
||||
}
|
||||
|
||||
fun givenUnstableEndpointWorks() {
|
||||
coEvery { instance.getSpaceHierarchyUnstable(params.spaceId, params.suggestedOnly, params.limit, params.maxDepth, params.from) } returns response
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (c) 2022 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.matrix.android.sdk.test.fixtures
|
||||
|
||||
import org.matrix.android.sdk.internal.session.space.ResolveSpaceInfoTask
|
||||
|
||||
internal object ResolveSpaceInfoTaskParamsFixture {
|
||||
fun aResolveSpaceInfoTaskParams(
|
||||
spaceId: String = "",
|
||||
limit: Int? = null,
|
||||
maxDepth: Int? = null,
|
||||
from: String? = null,
|
||||
suggestedOnly: Boolean? = null,
|
||||
) = ResolveSpaceInfoTask.Params(
|
||||
spaceId,
|
||||
limit,
|
||||
maxDepth,
|
||||
from,
|
||||
suggestedOnly,
|
||||
)
|
||||
}
|
30
matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/SpacesResponseFixture.kt
vendored
Normal file
30
matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fixtures/SpacesResponseFixture.kt
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (c) 2022 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.matrix.android.sdk.test.fixtures
|
||||
|
||||
import org.matrix.android.sdk.internal.session.space.SpaceChildSummaryResponse
|
||||
import org.matrix.android.sdk.internal.session.space.SpacesResponse
|
||||
|
||||
internal object SpacesResponseFixture {
|
||||
fun aSpacesResponse(
|
||||
nextBatch: String? = null,
|
||||
rooms: List<SpaceChildSummaryResponse>? = null,
|
||||
) = SpacesResponse(
|
||||
nextBatch,
|
||||
rooms,
|
||||
)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user