android: unify api error mapping across data repositories
Some checks failed
CI / test (push) Failing after 2m13s
Some checks failed
CI / test (push) Failing after 2m13s
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package ru.daemonlord.messenger.data.common
|
||||
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.ResponseBody.Companion.toResponseBody
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import retrofit2.HttpException
|
||||
import retrofit2.Response
|
||||
import ru.daemonlord.messenger.domain.common.AppError
|
||||
import java.io.IOException
|
||||
|
||||
class ApiErrorMapperTest {
|
||||
|
||||
@Test
|
||||
fun ioException_mapsToNetwork() {
|
||||
val error = IOException("offline").toAppError()
|
||||
assertTrue(error is AppError.Network)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun loginMode_maps401ToInvalidCredentials() {
|
||||
val error = httpException(401).toAppError(mode = ApiErrorMode.LOGIN)
|
||||
assertTrue(error is AppError.InvalidCredentials)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun defaultMode_maps401ToUnauthorized() {
|
||||
val error = httpException(401).toAppError(mode = ApiErrorMode.DEFAULT)
|
||||
assertTrue(error is AppError.Unauthorized)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun defaultMode_maps500ToServer() {
|
||||
val error = httpException(500).toAppError(mode = ApiErrorMode.DEFAULT)
|
||||
assertTrue(error is AppError.Server)
|
||||
}
|
||||
|
||||
private fun httpException(code: Int): HttpException {
|
||||
val body = "{}".toResponseBody("application/json".toMediaType())
|
||||
return HttpException(Response.error<Any>(code, body))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user