/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package RestTestServiceResource;

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Client;
import javax.ws.rs.core.Response;
import javax.ws.rs.client.Entity;

/**
 *
 * @author user-admin
 */
public class MyClient {

    public static void main(String[] args) {
        Client client = ClientBuilder.newClient();
        try {
            System.out.println("Creating a New Customer");
            String xml = "<customer>"
                    + "<first-name>Bill</first-name>"
                    + "<last-name>Burke</last-name>"
                    + "<street>256 Clarendon Street</street>"
                    + "<city>Boston</city>"
                    + "<state>MA</state>"
                    + "<zip>02115</zip>"
                    + "<country>USA</country>"
                    + "</customer>";
            System.out.println(xml);
            Response response
                    = client.target("http://localhost:8080/RestTestService/webresources/customers")
                            .request().post(Entity.xml(xml));
            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("Location: " + location);
            System.out.println("*** GET Created Customer **");
            String customer = client.target(location).request().get(String.class);
            System.out.println(customer);
            String updateCustomer = "<customer>"
                    + "<first-name>William</first-name>"
                    + "<last-name>Burke</last-name>"
                    + "<street>256 Clarendon Street</street>"
                    + "<city>Boston</city>"
                    + "<state>MA</state>"
                    + "<zip>02115</zip>"
                    + "<country>USA</country>"
                    + "</customer>";
            System.out.println(xml);
            response = client.target(location).request().put(Entity.xml(updateCustomer));
            if (response.getStatus() != 204) {
                throw new RuntimeException("Failed to update");
            }
            response.close();
            System.out.println("*** After Update ***");
            customer = client.target(location).request().get(String.class);
            System.out.println(customer);
        } finally {
            client.close();
        }
    }

}
