Refine RequestResponseBodyMethodProcessorKotlinTests
Improve read tests and fix copyright. See gh-34992
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2025 the original author or authors.
|
* Copyright 2002-2025 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,16 +19,21 @@ package org.springframework.web.servlet.mvc.method.annotation
|
|||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import org.assertj.core.api.Assertions
|
import org.assertj.core.api.Assertions
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.springframework.core.MethodParameter
|
||||||
import org.springframework.http.converter.StringHttpMessageConverter
|
import org.springframework.http.converter.StringHttpMessageConverter
|
||||||
import org.springframework.http.converter.json.KotlinSerializationJsonHttpMessageConverter
|
import org.springframework.http.converter.json.KotlinSerializationJsonHttpMessageConverter
|
||||||
|
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean
|
||||||
|
import org.springframework.web.bind.WebDataBinder
|
||||||
import org.springframework.web.bind.annotation.RequestMapping
|
import org.springframework.web.bind.annotation.RequestMapping
|
||||||
import org.springframework.web.bind.annotation.ResponseBody
|
import org.springframework.web.bind.annotation.ResponseBody
|
||||||
|
import org.springframework.web.bind.support.WebDataBinderFactory
|
||||||
import org.springframework.web.context.request.NativeWebRequest
|
import org.springframework.web.context.request.NativeWebRequest
|
||||||
import org.springframework.web.context.request.ServletWebRequest
|
import org.springframework.web.context.request.ServletWebRequest
|
||||||
import org.springframework.web.method.HandlerMethod
|
import org.springframework.web.method.HandlerMethod
|
||||||
import org.springframework.web.method.support.ModelAndViewContainer
|
import org.springframework.web.method.support.ModelAndViewContainer
|
||||||
import org.springframework.web.testfixture.servlet.MockHttpServletRequest
|
import org.springframework.web.testfixture.servlet.MockHttpServletRequest
|
||||||
import org.springframework.web.testfixture.servlet.MockHttpServletResponse
|
import org.springframework.web.testfixture.servlet.MockHttpServletResponse
|
||||||
|
import java.nio.charset.StandardCharsets
|
||||||
import kotlin.reflect.jvm.javaMethod
|
import kotlin.reflect.jvm.javaMethod
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -44,6 +49,8 @@ class RequestResponseBodyMethodProcessorKotlinTests {
|
|||||||
|
|
||||||
private val request: NativeWebRequest = ServletWebRequest(servletRequest, servletResponse)
|
private val request: NativeWebRequest = ServletWebRequest(servletRequest, servletResponse)
|
||||||
|
|
||||||
|
private val factory = ValidatingBinderFactory()
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun writeWithKotlinSerializationJsonMessageConverter() {
|
fun writeWithKotlinSerializationJsonMessageConverter() {
|
||||||
val method = SampleController::writeMessage::javaMethod.get()!!
|
val method = SampleController::writeMessage::javaMethod.get()!!
|
||||||
@@ -79,33 +86,37 @@ class RequestResponseBodyMethodProcessorKotlinTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun readWithKotlinSerializationJsonMessageConverter() {
|
fun readWithKotlinSerializationJsonMessageConverter() {
|
||||||
val method = SampleController::readMessage::javaMethod.get()!!
|
val content = "{\"value\" : \"foo\"}"
|
||||||
val handlerMethod = HandlerMethod(SampleController(), method)
|
this.servletRequest.setContent(content.toByteArray(StandardCharsets.UTF_8))
|
||||||
val methodReturnType = handlerMethod.returnType
|
this.servletRequest.setContentType("application/json")
|
||||||
|
|
||||||
val converters = listOf(StringHttpMessageConverter(), KotlinSerializationJsonHttpMessageConverter())
|
val converters = listOf(StringHttpMessageConverter(), KotlinSerializationJsonHttpMessageConverter())
|
||||||
val processor = RequestResponseBodyMethodProcessor(converters, null, null)
|
val processor = RequestResponseBodyMethodProcessor(converters, null, null)
|
||||||
|
|
||||||
val returnValue: Any = SampleController().readMessage(Message("foo"))
|
val method = SampleController::readMessage::javaMethod.get()!!
|
||||||
processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request)
|
val methodParameter = MethodParameter(method, 0)
|
||||||
|
|
||||||
Assertions.assertThat(this.servletResponse.contentAsString).isEqualTo("foo")
|
val result = processor.resolveArgument(methodParameter, container, request, factory) as Message
|
||||||
|
|
||||||
|
Assertions.assertThat(result).isEqualTo(Message("foo"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
@Test
|
@Test
|
||||||
fun readGenericTypeWithKotlinSerializationJsonMessageConverter() {
|
fun readGenericTypeWithKotlinSerializationJsonMessageConverter() {
|
||||||
val method = SampleController::readMessages::javaMethod.get()!!
|
val content = "[{\"value\" : \"foo\"}, {\"value\" : \"bar\"}]"
|
||||||
val handlerMethod = HandlerMethod(SampleController(), method)
|
this.servletRequest.setContent(content.toByteArray(StandardCharsets.UTF_8))
|
||||||
val methodReturnType = handlerMethod.returnType
|
this.servletRequest.setContentType("application/json")
|
||||||
|
|
||||||
val converters = listOf(StringHttpMessageConverter(), KotlinSerializationJsonHttpMessageConverter())
|
val converters = listOf(StringHttpMessageConverter(), KotlinSerializationJsonHttpMessageConverter())
|
||||||
val processor = RequestResponseBodyMethodProcessor(converters, null, null)
|
val processor = RequestResponseBodyMethodProcessor(converters, null, null)
|
||||||
|
|
||||||
val returnValue: Any = SampleController().readMessages(listOf(Message("foo"), Message("bar")))
|
val method = SampleController::readMessages::javaMethod.get()!!
|
||||||
processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request)
|
val methodParameter = MethodParameter(method, 0)
|
||||||
|
|
||||||
Assertions.assertThat(this.servletResponse.contentAsString)
|
val result = processor.resolveArgument(methodParameter, container, request, factory) as List<Message>
|
||||||
.isEqualTo("foo bar")
|
|
||||||
|
Assertions.assertThat(result).containsExactly(Message("foo"), Message("bar"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -130,4 +141,15 @@ class RequestResponseBodyMethodProcessorKotlinTests {
|
|||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class Message(val value: String)
|
data class Message(val value: String)
|
||||||
|
|
||||||
|
private class ValidatingBinderFactory : WebDataBinderFactory {
|
||||||
|
override fun createBinder(request: NativeWebRequest, target: Any?, objectName: String): WebDataBinder {
|
||||||
|
val validator = LocalValidatorFactoryBean()
|
||||||
|
validator.afterPropertiesSet()
|
||||||
|
val dataBinder = WebDataBinder(target, objectName)
|
||||||
|
dataBinder.setValidator(validator)
|
||||||
|
return dataBinder
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user