|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.web.reactive.result.method.annotation |
| 18 | + |
| 19 | +import kotlinx.serialization.Serializable |
| 20 | +import org.assertj.core.api.Assertions |
| 21 | +import org.junit.jupiter.api.Test |
| 22 | +import org.springframework.core.ResolvableType |
| 23 | +import org.springframework.core.io.buffer.DataBuffer |
| 24 | +import org.springframework.http.MediaType |
| 25 | +import org.springframework.http.ResponseEntity |
| 26 | +import org.springframework.http.codec.EncoderHttpMessageWriter |
| 27 | +import org.springframework.http.codec.HttpMessageWriter |
| 28 | +import org.springframework.http.codec.json.Jackson2JsonEncoder |
| 29 | +import org.springframework.http.codec.json.KotlinSerializationJsonEncoder |
| 30 | +import org.springframework.util.ObjectUtils |
| 31 | +import org.springframework.web.bind.annotation.GetMapping |
| 32 | +import org.springframework.web.bind.annotation.RestController |
| 33 | +import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder |
| 34 | +import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest |
| 35 | +import org.springframework.web.testfixture.method.ResolvableMethod |
| 36 | +import org.springframework.web.testfixture.server.MockServerWebExchange |
| 37 | +import reactor.test.StepVerifier |
| 38 | +import java.nio.charset.StandardCharsets |
| 39 | +import java.time.Duration |
| 40 | +import java.util.* |
| 41 | + |
| 42 | +/** |
| 43 | + * Kotlin unit tests for {@link AbstractMessageWriterResultHandler}. |
| 44 | + * |
| 45 | + * @author Sebastien Deleuze |
| 46 | + */ |
| 47 | +class KotlinMessageWriterResultHandlerTests { |
| 48 | + |
| 49 | + private val resultHandler = initResultHandler() |
| 50 | + |
| 51 | + private val exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path")) |
| 52 | + |
| 53 | + |
| 54 | + private fun initResultHandler(vararg writers: HttpMessageWriter<*>): AbstractMessageWriterResultHandler { |
| 55 | + val writerList = if (ObjectUtils.isEmpty(writers)) { |
| 56 | + listOf( |
| 57 | + EncoderHttpMessageWriter(KotlinSerializationJsonEncoder()), |
| 58 | + EncoderHttpMessageWriter(Jackson2JsonEncoder()) |
| 59 | + ) |
| 60 | + } else { |
| 61 | + listOf(*writers) |
| 62 | + } |
| 63 | + val resolver = RequestedContentTypeResolverBuilder().build() |
| 64 | + return object : AbstractMessageWriterResultHandler(writerList, resolver) {} |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun nonSuspendWithoutResponseEntity() { |
| 69 | + val returnType = ResolvableMethod.on(SampleController::class.java) |
| 70 | + .resolveReturnType(List::class.java, Person::class.java) |
| 71 | + val body = listOf(Person(UserId(1), "John")) |
| 72 | + resultHandler.writeBody(body, returnType, exchange).block(Duration.ofSeconds(5)) |
| 73 | + |
| 74 | + Assertions.assertThat(exchange.response.headers.contentType).isEqualTo(MediaType.APPLICATION_JSON) |
| 75 | + assertResponseBody("[{\"userId\":1,\"name\":\"John\"}]") |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun nonSuspendWithResponseEntity() { |
| 80 | + val returnType = ResolvableMethod.on(SampleController::class.java) |
| 81 | + .returning(ResolvableType.forClassWithGenerics(ResponseEntity::class.java, |
| 82 | + ResolvableType.forClassWithGenerics(List::class.java, Person::class.java))) |
| 83 | + .build().returnType() |
| 84 | + val body = ResponseEntity.ok(listOf(Person(UserId(1), "John"))) |
| 85 | + resultHandler.writeBody(body.body, returnType.nested(), returnType, exchange).block(Duration.ofSeconds(5)) |
| 86 | + |
| 87 | + Assertions.assertThat(exchange.response.headers.contentType).isEqualTo(MediaType.APPLICATION_JSON) |
| 88 | + assertResponseBody("[{\"userId\":1,\"name\":\"John\"}]") |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun suspendWithoutResponseEntity() { |
| 93 | + val returnType = ResolvableMethod.on(CoroutinesSampleController::class.java) |
| 94 | + .resolveReturnType(List::class.java, Person::class.java) |
| 95 | + val body = listOf(Person(UserId(1), "John")) |
| 96 | + resultHandler.writeBody(body, returnType, exchange).block(Duration.ofSeconds(5)) |
| 97 | + |
| 98 | + Assertions.assertThat(exchange.response.headers.contentType).isEqualTo(MediaType.APPLICATION_JSON) |
| 99 | + assertResponseBody("[{\"userId\":1,\"name\":\"John\"}]") |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + fun suspendWithResponseEntity() { |
| 104 | + val returnType = ResolvableMethod.on(CoroutinesSampleController::class.java) |
| 105 | + .returning(ResolvableType.forClassWithGenerics(ResponseEntity::class.java, |
| 106 | + ResolvableType.forClassWithGenerics(List::class.java, Person::class.java))) |
| 107 | + .build().returnType() |
| 108 | + val body = ResponseEntity.ok(listOf(Person(UserId(1), "John"))) |
| 109 | + resultHandler.writeBody(body.body, returnType.nested(), returnType, exchange).block(Duration.ofSeconds(5)) |
| 110 | + |
| 111 | + Assertions.assertThat(exchange.response.headers.contentType).isEqualTo(MediaType.APPLICATION_JSON) |
| 112 | + assertResponseBody("[{\"userId\":1,\"name\":\"John\"}]") |
| 113 | + } |
| 114 | + |
| 115 | + private fun assertResponseBody(responseBody: String) { |
| 116 | + StepVerifier.create(exchange.response.body) |
| 117 | + .consumeNextWith { buf: DataBuffer -> |
| 118 | + Assertions.assertThat( |
| 119 | + buf.toString(StandardCharsets.UTF_8) |
| 120 | + ).isEqualTo(responseBody) |
| 121 | + } |
| 122 | + .expectComplete() |
| 123 | + .verify() |
| 124 | + } |
| 125 | + |
| 126 | + @RestController |
| 127 | + class SampleController { |
| 128 | + |
| 129 | + @GetMapping("/non-suspend-with-response-entity") |
| 130 | + fun withResponseEntity(): ResponseEntity<List<Person>> = |
| 131 | + TODO() |
| 132 | + |
| 133 | + @GetMapping("/non-suspend-without-response-entity") |
| 134 | + fun withoutResponseEntity(): List<Person> = |
| 135 | + TODO() |
| 136 | + } |
| 137 | + |
| 138 | + @RestController |
| 139 | + class CoroutinesSampleController { |
| 140 | + |
| 141 | + @GetMapping("/suspend-with-response-entity") |
| 142 | + suspend fun suspendAndResponseEntity(): ResponseEntity<List<Person>> = |
| 143 | + TODO() |
| 144 | + |
| 145 | + @GetMapping("/suspend-without-response-entity") |
| 146 | + suspend fun suspendWithoutResponseEntity(): List<Person> = |
| 147 | + TODO() |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + @Serializable |
| 152 | + data class Person( |
| 153 | + val userId: UserId, |
| 154 | + val name: String, |
| 155 | + ) |
| 156 | + |
| 157 | + @JvmInline |
| 158 | + @Serializable |
| 159 | + value class UserId(val id: Int) |
| 160 | + |
| 161 | +} |
0 commit comments