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: " + updateId,
48 collectionObject, CollectionObject.class);
50 //collectionObject.setCsid("updated-" + updateId);
51 collectionObject.setIdentifier("updated-" + collectionObject.getIdentifier());
52 collectionObject.setDescription("updated-" + collectionObject.getDescription());
54 res = collectionObjectClient.updateCollectionObject(updateId, collectionObject);
55 CollectionObject updatedCollectionObject = res.getEntity();
56 Assert.assertEquals(updatedCollectionObject.getDescription(), collectionObject.getDescription());
58 verbose("updated collectionObject", updatedCollectionObject, CollectionObject.class);
63 @Test(dependsOnMethods = {"createCollectionObject"})
64 public void createCollection() {
65 for (int i = 0; i < 3; i++) {
66 this.createCollectionObject();
70 @Test(dependsOnMethods = {"createCollection"})
71 public void getCollectionObjectList() {
72 //the resource method is expected to return at least an empty list
73 CollectionObjectList coList = collectionObjectClient.getCollectionObjectList().getEntity();
74 List<CollectionObjectList.CollectionObjectListItem> coItemList = coList.getCollectionObjectListItem();
76 for(CollectionObjectList.CollectionObjectListItem pli : coItemList) {
77 verbose("getCollectionObjectList: list-item[" + i + "] csid=" + pli.getCsid());
78 verbose("getCollectionObjectList: list-item[" + i + "] identifier=" + pli.getIdentifier());
79 verbose("getCollectionObjectList: list-item[" + i + "] URI=" + pli.getUri());
84 @Test(dependsOnMethods = {"updateCollectionObject"})
85 public void deleteCollectionObject() {
86 ClientResponse<Response> res = collectionObjectClient.deleteCollectionObject(deleteId);
87 verbose("deleteCollectionObject: csid=" + deleteId);
88 verbose("deleteCollectionObject: status = " + res.getStatus());
89 Assert.assertEquals(res.getStatus(), Response.Status.NO_CONTENT.getStatusCode());
92 private CollectionObject createCollectionObject(String csid, String identifier, String description) {
93 CollectionObject collectionObject = new CollectionObject();
95 collectionObject.setCsid(csid);
96 collectionObject.setIdentifier(identifier);
97 collectionObject.setDescription(description);
99 return collectionObject;
102 private CollectionObject createCollectionObject(long identifier) {
103 CollectionObject collectionObject = createCollectionObject("csid-" + identifier,
104 "did-" + identifier, "description-" + identifier);
106 return collectionObject;
109 private String extractId(ClientResponse<Response> res) {
110 MultivaluedMap mvm = res.getMetadata();
111 String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
112 String[] segments = uri.split("/");
113 String id = segments[segments.length - 1];
118 private void verbose(String msg) {
119 System.out.println("CollectionObjectServiceTest : " + msg);
122 private void verbose(String msg, Object o, Class clazz) {
125 JAXBContext jc = JAXBContext.newInstance(clazz);
126 Marshaller m = jc.createMarshaller();
127 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
129 m.marshal(o, System.out);
135 private void verboseMap(MultivaluedMap map) {
136 for(Object entry : map.entrySet()){
137 MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;
138 verbose(" name=" + mentry.getKey() + " value=" + mentry.getValue());
142 private long createIdentifier() {
143 long identifier = System.currentTimeMillis();