websvc.restkit/test/qa-functional/data/projects/TestClientTestApp/test/test/RestTestClientTest.java
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
24 * If you wish your version of this file to be governed by only the CDDL
25 * or only the GPL Version 2, indicate your decision by adding
26 * "[Contributor] elects to include this software in this distribution
27 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28 * single choice of license, a recipient has the option to distribute
29 * your version of this file under either the CDDL, the GPL Version 2 or
30 * to extend the choice of license to its licensees as provided above.
31 * However, if you add GPL Version 2 code and therefore, elected the GPL
32 * Version 2 license, then the option applies only if the new code is
33 * made subject to such option by the copyright holder.
37 * Portions Copyrighted 2009 Sun Microsystems, Inc.
41 import org.codehaus.jettison.json.JSONException;
42 import org.codehaus.jettison.json.JSONObject;
43 import static org.junit.Assert.*;
44 import org.junit.Before;
45 import org.junit.BeforeClass;
46 import org.junit.Test;
47 import org.tellurium.test.java.TelluriumJavaTestCase;
48 import org.xml.sax.SAXParseException;
51 * Test for REST test client
53 * Duration of this test suite: aprox. 3min (using Firefox 3)
57 public class RestTestClientTest extends TelluriumJavaTestCase {
59 public RestTestClientTest() {
62 private static TestClient tc;
65 public static void initUi() {
66 tc = new TestClient();
71 public void setUpBeforeTest(){
72 connectUrl("http://localhost:8080/CustomerDB/rest-test/test-resbeans.html"); //NOI18N
76 * Test for GET request
79 public void testGetResponseFormatOnContainer() {
80 // show test UI for 'customers' resource
81 tc.clickOn("customers"); //NOI18N
82 // GET(application/json) should be selected by default - let's check it
83 assertEquals("GET(application/json)", tc.getSelectedRMethod()); //NOI18N
84 //should have four options:
85 // GET(application/xml), GET(application/json),
86 // POST(application/xml), POST(application/json)
87 assertEquals(4, tc.getAvailableRMethods().length);
90 String s = tc.getContentFromView("raw"); //NOI18N
92 JSONObject json = new JSONObject(s);
93 } catch (JSONException ex) {
94 ex.printStackTrace(System.err);
95 fail("invalid JSON string: [" + s + "]"); //NOI18N
97 // check app/xml response format
98 tc.setSelectedRMethod("GET(application/xml)"); //NOI18N
99 assertEquals("GET(application/xml)", tc.getSelectedRMethod()); //NOI18N
101 s = tc.getContentFromView("raw"); //NOI18N
104 } catch (SAXParseException se) {
105 se.printStackTrace(System.err);
106 fail("invalid xml response [" + s + "]"); //NOI18N
111 * Test for GET request
114 public void testGetResponseFormat() {
115 // show test UI for 'customers/{customerId}' resource
116 tc.expand("customers"); //NOI18N
117 tc.clickOn("customerId"); //NOI18N
118 // GET and application/xml should be selected by default - let's check it
119 // XXX - should the default mime be app/json? IZ #156896
120 assertEquals("GET", tc.getSelectedRMethod()); //NOI18N
121 assertEquals("application/xml", tc.getSelectedMIMEType()); //NOI18N
122 //should have three options:
124 assertEquals(3, tc.getAvailableRMethods().length);
125 // set an ID of a customer
126 tc.setTestArg("resourceId", "1"); //NOI18N
128 String s = tc.getContentFromView("raw"); //NOI18N
131 } catch (SAXParseException se) {
132 se.printStackTrace(System.err);
133 fail("invalid xml response [" + s + "]"); //NOI18N
136 // check app/json response format
137 tc.setSelectedMIMEType("application/json"); //NOI18N
138 assertEquals("application/json", tc.getSelectedMIMEType()); //NOI18N
140 s = tc.getContentFromView("raw"); //NOI18N
142 JSONObject json = new JSONObject(s);
143 } catch (JSONException ex) {
144 ex.printStackTrace(System.err);
145 fail("invalid JSON string: [" + s + "]"); //NOI18N
150 * Test for POST request
153 public void testPostRequest() {
154 // show test UI for 'customers' resource
155 tc.clickOn("customers"); //NOI18N
156 // choose post - app/xml
157 tc.setSelectedRMethod("POST(application/xml)"); //NOI18N
158 assertEquals("POST(application/xml)", tc.getSelectedRMethod()); //NOI18N
159 tc.setTestArg("content", Utils.readFile("resources/newCustomer.xml")); //NOI18N
161 String s = tc.getContentFromView("raw"); //NOI18N
162 assertEquals(1000000, Utils.getCreditLimit(1001));
164 // choose post - app/json
165 tc.setSelectedRMethod("POST(application/json)"); //NOI18N
166 assertEquals("POST(application/json)", tc.getSelectedRMethod()); //NOI18N
167 tc.setTestArg("content", Utils.readFile("resources/newCustomer.json")); //NOI18N
169 s = tc.getContentFromView("raw"); //NOI18N
170 assertEquals(1000000, Utils.getCreditLimit(1010));
174 * Test for PUT request
177 public void testPutRequest() {
178 // show test UI for 'customers/{customerId}' resource
179 tc.expand("customers"); //NOI18N
180 tc.clickOn("customerId"); //NOI18N
182 tc.setSelectedRMethod("PUT"); //NOI18N
183 assertEquals("PUT", tc.getSelectedRMethod()); //NOI18N
184 // choose app/json response format
185 tc.setSelectedMIMEType("application/json"); //NOI18N
186 assertEquals("application/json", tc.getSelectedMIMEType()); //NOI18N
187 // set resource to be modified ID
188 tc.setTestArg("resourceId", "1010"); //NOI18N
189 tc.setTestArg("content", Utils.readFile("resources/putCustomer.json")); //NOI18N
191 String s = tc.getContentFromView("raw"); //NOI18N
192 assertEquals(0, Utils.getCreditLimit(1010));
195 tc.setSelectedMIMEType("application/xml"); //NOI18N
196 assertEquals("application/xml", tc.getSelectedMIMEType()); //NOI18N
197 assertEquals("PUT", tc.getSelectedRMethod()); //NOI18N
198 // set resource to be modified ID
199 tc.setTestArg("resourceId", "1001"); //NOI18N
200 tc.setTestArg("content", Utils.readFile("resources/putCustomer.xml")); //NOI18N
202 s = tc.getContentFromView("raw"); //NOI18N
203 assertEquals(0, Utils.getCreditLimit(1001));
207 * Test for DELETE request
210 public void testDeleteRequest() {
211 // show test UI for 'customers/{customerId}' resource
212 tc.expand("customers"); //NOI18N
213 tc.clickOn("customerId"); //NOI18N
215 tc.setSelectedRMethod("DELETE"); //NOI18N
216 assertEquals("DELETE", tc.getSelectedRMethod()); //NOI18N
218 tc.setSelectedMIMEType("application/xml"); //NOI18N
219 assertEquals("application/xml", tc.getSelectedMIMEType()); //NOI18N
220 // set resource to be deleted ID
221 tc.setTestArg("resourceId", "1001"); //NOI18N
223 String s = tc.getContentFromView("raw"); //NOI18N
224 assertEquals(-1, Utils.getCreditLimit(1001));
226 // choose app/json response format
227 tc.setSelectedMIMEType("application/json"); //NOI18N
228 assertEquals("application/json", tc.getSelectedMIMEType()); //NOI18N
229 assertEquals("DELETE", tc.getSelectedRMethod()); //NOI18N
230 // set resource to be deleted ID
231 tc.setTestArg("resourceId", "1010"); //NOI18N
233 s = tc.getContentFromView("raw"); //NOI18N
234 assertEquals(-1, Utils.getCreditLimit(1010));