From 5509d6ade57d3047c55c56f188ade1f11fb29847 Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Sat, 2 May 2009 05:23:27 +0000 Subject: [PATCH] CSPACE-84: Adding Client Library code to new source structure. --- services/collectionobject/client/.classpath | 10 ++ services/collectionobject/client/.project | 23 +++ .../.settings/org.eclipse.jdt.core.prefs | 12 ++ .../.settings/org.maven.ide.eclipse.prefs | 8 + services/collectionobject/client/pom.xml | 86 ++++++++++ .../client/CollectionObjectClient.java | 94 +++++++++++ .../client/CollectionObjectProxy.java | 46 ++++++ .../test/CollectionObjectServiceTest.java | 147 ++++++++++++++++++ .../client/src/test/resources/log4j.xml | 45 ++++++ 9 files changed, 471 insertions(+) create mode 100644 services/collectionobject/client/.classpath create mode 100644 services/collectionobject/client/.project create mode 100644 services/collectionobject/client/.settings/org.eclipse.jdt.core.prefs create mode 100644 services/collectionobject/client/.settings/org.maven.ide.eclipse.prefs create mode 100644 services/collectionobject/client/pom.xml create mode 100644 services/collectionobject/client/src/main/java/org/collectionspace/services/client/CollectionObjectClient.java create mode 100644 services/collectionobject/client/src/main/java/org/collectionspace/services/client/CollectionObjectProxy.java create mode 100644 services/collectionobject/client/src/test/java/org/collectionspace/services/client/test/CollectionObjectServiceTest.java create mode 100644 services/collectionobject/client/src/test/resources/log4j.xml diff --git a/services/collectionobject/client/.classpath b/services/collectionobject/client/.classpath new file mode 100644 index 000000000..96f09f11f --- /dev/null +++ b/services/collectionobject/client/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/services/collectionobject/client/.project b/services/collectionobject/client/.project new file mode 100644 index 000000000..f44220ec1 --- /dev/null +++ b/services/collectionobject/client/.project @@ -0,0 +1,23 @@ + + + javaee-addressbook-client + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.maven.ide.eclipse.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.maven.ide.eclipse.maven2Nature + + diff --git a/services/collectionobject/client/.settings/org.eclipse.jdt.core.prefs b/services/collectionobject/client/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..8a8650d9d --- /dev/null +++ b/services/collectionobject/client/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +#Thu Mar 26 13:03:04 PDT 2009 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/services/collectionobject/client/.settings/org.maven.ide.eclipse.prefs b/services/collectionobject/client/.settings/org.maven.ide.eclipse.prefs new file mode 100644 index 000000000..c6e4123cd --- /dev/null +++ b/services/collectionobject/client/.settings/org.maven.ide.eclipse.prefs @@ -0,0 +1,8 @@ +#Thu Feb 26 16:30:28 PST 2009 +activeProfiles= +eclipse.preferences.version=1 +fullBuildGoals=process-test-resources +includeModules=false +resolveWorkspaceProjects=true +resourceFilterGoals=process-resources resources\:testResources +version=1 diff --git a/services/collectionobject/client/pom.xml b/services/collectionobject/client/pom.xml new file mode 100644 index 000000000..d3d0a9261 --- /dev/null +++ b/services/collectionobject/client/pom.xml @@ -0,0 +1,86 @@ + + + + + 4.0.0 + org.collectionspace.services.client + collectionobject-client + jar + 0.2 + CollectionObject Client + + + org.collectionspace.services + collectionobject-jaxb + 0.2 + + + org.collectionspace.services + org-collectionspace-services-common + 0.2 + + + + org.testng + testng + 5.6 + + + org.slf4j + slf4j-api + 1.5.2 + + + org.slf4j + slf4j-log4j12 + 1.5.2 + + + org.jboss.resteasy + resteasy-jaxrs + 1.0.2.GA + + + + tjws + webserver + + + + + org.jboss.resteasy + resteasy-jaxb-provider + 1.0.2.GA + + + org.jboss.resteasy + resteasy-multipart-provider + 1.0.2.GA + + + commons-httpclient + commons-httpclient + 3.1 + + + + + + + maven-compiler-plugin + 2.0.2 + + 1.5 + 1.5 + + + + + diff --git a/services/collectionobject/client/src/main/java/org/collectionspace/services/client/CollectionObjectClient.java b/services/collectionobject/client/src/main/java/org/collectionspace/services/client/CollectionObjectClient.java new file mode 100644 index 000000000..9bd95c175 --- /dev/null +++ b/services/collectionobject/client/src/main/java/org/collectionspace/services/client/CollectionObjectClient.java @@ -0,0 +1,94 @@ +package org.collectionspace.services.client; + +import javax.ws.rs.core.Response; + +import org.collectionspace.services.collectionobject.CollectionObject; +import org.collectionspace.services.collectionobject.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 extends CollectionSpaceClient { + + + /** + * + */ + private static final CollectionObjectClient instance = new CollectionObjectClient(); + /** + * + */ + private CollectionObjectProxy collectionObjectProxy; + + /** + * + * Default constructor for CollectionObjectClient class. + * + */ + private CollectionObjectClient() { + ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance(); + RegisterBuiltin.register(factory); + collectionObjectProxy = ProxyFactory.create(CollectionObjectProxy.class, getURL()); + } + + /** + * FIXME Comment this + * + * @return + */ + public static CollectionObjectClient getInstance() { + return instance; + } + + /** + * @return + * @see org.collectionspace.hello.client.CollectionObjectProxy#getCollectionObject() + */ + public ClientResponse getCollectionObjectList() { + return collectionObjectProxy.getCollectionObjectList(); + } + + /** + * @param csid + * @return + * @see org.collectionspace.hello.client.CollectionObjectProxy#getCollectionObject(java.lang.String) + */ + public ClientResponse getCollectionObject(String csid) { + return collectionObjectProxy.getCollectionObject(csid); + } + + /** + * @param collectionobject + * @return + * @see org.collectionspace.hello.client.CollectionObjectProxy#createCollectionObject(org.collectionspace.hello.CollectionObject) + */ + public ClientResponse createCollectionObject(CollectionObject collectionObject) { + return collectionObjectProxy.createCollectionObject(collectionObject); + } + + /** + * @param csid + * @param collectionobject + * @return + * @see org.collectionspace.hello.client.CollectionObjectProxy#updateCollectionObject(java.lang.Long, org.collectionspace.hello.CollectionObject) + */ + public ClientResponse updateCollectionObject(String csid, CollectionObject collectionObject) { + return collectionObjectProxy.updateCollectionObject(csid, collectionObject); + } + + /** + * @param csid + * @return + * @see org.collectionspace.hello.client.CollectionObjectProxy#deleteCollectionObject(java.lang.Long) + */ + public ClientResponse deleteCollectionObject(String csid) { + return collectionObjectProxy.deleteCollectionObject(csid); + } +} diff --git a/services/collectionobject/client/src/main/java/org/collectionspace/services/client/CollectionObjectProxy.java b/services/collectionobject/client/src/main/java/org/collectionspace/services/client/CollectionObjectProxy.java new file mode 100644 index 000000000..124e243c5 --- /dev/null +++ b/services/collectionobject/client/src/main/java/org/collectionspace/services/client/CollectionObjectProxy.java @@ -0,0 +1,46 @@ +package org.collectionspace.services.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.services.collectionobject.CollectionObject; +import org.collectionspace.services.collectionobject.CollectionObjectList; +import org.jboss.resteasy.client.ClientResponse; + +/** + * @version $Revision:$ + */ +@Path("/collectionobjects/") +@Produces({"application/xml"}) +@Consumes({"application/xml"}) +public interface CollectionObjectProxy { + + @GET + ClientResponse getCollectionObjectList(); + + //(C)reate + @POST + ClientResponse createCollectionObject(CollectionObject co); + + //(R)ead + @GET + @Path("/{csid}") + ClientResponse getCollectionObject(@PathParam("csid") String csid); + + //(U)pdate + @PUT + @Path("/{csid}") + ClientResponse updateCollectionObject(@PathParam("csid") String csid, CollectionObject co); + + //(D)elete + @DELETE + @Path("/{csid}") + ClientResponse deleteCollectionObject(@PathParam("csid") String csid); +} \ No newline at end of file diff --git a/services/collectionobject/client/src/test/java/org/collectionspace/services/client/test/CollectionObjectServiceTest.java b/services/collectionobject/client/src/test/java/org/collectionspace/services/client/test/CollectionObjectServiceTest.java new file mode 100644 index 000000000..c2780a8a4 --- /dev/null +++ b/services/collectionobject/client/src/test/java/org/collectionspace/services/client/test/CollectionObjectServiceTest.java @@ -0,0 +1,147 @@ +package org.collectionspace.services.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.jboss.resteasy.client.ClientResponse; +import org.testng.Assert; +import org.testng.annotations.Test; + +import org.collectionspace.services.collectionobject.CollectionObject; +import org.collectionspace.services.collectionobject.CollectionObjectList; +import org.collectionspace.services.client.CollectionObjectClient; + +/** + * A CollectionObjectNuxeoServiceTest. + * + * @version $Revision:$ + */ +public class CollectionObjectServiceTest { + + private CollectionObjectClient collectionObjectClient = CollectionObjectClient.getInstance(); + private String updateId = null; + private String deleteId = null; + + @Test + public void createCollectionObject() { + long identifier = this.createIdentifier(); + + CollectionObject collectionObject = createCollectionObject(identifier); + ClientResponse res = collectionObjectClient.createCollectionObject(collectionObject); + Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode()); + + //store updateId locally for "update" test + if (updateId == null) + updateId = extractId(res); + else + deleteId = extractId(res); + } + + @Test(dependsOnMethods = {"createCollectionObject"}) + public void updateCollectionObject() { + ClientResponse res = collectionObjectClient.getCollectionObject(updateId); + CollectionObject collectionObject = res.getEntity(); + verbose("Got CollectionObject to update with ID: " + updateId, + collectionObject, CollectionObject.class); + + //collectionObject.setCsid("updated-" + updateId); + collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber()); + collectionObject.setObjectName("updated-" + collectionObject.getObjectName()); + + // make call to update service + res = collectionObjectClient.updateCollectionObject(updateId, collectionObject); + + // check the response + CollectionObject updatedCollectionObject = res.getEntity(); + Assert.assertEquals(updatedCollectionObject.getObjectName(), collectionObject.getObjectName()); + verbose("updateCollectionObject: ", updatedCollectionObject, CollectionObject.class); + + return; + } + + @Test(dependsOnMethods = {"createCollectionObject"}) + public void createCollection() { + for (int i = 0; i < 3; i++) { + this.createCollectionObject(); + } + } + + @Test(dependsOnMethods = {"createCollection"}) + public void getCollectionObjectList() { + //the resource method is expected to return at least an empty list + CollectionObjectList coList = collectionObjectClient.getCollectionObjectList().getEntity(); + List coItemList = coList.getCollectionObjectListItem(); + int i = 0; + for(CollectionObjectList.CollectionObjectListItem pli : coItemList) { + verbose("getCollectionObjectList: list-item[" + i + "] csid=" + pli.getCsid()); + verbose("getCollectionObjectList: list-item[" + i + "] objectNumber=" + pli.getObjectNumber()); + verbose("getCollectionObjectList: list-item[" + i + "] URI=" + pli.getUri()); + i++; + } + } + + @Test(dependsOnMethods = {"updateCollectionObject"}) + public void deleteCollectionObject() { + ClientResponse res = collectionObjectClient.deleteCollectionObject(deleteId); + verbose("deleteCollectionObject: csid=" + deleteId); + verbose("deleteCollectionObject: status = " + res.getStatus()); + Assert.assertEquals(res.getStatus(), Response.Status.NO_CONTENT.getStatusCode()); + } + + private CollectionObject createCollectionObject(long identifier) { + CollectionObject collectionObject = createCollectionObject("objectNumber-" + identifier, + "objectName-" + identifier); + + return collectionObject; + } + + private CollectionObject createCollectionObject(String objectNumber, String objectName) { + CollectionObject collectionObject = new CollectionObject(); + + collectionObject.setObjectNumber(objectNumber); + collectionObject.setObjectName(objectName); + + return collectionObject; + } + + private String extractId(ClientResponse res) { + MultivaluedMap mvm = res.getMetadata(); + String uri = (String) ((ArrayList) mvm.get("Location")).get(0); + String[] segments = uri.split("/"); + String id = segments[segments.length - 1]; + verbose("id=" + id); + return id; + } + + private void verbose(String msg) { + System.out.println("CollectionObject Test: " + 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); + }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()); + } + } + + private long createIdentifier() { + long identifier = System.currentTimeMillis(); + return identifier; + } +} diff --git a/services/collectionobject/client/src/test/resources/log4j.xml b/services/collectionobject/client/src/test/resources/log4j.xml new file mode 100644 index 000000000..52121cb83 --- /dev/null +++ b/services/collectionobject/client/src/test/resources/log4j.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- 2.47.3