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