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.stream.StreamResult;
033: import javax.xml.transform.stream.StreamSource;
034: import javax.xml.ws.Dispatch;
035:
036: import javax.xml.ws.Service;
037: import javax.xml.ws.handler.MessageContext;
038: import javax.xml.ws.http.HTTPBinding;
039:
040: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
041: import org.junit.BeforeClass;
042: import org.junit.Test;
043:
044: public class RestClientServerHttpBindingTest extends
045: AbstractBusClientServerTestBase {
046: private final QName serviceName = new QName(
047: "http://apache.org/hello_world_xml_http/wrapped",
048: "XMLService");
049:
050: private final QName portName = new QName(
051: "http://apache.org/hello_world_xml_http/wrapped",
052: "RestProviderPort");
053:
054: private final String endpointAddress = "http://localhost:9024/XMLService/RestProviderPort/Customer";
055:
056: @BeforeClass
057: public static void startServers() throws Exception {
058: assertTrue("server did not launch correctly",
059: launchServer(HttpBindingServer.class));
060: }
061:
062: @Test
063: public void testHttpGET() throws Exception {
064: URL url = new URL(endpointAddress + "?name=john&address=20");
065: InputStream in = url.openStream();
066: assertNotNull(in);
067: }
068:
069: @Test
070: public void testHttpPOSTDispatchHTTPBinding() throws Exception {
071: Service service = Service.create(serviceName);
072: service.addPort(portName, HTTPBinding.HTTP_BINDING,
073: endpointAddress);
074: Dispatch<Source> dispatcher = service.createDispatch(portName,
075: Source.class, Service.Mode.MESSAGE);
076: Map<String, Object> requestContext = dispatcher
077: .getRequestContext();
078: requestContext.put(MessageContext.HTTP_REQUEST_METHOD, "POST");
079: InputStream is = getClass().getResourceAsStream(
080: "resources/CustomerJohnReq.xml");
081: Source result = dispatcher.invoke(new StreamSource(is));
082: String tempstring = source2String(result);
083: assertTrue("Result should start with Customer", tempstring
084: .startsWith("<ns4:Customer"));
085: assertTrue("Result should have CustomerID", tempstring
086: .lastIndexOf(">123456<") > 0);
087: }
088:
089: private String source2String(Source source) throws Exception {
090: ByteArrayOutputStream bos = new ByteArrayOutputStream();
091: StreamResult sr = new StreamResult(bos);
092: Transformer trans = TransformerFactory.newInstance()
093: .newTransformer();
094: Properties oprops = new Properties();
095: oprops.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
096: trans.setOutputProperties(oprops);
097: trans.transform(source, sr);
098: return bos.toString();
099: }
100: }
|