Skip to content

Commit

Permalink
#714 convert one more test
Browse files Browse the repository at this point in the history
  • Loading branch information
David Dufour-Boivin committed Jun 27, 2018
1 parent 7cd0fd8 commit 71dd5e7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
Expand Up @@ -38,7 +38,7 @@ class MediaTypesRule {
fun validate(context: Context): List<Violation> =
context.api.paths.orEmpty().values.flatMap { path ->
path.readOperations().flatMap { operation ->
val consumedMediaViolations = operation.requestBody.content.orEmpty()
val consumedMediaViolations = operation.requestBody?.content.orEmpty()
.filter { (mediaType, _) -> this.isViolatingMediaType(mediaType) }
.map { context.violation(description, it) } // todo #714: Check if pointer still valid without the 2nd parameter
val producedMediaViolation = operation.responses.orEmpty().values
Expand Down
Expand Up @@ -4,14 +4,19 @@ import de.zalando.zally.getFixture
import de.zalando.zally.rule.Context
import de.zalando.zally.util.PatternUtil.isApplicationJsonOrProblemJson
import de.zalando.zally.util.PatternUtil.isCustomMediaTypeWithVersioning
import io.swagger.models.Operation
import io.swagger.models.Path
import io.swagger.models.Swagger
import io.swagger.parser.SwaggerParser
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.media.Content
import io.swagger.v3.oas.models.media.MediaType
import io.swagger.v3.oas.models.responses.ApiResponse
import io.swagger.v3.oas.models.responses.ApiResponses
import org.assertj.core.api.Assertions.assertThat
import org.intellij.lang.annotations.Language
import org.junit.Test
import io.swagger.models.Operation as v2Operation
import io.swagger.models.Path as v2Path

class MediaTypesRuleTest {

Expand Down Expand Up @@ -45,12 +50,6 @@ class MediaTypesRuleTest {
assertThat(isCustomMediaTypeWithVersioning("application/x.zalando.contract+json;")).isFalse()
}

// todo #714: Delete this when conversion to `Context` is over.
@Test
fun `DEPRECATED empty specification causes no violation`() {
assertThat(rule.validate(Swagger())).isNull()
}

@Test
fun `empty specification causes no violation`() {
val context = Context(OpenAPI())
Expand All @@ -59,11 +58,11 @@ class MediaTypesRuleTest {

@Test
fun `versioned custom media type causes no violation`() {
val swagger = swaggerWithMediaTypes(
val context = contextWithMediaTypes(
"/shipment-order/{shipment_order_id}" to listOf(
"application/x.zalando.contract+json;v=123",
"application/vnd.api+json;version=3"))
assertThat(rule.validate(swagger)).isNull()
assertThat(rule.validate(context)).isEmpty()
}

@Test
Expand Down Expand Up @@ -114,11 +113,28 @@ class MediaTypesRuleTest {
Swagger().apply {
paths = pathToMedia
.map { (path, types) ->
path to Path().apply {
this["get"] = Operation().apply { produces = types }
path to v2Path().apply {
this["get"] = v2Operation().apply { produces = types }
}
}
.toMap()
}

private fun contextWithMediaTypes(vararg pathToMedia: Pair<String, List<String>>): Context =
Context(OpenAPI().apply {
paths = pathToMedia.fold(Paths()) { paths, (path, types) ->
paths.addPathItem(path, PathItem().apply {
get = Operation().apply {
responses = ApiResponses().apply {
addApiResponse("200", ApiResponse().apply {
content = types.fold(Content()) { content, type ->
content.addMediaType(type, MediaType())
}
})
}
}
})
}
})

}

0 comments on commit 71dd5e7

Please sign in to comment.