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.addressing.server;
18  
19  import java.net.URI;
20  import java.util.Iterator;
21  import java.util.Locale;
22  
23  import org.springframework.ws.context.DefaultMessageContext;
24  import org.springframework.ws.context.MessageContext;
25  import org.springframework.ws.soap.SoapHeaderElement;
26  import org.springframework.ws.soap.SoapMessage;
27  import org.springframework.ws.soap.addressing.AbstractWsAddressingTestCase;
28  import org.springframework.ws.soap.addressing.messageid.MessageIdStrategy;
29  import org.springframework.ws.soap.addressing.version.AddressingVersion;
30  import org.springframework.ws.soap.saaj.SaajSoapMessage;
31  import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
32  import org.springframework.ws.transport.WebServiceConnection;
33  import org.springframework.ws.transport.WebServiceMessageSender;
34  
35  import org.junit.Before;
36  import org.junit.Test;
37  
38  import static org.easymock.EasyMock.*;
39  import static org.junit.Assert.assertFalse;
40  import static org.junit.Assert.assertTrue;
41  
42  public abstract class AbstractAddressingInterceptorTestCase extends AbstractWsAddressingTestCase {
43  
44      protected AddressingEndpointInterceptor interceptor;
45  
46      protected MessageIdStrategy strategyMock;
47  
48      @Before
49      public void createMocks() throws Exception {
50          strategyMock = createMock(MessageIdStrategy.class);
51          expect(strategyMock.isDuplicate(isA(URI.class))).andReturn(false).anyTimes();
52          URI replyAction = new URI("urn:replyAction");
53          URI faultAction = new URI("urn:faultAction");
54          interceptor = new AddressingEndpointInterceptor(getVersion(), strategyMock, new WebServiceMessageSender[0],
55                  replyAction, faultAction);
56      }
57  
58      @Test
59      public void testUnderstands() throws Exception {
60          SaajSoapMessage validRequest = loadSaajMessage(getTestPath() + "/valid.xml");
61          Iterator<SoapHeaderElement> iterator = validRequest.getSoapHeader().examineAllHeaderElements();
62  
63          replay(strategyMock);
64  
65          while (iterator.hasNext()) {
66              SoapHeaderElement headerElement = iterator.next();
67              assertTrue("Header [" + headerElement.getName() + " not understood",
68                      interceptor.understands(headerElement));
69          }
70  
71          verify(strategyMock);
72      }
73  
74      @Test
75      public void testValidRequest() throws Exception {
76          SaajSoapMessage valid = loadSaajMessage(getTestPath() + "/valid.xml");
77          MessageContext context = new DefaultMessageContext(valid, new SaajSoapMessageFactory(messageFactory));
78  
79          replay(strategyMock);
80  
81          boolean result = interceptor.handleRequest(context, null);
82          assertTrue("Valid request not handled", result);
83          assertFalse("Message Context has response", context.hasResponse());
84  
85          verify(strategyMock);
86      }
87  
88      @Test
89      public void testNoMessageId() throws Exception {
90          SaajSoapMessage valid = loadSaajMessage(getTestPath() + "/request-no-message-id.xml");
91          MessageContext context = new DefaultMessageContext(valid, new SaajSoapMessageFactory(messageFactory));
92  
93          replay(strategyMock);
94  
95          boolean result = interceptor.handleRequest(context, null);
96          assertFalse("Request with no MessageID handled", result);
97          assertTrue("Message Context has no response", context.hasResponse());
98          SaajSoapMessage expectedResponse = loadSaajMessage(getTestPath() + "/response-no-message-id.xml");
99          assertXMLEqual("Invalid response for message with no MessageID", expectedResponse,
100                 (SaajSoapMessage) context.getResponse());
101 
102         verify(strategyMock);
103     }
104 
105     @Test
106     public void testNoReplyTo() throws Exception {
107         SaajSoapMessage valid = loadSaajMessage(getTestPath() + "/request-no-reply-to.xml");
108         MessageContext context = new DefaultMessageContext(valid, new SaajSoapMessageFactory(messageFactory));
109         URI messageId = new URI("uid:1234");
110 
111         expect(strategyMock.newMessageId((SoapMessage) context.getResponse())).andReturn(messageId);
112         replay(strategyMock);
113 
114         boolean result = interceptor.handleResponse(context, null);
115         assertTrue("Request with no ReplyTo not handled", result);
116         assertTrue("Message Context has no response", context.hasResponse());
117         SaajSoapMessage expectedResponse = loadSaajMessage(getTestPath() + "/response-anonymous.xml");
118         assertXMLEqual("Invalid response for message with invalid MAP", expectedResponse,
119                 (SaajSoapMessage) context.getResponse());
120 
121         verify(strategyMock);
122     }
123 
124     @Test
125     public void testAnonymousReplyTo() throws Exception {
126         SaajSoapMessage valid = loadSaajMessage(getTestPath() + "/request-anonymous.xml");
127         MessageContext context = new DefaultMessageContext(valid, new SaajSoapMessageFactory(messageFactory));
128         URI messageId = new URI("uid:1234");
129 
130         expect(strategyMock.newMessageId((SoapMessage) context.getResponse())).andReturn(messageId);
131         replay(strategyMock);
132 
133         boolean result = interceptor.handleResponse(context, null);
134         assertTrue("Request with anonymous ReplyTo not handled", result);
135         SaajSoapMessage expectedResponse = loadSaajMessage(getTestPath() + "/response-anonymous.xml");
136         assertXMLEqual("Invalid response for message with invalid MAP", expectedResponse,
137                 (SaajSoapMessage) context.getResponse());
138 
139         verify(strategyMock);
140     }
141 
142     @Test
143     public void testNoneReplyTo() throws Exception {
144         SaajSoapMessage valid = loadSaajMessage(getTestPath() + "/request-none.xml");
145         MessageContext context = new DefaultMessageContext(valid, new SaajSoapMessageFactory(messageFactory));
146         replay(strategyMock);
147         boolean result = interceptor.handleResponse(context, null);
148         assertFalse("None request handled", result);
149         assertFalse("Message context has response", context.hasResponse());
150         verify(strategyMock);
151     }
152 
153     @Test
154     public void testFaultTo() throws Exception {
155         SaajSoapMessage valid = loadSaajMessage(getTestPath() + "/request-fault-to.xml");
156         MessageContext context = new DefaultMessageContext(valid, new SaajSoapMessageFactory(messageFactory));
157         SaajSoapMessage response = (SaajSoapMessage) context.getResponse();
158         response.getSoapBody().addServerOrReceiverFault("Error", Locale.ENGLISH);
159         URI messageId = new URI("uid:1234");
160         expect(strategyMock.newMessageId((SoapMessage) context.getResponse())).andReturn(messageId);
161         replay(strategyMock);
162         boolean result = interceptor.handleFault(context, null);
163         assertTrue("Request with anonymous FaultTo not handled", result);
164         SaajSoapMessage expectedResponse = loadSaajMessage(getTestPath() + "/response-fault-to.xml");
165         assertXMLEqual("Invalid response for message with invalid MAP", expectedResponse,
166                 (SaajSoapMessage) context.getResponse());
167         verify(strategyMock);
168     }
169 
170     @Test
171     public void testOutOfBandReplyTo() throws Exception {
172         WebServiceMessageSender senderMock = createMock(WebServiceMessageSender.class);
173 
174         URI replyAction = new URI("urn:replyAction");
175         URI faultAction = new URI("urn:replyAction");
176         interceptor =
177                 new AddressingEndpointInterceptor(getVersion(), strategyMock, new WebServiceMessageSender[]{senderMock},
178                         replyAction, faultAction);
179 
180         WebServiceConnection connectionMock = createMock(WebServiceConnection.class);
181 
182         SaajSoapMessage valid = loadSaajMessage(getTestPath() + "/valid.xml");
183         MessageContext context = new DefaultMessageContext(valid, new SaajSoapMessageFactory(messageFactory));
184         SaajSoapMessage response = (SaajSoapMessage) context.getResponse();
185 
186         URI messageId = new URI("uid:1234");
187         expect(strategyMock.newMessageId((SoapMessage) context.getResponse())).andReturn(messageId);
188 
189         URI uri = new URI("http://example.com/business/client1");
190         expect(senderMock.supports(uri)).andReturn(true);
191         expect(senderMock.createConnection(uri)).andReturn(connectionMock);
192         connectionMock.send(response);
193         connectionMock.close();
194 
195         replay(strategyMock, senderMock, connectionMock);
196 
197         boolean result = interceptor.handleResponse(context, null);
198         assertFalse("Out of Band request handled", result);
199         assertFalse("Message context has response", context.hasResponse());
200 
201         verify(strategyMock, senderMock, connectionMock);
202     }
203 
204     protected abstract AddressingVersion getVersion();
205 
206     protected abstract String getTestPath();
207 
208 }