Skip to content

Commit

Permalink
Replace Java HttpClient with Ktor Http Client and add support for mul…
Browse files Browse the repository at this point in the history
…ti-part form data post (#404)
  • Loading branch information
osoykan authored Apr 22, 2024
1 parent 48af208 commit 8c7d26a
Show file tree
Hide file tree
Showing 10 changed files with 959 additions and 159 deletions.
672 changes: 672 additions & 0 deletions detekt.yml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package stove.spring.example.infrastructure.api

import kotlinx.coroutines.reactive.*
import kotlinx.coroutines.reactor.mono
import org.springframework.http.codec.multipart.FilePart
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RequestPart
import org.springframework.web.bind.annotation.RestController
import stove.spring.example.application.handlers.ProductCreateRequest
import stove.spring.example.application.handlers.ProductCreator
Expand All @@ -25,4 +29,16 @@ class ProductController(private val productCreator: ProductCreator) {
): String {
return productCreator.create(productCreateRequest)
}

@PostMapping("/product/import")
suspend fun importFile(
@RequestPart(name = "name") name: String,
@RequestPart(name = "file") file: FilePart
): String {
val content = file.content()
.flatMap { mono { it.asInputStream().readAllBytes() } }
.awaitSingle()
.let { String(it) }
return "File ${file.filename()} is imported with $name and content: $content"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package com.stove.spring.example.e2e

import arrow.core.some
import com.trendyol.stove.testing.e2e.couchbase.couchbase
import com.trendyol.stove.testing.e2e.http.http
import com.trendyol.stove.testing.e2e.http.*
import com.trendyol.stove.testing.e2e.kafka.kafka
import com.trendyol.stove.testing.e2e.system.TestSystem
import com.trendyol.stove.testing.e2e.using
import com.trendyol.stove.testing.e2e.wiremock.wiremock
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import org.springframework.http.MediaType
import stove.spring.example.application.handlers.*
import stove.spring.example.application.services.SupplierPermission
import stove.spring.example.infrastructure.couchbase.CouchbaseProperties
Expand Down Expand Up @@ -165,4 +166,25 @@ class ExampleTest : FunSpec({
}
}
}

test("file import should work") {
TestSystem.validate {
http {
postMultipartAndExpectResponse<String>(
"/api/product/import",
body = listOf(
StoveMultiPartContent.Text("name", "product name"),
StoveMultiPartContent.File(
"file",
"file.txt",
"file".toByteArray(),
contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE
)
)
) { actual ->
actual.body() shouldBe "File file.txt is imported with product name and content: file"
}
}
}
}
})
10 changes: 8 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ r2dbc-spi = "1.0.0.RELEASE"
r2dbc-postgresql = "0.8.13.RELEASE"
elastic = "8.13.2"
mongodb = "5.0.1"
wiremock = "3.5.3"
wiremock = "3.5.2"
testcontainers = "1.19.7"
r2dbc-mssql = "1.0.2.RELEASE"
spotless = "6.25.0"
Expand Down Expand Up @@ -59,14 +59,20 @@ ktor-server = { module = "io.ktor:ktor-server", version.ref = "ktor" }
ktor-server-call-logging = { module = "io.ktor:ktor-server-call-logging", version.ref = "ktor" }
ktor-server-netty = { module = "io.ktor:ktor-server-netty", version.ref = "ktor" }
ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
ktor-client-cio = { module = "io.ktor:ktor-client-cio", version.ref = "ktor" }
ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" }
ktor-client-plugins-logging = { module = "io.ktor:ktor-client-logging", version.ref = "ktor" }
ktor-client-content-negotiation = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktor" }
ktor-serialization-jackson-json = { module = "io.ktor:ktor-serialization-jackson", version.ref = "ktor" }
koin-ktor = { module = "io.insert-koin:koin-ktor", version.ref = "koin" }
koin-logger-slf4j = { module = "io.insert-koin:koin-logger-slf4j", version.ref = "koin" }
r2dbc-spi = { module = "io.r2dbc:r2dbc-spi", version.ref = "r2dbc-spi" }
r2dbc-postgresql = { module = "io.r2dbc:r2dbc-postgresql", version.ref = "r2dbc-postgresql" }
elastic = { module = "co.elastic.clients:elasticsearch-java", version.ref = "elastic" }
mongodb-reactivestreams = { module = "org.mongodb:mongodb-driver-reactivestreams", version.ref = "mongodb" }

wiremock = { module = "org.wiremock:wiremock-standalone", version.ref = "wiremock" }
wiremock-standalone = { module = "org.wiremock:wiremock-standalone", version.ref = "wiremock" }
testcontainers = { module = "org.testcontainers:testcontainers", version.ref = "testcontainers" }
testcontainers-jdbc = { module = "org.testcontainers:jdbc", version.ref = "testcontainers" }
testcontainers-kafka = { module = "org.testcontainers:kafka", version.ref = "testcontainers" }
Expand Down
5 changes: 5 additions & 0 deletions lib/stove-testing-e2e-http/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
dependencies {
api(projects.lib.stoveTestingE2e)
api(libs.ktor.client.core)
api(libs.ktor.client.okhttp)
implementation(libs.kotlinx.core)
implementation(libs.kotlinx.io.reactor)
implementation(libs.kotlinx.reactive)
implementation(libs.kotlinx.jdk8)
implementation(libs.ktor.client.plugins.logging)
implementation(libs.ktor.client.content.negotiation)
implementation(libs.ktor.serialization.jackson.json)
testImplementation(projects.lib.stoveTestingE2eWiremock)
}
Loading

0 comments on commit 8c7d26a

Please sign in to comment.