Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
blockvote committed Jun 3, 2024
1 parent 02561fd commit 1ecb2ff
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import org.assertj.core.api.BDDAssertions.thenThrownBy
import org.junit.jupiter.api.Test

class OpenApiValidatorTest {

val testHandler = TestRequestHandler()

val validator = OpenApiValidator("openapi.yml")

@Test
fun `should handle and validate request`() {
val request = GET("/tests")
.withHeaders(mapOf("Accept" to "application/json"))
val request =
GET("/tests")
.withHeaders(mapOf("Accept" to "application/json"))

val response = testHandler.handleRequest(request, mockk())

Expand All @@ -29,8 +29,9 @@ class OpenApiValidatorTest {

@Test
fun `should fail on undocumented request`() {
val request = GET("/tests-not-documented")
.withHeaders(mapOf("Accept" to "application/json"))
val request =
GET("/tests-not-documented")
.withHeaders(mapOf("Accept" to "application/json"))

val response = testHandler.handleRequest(request, mockk())

Expand All @@ -40,37 +41,39 @@ class OpenApiValidatorTest {

@Test
fun `should fail on invalid schema`() {
val request = GET("/tests")
.withHeaders(mapOf("Accept" to "application/json"))
val request =
GET("/tests")
.withHeaders(mapOf("Accept" to "application/json"))

val response = TestInvalidRequestHandler()
.handleRequest(request, mockk())
val response =
TestInvalidRequestHandler()
.handleRequest(request, mockk())

thenThrownBy { validator.assertValid(request, response) }.isInstanceOf(OpenApiValidator.ApiInteractionInvalid::class.java)
}

class TestRequestHandler : RequestHandler() {

data class TestResponse(val name: String)

override val router = Router.router {
GET("/tests") { _: Request<Unit> ->
ResponseEntity.ok(TestResponse("Hello"))
override val router =
Router.router {
GET("/tests") { _: Request<Unit> ->
ResponseEntity.ok(TestResponse("Hello"))
}
GET("/tests-not-documented") { _: Request<Unit> ->
ResponseEntity.ok(TestResponse("Hello"))
}
}
GET("/tests-not-documented") { _: Request<Unit> ->
ResponseEntity.ok(TestResponse("Hello"))
}
}
}

class TestInvalidRequestHandler : RequestHandler() {

data class TestResponseInvalid(val invalid: String)

override val router = Router.router {
GET("/tests") { _: Request<Unit> ->
ResponseEntity.ok(TestResponseInvalid("Hello"))
override val router =
Router.router {
GET("/tests") { _: Request<Unit> ->
ResponseEntity.ok(TestResponseInvalid("Hello"))
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import org.assertj.core.api.BDDAssertions.thenThrownBy
import org.junit.jupiter.api.Test

class ValidatingRequestRouterWrapperTest {

@Test
fun `should return response on successful validation`() {
val response = ValidatingRequestRouterWrapper(TestRequestHandler(), "openapi.yml")
.handleRequest(GET("/tests").withAcceptHeader("application/json"), mockk())
val response =
ValidatingRequestRouterWrapper(TestRequestHandler(), "openapi.yml")
.handleRequest(GET("/tests").withAcceptHeader("application/json"), mockk())

then(response.statusCode).isEqualTo(200)
}
Expand All @@ -43,8 +43,12 @@ class ValidatingRequestRouterWrapperTest {

@Test
fun `should skip validation`() {
val response = ValidatingRequestRouterWrapper(InvalidTestRequestHandler(), "openapi.yml")
.handleRequestSkippingRequestAndResponseValidation(GET("/path-not-documented").withAcceptHeader("application/json"), mockk())
val response =
ValidatingRequestRouterWrapper(InvalidTestRequestHandler(), "openapi.yml")
.handleRequestSkippingRequestAndResponseValidation(
GET("/path-not-documented").withAcceptHeader("application/json"),
mockk(),
)
then(response.statusCode).isEqualTo(404)
}

Expand All @@ -54,7 +58,7 @@ class ValidatingRequestRouterWrapperTest {
ValidatingRequestRouterWrapper(
delegate = OpenApiValidatorTest.TestRequestHandler(),
specUrlOrPayload = "openapi.yml",
additionalRequestValidationFunctions = listOf({ _ -> throw RequestValidationFailedException() })
additionalRequestValidationFunctions = listOf({ _ -> throw RequestValidationFailedException() }),
)
.handleRequest(GET("/tests").withAcceptHeader("application/json"), mockk())
}
Expand All @@ -67,29 +71,32 @@ class ValidatingRequestRouterWrapperTest {
ValidatingRequestRouterWrapper(
delegate = OpenApiValidatorTest.TestRequestHandler(),
specUrlOrPayload = "openapi.yml",
additionalResponseValidationFunctions = listOf({ _, _ -> throw ResponseValidationFailedException() })
additionalResponseValidationFunctions = listOf({ _, _ -> throw ResponseValidationFailedException() }),
)
.handleRequest(GET("/tests").withAcceptHeader("application/json"), mockk())
}
.isInstanceOf(ResponseValidationFailedException::class.java)
}

private class RequestValidationFailedException : RuntimeException("request validation failed")

private class ResponseValidationFailedException : RuntimeException("request validation failed")

private class TestRequestHandler : RequestHandler() {
override val router = router {
GET("/tests") { _: Request<Unit> ->
ResponseEntity.ok("""{"name": "some"}""")
override val router =
router {
GET("/tests") { _: Request<Unit> ->
ResponseEntity.ok("""{"name": "some"}""")
}
}
}
}

private class InvalidTestRequestHandler : RequestHandler() {
override val router = router {
GET("/tests") { _: Request<Unit> ->
ResponseEntity.notFound(Unit)
override val router =
router {
GET("/tests") { _: Request<Unit> ->
ResponseEntity.notFound(Unit)
}
}
}
}
}

0 comments on commit 1ecb2ff

Please sign in to comment.