1 package org.collectionspace.services.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.services.collectionobject.CollectionObject;
14 import org.collectionspace.services.collectionobject.CollectionObjectList;
15 import org.collectionspace.services.client.CollectionObjectClient;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * A CollectionObjectNuxeoServiceTest.
22 * @version $Revision:$
24 public class CollectionObjectServiceTest {
26 private CollectionObjectClient collectionObjectClient = CollectionObjectClient.getInstance();
27 private String updateId = null;
28 private String deleteId = null;
29 final Logger logger = LoggerFactory.getLogger(CollectionObjectServiceTest.class);
32 public void createCollectionObject() {
33 long identifier = this.createIdentifier();
35 CollectionObject collectionObject = createCollectionObject(identifier);
36 ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
37 verbose("createCollectionObject: status = " + res.getStatus());
38 Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
40 //store updateId locally for "update" test
42 updateId = extractId(res);
44 deleteId = extractId(res);
45 verbose("Set deleteId: " + deleteId);
49 @Test(dependsOnMethods = {"createCollectionObject"})
50 public void updateCollectionObject() {
51 ClientResponse<CollectionObject> res = collectionObjectClient.getCollectionObject(updateId);
52 verbose("getCollectionObject: status = " + res.getStatus());
53 Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
55 CollectionObject collectionObject = res.getEntity();
56 verbose("Got CollectionObject to update with ID: " + updateId,
57 collectionObject, CollectionObject.class);
59 //collectionObject.setCsid("updated-" + updateId);
60 collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
61 collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
63 // make call to update service
64 res = collectionObjectClient.updateCollectionObject(updateId, collectionObject);
65 verbose("updateCollectionObject: status = " + res.getStatus());
66 Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
69 CollectionObject updatedCollectionObject = res.getEntity();
70 Assert.assertEquals(updatedCollectionObject.getObjectName(), collectionObject.getObjectName());
71 verbose("updateCollectionObject: ", updatedCollectionObject, CollectionObject.class);
76 @Test(dependsOnMethods = {"createCollectionObject"})
77 public void createCollection() {
78 for(int i = 0; i < 3; i++){
79 this.createCollectionObject();
83 @Test(dependsOnMethods = {"createCollection"})
84 public void getCollectionObjectList() {
85 //the resource method is expected to return at least an empty list
86 ClientResponse<CollectionObjectList> res = collectionObjectClient.getCollectionObjectList();
87 CollectionObjectList coList = res.getEntity();
88 verbose("getCollectionObjectList: status = " + res.getStatus());
89 Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
91 List<CollectionObjectList.CollectionObjectListItem> coItemList = coList.getCollectionObjectListItem();
93 for(CollectionObjectList.CollectionObjectListItem pli : coItemList){
94 verbose("getCollectionObjectList: list-item[" + i + "] csid=" + pli.getCsid());
95 verbose("getCollectionObjectList: list-item[" + i + "] objectNumber=" + pli.getObjectNumber());
96 verbose("getCollectionObjectList: list-item[" + i + "] URI=" + pli.getUri());
101 @Test(dependsOnMethods = {"createCollection"})
102 public void deleteCollectionObject() {
103 verbose("Calling deleteCollectionObject:" + deleteId);
104 ClientResponse<Response> res = collectionObjectClient.deleteCollectionObject(deleteId);
105 verbose("deleteCollectionObject: csid=" + deleteId);
106 verbose("deleteCollectionObject: status = " + res.getStatus());
107 Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
110 private CollectionObject createCollectionObject(long identifier) {
111 CollectionObject collectionObject = createCollectionObject("objectNumber-" + identifier,
112 "objectName-" + identifier);
114 return collectionObject;
117 private CollectionObject createCollectionObject(String objectNumber, String objectName) {
118 CollectionObject collectionObject = new CollectionObject();
120 collectionObject.setObjectNumber(objectNumber);
121 collectionObject.setObjectName(objectName);
123 return collectionObject;
126 private String extractId(ClientResponse<Response> res) {
127 MultivaluedMap mvm = res.getMetadata();
128 String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
129 String[] segments = uri.split("/");
130 String id = segments[segments.length - 1];
135 private void verbose(String msg) {
136 // if(logger.isInfoEnabled()){
137 // logger.debug(msg);
139 System.out.println(msg);
142 private void verbose(String msg, Object o, Class clazz) {
145 JAXBContext jc = JAXBContext.newInstance(clazz);
146 Marshaller m = jc.createMarshaller();
147 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
149 m.marshal(o, System.out);
155 private void verboseMap(MultivaluedMap map) {
156 for(Object entry : map.entrySet()){
157 MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;
158 verbose(" name=" + mentry.getKey() + " value=" + mentry.getValue());
162 private long createIdentifier() {
163 long identifier = System.currentTimeMillis();