Skip to content

Commit

Permalink
Improve testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso committed Oct 7, 2023
1 parent 85a8630 commit 338ba0e
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 10 deletions.
8 changes: 6 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ binaryCompatibilityValidator = "0.13.2"
coroutines = "1.7.3"
detekt = "1.23.1"
gradlePlugin = "1.7.0"
junit = "4.13.2"
kctfork = "0.3.2"
kotlin = "1.9.10"
kotlinPoet = "1.14.2"
kspVersion = "1.9.10-1.0.13"
ktorfit = "1.8.0-SNAPSHOT"
ktorVersion = "2.3.4"
mockk = "1.13.8"
mockito-kotlin = "4.1.0"
gradleMavenPublishPlugin = "0.25.3"
vannikMavenPublish = "0.25.3"

Expand All @@ -20,7 +23,7 @@ auto-service-ksp = { module = "dev.zacsweers.autoservice:auto-service-ksp", vers
autoService = { module = "com.google.auto.service:auto-service", version.ref = "autoService" }
gradle-maven-publish-plugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "gradleMavenPublishPlugin" }
gradle-plugin = { module = "de.jensklingenberg.ktorfit:gradle-plugin", version.ref = "gradlePlugin" }
junit = "junit:junit:4.13.2"
junit = { module = "junit:junit", version.ref = "junit" }
kctfork-core = { module = "dev.zacsweers.kctfork:core", version.ref = "kctfork" }
kctfork-ksp = { module = "dev.zacsweers.kctfork:ksp", version.ref = "kctfork" }
kotlin-compiler-embeddable = { module = "org.jetbrains.kotlin:kotlin-compiler-embeddable", version.ref = "kotlin" }
Expand Down Expand Up @@ -53,7 +56,8 @@ ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx

licensee-gradle-plugin = "app.cash.licensee:licensee-gradle-plugin:1.8.0"
logbackClassic = "ch.qos.logback:logback-classic:1.4.11"
mockito-kotlin = "org.mockito.kotlin:mockito-kotlin:4.1.0"
mockito-kotlin = { module = "org.mockito.kotlin:mockito-kotlin", version.ref = "mockito-kotlin" }
mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
truth = "com.google.truth:truth:1.1.5"

[plugins]
Expand Down
14 changes: 13 additions & 1 deletion ktorfit-converters/call/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,22 @@ kotlin {
implementation(projects.ktorfitLibCommon)
}
}
val commonTest by getting {
dependencies {
implementation(libs.kotlin.test)
}
}
val linuxX64Main by getting
val mingwX64Main by getting
val androidMain by getting
val jvmMain by getting
val jvmTest by getting {
dependencies {
implementation(libs.ktor.client.mock)
implementation(libs.mockk)
implementation(libs.mockito.kotlin)
}
}
val iosX64Main by getting
val iosArm64Main by getting
val iosSimulatorArm64Main by getting
Expand Down Expand Up @@ -123,7 +135,7 @@ publishing {

pom {
name.set(project.name)
description.set("Flow Converter for Ktorfit")
description.set("Call Converter for Ktorfit")
url.set("https://github.com/Foso/Ktorfit")

licenses {
Expand Down
66 changes: 66 additions & 0 deletions ktorfit-converters/call/src/jvmTest/kotlin/ConverterTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import de.jensklingenberg.ktorfit.Call
import de.jensklingenberg.ktorfit.Callback
import de.jensklingenberg.ktorfit.Ktorfit
import de.jensklingenberg.ktorfit.converter.builtin.CallConverterFactory
import de.jensklingenberg.ktorfit.internal.TypeData
import io.ktor.client.*
import io.ktor.client.engine.mock.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.util.reflect.*
import io.ktor.utils.io.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class ConverterTest {

@Test
fun whenCallConverterIsUsedThenRespondSuccessful() = runBlocking {
val mockResponseContent = """{"ip":"127.0.0.1"}"""

val mockEngine = MockEngine { _ ->
respond(
content = ByteReadChannel(mockResponseContent),
status = HttpStatusCode.OK,
headers = headersOf(HttpHeaders.ContentType, "application/json")
)
}

val client = HttpClient(mockEngine) {}

val responseFunc = suspend { client.request("http://example.org/") }
val converter = CallConverterFactory()
val ktor =
Ktorfit.Builder()
.baseUrl("http://example.org/")
.httpClient(client)
.build()

val typeData = TypeData(
"de.jensklingenberg.ktorfit.Call<kotlin.String>",
typeInfo = typeInfo<Call<String>>(),
typeArgs = listOf(
TypeData("kotlin.String", typeInfo = typeInfo<String>())
)
)

val responseConverter = converter.responseConverter(typeData, ktor)
assertNotNull(responseConverter)
val converted = responseConverter.convert(responseFunc) as Call<String>
converted.onExecute(object : Callback<String> {
override fun onResponse(call: String, response: HttpResponse) {
assertEquals(mockResponseContent, call)
}

override fun onError(exception: Throwable) {
exception
}

})
delay(100)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public data class RequestData(
private val returnTypeName: String,
private val returnTypeInfo: TypeInfo
) {
internal fun getTypeData(): TypeData = createTypeData(returnTypeName, returnTypeInfo)
internal fun getTypeData(): TypeData = createTypeData(qualifiedTypename = returnTypeName, typeInfo = returnTypeInfo)
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,11 @@ class ClientTest {
}

val ktorfit = Ktorfit.Builder().baseUrl(testBaseUrl).httpClient(HttpClient(engine)).build()
try {
runBlocking {
ktorfit.create<ClientTestApi>(_ClientTestApiImpl()).checkCorrectHttpMethod()

}
} catch (exception: Exception) {
exception
runBlocking {
ktorfit.create<ClientTestApi>(_ClientTestApiImpl()).checkCorrectHttpMethod()
}

}

@Test
Expand Down

0 comments on commit 338ba0e

Please sign in to comment.