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.soap.soap11;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.ByteArrayOutputStream;
21  import javax.xml.parsers.DocumentBuilder;
22  import javax.xml.parsers.DocumentBuilderFactory;
23  import javax.xml.transform.dom.DOMResult;
24  import javax.xml.transform.stream.StreamSource;
25  
26  import org.springframework.core.io.ByteArrayResource;
27  import org.springframework.core.io.ClassPathResource;
28  import org.springframework.core.io.InputStreamSource;
29  import org.springframework.core.io.Resource;
30  import org.springframework.ws.soap.AbstractSoapMessageTestCase;
31  import org.springframework.ws.soap.SoapBody;
32  import org.springframework.ws.soap.SoapVersion;
33  import org.springframework.ws.transport.MockTransportOutputStream;
34  import org.springframework.xml.transform.StringSource;
35  
36  import junit.framework.Assert;
37  import org.w3c.dom.Document;
38  import org.w3c.dom.Element;
39  
40  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
41  import static org.junit.Assert.*;
42  
43  public abstract class AbstractSoap11MessageTestCase extends AbstractSoapMessageTestCase {
44  
45      @Override
46      protected final Resource[] getSoapSchemas() {
47          return new Resource[]{new ClassPathResource("soap11.xsd", AbstractSoap11MessageTestCase.class)};
48      }
49  
50      @Override
51      public void testGetVersion() throws Exception {
52          Assert.assertEquals("Invalid SOAP version", SoapVersion.SOAP_11, soapMessage.getVersion());
53      }
54  
55      @Override
56      public void testWriteToTransportOutputStream() throws Exception {
57          SoapBody body = soapMessage.getSoapBody();
58          String payload = "<payload xmlns='http://www.springframework.org' />";
59          transformer.transform(new StringSource(payload), body.getPayloadResult());
60  
61          ByteArrayOutputStream bos = new ByteArrayOutputStream();
62          MockTransportOutputStream tos = new MockTransportOutputStream(bos);
63          String soapAction = "http://springframework.org/spring-ws/Action";
64          soapMessage.setSoapAction(soapAction);
65          soapMessage.writeTo(tos);
66          String result = bos.toString("UTF-8");
67          assertXMLEqual(
68                  "<Envelope xmlns='http://schemas.xmlsoap.org/soap/envelope/'><Body><payload xmlns='http://www.springframework.org' /></Body></Envelope>",
69                  result);
70          String contentType = (String) tos.getHeaders().get("Content-Type");
71          assertTrue("Invalid Content-Type set", contentType.indexOf(SoapVersion.SOAP_11.getContentType()) != -1);
72          String resultSoapAction = (String) tos.getHeaders().get("SOAPAction");
73          assertEquals("Invalid soap action", "\"" + soapAction + "\"", resultSoapAction);
74          String resultAccept = (String) tos.getHeaders().get("Accept");
75          assertNotNull("Invalid accept header", resultAccept);
76      }
77  
78      @Override
79      public void testWriteToTransportResponseAttachment() throws Exception {
80          InputStreamSource inputStreamSource = new ByteArrayResource("contents".getBytes("UTF-8"));
81          soapMessage.addAttachment("contentId", inputStreamSource, "text/plain");
82          ByteArrayOutputStream bos = new ByteArrayOutputStream();
83          MockTransportOutputStream tos = new MockTransportOutputStream(bos);
84          soapMessage.writeTo(tos);
85          String contentType = (String) tos.getHeaders().get("Content-Type");
86          assertTrue("Content-Type for attachment message does not contains multipart/related",
87                  contentType.indexOf("multipart/related") != -1);
88          assertTrue("Content-Type for attachment message does not contains type=\"text/xml\"",
89                  contentType.indexOf("type=\"text/xml\"") != -1);
90      }
91  
92      @Override
93      public void testToDocument() throws Exception {
94          transformer.transform(new StringSource("<payload xmlns='http://www.springframework.org' />"),
95                  soapMessage.getSoapBody().getPayloadResult());
96  
97          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
98          documentBuilderFactory.setNamespaceAware(true);
99          DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
100         Document expected = documentBuilder.newDocument();
101         Element envelope = expected.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "Envelope");
102         expected.appendChild(envelope);
103         Element body = expected.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "Body");
104         envelope.appendChild(body);
105         Element payload = expected.createElementNS("http://www.springframework.org", "payload");
106         body.appendChild(payload);
107 
108         Document result = soapMessage.getDocument();
109 
110         assertXMLEqual(expected, result);
111     }
112 
113     @Override
114     public void testSetLiveDocument() throws Exception {
115         transformer.transform(new StringSource("<payload xmlns='http://www.springframework.org' />"),
116                 soapMessage.getSoapBody().getPayloadResult());
117 
118         Document document = soapMessage.getDocument();
119 
120         soapMessage.setDocument(document);
121 
122         ByteArrayOutputStream bos = new ByteArrayOutputStream();
123         soapMessage.writeTo(bos);
124 
125         String result = bos.toString("UTF-8");
126         assertXMLEqual(
127                 "<Envelope xmlns='http://schemas.xmlsoap.org/soap/envelope/'><Body><payload xmlns='http://www.springframework.org' /></Body></Envelope>",
128                 result);
129     }
130 
131     @Override
132     public void testSetOtherDocument() throws Exception {
133         transformer.transform(new StringSource("<payload xmlns='http://www.springframework.org' />"),
134                 soapMessage.getSoapBody().getPayloadResult());
135 
136         ByteArrayOutputStream bos = new ByteArrayOutputStream();
137         soapMessage.writeTo(bos);
138         ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
139 
140         DOMResult domResult = new DOMResult();
141         transformer.transform(new StreamSource(bis), domResult);
142 
143         Document document = (Document) domResult.getNode();
144 
145         soapMessage.setDocument(document);
146 
147         bos = new ByteArrayOutputStream();
148         soapMessage.writeTo(bos);
149 
150         String result = bos.toString("UTF-8");
151         assertXMLEqual(
152                 "<Envelope xmlns='http://schemas.xmlsoap.org/soap/envelope/'><Body><payload xmlns='http://www.springframework.org' /></Body></Envelope>",
153                 result);
154     }
155 
156 }