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.server.endpoint.interceptor;
18  
19  import javax.xml.soap.MessageFactory;
20  import javax.xml.soap.SOAPMessage;
21  import javax.xml.transform.Transformer;
22  import javax.xml.transform.TransformerFactory;
23  import javax.xml.transform.sax.SAXSource;
24  
25  import org.springframework.core.io.ClassPathResource;
26  import org.springframework.core.io.Resource;
27  import org.springframework.ws.MockWebServiceMessage;
28  import org.springframework.ws.MockWebServiceMessageFactory;
29  import org.springframework.ws.context.DefaultMessageContext;
30  import org.springframework.ws.context.MessageContext;
31  import org.springframework.ws.pox.dom.DomPoxMessage;
32  import org.springframework.ws.pox.dom.DomPoxMessageFactory;
33  import org.springframework.ws.soap.saaj.SaajSoapMessage;
34  import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
35  import org.springframework.xml.sax.SaxUtils;
36  import org.springframework.xml.transform.ResourceSource;
37  import org.springframework.xml.transform.StringResult;
38  
39  import org.custommonkey.xmlunit.XMLUnit;
40  import org.junit.Assert;
41  import org.junit.Before;
42  import org.junit.Test;
43  
44  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
45  
46  public class PayloadTransformingInterceptorTest {
47  
48      private PayloadTransformingInterceptor interceptor;
49  
50      private Transformer transformer;
51  
52      private Resource input;
53  
54      private Resource output;
55  
56      private Resource xslt;
57  
58      @Before
59      public void setUp() throws Exception {
60          interceptor = new PayloadTransformingInterceptor();
61          TransformerFactory transformerFactory = TransformerFactory.newInstance();
62          transformer = transformerFactory.newTransformer();
63          input = new ClassPathResource("transformInput.xml", getClass());
64          output = new ClassPathResource("transformOutput.xml", getClass());
65          xslt = new ClassPathResource("transformation.xslt", getClass());
66          XMLUnit.setIgnoreWhitespace(true);
67      }
68  
69      @Test
70      public void testHandleRequest() throws Exception {
71          interceptor.setRequestXslt(xslt);
72          interceptor.afterPropertiesSet();
73          MockWebServiceMessage request = new MockWebServiceMessage(input);
74          MessageContext context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
75  
76          boolean result = interceptor.handleRequest(context, null);
77          Assert.assertTrue("Invalid interceptor result", result);
78          StringResult expected = new StringResult();
79          transformer.transform(new SAXSource(SaxUtils.createInputSource(output)), expected);
80          assertXMLEqual(expected.toString(), request.getPayloadAsString());
81      }
82  
83      @Test
84      public void testHandleRequestNoXslt() throws Exception {
85          interceptor.setResponseXslt(xslt);
86          interceptor.afterPropertiesSet();
87          MockWebServiceMessage request = new MockWebServiceMessage(input);
88          MessageContext context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
89  
90          boolean result = interceptor.handleRequest(context, null);
91          Assert.assertTrue("Invalid interceptor result", result);
92          StringResult expected = new StringResult();
93          transformer.transform(new SAXSource(SaxUtils.createInputSource(input)), expected);
94          assertXMLEqual(expected.toString(), request.getPayloadAsString());
95      }
96  
97      @Test
98      public void testHandleResponse() throws Exception {
99          interceptor.setResponseXslt(xslt);
100         interceptor.afterPropertiesSet();
101         MockWebServiceMessage request = new MockWebServiceMessage(input);
102         MessageContext context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
103         MockWebServiceMessage response = (MockWebServiceMessage) context.getResponse();
104         response.setPayload(input);
105 
106         boolean result = interceptor.handleResponse(context, null);
107         Assert.assertTrue("Invalid interceptor result", result);
108         StringResult expected = new StringResult();
109         transformer.transform(new SAXSource(SaxUtils.createInputSource(output)), expected);
110         assertXMLEqual(expected.toString(), response.getPayloadAsString());
111     }
112 
113     @Test
114     public void testHandleResponseNoXslt() throws Exception {
115         interceptor.setRequestXslt(xslt);
116         interceptor.afterPropertiesSet();
117         MockWebServiceMessage request = new MockWebServiceMessage(input);
118         MessageContext context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
119         MockWebServiceMessage response = (MockWebServiceMessage) context.getResponse();
120         response.setPayload(input);
121 
122         boolean result = interceptor.handleResponse(context, null);
123         Assert.assertTrue("Invalid interceptor result", result);
124         StringResult expected = new StringResult();
125         transformer.transform(new SAXSource(SaxUtils.createInputSource(input)), expected);
126         assertXMLEqual(expected.toString(), response.getPayloadAsString());
127     }
128 
129     @Test
130     public void testSaaj() throws Exception {
131         interceptor.setRequestXslt(xslt);
132         interceptor.afterPropertiesSet();
133         MessageFactory messageFactory = MessageFactory.newInstance();
134         SOAPMessage saajMessage = messageFactory.createMessage();
135         SaajSoapMessage message = new SaajSoapMessage(saajMessage);
136         transformer.transform(new ResourceSource(input), message.getPayloadResult());
137         MessageContext context = new DefaultMessageContext(message, new SaajSoapMessageFactory(messageFactory));
138 
139         Assert.assertTrue("Invalid interceptor result", interceptor.handleRequest(context, null));
140         StringResult expected = new StringResult();
141         transformer.transform(new SAXSource(SaxUtils.createInputSource(output)), expected);
142         StringResult result = new StringResult();
143         transformer.transform(message.getPayloadSource(), result);
144         assertXMLEqual(expected.toString(), result.toString());
145 
146     }
147 
148     @Test
149     public void testPox() throws Exception {
150         interceptor.setRequestXslt(xslt);
151         interceptor.afterPropertiesSet();
152         DomPoxMessageFactory factory = new DomPoxMessageFactory();
153         DomPoxMessage message = (DomPoxMessage) factory.createWebServiceMessage();
154         transformer.transform(new ResourceSource(input), message.getPayloadResult());
155         MessageContext context = new DefaultMessageContext(message, factory);
156 
157         Assert.assertTrue("Invalid interceptor result", interceptor.handleRequest(context, null));
158         StringResult expected = new StringResult();
159         transformer.transform(new SAXSource(SaxUtils.createInputSource(output)), expected);
160         StringResult result = new StringResult();
161         transformer.transform(message.getPayloadSource(), result);
162         assertXMLEqual(expected.toString(), result.toString());
163 
164     }
165 
166     @Test
167     public void testNoStylesheetsSet() throws Exception {
168         try {
169             interceptor.afterPropertiesSet();
170             Assert.fail("Should have thrown an Exception");
171         }
172         catch (IllegalArgumentException ex) {
173         }
174     }
175 }