]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
Adding tests to CollectionObject service
authorRichard Millet <richard.millet@berkeley.edu>
Tue, 7 Apr 2009 18:27:52 +0000 (18:27 +0000)
committerRichard Millet <richard.millet@berkeley.edu>
Tue, 7 Apr 2009 18:27:52 +0000 (18:27 +0000)
sandbox/richard/HelloWorld/HelloWorldClient/src/main/java/org/collectionspace/hello/client/IdentifierClient.java
sandbox/richard/HelloWorld/HelloWorldClient/src/test/java/org/collectionspace/hello/client/test/CollectionObjectServiceTest.java [new file with mode: 0644]
sandbox/richard/HelloWorld/HelloWorldNuxeoService/src/main/java/org/collectionspace/hello/services/HelloworldNuxeoApplication.java
sandbox/richard/HelloWorld/HelloWorldNuxeoService/src/main/java/org/collectionspace/hello/services/IdentifierResource.java

index a7b12f9673b514109b711f5c7e46002ae1de091c..eda08b0d58830dda3dd0d46b7cb02029e378c2dd 100644 (file)
@@ -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 (file)
index 0000000..db1674a
--- /dev/null
@@ -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<Response> 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<CollectionObject> 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<CollectionObjectList.CollectionObjectListItem> 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<Response> 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<Response> 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;
+    }
+}
index d6b05c69194a3b5b5f7cad7535cbb565faea7284..e9e233ae19d07155d6147cc76de136302933e585 100644 (file)
@@ -11,6 +11,7 @@ public class HelloworldNuxeoApplication extends Application {
 
     public HelloworldNuxeoApplication() {
         singletons.add(new CollectionObjectResource());
+        singletons.add(new IdentifierResource());
         singletons.add(new PersonNuxeoResource());
     }
 
index 4c41048907f51ef1f2fbb1d3a8e22c8f6cdb43cc..a91b4b56ff8a2b15b7332b703f3bd4403b7a07dc 100644 (file)
@@ -29,6 +29,7 @@ public class IdentifierResource {
     private AtomicLong idCounter = new AtomicLong();
 
     public IdentifierResource() {
+       // do nothing
     }
 
     @POST