]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
db1674aaa7c705b40fd8b7aca05d1d39b138dbfb
[tmp/jakarta-migration.git] /
1 package org.collectionspace.hello.client.test;
2
3 import java.util.ArrayList;
4 import java.util.List;
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;
12
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;
17
18 /**
19  * A PersonNuxeoServiceTest.
20  * 
21  * @version $Revision:$
22  */
23 public class CollectionObjectServiceTest {
24
25     private CollectionObjectClient collectionObjectClient = CollectionObjectClient.getInstance();
26     private String updateId = "";
27     private String deleteId = "";
28
29     @Test
30     public void createCollectionObject() {
31         long identifier = this.createIdentifier();
32         
33         CollectionObject collectionObject = createCollectionObject(identifier);
34         ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
35         Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
36         
37         //store updateId locally for "update" test
38         updateId = extractId(res);        
39     }
40
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);
47         
48         collectionObject.setCsid("updated-" + updateId);
49         collectionObject.setIdentifier("updated-" + collectionObject.getIdentifier());
50         collectionObject.setDescription("updated-" + collectionObject.getDescription());
51         
52         res = collectionObjectClient.updateCollectionObject(updateId, collectionObject);
53         CollectionObject updatedCollectionObject = res.getEntity();
54         Assert.assertEquals(updatedCollectionObject.getDescription(), "updated-" + collectionObject.getDescription());
55
56         verbose("updated collectionObject", updatedCollectionObject, CollectionObject.class);
57         
58         return;
59     }
60
61     @Test(dependsOnMethods = {"createCollectionObject"})
62     public void createCollection() {
63         for (int i = 0; i < 3; i++) {
64                 this.createCollectionObject();
65         }
66     }
67     
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();
73         int i = 0;
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());
78             i++;
79         }
80     }
81
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());
88     }
89
90     private CollectionObject createCollectionObject(String csid, String identifier, String description) {
91         CollectionObject collectionObject = new CollectionObject();
92         
93         collectionObject.setCsid(csid);
94         collectionObject.setIdentifier(identifier);
95         collectionObject.setDescription(description);
96
97         return collectionObject;
98     }
99
100     private CollectionObject createCollectionObject(long identifier) {
101         CollectionObject collectionObject = createCollectionObject("csid-" + identifier,
102                         "did-" + identifier, "description-" + identifier);      
103
104         return collectionObject;
105     }
106
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];
112         verbose("id=" + id);
113         return id;
114     }
115
116     private void verbose(String msg) {
117         System.out.println("PersonServiceTest : " + msg);
118     }
119
120     private void verbose(String msg, Object o, Class clazz) {
121         try{
122             verbose(msg);
123             JAXBContext jc = JAXBContext.newInstance(clazz);
124             Marshaller m = jc.createMarshaller();
125             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
126                     Boolean.TRUE);
127             m.marshal(o, System.out);
128         }catch(Exception e){
129             e.printStackTrace();
130         }
131     }
132
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());
137         }
138     }
139     
140     private long createIdentifier() {
141         long identifier = System.currentTimeMillis();
142         return identifier;
143     }
144 }