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 6d7fb5a commit c4430d6
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import org.assertj.core.api.BDDAssertions.then
import org.junit.jupiter.api.Test

class ProtoBufUtilsTest {

@Test
fun `should serialize empty list`() {
val message = ComplexSample.newBuilder()
.addAllSamples(emptyList())
.build()
val message =
ComplexSample.newBuilder()
.addAllSamples(emptyList())
.build()

val json = ProtoBufUtils.toJsonWithoutWrappers(message)

Expand All @@ -22,9 +22,10 @@ class ProtoBufUtilsTest {

@Test
fun `should remove wrapper object`() {
val message = ComplexSample.newBuilder()
.setSomeString(StringValue.newBuilder().setValue("some").build())
.build()
val message =
ComplexSample.newBuilder()
.setSomeString(StringValue.newBuilder().setValue("some").build())
.build()

val json = ProtoBufUtils.toJsonWithoutWrappers(message)

Expand All @@ -33,9 +34,10 @@ class ProtoBufUtilsTest {

@Test
fun `should serialize value when it is the default`() {
val message = ComplexSample.newBuilder()
.setEnumAttribute(ONE) // enum zero value
.build()
val message =
ComplexSample.newBuilder()
.setEnumAttribute(ONE) // enum zero value
.build()

val json = ProtoBufUtils.toJsonWithoutWrappers(message)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

internal class ProtoDeserializationHandlerTest {

@Test
fun `Deserializer should not support if the content type of the input is null`() {
assertFalse(ProtoDeserializationHandler().supports(APIGatewayProxyRequestEvent()))
Expand All @@ -20,7 +19,13 @@ internal class ProtoDeserializationHandlerTest {

@Test
fun `Deserializer should support if the content type of the input is protobuf`() {
assertTrue(ProtoDeserializationHandler().supports(APIGatewayProxyRequestEvent().withHeader("content-type", "application/x-protobuf")))
assertTrue(ProtoDeserializationHandler().supports(APIGatewayProxyRequestEvent().withHeader("content-type", "application/vnd.moia.v1+x-protobuf")))
assertTrue(
ProtoDeserializationHandler().supports(APIGatewayProxyRequestEvent().withHeader("content-type", "application/x-protobuf")),
)
assertTrue(
ProtoDeserializationHandler().supports(
APIGatewayProxyRequestEvent().withHeader("content-type", "application/vnd.moia.v1+x-protobuf"),
),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,48 @@ import org.junit.jupiter.api.Test
import java.util.Base64

class RequestHandlerTest {

private val testRequestHandler = TestRequestHandler()

@Test
fun `should match request to proto handler and return json`() {
val response = testRequestHandler.handleRequest(
APIGatewayProxyRequestEvent()
.withPath("/some-proto")
.withHttpMethod("GET")
.withHeaders(mapOf("Accept" to "application/json")),
mockk()
)
val response =
testRequestHandler.handleRequest(
APIGatewayProxyRequestEvent()
.withPath("/some-proto")
.withHttpMethod("GET")
.withHeaders(mapOf("Accept" to "application/json")),
mockk(),
)

assertThat(response.statusCode).isEqualTo(200)
assertThat(response.body).isEqualTo("""{"hello":"Hello","request":""}""")
}

@Test
fun `should match request to proto handler with version accept header and return json`() {
val response = testRequestHandler.handleRequest(
APIGatewayProxyRequestEvent()
.withPath("/some-proto")
.withHttpMethod("GET")
.withHeaders(mapOf("Accept" to "application/vnd.moia.v1+json")),
mockk()
)
val response =
testRequestHandler.handleRequest(
APIGatewayProxyRequestEvent()
.withPath("/some-proto")
.withHttpMethod("GET")
.withHeaders(mapOf("Accept" to "application/vnd.moia.v1+json")),
mockk(),
)

assertThat(response.statusCode).isEqualTo(200)
assertThat(response.body).isEqualTo("""{"hello":"v1","request":""}""")
}

@Test
fun `should match request to proto handler and return proto`() {
val response = testRequestHandler.handleRequest(
APIGatewayProxyRequestEvent()
.withPath("/some-proto")
.withHttpMethod("GET")
.withHeaders(mapOf("Accept" to "application/x-protobuf")),
mockk()
)
val response =
testRequestHandler.handleRequest(
APIGatewayProxyRequestEvent()
.withPath("/some-proto")
.withHttpMethod("GET")
.withHeaders(mapOf("Accept" to "application/x-protobuf")),
mockk(),
)

assertThat(response.statusCode).isEqualTo(200)
assertThat(Sample.parseFrom(response.bodyAsBytes())).isEqualTo(Sample.newBuilder().setHello("Hello").setRequest("").build())
Expand All @@ -67,19 +69,20 @@ class RequestHandlerTest {
fun `should match request to proto handler and deserialize and return proto`() {
val request = Sample.newBuilder().setHello("Hello").setRequest("").build()

val response = testRequestHandler.handleRequest(
APIGatewayProxyRequestEvent()
.withPath("/some-proto")
.withHttpMethod("POST")
.withBody(Base64.getEncoder().encodeToString(request.toByteArray()))
.withHeaders(
mapOf(
"Accept" to "application/x-protobuf",
"Content-Type" to "application/x-protobuf"
)
),
mockk()
)
val response =
testRequestHandler.handleRequest(
APIGatewayProxyRequestEvent()
.withPath("/some-proto")
.withHttpMethod("POST")
.withBody(Base64.getEncoder().encodeToString(request.toByteArray()))
.withHeaders(
mapOf(
"Accept" to "application/x-protobuf",
"Content-Type" to "application/x-protobuf",
),
),
mockk(),
)

assertThat(response.statusCode).isEqualTo(200)
assertThat(Sample.parseFrom(response.bodyAsBytes())).isEqualTo(request)
Expand All @@ -88,31 +91,35 @@ class RequestHandlerTest {

@Test
fun `should return 406-unacceptable error in proto`() {
val response = testRequestHandler.handleRequest(
GET("/some-proto")
.withHeaders(
mapOf(
"Accept" to "text/plain"
)
),
mockk()
)
val response =
testRequestHandler.handleRequest(
GET("/some-proto")
.withHeaders(
mapOf(
"Accept" to "text/plain",
),
),
mockk(),
)

assertThat(response.statusCode).isEqualTo(406)
assertThat(io.moia.router.proto.sample.SampleOuterClass.ApiError.parseFrom(response.bodyAsBytes()).getCode()).isEqualTo("NOT_ACCEPTABLE")
assertThat(
io.moia.router.proto.sample.SampleOuterClass.ApiError.parseFrom(response.bodyAsBytes()).getCode(),
).isEqualTo("NOT_ACCEPTABLE")
}

@Test
fun `should return api error in protos`() {
val response = testRequestHandler.handleRequest(
GET("/some-error")
.withHeaders(
mapOf(
"Accept" to "application/x-protobuf"
)
),
mockk()
)
val response =
testRequestHandler.handleRequest(
GET("/some-error")
.withHeaders(
mapOf(
"Accept" to "application/x-protobuf",
),
),
mockk(),
)

assertThat(response.statusCode).isEqualTo(400)
with(io.moia.router.proto.sample.SampleOuterClass.ApiError.parseFrom(response.bodyAsBytes())) {
Expand All @@ -122,21 +129,21 @@ class RequestHandlerTest {
}

class TestRequestHandler : ProtoEnabledRequestHandler() {
override val router =
router {
defaultProducing = setOf("application/x-protobuf")
defaultConsuming = setOf("application/x-protobuf")

override val router = router {
defaultProducing = setOf("application/x-protobuf")
defaultConsuming = setOf("application/x-protobuf")

defaultContentType = "application/x-protobuf"
defaultContentType = "application/x-protobuf"

GET("/some-proto") { _: Request<Unit> -> ResponseEntity.ok(Sample.newBuilder().setHello("v1").build()) }
.producing("application/vnd.moia.v1+x-protobuf", "application/vnd.moia.v1+json")
GET("/some-proto") { _: Request<Unit> -> ResponseEntity.ok(Sample.newBuilder().setHello("v1").build()) }
.producing("application/vnd.moia.v1+x-protobuf", "application/vnd.moia.v1+json")

GET("/some-proto") { _: Request<Unit> -> ResponseEntity.ok(Sample.newBuilder().setHello("Hello").build()) }
.producing("application/x-protobuf", "application/json")
POST("/some-proto") { r: Request<Sample> -> ResponseEntity.ok(r.body) }
GET<Unit, Unit>("/some-error") { _: Request<Unit> -> throw ApiException("boom", "BOOM", 400) }
}
GET("/some-proto") { _: Request<Unit> -> ResponseEntity.ok(Sample.newBuilder().setHello("Hello").build()) }
.producing("application/x-protobuf", "application/json")
POST("/some-proto") { r: Request<Sample> -> ResponseEntity.ok(r.body) }
GET<Unit, Unit>("/some-error") { _: Request<Unit> -> throw ApiException("boom", "BOOM", 400) }
}

override fun createErrorBody(error: ApiError): Any =
io.moia.router.proto.sample.SampleOuterClass.ApiError.newBuilder()
Expand Down

0 comments on commit c4430d6

Please sign in to comment.