1   /*
2    * Copyright 2006 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.transport.http;
18  
19  import javax.servlet.http.HttpServletResponse;
20  
21  import junit.framework.TestCase;
22  import org.easymock.MockControl;
23  import org.springframework.mock.web.MockHttpServletRequest;
24  import org.springframework.mock.web.MockHttpServletResponse;
25  import org.springframework.ws.FaultAwareWebServiceMessage;
26  import org.springframework.ws.NoEndpointFoundException;
27  import org.springframework.ws.WebServiceMessageFactory;
28  import org.springframework.ws.context.MessageContext;
29  import org.springframework.ws.transport.WebServiceMessageReceiver;
30  
31  public class WebServiceMessageReceiverHandlerAdapterTest extends TestCase {
32  
33      private static final String REQUEST = " <SOAP-ENV:Envelope\n" +
34              "  xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
35              "  SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" + "   <SOAP-ENV:Body>\n" +
36              "       <m:GetLastTradePrice xmlns:m=\"Some-URI\">\n" + "           <symbol>DIS</symbol>\n" +
37              "       </m:GetLastTradePrice>\n" + "   </SOAP-ENV:Body>\n" + "</SOAP-ENV:Envelope>";
38  
39      private WebServiceMessageReceiverHandlerAdapter adapter;
40  
41      private MockHttpServletRequest httpRequest;
42  
43      private MockHttpServletResponse httpResponse;
44  
45      private MockControl factoryControl;
46  
47      private WebServiceMessageFactory factoryMock;
48  
49      private MockControl messageControl;
50  
51      private FaultAwareWebServiceMessage responseMock;
52  
53      private FaultAwareWebServiceMessage requestMock;
54  
55      protected void setUp() throws Exception {
56          adapter = new WebServiceMessageReceiverHandlerAdapter();
57          httpRequest = new MockHttpServletRequest();
58          httpResponse = new MockHttpServletResponse();
59          factoryControl = MockControl.createControl(WebServiceMessageFactory.class);
60          factoryMock = (WebServiceMessageFactory) factoryControl.getMock();
61          adapter.setMessageFactory(factoryMock);
62          messageControl = MockControl.createControl(FaultAwareWebServiceMessage.class);
63          requestMock = (FaultAwareWebServiceMessage) messageControl.getMock();
64          responseMock = (FaultAwareWebServiceMessage) messageControl.getMock();
65      }
66  
67      public void testHandleNonPost() throws Exception {
68          httpRequest.setMethod("GET");
69          replayMockControls();
70          WebServiceMessageReceiver endpoint = new WebServiceMessageReceiver() {
71  
72              public void receive(MessageContext messageContext) throws Exception {
73              }
74          };
75          adapter.handle(httpRequest, httpResponse, endpoint);
76          assertEquals("METHOD_NOT_ALLOWED expected", HttpServletResponse.SC_METHOD_NOT_ALLOWED,
77                  httpResponse.getStatus());
78          verifyMockControls();
79      }
80  
81      public void testHandlePostNoResponse() throws Exception {
82          httpRequest.setMethod("POST");
83          httpRequest.setContent(REQUEST.getBytes("UTF-8"));
84          httpRequest.setContentType("text/xml; charset=\"utf-8\"");
85          httpRequest.setCharacterEncoding("UTF-8");
86          factoryMock.createWebServiceMessage(null);
87          factoryControl.setMatcher(MockControl.ALWAYS_MATCHER);
88          factoryControl.setReturnValue(responseMock);
89  
90          replayMockControls();
91          WebServiceMessageReceiver endpoint = new WebServiceMessageReceiver() {
92  
93              public void receive(MessageContext messageContext) throws Exception {
94              }
95          };
96  
97          adapter.handle(httpRequest, httpResponse, endpoint);
98  
99          assertEquals("Invalid status code on response", HttpServletResponse.SC_ACCEPTED, httpResponse.getStatus());
100         assertEquals("Response written", 0, httpResponse.getContentAsString().length());
101         verifyMockControls();
102     }
103 
104     public void testHandlePostResponse() throws Exception {
105         httpRequest.setMethod("POST");
106         httpRequest.setContent(REQUEST.getBytes("UTF-8"));
107         httpRequest.setContentType("text/xml; charset=\"utf-8\"");
108         httpRequest.setCharacterEncoding("UTF-8");
109         factoryMock.createWebServiceMessage(null);
110         factoryControl.setMatcher(MockControl.ALWAYS_MATCHER);
111         factoryControl.setReturnValue(requestMock);
112         factoryControl.expectAndReturn(factoryMock.createWebServiceMessage(), responseMock);
113         messageControl.expectAndReturn(responseMock.hasFault(), false);
114         responseMock.writeTo(null);
115         messageControl.setMatcher(MockControl.ALWAYS_MATCHER);
116 
117         replayMockControls();
118         WebServiceMessageReceiver endpoint = new WebServiceMessageReceiver() {
119 
120             public void receive(MessageContext messageContext) throws Exception {
121                 messageContext.getResponse();
122             }
123         };
124 
125         adapter.handle(httpRequest, httpResponse, endpoint);
126 
127         assertEquals("Invalid status code on response", HttpServletResponse.SC_OK, httpResponse.getStatus());
128         verifyMockControls();
129     }
130 
131     public void testHandlePostFault() throws Exception {
132         httpRequest.setMethod("POST");
133         httpRequest.setContent(REQUEST.getBytes("UTF-8"));
134         httpRequest.setContentType("text/xml; charset=\"utf-8\"");
135         httpRequest.setCharacterEncoding("UTF-8");
136         factoryMock.createWebServiceMessage(null);
137         factoryControl.setMatcher(MockControl.ALWAYS_MATCHER);
138         factoryControl.setReturnValue(requestMock);
139         factoryControl.expectAndReturn(factoryMock.createWebServiceMessage(), responseMock);
140         messageControl.expectAndReturn(responseMock.hasFault(), true);
141         responseMock.writeTo(null);
142         messageControl.setMatcher(MockControl.ALWAYS_MATCHER);
143 
144         replayMockControls();
145         WebServiceMessageReceiver endpoint = new WebServiceMessageReceiver() {
146 
147             public void receive(MessageContext messageContext) throws Exception {
148                 messageContext.getResponse();
149             }
150         };
151 
152         adapter.handle(httpRequest, httpResponse, endpoint);
153 
154         assertEquals("Invalid status code on response", HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
155                 httpResponse.getStatus());
156         verifyMockControls();
157     }
158 
159     public void testHandleNotFound() throws Exception {
160         httpRequest.setMethod("POST");
161         httpRequest.setContent(REQUEST.getBytes("UTF-8"));
162         httpRequest.setContentType("text/xml; charset=\"utf-8\"");
163         httpRequest.setCharacterEncoding("UTF-8");
164         factoryMock.createWebServiceMessage(null);
165         factoryControl.setMatcher(MockControl.ALWAYS_MATCHER);
166         factoryControl.setReturnValue(requestMock);
167 
168         replayMockControls();
169 
170         WebServiceMessageReceiver endpoint = new WebServiceMessageReceiver() {
171 
172             public void receive(MessageContext messageContext) throws Exception {
173                 throw new NoEndpointFoundException(messageContext.getRequest());
174             }
175         };
176 
177         adapter.handle(httpRequest, httpResponse, endpoint);
178         assertEquals("No 404 returned", HttpServletResponse.SC_NOT_FOUND, httpResponse.getStatus());
179 
180         verifyMockControls();
181 
182     }
183 
184     private void replayMockControls() {
185         factoryControl.replay();
186         messageControl.replay();
187     }
188 
189     private void verifyMockControls() {
190         factoryControl.verify();
191         messageControl.verify();
192     }
193 
194 }