Commit 87dd1150 authored by Madhura Bhave's avatar Madhura Bhave

Refactor HypermediaAutoConfigurationTests

parent ac10ebba
...@@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.hateoas; ...@@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.hateoas;
import java.util.Optional; import java.util.Optional;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
...@@ -26,8 +25,7 @@ import org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguratio ...@@ -26,8 +25,7 @@ import org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguratio
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.hateoas.MediaTypes; import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.client.LinkDiscoverer; import org.springframework.hateoas.client.LinkDiscoverer;
...@@ -39,7 +37,6 @@ import org.springframework.hateoas.server.EntityLinks; ...@@ -39,7 +37,6 @@ import org.springframework.hateoas.server.EntityLinks;
import org.springframework.hateoas.server.mvc.TypeConstrainedMappingJackson2HttpMessageConverter; import org.springframework.hateoas.server.mvc.TypeConstrainedMappingJackson2HttpMessageConverter;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -50,78 +47,62 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -50,78 +47,62 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Roy Clarkson * @author Roy Clarkson
* @author Oliver Gierke * @author Oliver Gierke
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Madhura Bhave
*/ */
class HypermediaAutoConfigurationTests { class HypermediaAutoConfigurationTests {
private AnnotationConfigServletWebApplicationContext context; private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withUserConfiguration(BaseConfig.class);
@AfterEach
void close() {
if (this.context != null) {
this.context.close();
}
}
@Test @Test
void linkDiscoverersCreated() { void linkDiscoverersCreated() {
this.context = new AnnotationConfigServletWebApplicationContext(); this.contextRunner.run((context) -> {
this.context.setServletContext(new MockServletContext()); LinkDiscoverers discoverers = context.getBean(LinkDiscoverers.class);
this.context.register(BaseConfig.class); assertThat(discoverers).isNotNull();
this.context.refresh(); Optional<LinkDiscoverer> discoverer = discoverers.getLinkDiscovererFor(MediaTypes.HAL_JSON);
LinkDiscoverers discoverers = this.context.getBean(LinkDiscoverers.class); assertThat(discoverer).containsInstanceOf(HalLinkDiscoverer.class);
assertThat(discoverers).isNotNull(); });
Optional<LinkDiscoverer> discoverer = discoverers.getLinkDiscovererFor(MediaTypes.HAL_JSON);
assertThat(discoverer).containsInstanceOf(HalLinkDiscoverer.class);
} }
@Test @Test
void entityLinksCreated() { void entityLinksCreated() {
this.context = new AnnotationConfigServletWebApplicationContext(); this.contextRunner.run((context) -> {
this.context.setServletContext(new MockServletContext()); EntityLinks discoverers = context.getBean(EntityLinks.class);
this.context.register(BaseConfig.class); assertThat(discoverers).isNotNull();
this.context.refresh(); });
EntityLinks discoverers = this.context.getBean(EntityLinks.class);
assertThat(discoverers).isNotNull();
} }
@Test @Test
void doesBackOffIfEnableHypermediaSupportIsDeclaredManually() { void doesBackOffIfEnableHypermediaSupportIsDeclaredManually() {
this.context = new AnnotationConfigServletWebApplicationContext(); this.contextRunner.withUserConfiguration(EnableHypermediaSupportConfig.class)
this.context.setServletContext(new MockServletContext()); .withPropertyValues("spring.jackson.serialization.INDENT_OUTPUT:true")
this.context.register(EnableHypermediaSupportConfig.class, BaseConfig.class); .run((context) -> assertThat(context.getBeansOfType(HypermediaConfiguration.class)).isEmpty());
TestPropertyValues.of("spring.jackson.serialization.INDENT_OUTPUT:true").applyTo(this.context);
this.context.refresh();
assertThat(this.context.getBeansOfType(HypermediaConfiguration.class)).isEmpty();
} }
@Test @Test
void supportedMediaTypesOfTypeConstrainedConvertersIsCustomized() { void supportedMediaTypesOfTypeConstrainedConvertersIsCustomized() {
this.context = new AnnotationConfigServletWebApplicationContext(); this.contextRunner.run((context) -> {
this.context.setServletContext(new MockServletContext()); RequestMappingHandlerAdapter handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class);
this.context.register(BaseConfig.class); for (HttpMessageConverter<?> converter : handlerAdapter.getMessageConverters()) {
this.context.refresh(); if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) {
RequestMappingHandlerAdapter handlerAdapter = this.context.getBean(RequestMappingHandlerAdapter.class); assertThat(converter.getSupportedMediaTypes()).contains(MediaType.APPLICATION_JSON,
for (HttpMessageConverter<?> converter : handlerAdapter.getMessageConverters()) { MediaTypes.HAL_JSON);
if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) { }
assertThat(converter.getSupportedMediaTypes()).contains(MediaType.APPLICATION_JSON,
MediaTypes.HAL_JSON);
} }
} });
} }
@Test @Test
void customizationOfSupportedMediaTypesCanBeDisabled() { void customizationOfSupportedMediaTypesCanBeDisabled() {
this.context = new AnnotationConfigServletWebApplicationContext(); this.contextRunner.withPropertyValues("spring.hateoas.use-hal-as-default-json-media-type:false")
this.context.setServletContext(new MockServletContext()); .run((context) -> {
this.context.register(BaseConfig.class); RequestMappingHandlerAdapter handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class);
TestPropertyValues.of("spring.hateoas.use-hal-as-default-json-media-type:false").applyTo(this.context); for (HttpMessageConverter<?> converter : handlerAdapter.getMessageConverters()) {
this.context.refresh(); if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) {
RequestMappingHandlerAdapter handlerAdapter = this.context.getBean(RequestMappingHandlerAdapter.class); assertThat(converter.getSupportedMediaTypes()).containsExactly(MediaTypes.HAL_JSON);
for (HttpMessageConverter<?> converter : handlerAdapter.getMessageConverters()) { }
if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) { }
assertThat(converter.getSupportedMediaTypes()).containsExactly(MediaTypes.HAL_JSON); });
}
}
} }
@ImportAutoConfiguration({ HttpMessageConvertersAutoConfiguration.class, WebMvcAutoConfiguration.class, @ImportAutoConfiguration({ HttpMessageConvertersAutoConfiguration.class, WebMvcAutoConfiguration.class,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment