@Test public void testCustomerResource() throws Exception { System.out.println("*** Create a new Customer ***"); Customer newCustomer = new Customer(); newCustomer.setFirstName("Bill"); newCustomer.setLastName("Burke"); newCustomer.setStreet("256 Clarendon Street"); newCustomer.setCity("Boston"); newCustomer.setState("MA"); newCustomer.setZip("02115"); newCustomer.setCountry("USA"); Response response = client .target("http://localhost:8080/services/customers") .request() .post(Entity.xml(newCustomer)); if (response.getStatus() != 201) throw new RuntimeException("Failed to create"); String location = response.getLocation().toString(); System.out.println("Location: " + location); response.close(); System.out.println("*** GET XML Created Customer **"); String xml = client.target(location).request().accept(MediaType.APPLICATION_XML_TYPE).get(String.class); System.out.println(xml); System.out.println("*** GET JSON Created Customer **"); String json = client.target(location).request().accept(MediaType.APPLICATION_JSON_TYPE).get(String.class); System.out.println(json); }
@Test public void testCreateCancelPurge() throws Exception { String base = "http://localhost:8080/services/shop"; Response response = client.target(base).request().head(); Link customers = response.getLink("customers"); Link orders = response.getLink("orders"); response.close(); System.out.println("** Create a customer through this URL: " + customers.getUri().toString()); 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"); response = client.target(customers).request().post(Entity.xml(customer)); Assert.assertEquals(201, response.getStatus()); response.close(); 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.getUri().toString()); response = client.target(orders).request().post(Entity.xml(order)); Assert.assertEquals(201, response.getStatus()); URI createdOrderUrl = response.getLocation(); response.close(); System.out.println(); System.out.println("** New list of orders"); response = client.target(orders).request().get(); String orderList = response.readEntity(String.class); System.out.println(orderList); Link purge = response.getLink("purge"); response.close(); response = client.target(createdOrderUrl).request().head(); Link cancel = response.getLink("cancel"); response.close(); if (cancel != null) { System.out.println("** Canceling the order at URL: " + cancel.getUri().toString()); response = client.target(cancel).request().post(null); Assert.assertEquals(204, response.getStatus()); response.close(); } System.out.println(); System.out.println("** New list of orders after cancel: "); orderList = client.target(orders).request().get(String.class); System.out.println(orderList); System.out.println(); System.out.println("** Purge cancelled orders at URL: " + purge.getUri().toString()); response = client.target(purge).request().post(null); Assert.assertEquals(204, response.getStatus()); response.close(); System.out.println(); System.out.println("** New list of orders after purge: "); orderList = client.target(orders).request().get(String.class); System.out.println(orderList); }