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.PersonNuxeo;
16 import org.collectionspace.hello.client.CollectionObjectClient;
19 * A PersonNuxeoServiceTest.
21 * @version $Revision:$
23 public class CollectionObjectServiceTest {
25 private CollectionObjectClient collectionObjectClient = CollectionObjectClient.getInstance();
26 private String updateId = "";
27 private String deleteId = "";
30 public void createCollectionObject() {
31 long identifier = this.createIdentifier();
33 CollectionObject collectionObject = createCollectionObject(identifier);
34 ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
35 Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
37 //store updateId locally for "update" test
38 updateId = extractId(res);
41 @Test(dependsOnMethods = {"createCollectionObject"})
42 public void updatePerson() {
43 ClientResponse<CollectionObject> res = collectionObjectClient.getCollectionObject(updateId);
44 CollectionObject collectionObject = res.getEntity();
45 verbose("got collectionobject to update: " + updateId,
46 collectionObject, CollectionObject.class);
48 collectionObject.setCsid("updated-" + updateId);
49 collectionObject.setIdentifier("updated-" + collectionObject.getIdentifier());
50 collectionObject.setDescription("updated-" + collectionObject.getDescription());
52 res = collectionObjectClient.updateCollectionObject(updateId, collectionObject);
53 CollectionObject updatedCollectionObject = res.getEntity();
54 Assert.assertEquals(updatedCollectionObject.getDescription(), "updated-" + collectionObject.getDescription());
56 verbose("updated collectionObject", updatedCollectionObject, CollectionObject.class);
61 @Test(dependsOnMethods = {"createCollectionObject"})
62 public void createCollection() {
63 for (int i = 0; i < 3; i++) {
64 this.createCollectionObject();
68 @Test(dependsOnMethods = {"createCollection"})
69 public void getCollectionObjectList() {
70 //the resource method is expected to return at least an empty list
71 CollectionObjectList coList = collectionObjectClient.getCollectionObjectList().getEntity();
72 List<CollectionObjectList.CollectionObjectListItem> coItemList = coList.getCollectionObjectListItem();
74 for(CollectionObjectList.CollectionObjectListItem pli : coItemList) {
75 verbose("getCollectionObjectList: list-item[" + i + "] csid=" + pli.getCsid());
76 verbose("getCollectionObjectList: list-item[" + i + "] identifier=" + pli.getIdentifier());
77 verbose("getCollectionObjectList: list-item[" + i + "] URI=" + pli.getUri());
82 @Test(dependsOnMethods = {"updateCollectionObject"})
83 public void deleteCollectionObject() {
84 ClientResponse<Response> res = collectionObjectClient.deleteCollectionObject(deleteId);
85 verbose("deleteCollectionObject: csid=" + deleteId);
86 verbose("deleteCollectionObject: status = " + res.getStatus());
87 Assert.assertEquals(res.getStatus(), Response.Status.NO_CONTENT.getStatusCode());
90 private CollectionObject createCollectionObject(String csid, String identifier, String description) {
91 CollectionObject collectionObject = new CollectionObject();
93 collectionObject.setCsid(csid);
94 collectionObject.setIdentifier(identifier);
95 collectionObject.setDescription(description);
97 return collectionObject;
100 private CollectionObject createCollectionObject(long identifier) {
101 CollectionObject collectionObject = createCollectionObject("csid-" + identifier,
102 "did-" + identifier, "description-" + identifier);
104 return collectionObject;
107 private String extractId(ClientResponse<Response> res) {
108 MultivaluedMap mvm = res.getMetadata();
109 String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
110 String[] segments = uri.split("/");
111 String id = segments[segments.length - 1];
116 private void verbose(String msg) {
117 System.out.println("PersonServiceTest : " + msg);
120 private void verbose(String msg, Object o, Class clazz) {
123 JAXBContext jc = JAXBContext.newInstance(clazz);
124 Marshaller m = jc.createMarshaller();
125 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
127 m.marshal(o, System.out);
133 private void verboseMap(MultivaluedMap map) {
134 for(Object entry : map.entrySet()){
135 MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;
136 verbose(" name=" + mentry.getKey() + " value=" + mentry.getValue());
140 private long createIdentifier() {
141 long identifier = System.currentTimeMillis();