Skip to content

Commit 71e81b0

Browse files
committed
SWS-786 - SOAP envelope Matcher / MessageCreator
1 parent 690eebc commit 71e81b0

File tree

9 files changed

+383
-17
lines changed

9 files changed

+383
-17
lines changed

core/src/main/java/org/springframework/ws/soap/SoapMessage.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2005-2010 the original author or authors.
2+
* Copyright 2005-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -85,10 +85,9 @@ public interface SoapMessage extends MimeMessage, FaultAwareWebServiceMessage {
8585
Document getDocument();
8686

8787
/**
88-
* Returns this message as a {@link Document}.
88+
* Sets the contents of the message to the given {@link Document}.
8989
*
90-
* Depending on the underlying implementation, this Document may be 'live' or not.
91-
* @return this soap message as a DOM document
90+
* @param document the soap message as a DOM document
9291
*/
9392
void setDocument(Document document);
9493

test/src/main/java/org/springframework/ws/test/client/RequestMatchers.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2005-2010 the original author or authors.
2+
* Copyright 2005-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -27,6 +27,7 @@
2727
import org.springframework.ws.WebServiceMessage;
2828
import org.springframework.ws.test.support.matcher.PayloadDiffMatcher;
2929
import org.springframework.ws.test.support.matcher.SchemaValidatingMatcher;
30+
import org.springframework.ws.test.support.matcher.SoapEnvelopeDiffMatcher;
3031
import org.springframework.ws.test.support.matcher.SoapHeaderMatcher;
3132
import org.springframework.xml.transform.ResourceSource;
3233

@@ -54,6 +55,8 @@ public void match(URI uri, WebServiceMessage request) throws IOException, Assert
5455
};
5556
}
5657

58+
// Payload
59+
5760
/**
5861
* Expects the given {@link javax.xml.transform.Source} XML payload.
5962
*
@@ -108,6 +111,32 @@ public static RequestXPathExpectations xpath(String xpathExpression, Map<String,
108111
return new XPathExpectationsHelperAdapter(xpathExpression, namespaceMapping);
109112
}
110113

114+
// SOAP
115+
116+
/**
117+
* Expects the given {@link javax.xml.transform.Source} XML SOAP envelope.
118+
*
119+
* @param soapEnvelope the XML SOAP envelope
120+
* @return the request matcher
121+
* @since 2.1.1
122+
*/
123+
public static RequestMatcher soapEnvelope(Source soapEnvelope) {
124+
Assert.notNull(soapEnvelope, "'soapEnvelope' must not be null");
125+
return new WebServiceMessageMatcherAdapter(new SoapEnvelopeDiffMatcher(soapEnvelope));
126+
}
127+
128+
/**
129+
* Expects the given {@link org.springframework.core.io.Resource} XML SOAP envelope.
130+
*
131+
* @param soapEnvelope the XML SOAP envelope
132+
* @return the request matcher
133+
* @since 2.1.1
134+
*/
135+
public static RequestMatcher soapEnvelope(Resource soapEnvelope) throws IOException {
136+
Assert.notNull(soapEnvelope, "'soapEnvelope' must not be null");
137+
return soapEnvelope(new ResourceSource(soapEnvelope));
138+
}
139+
111140
/**
112141
* Expects the given SOAP header in the outgoing message.
113142
*
@@ -119,6 +148,8 @@ public static RequestMatcher soapHeader(QName soapHeaderName) {
119148
return new WebServiceMessageMatcherAdapter(new SoapHeaderMatcher(soapHeaderName));
120149
}
121150

151+
// Other
152+
122153
/**
123154
* Expects a connection to the given URI.
124155
*

test/src/main/java/org/springframework/ws/test/client/ResponseCreators.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2005-2010 the original author or authors.
2+
* Copyright 2005-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -27,6 +27,7 @@
2727
import org.springframework.ws.WebServiceMessageFactory;
2828
import org.springframework.ws.soap.SoapBody;
2929
import org.springframework.ws.test.support.creator.PayloadMessageCreator;
30+
import org.springframework.ws.test.support.creator.SoapEnvelopeMessageCreator;
3031
import org.springframework.ws.test.support.creator.WebServiceMessageCreator;
3132
import org.springframework.xml.transform.ResourceSource;
3233

@@ -42,6 +43,8 @@ public abstract class ResponseCreators {
4243
private ResponseCreators() {
4344
}
4445

46+
// Payload
47+
4548
/**
4649
* Respond with the given {@link javax.xml.transform.Source} XML as payload response.
4750
*
@@ -64,6 +67,8 @@ public static ResponseCreator withPayload(Resource payload) throws IOException {
6467
return withPayload(new ResourceSource(payload));
6568
}
6669

70+
// Error/Exception
71+
6772
/**
6873
* Respond with an error.
6974
*
@@ -99,6 +104,33 @@ public static ResponseCreator withException(RuntimeException ex) {
99104
return new ExceptionResponseCreator(ex);
100105
}
101106

107+
// SOAP
108+
109+
/**
110+
* Respond with the given {@link javax.xml.transform.Source} XML as SOAP envelope response.
111+
*
112+
* @param soapEnvelope the response SOAP envelope
113+
* @return the response callback
114+
* @since 2.1.1
115+
*/
116+
public static ResponseCreator withSoapEnvelope(Source soapEnvelope) {
117+
Assert.notNull(soapEnvelope, "'soapEnvelope' must not be null");
118+
return new WebServiceMessageCreatorAdapter(new SoapEnvelopeMessageCreator(soapEnvelope));
119+
}
120+
121+
/**
122+
* Respond with the given {@link org.springframework.core.io.Resource} XML as SOAP envelope response.
123+
*
124+
* @param soapEnvelope the response SOAP envelope
125+
* @return the response callback
126+
* @since 2.1.1
127+
*/
128+
public static ResponseCreator withSoapEnvelope(Resource soapEnvelope) throws IOException {
129+
Assert.notNull(soapEnvelope, "'soapEnvelope' must not be null");
130+
return withSoapEnvelope(new ResourceSource(soapEnvelope));
131+
}
132+
133+
102134
/**
103135
* Respond with a {@code MustUnderstand} fault.
104136
*

test/src/main/java/org/springframework/ws/test/server/RequestCreators.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2005-2010 the original author or authors.
2+
* Copyright 2005-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -24,6 +24,7 @@
2424
import org.springframework.ws.WebServiceMessage;
2525
import org.springframework.ws.WebServiceMessageFactory;
2626
import org.springframework.ws.test.support.creator.PayloadMessageCreator;
27+
import org.springframework.ws.test.support.creator.SoapEnvelopeMessageCreator;
2728
import org.springframework.ws.test.support.creator.WebServiceMessageCreator;
2829
import org.springframework.xml.transform.ResourceSource;
2930

@@ -39,6 +40,8 @@ public abstract class RequestCreators {
3940
private RequestCreators() {
4041
}
4142

43+
// Payload
44+
4245
/**
4346
* Create a request with the given {@link Source} XML as payload.
4447
*
@@ -61,6 +64,32 @@ public static RequestCreator withPayload(Resource payload) throws IOException {
6164
return withPayload(new ResourceSource(payload));
6265
}
6366

67+
// SOAP
68+
69+
/**
70+
* Create a request with the given {@link Source} XML as SOAP envelope.
71+
*
72+
* @param soapEnvelope the request SOAP envelope
73+
* @return the request creator
74+
* @since 2.1.1
75+
*/
76+
public static RequestCreator withSoapEnvelope(Source soapEnvelope) {
77+
Assert.notNull(soapEnvelope, "'soapEnvelope' must not be null");
78+
return new WebServiceMessageCreatorAdapter(new SoapEnvelopeMessageCreator(soapEnvelope));
79+
}
80+
81+
/**
82+
* Create a request with the given {@link Resource} XML as SOAP envelope.
83+
*
84+
* @param soapEnvelope the request SOAP envelope
85+
* @return the request creator
86+
* @since 2.1.1
87+
*/
88+
public static RequestCreator withSoapEnvelope(Resource soapEnvelope) throws IOException {
89+
Assert.notNull(soapEnvelope, "'soapEnvelope' must not be null");
90+
return withSoapEnvelope(new ResourceSource(soapEnvelope));
91+
}
92+
6493
/**
6594
* Adapts a {@link WebServiceMessageCreator} to the {@link RequestCreator} contract.
6695
*/

test/src/main/java/org/springframework/ws/test/server/ResponseMatchers.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2005-2010 the original author or authors.
2+
* Copyright 2005-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -29,6 +29,7 @@
2929
import org.springframework.ws.soap.SoapVersion;
3030
import org.springframework.ws.test.support.matcher.PayloadDiffMatcher;
3131
import org.springframework.ws.test.support.matcher.SchemaValidatingMatcher;
32+
import org.springframework.ws.test.support.matcher.SoapEnvelopeDiffMatcher;
3233
import org.springframework.ws.test.support.matcher.SoapHeaderMatcher;
3334
import org.springframework.xml.transform.ResourceSource;
3435

@@ -45,7 +46,7 @@ public abstract class ResponseMatchers {
4546

4647
private ResponseMatchers() {
4748
}
48-
49+
4950
// Payload
5051

5152
/**
@@ -100,9 +101,30 @@ public static ResponseXPathExpectations xpath(String xpathExpression, Map<String
100101
return new XPathExpectationsHelperAdapter(xpathExpression, namespaceMapping);
101102
}
102103

103-
104104
// SOAP
105105

106+
/**
107+
* Expects the given {@link Source} XML SOAP envelope.
108+
*
109+
* @param soapEnvelope the XML SOAP envelope
110+
* @return the response matcher
111+
* @since 2.1.1
112+
*/
113+
public static ResponseMatcher soapEnvelope(Source soapEnvelope) {
114+
return new WebServiceMessageMatcherAdapter(new SoapEnvelopeDiffMatcher(soapEnvelope));
115+
}
116+
117+
/**
118+
* Expects the given {@link Resource} XML SOAP envelope.
119+
*
120+
* @param soapEnvelope the XML SOAP envelope
121+
* @return the response matcher
122+
* @since 2.1.1
123+
*/
124+
public static ResponseMatcher soapEnvelope(Resource soapEnvelope) throws IOException {
125+
return soapEnvelope(new ResourceSource(soapEnvelope));
126+
}
127+
106128
/**
107129
* Expects the given SOAP header in the outgoing message.
108130
*
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2005-2012 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+
* http://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.ws.test.support.creator;
18+
19+
import java.io.IOException;
20+
import javax.xml.transform.Source;
21+
import javax.xml.transform.TransformerException;
22+
import javax.xml.transform.dom.DOMResult;
23+
24+
import org.springframework.util.Assert;
25+
import org.springframework.ws.WebServiceMessage;
26+
import org.springframework.ws.soap.SoapMessage;
27+
import org.springframework.xml.transform.TransformerHelper;
28+
29+
import org.w3c.dom.Document;
30+
31+
import static org.springframework.ws.test.support.AssertionErrors.assertTrue;
32+
import static org.springframework.ws.test.support.AssertionErrors.fail;
33+
34+
/**
35+
* Implementation of {@link WebServiceMessageCreator} that creates a request based on a SOAP envelope {@link Source}.
36+
*
37+
* @author Alexander Shutyaev
38+
* @since 2.1.1
39+
*/
40+
public class SoapEnvelopeMessageCreator extends AbstractMessageCreator {
41+
42+
private final Source soapEnvelope;
43+
44+
private final TransformerHelper transformerHelper = new TransformerHelper();
45+
46+
/**
47+
* Creates a new instance of the {@code SoapEnvelopeMessageCreator} with the given SOAP envelope source.
48+
*
49+
* @param soapEnvelope the SOAP envelope source
50+
*/
51+
public SoapEnvelopeMessageCreator(Source soapEnvelope) {
52+
Assert.notNull(soapEnvelope, "'soapEnvelope' must not be null");
53+
this.soapEnvelope = soapEnvelope;
54+
}
55+
56+
@Override
57+
protected void doWithMessage(WebServiceMessage message) throws IOException {
58+
assertTrue("Message created with factory is not a SOAP message", message instanceof SoapMessage);
59+
SoapMessage soapMessage = (SoapMessage) message;
60+
try {
61+
DOMResult result = new DOMResult();
62+
transformerHelper.transform(soapEnvelope, result);
63+
soapMessage.setDocument((Document) result.getNode());
64+
}
65+
catch (TransformerException ex) {
66+
fail("Could not transform request SOAP envelope to message: " + ex.getMessage());
67+
}
68+
}
69+
70+
}

0 commit comments

Comments
 (0)