Skip to content

Commit 27fcfca

Browse files
committed
Add @WebServiceClientTest annotation that can be used
when testing SOAP clients
1 parent 952e529 commit 27fcfca

24 files changed

+1082
-2
lines changed

spring-boot-project/spring-boot-dependencies/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,7 @@ bom {
16891689
]
16901690
}
16911691
}
1692-
library("Spring WS", "3.0.8.RELEASE") {
1692+
library("Spring WS", "3.0.9.BUILD-SNAPSHOT") {
16931693
group("org.springframework.ws") {
16941694
modules = [
16951695
"spring-ws-core",

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ dependencies {
4545
optional("org.springframework.restdocs:spring-restdocs-webtestclient")
4646
optional("org.springframework.security:spring-security-config")
4747
optional("org.springframework.security:spring-security-test")
48+
optional("org.springframework.ws:spring-ws-core")
49+
optional("org.springframework.ws:spring-ws-test")
4850
optional("org.apache.tomcat.embed:tomcat-embed-core")
4951
optional("org.mongodb:mongodb-driver-reactivestreams")
5052
optional("org.mongodb:mongodb-driver-sync")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.webservices.client;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Inherited;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
25+
26+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
27+
import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
28+
import org.springframework.ws.test.client.MockWebServiceServer;
29+
30+
/**
31+
* Annotation that can be applied to a test class to enable and configure
32+
* auto-configuration of a single {@link MockWebServiceServer}.
33+
*
34+
* @author Dmytro Nosan
35+
* @since 2.3.0
36+
*/
37+
@Target(ElementType.TYPE)
38+
@Retention(RetentionPolicy.RUNTIME)
39+
@Documented
40+
@Inherited
41+
@ImportAutoConfiguration
42+
@PropertyMapping("spring.test.webservice.client.mock-server")
43+
public @interface AutoConfigureMockWebServiceServer {
44+
45+
/**
46+
* If {@link MockWebServiceServer} bean should be registered. Defaults to
47+
* {@code true}.
48+
* @return if mock support is enabled
49+
*/
50+
boolean enabled() default true;
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.webservices.client;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Inherited;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
25+
26+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
27+
import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
28+
import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
29+
import org.springframework.ws.client.core.WebServiceTemplate;
30+
31+
/**
32+
* Annotation that can be applied to a test class to enable and configure
33+
* auto-configuration of web service clients.
34+
*
35+
* @author Dmytro Nosan
36+
* @since 2.3.0
37+
*/
38+
@Target(ElementType.TYPE)
39+
@Retention(RetentionPolicy.RUNTIME)
40+
@Documented
41+
@Inherited
42+
@ImportAutoConfiguration
43+
@PropertyMapping("spring.test.webservice.client")
44+
public @interface AutoConfigureWebServiceClient {
45+
46+
/**
47+
* If a {@link WebServiceTemplate} bean should be registered. Defaults to
48+
* {@code false} with the assumption that the {@link WebServiceTemplateBuilder} will
49+
* be used.
50+
* @return if a {@link WebServiceTemplate} bean should be added.
51+
*/
52+
boolean registerWebServiceTemplate() default false;
53+
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.webservices.client;
18+
19+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.ws.client.core.WebServiceTemplate;
24+
import org.springframework.ws.test.client.MockWebServiceMessageSender;
25+
import org.springframework.ws.test.client.MockWebServiceServer;
26+
27+
/**
28+
* Auto-configuration for {@link MockWebServiceServer} support.
29+
*
30+
* @author Dmytro Nosan
31+
* @see AutoConfigureMockWebServiceServer
32+
* @since 2.3.0
33+
*/
34+
@Configuration(proxyBeanMethods = false)
35+
@ConditionalOnProperty(prefix = "spring.test.webservice.client.mock-server", name = "enabled")
36+
@ConditionalOnClass({ MockWebServiceServer.class, WebServiceTemplate.class })
37+
public class MockWebServiceServerAutoConfiguration {
38+
39+
@Bean
40+
public TestMockWebServiceServer mockWebServiceServer() {
41+
return new TestMockWebServiceServer(new MockWebServiceMessageSender());
42+
}
43+
44+
@Bean
45+
public MockWebServiceServerWebServiceTemplateCustomizer mockWebServiceServerWebServiceTemplateCustomizer(
46+
TestMockWebServiceServer mockWebServiceServer) {
47+
return new MockWebServiceServerWebServiceTemplateCustomizer(mockWebServiceServer);
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.webservices.client;
18+
19+
import org.springframework.context.ApplicationContext;
20+
import org.springframework.core.Ordered;
21+
import org.springframework.test.context.TestContext;
22+
import org.springframework.test.context.TestExecutionListener;
23+
import org.springframework.test.context.support.AbstractTestExecutionListener;
24+
import org.springframework.util.ClassUtils;
25+
import org.springframework.ws.test.client.MockWebServiceServer;
26+
27+
/**
28+
* {@link TestExecutionListener} to {@code verify} and {@code reset}
29+
* {@link MockWebServiceServer}.
30+
*
31+
* @author Dmytro Nosan
32+
* @since 2.3.0
33+
*/
34+
public class MockWebServiceServerTestExecutionListener extends AbstractTestExecutionListener {
35+
36+
private static final String MOCK_SERVER_CLASS = "org.springframework.ws.test.client.MockWebServiceServer";
37+
38+
@Override
39+
public int getOrder() {
40+
return Ordered.LOWEST_PRECEDENCE - 100;
41+
}
42+
43+
@Override
44+
public void afterTestMethod(TestContext testContext) {
45+
if (isMockWebServiceServerPresent()) {
46+
ApplicationContext applicationContext = testContext.getApplicationContext();
47+
String[] names = applicationContext.getBeanNamesForType(MockWebServiceServer.class, false, false);
48+
for (String name : names) {
49+
MockWebServiceServer mockServer = applicationContext.getBean(name, MockWebServiceServer.class);
50+
mockServer.verify();
51+
mockServer.reset();
52+
}
53+
}
54+
}
55+
56+
private boolean isMockWebServiceServerPresent() {
57+
return ClassUtils.isPresent(MOCK_SERVER_CLASS, getClass().getClassLoader());
58+
}
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.webservices.client;
18+
19+
import java.util.concurrent.atomic.AtomicBoolean;
20+
21+
import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
22+
import org.springframework.boot.webservices.client.WebServiceTemplateCustomizer;
23+
import org.springframework.ws.client.core.WebServiceTemplate;
24+
import org.springframework.ws.test.client.MockWebServiceServer;
25+
26+
/**
27+
* {@link WebServiceTemplateCustomizer} that can be applied to a
28+
* {@link WebServiceTemplateBuilder} instances to add {@link MockWebServiceServer}
29+
* support.
30+
*
31+
* @author Dmytro Nosan
32+
*/
33+
class MockWebServiceServerWebServiceTemplateCustomizer implements WebServiceTemplateCustomizer {
34+
35+
private final AtomicBoolean alreadySet = new AtomicBoolean();
36+
37+
private final TestMockWebServiceServer mockServer;
38+
39+
MockWebServiceServerWebServiceTemplateCustomizer(TestMockWebServiceServer mockServer) {
40+
this.mockServer = mockServer;
41+
}
42+
43+
@Override
44+
public void customize(WebServiceTemplate webServiceTemplate) {
45+
if (this.alreadySet.compareAndSet(false, true)) {
46+
webServiceTemplate.setMessageSender(this.mockServer.getMockMessageSender());
47+
}
48+
else {
49+
throw new IllegalStateException("@WebServiceClientTest supports only a single WebServiceTemplate");
50+
}
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.webservices.client;
18+
19+
import org.springframework.ws.test.client.MockWebServiceMessageSender;
20+
import org.springframework.ws.test.client.MockWebServiceServer;
21+
22+
/**
23+
* Test {@link MockWebServiceServer} which provides access to the underlying
24+
* {@link MockWebServiceMessageSender}.
25+
*
26+
* @author Dmytro Nosan
27+
*/
28+
final class TestMockWebServiceServer extends MockWebServiceServer {
29+
30+
private final MockWebServiceMessageSender mockMessageSender;
31+
32+
TestMockWebServiceServer(MockWebServiceMessageSender mockMessageSender) {
33+
super(mockMessageSender);
34+
this.mockMessageSender = mockMessageSender;
35+
}
36+
37+
MockWebServiceMessageSender getMockMessageSender() {
38+
return mockMessageSender;
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.webservices.client;
18+
19+
import java.util.Arrays;
20+
import java.util.LinkedHashSet;
21+
import java.util.Set;
22+
23+
import org.springframework.boot.context.TypeExcludeFilter;
24+
import org.springframework.boot.test.autoconfigure.filter.StandardAnnotationCustomizableTypeExcludeFilter;
25+
26+
/**
27+
* {@link TypeExcludeFilter} for {@link WebServiceClientTest @WebServiceClientTest}.
28+
*
29+
* @author Dmytro Nosan
30+
* @since 2.3.0
31+
*/
32+
public final class WebServiceClientExcludeFilter
33+
extends StandardAnnotationCustomizableTypeExcludeFilter<WebServiceClientTest> {
34+
35+
private final Class<?>[] components;
36+
37+
protected WebServiceClientExcludeFilter(Class<?> testClass) {
38+
super(testClass);
39+
this.components = getAnnotation().getValue("components", Class[].class).orElse(new Class[0]);
40+
}
41+
42+
@Override
43+
protected Set<Class<?>> getComponentIncludes() {
44+
return new LinkedHashSet<>(Arrays.asList(this.components));
45+
}
46+
47+
}

0 commit comments

Comments
 (0)