Skip to content

Commit b62bb53

Browse files
quaffmp911de
authored andcommitted
Add more integration tests for PageSerializationMode configuration.
Closes #3028
1 parent 09ea442 commit b62bb53

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java

+78
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@
2020
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
2121

2222
import java.util.Arrays;
23+
import java.util.List;
2324

25+
import com.fasterxml.jackson.databind.Module;
2426
import org.junit.jupiter.api.Test;
27+
import org.springframework.beans.factory.annotation.Autowired;
2528
import org.springframework.context.ApplicationContext;
2629
import org.springframework.context.annotation.Bean;
2730
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.context.annotation.Primary;
2832
import org.springframework.core.convert.ConversionService;
2933
import org.springframework.data.classloadersupport.HidingClassLoader;
3034
import org.springframework.data.geo.Distance;
@@ -40,9 +44,13 @@
4044
import org.springframework.data.web.WebTestUtils;
4145
import org.springframework.data.web.config.SpringDataJacksonConfiguration.PageModule;
4246
import org.springframework.hateoas.Link;
47+
import org.springframework.http.converter.HttpMessageConverter;
48+
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
49+
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
4350
import org.springframework.test.util.ReflectionTestUtils;
4451
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
4552
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
53+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
4654
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
4755
import org.springframework.web.util.UriComponentsBuilder;
4856

@@ -114,6 +122,43 @@ SimpleEntityPathResolver entityPathResolver() {
114122
}
115123
}
116124

125+
@Configuration
126+
static class PageSampleConfig extends WebMvcConfigurationSupport {
127+
128+
@Autowired
129+
private List<Module> modules;
130+
131+
@Bean
132+
PageSampleController controller() {
133+
return new PageSampleController();
134+
}
135+
136+
@Override
137+
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
138+
Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json().modules(modules);
139+
converters.add(0, new MappingJackson2HttpMessageConverter(builder.build()));
140+
}
141+
}
142+
143+
144+
@EnableSpringDataWebSupport
145+
static class PageSampleConfigWithDirect extends PageSampleConfig {
146+
}
147+
148+
@EnableSpringDataWebSupport(pageSerializationMode = EnableSpringDataWebSupport.PageSerializationMode.VIA_DTO)
149+
static class PageSampleConfigWithViaDto extends PageSampleConfig {
150+
}
151+
152+
@EnableSpringDataWebSupport
153+
static class PageSampleConfigWithSpringDataWebSettings extends PageSampleConfig {
154+
155+
@Primary
156+
@Bean
157+
SpringDataWebSettings SpringDataWebSettings() {
158+
return new SpringDataWebSettings(EnableSpringDataWebSupport.PageSerializationMode.VIA_DTO);
159+
}
160+
}
161+
117162
@Test // DATACMNS-330
118163
void registersBasicBeanDefinitions() throws Exception {
119164

@@ -273,6 +318,39 @@ void registersSpringDataWebSettingsBean() {
273318
});
274319
}
275320

321+
@Test // GH-3024
322+
void usesDirectPageSerializationMode() throws Exception {
323+
324+
var applicationContext = WebTestUtils.createApplicationContext(PageSampleConfigWithDirect.class);
325+
var mvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
326+
327+
mvc.perform(post("/page")).//
328+
andExpect(status().isOk()).//
329+
andExpect(jsonPath("$.pageable").exists());
330+
}
331+
332+
@Test // GH-3024
333+
void usesViaDtoPageSerializationMode() throws Exception {
334+
335+
var applicationContext = WebTestUtils.createApplicationContext(PageSampleConfigWithViaDto.class);
336+
var mvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
337+
338+
mvc.perform(post("/page")).//
339+
andExpect(status().isOk()).//
340+
andExpect(jsonPath("$.page").exists());
341+
}
342+
343+
@Test // GH-3024
344+
void overridesPageSerializationModeByCustomizingSpringDataWebSettings() throws Exception {
345+
346+
var applicationContext = WebTestUtils.createApplicationContext(PageSampleConfigWithSpringDataWebSettings.class);
347+
var mvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
348+
349+
mvc.perform(post("/page")).//
350+
andExpect(status().isOk()).//
351+
andExpect(jsonPath("$.page").exists());
352+
}
353+
276354
private static void assertResolversRegistered(ApplicationContext context, Class<?>... resolverTypes) {
277355

278356
var adapter = context.getBean(RequestMappingHandlerAdapter.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2015-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+
package org.springframework.data.web.config;
17+
18+
import org.springframework.data.domain.Page;
19+
import org.springframework.data.domain.PageImpl;
20+
import org.springframework.data.domain.Pageable;
21+
import org.springframework.web.bind.annotation.RequestMapping;
22+
import org.springframework.web.bind.annotation.RestController;
23+
24+
import java.util.List;
25+
26+
/**
27+
* @author Yanming Zhou
28+
*/
29+
@RestController
30+
class PageSampleController {
31+
32+
@RequestMapping("/page")
33+
Page<String> page() {
34+
return new PageImpl<>(List.of("a", "b", "c"), Pageable.ofSize(10).withPage(0), 3);
35+
}
36+
}

0 commit comments

Comments
 (0)