]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
First working test of the CollectionObject client and proxy. Currently executes...
authorAron Roberts <aron@socrates.berkeley.edu>
Thu, 26 Mar 2009 21:30:36 +0000 (21:30 +0000)
committerAron Roberts <aron@socrates.berkeley.edu>
Thu, 26 Mar 2009 21:30:36 +0000 (21:30 +0000)
sandbox/aron/HelloWorld-CollectionObject/HelloWorldClient/src/main/java/org/collectionspace/hello/client/CollectionObjectClient.java [new file with mode: 0644]
sandbox/aron/HelloWorld-CollectionObject/HelloWorldClient/src/main/java/org/collectionspace/hello/client/CollectionObjectProxy.java [new file with mode: 0644]
sandbox/aron/HelloWorld-CollectionObject/HelloWorldClient/src/test/java/org/collectionspace/hello/client/test/CollectionObjectTest.java [new file with mode: 0644]

diff --git a/sandbox/aron/HelloWorld-CollectionObject/HelloWorldClient/src/main/java/org/collectionspace/hello/client/CollectionObjectClient.java b/sandbox/aron/HelloWorld-CollectionObject/HelloWorldClient/src/main/java/org/collectionspace/hello/client/CollectionObjectClient.java
new file mode 100644 (file)
index 0000000..6a20afb
--- /dev/null
@@ -0,0 +1,94 @@
+package org.collectionspace.hello.client;
+
+import javax.ws.rs.core.Response;
+
+import org.collectionspace.hello.CollectionObject;
+import org.collectionspace.hello.CollectionObjectList;
+import org.jboss.resteasy.client.ProxyFactory;
+import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
+import org.jboss.resteasy.client.ClientResponse;
+import org.jboss.resteasy.spi.ResteasyProviderFactory;
+
+/**
+ * A CollectionObjectClient.
+
+ * @version $Revision:$
+ */
+public class CollectionObjectClient {
+
+    /**
+     *
+     */
+    private static final CollectionObjectClient instance = new CollectionObjectClient();
+    /**
+     *
+     */
+    private CollectionObjectProxy CollectionObjectProxy;
+
+    /**
+     *
+     * Create a new CollectionObjectClient.
+     *
+     */
+    private CollectionObjectClient() {
+        ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance();
+        RegisterBuiltin.register(factory);
+        CollectionObjectProxy =
+          ProxyFactory.create(CollectionObjectProxy.class, "http://localhost:8080/helloworld/cspace");
+    }
+
+    /**
+     * FIXME Comment this
+     *
+     * @return
+     */
+    public static CollectionObjectClient getInstance() {
+        return instance;
+    }
+
+    /**
+     * @param id
+     * @return
+     * @see org.collectionspace.hello.client.CollectionObjectProxy#getCollectionObject()
+     */
+    public ClientResponse<CollectionObjectList> getCollectionObjectList() {
+        return CollectionObjectProxy.getCollectionObjectList();
+    }
+
+    /**
+     * @param id
+     * @return
+     * @see org.collectionspace.hello.client.CollectionObjectProxy#getCollectionObject(java.lang.String)
+     */
+    public ClientResponse<CollectionObject> getCollectionObject(String id) {
+        return CollectionObjectProxy.getCollectionObject(id);
+    }
+
+    /**
+     * @param CollectionObject
+     * @return
+     * @see org.collectionspace.hello.client.CollectionObjectProxy#createCollectionObject(org.collectionspace.hello.client.entity.CollectionObject)
+     */
+    public ClientResponse<Response> createCollectionObject(CollectionObject CollectionObject) {
+        return CollectionObjectProxy.createCollectionObject(CollectionObject);
+    }
+
+    /**
+     * @param id
+     * @param CollectionObject
+     * @return
+     * @see org.collectionspace.hello.client.CollectionObjectProxy#updateCollectionObject(java.lang.String, org.collectionspace.hello.client.entity.CollectionObject)
+     */
+    public ClientResponse<CollectionObject> updateCollectionObject(String id, CollectionObject CollectionObject) {
+        return CollectionObjectProxy.updateCollectionObject(id, CollectionObject);
+    }
+
+    /**
+     * @param id
+     * @return
+     * @see org.collectionspace.hello.client.CollectionObjectProxy#deleteCollectionObject(java.lang.String)
+     */
+    public ClientResponse<Response> deleteCollectionObject(String id) {
+        return CollectionObjectProxy.deleteCollectionObject(id);
+    }
+}
diff --git a/sandbox/aron/HelloWorld-CollectionObject/HelloWorldClient/src/main/java/org/collectionspace/hello/client/CollectionObjectProxy.java b/sandbox/aron/HelloWorld-CollectionObject/HelloWorldClient/src/main/java/org/collectionspace/hello/client/CollectionObjectProxy.java
new file mode 100644 (file)
index 0000000..fcaefa0
--- /dev/null
@@ -0,0 +1,44 @@
+package org.collectionspace.hello.client;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Response;
+
+import org.collectionspace.hello.CollectionObject;
+import org.collectionspace.hello.CollectionObjectList;
+import org.jboss.resteasy.client.ClientResponse;
+
+/**
+ * @version $Revision:$
+ */
+@Path("/collectionobjects/")
+@Produces({"application/xml"})
+@Consumes({"application/xml"})
+public interface CollectionObjectProxy {
+
+    @GET
+    @Path("/{id}")
+    ClientResponse<CollectionObject> getCollectionObject(@PathParam("id") String id);
+
+    // List
+    @GET
+    ClientResponse<CollectionObjectList> getCollectionObjectList();
+
+    @POST
+    ClientResponse<Response> createCollectionObject(CollectionObject co);
+
+    @PUT
+    @Path("/{id}")
+    ClientResponse<CollectionObject> 
+      updateCollectionObject(@PathParam("id") String id, CollectionObject co);
+
+    @DELETE
+    @Path("/{id}")
+    ClientResponse<Response> deleteCollectionObject(@PathParam("id") String id);
+}
\ No newline at end of file
diff --git a/sandbox/aron/HelloWorld-CollectionObject/HelloWorldClient/src/test/java/org/collectionspace/hello/client/test/CollectionObjectTest.java b/sandbox/aron/HelloWorld-CollectionObject/HelloWorldClient/src/test/java/org/collectionspace/hello/client/test/CollectionObjectTest.java
new file mode 100644 (file)
index 0000000..db00786
--- /dev/null
@@ -0,0 +1,165 @@
+package org.collectionspace.hello.client.test;
+
+import java.util.ArrayList;
+import java.util.List;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+import org.collectionspace.hello.CollectionObject;
+import org.collectionspace.hello.CollectionObjectList;
+import org.collectionspace.hello.client.CollectionObjectClient;
+import org.jboss.resteasy.client.ClientResponse;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/**
+ * A CollectionObjectTest.
+ * 
+ * @version $Revision:$
+ */
+public class CollectionObjectTest {
+
+    private CollectionObjectClient client = CollectionObjectClient.getInstance();
+    private String updateId = "1";
+
+/*
+    @Test
+    public void createCollectionObject() {
+        CollectionObject CollectionObject = createCollectionObject("Chris", "Hoffman");
+        ClientResponse<Response> res = CollectionObjectClient.createCollectionObject(CollectionObject);
+        Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
+        //store updateId locally
+        updateId = extractId(res);
+    }
+
+    @Test
+    public void createCollectionObjectList() {
+        CollectionObject CollectionObject = createCollectionObject("Aron", "Roberts");
+        ClientResponse<Response> res = CollectionObjectClient.createCollectionObject(CollectionObject);
+        Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
+        CollectionObject = createCollectionObject("Dan", "Sheppard");
+        res = CollectionObjectClient.createCollectionObject(CollectionObject);
+        Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
+    }
+
+    @Test(dependsOnMethods = {"createCollectionObject"})
+    public void updateCollectionObject() {
+        CollectionObject touCollectionObject = CollectionObjectClient.getCollectionObject(updateId).getEntity();
+        verbose("got CollectionObject to update", touCollectionObject, CollectionObject.class);
+        touCollectionObject.setFirstName("Richard");
+        touCollectionObject.setLastName("Millet");
+        int initialVersion = touCollectionObject.getVersion();
+        CollectionObject uCollectionObject = CollectionObjectClient.updateCollectionObject(updateId, touCollectionObject).getEntity();
+        verbose("updated CollectionObject", uCollectionObject, CollectionObject.class);
+        Assert.assertNotSame(uCollectionObject.getVersion(), initialVersion);
+        Assert.assertEquals(uCollectionObject.getFirstName(), "Richard");
+    }
+
+    @Test(dependsOnMethods = {"createCollectionObject"})
+    public void getCollectionObjectList() {
+        //the resource method is expected to return at least an empty list
+        CollectionObjectList CollectionObjectList = CollectionObjectClient.getCollectionObjectList().getEntity();
+        List<CollectionObjectList.CollectionObjectListItem> list = CollectionObjectList.getCollectionObjectListItem();
+        int i = 0;
+        for (CollectionObjectList.CollectionObjectListItem pli : list) {
+            verbose("getCollectionObjectList: list-item[" + i + "] firstName=" + pli.getFirstName());
+            verbose("getCollectionObjectList: list-item[" + i + "] lastName=" + pli.getLastName());
+            verbose("getCollectionObjectList: list-item[" + i + "] uri=" + pli.getUri());
+            i++;
+        }
+    }
+
+*/
+    @Test
+    public void getNonExistingCollectionObject() {
+        ClientResponse<CollectionObject> res = client.getCollectionObject("foo");
+
+        Response.Status status = res.getResponseStatus();
+        verbose(this.getClass().getName() + ": " +
+                "getNonExistingCollectionObject: Status: code=" + status.getStatusCode() +
+                " message=" + status.toString());
+        verbose("getNonExistingCollectionObject: Metadata:");
+        verboseMap(res.getMetadata());
+        verbose("getNonExistingCollectionObject: Headers:");
+        verboseMap(res.getHeaders());
+        if (status.equals(Response.Status.NOT_FOUND)) {
+            String msg = res.getEntity(String.class, String.class);
+            verbose("getNonExistingCollectionObject: error message=" + msg);
+        }
+    }
+
+/*
+    @Test(dependsOnMethods = {"updateCollectionObject"})
+    public void updateWrongCollectionObject() {
+        CollectionObject touCollectionObject = CollectionObjectClient.getCollectionObject(updateId).getEntity();
+        verbose("updateWrongCollectionObject: got CollectionObject to update", touCollectionObject, CollectionObject.class);
+        touCollectionObject.setFirstName("Richard");
+        touCollectionObject.setLastName("Millet");
+        //use non existing CollectionObject to update
+        ClientResponse<CollectionObject> res = CollectionObjectClient.updateCollectionObject(9999L, touCollectionObject);
+        if (res.getResponseStatus().equals(Response.Status.NOT_FOUND)) {
+            verbose("updateWrongCollectionObject: Status=" + res.getResponseStatus().toString());
+            String msg = res.getEntity(String.class, String.class);
+            verbose("updateWrongCollectionObject: application error message=" + msg);
+        }
+    }
+
+
+    @Test(dependsOnMethods = {"updateWrongCollectionObject"})
+    public void deleteCollectionObject() {
+        ClientResponse<Response> res = CollectionObjectClient.deleteCollectionObject(updateId);
+        verbose("deleteCollectionObject: id=" + updateId);
+        verbose("deleteCollectionObject: status = " + res.getStatus());
+        Assert.assertEquals(res.getStatus(), Response.Status.NO_CONTENT.getStatusCode());
+    }
+    
+    private CollectionObject createCollectionObject(String firstName, String lastName) {
+        CollectionObject CollectionObject = new CollectionObject();
+        CollectionObject.setFirstName(firstName);
+        CollectionObject.setLastName(lastName);
+        CollectionObject.setStreet("2195 Hearst Ave.");
+        CollectionObject.setCity("Berkeley");
+        CollectionObject.setState("CA");
+        CollectionObject.setZip("94704");
+        CollectionObject.setCountry("US");
+        return CollectionObject;
+    }
+
+    private Long extractId(ClientResponse<Response> res) {
+        MultivaluedMap mvm = res.getMetadata();
+        String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
+        String[] segments = uri.split("/");
+        verbose("id=" + segments[segments.length - 1]);
+        return Long.valueOf(segments[segments.length - 1]);
+    }
+
+*/
+
+    private void verbose(String msg) {
+        System.out.println("CollectionObjectListerviceTest : " + msg);
+    }
+
+    private void verbose(String msg, Object o, Class clazz) {
+        try {
+            verbose(msg);
+            JAXBContext jc = JAXBContext.newInstance(clazz);
+            Marshaller m = jc.createMarshaller();
+            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
+                    Boolean.TRUE);
+            m.marshal(o, System.out);
+        //m.marshal(new JAXBElement(new QName("uri", "local"), CollectionObject.class, p), System.out);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    private void verboseMap(MultivaluedMap map) {
+        for (Object entry : map.entrySet()) {
+            MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;
+            verbose("    name=" + mentry.getKey() + " value=" + mentry.getValue());
+        }
+    }
+    
+
+}