Java Reference
In-Depth Information
request.get(String.class).getEntity();
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("SEAT List \n" +
seatList);
Object obj=JSONValue.parse(seatList);
JSONArray array=(JSONArray)obj;
JSONObject seat =(JSONObject)array.get(4);
Boolean isbooked =
(Boolean)seat.get("booked");
assertTrue(isbooked);
}
So in our test, we are first creating the ClientRequestFactory class from the
URI http://localhost:8080/ticket-agency-ws/rest/seat . The factory
is then used to issue two ClientRequest ; the first one will be used to book a Tick-
et, adding the URI /4 , that will be translated on the server side as an invocation of
buyTicket , passing as the ID value of 4 .
Next, another ClientRequest is issued to retrieve the list of Seats which will be
returned in the JSON format. The last piece of code uses a utility library named
JSON.simple ( http://code.google.com/p/json-simple/ ) that adds a thin wrapper over
your JSON objects so that they can conveniently be converted from/to Java objects.
Once we have got a reference to the fourth element in the JSONArray , we'll simply
check the Boolean value of the field booked, asserting that it has actually been
booked.
Compiling our Ticket example
In order to compile our project with the REST web service, we need to import the
JAX-RS API that is included in the application server libraries. This ensures that we
Search WWH ::




Custom Search