diff --git a/test/src/main/java/org/springframework/ws/mock/server/AbstractRequestCreator.java b/test/src/main/java/org/springframework/ws/mock/server/AbstractRequestCreator.java new file mode 100644 index 00000000..fa203d78 --- /dev/null +++ b/test/src/main/java/org/springframework/ws/mock/server/AbstractRequestCreator.java @@ -0,0 +1,39 @@ +/* + * Copyright 2005-2010 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 + * + * http://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.ws.mock.server; + +import java.io.IOException; + +import org.springframework.ws.WebServiceMessage; +import org.springframework.ws.WebServiceMessageFactory; +import org.springframework.xml.transform.TransformerObjectSupport; + +/** + * @author Arjen Poutsma + */ +abstract class AbstractRequestCreator extends TransformerObjectSupport + implements RequestCreator { + + public final T createRequest(WebServiceMessageFactory messageFactory) throws IOException { + T request = messageFactory.createWebServiceMessage(); + doWithRequest(request); + return request; + } + + protected abstract void doWithRequest(T request) throws IOException; + +} diff --git a/test/src/main/java/org/springframework/ws/mock/server/PayloadRequestCreator.java b/test/src/main/java/org/springframework/ws/mock/server/PayloadRequestCreator.java new file mode 100644 index 00000000..0f53befd --- /dev/null +++ b/test/src/main/java/org/springframework/ws/mock/server/PayloadRequestCreator.java @@ -0,0 +1,49 @@ +/* + * Copyright 2005-2010 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 + * + * http://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.ws.mock.server; + +import java.io.IOException; +import javax.xml.transform.Source; +import javax.xml.transform.TransformerException; + +import org.springframework.ws.WebServiceMessage; + +/** + * Implementation of {@link org.springframework.ws.mock.client.ResponseCreator} that writes a {@link + * javax.xml.transform.Source} response. + * + * @author Arjen Poutsma + * @since 2.0 + */ +class PayloadRequestCreator extends AbstractRequestCreator { + + private final Source payload; + + PayloadRequestCreator(Source payload) { + this.payload = payload; + } + + @Override + protected void doWithRequest(WebServiceMessage request) throws IOException { + try { + transform(payload, request.getPayloadResult()); + } + catch (TransformerException ex) { + throw new AssertionError("Could not transform request payload to message: " + ex.getMessage()); + } + } +} diff --git a/test/src/main/java/org/springframework/ws/mock/server/RequestCreator.java b/test/src/main/java/org/springframework/ws/mock/server/RequestCreator.java new file mode 100644 index 00000000..8e8b15bd --- /dev/null +++ b/test/src/main/java/org/springframework/ws/mock/server/RequestCreator.java @@ -0,0 +1,38 @@ +/* + * Copyright 2005-2010 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 + * + * http://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.ws.mock.server; + +import java.io.IOException; + +import org.springframework.ws.WebServiceMessage; +import org.springframework.ws.WebServiceMessageFactory; + +/** + * @author Arjen Poutsma + */ +public interface RequestCreator { + + /** + * Create a request. + * + * @param messageFactory the message that can be used to create responses + * @throws java.io.IOException in case of I/O errors + */ + T createRequest(WebServiceMessageFactory messageFactory) throws IOException; + + +} diff --git a/test/src/main/java/org/springframework/ws/mock/server/ResponseActions.java b/test/src/main/java/org/springframework/ws/mock/server/ResponseActions.java new file mode 100644 index 00000000..9cf5adae --- /dev/null +++ b/test/src/main/java/org/springframework/ws/mock/server/ResponseActions.java @@ -0,0 +1,32 @@ +/* + * Copyright 2005-2010 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 + * + * http://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.ws.mock.server; + +/** + * @author Arjen Poutsma + */ +public interface ResponseActions { + + /** + * Allows for further expectations to be set on the request. + * + * @return the request expectations + */ + ResponseActions andExpect(ResponseMatcher responseMatcher); + + +} diff --git a/test/src/main/java/org/springframework/ws/mock/server/ResponseMatcher.java b/test/src/main/java/org/springframework/ws/mock/server/ResponseMatcher.java new file mode 100644 index 00000000..c6549ecb --- /dev/null +++ b/test/src/main/java/org/springframework/ws/mock/server/ResponseMatcher.java @@ -0,0 +1,39 @@ +/* + * Copyright 2005-2010 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 + * + * http://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.ws.mock.server; + +import java.io.IOException; + +import org.springframework.ws.WebServiceMessage; + +/** + * @author Arjen Poutsma + * @since 2.0 + */ +public interface ResponseMatcher { + + /** + * Matches the given response message against the expectations. Implementations typically make use of JUnit-based + * assertions. + * + * @param response the request message to make assertions on + * @throws IOException in case of I/O errors + * @throws AssertionError if expectations are not met + */ + void match(T response) throws IOException, AssertionError; + +} diff --git a/test/src/main/java/org/springframework/ws/mock/server/WebServiceMock.java b/test/src/main/java/org/springframework/ws/mock/server/WebServiceMock.java new file mode 100644 index 00000000..ac4174ef --- /dev/null +++ b/test/src/main/java/org/springframework/ws/mock/server/WebServiceMock.java @@ -0,0 +1,75 @@ +/* + * Copyright 2005-2010 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 + * + * http://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.ws.mock.server; + +import java.io.IOException; +import javax.xml.transform.Source; + +import org.springframework.core.io.Resource; +import org.springframework.util.Assert; +import org.springframework.ws.WebServiceMessage; +import org.springframework.xml.transform.ResourceSource; + +/** + * @author Arjen Poutsma + */ +public abstract class WebServiceMock { + + + public static ResponseActions receiveMessage(RequestCreator requestCreator) { + return null; + } + + public static RequestCreator withPayload(Source payload) { + Assert.notNull(payload, "'payload' must not be null"); + return new PayloadRequestCreator(payload); + } + + public static RequestCreator withPayload(Resource payload) { + Assert.notNull(payload, "'payload' must not be null"); + return new PayloadRequestCreator(createResourceSource(payload)); + } + + + public static ResponseMatcher payload(Source payload) { + return null; + } + + /** + * Expects any request. + * + * @return the request matcher + */ + public static ResponseMatcher anything() { + return new ResponseMatcher() { + public void match(WebServiceMessage response) throws IOException, AssertionError { + } + }; + } + + + + private static ResourceSource createResourceSource(Resource resource) { + try { + return new ResourceSource(resource); + } + catch (IOException ex) { + throw new IllegalArgumentException(resource + " could not be opened", ex); + } + } + +} diff --git a/test/src/test/java/org/springframework/ws/mock/client/integration/IntegrationTest.java b/test/src/test/java/org/springframework/ws/mock/client/integration/ClientIntegrationTest.java similarity index 93% rename from test/src/test/java/org/springframework/ws/mock/client/integration/IntegrationTest.java rename to test/src/test/java/org/springframework/ws/mock/client/integration/ClientIntegrationTest.java index 893dbad2..97766394 100644 --- a/test/src/test/java/org/springframework/ws/mock/client/integration/IntegrationTest.java +++ b/test/src/test/java/org/springframework/ws/mock/client/integration/ClientIntegrationTest.java @@ -22,6 +22,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.ws.client.core.WebServiceTemplate; +import org.springframework.ws.mock.integration.CustomerCountRequest; +import org.springframework.ws.mock.integration.CustomerCountResponse; import org.springframework.xml.transform.StringSource; import org.junit.Before; @@ -39,7 +41,7 @@ import static org.springframework.ws.mock.client.WebServiceMock.*; */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("integration-test.xml") -public class IntegrationTest { +public class ClientIntegrationTest { @Autowired private WebServiceTemplate webServiceTemplate; diff --git a/test/src/test/java/org/springframework/ws/mock/client/integration/CustomerCountRequest.java b/test/src/test/java/org/springframework/ws/mock/integration/CustomerCountRequest.java similarity index 95% rename from test/src/test/java/org/springframework/ws/mock/client/integration/CustomerCountRequest.java rename to test/src/test/java/org/springframework/ws/mock/integration/CustomerCountRequest.java index ca27fe72..0bb3cb5f 100644 --- a/test/src/test/java/org/springframework/ws/mock/client/integration/CustomerCountRequest.java +++ b/test/src/test/java/org/springframework/ws/mock/integration/CustomerCountRequest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ws.mock.client.integration; +package org.springframework.ws.mock.integration; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; diff --git a/test/src/test/java/org/springframework/ws/mock/client/integration/CustomerCountResponse.java b/test/src/test/java/org/springframework/ws/mock/integration/CustomerCountResponse.java similarity index 95% rename from test/src/test/java/org/springframework/ws/mock/client/integration/CustomerCountResponse.java rename to test/src/test/java/org/springframework/ws/mock/integration/CustomerCountResponse.java index f4a0eebb..503a2b05 100644 --- a/test/src/test/java/org/springframework/ws/mock/client/integration/CustomerCountResponse.java +++ b/test/src/test/java/org/springframework/ws/mock/integration/CustomerCountResponse.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ws.mock.client.integration; +package org.springframework.ws.mock.integration; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; diff --git a/test/src/test/java/org/springframework/ws/mock/server/integration/CustomerEndpoint.java b/test/src/test/java/org/springframework/ws/mock/server/integration/CustomerEndpoint.java new file mode 100644 index 00000000..db698d57 --- /dev/null +++ b/test/src/test/java/org/springframework/ws/mock/server/integration/CustomerEndpoint.java @@ -0,0 +1,35 @@ +/* + * Copyright 2005-2010 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 + * + * http://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.ws.mock.server.integration; + +import org.springframework.ws.mock.integration.CustomerCountRequest; +import org.springframework.ws.mock.integration.CustomerCountResponse; +import org.springframework.ws.server.endpoint.annotation.Endpoint; +import org.springframework.ws.server.endpoint.annotation.RequestPayload; +import org.springframework.ws.server.endpoint.annotation.ResponsePayload; + +@Endpoint +public class CustomerEndpoint { + + @ResponsePayload + public CustomerCountResponse getCustomerCount(@RequestPayload CustomerCountRequest request) { + CustomerCountResponse response = new CustomerCountResponse(); + response.setCustomerCount(42); + return response; + } + +} diff --git a/test/src/test/java/org/springframework/ws/mock/server/integration/ServerIntegrationTest.java b/test/src/test/java/org/springframework/ws/mock/server/integration/ServerIntegrationTest.java new file mode 100644 index 00000000..546c352f --- /dev/null +++ b/test/src/test/java/org/springframework/ws/mock/server/integration/ServerIntegrationTest.java @@ -0,0 +1,48 @@ +/* + * Copyright 2005-2010 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 + * + * http://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.ws.mock.server.integration; + +import javax.xml.transform.Source; + +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.xml.transform.StringSource; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.springframework.ws.mock.server.WebServiceMock.*; + +/** + * @author Arjen Poutsma + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("integration-test.xml") +public class ServerIntegrationTest { + + @Test + public void basic() throws Exception { + Source requestPayload = new StringSource("" + + "John Doe" + ""); + Source responsePayload = new StringSource("" + + "10" + ""); + + receiveMessage(withPayload(requestPayload)).andExpect(payload(responsePayload)); + } + + +} diff --git a/test/src/test/resources/org/springframework/ws/mock/client/integration/integration-test.xml b/test/src/test/resources/org/springframework/ws/mock/client/integration/integration-test.xml index c059479e..b3fce516 100644 --- a/test/src/test/resources/org/springframework/ws/mock/client/integration/integration-test.xml +++ b/test/src/test/resources/org/springframework/ws/mock/client/integration/integration-test.xml @@ -12,8 +12,8 @@ - org.springframework.ws.mock.client.integration.CustomerCountRequest - org.springframework.ws.mock.client.integration.CustomerCountResponse + org.springframework.ws.mock.integration.CustomerCountRequest + org.springframework.ws.mock.integration.CustomerCountResponse diff --git a/test/src/test/resources/org/springframework/ws/mock/server/integration/integration-test.xml b/test/src/test/resources/org/springframework/ws/mock/server/integration/integration-test.xml new file mode 100644 index 00000000..b3fce516 --- /dev/null +++ b/test/src/test/resources/org/springframework/ws/mock/server/integration/integration-test.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + org.springframework.ws.mock.integration.CustomerCountRequest + org.springframework.ws.mock.integration.CustomerCountResponse + + + + + \ No newline at end of file