Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @WebServiceClientTest annotation that can be used when testing SOAP clients #17274

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion spring-boot-project/spring-boot-dependencies/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,7 @@ bom {
]
}
}
library("Spring WS", "3.0.8.RELEASE") {
library("Spring WS", "3.0.9.BUILD-SNAPSHOT") {
group("org.springframework.ws") {
modules = [
"spring-ws-core",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ dependencies {
optional("org.springframework.restdocs:spring-restdocs-webtestclient")
optional("org.springframework.security:spring-security-config")
optional("org.springframework.security:spring-security-test")
optional("org.springframework.ws:spring-ws-core")
optional("org.springframework.ws:spring-ws-test")
optional("org.apache.tomcat.embed:tomcat-embed-core")
optional("org.mongodb:mongodb-driver-reactivestreams")
optional("org.mongodb:mongodb-driver-sync")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.test.autoconfigure.webservices.client;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
import org.springframework.ws.test.client.MockWebServiceServer;

/**
* Annotation that can be applied to a test class to enable and configure
* auto-configuration of a single {@link MockWebServiceServer}.
*
* @author Dmytro Nosan
* @since 2.3.0
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@ImportAutoConfiguration
@PropertyMapping("spring.test.webservice.client.mock-server")
public @interface AutoConfigureMockWebServiceServer {

/**
* If {@link MockWebServiceServer} bean should be registered. Defaults to
* {@code true}.
* @return if mock support is enabled
*/
boolean enabled() default true;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.test.autoconfigure.webservices.client;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
import org.springframework.ws.client.core.WebServiceTemplate;

/**
* Annotation that can be applied to a test class to enable and configure
* auto-configuration of web service clients.
*
* @author Dmytro Nosan
* @since 2.3.0
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@ImportAutoConfiguration
@PropertyMapping("spring.test.webservice.client")
public @interface AutoConfigureWebServiceClient {

/**
* If a {@link WebServiceTemplate} bean should be registered. Defaults to
* {@code false} with the assumption that the {@link WebServiceTemplateBuilder} will
* be used.
* @return if a {@link WebServiceTemplate} bean should be added.
*/
boolean registerWebServiceTemplate() default false;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.test.autoconfigure.webservices.client;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.test.client.MockWebServiceMessageSender;
import org.springframework.ws.test.client.MockWebServiceServer;

/**
* Auto-configuration for {@link MockWebServiceServer} support.
*
* @author Dmytro Nosan
* @see AutoConfigureMockWebServiceServer
* @since 2.3.0
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(prefix = "spring.test.webservice.client.mock-server", name = "enabled")
@ConditionalOnClass({ MockWebServiceServer.class, WebServiceTemplate.class })
public class MockWebServiceServerAutoConfiguration {

@Bean
public TestMockWebServiceServer mockWebServiceServer() {
return new TestMockWebServiceServer(new MockWebServiceMessageSender());
}

@Bean
public MockWebServiceServerWebServiceTemplateCustomizer mockWebServiceServerWebServiceTemplateCustomizer(
TestMockWebServiceServer mockWebServiceServer) {
return new MockWebServiceServerWebServiceTemplateCustomizer(mockWebServiceServer);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.test.autoconfigure.webservices.client;

import org.springframework.context.ApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.support.AbstractTestExecutionListener;
import org.springframework.util.ClassUtils;
import org.springframework.ws.test.client.MockWebServiceServer;

/**
* {@link TestExecutionListener} to {@code verify} and {@code reset}
* {@link MockWebServiceServer}.
*
* @author Dmytro Nosan
* @since 2.3.0
*/
public class MockWebServiceServerTestExecutionListener extends AbstractTestExecutionListener {

private static final String MOCK_SERVER_CLASS = "org.springframework.ws.test.client.MockWebServiceServer";

@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE - 100;
}

@Override
public void afterTestMethod(TestContext testContext) {
if (isMockWebServiceServerPresent()) {
ApplicationContext applicationContext = testContext.getApplicationContext();
String[] names = applicationContext.getBeanNamesForType(MockWebServiceServer.class, false, false);
for (String name : names) {
MockWebServiceServer mockServer = applicationContext.getBean(name, MockWebServiceServer.class);
mockServer.verify();
mockServer.reset();
}
}
}

private boolean isMockWebServiceServerPresent() {
return ClassUtils.isPresent(MOCK_SERVER_CLASS, getClass().getClassLoader());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.test.autoconfigure.webservices.client;

import java.util.concurrent.atomic.AtomicBoolean;

import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
import org.springframework.boot.webservices.client.WebServiceTemplateCustomizer;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.test.client.MockWebServiceServer;

/**
* {@link WebServiceTemplateCustomizer} that can be applied to a
* {@link WebServiceTemplateBuilder} instances to add {@link MockWebServiceServer}
* support.
*
* @author Dmytro Nosan
*/
class MockWebServiceServerWebServiceTemplateCustomizer implements WebServiceTemplateCustomizer {

private final AtomicBoolean alreadySet = new AtomicBoolean();

private final TestMockWebServiceServer mockServer;

MockWebServiceServerWebServiceTemplateCustomizer(TestMockWebServiceServer mockServer) {
this.mockServer = mockServer;
}

@Override
public void customize(WebServiceTemplate webServiceTemplate) {
if (this.alreadySet.compareAndSet(false, true)) {
webServiceTemplate.setMessageSender(this.mockServer.getMockMessageSender());
}
else {
throw new IllegalStateException("@WebServiceClientTest supports only a single WebServiceTemplate");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.test.autoconfigure.webservices.client;

import org.springframework.ws.test.client.MockWebServiceMessageSender;
import org.springframework.ws.test.client.MockWebServiceServer;

/**
* Test {@link MockWebServiceServer} which provides access to the underlying
* {@link MockWebServiceMessageSender}.
*
* @author Dmytro Nosan
*/
final class TestMockWebServiceServer extends MockWebServiceServer {

private final MockWebServiceMessageSender mockMessageSender;

TestMockWebServiceServer(MockWebServiceMessageSender mockMessageSender) {
super(mockMessageSender);
this.mockMessageSender = mockMessageSender;
}

MockWebServiceMessageSender getMockMessageSender() {
return this.mockMessageSender;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.test.autoconfigure.webservices.client;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.boot.test.autoconfigure.filter.StandardAnnotationCustomizableTypeExcludeFilter;

/**
* {@link TypeExcludeFilter} for {@link WebServiceClientTest @WebServiceClientTest}.
*
* @author Dmytro Nosan
* @since 2.3.0
*/
public final class WebServiceClientExcludeFilter
extends StandardAnnotationCustomizableTypeExcludeFilter<WebServiceClientTest> {

private final Class<?>[] components;

protected WebServiceClientExcludeFilter(Class<?> testClass) {
super(testClass);
this.components = getAnnotation().getValue("components", Class[].class).orElse(new Class[0]);
}

@Override
protected Set<Class<?>> getComponentIncludes() {
return new LinkedHashSet<>(Arrays.asList(this.components));
}

}