From d124b59be71cea0a75c3cd593bdb7a537f35a6a5 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Thu, 26 Mar 2009 21:30:36 +0000 Subject: [PATCH] First working test of the CollectionObject client and proxy. Currently executes only one simple test case; need to complete the others. --- .../hello/client/CollectionObjectClient.java | 94 ++++++++++ .../hello/client/CollectionObjectProxy.java | 44 +++++ .../client/test/CollectionObjectTest.java | 165 ++++++++++++++++++ 3 files changed, 303 insertions(+) create mode 100644 sandbox/aron/HelloWorld-CollectionObject/HelloWorldClient/src/main/java/org/collectionspace/hello/client/CollectionObjectClient.java create mode 100644 sandbox/aron/HelloWorld-CollectionObject/HelloWorldClient/src/main/java/org/collectionspace/hello/client/CollectionObjectProxy.java create mode 100644 sandbox/aron/HelloWorld-CollectionObject/HelloWorldClient/src/test/java/org/collectionspace/hello/client/test/CollectionObjectTest.java 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 index 000000000..6a20afb94 --- /dev/null +++ b/sandbox/aron/HelloWorld-CollectionObject/HelloWorldClient/src/main/java/org/collectionspace/hello/client/CollectionObjectClient.java @@ -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 getCollectionObjectList() { + return CollectionObjectProxy.getCollectionObjectList(); + } + + /** + * @param id + * @return + * @see org.collectionspace.hello.client.CollectionObjectProxy#getCollectionObject(java.lang.String) + */ + public ClientResponse 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 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 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 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 index 000000000..fcaefa050 --- /dev/null +++ b/sandbox/aron/HelloWorld-CollectionObject/HelloWorldClient/src/main/java/org/collectionspace/hello/client/CollectionObjectProxy.java @@ -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 getCollectionObject(@PathParam("id") String id); + + // List + @GET + ClientResponse getCollectionObjectList(); + + @POST + ClientResponse createCollectionObject(CollectionObject co); + + @PUT + @Path("/{id}") + ClientResponse + updateCollectionObject(@PathParam("id") String id, CollectionObject co); + + @DELETE + @Path("/{id}") + ClientResponse 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 index 000000000..db00786bf --- /dev/null +++ b/sandbox/aron/HelloWorld-CollectionObject/HelloWorldClient/src/test/java/org/collectionspace/hello/client/test/CollectionObjectTest.java @@ -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 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 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 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 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 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 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 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()); + } + } + + +} -- 2.47.3