TestingRESTServices

Revision as of 16:50, 5 November 2009 by Admin (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)

Testing RESTful Web Services using NB6

Testing REST Services can be easily done using Test Client that is part of the REST Service Support. An action 'Test REST Services' is provided that does build, deploy, and invoke the browser to test the REST Services.

Contents



Image:customers_TestingRESTServices.png

WADL used:

<?xml version="1.0" encoding="UTF-8"?>    
  <application xmlns="http://research.sun.com/wadl/2006/10">
    <resources base="http://localhost:8080/CustomerDB/resources/">
      <resource path="/discountCodes/">
        <method name="GET">
          <request>
            <param xmlns:xs="http://www.w3.org/2001/XMLSchema" default="0" type="xs:int" style="query" name="start">
            <param xmlns:xs="http://www.w3.org/2001/XMLSchema" default="10" type="xs:int" style="query" name="max">
          </request>
          <response>
            <representation mediaType="application/xml">
            <representation mediaType="application/json">
          </response>
        </method>
        <method name="POST">
          <request>
            <representation mediaType="application/xml">
            <representation mediaType="application/json">
          </request>
          <response>
            <representation mediaType="*/*">
          </response>
        </method>
        <resource path="{discountCode}/">
      </resource>
      <resource path="/customers/">
        <method name="GET">
          <request>
            <param xmlns:xs="http://www.w3.org/2001/XMLSchema" default="0" type="xs:int" style="query" name="start">
            <param xmlns:xs="http://www.w3.org/2001/XMLSchema" default="10" type="xs:int" style="query" name="max">
          </request>
          <response>
            <representation mediaType="application/xml">
            <representation mediaType="application/json">
          </response>
        </method>
        <method name="POST">
          <request>
            <representation mediaType="application/xml">
            <representation mediaType="application/json">
          </request>
          <response>
            <representation mediaType="*/*">
          </response>
        </method>
        <resource path="{customerId}/">
        <resource path="googleMap/">
      </resource>
      <resource path="googleMap/">
        <method name="GET">
          <request>
            <param xmlns:xs="http://www.w3.org/2001/XMLSchema" type="xs:string" style="query" name="apiKey">
            <param xmlns:xs="http://www.w3.org/2001/XMLSchema" default="16 Network Circle, Menlo Park" type="xs:string" style="query" name="address">
            <param xmlns:xs="http://www.w3.org/2001/XMLSchema" default="15" type="xs:int" style="query" name="zoom">
          </request>
          <response>
            <representation mediaType="text/html">
          </response>
        </method>
      </resource>
    </resources>
  </application>

Preparation:

0. Required Installations:



http://wiki.netbeans.org/wiki/attach/RestJMakiComponents/InstallRestPlugin.png

  • Also install the jMaki Ajax plugin (in IDE follow Tools->Plugins->Available Plugins,

then select 'jMaki Ajax support' and click 'Install' button), as shown below:


http://wiki.netbeans.org/wiki/attach/RestJMakiComponents/InstalljMakiPlugin.png

Note: You can also download org-netbeans-modules-sun-jmaki.nbm from jMaki Netbeans plugin area and install the plugin seperately.

1. Creating a 'CustomerDB' Web Project

First, you need to create a web project. In this case, we will use one of the samples that is included in the New Project wizard as our starting point.

  • Choose File > New Project. Under Categories, select Samples and then RESTful Web Services. Under Projects, select Customer Database, as shown below:



http://wiki.netbeans.org/wiki/attach/RestJMakiComponents/CreateCustomerDB.png

  • Click Next and click Finish.

The sample project opens in the IDE, with its project structure in the Projects window.


2. Testing the 'CustomerDB' RESTful Web Services

To test the application, do the following:

  • In the Projects window, right click the project and choose Test RESTful Web Services. The server starts and the browser opens, displaying the RESTful web client, which you can use for testing the interaction with the RESTful web service.


http://wiki.netbeans.org/wiki/attach/RestJMakiComponents/TestCustomerDB.png

  • Now, Select the '/customers' link and then click Test. Next, select a customer link (such as '/customers/1/') and then click Test again. (Following snapshot is taken from NB6.0 FCS RESTful Web Services support)


http://wiki.netbeans.org/wiki/attach/RestJMakiComponents/TestPage.png

  • In the browser "Allow to run Javascript". Each browser has a unique way of asking to run Javascript.
  • On the Left side of the Test Client click a resource "customers". On the right side

click "Test". This by default tests "GET" operation.

  • On the "Test Output" section, you can see a table, where you can click further to

navigate to containee resources. For eg: /customers/863

  • On /customers you can do "POST" operation too. In this case enter the xml

that you wish to POST.

Note: For Containee resources like /customers/863 you can do following operations

  • GET
  • PUT
  • DELETE

whereas for a Container resource like /customers, you can only do

  • GET
  • POST

Testing Data

1. GET (to update Entity Resource) operation

You dont need any input for GET operation

2. POST (to Container Resource for creating an Entity Resource) operation

  1. 1 Posting to /customers (to create /customers/10101 Customer Resource)
<customer>
   <addressline1>111 E. Las Olas Blvd</addressline1>
   <addressline2>Suite 51</addressline2>
   <city>Fort Lauderdale</city>
   <creditLimit>100000</creditLimit>
   <customerId>10101</customerId>
   <discountCodeRef uri="http://localhost:8080/RESTDBApp7/restbean/discountCodes/N/"/>
   <email>jumbocom@gmail.com</email>
   <fax>305-777-4635</fax>
   <name>Barfoo</name>
   <phone>305-777-4632</phone>
   <state>FL</state>
   <zip>33015</zip>
</customer>
  1. 2 Posting to /purchaseOrders (to create /purchaseOrders/20198010 PurchaseOrder Resource)
<purchaseOrder>
  <customerRef uri="http://localhost:8080/RESTDBApp7/restbean/customers/1"/>
  <freightCompany>We deliver</freightCompany>
  <orderNum>20198010</orderNum>
  <productRef uri="http://localhost:8080/RESTDBApp7/restbean/products/980001/"/>
  <quantity>75</quantity>
  <salesDate>2006-07-28T00:00:00-07:00</salesDate>
  <shippingCost>105.00</shippingCost>
  <shippingDate>2006-07-28T00:00:00-07:00</shippingDate>
</purchaseOrder>

Just remember that all references are looked up using URIs and the resources for those URIs have to exist. Otherwise, you will get an 404 response.

Peter


3. PUT (to update Entity Resource) operation

  • To make it easy, do a GET on /customers/863, copy the xml
  • make some changes to xml
  • select "PUT", also select "application/xml" mimetype
  • paste the modified xml to the textarea before "Test" button
  • Click "Test"
  • You should see the updated content in the "Raw view"

4. DELETE (to update Entity Resource) operation

  • Select an entity /customers/863
  • Select "DELETE"
  • Select /customers and do get to see if /customers/863 exist.

You need to know before testing

Let me elaborate on testing POST, PUT and DELETE. Due to database constraints, not all operations will succeed. We rely on the underlying database to detect this and throw exception. For example, if you try to post (create) a new customer to the uri /customers using the following xml document:

<customer>
  <addressline1>18333 Ginnini Place</addressline1>
  <addressline2>Suite 51</addressline2>
  <city>Castro</city>
  <creditLimit>100000</creditLimit>
  <customerId>10007</customerId>
  <email>foo.bar@sun.com</email>
  <fax>925-264-7259</fax>
  <name>Foo Bar</name>
  <phone>305-777-4632</phone>
  <state>CA</state>
  <zip>33015</zip>
</customer>

You will get an exception from the database because a discountCode is required. So, the proper way to post this is to include a reference to a discountCode, e.g. <discountCodeRef uri="http://localhost:8080/CustomerDB/resources/discountCodes/N"/> or <discountCodeRef uri="http://localhost:8080/CustomerDB/resources/customers/3/discountCode/"/>

Note that the second reference will set the new customer's discountCode to customer 3's discountCode.

However, if you post the new customers to /discountCodes/N/customers, then you don't need to specify a discountCodeRef. This is because the generated code will automatically set the new customer's discountCode to discountCode N. In fact, it doesn't matter what discountCodeRef you specify in the xml because it will always get set to discountCode N.

As for the PUT operation, let's say you want to modify customer 2's record using the following xml:

<customer>
  <addressline1>22 Sun Drive</addressline1>
  <city>Ramon</city>
  <creditLimit>5000000</creditLimit>
  <customerId>2</customerId>
  <discountCodeRef uri="http://localhost:8080/CustomerDB/resources/discountCodes/L/"/>
  <email>Sun Microsystems</email>
  <fax>305-456-8889</fax>
  <name>Liu Enterprise</name>
  <phone>305-456-8888</phone>
  <purchaseOrders>
      <purchaseOrderRef uri="http://localhost:8080/CustomerDB/resources/customers/2/purchaseOrders/10398002/"/>
      <purchaseOrderRef uri="http://localhost:8080/CustomerDB/resources/customers/2/purchaseOrders/10398003/"/>
  </purchaseOrders>
  <state>CA</state>
  <zip>94552</zip>
</customer>

As before, you can set the discountCode to a different URI but it cannot be null. In addition, for the purchaseOrders, due to database constraint (a purchase order cannot exist without a customer), you cannot remove a purchaseOrder from the purchaseOrders collection. However, you can add an existing purchaseOrder, e.g. <purchaseOrderRef uri="http://localhost:8080/CustomerDB/resources/customers/3/purchaseOrders/10398003/"/>

This will move the purchaseOrder 10398003 belonging to customer 3 to customer 2.

BTW, the only way to remove a purchaseOrder from a customer's purchaseOrders collection is to delete the purchaseOrder. This brings us to DELETE. Again, not all delete will succeed. Currently, the generated code relies on cascade delete to handle relationship deletion. So, if cascade delete is not specified in the entity bean, then delete may fail due to database constraints.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo