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.client;
18
19 import java.io.IOException;
20 import java.net.URI;
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.WebServiceMessage;
28 import org.springframework.ws.test.support.matcher.PayloadDiffMatcher;
29 import org.springframework.ws.test.support.matcher.SchemaValidatingMatcher;
30 import org.springframework.ws.test.support.matcher.SoapEnvelopeDiffMatcher;
31 import org.springframework.ws.test.support.matcher.SoapHeaderMatcher;
32 import org.springframework.xml.transform.ResourceSource;
33
34 /**
35 * Factory methods for {@link RequestMatcher} classes. Typically used to provide input for {@link
36 * MockWebServiceServer#expect(RequestMatcher)}.
37 *
38 * @author Arjen Poutsma
39 * @since 2.0
40 */
41 public abstract class RequestMatchers {
42
43 private RequestMatchers() {
44 }
45
46 /**
47 * Expects any request.
48 *
49 * @return the request matcher
50 */
51 public static RequestMatcher anything() {
52 return new RequestMatcher() {
53 public void match(URI uri, WebServiceMessage request) throws IOException, AssertionError {
54 }
55 };
56 }
57
58 // Payload
59
60 /**
61 * Expects the given {@link javax.xml.transform.Source} XML payload.
62 *
63 * @param payload the XML payload
64 * @return the request matcher
65 */
66 public static RequestMatcher payload(Source payload) {
67 Assert.notNull(payload, "'payload' must not be null");
68 return new WebServiceMessageMatcherAdapter(new PayloadDiffMatcher(payload));
69 }
70
71 /**
72 * Expects the given {@link org.springframework.core.io.Resource} XML payload.
73 *
74 * @param payload the XML payload
75 * @return the request matcher
76 */
77 public static RequestMatcher payload(Resource payload) throws IOException {
78 Assert.notNull(payload, "'payload' must not be null");
79 return payload(new ResourceSource(payload));
80 }
81
82 /**
83 * Expects the payload to validate against the given XSD schema(s).
84 *
85 * @param schema the schema
86 * @param furtherSchemas further schemas, if necessary
87 * @return the request matcher
88 */
89 public static RequestMatcher validPayload(Resource schema, Resource... furtherSchemas) throws IOException {
90 return new WebServiceMessageMatcherAdapter(new SchemaValidatingMatcher(schema, furtherSchemas));
91 }
92
93 /**
94 * Expects the given XPath expression to (not) exist or be evaluated to a value.
95 *
96 * @param xpathExpression the XPath expression
97 * @return the XPath expectations, to be further configured
98 */
99 public static RequestXPathExpectations xpath(String xpathExpression) {
100 return new XPathExpectationsHelperAdapter(xpathExpression, null);
101 }
102
103 /**
104 * Expects the given XPath expression to (not) exist or be evaluated to a value.
105 *
106 * @param xpathExpression the XPath expression
107 * @param namespaceMapping the namespaces
108 * @return the XPath expectations, to be further configured
109 */
110 public static RequestXPathExpectations xpath(String xpathExpression, Map<String, String> namespaceMapping) {
111 return new XPathExpectationsHelperAdapter(xpathExpression, namespaceMapping);
112 }
113
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
140 /**
141 * Expects the given SOAP header in the outgoing message.
142 *
143 * @param soapHeaderName the qualified name of the SOAP header to expect
144 * @return the request matcher
145 */
146 public static RequestMatcher soapHeader(QName soapHeaderName) {
147 Assert.notNull(soapHeaderName, "'soapHeaderName' must not be null");
148 return new WebServiceMessageMatcherAdapter(new SoapHeaderMatcher(soapHeaderName));
149 }
150
151 // Other
152
153 /**
154 * Expects a connection to the given URI.
155 *
156 * @param uri the String uri expected to connect to
157 * @return the request matcher
158 */
159 public static RequestMatcher connectionTo(String uri) {
160 Assert.notNull(uri, "'uri' must not be null");
161 return connectionTo(URI.create(uri));
162 }
163
164 /**
165 * Expects a connection to the given URI.
166 *
167 * @param uri the String uri expected to connect to
168 * @return the request matcher
169 */
170 public static RequestMatcher connectionTo(URI uri) {
171 Assert.notNull(uri, "'uri' must not be null");
172 return new UriMatcher(uri);
173 }
174
175 }