Skip to content

Commit

Permalink
WIP Make more test pass, but spp api still does not (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Dufour-Boivin committed Jul 5, 2018
1 parent b9d0981 commit 5448c96
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
Expand Up @@ -10,7 +10,6 @@ import io.swagger.v3.oas.models.Operation
import io.swagger.v3.oas.models.PathItem
import io.swagger.v3.oas.models.media.ArraySchema
import io.swagger.v3.oas.models.media.ComposedSchema
import io.swagger.v3.oas.models.media.ObjectSchema
import io.swagger.v3.oas.models.media.Schema
import io.swagger.v3.oas.models.responses.ApiResponse

Expand Down Expand Up @@ -79,14 +78,12 @@ class NoUnusedDefinitionsRule2 {
findAllSchemas(schema.items)
is ComposedSchema ->
findAllSchemas(schema)
is ObjectSchema -> {
else -> {
val self = if (includeSelf) listOf(schema) else emptyList()
val properties = schema.properties.orEmpty().values.flatMap(this::findAllSchemas)
val additionalProperties = findAllRefsFromProperties(schema.properties)
self + properties + additionalProperties
}
else ->
emptyList()
}

private fun findAllSchemas(schema: ComposedSchema): List<Schema<*>> =
Expand Down
12 changes: 5 additions & 7 deletions server/src/test/java/de/zalando/zally/TestUtil.kt
Expand Up @@ -5,11 +5,7 @@ import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory
import de.zalando.zally.rule.Context
import de.zalando.zally.rule.ObjectTreeReader
import io.swagger.models.ModelImpl
import io.swagger.models.Operation
import io.swagger.models.Path
import io.swagger.models.Response
import io.swagger.models.Swagger
import io.swagger.models.*
import io.swagger.models.parameters.HeaderParameter
import io.swagger.models.properties.StringProperty
import io.swagger.parser.SwaggerParser
Expand All @@ -26,9 +22,11 @@ val testConfig: Config by lazy {

fun getFixture(fileName: String): Swagger = SwaggerParser().read("fixtures/$fileName")

fun getContextFromFixture(fileName: String): Context? {
fun getContextFromFixture(fileName: String): Context {
val content = getResourceContent(fileName)
return Context.createOpenApiContext(content) ?: Context.createSwaggerContext(content)
return Context.createOpenApiContext(content)
?: Context.createSwaggerContext(content)
?: throw AssertionError("Could not generate a context for fixture `$fileName`.")
}

fun getResourceContent(fileName: String): String = ClasspathHelper.loadFileFromClasspath("fixtures/$fileName")
Expand Down
Expand Up @@ -120,7 +120,7 @@ class MediaTypesRuleTest {

@Test
fun `the SPP API generates violations`() {
val context = getContextFromFixture("api_spp.json")!!
val context = getContextFromFixture("api_spp.json")
val result = rule.validate(context)
assertThat(result).hasSameElementsAs(listOf(
// --- consumes ---
Expand All @@ -137,7 +137,7 @@ class MediaTypesRuleTest {

@Test
fun `the SPA API generates no violations`() {
val context = getContextFromFixture("api_spa.yaml")!!
val context = getContextFromFixture("api_spa.yaml")
assertThat(rule.validate(context)).isEmpty()
}

Expand Down
Expand Up @@ -2,45 +2,50 @@ package de.zalando.zally.rule.zally

import com.fasterxml.jackson.core.JsonPointer
import de.zalando.zally.getContextFromFixture
import de.zalando.zally.rule.Context
import de.zalando.zally.rule.api.Violation
import io.swagger.v3.oas.models.OpenAPI
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

class NoUnusedDefinitionsRule2Test {

@Test
fun positiveCase() {
val context = getContextFromFixture("unusedDefinitionsValid.json")!!
fun `positive case causes no violation`() {
val context = getContextFromFixture("unusedDefinitionsValid.json")
assertThat(rule.validate(context))
.isEmpty()
}

@Test
fun negativeCase() {
val results = rule.validate(getContextFromFixture("unusedDefinitionsInvalid.json")!!)
fun `negative case causes violations`() {
val results = rule.validate(getContextFromFixture("unusedDefinitionsInvalid.json"))
assertThat(results).hasSameElementsAs(listOf(
vSchema("/definitions/PetName"),
vParam("/parameters/FlowId")
))
}

// @Test
// fun emptySwaggerShouldPass() {
// val swagger = Swagger()
// assertThat(rule.validate(swagger)).isNull()
// }
//
// @Test
// fun positiveCaseSpp() {
// val swagger = getFixture("api_spp.json")
// assertThat(rule.validate(swagger)).isNull()
// }
//
// @Test
// fun positiveCaseTinbox() {
// val swagger = getFixture("api_tinbox.yaml")
// assertThat(rule.validate(swagger)).isNull()
// }
@Test
fun `empty specification causes no violation`() {
val context = Context(OpenAPI())
assertThat(rule.validate(context))
.isEmpty()
}

@Test
fun `the SPP API causes no violations`() {
val context = getContextFromFixture("api_spp.json")
assertThat(rule.validate(context))
.isEmpty()
}

@Test
fun `the tinbox API causes no violation`() {
val context = getContextFromFixture("api_tinbox.yaml")
assertThat(rule.validate(context))
.isEmpty()
}

private val rule = NoUnusedDefinitionsRule2()

Expand Down

0 comments on commit 5448c96

Please sign in to comment.