assertEquals("Can't get the right Json customer ", response.getText(), JSON_CUSTOMER);
}
private void testInvokingRestService(String serviceAddress) throws Exception {
ServletUnitClient client = newClient();
client.setExceptionsThrownOnErrorStatus(false);
WebRequest req =
new GetMethodQueryWebRequest(CONTEXT_URL + serviceAddress + "/customers");
WebResponse response = client.getResponse(req);
Document doc = DOMUtils.readXml(response.getInputStream());
assertNotNull(doc);
addNamespace("c", "http://cxf.apache.org/jra");
assertValid("/c:customers", doc);
assertValid("/c:customers/c:customer/c:id[text()='123']", doc);
assertValid("/c:customers/c:customer/c:name[text()='Dan Diephouse']", doc);
req = new GetMethodQueryWebRequest(CONTEXT_URL + serviceAddress + "/customers/123");
response = client.getResponse(req);
doc = DOMUtils.readXml(response.getInputStream());
assertNotNull(doc);
assertValid("/c:customer", doc);
assertValid("/c:customer/c:id[text()='123']", doc);
assertValid("/c:customer/c:name[text()='Dan Diephouse']", doc);
// Try invalid customer
req = new GetMethodQueryWebRequest(CONTEXT_URL + serviceAddress + "/customers/0");
response = client.getResponse(req);
assertEquals("Expect the wrong response code", response.getResponseCode(), 500);
doc = DOMUtils.readXml(response.getInputStream());
assertNotNull(doc);
assertValid("//c:CustomerNotFoundFault", doc);
PostMethodWebRequest postReq =
new PostMethodWebRequest(CONTEXT_URL + serviceAddress + "/customers",
getClass().getResourceAsStream("add.xml"),
"text/xml; charset=UTF-8");
response = client.getResponse(postReq);
doc = DOMUtils.readXml(response.getInputStream());
assertNotNull(doc);
assertValid("/c:addCustomer", doc);
PutMethodWebRequest putReq =
new PutMethodWebRequest(CONTEXT_URL + serviceAddress + "/customers/123",
getClass().getResourceAsStream("update.xml"),
"text/xml; charset=UTF-8");
response = client.getResponse(putReq);
doc = DOMUtils.readXml(response.getInputStream());
assertNotNull(doc);
assertValid("/c:updateCustomer", doc);
// Get the updated document
req = new GetMethodQueryWebRequest(CONTEXT_URL + serviceAddress + "/customers/123");
response = client.getResponse(req);
doc = DOMUtils.readXml(response.getInputStream());
assertNotNull(doc);
assertValid("/c:customer", doc);
assertValid("/c:customer/c:id[text()='123']", doc);