From bb1c1969cec8eaefbb8acc6a2547ee4dfd0a7155 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Tue, 16 Jan 2024 09:10:31 +0800 Subject: [PATCH] Add more integration tests for PageSerializationMode configuration --- ...eSpringDataWebSupportIntegrationTests.java | 78 +++++++++++++++++++ .../data/web/config/PageSampleController.java | 36 +++++++++ 2 files changed, 114 insertions(+) create mode 100755 src/test/java/org/springframework/data/web/config/PageSampleController.java diff --git a/src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java b/src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java index 34b7330f4a..97f8f93057 100755 --- a/src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java +++ b/src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java @@ -20,11 +20,15 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import java.util.Arrays; +import java.util.List; +import com.fasterxml.jackson.databind.Module; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; import org.springframework.core.convert.ConversionService; import org.springframework.data.classloadersupport.HidingClassLoader; import org.springframework.data.geo.Distance; @@ -40,9 +44,13 @@ import org.springframework.data.web.WebTestUtils; import org.springframework.data.web.config.SpringDataJacksonConfiguration.PageModule; import org.springframework.hateoas.Link; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import org.springframework.web.util.UriComponentsBuilder; @@ -114,6 +122,43 @@ SimpleEntityPathResolver entityPathResolver() { } } + @Configuration + static class PageSampleConfig extends WebMvcConfigurationSupport { + + @Autowired + private List modules; + + @Bean + PageSampleController controller() { + return new PageSampleController(); + } + + @Override + protected void configureMessageConverters(List> converters) { + Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json().modules(modules); + converters.add(0, new MappingJackson2HttpMessageConverter(builder.build())); + } + } + + + @EnableSpringDataWebSupport + static class PageSampleConfigWithDirect extends PageSampleConfig { + } + + @EnableSpringDataWebSupport(pageSerializationMode = EnableSpringDataWebSupport.PageSerializationMode.VIA_DTO) + static class PageSampleConfigWithViaDto extends PageSampleConfig { + } + + @EnableSpringDataWebSupport + static class PageSampleConfigWithSpringDataWebSettings extends PageSampleConfig { + + @Primary + @Bean + SpringDataWebSettings SpringDataWebSettings() { + return new SpringDataWebSettings(EnableSpringDataWebSupport.PageSerializationMode.VIA_DTO); + } + } + @Test // DATACMNS-330 void registersBasicBeanDefinitions() throws Exception { @@ -273,6 +318,39 @@ void registersSpringDataWebSettingsBean() { }); } + @Test // GH-3024 + void usesDirectPageSerializationMode() throws Exception { + + var applicationContext = WebTestUtils.createApplicationContext(PageSampleConfigWithDirect.class); + var mvc = MockMvcBuilders.webAppContextSetup(applicationContext).build(); + + mvc.perform(post("/page")).// + andExpect(status().isOk()).// + andExpect(jsonPath("$.pageable").exists()); + } + + @Test // GH-3024 + void usesViaDtoPageSerializationMode() throws Exception { + + var applicationContext = WebTestUtils.createApplicationContext(PageSampleConfigWithViaDto.class); + var mvc = MockMvcBuilders.webAppContextSetup(applicationContext).build(); + + mvc.perform(post("/page")).// + andExpect(status().isOk()).// + andExpect(jsonPath("$.page").exists()); + } + + @Test // GH-3024 + void overridesPageSerializationModeByCustomizingSpringDataWebSettings() throws Exception { + + var applicationContext = WebTestUtils.createApplicationContext(PageSampleConfigWithSpringDataWebSettings.class); + var mvc = MockMvcBuilders.webAppContextSetup(applicationContext).build(); + + mvc.perform(post("/page")).// + andExpect(status().isOk()).// + andExpect(jsonPath("$.page").exists()); + } + private static void assertResolversRegistered(ApplicationContext context, Class... resolverTypes) { var adapter = context.getBean(RequestMappingHandlerAdapter.class); diff --git a/src/test/java/org/springframework/data/web/config/PageSampleController.java b/src/test/java/org/springframework/data/web/config/PageSampleController.java new file mode 100755 index 0000000000..bd0899944a --- /dev/null +++ b/src/test/java/org/springframework/data/web/config/PageSampleController.java @@ -0,0 +1,36 @@ +/* + * Copyright 2015-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.web.config; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +/** + * @author Yanming Zhou + */ +@RestController +class PageSampleController { + + @RequestMapping("/page") + Page page() { + return new PageImpl<>(List.of("a", "b", "c"), Pageable.ofSize(10).withPage(0), 3); + } +}