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.rest;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.InputStream;
022: import java.net.URL;
023: import java.util.Map;
024: import java.util.Properties;
025:
026: import javax.xml.namespace.QName;
027: import javax.xml.transform.OutputKeys;
028: import javax.xml.transform.Source;
029: import javax.xml.transform.Transformer;
030: import javax.xml.transform.TransformerFactory;
031:
032: import javax.xml.transform.dom.DOMSource;
033: import javax.xml.transform.stream.StreamResult;
034: import javax.xml.transform.stream.StreamSource;
035: import javax.xml.ws.Dispatch;
036:
037: import javax.xml.ws.Service;
038: import javax.xml.ws.handler.MessageContext;
039: import javax.xml.ws.http.HTTPBinding;
040:
041: import org.w3c.dom.Document;
042: import org.w3c.dom.Node;
043:
044: import org.apache.cxf.helpers.XMLUtils;
045: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
046: import org.apache.hello_world_xml_http.wrapped.XMLService;
047: import org.junit.BeforeClass;
048: import org.junit.Test;
049:
050: public class RestClientServerTest extends
051: AbstractBusClientServerTestBase {
052: private final QName serviceName = new QName(
053: "http://apache.org/hello_world_xml_http/wrapped",
054: "XMLService");
055:
056: private final QName portName = new QName(
057: "http://apache.org/hello_world_xml_http/wrapped",
058: "RestProviderPort");
059:
060: private final String endpointAddress = "http://localhost:9023/XMLService/RestProviderPort/Customer";
061:
062: @BeforeClass
063: public static void startServers() throws Exception {
064: assertTrue("server did not launch correctly",
065: launchServer(Server.class));
066: }
067:
068: @Test
069: public void testHttpPOSTDispatchXMLBinding() throws Exception {
070: URL wsdl = getClass().getResource(
071: "/wsdl/hello_world_xml_wrapped.wsdl");
072: assertNotNull(wsdl);
073:
074: XMLService service = new XMLService(wsdl, serviceName);
075: assertNotNull(service);
076:
077: InputStream is = getClass().getResourceAsStream(
078: "resources/CustomerJohnReq.xml");
079: Document doc = XMLUtils.parse(is);
080: DOMSource reqMsg = new DOMSource(doc);
081: assertNotNull(reqMsg);
082:
083: Dispatch<DOMSource> disp = service.createDispatch(portName,
084: DOMSource.class, Service.Mode.PAYLOAD);
085: DOMSource result = disp.invoke(reqMsg);
086: assertNotNull(result);
087:
088: Node respDoc = result.getNode();
089: assertEquals("Customer", respDoc.getFirstChild().getLocalName());
090: }
091:
092: @Test
093: public void testHttpGET() throws Exception {
094: URL url = new URL(endpointAddress + "?name=john&address=20");
095: InputStream in = url.openStream();
096: assertNotNull(in);
097: }
098:
099: @Test
100: public void testHttpPOSTDispatchHTTPBinding() throws Exception {
101: Service service = Service.create(serviceName);
102: service.addPort(portName, HTTPBinding.HTTP_BINDING,
103: endpointAddress);
104: Dispatch<Source> dispatcher = service.createDispatch(portName,
105: Source.class, Service.Mode.MESSAGE);
106: Map<String, Object> requestContext = dispatcher
107: .getRequestContext();
108: requestContext.put(MessageContext.HTTP_REQUEST_METHOD, "POST");
109: InputStream is = getClass().getResourceAsStream(
110: "resources/CustomerJohnReq.xml");
111: Source result = dispatcher.invoke(new StreamSource(is));
112: String tempstring = source2String(result);
113: assertTrue("Result should start with Customer", tempstring
114: .startsWith("<ns4:Customer"));
115: assertTrue("Result should have CustomerID", tempstring
116: .lastIndexOf(">123456<") > 0);
117: }
118:
119: private String source2String(Source source) throws Exception {
120: ByteArrayOutputStream bos = new ByteArrayOutputStream();
121: StreamResult sr = new StreamResult(bos);
122: Transformer trans = TransformerFactory.newInstance()
123: .newTransformer();
124: Properties oprops = new Properties();
125: oprops.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
126: trans.setOutputProperties(oprops);
127: trans.transform(source, sr);
128: return bos.toString();
129: }
130: }
|