1 package org.collectionspace.hello.client.test;
3 import java.util.ArrayList;
5 import javax.ws.rs.core.MultivaluedMap;
6 import javax.ws.rs.core.Response;
7 import javax.xml.bind.JAXBContext;
8 import javax.xml.bind.Marshaller;
9 import org.jboss.resteasy.client.ClientResponse;
10 import org.testng.Assert;
11 import org.testng.annotations.Test;
13 import org.collectionspace.hello.CollectionObject;
14 import org.collectionspace.hello.CollectionObjectList;
15 import org.collectionspace.hello.client.CollectionObjectClient;
18 * A CollectionObjectNuxeoServiceTest.
20 * @version $Revision:$
22 public class CollectionObjectServiceTest {
24 private CollectionObjectClient collectionObjectClient = CollectionObjectClient.getInstance();
25 private String updateId = null;
26 private String deleteId = null;
29 public void createCollectionObject() {
30 long identifier = this.createIdentifier();
32 CollectionObject collectionObject = createCollectionObject(identifier);
33 ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
34 Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
36 //store updateId locally for "update" test
38 updateId = extractId(res);
40 deleteId = extractId(res);
43 @Test(dependsOnMethods = {"createCollectionObject"})
44 public void updateCollectionObject() {
45 ClientResponse<CollectionObject> res = collectionObjectClient.getCollectionObject(updateId);
46 CollectionObject collectionObject = res.getEntity();
47 verbose("Got CollectionObject to update with ID: " + updateId,
48 collectionObject, CollectionObject.class);
50 //collectionObject.setCsid("updated-" + updateId);
51 collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
52 collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
54 // make call to update service
55 res = collectionObjectClient.updateCollectionObject(updateId, collectionObject);
58 CollectionObject updatedCollectionObject = res.getEntity();
59 Assert.assertEquals(updatedCollectionObject.getObjectName(), collectionObject.getObjectName());
60 verbose("updateCollectionObject: ", updatedCollectionObject, CollectionObject.class);
65 @Test(dependsOnMethods = {"createCollectionObject"})
66 public void createCollection() {
67 for (int i = 0; i < 3; i++) {
68 this.createCollectionObject();
72 @Test(dependsOnMethods = {"createCollection"})
73 public void getCollectionObjectList() {
74 //the resource method is expected to return at least an empty list
75 CollectionObjectList coList = collectionObjectClient.getCollectionObjectList().getEntity();
76 List<CollectionObjectList.CollectionObjectListItem> coItemList = coList.getCollectionObjectListItem();
78 for(CollectionObjectList.CollectionObjectListItem pli : coItemList) {
79 verbose("getCollectionObjectList: list-item[" + i + "] csid=" + pli.getCsid());
80 verbose("getCollectionObjectList: list-item[" + i + "] objectNumber=" + pli.getObjectNumber());
81 verbose("getCollectionObjectList: list-item[" + i + "] URI=" + pli.getUri());
86 @Test(dependsOnMethods = {"updateCollectionObject"})
87 public void deleteCollectionObject() {
88 ClientResponse<Response> res = collectionObjectClient.deleteCollectionObject(deleteId);
89 verbose("deleteCollectionObject: csid=" + deleteId);
90 verbose("deleteCollectionObject: status = " + res.getStatus());
91 Assert.assertEquals(res.getStatus(), Response.Status.NO_CONTENT.getStatusCode());
94 private CollectionObject createCollectionObject(long identifier) {
95 CollectionObject collectionObject = createCollectionObject("objectNumber-" + identifier,
96 "objectName-" + identifier);
98 return collectionObject;
101 private CollectionObject createCollectionObject(String objectNumber, String objectName) {
102 CollectionObject collectionObject = new CollectionObject();
104 collectionObject.setObjectNumber(objectNumber);
105 collectionObject.setObjectName(objectName);
107 return collectionObject;
110 private String extractId(ClientResponse<Response> res) {
111 MultivaluedMap mvm = res.getMetadata();
112 String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
113 String[] segments = uri.split("/");
114 String id = segments[segments.length - 1];
119 private void verbose(String msg) {
120 System.out.println("CollectionObject Test: " + msg);
123 private void verbose(String msg, Object o, Class clazz) {
126 JAXBContext jc = JAXBContext.newInstance(clazz);
127 Marshaller m = jc.createMarshaller();
128 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
130 m.marshal(o, System.out);
136 private void verboseMap(MultivaluedMap map) {
137 for(Object entry : map.entrySet()){
138 MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;
139 verbose(" name=" + mentry.getKey() + " value=" + mentry.getValue());
143 private long createIdentifier() {
144 long identifier = System.currentTimeMillis();