001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.transport.http;
018:
019: import javax.servlet.http.HttpServletResponse;
020:
021: import junit.framework.TestCase;
022: import org.easymock.MockControl;
023: import org.springframework.mock.web.MockHttpServletRequest;
024: import org.springframework.mock.web.MockHttpServletResponse;
025: import org.springframework.ws.FaultAwareWebServiceMessage;
026: import org.springframework.ws.NoEndpointFoundException;
027: import org.springframework.ws.WebServiceMessageFactory;
028: import org.springframework.ws.context.MessageContext;
029: import org.springframework.ws.transport.WebServiceMessageReceiver;
030:
031: public class WebServiceMessageReceiverHandlerAdapterTest extends
032: TestCase {
033:
034: private static final String REQUEST = " <SOAP-ENV:Envelope\n"
035: + " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\n"
036: + " SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
037: + " <SOAP-ENV:Body>\n"
038: + " <m:GetLastTradePrice xmlns:m=\"Some-URI\">\n"
039: + " <symbol>DIS</symbol>\n"
040: + " </m:GetLastTradePrice>\n"
041: + " </SOAP-ENV:Body>\n" + "</SOAP-ENV:Envelope>";
042:
043: private WebServiceMessageReceiverHandlerAdapter adapter;
044:
045: private MockHttpServletRequest httpRequest;
046:
047: private MockHttpServletResponse httpResponse;
048:
049: private MockControl factoryControl;
050:
051: private WebServiceMessageFactory factoryMock;
052:
053: private MockControl messageControl;
054:
055: private FaultAwareWebServiceMessage responseMock;
056:
057: private FaultAwareWebServiceMessage requestMock;
058:
059: protected void setUp() throws Exception {
060: adapter = new WebServiceMessageReceiverHandlerAdapter();
061: httpRequest = new MockHttpServletRequest();
062: httpResponse = new MockHttpServletResponse();
063: factoryControl = MockControl
064: .createControl(WebServiceMessageFactory.class);
065: factoryMock = (WebServiceMessageFactory) factoryControl
066: .getMock();
067: adapter.setMessageFactory(factoryMock);
068: messageControl = MockControl
069: .createControl(FaultAwareWebServiceMessage.class);
070: requestMock = (FaultAwareWebServiceMessage) messageControl
071: .getMock();
072: responseMock = (FaultAwareWebServiceMessage) messageControl
073: .getMock();
074: }
075:
076: public void testHandleNonPost() throws Exception {
077: httpRequest.setMethod("GET");
078: replayMockControls();
079: WebServiceMessageReceiver endpoint = new WebServiceMessageReceiver() {
080:
081: public void receive(MessageContext messageContext)
082: throws Exception {
083: }
084: };
085: adapter.handle(httpRequest, httpResponse, endpoint);
086: assertEquals("METHOD_NOT_ALLOWED expected",
087: HttpServletResponse.SC_METHOD_NOT_ALLOWED, httpResponse
088: .getStatus());
089: verifyMockControls();
090: }
091:
092: public void testHandlePostNoResponse() throws Exception {
093: httpRequest.setMethod("POST");
094: httpRequest.setContent(REQUEST.getBytes("UTF-8"));
095: httpRequest.setContentType("text/xml; charset=\"utf-8\"");
096: httpRequest.setCharacterEncoding("UTF-8");
097: factoryMock.createWebServiceMessage(null);
098: factoryControl.setMatcher(MockControl.ALWAYS_MATCHER);
099: factoryControl.setReturnValue(responseMock);
100:
101: replayMockControls();
102: WebServiceMessageReceiver endpoint = new WebServiceMessageReceiver() {
103:
104: public void receive(MessageContext messageContext)
105: throws Exception {
106: }
107: };
108:
109: adapter.handle(httpRequest, httpResponse, endpoint);
110:
111: assertEquals("Invalid status code on response",
112: HttpServletResponse.SC_ACCEPTED, httpResponse
113: .getStatus());
114: assertEquals("Response written", 0, httpResponse
115: .getContentAsString().length());
116: verifyMockControls();
117: }
118:
119: public void testHandlePostResponse() throws Exception {
120: httpRequest.setMethod("POST");
121: httpRequest.setContent(REQUEST.getBytes("UTF-8"));
122: httpRequest.setContentType("text/xml; charset=\"utf-8\"");
123: httpRequest.setCharacterEncoding("UTF-8");
124: factoryMock.createWebServiceMessage(null);
125: factoryControl.setMatcher(MockControl.ALWAYS_MATCHER);
126: factoryControl.setReturnValue(requestMock);
127: factoryControl.expectAndReturn(factoryMock
128: .createWebServiceMessage(), responseMock);
129: messageControl.expectAndReturn(responseMock.hasFault(), false);
130: responseMock.writeTo(null);
131: messageControl.setMatcher(MockControl.ALWAYS_MATCHER);
132:
133: replayMockControls();
134: WebServiceMessageReceiver endpoint = new WebServiceMessageReceiver() {
135:
136: public void receive(MessageContext messageContext)
137: throws Exception {
138: messageContext.getResponse();
139: }
140: };
141:
142: adapter.handle(httpRequest, httpResponse, endpoint);
143:
144: assertEquals("Invalid status code on response",
145: HttpServletResponse.SC_OK, httpResponse.getStatus());
146: verifyMockControls();
147: }
148:
149: public void testHandlePostFault() throws Exception {
150: httpRequest.setMethod("POST");
151: httpRequest.setContent(REQUEST.getBytes("UTF-8"));
152: httpRequest.setContentType("text/xml; charset=\"utf-8\"");
153: httpRequest.setCharacterEncoding("UTF-8");
154: factoryMock.createWebServiceMessage(null);
155: factoryControl.setMatcher(MockControl.ALWAYS_MATCHER);
156: factoryControl.setReturnValue(requestMock);
157: factoryControl.expectAndReturn(factoryMock
158: .createWebServiceMessage(), responseMock);
159: messageControl.expectAndReturn(responseMock.hasFault(), true);
160: responseMock.writeTo(null);
161: messageControl.setMatcher(MockControl.ALWAYS_MATCHER);
162:
163: replayMockControls();
164: WebServiceMessageReceiver endpoint = new WebServiceMessageReceiver() {
165:
166: public void receive(MessageContext messageContext)
167: throws Exception {
168: messageContext.getResponse();
169: }
170: };
171:
172: adapter.handle(httpRequest, httpResponse, endpoint);
173:
174: assertEquals("Invalid status code on response",
175: HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
176: httpResponse.getStatus());
177: verifyMockControls();
178: }
179:
180: public void testHandleNotFound() throws Exception {
181: httpRequest.setMethod("POST");
182: httpRequest.setContent(REQUEST.getBytes("UTF-8"));
183: httpRequest.setContentType("text/xml; charset=\"utf-8\"");
184: httpRequest.setCharacterEncoding("UTF-8");
185: factoryMock.createWebServiceMessage(null);
186: factoryControl.setMatcher(MockControl.ALWAYS_MATCHER);
187: factoryControl.setReturnValue(requestMock);
188:
189: replayMockControls();
190:
191: WebServiceMessageReceiver endpoint = new WebServiceMessageReceiver() {
192:
193: public void receive(MessageContext messageContext)
194: throws Exception {
195: throw new NoEndpointFoundException(messageContext
196: .getRequest());
197: }
198: };
199:
200: adapter.handle(httpRequest, httpResponse, endpoint);
201: assertEquals("No 404 returned",
202: HttpServletResponse.SC_NOT_FOUND, httpResponse
203: .getStatus());
204:
205: verifyMockControls();
206:
207: }
208:
209: private void replayMockControls() {
210: factoryControl.replay();
211: messageControl.replay();
212: }
213:
214: private void verifyMockControls() {
215: factoryControl.verify();
216: messageControl.verify();
217: }
218:
219: }
|