|
| 1 | +package com.hoc081098.github_search_kmm.data.remote |
| 2 | + |
| 3 | +import arrow.core.getOrHandle |
| 4 | +import com.hoc081098.github_search_kmm.TestAntilog |
| 5 | +import com.hoc081098.github_search_kmm.TestAppCoroutineDispatchers |
| 6 | +import com.hoc081098.github_search_kmm.domain.model.ArgbColor |
| 7 | +import io.github.aakira.napier.Napier |
| 8 | +import io.ktor.client.HttpClient |
| 9 | +import io.ktor.client.engine.mock.MockEngine |
| 10 | +import io.ktor.client.engine.mock.MockRequestHandler |
| 11 | +import io.ktor.client.engine.mock.respond |
| 12 | +import io.ktor.http.ContentType |
| 13 | +import io.ktor.http.HttpHeaders |
| 14 | +import io.ktor.http.HttpMethod |
| 15 | +import io.ktor.http.HttpStatusCode |
| 16 | +import io.ktor.http.Url |
| 17 | +import io.ktor.http.headersOf |
| 18 | +import kotlin.test.AfterTest |
| 19 | +import kotlin.test.BeforeTest |
| 20 | +import kotlin.test.Test |
| 21 | +import kotlin.test.assertEquals |
| 22 | +import kotlin.test.fail |
| 23 | +import kotlinx.coroutines.channels.Channel |
| 24 | +import kotlinx.coroutines.test.runTest |
| 25 | + |
| 26 | +class KtorGithubLanguageColorApiTest { |
| 27 | + private lateinit var ktorGithubLanguageColorApi: KtorGithubLanguageColorApi |
| 28 | + private lateinit var httpClient: HttpClient |
| 29 | + private lateinit var handlerChannel: Channel<MockRequestHandler> |
| 30 | + |
| 31 | + private val url = Url("https://127.0.0.1:8080") |
| 32 | + private val testAppCoroutineDispatchers = TestAppCoroutineDispatchers() |
| 33 | + private val json = createJson() |
| 34 | + private val antilog = TestAntilog() |
| 35 | + |
| 36 | + @BeforeTest |
| 37 | + fun setup() { |
| 38 | + Napier.base(antilog) |
| 39 | + |
| 40 | + handlerChannel = Channel(Channel.UNLIMITED) |
| 41 | + httpClient = createHttpClient(MockEngine, json) { |
| 42 | + addHandler { request -> |
| 43 | + handlerChannel.receive()(request) |
| 44 | + } |
| 45 | + } |
| 46 | + ktorGithubLanguageColorApi = KtorGithubLanguageColorApi( |
| 47 | + httpClient = httpClient, |
| 48 | + url = url, |
| 49 | + appCoroutineDispatchers = testAppCoroutineDispatchers |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | + @AfterTest |
| 54 | + fun teardown() { |
| 55 | + httpClient.close() |
| 56 | + handlerChannel.close() |
| 57 | + Napier.takeLogarithm(antilog) |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun `getColors returns a Right WHEN httpClient returns a successful response and it is valid`() = |
| 62 | + runTest(testAppCoroutineDispatchers.testCoroutineDispatcher) { |
| 63 | + val colorsResponse = """ |
| 64 | + |{ |
| 65 | + | "ABAP CDS": { |
| 66 | + | "color": "#555e25", |
| 67 | + | "url": "https://github.com/trending?l=ABAP-CDS" |
| 68 | + | }, |
| 69 | + | "Ada": { |
| 70 | + | "url": "https://github.com/trending?l=Ada" |
| 71 | + | }, |
| 72 | + | "Agda": { |
| 73 | + | "color": "#315665", |
| 74 | + | "url": "https://github.com/trending?l=Agda" |
| 75 | + | } |
| 76 | + |} |
| 77 | + """.trimMargin("|") |
| 78 | + |
| 79 | + handlerChannel.trySend { request -> |
| 80 | + when (request.url.encodedPath) { |
| 81 | + "" -> { |
| 82 | + check(request.method == HttpMethod.Get) |
| 83 | + |
| 84 | + respond( |
| 85 | + content = colorsResponse, |
| 86 | + status = HttpStatusCode.OK, |
| 87 | + headers = headersOf( |
| 88 | + HttpHeaders.ContentType, |
| 89 | + ContentType.Application.Json.toString() |
| 90 | + ) |
| 91 | + ) |
| 92 | + } |
| 93 | + else -> error("Unhandled request ${request.url}") |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + val either = ktorGithubLanguageColorApi.getColors() |
| 98 | + val colors = either.getOrHandle { throw it } |
| 99 | + |
| 100 | + assertEquals( |
| 101 | + mapOf( |
| 102 | + "ABAP CDS" to ArgbColor.parse("#555e25").getOrHandle { fail(it) }, |
| 103 | + "Agda" to ArgbColor.parse("#315665").getOrHandle { fail(it) }, |
| 104 | + ), |
| 105 | + colors, |
| 106 | + ) |
| 107 | + } |
| 108 | +} |
0 commit comments