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.File;
021: import java.io.InputStream;
022: import java.net.URL;
023: import java.util.logging.Logger;
024:
025: import org.apache.commons.httpclient.HttpClient;
026: import org.apache.commons.httpclient.methods.FileRequestEntity;
027: import org.apache.commons.httpclient.methods.PostMethod;
028: import org.apache.commons.httpclient.methods.RequestEntity;
029: import org.apache.cxf.binding.http.HttpBindingFactory;
030: import org.apache.cxf.customer.book.Book;
031: import org.apache.cxf.customer.book.BookService;
032: import org.apache.cxf.customer.book.GetAnotherBook;
033: import org.apache.cxf.customer.book.GetBook;
034: import org.apache.cxf.helpers.IOUtils;
035: import org.apache.cxf.io.CachedOutputStream;
036: import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
037: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
038: import org.junit.BeforeClass;
039: import org.junit.Ignore;
040: import org.junit.Test;
041:
042: public class RestClientServerBookTest extends
043: AbstractBusClientServerTestBase {
044: static final Logger LOG = Logger
045: .getLogger(RestClientServerBookTest.class.getName());
046:
047: @BeforeClass
048: public static void startServers() throws Exception {
049: assertTrue("server did not launch correctly",
050: launchServer(BookServer.class));
051: }
052:
053: @Test
054: @Ignore
055: public void testGetBookWithXmlRootElement() throws Exception {
056: JaxWsProxyFactoryBean sf = new JaxWsProxyFactoryBean();
057: sf.setServiceClass(BookService.class);
058:
059: // Turn off wrapped mode to make our xml prettier
060: sf.getServiceFactory().setWrapped(false);
061:
062: // Use the HTTP Binding which understands the Java Rest Annotations
063: sf.getClientFactoryBean().setBindingId(
064: HttpBindingFactory.HTTP_BINDING_ID);
065: sf.setAddress("http://localhost:9080/xml/");
066: BookService bs = (BookService) sf.create();
067: GetBook getBook = new GetBook();
068: getBook.setId(123);
069: Book book = bs.getBook(getBook);
070: assertEquals(book.getId(), (long) 123);
071: assertEquals(book.getName(), "CXF in Action");
072: }
073:
074: @Test
075: public void testGetBookWithOutXmlRootElement() throws Exception {
076: JaxWsProxyFactoryBean sf = new JaxWsProxyFactoryBean();
077: sf.setServiceClass(BookService.class);
078:
079: // Turn off wrapped mode to make our xml prettier
080: sf.getServiceFactory().setWrapped(false);
081:
082: // Use the HTTP Binding which understands the Java Rest Annotations
083: sf.getClientFactoryBean().setBindingId(
084: HttpBindingFactory.HTTP_BINDING_ID);
085: sf.setAddress("http://localhost:9080/xml/");
086: BookService bs = (BookService) sf.create();
087: GetAnotherBook getAnotherBook = new GetAnotherBook();
088: getAnotherBook.setId(123);
089: Book book = bs.getAnotherBook(getAnotherBook);
090: assertEquals(book.getId(), (long) 123);
091: assertEquals(book.getName(), "CXF in Action");
092: }
093:
094: @Test
095: public void testGetBooksJSON() throws Exception {
096: String endpointAddress = "http://localhost:9080/json/books";
097: URL url = new URL(endpointAddress);
098: InputStream in = url.openStream();
099: assertNotNull(in);
100:
101: InputStream expected = getClass().getResourceAsStream(
102: "resources/expected_json_books.txt");
103:
104: assertEquals(getStringFromInputStream(expected),
105: getStringFromInputStream(in));
106: }
107:
108: @Test
109: @Ignore
110: public void testGetBookJSON() throws Exception {
111: String endpointAddress = "http://localhost:9080/json/books/123";
112: URL url = new URL(endpointAddress);
113: InputStream in = url.openStream();
114: assertNotNull(in);
115:
116: InputStream expected = getClass().getResourceAsStream(
117: "resources/expected_json_book123.txt");
118:
119: assertEquals(getStringFromInputStream(expected),
120: getStringFromInputStream(in));
121: }
122:
123: @Test
124: public void testAddBookJSON() throws Exception {
125: String endpointAddress = "http://localhost:9080/json/books";
126:
127: String inputFile = getClass().getResource(
128: "resources/add_book_json.txt").getFile();
129: File input = new File(inputFile);
130: PostMethod post = new PostMethod(endpointAddress);
131: RequestEntity entity = new FileRequestEntity(input,
132: "text/plain; charset=ISO-8859-1");
133: post.setRequestEntity(entity);
134: HttpClient httpclient = new HttpClient();
135:
136: try {
137: int result = httpclient.executeMethod(post);
138: System.out.println("Response status code: " + result);
139: System.out.println("Response body: ");
140: System.out.println(post.getResponseBodyAsString());
141:
142: //InputStream expected = getClass().getResourceAsStream("resources/expected_add_book_json.txt");
143:
144: //FIXME: result returned is not correct: {"acme.addBookResponse":"2"}
145: //assertEquals(getStringFromInputStream(expected), post.getResponseBodyAsString());
146: } finally {
147: // Release current connection to the connection pool once you are done
148: post.releaseConnection();
149: }
150: }
151:
152: private String getStringFromInputStream(InputStream in)
153: throws Exception {
154: CachedOutputStream bos = new CachedOutputStream();
155: IOUtils.copy(in, bos);
156: in.close();
157: bos.close();
158: System.out.println(bos.getOut().toString());
159: return bos.getOut().toString();
160: }
161:
162: }
|