Skip to content

Commit

Permalink
feat(server): add tests for OpenApiUtil class (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-tschumak committed Sep 6, 2018
1 parent 1d41093 commit 01c6924
Show file tree
Hide file tree
Showing 2 changed files with 316 additions and 8 deletions.
12 changes: 6 additions & 6 deletions server/src/main/java/de/zalando/zally/util/OpenApiUtil.kt
Expand Up @@ -22,7 +22,7 @@ fun OpenAPI.getAllHeaders(): Set<HeaderElement> {
.map { HeaderElement(it.key, it.value) }
.toSet()

val fromComponentsParams = components.parameters.orEmpty().values.extractHeaders()
val fromComponentsParams = components?.parameters.orEmpty().values.extractHeaders()

val fromPaths = paths.orEmpty().flatMap { (_, path) ->
val fromPathParameters = path.parameters.extractHeaders()
Expand All @@ -34,7 +34,7 @@ fun OpenAPI.getAllHeaders(): Set<HeaderElement> {
fromPathParameters + fromOperations
}

val fromComponentsHeaders = components.headers.orEmpty().map { HeaderElement(it.key, it.value) }
val fromComponentsHeaders = components?.headers.orEmpty().map { HeaderElement(it.key, it.value) }

return fromComponentsParams + fromPaths + fromComponentsHeaders
}
Expand All @@ -43,9 +43,9 @@ fun OpenAPI.getAllHeaders(): Set<HeaderElement> {
* Returns all defined schemas of an API specification
* @return a collection of schemas
*/
fun OpenAPI.getAllSchemas(): Collection<Schema<Any>> = this.components.schemas.orEmpty().values +
this.components.responses.values.flatMap { it.content.values.mapNotNull { it.schema } } +
this.components.requestBodies.values.flatMap { it.content.values.mapNotNull { it.schema } } +
fun OpenAPI.getAllSchemas(): Collection<Schema<Any>> = this.components?.schemas.orEmpty().values +
this.components?.responses.orEmpty().values.flatMap { it.content.orEmpty().values.mapNotNull { it.schema } } +
this.components?.requestBodies.orEmpty().values.flatMap { it.content.orEmpty().values.mapNotNull { it.schema } } +
this.paths.orEmpty().flatMap {
it.value.readOperations().flatMap { it.parameters.orEmpty().mapNotNull { it.schema } }
} +
Expand Down Expand Up @@ -109,7 +109,7 @@ fun OpenAPI.getAllProperties(): Map<String, Schema<Any>> {
* Returns all defined parameters of an API specification
* @return a collection of parameters
*/
fun OpenAPI.getAllParameters(): Map<String, Parameter> = this.components.parameters.orEmpty() +
fun OpenAPI.getAllParameters(): Map<String, Parameter> = this.components?.parameters.orEmpty() +
this.paths.orEmpty().values.flatMap { it.parameters.orEmpty().mapNotNull { it.name to it } } +
this.paths.orEmpty().values.flatMap {
it.readOperations()
Expand Down
312 changes: 310 additions & 2 deletions server/src/test/java/de/zalando/zally/util/OpenApiUtilTest.kt
@@ -1,26 +1,334 @@
package de.zalando.zally.util

import io.swagger.v3.oas.models.Components
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.Operation
import io.swagger.v3.oas.models.PathItem
import io.swagger.v3.oas.models.Paths
import io.swagger.v3.oas.models.headers.Header
import io.swagger.v3.oas.models.media.Content
import io.swagger.v3.oas.models.media.MediaType
import io.swagger.v3.oas.models.media.Schema
import io.swagger.v3.oas.models.parameters.Parameter
import io.swagger.v3.oas.models.parameters.RequestBody
import io.swagger.v3.oas.models.responses.ApiResponse
import io.swagger.v3.oas.models.responses.ApiResponses
import org.junit.Test
import org.assertj.core.api.Assertions.assertThat

class OpenApiUtilTest {

@Test
fun `getAllHeaders should return headers from components parameters`() {
val api = OpenAPI().apply {
components = Components().apply {
parameters = mapOf("User-Agent" to Parameter().apply {
`in` = "header"
name = "User-Agent"
})
}
}

val headers = api.getAllHeaders()

assertThat(headers).isNotNull
assertThat(headers).hasSize(1)
}

@Test
fun `getAllHeaders should return headers from paths`() {
val api = OpenAPI().apply {
val paths = Paths()
val pathItem = PathItem()
pathItem.get = Operation().apply {
parameters = listOf(Parameter().apply {
`in` = "header"
name = "User-Agent"
})
}
paths.addPathItem("path", pathItem)

this.paths = paths
}

val headers = api.getAllHeaders()

assertThat(headers).isNotNull
assertThat(headers).hasSize(1)
}

@Test
fun `getAllHeaders should return headers from components`() {
val api = OpenAPI().apply {
components = Components().apply {
headers = mapOf("User-Agent" to Header())
}
}

val headers = api.getAllHeaders()

assertThat(headers).isNotNull
assertThat(headers).hasSize(1)
}

@Test
fun `getAllHeaders should return empty set if no headers specified`() {
val api = OpenAPI()

val headers = api.getAllHeaders()

assertThat(headers).isNotNull
assertThat(headers).isEmpty()
}

@Test
fun `getAllSchemas should return component schemas`() {
val api = OpenAPI().apply {
components = Components().apply {
schemas = mapOf("pet" to Schema<String>())
}
}

val schemas = api.getAllSchemas()

assertThat(schemas).isNotNull
assertThat(schemas).hasSize(1)
}

@Test
fun `getAllSchemas should return component response schemas`() {
val api = OpenAPI().apply {
components = Components().apply {
responses = mapOf("response" to ApiResponse().apply {
schemas = mapOf("pet" to Schema<String>())
})
}
}

val schemas = api.getAllSchemas()

assertThat(schemas).isNotNull
assertThat(schemas).hasSize(1)
}

@Test
fun `getAllSchemas should return component request body schemas`() {
val api = OpenAPI().apply {
components = Components().apply {
requestBodies = mapOf("request" to RequestBody().apply {
schemas = mapOf("pet" to Schema<String>())
})
}
}

val schemas = api.getAllSchemas()

assertThat(schemas).isNotNull
assertThat(schemas).hasSize(1)
}

@Test
fun `getAllSchemas should return operation parameter schemas`() {
val api = OpenAPI().apply {
val paths = Paths()
val pathItem = PathItem()
pathItem.get = Operation().apply {
parameters = listOf(Parameter().apply {
`in` = "body"
schema = Schema<String>()
})
}
paths.addPathItem("path", pathItem)
this.paths = paths
}

val schemas = api.getAllSchemas()

assertThat(schemas).isNotNull
assertThat(schemas).hasSize(1)
}

@Test
fun `getAllSchemas should return operation response schemas`() {
val api = OpenAPI().apply {
val paths = Paths()
val pathItem = PathItem()
pathItem.get = Operation().apply {
val responses = ApiResponses()
responses.addApiResponse("response", ApiResponse().apply {
val content = Content()
content.addMediaType("application/json", MediaType().apply {
schema = Schema<String>()
})
this.content = content
})
this.responses = responses
}
paths.addPathItem("path", pathItem)
this.paths = paths
}

val schemas = api.getAllSchemas()

assertThat(schemas).isNotNull
assertThat(schemas).hasSize(1)
}

@Test
fun `getAllSchemas should return operation request body schemas`() {
val api = OpenAPI().apply {
val paths = Paths()
val pathItem = PathItem()
pathItem.get = Operation().apply {
requestBody = RequestBody().apply {
val content = Content()
content.addMediaType("application/json", MediaType().apply {
schema = Schema<String>()
})
this.content = content
}
}
paths.addPathItem("path", pathItem)
this.paths = paths
}

val schemas = api.getAllSchemas()

assertThat(schemas).isNotNull
assertThat(schemas).hasSize(1)
}

@Test
fun `getAllSchemas should return an empty collection if no schemas are specified`() {
val api = OpenAPI()

val schemas = api.getAllSchemas()

assertThat(schemas).isNotNull
assertThat(schemas).isEmpty()
}

@Test
fun `getAllTransitiveSchemas should return a set of all schemas`() {
val api = OpenAPI().apply {
components = Components().apply {
schemas = mapOf("pet" to Schema<Any>().apply {
title = "pet"
properties = mapOf(
"name" to Schema<String>().apply { title = "name" },
"age" to Schema<Int>().apply { title = "age" }
)
})
}
}

val schemas = api.getAllTransitiveSchemas()

assertThat(schemas).isNotNull
assertThat(schemas).hasSize(2)
}

@Test
fun `getAllTransitiveSchemas should return an empty set if no schemas are specified`() {
val api = OpenAPI()

val schemas = api.getAllTransitiveSchemas()

assertThat(schemas).isNotNull
assertThat(schemas).isEmpty()
}

@Test
fun `getAllProperties should return a map of all properties`() {
val api = OpenAPI().apply {
components = Components().apply {
schemas = mapOf("pet" to Schema<Any>().apply {
title = "pet"
properties = mapOf(
"name" to Schema<String>().apply { title = "name" },
"age" to Schema<Int>().apply { title = "age" }
)
})
}
}

val schemas = api.getAllProperties()

assertThat(schemas).isNotNull
assertThat(schemas).hasSize(2)
}

@Test
fun `getAllProperties should return an empty set if no properties are specified`() {
val api = OpenAPI()

val schemas = api.getAllProperties()

assertThat(schemas).isNotNull
assertThat(schemas).isEmpty()
}

@Test
fun `getAllParameters should return components parameters`() {
val api = OpenAPI().apply {
components = Components().apply {
parameters = mapOf("pet" to Parameter().apply {
name = "name"
})
}
}

val schemas = api.getAllParameters()

assertThat(schemas).isNotNull
assertThat(schemas).hasSize(1)
}

@Test
fun `getAllParameters should return paths parameters`() {
val api = OpenAPI().apply {
val paths = Paths()
val pathItem = PathItem()
pathItem.parameters = listOf(Parameter().apply {
name = "name"
})
paths.addPathItem("path", pathItem)
this.paths = paths
}

val schemas = api.getAllParameters()

assertThat(schemas).isNotNull
assertThat(schemas).hasSize(1)
}

@Test
fun `getAllHeaders should return headers from compoents`() {
fun `getAllParameters should return path operation parameters`() {
val api = OpenAPI().apply {
val paths = Paths()
val pathItem = PathItem()
pathItem.get = Operation().apply {
parameters = listOf(Parameter().apply {
name = "name"
})
}
paths.addPathItem("path", pathItem)
this.paths = paths
}

val schemas = api.getAllParameters()

assertThat(schemas).isNotNull
assertThat(schemas).hasSize(1)
}

@Test
fun `getAllHeaders should `() {
fun `getAllParameters should return an empty map if no parameters are specified`() {
val api = OpenAPI()

val schemas = api.getAllParameters()

assertThat(schemas).isNotNull
assertThat(schemas).isEmpty()
}
}

0 comments on commit 01c6924

Please sign in to comment.