websvc.restkit/test/qa-functional/data/projects/TestClientTestApp/test/test/RestTestClientTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun Nov 22 11:35:02 2009 +0100 (10 hours ago)
changeset 154033 fa8b12981306
parent 1145843b732982cf4a
permissions -rw-r--r--
Merge RP javadoc improvements
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
     5  *
     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]"
    23  *
    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.
    34  *
    35  * Contributor(s):
    36  *
    37  * Portions Copyrighted 2009 Sun Microsystems, Inc.
    38  */
    39 package test;
    40 
    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;
    49 
    50 /**
    51  * Test for REST test client
    52  *
    53  * Duration of this test suite: aprox. 3min (using Firefox 3)
    54  *
    55  * @author lukas
    56  */
    57 public class RestTestClientTest extends TelluriumJavaTestCase {
    58 
    59     public RestTestClientTest() {
    60     }
    61 
    62     private static TestClient tc;
    63 
    64     @BeforeClass
    65     public static void initUi() {
    66         tc = new TestClient();
    67         tc.defineUi();
    68     }
    69 
    70     @Before
    71     public void setUpBeforeTest(){
    72         connectUrl("http://localhost:8080/CustomerDB/rest-test/test-resbeans.html"); //NOI18N
    73     }
    74 
    75     /**
    76      * Test for GET request
    77      */
    78     @Test
    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);
    88         tc.doTest();
    89 
    90         String s = tc.getContentFromView("raw"); //NOI18N
    91         try {
    92             JSONObject json = new JSONObject(s);
    93         } catch (JSONException ex) {
    94             ex.printStackTrace(System.err);
    95             fail("invalid JSON string: [" + s + "]"); //NOI18N
    96         }
    97         // check app/xml response format
    98         tc.setSelectedRMethod("GET(application/xml)"); //NOI18N
    99         assertEquals("GET(application/xml)", tc.getSelectedRMethod()); //NOI18N
   100         tc.doTest();
   101         s = tc.getContentFromView("raw"); //NOI18N
   102         try {
   103             Utils.readXml(s);
   104         } catch (SAXParseException se) {
   105             se.printStackTrace(System.err);
   106             fail("invalid xml response [" + s + "]"); //NOI18N
   107         }
   108     }
   109 
   110     /**
   111      * Test for GET request
   112      */
   113     @Test
   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:
   123         // GET, PUT, DELETE
   124         assertEquals(3, tc.getAvailableRMethods().length);
   125         // set an ID of a customer
   126         tc.setTestArg("resourceId", "1"); //NOI18N
   127         tc.doTest();
   128         String s = tc.getContentFromView("raw"); //NOI18N
   129         try {
   130             Utils.readXml(s);
   131         } catch (SAXParseException se) {
   132             se.printStackTrace(System.err);
   133             fail("invalid xml response [" + s + "]"); //NOI18N
   134         }
   135 
   136         // check app/json response format
   137         tc.setSelectedMIMEType("application/json"); //NOI18N
   138         assertEquals("application/json", tc.getSelectedMIMEType()); //NOI18N
   139         tc.doTest();
   140         s = tc.getContentFromView("raw"); //NOI18N
   141         try {
   142             JSONObject json = new JSONObject(s);
   143         } catch (JSONException ex) {
   144             ex.printStackTrace(System.err);
   145             fail("invalid JSON string: [" + s + "]"); //NOI18N
   146         }
   147     }
   148 
   149     /**
   150      * Test for POST request
   151      */
   152     @Test
   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
   160         tc.doTest();
   161         String s = tc.getContentFromView("raw"); //NOI18N
   162         assertEquals(1000000, Utils.getCreditLimit(1001));
   163 
   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
   168         tc.doTest();
   169         s = tc.getContentFromView("raw"); //NOI18N
   170         assertEquals(1000000, Utils.getCreditLimit(1010));
   171     }
   172 
   173     /**
   174      * Test for PUT request
   175      */
   176     @Test
   177     public void testPutRequest() {
   178         // show test UI for 'customers/{customerId}' resource
   179         tc.expand("customers"); //NOI18N
   180         tc.clickOn("customerId"); //NOI18N
   181         // choose put
   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
   190         tc.doTest();
   191         String s = tc.getContentFromView("raw"); //NOI18N
   192         assertEquals(0, Utils.getCreditLimit(1010));
   193 
   194         // choose app/xml
   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
   201         tc.doTest();
   202         s = tc.getContentFromView("raw"); //NOI18N
   203         assertEquals(0, Utils.getCreditLimit(1001));
   204     }
   205 
   206     /**
   207      * Test for DELETE request
   208      */
   209     @Test
   210     public void testDeleteRequest() {
   211         // show test UI for 'customers/{customerId}' resource
   212         tc.expand("customers"); //NOI18N
   213         tc.clickOn("customerId"); //NOI18N
   214         // choose delete
   215         tc.setSelectedRMethod("DELETE"); //NOI18N
   216         assertEquals("DELETE", tc.getSelectedRMethod()); //NOI18N
   217         // choose app/xml
   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
   222         tc.doTest();
   223         String s = tc.getContentFromView("raw"); //NOI18N
   224         assertEquals(-1, Utils.getCreditLimit(1001));
   225 
   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
   232         tc.doTest();
   233         s = tc.getContentFromView("raw"); //NOI18N
   234         assertEquals(-1, Utils.getCreditLimit(1010));
   235     }
   236 }