Add more integration tests for PageSerializationMode configuration.
Closes #3028
This commit is contained in:
committed by
Mark Paluch
parent
09ea4426c5
commit
b62bb535f1
@@ -20,11 +20,15 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
|
||||
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.SortHandlerMethodArgumentResolver;
|
||||
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 @@ class EnableSpringDataWebSupportIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class PageSampleConfig extends WebMvcConfigurationSupport {
|
||||
|
||||
@Autowired
|
||||
private List<Module> modules;
|
||||
|
||||
@Bean
|
||||
PageSampleController controller() {
|
||||
return new PageSampleController();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configureMessageConverters(List<HttpMessageConverter<?>> 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 @@ class EnableSpringDataWebSupportIntegrationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
36
src/test/java/org/springframework/data/web/config/PageSampleController.java
Executable file
36
src/test/java/org/springframework/data/web/config/PageSampleController.java
Executable file
@@ -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<String> page() {
|
||||
return new PageImpl<>(List.of("a", "b", "c"), Pageable.ofSize(10).withPage(0), 3);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user