Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): add tests for OpenApiUtil class (#714)
- Loading branch information
1 parent
1d41093
commit 01c6924
Showing
2 changed files
with
316 additions
and
8 deletions.
There are no files selected for viewing
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
312 changes: 310 additions & 2 deletions
312
server/src/test/java/de/zalando/zally/util/OpenApiUtilTest.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 |
---|---|---|
@@ -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() | ||
} | ||
} |