|
18 | 18 |
|
19 | 19 | import java.util.Optional; |
20 | 20 |
|
21 | | -import org.junit.jupiter.api.AfterEach; |
22 | 21 | import org.junit.jupiter.api.Test; |
23 | 22 |
|
24 | 23 | import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
25 | 24 | import org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration.HypermediaConfiguration; |
26 | 25 | import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration; |
27 | 26 | import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; |
28 | 27 | import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; |
29 | | -import org.springframework.boot.test.util.TestPropertyValues; |
30 | | -import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext; |
| 28 | +import org.springframework.boot.test.context.FilteredClassLoader; |
| 29 | +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
31 | 30 | import org.springframework.context.annotation.Configuration; |
32 | 31 | import org.springframework.hateoas.MediaTypes; |
33 | 32 | import org.springframework.hateoas.client.LinkDiscoverer; |
|
39 | 38 | import org.springframework.hateoas.server.mvc.TypeConstrainedMappingJackson2HttpMessageConverter; |
40 | 39 | import org.springframework.http.MediaType; |
41 | 40 | import org.springframework.http.converter.HttpMessageConverter; |
42 | | -import org.springframework.mock.web.MockServletContext; |
43 | 41 | import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; |
44 | 42 |
|
45 | 43 | import static org.assertj.core.api.Assertions.assertThat; |
|
50 | 48 | * @author Roy Clarkson |
51 | 49 | * @author Oliver Gierke |
52 | 50 | * @author Andy Wilkinson |
| 51 | + * @author Madhura Bhave |
53 | 52 | */ |
54 | 53 | class HypermediaAutoConfigurationTests { |
55 | 54 |
|
56 | | - private AnnotationConfigServletWebApplicationContext context; |
| 55 | + private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() |
| 56 | + .withUserConfiguration(BaseConfig.class); |
57 | 57 |
|
58 | | - @AfterEach |
59 | | - void close() { |
60 | | - if (this.context != null) { |
61 | | - this.context.close(); |
62 | | - } |
| 58 | + @Test |
| 59 | + void autoConfigurationWhenSpringMvcNotOnClasspathShouldBackOff() { |
| 60 | + this.contextRunner.withClassLoader(new FilteredClassLoader(RequestMappingHandlerAdapter.class)) |
| 61 | + .run((context) -> assertThat(context.getBeansOfType(HypermediaConfiguration.class)).isEmpty()); |
63 | 62 | } |
64 | 63 |
|
65 | 64 | @Test |
66 | 65 | void linkDiscoverersCreated() { |
67 | | - this.context = new AnnotationConfigServletWebApplicationContext(); |
68 | | - this.context.setServletContext(new MockServletContext()); |
69 | | - this.context.register(BaseConfig.class); |
70 | | - this.context.refresh(); |
71 | | - LinkDiscoverers discoverers = this.context.getBean(LinkDiscoverers.class); |
72 | | - assertThat(discoverers).isNotNull(); |
73 | | - Optional<LinkDiscoverer> discoverer = discoverers.getLinkDiscovererFor(MediaTypes.HAL_JSON); |
74 | | - assertThat(discoverer).containsInstanceOf(HalLinkDiscoverer.class); |
| 66 | + this.contextRunner.run((context) -> { |
| 67 | + LinkDiscoverers discoverers = context.getBean(LinkDiscoverers.class); |
| 68 | + assertThat(discoverers).isNotNull(); |
| 69 | + Optional<LinkDiscoverer> discoverer = discoverers.getLinkDiscovererFor(MediaTypes.HAL_JSON); |
| 70 | + assertThat(discoverer).containsInstanceOf(HalLinkDiscoverer.class); |
| 71 | + }); |
75 | 72 | } |
76 | 73 |
|
77 | 74 | @Test |
78 | 75 | void entityLinksCreated() { |
79 | | - this.context = new AnnotationConfigServletWebApplicationContext(); |
80 | | - this.context.setServletContext(new MockServletContext()); |
81 | | - this.context.register(BaseConfig.class); |
82 | | - this.context.refresh(); |
83 | | - EntityLinks discoverers = this.context.getBean(EntityLinks.class); |
84 | | - assertThat(discoverers).isNotNull(); |
| 76 | + this.contextRunner.run((context) -> { |
| 77 | + EntityLinks discoverers = context.getBean(EntityLinks.class); |
| 78 | + assertThat(discoverers).isNotNull(); |
| 79 | + }); |
85 | 80 | } |
86 | 81 |
|
87 | 82 | @Test |
88 | 83 | void doesBackOffIfEnableHypermediaSupportIsDeclaredManually() { |
89 | | - this.context = new AnnotationConfigServletWebApplicationContext(); |
90 | | - this.context.setServletContext(new MockServletContext()); |
91 | | - this.context.register(EnableHypermediaSupportConfig.class, BaseConfig.class); |
92 | | - TestPropertyValues.of("spring.jackson.serialization.INDENT_OUTPUT:true").applyTo(this.context); |
93 | | - this.context.refresh(); |
94 | | - assertThat(this.context.getBeansOfType(HypermediaConfiguration.class)).isEmpty(); |
| 84 | + this.contextRunner.withUserConfiguration(EnableHypermediaSupportConfig.class) |
| 85 | + .withPropertyValues("spring.jackson.serialization.INDENT_OUTPUT:true") |
| 86 | + .run((context) -> assertThat(context.getBeansOfType(HypermediaConfiguration.class)).isEmpty()); |
95 | 87 | } |
96 | 88 |
|
97 | 89 | @Test |
98 | 90 | void supportedMediaTypesOfTypeConstrainedConvertersIsCustomized() { |
99 | | - this.context = new AnnotationConfigServletWebApplicationContext(); |
100 | | - this.context.setServletContext(new MockServletContext()); |
101 | | - this.context.register(BaseConfig.class); |
102 | | - this.context.refresh(); |
103 | | - RequestMappingHandlerAdapter handlerAdapter = this.context.getBean(RequestMappingHandlerAdapter.class); |
104 | | - for (HttpMessageConverter<?> converter : handlerAdapter.getMessageConverters()) { |
105 | | - if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) { |
106 | | - assertThat(converter.getSupportedMediaTypes()).contains(MediaType.APPLICATION_JSON, |
107 | | - MediaTypes.HAL_JSON); |
| 91 | + this.contextRunner.run((context) -> { |
| 92 | + RequestMappingHandlerAdapter handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class); |
| 93 | + for (HttpMessageConverter<?> converter : handlerAdapter.getMessageConverters()) { |
| 94 | + if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) { |
| 95 | + assertThat(converter.getSupportedMediaTypes()).contains(MediaType.APPLICATION_JSON, |
| 96 | + MediaTypes.HAL_JSON); |
| 97 | + } |
108 | 98 | } |
109 | | - } |
| 99 | + }); |
110 | 100 | } |
111 | 101 |
|
112 | 102 | @Test |
113 | 103 | void customizationOfSupportedMediaTypesCanBeDisabled() { |
114 | | - this.context = new AnnotationConfigServletWebApplicationContext(); |
115 | | - this.context.setServletContext(new MockServletContext()); |
116 | | - this.context.register(BaseConfig.class); |
117 | | - TestPropertyValues.of("spring.hateoas.use-hal-as-default-json-media-type:false").applyTo(this.context); |
118 | | - this.context.refresh(); |
119 | | - RequestMappingHandlerAdapter handlerAdapter = this.context.getBean(RequestMappingHandlerAdapter.class); |
120 | | - for (HttpMessageConverter<?> converter : handlerAdapter.getMessageConverters()) { |
121 | | - if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) { |
122 | | - assertThat(converter.getSupportedMediaTypes()).containsExactly(MediaTypes.HAL_JSON); |
123 | | - } |
124 | | - } |
| 104 | + this.contextRunner.withPropertyValues("spring.hateoas.use-hal-as-default-json-media-type:false") |
| 105 | + .run((context) -> { |
| 106 | + RequestMappingHandlerAdapter handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class); |
| 107 | + for (HttpMessageConverter<?> converter : handlerAdapter.getMessageConverters()) { |
| 108 | + if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) { |
| 109 | + assertThat(converter.getSupportedMediaTypes()).containsExactly(MediaTypes.HAL_JSON); |
| 110 | + } |
| 111 | + } |
| 112 | + }); |
125 | 113 | } |
126 | 114 |
|
127 | 115 | @ImportAutoConfiguration({ HttpMessageConvertersAutoConfiguration.class, WebMvcAutoConfiguration.class, |
|
0 commit comments