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.util.Locale;
20  import javax.xml.namespace.QName;
21  
22  import org.springframework.ws.soap.AbstractSoapBodyTestCase;
23  import org.springframework.ws.soap.SoapFault;
24  import org.springframework.ws.soap.SoapFaultDetail;
25  import org.springframework.ws.soap.SoapFaultDetailElement;
26  import org.springframework.ws.soap.SoapVersion;
27  import org.springframework.xml.transform.StringResult;
28  import org.springframework.xml.transform.StringSource;
29  
30  import org.junit.Test;
31  
32  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
33  import static org.junit.Assert.*;
34  
35  public abstract class AbstractSoap11BodyTestCase extends AbstractSoapBodyTestCase {
36  
37      @Test
38      public void testGetType() {
39          assertTrue("Invalid type returned", soapBody instanceof Soap11Body);
40      }
41  
42      @Test
43      public void testGetName() throws Exception {
44          assertEquals("Invalid qualified name", SoapVersion.SOAP_11.getBodyName(), soapBody.getName());
45      }
46  
47      @Test
48      public void testGetSource() throws Exception {
49          StringResult result = new StringResult();
50          transformer.transform(soapBody.getSource(), result);
51          assertXMLEqual("Invalid contents of body", "<Body xmlns='http://schemas.xmlsoap.org/soap/envelope/' />",
52                  result.toString());
53      }
54  
55      @Test
56      public void testAddMustUnderstandFault() throws Exception {
57          SoapFault fault = soapBody.addMustUnderstandFault("SOAP Must Understand Error", null);
58          assertEquals("Invalid fault code", new QName("http://schemas.xmlsoap.org/soap/envelope/", "MustUnderstand"),
59                  fault.getFaultCode());
60          assertPayloadEqual("<SOAP-ENV:Fault xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>" +
61                  "<faultcode>" + soapBody.getName().getPrefix() + ":MustUnderstand</faultcode>" +
62                  "<faultstring>SOAP Must Understand Error</faultstring></SOAP-ENV:Fault>");
63      }
64  
65      @Test
66      public void testAddClientFault() throws Exception {
67          SoapFault fault = soapBody.addClientOrSenderFault("faultString", null);
68          assertEquals("Invalid fault code", new QName("http://schemas.xmlsoap.org/soap/envelope/", "Client"),
69                  fault.getFaultCode());
70          assertPayloadEqual("<SOAP-ENV:Fault xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>" +
71                  "<faultcode>" + soapBody.getName().getPrefix() + ":Client</faultcode>" +
72                  "<faultstring>faultString</faultstring>" + "</SOAP-ENV:Fault>");
73      }
74  
75      @Test
76      public void testAddServerFault() throws Exception {
77          SoapFault fault = soapBody.addServerOrReceiverFault("faultString", null);
78          assertEquals("Invalid fault code", new QName("http://schemas.xmlsoap.org/soap/envelope/", "Server"),
79                  fault.getFaultCode());
80          assertPayloadEqual("<SOAP-ENV:Fault xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>" +
81                  "<faultcode>" + soapBody.getName().getPrefix() + ":Server</faultcode>" +
82                  "<faultstring>faultString</faultstring>" + "</SOAP-ENV:Fault>");
83      }
84  
85      @Test
86      public void testAddFault() throws Exception {
87          QName faultCode = new QName("http://www.springframework.org", "fault", "spring");
88          String faultString = "faultString";
89          Soap11Fault fault = ((Soap11Body) soapBody).addFault(faultCode, faultString, Locale.ENGLISH);
90          assertNotNull("Null returned", fault);
91          assertTrue("SoapBody has no fault", soapBody.hasFault());
92          assertNotNull("SoapBody has no fault", soapBody.getFault());
93          assertEquals("Invalid fault code", faultCode, fault.getFaultCode());
94          assertEquals("Invalid fault string", faultString, fault.getFaultStringOrReason());
95          assertEquals("Invalid fault string locale", Locale.ENGLISH, fault.getFaultStringLocale());
96          String actor = "http://www.springframework.org/actor";
97          fault.setFaultActorOrRole(actor);
98          assertEquals("Invalid fault actor", actor, fault.getFaultActorOrRole());
99          assertPayloadEqual("<SOAP-ENV:Fault xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' " +
100                 "xmlns:spring='http://www.springframework.org'>" + "<faultcode>spring:fault</faultcode>" +
101                 "<faultstring xml:lang='en'>" + faultString + "</faultstring>" + "<faultactor>" + actor +
102                 "</faultactor>" + "</SOAP-ENV:Fault>");
103     }
104 
105     @Test
106     public void testAddFaultNoPrefix() throws Exception {
107         QName faultCode = new QName("http://www.springframework.org", "fault");
108         String faultString = "faultString";
109         Soap11Fault fault = ((Soap11Body) soapBody).addFault(faultCode, faultString, Locale.ENGLISH);
110         assertNotNull("Null returned", fault);
111         assertTrue("SoapBody has no fault", soapBody.hasFault());
112         assertNotNull("SoapBody has no fault", soapBody.getFault());
113         assertEquals("Invalid fault code", faultCode, fault.getFaultCode());
114         assertEquals("Invalid fault string", faultString, fault.getFaultStringOrReason());
115         assertEquals("Invalid fault string locale", Locale.ENGLISH, fault.getFaultStringLocale());
116         String actor = "http://www.springframework.org/actor";
117         fault.setFaultActorOrRole(actor);
118         assertEquals("Invalid fault actor", actor, fault.getFaultActorOrRole());
119     }
120 
121     @Test
122     public void testAddFaultWithDetail() throws Exception {
123         QName faultCode = new QName("http://www.springframework.org", "fault", "spring");
124         String faultString = "faultString";
125         SoapFault fault = ((Soap11Body) soapBody).addFault(faultCode, faultString, null);
126         SoapFaultDetail detail = fault.addFaultDetail();
127         QName detailName = new QName("http://www.springframework.org", "detailEntry", "spring");
128         SoapFaultDetailElement detailElement1 = detail.addFaultDetailElement(detailName);
129         StringSource detailContents = new StringSource("<detailContents xmlns='namespace'/>");
130         transformer.transform(detailContents, detailElement1.getResult());
131         SoapFaultDetailElement detailElement2 = detail.addFaultDetailElement(detailName);
132         detailContents = new StringSource("<detailContents xmlns='namespace'/>");
133         transformer.transform(detailContents, detailElement2.getResult());
134         assertPayloadEqual(
135                 "<SOAP-ENV:Fault xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:spring='http://www.springframework.org'>" +
136                         "<faultcode>spring:fault</faultcode>" + "<faultstring>" + faultString + "</faultstring>" +
137                         "<detail>" +
138                         "<spring:detailEntry xmlns:spring='http://www.springframework.org'><detailContents xmlns='namespace'/></spring:detailEntry>" +
139                         "<spring:detailEntry xmlns:spring='http://www.springframework.org'><detailContents xmlns='namespace'/></spring:detailEntry>" +
140                         "</detail>" + "</SOAP-ENV:Fault>");
141 
142     }
143 
144     @Test
145     public void testAddFaultWithDetailResult() throws Exception {
146         SoapFault fault = ((Soap11Body) soapBody)
147                 .addFault(new QName("namespace", "localPart", "prefix"), "Fault", null);
148         SoapFaultDetail detail = fault.addFaultDetail();
149         transformer.transform(new StringSource("<detailContents xmlns='namespace'/>"), detail.getResult());
150         transformer.transform(new StringSource("<detailContents xmlns='namespace'/>"), detail.getResult());
151         assertPayloadEqual("<SOAP-ENV:Fault xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>" +
152                 "<faultcode xmlns:prefix='namespace'>prefix:localPart</faultcode>" +
153                 "<faultstring>Fault</faultstring>" + "<detail>" + "<detailContents xmlns='namespace'/>" +
154                 "<detailContents xmlns='namespace'/>" + "</detail></SOAP-ENV:Fault>");
155     }
156 
157 }