REDI-96: repository MockEngine test + Compose robot UI test + serialization fix
NetworkCharacterRepositoryTest swaps a Ktor MockEngine into HttpClientFactory and covers success mapping (incl. request URL/page-param construction), 404 -> NOT_FOUND, 500 -> SERVER_ERROR, and malformed body -> SERIALIZATION. That last case exposed a real bug: Ktor wraps the kotlinx SerializationException in its own ContentConvertException, so safeCall mapped it to UNKNOWN; safeCall now scans the cause chain and maps it to SERIALIZATION. Adds an instrumented Compose UI test (CharacterListScreen) using the chaining CharacterListRobot: rendered items, empty/error states, and tap -> Action.
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
package com.example.architecture.feature.characters.data
|
||||
|
||||
import assertk.assertThat
|
||||
import assertk.assertions.endsWith
|
||||
import assertk.assertions.isEqualTo
|
||||
import assertk.assertions.isInstanceOf
|
||||
import assertk.assertions.isNotNull
|
||||
import com.example.architecture.core.data.network.HttpClientFactory
|
||||
import com.example.architecture.core.domain.DataError
|
||||
import com.example.architecture.core.domain.Result
|
||||
import com.example.architecture.feature.characters.data.datasource.KtorCharacterDataSource
|
||||
import io.ktor.client.engine.mock.MockEngine
|
||||
import io.ktor.client.engine.mock.MockRequestHandleScope
|
||||
import io.ktor.client.engine.mock.respond
|
||||
import io.ktor.client.request.HttpRequestData
|
||||
import io.ktor.client.request.HttpResponseData
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.http.headersOf
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
/**
|
||||
* Data-layer test for [NetworkCharacterRepository]. A Ktor [MockEngine] is swapped into the real
|
||||
* [HttpClientFactory] (`create(engine)` takes the engine precisely so tests can do this) — so the
|
||||
* full path under test is genuine: Ktor request → status/JSON handling in `safeCall` → DTO mapping →
|
||||
* domain model. Covers success mapping, a 404 and a 5xx mapped to typed [DataError.Network], and a
|
||||
* malformed-body → SERIALIZATION case.
|
||||
*/
|
||||
class NetworkCharacterRepositoryTest {
|
||||
|
||||
private fun repository(
|
||||
handler: MockRequestHandleScope.(HttpRequestData) -> HttpResponseData,
|
||||
): NetworkCharacterRepository {
|
||||
val engine = MockEngine { request -> handler(request) }
|
||||
val httpClient = HttpClientFactory.create(engine)
|
||||
return NetworkCharacterRepository(KtorCharacterDataSource(httpClient))
|
||||
}
|
||||
|
||||
private fun jsonHeaders() = headersOf(HttpHeaders.ContentType, "application/json")
|
||||
|
||||
@Test
|
||||
fun `getCharacters maps a successful response to a domain page`() = runTest {
|
||||
var requestedPath: String? = null
|
||||
var requestedPage: String? = null
|
||||
val repository = repository { request ->
|
||||
requestedPath = request.url.encodedPath
|
||||
requestedPage = request.url.parameters["page"]
|
||||
respond(content = CHARACTERS_PAGE_JSON, status = HttpStatusCode.OK, headers = jsonHeaders())
|
||||
}
|
||||
|
||||
val result = repository.getCharacters(page = 3)
|
||||
|
||||
// Request construction: correct endpoint and the page forwarded as a query param.
|
||||
assertThat(requestedPath).isNotNull().endsWith("/character")
|
||||
assertThat(requestedPage).isEqualTo("3")
|
||||
|
||||
assertThat(result).isInstanceOf(Result.Success::class)
|
||||
val page = (result as Result.Success).data
|
||||
assertThat(page.characters.size).isEqualTo(2)
|
||||
assertThat(page.characters.first().name).isEqualTo("Rick Sanchez")
|
||||
// `next` URL ".../character?page=2" is parsed to a page number.
|
||||
assertThat(page.nextPage).isEqualTo(2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getCharacters maps 404 to NOT_FOUND`() = runTest {
|
||||
val repository = repository {
|
||||
respond(content = "", status = HttpStatusCode.NotFound)
|
||||
}
|
||||
|
||||
val result = repository.getCharacters(page = 1)
|
||||
|
||||
assertThat(result).isEqualTo(Result.Error(DataError.Network.NOT_FOUND))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getCharacters maps 500 to SERVER_ERROR`() = runTest {
|
||||
val repository = repository {
|
||||
respond(content = "", status = HttpStatusCode.InternalServerError)
|
||||
}
|
||||
|
||||
val result = repository.getCharacters(page = 1)
|
||||
|
||||
assertThat(result).isEqualTo(Result.Error(DataError.Network.SERVER_ERROR))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getCharacters maps a malformed body to SERIALIZATION`() = runTest {
|
||||
val repository = repository {
|
||||
respond(content = "{ this is not valid json", status = HttpStatusCode.OK, headers = jsonHeaders())
|
||||
}
|
||||
|
||||
val result = repository.getCharacters(page = 1)
|
||||
|
||||
assertThat(result).isEqualTo(Result.Error(DataError.Network.SERIALIZATION))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getCharacterDetails maps a successful response to domain details`() = runTest {
|
||||
var requestedPath: String? = null
|
||||
val repository = repository { request ->
|
||||
requestedPath = request.url.encodedPath
|
||||
respond(content = CHARACTER_JSON, status = HttpStatusCode.OK, headers = jsonHeaders())
|
||||
}
|
||||
|
||||
val result = repository.getCharacterDetails(id = 1)
|
||||
|
||||
// Request construction: the id is placed in the path.
|
||||
assertThat(requestedPath).isNotNull().endsWith("/character/1")
|
||||
|
||||
assertThat(result).isInstanceOf(Result.Success::class)
|
||||
val details = (result as Result.Success).data
|
||||
assertThat(details.name).isEqualTo("Rick Sanchez")
|
||||
assertThat(details.origin).isEqualTo("Earth (C-137)")
|
||||
assertThat(details.episodeCount).isEqualTo(3)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
val CHARACTER_JSON = """
|
||||
{
|
||||
"id": 1,
|
||||
"name": "Rick Sanchez",
|
||||
"status": "Alive",
|
||||
"species": "Human",
|
||||
"type": "",
|
||||
"gender": "Male",
|
||||
"origin": { "name": "Earth (C-137)", "url": "" },
|
||||
"location": { "name": "Citadel of Ricks", "url": "" },
|
||||
"image": "https://example.com/1.png",
|
||||
"episode": ["e1", "e2", "e3"]
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
val CHARACTERS_PAGE_JSON = """
|
||||
{
|
||||
"info": {
|
||||
"count": 2,
|
||||
"pages": 1,
|
||||
"next": "https://rickandmortyapi.com/api/character?page=2",
|
||||
"prev": null
|
||||
},
|
||||
"results": [
|
||||
{
|
||||
"id": 1, "name": "Rick Sanchez", "status": "Alive", "species": "Human",
|
||||
"type": "", "gender": "Male",
|
||||
"origin": { "name": "Earth (C-137)", "url": "" },
|
||||
"location": { "name": "Citadel of Ricks", "url": "" },
|
||||
"image": "https://example.com/1.png", "episode": ["e1", "e2"]
|
||||
},
|
||||
{
|
||||
"id": 2, "name": "Morty Smith", "status": "Alive", "species": "Human",
|
||||
"type": "", "gender": "Male",
|
||||
"origin": { "name": "Earth (C-137)", "url": "" },
|
||||
"location": { "name": "Citadel of Ricks", "url": "" },
|
||||
"image": "https://example.com/2.png", "episode": ["e1"]
|
||||
}
|
||||
]
|
||||
}
|
||||
""".trimIndent()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user