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.server.endpoint;
18  
19  import java.io.IOException;
20  import java.util.Iterator;
21  import javax.xml.namespace.QName;
22  import javax.xml.soap.Detail;
23  import javax.xml.soap.DetailEntry;
24  import javax.xml.soap.MessageFactory;
25  import javax.xml.soap.SOAPFault;
26  import javax.xml.soap.SOAPMessage;
27  import javax.xml.transform.Result;
28  import javax.xml.transform.Source;
29  
30  import org.springframework.context.support.ResourceBundleMessageSource;
31  import org.springframework.oxm.Marshaller;
32  import org.springframework.oxm.Unmarshaller;
33  import org.springframework.oxm.XmlMappingException;
34  import org.springframework.validation.Errors;
35  import org.springframework.validation.ValidationUtils;
36  import org.springframework.validation.Validator;
37  import org.springframework.ws.context.DefaultMessageContext;
38  import org.springframework.ws.context.MessageContext;
39  import org.springframework.ws.soap.saaj.SaajSoapMessage;
40  import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
41  
42  import org.junit.Assert;
43  import org.junit.Before;
44  import org.junit.Test;
45  
46  public class FaultCreatingValidatingMarshallingPayloadEndpointTest {
47  
48      private MessageContext messageContext;
49  
50      private ResourceBundleMessageSource messageSource;
51  
52      @Before
53      public void setUp() throws Exception {
54          this.messageSource = new ResourceBundleMessageSource();
55          this.messageSource.setBasename("org.springframework.ws.soap.server.endpoint.messages");
56          MessageFactory messageFactory = MessageFactory.newInstance();
57          SOAPMessage request = messageFactory.createMessage();
58          request.getSOAPBody().addBodyElement(new QName("http://www.springframework.org/spring-ws", "request"));
59          messageContext =
60                  new DefaultMessageContext(new SaajSoapMessage(request), new SaajSoapMessageFactory(messageFactory));
61      }
62  
63      @Test
64      public void testValidationIncorrect() throws Exception {
65          Person p = new Person("", -1);
66          PersonMarshaller marshaller = new PersonMarshaller(p);
67  
68          AbstractFaultCreatingValidatingMarshallingPayloadEndpoint endpoint =
69                  new AbstractFaultCreatingValidatingMarshallingPayloadEndpoint() {
70  
71                      @Override
72                      protected Object invokeInternal(Object requestObject) throws Exception {
73                          Assert.fail("No expected");
74                          return null;
75                      }
76                  };
77          endpoint.setValidator(new PersonValidator());
78          endpoint.setMessageSource(messageSource);
79          endpoint.setMarshaller(marshaller);
80          endpoint.setUnmarshaller(marshaller);
81  
82          endpoint.invoke(messageContext);
83  
84          SOAPMessage response = ((SaajSoapMessage) messageContext.getResponse()).getSaajMessage();
85          Assert.assertTrue("Response has no fault", response.getSOAPBody().hasFault());
86          SOAPFault fault = response.getSOAPBody().getFault();
87          Assert.assertEquals("Invalid fault code", new QName("http://schemas.xmlsoap.org/soap/envelope/", "Client"),
88                  fault.getFaultCodeAsQName());
89          Assert.assertEquals("Invalid fault string", endpoint.getFaultStringOrReason(), fault.getFaultString());
90          Detail detail = fault.getDetail();
91          Assert.assertNotNull("No detail", detail);
92          Iterator<?> iterator = detail.getDetailEntries();
93          Assert.assertTrue("No detail entry", iterator.hasNext());
94          DetailEntry detailEntry = (DetailEntry) iterator.next();
95          Assert.assertEquals("Invalid detail entry name",
96                  new QName("http://springframework.org/spring-ws", "ValidationError"), detailEntry.getElementQName());
97          Assert.assertEquals("Invalid detail entry text", "Name is required", detailEntry.getTextContent());
98          Assert.assertTrue("No detail entry", iterator.hasNext());
99          detailEntry = (DetailEntry) iterator.next();
100         Assert.assertEquals("Invalid detail entry name",
101                 new QName("http://springframework.org/spring-ws", "ValidationError"), detailEntry.getElementQName());
102         Assert.assertEquals("Invalid detail entry text", "Age Cannot be negative", detailEntry.getTextContent());
103         Assert.assertFalse("Too many detail entries", iterator.hasNext());
104     }
105 
106     @Test
107     public void testValidationCorrect() throws Exception {
108         Person p = new Person("John", 42);
109         PersonMarshaller marshaller = new PersonMarshaller(p);
110         AbstractFaultCreatingValidatingMarshallingPayloadEndpoint endpoint =
111                 new AbstractFaultCreatingValidatingMarshallingPayloadEndpoint() {
112 
113                     @Override
114                     protected Object invokeInternal(Object requestObject) throws Exception {
115                         return null;
116                     }
117                 };
118         endpoint.setValidator(new PersonValidator());
119         endpoint.setMessageSource(messageSource);
120         endpoint.setMarshaller(marshaller);
121         endpoint.setUnmarshaller(marshaller);
122 
123         endpoint.invoke(messageContext);
124 
125         SOAPMessage response = ((SaajSoapMessage) messageContext.getResponse()).getSaajMessage();
126         Assert.assertFalse("Response has fault", response.getSOAPBody().hasFault());
127     }
128 
129     private static class PersonValidator implements Validator {
130 
131         public boolean supports(Class<?> clazz) {
132             return Person.class.equals(clazz);
133         }
134 
135         public void validate(Object obj, Errors e) {
136             ValidationUtils.rejectIfEmpty(e, "name", "name.empty");
137             Person p = (Person) obj;
138             if (p.getAge() < 0) {
139                 e.rejectValue("age", "age.negativevalue");
140             }
141             else if (p.getAge() > 110) {
142                 e.rejectValue("age", "too.darn.old");
143             }
144         }
145     }
146 
147     private static class Person {
148 
149         private String name;
150 
151         private int age;
152 
153         private Person(String name, int age) {
154             this.name = name;
155             this.age = age;
156         }
157 
158         public String getName() {
159             return name;
160         }
161 
162         public void setName(String name) {
163             this.name = name;
164         }
165 
166         public int getAge() {
167             return age;
168         }
169 
170         public void setAge(int age) {
171             this.age = age;
172         }
173 
174         public String toString() {
175             return "Person{" + name + "," + age + "}";
176         }
177     }
178 
179     private static class PersonMarshaller implements Unmarshaller, Marshaller {
180 
181         private final Person person;
182 
183         private PersonMarshaller(Person person) {
184             this.person = person;
185         }
186 
187         public Object unmarshal(Source source) throws XmlMappingException, IOException {
188             return person;
189         }
190 
191         public boolean supports(Class<?> clazz) {
192             return Person.class.equals(clazz);
193         }
194 
195         public void marshal(Object graph, Result result) throws XmlMappingException, IOException {
196         }
197     }
198 
199 }