kotlin-wiremock.api-stub-builder.mustache Maven / Gradle / Ivy
@file:Suppress(
"RemoveRedundantQualifierName",
"UnusedImport",
"unused",
)
package {{apiPackage}}
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.tomakehurst.wiremock.client.MappingBuilder
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder
import com.github.tomakehurst.wiremock.client.WireMock.*
import com.github.tomakehurst.wiremock.matching.StringValuePattern
import {{modelPackage}}.*
{{#operations}}
{{#operation}}
/**
* Builder for WireMock stubs of operation {{operationId}}.
*/
class {{operationIdCamelCase}}StubBuilder internal constructor(private val objectMapper: ObjectMapper, private val stub: MappingBuilder) {
{{#responses}}
{{^wildcard}}
/**
* Let the stub for {{operationId}} respond with HTTP status code {{code}}.
*
{{#returnType}}
* @param body response body for the [MappingBuilder].
{{/returnType}}
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
*/
fun respondWith{{code}}(
{{#returnType}}
body: {{{.}}},
{{/returnType}}
configurer: ResponseDefinitionBuilder.() -> ResponseDefinitionBuilder = { this },
): MappingBuilder =
stub.willReturn(aResponse()
.withStatus({{code}})
{{#returnType}}
.withHeader("Content-Type", "application/json")
.withBody(objectMapper.writeValueAsString(body))
{{/returnType}}
.configurer()
)
{{/wildcard}}
{{/responses}}
/**
* Let the stub for {{operationId}} respond with HTTP status code [code].
*
{{#returnType}}
* @param body response body for the [MappingBuilder].
{{/returnType}}
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
*/
fun respondWith(
code: Int,
body: Any? = null,
configurer: ResponseDefinitionBuilder.() -> ResponseDefinitionBuilder = { this }
): MappingBuilder =
stub.willReturn(aResponse()
.withStatus(code)
.apply {
body?.let {
withHeader("Content-Type", "application/json")
withBody(objectMapper.writeValueAsString(it))
}
}
.configurer()
)
}
{{/operation}}
{{/operations}}