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.soap.soap12;
18  
19  import java.io.InputStream;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import org.springframework.ws.WebServiceMessage;
25  import org.springframework.ws.mime.Attachment;
26  import org.springframework.ws.soap.AbstractSoapMessageFactoryTestCase;
27  import org.springframework.ws.soap.SoapMessage;
28  import org.springframework.ws.soap.SoapVersion;
29  import org.springframework.ws.transport.MockTransportInputStream;
30  import org.springframework.ws.transport.TransportConstants;
31  import org.springframework.ws.transport.TransportInputStream;
32  
33  import static org.junit.Assert.*;
34  
35  public abstract class AbstractSoap12MessageFactoryTestCase extends AbstractSoapMessageFactoryTestCase {
36  
37      @Override
38      public void testCreateEmptyMessage() throws Exception {
39          WebServiceMessage message = messageFactory.createWebServiceMessage();
40          assertTrue("Not a SoapMessage", message instanceof SoapMessage);
41          SoapMessage soapMessage = (SoapMessage) message;
42          assertEquals("Invalid soap version", SoapVersion.SOAP_12, soapMessage.getVersion());
43      }
44  
45      @Override
46      public void testCreateSoapMessageNoAttachment() throws Exception {
47          InputStream is = AbstractSoap12MessageFactoryTestCase.class.getResourceAsStream("soap12.xml");
48          Map<String, String> headers = new HashMap<String, String>();
49          String soapAction = "\"http://springframework.org/spring-ws/Action\"";
50          headers.put(TransportConstants.HEADER_CONTENT_TYPE, "application/soap+xml; action=" + soapAction);
51          TransportInputStream tis = new MockTransportInputStream(is, headers);
52  
53          WebServiceMessage message = messageFactory.createWebServiceMessage(tis);
54          assertTrue("Not a SoapMessage", message instanceof SoapMessage);
55          SoapMessage soapMessage = (SoapMessage) message;
56          assertEquals("Invalid soap version", SoapVersion.SOAP_12, soapMessage.getVersion());
57          assertEquals("Invalid soap action", soapAction, soapMessage.getSoapAction());
58          assertFalse("Message is a XOP pacakge", soapMessage.isXopPackage());
59      }
60  
61      @Override
62      public void testCreateSoapMessageIllFormedXml() throws Exception {
63          InputStream is = AbstractSoap12MessageFactoryTestCase.class.getResourceAsStream("soap12-ill-formed.xml");
64          Map<String, String> headers = new HashMap<String, String>();
65          headers.put(TransportConstants.HEADER_CONTENT_TYPE, "application/soap+xml");
66          TransportInputStream tis = new MockTransportInputStream(is, headers);
67  
68          messageFactory.createWebServiceMessage(tis);
69      }
70  
71  
72      @Override
73      public void testCreateSoapMessageSwA() throws Exception {
74          InputStream is = AbstractSoap12MessageFactoryTestCase.class.getResourceAsStream("soap12-attachment.bin");
75          Map<String, String> headers = new HashMap<String, String>();
76          headers.put("Content-Type", "multipart/related;" + "type=\"application/soap+xml\";" +
77                  "boundary=\"----=_Part_0_11416420.1149699787554\"");
78          TransportInputStream tis = new MockTransportInputStream(is, headers);
79  
80          WebServiceMessage message = messageFactory.createWebServiceMessage(tis);
81          assertTrue("Not a SoapMessage", message instanceof SoapMessage);
82          SoapMessage soapMessage = (SoapMessage) message;
83          assertEquals("Invalid soap version", SoapVersion.SOAP_12, soapMessage.getVersion());
84          assertFalse("Message is a XOP package", soapMessage.isXopPackage());
85          Attachment attachment = soapMessage.getAttachment("interface21");
86          assertNotNull("No attachment read", attachment);
87      }
88  
89      @Override
90      public void testCreateSoapMessageMtom() throws Exception {
91          InputStream is = AbstractSoap12MessageFactoryTestCase.class.getResourceAsStream("soap12-mtom.bin");
92          Map<String, String> headers = new HashMap<String, String>();
93          headers.put("Content-Type", "multipart/related;" + "start-info=\"application/soap+xml\";" +
94                  "type=\"application/xop+xml\";" + "start=\"<0.urn:uuid:40864869929B855F971176851454456@apache.org>\";" +
95                  "boundary=\"MIMEBoundaryurn_uuid_40864869929B855F971176851454455\"");
96          TransportInputStream tis = new MockTransportInputStream(is, headers);
97  
98          WebServiceMessage message = messageFactory.createWebServiceMessage(tis);
99          assertTrue("Not a SoapMessage", message instanceof SoapMessage);
100         SoapMessage soapMessage = (SoapMessage) message;
101         assertEquals("Invalid soap version", SoapVersion.SOAP_12, soapMessage.getVersion());
102         assertTrue("Message is not a XOP package", soapMessage.isXopPackage());
103         Iterator<Attachment> iter = soapMessage.getAttachments();
104         assertTrue("No attachments read", iter.hasNext());
105 
106         Attachment attachment = soapMessage.getAttachment("<1.urn:uuid:40864869929B855F971176851454452@apache.org>");
107         assertNotNull("No attachment read", attachment);
108     }
109 
110 
111 }