Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): migrate meta information rule to context object to support openapi 3 #819

Merged
merged 3 commits into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package de.zalando.zally.rule.zalando

import de.zalando.zally.rule.api.Check
import de.zalando.zally.rule.api.Context
import de.zalando.zally.rule.api.Rule
import de.zalando.zally.rule.api.Severity
import de.zalando.zally.rule.api.Violation

@Rule(
ruleSet = ZalandoRuleSet::class,
id = "218",
severity = Severity.MUST,
title = "Contain API Meta Information"
)
class ApiMetaInformationRule {

private val versionRegex = """^\d+.\d+(.\d+)?""".toRegex()

@Check(severity = Severity.MUST)
fun checkInfoTitle(context: Context): Violation? =
if (context.api.info?.title.isNullOrBlank()) {
context.violation("Title has to be provided")
} else null

@Check(severity = Severity.MUST)
fun checkInfoDescription(context: Context): Violation? =
if (context.api.info?.description.isNullOrBlank()) {
context.violation("Description has to be provided")
} else null

@Check(severity = Severity.MUST)
fun checkInfoVersion(context: Context): Violation? {
val version = context.api.info?.version
return when {
version == null || version.isBlank() ->
context.violation("Version has to be provided")
!versionRegex.matches(version) ->
context.violation("Version has to follow the Semver rules")
else -> null
}
}

@Check(severity = Severity.MUST)
fun checkContactName(context: Context): Violation? =
if (context.api.info?.contact?.name.isNullOrBlank()) {
context.violation("Contact name has to be provided")
} else null

@Check(severity = Severity.MUST)
fun checkContactUrl(context: Context): Violation? =
if (context.api.info?.contact?.url.isNullOrBlank()) {
context.violation("Contact URL has to be provided")
} else null

@Check(severity = Severity.MUST)
fun checkContactEmail(context: Context): Violation? =
if (context.api.info?.contact?.email.isNullOrBlank()) {
context.violation("Contact e-mail has to be provided")
} else null
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package de.zalando.zally.rule.zalando

import de.zalando.zally.getOpenApiContextFromContent
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

class ApiMetaInformationRuleTest {

val rule = ApiMetaInformationRule()

@Test
fun `checkInfoTitle should return violation if title is not set`() {
val spec = """
openapi: 3.0.1
""".trimIndent()
val context = getOpenApiContextFromContent(spec)

val violation = rule.checkInfoTitle(context)

assertThat(violation).isNotNull
assertThat(violation!!.description).isEqualTo("Title has to be provided")
assertThat(violation.pointer.toString()).isEqualTo("/info/title")
}

@Test
fun `checkInfoTitle should return no violation if title is set`() {
val spec = """
openapi: 3.0.1
info:
title: Awesome API
""".trimIndent()
val context = getOpenApiContextFromContent(spec)

val violation = rule.checkInfoTitle(context)

assertThat(violation).isNull()
}

@Test
fun `checkInfoDescription should return violation if description is not set`() {
val spec = """
openapi: 3.0.1
""".trimIndent()
val context = getOpenApiContextFromContent(spec)

val violation = rule.checkInfoDescription(context)

assertThat(violation).isNotNull
assertThat(violation!!.description).isEqualTo("Description has to be provided")
assertThat(violation.pointer.toString()).isEqualTo("/info/description")
}

@Test
fun `checkInfoDescription should return no violation if description is set`() {
val spec = """
openapi: 3.0.1
info:
description: super awesome mega turbo laser API
""".trimIndent()
val context = getOpenApiContextFromContent(spec)

val violation = rule.checkInfoDescription(context)

assertThat(violation).isNull()
}

@Test
fun `checkInfoVersion should return violation if version is not set`() {
val spec = """
openapi: 3.0.1
""".trimIndent()
val context = getOpenApiContextFromContent(spec)

val violation = rule.checkInfoVersion(context)

assertThat(violation).isNotNull
assertThat(violation!!.description).isEqualTo("Version has to be provided")
assertThat(violation.pointer.toString()).isEqualTo("/info/version")
}

@Test
fun `checkInfoVersion should return violation if version doesn't follow the Semver rules`() {
val spec = """
openapi: 3.0.1
info:
version: alpha-beta-gamma-version.1.1.1.1.1
""".trimIndent()
val context = getOpenApiContextFromContent(spec)

val violation = rule.checkInfoVersion(context)

assertThat(violation).isNotNull
assertThat(violation!!.description).isEqualTo("Version has to follow the Semver rules")
assertThat(violation.pointer.toString()).isEqualTo("/info/version")
}

@Test
fun `checkInfoVersion should return no violation if version is set`() {
val spec = """
openapi: 3.0.1
info:
version: 1.0.0
""".trimIndent()
val context = getOpenApiContextFromContent(spec)

val violation = rule.checkInfoVersion(context)

assertThat(violation).isNull()
}

@Test
fun `checkContactName should return violation if contact name is not set`() {
val spec = """
openapi: 3.0.1
""".trimIndent()
val context = getOpenApiContextFromContent(spec)

val violation = rule.checkContactName(context)

assertThat(violation).isNotNull
assertThat(violation!!.description).isEqualTo("Contact name has to be provided")
assertThat(violation.pointer.toString()).isEqualTo("/info/contact/name")
}

@Test
fun `checkContactName should return no violation if contact name is set`() {
val spec = """
openapi: 3.0.1
info:
contact:
name: Awesome Team
""".trimIndent()
val context = getOpenApiContextFromContent(spec)

val violation = rule.checkContactName(context)

assertThat(violation).isNull()
}

@Test
fun `checkContactUrl should return violation if contact URL is not set`() {
val spec = """
openapi: 3.0.1
""".trimIndent()
val context = getOpenApiContextFromContent(spec)

val violation = rule.checkContactUrl(context)

assertThat(violation).isNotNull
assertThat(violation!!.description).isEqualTo("Contact URL has to be provided")
assertThat(violation.pointer.toString()).isEqualTo("/info/contact/url")
}

@Test
fun `checkContactUrl should return no violation if contact URL is set`() {
val spec = """
openapi: 3.0.1
info:
contact:
url: https://awesome-team.company.com
""".trimIndent()
val context = getOpenApiContextFromContent(spec)

val violation = rule.checkContactUrl(context)

assertThat(violation).isNull()
}

@Test
fun `checkContactEmail should return violation if contact e-mail is not set`() {
val spec = """
openapi: 3.0.1
""".trimIndent()
val context = getOpenApiContextFromContent(spec)

val violation = rule.checkContactEmail(context)

assertThat(violation).isNotNull
assertThat(violation!!.description).isEqualTo("Contact e-mail has to be provided")
assertThat(violation.pointer.toString()).isEqualTo("/info/contact/email")
}

@Test
fun `checkContactEmail should return no violation if contact e-mail is set`() {
val spec = """
openapi: 3.0.1
info:
contact:
email: awesome-team@company.com
""".trimIndent()
val context = getOpenApiContextFromContent(spec)

val violation = rule.checkContactEmail(context)

assertThat(violation).isNull()
}
}