-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add dynamic query parameters to client calls (#311)
* Additional inclusion of query parameters * Complete testing
- Loading branch information
1 parent
97ff3c6
commit 10c5a67
Showing
21 changed files
with
297 additions
and
8 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...tests/okhttp/src/test/kotlin/com/cjbooms/fabrikt/clients/AdditionalQueryParametersTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.cjbooms.fabrikt.clients | ||
|
||
import com.example.client.ExamplePath1Client | ||
import com.example.models.FirstModel | ||
import com.example.models.QueryResult | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import com.github.tomakehurst.wiremock.common.ConsoleNotifier | ||
import com.github.tomakehurst.wiremock.core.WireMockConfiguration.options | ||
import com.marcinziolo.kotlin.wiremock.contains | ||
import com.marcinziolo.kotlin.wiremock.get | ||
import com.marcinziolo.kotlin.wiremock.like | ||
import com.marcinziolo.kotlin.wiremock.returns | ||
import java.net.ServerSocket | ||
import okhttp3.OkHttpClient | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestInstance | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class AdditionalQueryParametersTest { | ||
private val port: Int = ServerSocket(0).use { socket -> socket.localPort } | ||
private val wiremock: WireMockServer = WireMockServer(options().port(port).notifier(ConsoleNotifier(true))) | ||
private val mapper = ObjectMapper() | ||
private val httpClient = OkHttpClient.Builder().build() | ||
private val examplePath1Client = ExamplePath1Client(mapper, "http://localhost:$port", httpClient) | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
wiremock.start() | ||
} | ||
|
||
@AfterEach | ||
fun afterEach() { | ||
wiremock.resetAll() | ||
wiremock.stop() | ||
} | ||
|
||
@Test | ||
fun `additional query parameters are properly appended to requests`() { | ||
val expectedResponse = QueryResult(listOf(FirstModel(id = "the parameter was there!"))) | ||
wiremock.get { | ||
urlPath like "/example-path-1" | ||
queryParams contains "unspecified_param" like "some_value" | ||
} returns { | ||
statusCode = 200 | ||
body = mapper.writeValueAsString(expectedResponse) | ||
} | ||
val result = examplePath1Client.getExamplePath1(additionalQueryParameters = mapOf("unspecified_param" to "some_value")) | ||
assertThat(result.data).isEqualTo(expectedResponse) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -266,4 +266,4 @@ class Okio3Test { | |
|
||
assertThat(result.statusCode).isEqualTo(204) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
val fabrikt: Configuration by configurations.creating | ||
|
||
val generationDir = "$buildDir/generated" | ||
val apiFile = "${rootProject.projectDir}/src/test/resources/examples/okHttpClient/api.yaml" | ||
|
||
sourceSets { | ||
main { java.srcDirs("$generationDir/src/main/kotlin") } | ||
test { java.srcDirs("$generationDir/src/test/kotlin") } | ||
} | ||
|
||
plugins { | ||
id("org.jetbrains.kotlin.jvm") version "1.8.20" // Apply the Kotlin JVM plugin to add support for Kotlin. | ||
} | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
} | ||
|
||
val jacksonVersion: String by rootProject.extra | ||
val junitVersion: String by rootProject.extra | ||
|
||
dependencies { | ||
implementation("com.squareup.okhttp3:okhttp:4.10.0") | ||
implementation("io.github.openfeign:feign-core:13.3") | ||
implementation("io.github.openfeign:feign-jackson:13.3") | ||
implementation("io.github.openfeign:feign-okhttp:13.3") | ||
implementation("jakarta.validation:jakarta.validation-api:3.0.2") | ||
implementation("javax.validation:validation-api:2.0.1.Final") | ||
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion") | ||
implementation("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion") | ||
implementation("com.fasterxml.jackson.core:jackson-core:$jacksonVersion") | ||
implementation("com.fasterxml.jackson.core:jackson-annotations:$jacksonVersion") | ||
|
||
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion") | ||
testImplementation("org.junit.jupiter:junit-jupiter-engine:$junitVersion") | ||
testImplementation("org.junit.jupiter:junit-jupiter-params:$junitVersion") | ||
testImplementation("org.assertj:assertj-core:3.24.2") | ||
testImplementation("org.wiremock:wiremock:3.3.1") | ||
testImplementation("com.marcinziolo:kotlin-wiremock:2.1.1") | ||
} | ||
|
||
tasks { | ||
|
||
val generateCode by creating(JavaExec::class) { | ||
inputs.files(apiFile) | ||
outputs.dir(generationDir) | ||
outputs.cacheIf { true } | ||
classpath = rootProject.files("./build/libs/fabrikt-${rootProject.version}.jar") | ||
mainClass.set("com.cjbooms.fabrikt.cli.CodeGen") | ||
args = listOf( | ||
"--output-directory", generationDir, | ||
"--base-package", "com.example", | ||
"--api-file", apiFile, | ||
"--targets", "http_models", | ||
"--targets", "client", | ||
"--http-client-target", "open_feign", | ||
) | ||
dependsOn(":jar") | ||
dependsOn(":shadowJar") | ||
} | ||
|
||
withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> { | ||
kotlinOptions.jvmTarget = "17" | ||
dependsOn(generateCode) | ||
} | ||
|
||
|
||
withType<Test> { | ||
useJUnitPlatform() | ||
jvmArgs = listOf("--add-opens=java.base/java.lang=ALL-UNNAMED", "--add-opens=java.base/java.util=ALL-UNNAMED") | ||
|
||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...ts/openfeign/src/test/kotlin/com/cjbooms/fabrikt/clients/AdditionalQueryParametersTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.cjbooms.fabrikt.clients | ||
|
||
import com.example.client.ExamplePath1Client | ||
import com.example.models.FirstModel | ||
import com.example.models.QueryResult | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import com.github.tomakehurst.wiremock.common.ConsoleNotifier | ||
import com.github.tomakehurst.wiremock.core.WireMockConfiguration | ||
import com.marcinziolo.kotlin.wiremock.contains | ||
import com.marcinziolo.kotlin.wiremock.get | ||
import com.marcinziolo.kotlin.wiremock.like | ||
import com.marcinziolo.kotlin.wiremock.returns | ||
import feign.Feign | ||
import feign.jackson.JacksonDecoder | ||
import feign.jackson.JacksonEncoder | ||
import feign.okhttp.OkHttpClient | ||
import java.net.ServerSocket | ||
import org.assertj.core.api.Assertions | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestInstance | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class AdditionalQueryParametersTest { | ||
private val port: Int = ServerSocket(0).use { socket -> socket.localPort } | ||
private val wiremock: WireMockServer = WireMockServer( | ||
WireMockConfiguration.options().port(port).notifier(ConsoleNotifier(true))) | ||
private val mapper = ObjectMapper() | ||
private val examplePath1Client: ExamplePath1Client = Feign | ||
.builder() | ||
.client(OkHttpClient()) | ||
.encoder(JacksonEncoder(mapper)) | ||
.decoder(JacksonDecoder(mapper)) | ||
.target(ExamplePath1Client::class.java, "http://localhost:$port") | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
wiremock.start() | ||
} | ||
|
||
@AfterEach | ||
fun afterEach() { | ||
wiremock.resetAll() | ||
wiremock.stop() | ||
} | ||
|
||
@Test | ||
fun `additional query parameters are properly appended to requests`() { | ||
val expectedResponse = QueryResult(listOf(FirstModel(id = "the parameter was there!"))) | ||
wiremock.get { | ||
urlPath like "/example-path-1" | ||
queryParams contains "unspecified_param" like "some_value" | ||
} returns { | ||
statusCode = 200 | ||
body = mapper.writeValueAsString(expectedResponse) | ||
} | ||
val result = examplePath1Client.getExamplePath1(additionalQueryParameters = mapOf("unspecified_param" to "some_value")) | ||
Assertions.assertThat(result).isEqualTo(expectedResponse) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.