1   /*
2    * Copyright 2005-2010 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.Collections;
22  import java.util.Locale;
23  import java.util.Map;
24  import javax.xml.namespace.QName;
25  import javax.xml.soap.MessageFactory;
26  import javax.xml.transform.Source;
27  import javax.xml.transform.TransformerException;
28  
29  import org.springframework.context.support.StaticApplicationContext;
30  import org.springframework.core.io.ByteArrayResource;
31  import org.springframework.core.io.Resource;
32  import org.springframework.ws.WebServiceMessage;
33  import org.springframework.ws.client.core.WebServiceMessageCallback;
34  import org.springframework.ws.client.core.WebServiceTemplate;
35  import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
36  import org.springframework.ws.soap.SoapMessage;
37  import org.springframework.ws.soap.client.SoapFaultClientException;
38  import org.springframework.ws.soap.saaj.SaajSoapMessage;
39  import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
40  import org.springframework.xml.transform.StringResult;
41  import org.springframework.xml.transform.StringSource;
42  
43  import org.junit.Before;
44  import org.junit.Test;
45  
46  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
47  import static org.easymock.EasyMock.*;
48  import static org.junit.Assert.assertNotNull;
49  import static org.springframework.ws.test.client.RequestMatchers.*;
50  import static org.springframework.ws.test.client.ResponseCreators.withClientOrSenderFault;
51  import static org.springframework.ws.test.client.ResponseCreators.withPayload;
52  
53  public class MockWebServiceServerTest {
54  
55      private WebServiceTemplate template;
56  
57      private MockWebServiceServer server;
58  
59      @Before
60      public void setUp() throws Exception {
61          template = new WebServiceTemplate();
62          template.setDefaultUri("http://example.com");
63  
64          server = MockWebServiceServer.createServer(template);
65      }
66  
67      @Test
68      public void createServerWebServiceTemplate() throws Exception {
69          WebServiceTemplate template = new WebServiceTemplate();
70  
71          MockWebServiceServer server = MockWebServiceServer.createServer(template);
72          assertNotNull(server);
73      }
74  
75      @Test
76      public void createServerGatewaySupport() throws Exception {
77          MyClient client = new MyClient();
78  
79          MockWebServiceServer server = MockWebServiceServer.createServer(client);
80          assertNotNull(server);
81      }
82  
83      @Test
84      public void createServerApplicationContextWebServiceTemplate() throws Exception {
85          StaticApplicationContext applicationContext = new StaticApplicationContext();
86          applicationContext.registerSingleton("webServiceTemplate", WebServiceTemplate.class);
87          applicationContext.refresh();
88  
89          MockWebServiceServer server = MockWebServiceServer.createServer(applicationContext);
90          assertNotNull(server);
91      }
92  
93      @Test
94      public void createServerApplicationContextWebServiceGatewaySupport() throws Exception {
95          StaticApplicationContext applicationContext = new StaticApplicationContext();
96          applicationContext.registerSingleton("myClient", MyClient.class);
97          applicationContext.refresh();
98  
99          MockWebServiceServer server = MockWebServiceServer.createServer(applicationContext);
100         assertNotNull(server);
101     }
102 
103     @Test(expected = IllegalArgumentException.class)
104     public void createServerApplicationContextEmpty() throws Exception {
105         StaticApplicationContext applicationContext = new StaticApplicationContext();
106         applicationContext.refresh();
107 
108         MockWebServiceServer server = MockWebServiceServer.createServer(applicationContext);
109         assertNotNull(server);
110     }
111 
112     @Test
113     public void mocks() throws Exception {
114         URI uri = URI.create("http://example.com");
115 
116         RequestMatcher requestMatcher1 = createStrictMock("requestMatcher1", RequestMatcher.class);
117         RequestMatcher requestMatcher2 = createStrictMock("requestMatcher2", RequestMatcher.class);
118         ResponseCreator responseCreator = createStrictMock(ResponseCreator.class);
119 
120         SaajSoapMessage response = new SaajSoapMessageFactory(MessageFactory.newInstance()).createWebServiceMessage();
121 
122         requestMatcher1.match(eq(uri), isA(SaajSoapMessage.class));
123         requestMatcher2.match(eq(uri), isA(SaajSoapMessage.class));
124         expect(responseCreator.createResponse(eq(uri), isA(SaajSoapMessage.class), isA(SaajSoapMessageFactory.class)))
125                 .andReturn(response);
126 
127         replay(requestMatcher1, requestMatcher2, responseCreator);
128 
129         server.expect(requestMatcher1).andExpect(requestMatcher2).andRespond(responseCreator);
130         template.sendSourceAndReceiveToResult(uri.toString(), new StringSource("<request xmlns='http://example.com'/>"),
131                 new StringResult());
132 
133         verify(requestMatcher1, requestMatcher2, responseCreator);
134     }
135 
136     @Test
137     public void payloadMatch() throws Exception {
138         Source request = new StringSource("<request xmlns='http://example.com'/>");
139         Source response = new StringSource("<response xmlns='http://example.com'/>");
140 
141         server.expect(payload(request)).andRespond(withPayload(response));
142 
143         StringResult result = new StringResult();
144         template.sendSourceAndReceiveToResult(request, result);
145         assertXMLEqual(result.toString(), response.toString());
146     }
147 
148     @Test(expected = AssertionError.class)
149     public void payloadNonMatch() throws Exception {
150         Source expected = new StringSource("<request xmlns='http://example.com'/>");
151 
152         server.expect(payload(expected));
153 
154         StringResult result = new StringResult();
155         String actual = "<request xmlns='http://other.com'/>";
156         template.sendSourceAndReceiveToResult(new StringSource(actual), result);
157     }
158 
159     @Test
160     public void soapHeaderMatch() throws Exception {
161         final QName soapHeaderName = new QName("http://example.com", "mySoapHeader");
162 
163         server.expect(soapHeader(soapHeaderName));
164 
165         template.sendSourceAndReceiveToResult(new StringSource("<request xmlns='http://example.com'/>"),
166                 new WebServiceMessageCallback() {
167                     public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
168                         SoapMessage soapMessage = (SoapMessage) message;
169                         soapMessage.getSoapHeader().addHeaderElement(soapHeaderName);
170                     }
171                 }, new StringResult());
172     }
173 
174     @Test(expected = AssertionError.class)
175     public void soapHeaderNonMatch() throws Exception {
176         QName soapHeaderName = new QName("http://example.com", "mySoapHeader");
177 
178         server.expect(soapHeader(soapHeaderName));
179 
180         template.sendSourceAndReceiveToResult(new StringSource("<request xmlns='http://example.com'/>"),
181                 new StringResult());
182     }
183 
184     @Test
185     public void connectionMatch() throws Exception {
186         String uri = "http://example.com";
187         server.expect(connectionTo(uri));
188 
189         template.sendSourceAndReceiveToResult(uri, new StringSource("<request xmlns='http://example.com'/>"),
190                 new StringResult());
191     }
192 
193     @Test(expected = AssertionError.class)
194     public void connectionNonMatch() throws Exception {
195         String expected = "http://expected.com";
196         server.expect(connectionTo(expected));
197 
198         String actual = "http://actual.com";
199         template.sendSourceAndReceiveToResult(actual, new StringSource("<request xmlns='http://example.com'/>"),
200                 new StringResult());
201     }
202 
203     @Test(expected = AssertionError.class)
204     public void unexpectedConnection() throws Exception {
205         Source request = new StringSource("<request xmlns='http://example.com'/>");
206         Source response = new StringSource("<response xmlns='http://example.com'/>");
207 
208         server.expect(payload(request)).andRespond(withPayload(response));
209 
210         template.sendSourceAndReceiveToResult(request, new StringResult());
211         template.sendSourceAndReceiveToResult(request, new StringResult());
212     }
213 
214     @Test
215     public void xsdMatch() throws Exception {
216         Resource schema = new ByteArrayResource(
217                 "<schema xmlns=\"http://www.w3.org/2001/XMLSchema\" targetNamespace=\"http://example.com\" elementFormDefault=\"qualified\"><element name=\"request\"/></schema>".getBytes());
218 
219         server.expect(validPayload(schema));
220 
221         StringResult result = new StringResult();
222         String actual = "<request xmlns='http://example.com'/>";
223         template.sendSourceAndReceiveToResult(new StringSource(actual), result);
224     }
225 
226     @Test(expected = AssertionError.class)
227     public void xsdNonMatch() throws Exception {
228         Resource schema = new ByteArrayResource(
229                 "<schema xmlns=\"http://www.w3.org/2001/XMLSchema\" targetNamespace=\"http://example.com\" elementFormDefault=\"qualified\"><element name=\"request\"/></schema>".getBytes());
230 
231         server.expect(validPayload(schema));
232 
233         StringResult result = new StringResult();
234         String actual = "<request2 xmlns='http://example.com'/>";
235         template.sendSourceAndReceiveToResult(new StringSource(actual), result);
236     }
237 
238     @Test
239     public void xpathExistsMatch() throws Exception {
240         final Map<String, String> ns = Collections.singletonMap("ns", "http://example.com");
241 
242         server.expect(xpath("/ns:request", ns).exists());
243 
244         template.sendSourceAndReceiveToResult(new StringSource("<request xmlns='http://example.com'/>"),
245                 new StringResult());
246     }
247 
248     @Test(expected = AssertionError.class)
249     public void xpathExistsNonMatch() throws Exception {
250         final Map<String, String> ns = Collections.singletonMap("ns", "http://example.com");
251 
252         server.expect(xpath("/ns:foo", ns).exists());
253 
254         template.sendSourceAndReceiveToResult(new StringSource("<request xmlns='http://example.com'/>"),
255                 new StringResult());
256     }
257 
258     @Test
259     public void anythingMatch() throws Exception {
260         Source request = new StringSource("<request xmlns='http://example.com'/>");
261         Source response = new StringSource("<response xmlns='http://example.com'/>");
262 
263         server.expect(anything()).andRespond(withPayload(response));
264 
265         StringResult result = new StringResult();
266         template.sendSourceAndReceiveToResult(request, result);
267         assertXMLEqual(result.toString(), response.toString());
268 
269         server.verify();
270     }
271 
272     @Test(expected = IllegalStateException.class)
273     public void recordWhenReplay() throws Exception {
274         Source request = new StringSource("<request xmlns='http://example.com'/>");
275         Source response = new StringSource("<response xmlns='http://example.com'/>");
276 
277         server.expect(anything()).andRespond(withPayload(response));
278         server.expect(anything()).andRespond(withPayload(response));
279 
280         StringResult result = new StringResult();
281         template.sendSourceAndReceiveToResult(request, result);
282         assertXMLEqual(result.toString(), response.toString());
283 
284         server.expect(anything()).andRespond(withPayload(response));
285     }
286 
287     @Test(expected = AssertionError.class)
288     public void verifyFailure() throws Exception {
289         server.expect(anything());
290         server.verify();
291     }
292 
293     @Test
294     public void verifyOnly() throws Exception {
295         server.verify();
296     }
297 
298     @Test(expected = SoapFaultClientException.class)
299     public void fault() throws Exception {
300         Source request = new StringSource("<request xmlns='http://example.com'/>");
301 
302         server.expect(anything()).andRespond(withClientOrSenderFault("reason", Locale.ENGLISH));
303 
304         StringResult result = new StringResult();
305         template.sendSourceAndReceiveToResult(request, result);
306     }
307        
308     public static class MyClient extends WebServiceGatewaySupport {
309 
310     }
311 }