return links;
}
@Test
public void testCreateCancelPurge() throws Exception {
Client c = new Client();
String url = "http://localhost:" + getJettyPort() + "/shop";
WebResource wr = c.resource(url);
ClientResponse response = wr.head();
Map<String, Link> shoppingLinks = processLinkHeaders(response);
Link customers = shoppingLinks.get("customers");
System.out.println("** Create a customer through this URL: " + customers.getHref());
Customer customer = new Customer();
customer.setFirstName("Bill");
customer.setLastName("Burke");
customer.setStreet("10 Somewhere Street");
customer.setCity("Westford");
customer.setState("MA");
customer.setZip("01711");
customer.setCountry("USA");
wr = c.resource(customers.getHref());
response = wr.type(MediaType.APPLICATION_XML).post(ClientResponse.class, customer);
Assert.assertEquals(201, response.getStatus());
Link orders = shoppingLinks.get("orders");
Order order = new Order();
order.setTotal("$199.99");
order.setCustomer(customer);
order.setDate(new Date().toString());
LineItem item = new LineItem();
item.setCost("$199.99");
item.setProduct("iPhone");
order.setLineItems(new ArrayList<LineItem>());
order.getLineItems().add(item);
System.out.println();
System.out.println("** Create an order through this URL: " + orders.getHref());
wr = c.resource(orders.getHref());
response = wr.type(MediaType.APPLICATION_XML).post(ClientResponse.class, order);
Assert.assertEquals(201, response.getStatus());
String createdOrderUrl = (String) response.getHeaders().getFirst("Location");
System.out.println();
System.out.println("** New list of orders");
wr = c.resource(orders.getHref());
response = wr.get(ClientResponse.class);
System.out.println(response.getEntity(String.class));
Map<String, Link> ordersLinks = processLinkHeaders(response);
wr = c.resource(createdOrderUrl);
response = wr.head();
Map<String, Link> orderLinks = processLinkHeaders(response);
Link cancel = orderLinks.get("cancel");
if (cancel != null) {
System.out.println("** Canceling the order at URL: " + cancel.getHref());
wr = c.resource(cancel.getHref());
response = wr.post(ClientResponse.class);
Assert.assertEquals(204, response.getStatus());
}
System.out.println();
System.out.println("** New list of orders after cancel: ");
wr = c.resource(orders.getHref());
response = wr.get(ClientResponse.class);
System.out.println(response.getEntity(String.class));
System.out.println();
Link purge = ordersLinks.get("purge");
System.out.println("** Purge cancelled orders at URL: " + purge.getHref());
wr = c.resource(purge.getHref());
response = wr.post(ClientResponse.class);
Assert.assertEquals(204, response.getStatus());
System.out.println();
System.out.println("** New list of orders after purge: ");
wr = c.resource(orders.getHref());
response = wr.get(ClientResponse.class);
System.out.println(response.getEntity(String.class));
}