Skip to content

Add more integration tests for PageSerializationMode configuration #3028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -114,6 +122,43 @@ SimpleEntityPathResolver entityPathResolver() {
}
}

@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 {

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}