001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.systest.servlet;
019:
020: import org.w3c.dom.Document;
021:
022: import com.meterware.httpunit.PostMethodWebRequest;
023: import com.meterware.httpunit.PutMethodWebRequest;
024: import com.meterware.httpunit.WebRequest;
025: import com.meterware.httpunit.WebResponse;
026: import com.meterware.servletunit.ServletUnitClient;
027:
028: import org.apache.cxf.Bus;
029: import org.apache.cxf.BusException;
030: import org.apache.cxf.helpers.DOMUtils;
031: import org.junit.Test;
032:
033: public class HttpBindingServletTest extends AbstractServletTest {
034:
035: @Override
036: protected String getConfiguration() {
037: return "/org/apache/cxf/systest/servlet/web-restful.xml";
038: }
039:
040: @Override
041: protected Bus createBus() throws BusException {
042: // don't set up the bus, let the servlet do it
043: return null;
044: }
045:
046: @Test
047: public void testServerFactoryRestService() throws Exception {
048: testInvokingRestService("/services/serverFactory/restful");
049: }
050:
051: @Test
052: public void testEndpointRestService() throws Exception {
053: testInvokingRestService("/services/endpoint/restful");
054: }
055:
056: private void testInvokingRestService(String serviceAddress)
057: throws Exception {
058: ServletUnitClient client = newClient();
059: client.setExceptionsThrownOnErrorStatus(false);
060:
061: WebRequest req = new GetMethodQueryWebRequest(CONTEXT_URL
062: + serviceAddress + "/customers");
063:
064: WebResponse response = client.getResponse(req);
065: Document doc = DOMUtils.readXml(response.getInputStream());
066: assertNotNull(doc);
067:
068: addNamespace("c", "http://cxf.apache.org/jra");
069: assertValid("/c:customers", doc);
070: assertValid("/c:customers/c:customer/c:id[text()='123']", doc);
071: assertValid(
072: "/c:customers/c:customer/c:name[text()='Dan Diephouse']",
073: doc);
074:
075: req = new GetMethodQueryWebRequest(CONTEXT_URL + serviceAddress
076: + "/customers/123");
077: response = client.getResponse(req);
078: doc = DOMUtils.readXml(response.getInputStream());
079: assertNotNull(doc);
080:
081: assertValid("/c:customer", doc);
082: assertValid("/c:customer/c:id[text()='123']", doc);
083: assertValid("/c:customer/c:name[text()='Dan Diephouse']", doc);
084:
085: // Try invalid customer
086: req = new GetMethodQueryWebRequest(CONTEXT_URL + serviceAddress
087: + "/customers/0");
088: response = client.getResponse(req);
089:
090: assertEquals("Expect the wrong response code", response
091: .getResponseCode(), 500);
092: doc = DOMUtils.readXml(response.getInputStream());
093: assertNotNull(doc);
094:
095: assertValid("//c:CustomerNotFoundDetails", doc);
096:
097: PostMethodWebRequest postReq = new PostMethodWebRequest(
098: CONTEXT_URL + serviceAddress + "/customers", getClass()
099: .getResourceAsStream("add.xml"),
100: "text/xml; charset=UTF-8");
101: response = client.getResponse(postReq);
102: doc = DOMUtils.readXml(response.getInputStream());
103: assertNotNull(doc);
104: assertValid("/c:addCustomer", doc);
105:
106: PutMethodWebRequest putReq = new PutMethodWebRequest(
107: CONTEXT_URL + serviceAddress + "/customers/123",
108: getClass().getResourceAsStream("update.xml"),
109: "text/xml; charset=UTF-8");
110: response = client.getResponse(putReq);
111: doc = DOMUtils.readXml(response.getInputStream());
112: assertNotNull(doc);
113: assertValid("/c:updateCustomer", doc);
114:
115: // Get the updated document
116: req = new GetMethodQueryWebRequest(CONTEXT_URL + serviceAddress
117: + "/customers/123");
118: response = client.getResponse(req);
119: doc = DOMUtils.readXml(response.getInputStream());
120: assertNotNull(doc);
121:
122: assertValid("/c:customer", doc);
123: assertValid("/c:customer/c:id[text()='123']", doc);
124: assertValid("/c:customer/c:name[text()='Danno Manno']", doc);
125: }
126: }
|