templates.kotlin-micronaut.server.test.controller_test.mustache Maven / Gradle / Ivy
package {{package}}
{{#imports}}
import {{import}}
{{/imports}}
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.runtime.server.EmbeddedServer
import io.micronaut.http.HttpStatus
import io.micronaut.http.MutableHttpRequest
import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpResponse
import io.micronaut.http.MediaType
import io.micronaut.http.uri.UriTemplate
import io.micronaut.http.cookie.Cookie
import io.micronaut.http.client.multipart.MultipartBody
import io.micronaut.core.type.Argument
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Assertions
import {{javaxPackage}}.inject.Inject
{{#reactive}}
import reactor.core.publisher.Flux
{{/reactive}}
import java.io.File
import java.io.FileReader
import java.io.IOException
import kotlin.reflect.typeOf
/**
* API tests for {{classname}}
*/
@MicronautTest
class {{classname}}Test(
val server: EmbeddedServer,
@Client
val client: HttpClient,
val controller: {{classname}},
) {
{{#operations}}
{{#operation}}
/**
* This test is used to validate the implementation of {{operationId}}() method
*
* The method should: {{summary}}
{{#notes}}
*
* {{notes}}
{{/notes}}
*
* TODO fill in the parameters and test return value.
*/
{{^generateControllerFromExamples}}
@Disabled("Not Implemented")
{{/generateControllerFromExamples}}
@Test
fun {{operationId}}MethodTest() {
// given
{{#allParams}}
var {{{paramName}}} = {{{example}}}
{{/allParams}}
// when
{{#returnType}}var result = {{/returnType}}controller.{{operationId}}({{#allParams}}{{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}}){{#reactive}}.block(){{/reactive}}{{#wrapInHttpResponse}}.body(){{/wrapInHttpResponse}}
// then
{{^generateControllerFromExamples}}
Assertions.assertTrue(true)
{{/generateControllerFromExamples}}
{{#generateControllerFromExamples}}
{{#returnType}}
{{#vendorExtensions.example}}
Assertions.assertEquals(result, {{{vendorExtensions.example}}})
{{/vendorExtensions.example}}
{{/returnType}}
{{/generateControllerFromExamples}}
}
/**
* This test is used to check that the api available to client through
* '{{{path}}}' to the features of {{operationId}}() works as desired.
*
* TODO fill in the request parameters and test response.
*/
{{^generateControllerFromExamples}}
@Disabled("Not Implemented")
{{/generateControllerFromExamples}}
@Throws(IOException::class)
@Test
fun {{operationId}}ClientApiTest() {
// given
{{!Create the body}}
{{#bodyParam}}
var body: {{{dataType}}} = {{{example}}}
{{/bodyParam}}
{{#formParams.0}}
var form = HashMap()
// Fill in the body form parameters
{{#formParams}}
{{^isFile}}
form.put("{{{baseName}}}", {{{example}}})
{{/isFile}}
{{#isFile}}
form.put("{{{baseName}}}", FileReader(File.createTempFile("test", ".tmp")))
{{/isFile}}
{{/formParams}}
{{/formParams.0}}
{{#isMultipart}}
{{^formParams}}
var body = MultipartBody.builder() // Create multipart body
{{#bodyParams}}
{{^isFile}}
.addPart("{{{baseName}}}", {{{example}}}{{^isString}}.toString(){{/isString}})
{{/isFile}}
{{#isFile}}
{{#contentType}}
.addPart("{{{baseName}}}", "filename", MediaType.of("{{{contentType}}}"), File.createTempFile("test", ".tmp"))
{{/contentType}}
{{^contentType}}
.addPart("{{{baseName}}}", "filename", File.createTempFile("test", ".tmp"))
{{/contentType}}
{{/isFile}}
{{/bodyParams}}
.build()
{{/formParams}}
{{/isMultipart}}
{{!Create the uri with path variables}}
var uri = UriTemplate.of("{{{path}}}").expand(mapOf(
// Fill in the path variables
{{#pathParams}}
Pair("{{{baseName}}}", {{{example}}})
{{/pathParams}}
))
{{!Create the request with body and uri}}
var request = HttpRequest.{{httpMethod}}{{^vendorExtensions.methodAllowsBody}}{{/vendorExtensions.methodAllowsBody}}{{#vendorExtensions.methodAllowsBody}}{{#bodyParam}}(uri, body){{/bodyParam}}{{#isMultipart}}{{^formParams}}(uri, body){{/formParams}}{{/isMultipart}}{{#formParams.0}}(uri, form){{/formParams.0}}{{^bodyParam}}{{^isMultipart}}{{^formParams}}(uri, null){{/formParams}}{{/isMultipart}}{{/bodyParam}}{{/vendorExtensions.methodAllowsBody}}{{^vendorExtensions.methodAllowsBody}}(uri){{/vendorExtensions.methodAllowsBody}}{{!Fill in all the request parameters}}{{#vendorExtensions.x-contentType}}
.contentType("{{vendorExtensions.x-contentType}}"){{/vendorExtensions.x-contentType}}{{#vendorExtensions.x-accepts}}
.accept("{{vendorExtensions.x-accepts}}"){{/vendorExtensions.x-accepts}}{{#headerParams}}
.header("{{{baseName}}}", {{{example}}}{{^isString}}.toString(){{/isString}}){{/headerParams}}{{#cookieParams}}
.cookie(Cookie.of("{{{baseName}}}", {{{example}}})){{/cookieParams}}
{{!Fill in the query parameters}}
{{#queryParams.0}}
request.getParameters()
{{#queryParams}}
{{#isCollectionFormatMulti}}
.add("{{{baseName}}}", {{{example}}}){{#-last}}{{/-last}} // The query format should be multi
{{/isCollectionFormatMulti}}
{{#isDeepObject}}
.add("{{{baseName}}}[property]", "value"){{#-last}}{{/-last}} // The query format should be deep-object
{{/isDeepObject}}
{{^isCollectionFormatMulti}}
{{^isDeepObject}}
.add("{{{baseName}}}", {{{example}}}{{^isString}}.toString(){{/isString}}){{#-last}}{{/-last}} // The query parameter format should be {{collectionFormat}}
{{/isDeepObject}}
{{/isCollectionFormatMulti}}
{{/queryParams}}
{{/queryParams.0}}
// when
var response = client.toBlocking().exchange(request{{#returnType}}, {{#returnContainer}}Argument.of({{#isArray}}List{{/isArray}}{{#isMap}}Map{{/isMap}}::class.java, {{#isMap}}String::class.java, {{/isMap}}{{{returnBaseType}}}::class.java){{/returnContainer}}{{^returnContainer}}typeOf<{{{returnType}}}>()::class.java{{/returnContainer}}){{/returnType}}{{^returnType}}, Void::class.java) // To retrieve body you must specify required type (e.g. Map.class) as second argument {{/returnType}}
// then
Assertions.assertEquals(HttpStatus.OK, response.status())
{{#generateControllerFromExamples}}
{{#returnType}}
Assertions.assertEquals(response.body(), {{{vendorExtensions.example}}})
{{/returnType}}
{{/generateControllerFromExamples}}
}
{{/operation}}
{{/operations}}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy