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.util.Iterator;
20  import java.util.Locale;
21  import javax.xml.namespace.QName;
22  
23  import org.springframework.ws.soap.AbstractSoapBodyTestCase;
24  import org.springframework.ws.soap.SoapFault;
25  import org.springframework.ws.soap.SoapFaultDetail;
26  import org.springframework.ws.soap.SoapFaultDetailElement;
27  import org.springframework.ws.soap.SoapVersion;
28  import org.springframework.xml.transform.StringResult;
29  import org.springframework.xml.transform.StringSource;
30  
31  import org.junit.Test;
32  
33  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
34  import static org.junit.Assert.*;
35  
36  public abstract class AbstractSoap12BodyTestCase extends AbstractSoapBodyTestCase {
37  
38      @Test
39      public void testGetType() {
40          assertTrue("Invalid type returned", soapBody instanceof Soap12Body);
41      }
42  
43      @Test
44      public void testGetName() throws Exception {
45          assertEquals("Invalid qualified name", new QName(SoapVersion.SOAP_12.getEnvelopeNamespaceUri(), "Body"),
46                  soapBody.getName());
47      }
48  
49      @Test
50      public void testGetSource() throws Exception {
51          StringResult result = new StringResult();
52          transformer.transform(soapBody.getSource(), result);
53          assertXMLEqual("Invalid contents of body", "<Body xmlns='http://www.w3.org/2003/05/soap-envelope' />",
54                  result.toString());
55      }
56  
57      @Test
58      public void testAddMustUnderstandFault() throws Exception {
59          SoapFault fault = soapBody.addMustUnderstandFault("SOAP Must Understand Error", Locale.ENGLISH);
60          assertEquals("Invalid fault code", new QName("http://www.w3.org/2003/05/soap-envelope", "MustUnderstand"),
61                  fault.getFaultCode());
62          StringResult result = new StringResult();
63          transformer.transform(fault.getSource(), result);
64          assertXMLEqual("Invalid contents of body",
65                  "<soapenv:Fault xmlns:soapenv='http://www.w3.org/2003/05/soap-envelope'>" +
66                          "<soapenv:Code><soapenv:Value>" + soapBody.getName().getPrefix() +
67                          ":MustUnderstand</soapenv:Value></soapenv:Code>" +
68                          "<soapenv:Reason><soapenv:Text xml:lang='en'>SOAP Must Understand Error</soapenv:Text>" +
69                          "</soapenv:Reason></soapenv:Fault>", result.toString());
70      }
71  
72      @Test
73      public void testAddSenderFault() throws Exception {
74          SoapFault fault = soapBody.addClientOrSenderFault("faultString", Locale.ENGLISH);
75          assertEquals("Invalid fault code", new QName("http://www.w3.org/2003/05/soap-envelope", "Sender"),
76                  fault.getFaultCode());
77          StringResult result = new StringResult();
78          transformer.transform(fault.getSource(), result);
79          assertXMLEqual("Invalid contents of body",
80                  "<soapenv:Fault xmlns:soapenv='http://www.w3.org/2003/05/soap-envelope'>" +
81                          "<soapenv:Code><soapenv:Value>" + soapBody.getName().getPrefix() +
82                          ":Sender</soapenv:Value></soapenv:Code>" +
83                          "<soapenv:Reason><soapenv:Text xml:lang='en'>faultString</soapenv:Text></soapenv:Reason>" +
84                          "</soapenv:Fault>", result.toString());
85      }
86  
87      @Test
88      public void testAddReceiverFault() throws Exception {
89          SoapFault fault = soapBody.addServerOrReceiverFault("faultString", Locale.ENGLISH);
90          assertEquals("Invalid fault code", new QName("http://www.w3.org/2003/05/soap-envelope", "Receiver"),
91                  fault.getFaultCode());
92          StringResult result = new StringResult();
93          transformer.transform(fault.getSource(), result);
94          assertXMLEqual("Invalid contents of body",
95                  "<soapenv:Fault xmlns:soapenv='http://www.w3.org/2003/05/soap-envelope'>" +
96                          "<soapenv:Code><soapenv:Value>" + soapBody.getName().getPrefix() +
97                          ":Receiver</soapenv:Value></soapenv:Code>" +
98                          "<soapenv:Reason><soapenv:Text xml:lang='en'>faultString</soapenv:Text></soapenv:Reason>" +
99                          "</soapenv:Fault>", result.toString());
100     }
101 
102     @Test
103     public void testAddFaultWithDetail() throws Exception {
104         SoapFault fault = soapBody.addServerOrReceiverFault("faultString", Locale.ENGLISH);
105         SoapFaultDetail detail = fault.addFaultDetail();
106         SoapFaultDetailElement detailElement =
107                 detail.addFaultDetailElement(new QName("namespace", "localPart", "prefix"));
108         StringSource detailContents = new StringSource("<detailContents xmlns='namespace'/>");
109         transformer.transform(detailContents, detailElement.getResult());
110         StringResult result = new StringResult();
111         transformer.transform(fault.getSource(), result);
112         assertXMLEqual("Invalid source for body",
113                 "<soapenv:Fault xmlns:soapenv='http://www.w3.org/2003/05/soap-envelope'>" +
114                         "<soapenv:Code><soapenv:Value>" + soapBody.getName().getPrefix() + ":Receiver</soapenv:Value>" +
115                         "</soapenv:Code>" +
116                         "<soapenv:Reason><soapenv:Text xml:lang='en'>faultString</soapenv:Text></soapenv:Reason>" +
117                         "<soapenv:Detail><prefix:localPart xmlns:prefix='namespace'><detailContents xmlns='namespace'/>" +
118                         "</prefix:localPart></soapenv:Detail></soapenv:Fault>", result.toString());
119     }
120 
121     @Test
122     public void testAddFaultWithDetailResult() throws Exception {
123         SoapFault fault = soapBody.addServerOrReceiverFault("faultString", Locale.ENGLISH);
124         SoapFaultDetail detail = fault.addFaultDetail();
125         transformer.transform(new StringSource("<detailContents xmlns='namespace'/>"), detail.getResult());
126         transformer.transform(new StringSource("<detailContents xmlns='namespace'/>"), detail.getResult());
127         StringResult result = new StringResult();
128         transformer.transform(fault.getSource(), result);
129         assertXMLEqual("Invalid source for body",
130                 "<soapenv:Fault xmlns:soapenv='http://www.w3.org/2003/05/soap-envelope'>" +
131                         "<soapenv:Code><soapenv:Value>" + soapBody.getName().getPrefix() + ":Receiver</soapenv:Value>" +
132                         "</soapenv:Code>" +
133                         "<soapenv:Reason><soapenv:Text xml:lang='en'>faultString</soapenv:Text></soapenv:Reason>" +
134                         "<soapenv:Detail>" + "<detailContents xmlns='namespace'/>" +
135                         "<detailContents xmlns='namespace'/>" + "</soapenv:Detail></soapenv:Fault>", result.toString());
136     }
137 
138     @Test
139     public void testAddFaultWithSubcode() throws Exception {
140         Soap12Fault fault = (Soap12Fault) soapBody.addServerOrReceiverFault("faultString", Locale.ENGLISH);
141         QName subcode1 = new QName("http://www.springframework.org", "Subcode1", "spring-ws");
142         fault.addFaultSubcode(subcode1);
143         QName subcode2 = new QName("http://www.springframework.org", "Subcode2", "spring-ws");
144         fault.addFaultSubcode(subcode2);
145         Iterator<QName> iterator = fault.getFaultSubcodes();
146         assertTrue("No subcode found", iterator.hasNext());
147         assertEquals("Invalid subcode", subcode1, iterator.next());
148         assertTrue("No subcode found", iterator.hasNext());
149         assertEquals("Invalid subcode", subcode2, iterator.next());
150         assertFalse("Subcode found", iterator.hasNext());
151         StringResult result = new StringResult();
152         transformer.transform(fault.getSource(), result);
153         assertXMLEqual("Invalid source for body",
154                 "<soapenv:Fault xmlns:soapenv='http://www.w3.org/2003/05/soap-envelope'>" +
155                         "<soapenv:Code><soapenv:Value>" + soapBody.getName().getPrefix() + ":Receiver</soapenv:Value>" +
156                         "<soapenv:Subcode><soapenv:Value xmlns:spring-ws='http://www.springframework.org'>spring-ws:Subcode1</soapenv:Value>" +
157                         "<soapenv:Subcode><soapenv:Value xmlns:spring-ws='http://www.springframework.org'>spring-ws:Subcode2</soapenv:Value>" +
158                         "</soapenv:Subcode></soapenv:Subcode></soapenv:Code>" +
159                         "<soapenv:Reason><soapenv:Text xml:lang='en'>faultString</soapenv:Text></soapenv:Reason>" +
160                         "</soapenv:Fault>", result.toString());
161     }
162 
163 }