Test openapi schema parameters
This commit is contained in:
@@ -52,6 +52,7 @@ class `Article routes` : BaseTest() {
|
||||
`And the response should not be null`()
|
||||
`And have property`("$.total") `whish contains` 1
|
||||
`And have property`("$.result[0]workgroup.name") `whish contains` "Les papy"
|
||||
`And schema must be valid`()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,15 +2,15 @@ package integration.steps.then
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.databind.node.TextNode
|
||||
import fr.dcproject.common.utils.getResource
|
||||
import io.ktor.http.Url
|
||||
import io.ktor.request.contentType
|
||||
import io.ktor.request.httpMethod
|
||||
import io.ktor.request.uri
|
||||
import io.ktor.server.testing.TestApplicationResponse
|
||||
import org.openapi4j.core.model.v3.OAI3
|
||||
import org.openapi4j.parser.OpenApi3Parser
|
||||
import org.openapi4j.parser.model.v3.OpenApi3
|
||||
import org.openapi4j.parser.model.v3.Schema
|
||||
import org.openapi4j.schema.validator.ValidationContext
|
||||
import org.openapi4j.schema.validator.ValidationData
|
||||
import org.openapi4j.schema.validator.v3.SchemaValidator
|
||||
@@ -18,28 +18,40 @@ import java.io.File
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
fun TestApplicationResponse.`And schema must be valid`() {
|
||||
// Parse without validation, setting to true is strongly recommended for further data validation.
|
||||
val api: OpenApi3 = OpenApi3Parser().parse(File("/openapi2.yaml".getResource().toURI()), true)
|
||||
val uri = "/" + Url(call.request.uri).encodedPath
|
||||
val operation = call.request.httpMethod
|
||||
|
||||
val mediaType = this.call.request.contentType()
|
||||
val operation = this.call.request.httpMethod
|
||||
val uri = this.call.request.uri
|
||||
val status = this.call.response.status()
|
||||
OpenApi3Parser().parse(File("/openapi2.yaml".getResource().toURI()), true).let { api ->
|
||||
api.getPath(uri)
|
||||
?.getOperation(operation.value.toLowerCase())?.apply {
|
||||
val mediaType = call.request.contentType()
|
||||
val status = call.response.status()
|
||||
getResponse(status?.value?.toString() ?: error("HttpStatus not found"))
|
||||
?.getContentMediaType(mediaType.toString())
|
||||
?.schema?.let { schema ->
|
||||
val validationContext: ValidationContext<OAI3> = ValidationContext(api.context)
|
||||
val jsonNode: JsonNode = schema.toNode()
|
||||
val schemaValidator = SchemaValidator(validationContext, "", jsonNode)
|
||||
|
||||
val schema: Schema = api
|
||||
.getPath(uri)
|
||||
.getOperation(operation.value.toLowerCase())
|
||||
.getResponse(status?.value?.toString() ?: error("HttpStatus not found"))
|
||||
.getContentMediaType(mediaType.toString())
|
||||
.schema
|
||||
val results = ValidationData<Unit>()
|
||||
val mapper = ObjectMapper()
|
||||
schemaValidator.validate(mapper.readTree(content), results)
|
||||
|
||||
val validationContext: ValidationContext<OAI3> = ValidationContext(api.context)
|
||||
val jsonNode: JsonNode = schema.toNode()
|
||||
val schemaValidator = SchemaValidator(validationContext, "", jsonNode)
|
||||
assertTrue(results.isValid, results.results().toString())
|
||||
} ?: error("""No path found for "$operation $uri" for status ${status.value} with media type "$mediaType".""")
|
||||
}?.apply {
|
||||
Url(call.request.uri).parameters.forEach { parameter: String, values: List<String> ->
|
||||
getParametersIn(api.context, "query")
|
||||
?.firstOrNull { it.name == "workgroup" }?.schema?.let { schema ->
|
||||
val validationContext: ValidationContext<OAI3> = ValidationContext(api.context)
|
||||
val jsonNode: JsonNode = schema.toNode()
|
||||
val schemaValidator = SchemaValidator(validationContext, "", jsonNode)
|
||||
val params = ValidationData<Unit>()
|
||||
schemaValidator.validate(TextNode(values.first()), params)
|
||||
|
||||
val mapper = ObjectMapper()
|
||||
val results = ValidationData<Unit>()
|
||||
schemaValidator.validate(mapper.readTree(content), results)
|
||||
|
||||
assertTrue(results.isValid, results.results().toString())
|
||||
assertTrue(params.isValid, params.results().toString())
|
||||
} ?: error("""No path found for "$operation $uri" for status "$parameter".""")
|
||||
}
|
||||
} ?: error("""No path found for "$operation $uri".""")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user