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.util.Locale;
21  import javax.xml.namespace.QName;
22  import javax.xml.transform.Result;
23  import javax.xml.transform.TransformerException;
24  import javax.xml.transform.dom.DOMSource;
25  
26  import org.springframework.core.io.ByteArrayResource;
27  import org.springframework.ws.WebServiceMessage;
28  import org.springframework.ws.soap.SoapMessage;
29  import org.springframework.ws.soap.SoapVersion;
30  import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
31  import org.springframework.ws.soap.soap11.Soap11Fault;
32  import org.springframework.xml.transform.StringResult;
33  import org.springframework.xml.transform.StringSource;
34  import org.springframework.xml.transform.TransformerHelper;
35  
36  import org.junit.Before;
37  import org.junit.Test;
38  
39  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
40  import static org.junit.Assert.*;
41  
42  public class ResponseCreatorsTest {
43  
44      private final TransformerHelper transformerHelper = new TransformerHelper();
45  
46      private SaajSoapMessageFactory messageFactory;
47  
48      @Before
49      public void createMessageFactory() {
50          messageFactory = new SaajSoapMessageFactory();
51          messageFactory.afterPropertiesSet();
52      }
53  
54      @Test
55      public void withPayloadSource() throws Exception {
56          String payload = "<payload xmlns='http://springframework.org'/>";
57          ResponseCreator responseCreator = ResponseCreators.withPayload(new StringSource(payload));
58  
59          WebServiceMessage response = responseCreator.createResponse(null, null, messageFactory);
60  
61          assertXMLEqual(payload, getPayloadAsString(response));
62  
63      }
64  
65      @Test
66      public void withPayloadResource() throws Exception {
67          String payload = "<payload xmlns='http://springframework.org'/>";
68          ResponseCreator responseCreator =
69                  ResponseCreators.withPayload(new ByteArrayResource(payload.getBytes("UTF-8")));
70  
71          WebServiceMessage response = responseCreator.createResponse(null, null, messageFactory);
72  
73          assertXMLEqual(payload, getPayloadAsString(response));
74      }
75      
76      @Test
77      public void withSoapEnvelopeSource() throws Exception {
78      	StringBuilder xmlBuilder = new StringBuilder();
79  		xmlBuilder.append("<?xml version='1.0'?>");
80  		xmlBuilder.append("<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'>");
81  		xmlBuilder.append("<soap:Header><header xmlns='http://springframework.org'/></soap:Header>");
82  		xmlBuilder.append("<soap:Body><payload xmlns='http://springframework.org'/></soap:Body>");
83  		xmlBuilder.append("</soap:Envelope>");
84  		String envelope = xmlBuilder.toString();
85  		ResponseCreator responseCreator = ResponseCreators.withSoapEnvelope(new StringSource(envelope));
86          WebServiceMessage response = responseCreator.createResponse(null, null, messageFactory);
87          assertXMLEqual(envelope, getSoapEnvelopeAsString((SoapMessage)response));
88      }
89      
90      @Test
91      public void withSoapEnvelopeResource() throws Exception {
92      	StringBuilder xmlBuilder = new StringBuilder();
93  		xmlBuilder.append("<?xml version='1.0'?>");
94  		xmlBuilder.append("<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'>");
95  		xmlBuilder.append("<soap:Header><header xmlns='http://springframework.org'/></soap:Header>");
96  		xmlBuilder.append("<soap:Body><payload xmlns='http://springframework.org'/></soap:Body>");
97  		xmlBuilder.append("</soap:Envelope>");
98  		String envelope = xmlBuilder.toString();
99  		ResponseCreator responseCreator = ResponseCreators.withSoapEnvelope(new ByteArrayResource(envelope.getBytes("UTF-8")));
100         WebServiceMessage response = responseCreator.createResponse(null, null, messageFactory);
101         assertXMLEqual(envelope, getSoapEnvelopeAsString((SoapMessage)response));
102     }
103 
104     @Test
105     public void withIOException() throws Exception {
106         IOException expected = new IOException("Foo");
107         ResponseCreator responseCreator = ResponseCreators.withException(expected);
108 
109         try {
110             responseCreator.createResponse(null, null, null);
111         }
112         catch (IOException actual) {
113             assertSame(expected, actual);
114         }
115     }
116     
117     @Test
118     public void withRuntimeException() throws Exception {
119         RuntimeException expected = new RuntimeException("Foo");
120         ResponseCreator responseCreator = ResponseCreators.withException(expected);
121 
122         try {
123             responseCreator.createResponse(null, null, null);
124         }
125         catch (RuntimeException actual) {
126             assertSame(expected, actual);
127         }
128     }
129 
130     @Test
131     public void withMustUnderstandFault() throws Exception {
132         String faultString = "Foo";
133         ResponseCreator responseCreator = ResponseCreators.withMustUnderstandFault(faultString, Locale.ENGLISH);
134 
135         testFault(responseCreator, faultString, SoapVersion.SOAP_11.getMustUnderstandFaultName());
136     }
137 
138     @Test
139     public void withClientOrSenderFault() throws Exception {
140         String faultString = "Foo";
141         ResponseCreator responseCreator = ResponseCreators.withClientOrSenderFault(faultString, Locale.ENGLISH);
142 
143         testFault(responseCreator, faultString, SoapVersion.SOAP_11.getClientOrSenderFaultName());
144     }
145 
146     @Test
147     public void withServerOrReceiverFault() throws Exception {
148         String faultString = "Foo";
149         ResponseCreator responseCreator = ResponseCreators.withServerOrReceiverFault(faultString, Locale.ENGLISH);
150 
151         testFault(responseCreator, faultString, SoapVersion.SOAP_11.getServerOrReceiverFaultName());
152     }
153 
154     @Test
155     public void withVersionMismatchFault() throws Exception {
156         String faultString = "Foo";
157         ResponseCreator responseCreator = ResponseCreators.withVersionMismatchFault(faultString, Locale.ENGLISH);
158 
159         testFault(responseCreator, faultString, SoapVersion.SOAP_11.getVersionMismatchFaultName());
160     }
161     
162     private void testFault(ResponseCreator responseCreator, String faultString, QName faultCode) throws IOException {
163         SoapMessage response = (SoapMessage) responseCreator.createResponse(null, null, messageFactory);
164 
165         assertTrue("Response has no fault", response.hasFault());
166         Soap11Fault soapFault = (Soap11Fault) response.getSoapBody().getFault();
167         assertEquals("Response has invalid fault code", faultCode, soapFault.getFaultCode());
168         assertEquals("Response has invalid fault string", faultString, soapFault.getFaultStringOrReason());
169         assertEquals("Response has invalid fault locale", Locale.ENGLISH, soapFault.getFaultStringLocale());
170     }
171 
172     private String getPayloadAsString(WebServiceMessage message) throws TransformerException {
173         Result result = new StringResult();
174         transformerHelper.transform(message.getPayloadSource(), result);
175         return result.toString();
176     }
177     
178     private String getSoapEnvelopeAsString(SoapMessage message) throws TransformerException {
179     	DOMSource source = new DOMSource(message.getDocument());
180         Result result = new StringResult();
181         transformerHelper.transform(source, result);
182         return result.toString();
183     }
184 }