Skip to content

Commit ca1b4c8

Browse files
committed
Add hypermedia autoconfiguration for WebTestClient.
* Provide CodecCustomizer for WebTestClient. Related: spring-projects#16020.
1 parent 533a20a commit ca1b4c8

File tree

5 files changed

+224
-2
lines changed

5 files changed

+224
-2
lines changed

spring-boot-project/spring-boot-test-autoconfigure/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ dependencies {
4040
optional("org.springframework.data:spring-data-neo4j")
4141
optional("org.springframework.data:spring-data-r2dbc")
4242
optional("org.springframework.data:spring-data-redis")
43+
optional("org.springframework.hateoas:spring-hateoas")
4344
optional("org.springframework.restdocs:spring-restdocs-mockmvc")
4445
optional("org.springframework.restdocs:spring-restdocs-restassured")
4546
optional("org.springframework.restdocs:spring-restdocs-webtestclient")
@@ -73,8 +74,6 @@ dependencies {
7374
testImplementation("org.junit.jupiter:junit-jupiter")
7475
testImplementation("org.mockito:mockito-core")
7576
testImplementation("org.skyscreamer:jsonassert")
76-
testImplementation("org.springframework.hateoas:spring-hateoas")
77-
testImplementation("org.springframework.plugin:spring-plugin-core")
7877
testImplementation("org.testcontainers:junit-jupiter")
7978
testImplementation("org.testcontainers:neo4j")
8079
testImplementation("org.testcontainers:testcontainers")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2020 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.boot.test.autoconfigure.hateoas;
17+
18+
import java.util.List;
19+
20+
import org.springframework.beans.factory.ObjectProvider;
21+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
23+
import org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration;
24+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
25+
import org.springframework.boot.web.codec.CodecCustomizer;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.hateoas.config.HypermediaMappingInformation;
29+
import org.springframework.http.codec.json.Jackson2JsonDecoder;
30+
import org.springframework.http.codec.json.Jackson2JsonEncoder;
31+
import org.springframework.test.web.reactive.server.WebTestClient;
32+
import org.springframework.util.Assert;
33+
import org.springframework.util.MimeType;
34+
35+
import com.fasterxml.jackson.databind.ObjectMapper;
36+
37+
/**
38+
* Autoconfiguration for hypermedia-based testing.
39+
*
40+
* @author Greg Turnquist
41+
*/
42+
@Configuration(proxyBeanMethods = false)
43+
@AutoConfigureAfter({ CodecsAutoConfiguration.class })
44+
@EnableConfigurationProperties
45+
public class HypermediaTestAutoConfiguration {
46+
47+
/**
48+
* Create a {@link CodecCustomizer} that will be used to configure
49+
* {@link WebTestClient}.
50+
*/
51+
@Bean
52+
@ConditionalOnClass({ WebTestClient.class, HypermediaMappingInformation.class, ObjectMapper.class,
53+
Jackson2JsonDecoder.class })
54+
CodecCustomizer hypermediaCodecCustomizer(ObjectProvider<ObjectMapper> mapperProvider,
55+
List<HypermediaMappingInformation> hypermediaTypes) {
56+
return codecConfigurer -> {
57+
Assert.notNull(hypermediaTypes, "HypermediaMappingInformations must not be null!");
58+
59+
hypermediaTypes.forEach(hypermedia -> {
60+
61+
ObjectMapper objectMapper = hypermedia
62+
.configureObjectMapper(mapperProvider.getIfAvailable(ObjectMapper::new).copy());
63+
MimeType[] mimeTypes = hypermedia.getMediaTypes().toArray(new MimeType[0]);
64+
65+
codecConfigurer.customCodecs()
66+
.registerWithDefaultConfig(new Jackson2JsonEncoder(objectMapper, mimeTypes));
67+
codecConfigurer.customCodecs()
68+
.registerWithDefaultConfig(new Jackson2JsonDecoder(objectMapper, mimeTypes));
69+
});
70+
};
71+
}
72+
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2020 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+
17+
/**
18+
* Auto-configuration for Spring HATEOAS tests.
19+
*/
20+
package org.springframework.boot.test.autoconfigure.hateoas;

spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveO
100100
org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration,\
101101
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration,\
102102
org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration,\
103+
org.springframework.boot.test.autoconfigure.hateoas.HypermediaTestAutoConfiguration,\
103104
org.springframework.boot.test.autoconfigure.web.reactive.WebTestClientAutoConfiguration
104105

105106
# AutoConfigureWebFlux auto-configuration imports
@@ -182,3 +183,5 @@ org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListen
182183
org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener,\
183184
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener,\
184185
org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener
186+
187+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright 2012-2020 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+
17+
package org.springframework.boot.test.autoconfigure.hateoas;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.springframework.boot.autoconfigure.AutoConfigurations;
21+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
22+
import org.springframework.boot.test.autoconfigure.web.reactive.WebTestClientAutoConfiguration;
23+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.hateoas.MediaTypes;
27+
import org.springframework.hateoas.config.EnableHypermediaSupport;
28+
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
29+
import org.springframework.http.MediaType;
30+
import org.springframework.http.codec.HttpMessageReader;
31+
import org.springframework.http.codec.HttpMessageWriter;
32+
import org.springframework.test.util.ReflectionTestUtils;
33+
import org.springframework.test.web.reactive.server.WebTestClient;
34+
import org.springframework.web.reactive.function.client.ExchangeStrategies;
35+
import org.springframework.web.reactive.function.client.WebClient;
36+
import org.springframework.web.server.WebHandler;
37+
38+
import static org.assertj.core.api.Assertions.assertThat;
39+
import static org.mockito.Mockito.mock;
40+
41+
/**
42+
* Tests for hypermedia-based {@link WebTestClient}.
43+
*
44+
* @author Brian Clozel
45+
* @author Stephane Nicoll
46+
* @author Greg Turnquist
47+
*/
48+
class HypermediaTestAutoConfigurationTests {
49+
50+
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
51+
.withConfiguration(AutoConfigurations.of(WebTestClientAutoConfiguration.class));
52+
53+
@Test
54+
void codecsCustomizerShouldRegisterHypermediaTypesWithWebTestClient() {
55+
this.contextRunner.withUserConfiguration(BaseConfiguration.class).run((context) -> {
56+
WebTestClient webTestClient = context.getBean(WebTestClient.class);
57+
assertWebTestClientHasHypermedia(webTestClient, MediaTypes.HAL_JSON);
58+
});
59+
}
60+
61+
@Test
62+
void codecsCustomizerShouldRegisterAlternativeHypermediaTypesWithWebTestClient() {
63+
this.contextRunner.withUserConfiguration(HalFormsConfiguration.class).run((context) -> {
64+
WebTestClient webTestClient = context.getBean(WebTestClient.class);
65+
assertWebTestClientHasHypermedia(webTestClient, MediaTypes.HAL_FORMS_JSON);
66+
});
67+
}
68+
69+
@Test
70+
void codecsCustomizerShouldRegisterAllHypermediaTypesWithWebTestClient() {
71+
this.contextRunner.withUserConfiguration(AllHypermediaTypesConfiguration.class).run((context) -> {
72+
WebTestClient webTestClient = context.getBean(WebTestClient.class);
73+
assertWebTestClientHasHypermedia(webTestClient, MediaTypes.HAL_JSON, MediaTypes.HAL_FORMS_JSON,
74+
MediaTypes.COLLECTION_JSON, MediaTypes.UBER_JSON);
75+
});
76+
}
77+
78+
private static void assertWebTestClientHasHypermedia(WebTestClient webTestClient, MediaType... mediaTypes) {
79+
80+
WebClient webClient = (WebClient) ReflectionTestUtils.getField(webTestClient, "webClient");
81+
ExchangeStrategies strategies = (ExchangeStrategies) ReflectionTestUtils
82+
.getField(ReflectionTestUtils.getField(webClient, "exchangeFunction"), "strategies");
83+
84+
assertThat(strategies.messageReaders()).flatExtracting(HttpMessageReader::getReadableMediaTypes)
85+
.contains(mediaTypes);
86+
assertThat(strategies.messageWriters()).flatExtracting(HttpMessageWriter::getWritableMediaTypes)
87+
.contains(mediaTypes);
88+
}
89+
90+
@ImportAutoConfiguration(HypermediaTestAutoConfiguration.class)
91+
@Configuration(proxyBeanMethods = false)
92+
@EnableHypermediaSupport(type = HypermediaType.HAL)
93+
static class BaseConfiguration {
94+
95+
@Bean
96+
WebHandler webHandler() {
97+
return mock(WebHandler.class);
98+
}
99+
100+
}
101+
102+
@ImportAutoConfiguration(HypermediaTestAutoConfiguration.class)
103+
@Configuration(proxyBeanMethods = false)
104+
@EnableHypermediaSupport(type = HypermediaType.HAL_FORMS)
105+
static class HalFormsConfiguration {
106+
107+
@Bean
108+
WebHandler webHandler() {
109+
return mock(WebHandler.class);
110+
}
111+
112+
}
113+
114+
@ImportAutoConfiguration(HypermediaTestAutoConfiguration.class)
115+
@Configuration(proxyBeanMethods = false)
116+
@EnableHypermediaSupport(type = { HypermediaType.HAL, HypermediaType.HAL_FORMS, HypermediaType.COLLECTION_JSON,
117+
HypermediaType.UBER })
118+
static class AllHypermediaTypesConfiguration {
119+
120+
@Bean
121+
WebHandler webHandler() {
122+
return mock(WebHandler.class);
123+
}
124+
125+
}
126+
127+
}

0 commit comments

Comments
 (0)