BAEL-3682 specify accept header to fix ambiguity of message converters in case XML converter added

This commit is contained in:
Yavuz Tas 2021-05-10 02:01:25 +02:00
parent 099b9d514f
commit e5c3f48f86
6 changed files with 56 additions and 56 deletions

View File

@ -5,7 +5,6 @@ import java.util.List;
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter; import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
@ -16,35 +15,25 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration @Configuration
public class WebConfig implements WebMvcConfigurer { public class WebConfig implements WebMvcConfigurer {
// @Override @Override
// public void configureMessageConverters(final List<HttpMessageConverter<?>> messageConverters) { public void configureMessageConverters(final List<HttpMessageConverter<?>> messageConverters) {
// messageConverters.add(new MappingJackson2HttpMessageConverter()); messageConverters.add(new MappingJackson2HttpMessageConverter());
// messageConverters.add(createXmlHttpMessageConverter()); messageConverters.add(createXmlHttpMessageConverter());
// } }
//
// private HttpMessageConverter<Object> createXmlHttpMessageConverter() {
// final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
//
// final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
// xstreamMarshaller.setAutodetectAnnotations(true);
// xmlConverter.setMarshaller(xstreamMarshaller);
// xmlConverter.setUnmarshaller(xstreamMarshaller);
//
// return xmlConverter;
// }
// Another possibility is to create a bean which will be automatically added to the Spring Boot Autoconfigurations /**
// @Bean * There is another possibility to add a message converter, see {@link ConverterExtensionsConfig}
// public HttpMessageConverter<Object> createXmlHttpMessageConverter() { */
// final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter(); private HttpMessageConverter<Object> createXmlHttpMessageConverter() {
// final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
// final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
// xstreamMarshaller.setAutodetectAnnotations(true); final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
// xmlConverter.setMarshaller(xstreamMarshaller); xstreamMarshaller.setAutodetectAnnotations(true);
// xmlConverter.setUnmarshaller(xstreamMarshaller); xmlConverter.setMarshaller(xstreamMarshaller);
// xmlConverter.setUnmarshaller(xstreamMarshaller);
// return xmlConverter;
// } return xmlConverter;
}
// Etags // Etags
@ -52,7 +41,8 @@ public class WebConfig implements WebMvcConfigurer {
// AbstractAnnotationConfigDispatcherServletInitializer#getServletFilters // AbstractAnnotationConfigDispatcherServletInitializer#getServletFilters
@Bean @Bean
public FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter() { public FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter() {
FilterRegistrationBean<ShallowEtagHeaderFilter> filterRegistrationBean = new FilterRegistrationBean<>( new ShallowEtagHeaderFilter()); FilterRegistrationBean<ShallowEtagHeaderFilter> filterRegistrationBean =
new FilterRegistrationBean<>(new ShallowEtagHeaderFilter());
filterRegistrationBean.addUrlPatterns("/foos/*"); filterRegistrationBean.addUrlPatterns("/foos/*");
filterRegistrationBean.setName("etagFilter"); filterRegistrationBean.setName("etagFilter");
return filterRegistrationBean; return filterRegistrationBean;

View File

@ -24,6 +24,7 @@ import com.google.common.net.HttpHeaders;
import io.restassured.RestAssured; import io.restassured.RestAssured;
import io.restassured.http.ContentType; import io.restassured.http.ContentType;
import io.restassured.response.Response; import io.restassured.response.Response;
import org.springframework.http.MediaType;
public abstract class AbstractBasicLiveTest<T extends Serializable> extends AbstractLiveTest<T> { public abstract class AbstractBasicLiveTest<T extends Serializable> extends AbstractLiveTest<T> {
@ -54,7 +55,8 @@ public abstract class AbstractBasicLiveTest<T extends Serializable> extends Abst
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() { public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
create(); create();
final Response response = RestAssured.get(getURL() + "?page=0&size=10"); final Response response = RestAssured.given()
.accept(MediaType.APPLICATION_JSON_VALUE).get(getURL() + "?page=0&size=10");
assertFalse(response.body().as(List.class).isEmpty()); assertFalse(response.body().as(List.class).isEmpty());
} }

View File

@ -11,13 +11,12 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
/** /**
* * We'll start the whole context, but not the server. We'll mock the REST calls instead.
* We'll start the whole context, but not the server. We'll mock the REST calls instead.
*
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
@ -31,16 +30,18 @@ public class FooControllerAppIntegrationTest {
private IFooDao fooDao; private IFooDao fooDao;
@Before @Before
public void setup(){ public void setup() {
this.fooDao.deleteAll(); this.fooDao.deleteAll();
} }
@Test @Test
public void whenFindPaginatedRequest_thenEmptyResponse() throws Exception { public void whenFindPaginatedRequest_thenEmptyResponse() throws Exception {
this.mockMvc.perform(get("/foos").param("page", "0") this.mockMvc.perform(get("/foos")
.param("size", "2")) .param("page", "0")
.andExpect(status().isOk()) .param("size", "2")
.andExpect(content().json("[]")); .accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().json("[]"));
} }
} }

View File

@ -97,7 +97,8 @@ public class FooControllerCustomEtagIntegrationTest {
.getResponse() .getResponse()
.getHeader(HttpHeaders.LOCATION); .getHeader(HttpHeaders.LOCATION);
ResultActions findOneResponse = this.mvc ResultActions findOneResponse = this.mvc
.perform(get(createdResourceUri + CUSTOM_ETAG_ENDPOINT_SUFFIX).contentType(MediaType.APPLICATION_JSON)); .perform(get(createdResourceUri + CUSTOM_ETAG_ENDPOINT_SUFFIX)
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON));
String etag = findOneResponse.andReturn().getResponse().getHeader(HttpHeaders.ETAG); String etag = findOneResponse.andReturn().getResponse().getHeader(HttpHeaders.ETAG);
Foo createdFoo = deserializeFoo(findOneResponse.andReturn().getResponse().getContentAsString()); Foo createdFoo = deserializeFoo(findOneResponse.andReturn().getResponse().getContentAsString());
createdFoo.setName("updated name"); createdFoo.setName("updated name");

View File

@ -20,6 +20,7 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
@ -54,10 +55,12 @@ public class FooControllerWebLayerIntegrationTest {
doNothing().when(publisher) doNothing().when(publisher)
.publishEvent(any(PaginatedResultsRetrievedEvent.class)); .publishEvent(any(PaginatedResultsRetrievedEvent.class));
this.mockMvc.perform(get("/foos").param("page", "0") this.mockMvc.perform(get("/foos")
.param("size", "2")) .param("page", "0")
.andExpect(status().isOk()) .param("size", "2")
.andExpect(jsonPath("$",Matchers.hasSize(1))); .accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.hasSize(1)));
} }
@Test @Test

View File

@ -11,6 +11,7 @@ import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ -68,7 +69,9 @@ public class FooPageableLiveTest extends AbstractBasicLiveTest<Foo> {
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() { public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
create(); create();
final Response response = RestAssured.get(getPageableURL() + "?page=0&size=10"); final Response response = RestAssured.given()
.accept(MediaType.APPLICATION_JSON_VALUE)
.get(getPageableURL() + "?page=0&size=10");
assertFalse(response.body().as(List.class).isEmpty()); assertFalse(response.body().as(List.class).isEmpty());
} }