|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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.function |
| 18 | + |
| 19 | +import kotlinx.serialization.SerialName |
| 20 | +import kotlinx.serialization.Serializable |
| 21 | +import org.junit.jupiter.api.BeforeEach |
| 22 | +import org.junit.jupiter.api.Test |
| 23 | +import org.springframework.core.ParameterizedTypeReference |
| 24 | +import org.springframework.http.codec.EncoderHttpMessageWriter |
| 25 | +import org.springframework.http.codec.HttpMessageWriter |
| 26 | +import org.springframework.http.codec.json.KotlinSerializationJsonEncoder |
| 27 | +import org.springframework.http.server.reactive.ServerHttpRequest |
| 28 | +import org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse |
| 29 | +import reactor.test.StepVerifier |
| 30 | +import java.util.* |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Sebastien Deleuze |
| 34 | + */ |
| 35 | +class KotlinBodyInsertersTests { |
| 36 | + |
| 37 | + private lateinit var context: BodyInserter.Context |
| 38 | + |
| 39 | + private lateinit var hints: Map<String, Any> |
| 40 | + |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + fun createContext() { |
| 44 | + val messageWriters: MutableList<HttpMessageWriter<*>> = ArrayList() |
| 45 | + val jsonEncoder = KotlinSerializationJsonEncoder() |
| 46 | + messageWriters.add(EncoderHttpMessageWriter(jsonEncoder)) |
| 47 | + |
| 48 | + this.context = object : BodyInserter.Context { |
| 49 | + override fun messageWriters(): List<HttpMessageWriter<*>> { |
| 50 | + return messageWriters |
| 51 | + } |
| 52 | + |
| 53 | + override fun serverRequest(): Optional<ServerHttpRequest> { |
| 54 | + return Optional.empty() |
| 55 | + } |
| 56 | + |
| 57 | + override fun hints(): Map<String, Any> { |
| 58 | + return hints |
| 59 | + } |
| 60 | + } |
| 61 | + this.hints = HashMap() |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun ofObjectWithBodyType() { |
| 66 | + val somebody = SomeBody(1, "name") |
| 67 | + val body = listOf(somebody) |
| 68 | + val inserter = BodyInserters.fromValue(body, object: ParameterizedTypeReference<List<SomeBody>>() {}) |
| 69 | + val response = MockServerHttpResponse() |
| 70 | + val result = inserter.insert(response, context) |
| 71 | + StepVerifier.create(result).expectComplete().verify() |
| 72 | + |
| 73 | + StepVerifier.create(response.bodyAsString) |
| 74 | + .expectNext("[{\"user_id\":1,\"name\":\"name\"}]") |
| 75 | + .expectComplete() |
| 76 | + .verify() |
| 77 | + } |
| 78 | + |
| 79 | + @Serializable |
| 80 | + data class SomeBody(@SerialName("user_id") val userId: Int, val name: String) |
| 81 | +} |
0 commit comments