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.binding.http.wrapped;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.w3c.dom.Document;
024:
025: import org.apache.cxf.binding.BindingFactoryManager;
026: import org.apache.cxf.binding.http.AbstractRestTest;
027: import org.apache.cxf.binding.http.HttpBindingFactory;
028: import org.apache.cxf.binding.http.URIMapper;
029: import org.apache.cxf.binding.http.strategy.ConventionStrategy;
030: import org.apache.cxf.customer.wraped.CustomerService;
031: import org.apache.cxf.endpoint.ServerImpl;
032: import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
033: import org.apache.cxf.service.model.BindingOperationInfo;
034: import org.junit.Test;
035:
036: public class WrappedServiceTest extends AbstractRestTest {
037:
038: @Test
039: public void testConvention() throws Exception {
040: HttpBindingFactory hbif = new HttpBindingFactory();
041: hbif.getStrategies().clear();
042: hbif.getStrategies().add(new ConventionStrategy());
043:
044: testService(hbif);
045: }
046:
047: @Test
048: public void testJRA() throws Exception {
049: testService(new HttpBindingFactory());
050: }
051:
052: public void testService(HttpBindingFactory httpFactory)
053: throws Exception {
054: httpFactory.setBus(bus);
055: BindingFactoryManager bfm = getBus().getExtension(
056: BindingFactoryManager.class);
057: bfm.registerBindingFactory(HttpBindingFactory.HTTP_BINDING_ID,
058: httpFactory);
059:
060: JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
061: sf.setBus(getBus());
062: sf.setBindingId(HttpBindingFactory.HTTP_BINDING_ID);
063: sf.setServiceClass(CustomerService.class);
064: sf.getServiceFactory().setWrapped(true);
065: sf.setAddress("http://localhost:9001/");
066: sf.setServiceBean(new CustomerService());
067:
068: Map<String, Object> props = new HashMap<String, Object>();
069: props.put("contextMatchStrategy", "stem");
070: sf.setProperties(props);
071:
072: ServerImpl svr = (ServerImpl) sf.create();
073:
074: URIMapper mapper = (URIMapper) svr.getEndpoint().getService()
075: .get(URIMapper.class.getName());
076: assertNotNull(mapper);
077:
078: BindingOperationInfo bop = mapper.getOperation("/customers",
079: "GET", null);
080: assertNotNull(bop);
081: assertEquals("getCustomers", bop.getName().getLocalPart());
082: assertTrue(bop.isUnwrappedCapable());
083:
084: bop = mapper.getOperation("/customers", "POST", null);
085: assertNotNull(bop);
086: assertEquals("addCustomer", bop.getName().getLocalPart());
087:
088: bop = mapper.getOperation("/customers/123", "GET", null);
089: assertNotNull(bop);
090: assertEquals("getCustomer", bop.getName().getLocalPart());
091:
092: bop = mapper.getOperation("/customers/123", "PUT", null);
093: assertNotNull(bop);
094: assertEquals("updateCustomer", bop.getName().getLocalPart());
095:
096: // TEST POST/GETs
097: Document res = get("http://localhost:9001/customers");
098: assertNotNull(res);
099:
100: addNamespace("c", "http://cxf.apache.org/jra");
101: assertValid("/c:getCustomersResponse/c:customers", res);
102: assertValid(
103: "/c:getCustomersResponse/c:customers/c:customer/c:id[text()='123']",
104: res);
105: assertValid(
106: "/c:getCustomersResponse/c:customers/c:customer/c:name[text()='Dan Diephouse']",
107: res);
108:
109: res = get("http://localhost:9001/customers/123");
110: assertNotNull(res);
111:
112: addNamespace("c", "http://cxf.apache.org/jra");
113: assertValid("/c:getCustomerResponse/c:customer", res);
114: assertValid(
115: "/c:getCustomerResponse/c:customer/c:id[text()='123']",
116: res);
117: assertValid(
118: "/c:getCustomerResponse/c:customer/c:name[text()='Dan Diephouse']",
119: res);
120:
121: res = put("http://localhost:9001/customers/123", "update.xml");
122: assertNotNull(res);
123:
124: assertValid("/c:updateCustomerResponse", res);
125:
126: res = post("http://localhost:9001/customers", "add.xml");
127: assertNotNull(res);
128:
129: assertValid("/c:addCustomerResponse", res);
130:
131: // Get the updated document
132: res = get("http://localhost:9001/customers/123");
133: assertNotNull(res);
134:
135: assertValid("/c:getCustomerResponse/c:customer", res);
136: assertValid(
137: "/c:getCustomerResponse/c:customer/c:id[text()='123']",
138: res);
139: assertValid(
140: "/c:getCustomerResponse/c:customer/c:name[text()='Danno Manno']",
141: res);
142:
143: svr.stop();
144: }
145:
146: }
|