From bcb53d5c500d76f8493bc824efdd3a73ac9dab7d Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Tue, 7 Apr 2009 18:27:52 +0000 Subject: [PATCH] Adding tests to CollectionObject service --- .../hello/client/IdentifierClient.java | 2 +- .../test/CollectionObjectServiceTest.java | 144 ++++++++++++++++++ .../services/HelloworldNuxeoApplication.java | 1 + .../hello/services/IdentifierResource.java | 1 + 4 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 sandbox/richard/HelloWorld/HelloWorldClient/src/test/java/org/collectionspace/hello/client/test/CollectionObjectServiceTest.java diff --git a/sandbox/richard/HelloWorld/HelloWorldClient/src/main/java/org/collectionspace/hello/client/IdentifierClient.java b/sandbox/richard/HelloWorld/HelloWorldClient/src/main/java/org/collectionspace/hello/client/IdentifierClient.java index a7b12f967..eda08b0d5 100644 --- a/sandbox/richard/HelloWorld/HelloWorldClient/src/main/java/org/collectionspace/hello/client/IdentifierClient.java +++ b/sandbox/richard/HelloWorld/HelloWorldClient/src/main/java/org/collectionspace/hello/client/IdentifierClient.java @@ -32,7 +32,7 @@ public class IdentifierClient { private IdentifierClient() { ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance(); RegisterBuiltin.register(factory); - identifierProxy = ProxyFactory.create(IdentifierProxy.class, "http://localhost:8080/helloworld/cspace"); + identifierProxy = ProxyFactory.create(IdentifierProxy.class, "http://localhost:8080/helloworld/cspace-nuxeo"); } /** diff --git a/sandbox/richard/HelloWorld/HelloWorldClient/src/test/java/org/collectionspace/hello/client/test/CollectionObjectServiceTest.java b/sandbox/richard/HelloWorld/HelloWorldClient/src/test/java/org/collectionspace/hello/client/test/CollectionObjectServiceTest.java new file mode 100644 index 000000000..db1674aaa --- /dev/null +++ b/sandbox/richard/HelloWorld/HelloWorldClient/src/test/java/org/collectionspace/hello/client/test/CollectionObjectServiceTest.java @@ -0,0 +1,144 @@ +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.jboss.resteasy.client.ClientResponse; +import org.testng.Assert; +import org.testng.annotations.Test; + +import org.collectionspace.hello.CollectionObject; +import org.collectionspace.hello.CollectionObjectList; +import org.collectionspace.hello.PersonNuxeo; +import org.collectionspace.hello.client.CollectionObjectClient; + +/** + * A PersonNuxeoServiceTest. + * + * @version $Revision:$ + */ +public class CollectionObjectServiceTest { + + private CollectionObjectClient collectionObjectClient = CollectionObjectClient.getInstance(); + private String updateId = ""; + private String deleteId = ""; + + @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 + updateId = extractId(res); + } + + @Test(dependsOnMethods = {"createCollectionObject"}) + public void updatePerson() { + ClientResponse res = collectionObjectClient.getCollectionObject(updateId); + CollectionObject collectionObject = res.getEntity(); + verbose("got collectionobject to update: " + updateId, + collectionObject, CollectionObject.class); + + collectionObject.setCsid("updated-" + updateId); + collectionObject.setIdentifier("updated-" + collectionObject.getIdentifier()); + collectionObject.setDescription("updated-" + collectionObject.getDescription()); + + res = collectionObjectClient.updateCollectionObject(updateId, collectionObject); + CollectionObject updatedCollectionObject = res.getEntity(); + Assert.assertEquals(updatedCollectionObject.getDescription(), "updated-" + collectionObject.getDescription()); + + verbose("updated collectionObject", 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 + "] identifier=" + pli.getIdentifier()); + 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(String csid, String identifier, String description) { + CollectionObject collectionObject = new CollectionObject(); + + collectionObject.setCsid(csid); + collectionObject.setIdentifier(identifier); + collectionObject.setDescription(description); + + return collectionObject; + } + + private CollectionObject createCollectionObject(long identifier) { + CollectionObject collectionObject = createCollectionObject("csid-" + identifier, + "did-" + identifier, "description-" + identifier); + + 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("PersonServiceTest : " + 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/sandbox/richard/HelloWorld/HelloWorldNuxeoService/src/main/java/org/collectionspace/hello/services/HelloworldNuxeoApplication.java b/sandbox/richard/HelloWorld/HelloWorldNuxeoService/src/main/java/org/collectionspace/hello/services/HelloworldNuxeoApplication.java index d6b05c691..e9e233ae1 100644 --- a/sandbox/richard/HelloWorld/HelloWorldNuxeoService/src/main/java/org/collectionspace/hello/services/HelloworldNuxeoApplication.java +++ b/sandbox/richard/HelloWorld/HelloWorldNuxeoService/src/main/java/org/collectionspace/hello/services/HelloworldNuxeoApplication.java @@ -11,6 +11,7 @@ public class HelloworldNuxeoApplication extends Application { public HelloworldNuxeoApplication() { singletons.add(new CollectionObjectResource()); + singletons.add(new IdentifierResource()); singletons.add(new PersonNuxeoResource()); } diff --git a/sandbox/richard/HelloWorld/HelloWorldNuxeoService/src/main/java/org/collectionspace/hello/services/IdentifierResource.java b/sandbox/richard/HelloWorld/HelloWorldNuxeoService/src/main/java/org/collectionspace/hello/services/IdentifierResource.java index 4c4104890..a91b4b56f 100644 --- a/sandbox/richard/HelloWorld/HelloWorldNuxeoService/src/main/java/org/collectionspace/hello/services/IdentifierResource.java +++ b/sandbox/richard/HelloWorld/HelloWorldNuxeoService/src/main/java/org/collectionspace/hello/services/IdentifierResource.java @@ -29,6 +29,7 @@ public class IdentifierResource { private AtomicLong idCounter = new AtomicLong(); public IdentifierResource() { + // do nothing } @POST -- 2.47.3