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