Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…lities AvoidTrailingSlashesRule & test use Context and new convenience utility methods
- Loading branch information
Showing
10 changed files
with
221 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Set the default behavior, in case people don't have core.autocrlf set. | ||
* text=auto | ||
|
||
# Explicitly declare text files you want to always be normalized and converted | ||
# to native line endings on checkout. | ||
|
||
Dockerfile text | ||
|
||
*.conf text | ||
*.go text | ||
*.gradle text | ||
*.html text | ||
*.java text | ||
*.js text | ||
*.json text | ||
*.jsx text | ||
*.kt text | ||
*.lock text | ||
*.md text | ||
*.properties text | ||
*.pug text | ||
*.scss text | ||
*.sql text | ||
*.toml text | ||
*.txt text | ||
*.yaml text | ||
*.yml text | ||
|
||
# Declare files that will always have CRLF line endings on checkout. | ||
*.bat crlf eol=crlf | ||
|
||
# Declare files that will always have LF line endings on checkout. | ||
gradlew text eol=lf | ||
*.sh text eol=lf | ||
|
||
# Denote all files that are truly binary and should not be modified. | ||
*.ico binary | ||
*.jar binary | ||
*.jpg binary | ||
*.png binary |
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
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
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
33 changes: 33 additions & 0 deletions
33
server/src/test/java/de/zalando/zally/rule/ViolationAssert.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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package de.zalando.zally.rule | ||
|
||
import de.zalando.zally.rule.api.Violation | ||
import org.assertj.core.api.AbstractAssert | ||
import org.assertj.core.api.StringAssert | ||
|
||
@Suppress("UndocumentedPublicClass") | ||
class ViolationAssert(actual: Violation?) : AbstractAssert<ViolationAssert, Violation?>(actual, ViolationAssert::class.java) { | ||
|
||
fun descriptionEqualTo(description: String): ViolationAssert { | ||
description().isEqualTo(description) | ||
return this | ||
} | ||
|
||
fun descriptionMatches(description: String): ViolationAssert { | ||
description().matches(description) | ||
return this | ||
} | ||
|
||
private fun description() = StringAssert(actual?.description).`as`("description") | ||
|
||
fun pointerEqualTo(pointer: String): ViolationAssert { | ||
pointer().isEqualTo(pointer) | ||
return this | ||
} | ||
|
||
fun pointerMatches(pointer: String): ViolationAssert { | ||
pointer().matches(pointer) | ||
return this | ||
} | ||
|
||
private fun pointer() = StringAssert(actual?.pointer?.toString()).`as`("pointer") | ||
} |
35 changes: 35 additions & 0 deletions
35
server/src/test/java/de/zalando/zally/rule/ViolationsAssert.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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package de.zalando.zally.rule | ||
|
||
import de.zalando.zally.rule.api.Violation | ||
import org.assertj.core.api.AbstractListAssert | ||
import org.assertj.core.api.ListAssert | ||
import org.assertj.core.api.ObjectAssert | ||
|
||
@Suppress("UndocumentedPublicClass", "SpreadOperator") | ||
class ViolationsAssert(violations: List<Violation>?) : AbstractListAssert<ViolationsAssert, List<Violation>, Violation, ObjectAssert<Violation>>(violations, ViolationsAssert::class.java) { | ||
|
||
override fun toAssert(value: Violation?, description: String?): ObjectAssert<Violation> { | ||
return ObjectAssert<Violation>(value).`as`(description) | ||
} | ||
|
||
fun descriptionsAllEqualTo(description: String): ViolationsAssert { | ||
descriptions().containsOnly(description) | ||
return this | ||
} | ||
|
||
fun descriptionsEqualTo(vararg descriptions: String): ViolationsAssert { | ||
descriptions().containsExactly(*descriptions) | ||
return this | ||
} | ||
|
||
private fun descriptions(): ListAssert<String> { | ||
isNotNull | ||
return ListAssert(actual.map { it.description }).`as`("descriptions") | ||
} | ||
|
||
fun pointersEqualTo(vararg pointers: String): ViolationsAssert { | ||
isNotNull | ||
ListAssert(actual.map { it.pointer?.toString() }).`as`("pointers").containsExactly(*pointers) | ||
return this | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
server/src/test/java/de/zalando/zally/rule/ZallyAssertions.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package de.zalando.zally.rule | ||
|
||
import de.zalando.zally.rule.api.Violation | ||
|
||
@Suppress("UndocumentedPublicClass") | ||
class ZallyAssertions { | ||
companion object { | ||
|
||
fun assertThat(actual: Violation?): ViolationAssert = ViolationAssert(actual) | ||
|
||
fun assertThat(actual: List<Violation>?): ViolationsAssert = ViolationsAssert(actual) | ||
} | ||
} |
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
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
43 changes: 34 additions & 9 deletions
43
server/src/test/java/de/zalando/zally/rule/zalando/AvoidTrailingSlashesRuleTest.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,29 +1,54 @@ | ||
package de.zalando.zally.rule.zalando | ||
|
||
import de.zalando.zally.swaggerWithPaths | ||
import io.swagger.models.Swagger | ||
import org.assertj.core.api.Assertions.assertThat | ||
import de.zalando.zally.rule.Context | ||
import de.zalando.zally.rule.ZallyAssertions.Companion.assertThat | ||
import org.junit.Test | ||
|
||
@Suppress("StringLiteralDuplication", "UndocumentedPublicClass", "UnsafeCallOnNullableType") | ||
class AvoidTrailingSlashesRuleTest { | ||
|
||
private val rule = AvoidTrailingSlashesRule() | ||
|
||
@Test | ||
fun emptySwagger() { | ||
assertThat(rule.validate(Swagger())).isNull() | ||
val context = Context.createOpenApiContext(""" | ||
openapi: '3.0.0' | ||
""".trimIndent())!! | ||
|
||
val violations = rule.validate(context) | ||
|
||
assertThat(violations).isEmpty() | ||
} | ||
|
||
@Test | ||
fun positiveCase() { | ||
val testAPI = swaggerWithPaths("/api/test-api") | ||
assertThat(rule.validate(testAPI)).isNull() | ||
val context = Context.createOpenApiContext(""" | ||
openapi: '3.0.0' | ||
paths: | ||
/api/test-api: {} | ||
""".trimIndent())!! | ||
|
||
val violations = rule.validate(context) | ||
|
||
assertThat(violations).isEmpty() | ||
} | ||
|
||
@Test | ||
fun negativeCase() { | ||
val testAPI = swaggerWithPaths("/api/test-api/", "/api/test", "/some/other/path", "/long/bad/path/with/slash/") | ||
assertThat(rule.validate(testAPI)!!.paths).hasSameElementsAs( | ||
listOf("/api/test-api/", "/long/bad/path/with/slash/")) | ||
val context = Context.createOpenApiContext(""" | ||
openapi: '3.0.0' | ||
paths: | ||
/api/test-api/: {} | ||
/api/test: {} | ||
/some/other/path: {} | ||
/long/bad/path/with/slash/: {} | ||
""".trimIndent())!! | ||
|
||
val violations = rule.validate(context) | ||
|
||
assertThat(violations) | ||
.descriptionsAllEqualTo("Rule avoid trailing slashes is not followed") | ||
.pointersEqualTo("/paths/~1api~1test-api~1", "/paths/~1long~1bad~1path~1with~1slash~1") | ||
.hasSize(2) | ||
} | ||
} |