1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.test.server;
18
19 import java.io.IOException;
20 import java.util.Locale;
21 import java.util.Map;
22 import javax.xml.namespace.QName;
23 import javax.xml.transform.Source;
24
25 import org.springframework.core.io.Resource;
26 import org.springframework.util.Assert;
27 import org.springframework.ws.FaultAwareWebServiceMessage;
28 import org.springframework.ws.WebServiceMessage;
29 import org.springframework.ws.soap.SoapVersion;
30 import org.springframework.ws.test.support.matcher.PayloadDiffMatcher;
31 import org.springframework.ws.test.support.matcher.SchemaValidatingMatcher;
32 import org.springframework.ws.test.support.matcher.SoapEnvelopeDiffMatcher;
33 import org.springframework.ws.test.support.matcher.SoapHeaderMatcher;
34 import org.springframework.xml.transform.ResourceSource;
35
36 import static org.springframework.ws.test.support.AssertionErrors.fail;
37
38
39
40
41
42
43
44
45 public abstract class ResponseMatchers {
46
47 private ResponseMatchers() {
48 }
49
50
51
52
53
54
55
56
57
58 public static ResponseMatcher payload(Source payload) {
59 return new WebServiceMessageMatcherAdapter(new PayloadDiffMatcher(payload));
60 }
61
62
63
64
65
66
67
68 public static ResponseMatcher payload(Resource payload) throws IOException {
69 return payload(new ResourceSource(payload));
70 }
71
72
73
74
75
76
77
78
79 public static ResponseMatcher validPayload(Resource schema, Resource... furtherSchemas) throws IOException {
80 return new WebServiceMessageMatcherAdapter(new SchemaValidatingMatcher(schema, furtherSchemas));
81 }
82
83
84
85
86
87
88
89 public static ResponseXPathExpectations xpath(String xpathExpression) {
90 return new XPathExpectationsHelperAdapter(xpathExpression, null);
91 }
92
93
94
95
96
97
98
99
100 public static ResponseXPathExpectations xpath(String xpathExpression, Map<String, String> namespaceMapping) {
101 return new XPathExpectationsHelperAdapter(xpathExpression, namespaceMapping);
102 }
103
104
105
106
107
108
109
110
111
112
113 public static ResponseMatcher soapEnvelope(Source soapEnvelope) {
114 return new WebServiceMessageMatcherAdapter(new SoapEnvelopeDiffMatcher(soapEnvelope));
115 }
116
117
118
119
120
121
122
123
124 public static ResponseMatcher soapEnvelope(Resource soapEnvelope) throws IOException {
125 return soapEnvelope(new ResourceSource(soapEnvelope));
126 }
127
128
129
130
131
132
133
134 public static ResponseMatcher soapHeader(QName soapHeaderName) {
135 Assert.notNull(soapHeaderName, "'soapHeaderName' must not be null");
136 return new WebServiceMessageMatcherAdapter(new SoapHeaderMatcher(soapHeaderName));
137 }
138
139
140
141
142
143
144 public static ResponseMatcher noFault() {
145 return new ResponseMatcher() {
146 public void match(WebServiceMessage request, WebServiceMessage response)
147 throws IOException, AssertionError {
148 if (response instanceof FaultAwareWebServiceMessage) {
149 FaultAwareWebServiceMessage faultMessage = (FaultAwareWebServiceMessage) response;
150 if (faultMessage.hasFault()) {
151 fail("Response has a SOAP Fault: \"" + faultMessage.getFaultReason() + "\"");
152 }
153 }
154 }
155
156 };
157 }
158
159
160
161
162
163
164 public static ResponseMatcher mustUnderstandFault() {
165 return mustUnderstandFault(null);
166 }
167
168
169
170
171
172
173
174
175 public static ResponseMatcher mustUnderstandFault(String faultStringOrReason) {
176 return new SoapFaultResponseMatcher(faultStringOrReason) {
177 @Override
178 protected QName getExpectedFaultCode(SoapVersion version) {
179 return version.getMustUnderstandFaultName();
180 }
181 };
182 }
183
184
185
186
187
188
189 public static ResponseMatcher clientOrSenderFault() {
190 return clientOrSenderFault(null);
191 }
192
193
194
195
196
197
198
199
200 public static ResponseMatcher clientOrSenderFault(String faultStringOrReason) {
201 return new SoapFaultResponseMatcher(faultStringOrReason) {
202 @Override
203 protected QName getExpectedFaultCode(SoapVersion version) {
204 return version.getClientOrSenderFaultName();
205 }
206 };
207 }
208
209
210
211
212
213
214 public static ResponseMatcher serverOrReceiverFault() {
215 return serverOrReceiverFault(null);
216 }
217
218
219
220
221
222
223
224
225 public static ResponseMatcher serverOrReceiverFault(String faultStringOrReason) {
226 return new SoapFaultResponseMatcher(faultStringOrReason) {
227 @Override
228 protected QName getExpectedFaultCode(SoapVersion version) {
229 return version.getServerOrReceiverFaultName();
230 }
231 };
232 }
233
234
235
236
237
238
239 public static ResponseMatcher versionMismatchFault() {
240 return versionMismatchFault(null);
241 }
242
243
244
245
246
247
248
249
250 public static ResponseMatcher versionMismatchFault(String faultStringOrReason) {
251 return new SoapFaultResponseMatcher(faultStringOrReason) {
252 @Override
253 protected QName getExpectedFaultCode(SoapVersion version) {
254 return version.getVersionMismatchFaultName();
255 }
256 };
257 }
258
259 }