]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7887d0077a602e6c2d9c5bbade5ef2469837dd33
[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 with ID: " + updateId,
48                         collectionObject, CollectionObject.class);
49         
50         //collectionObject.setCsid("updated-" + updateId);
51         collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
52         collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
53         
54         // make call to update service
55         res = collectionObjectClient.updateCollectionObject(updateId, collectionObject);
56         
57         // check the response
58         CollectionObject updatedCollectionObject = res.getEntity();        
59         Assert.assertEquals(updatedCollectionObject.getObjectName(), collectionObject.getObjectName());
60         verbose("updateCollectionObject: ", updatedCollectionObject, CollectionObject.class);
61         
62         return;
63     }
64
65     @Test(dependsOnMethods = {"createCollectionObject"})
66     public void createCollection() {
67         for (int i = 0; i < 3; i++) {
68                 this.createCollectionObject();
69         }
70     }
71     
72     @Test(dependsOnMethods = {"createCollection"})
73     public void getCollectionObjectList() {
74         //the resource method is expected to return at least an empty list
75         CollectionObjectList coList = collectionObjectClient.getCollectionObjectList().getEntity();
76         List<CollectionObjectList.CollectionObjectListItem> coItemList = coList.getCollectionObjectListItem();
77         int i = 0;
78         for(CollectionObjectList.CollectionObjectListItem pli : coItemList) {
79             verbose("getCollectionObjectList: list-item[" + i + "] csid=" + pli.getCsid());
80             verbose("getCollectionObjectList: list-item[" + i + "] objectNumber=" + pli.getObjectNumber());
81             verbose("getCollectionObjectList: list-item[" + i + "] URI=" + pli.getUri());
82             i++;
83         }
84     }
85
86     @Test(dependsOnMethods = {"updateCollectionObject"})
87     public void deleteCollectionObject() {
88         ClientResponse<Response> res = collectionObjectClient.deleteCollectionObject(deleteId);
89         verbose("deleteCollectionObject: csid=" + deleteId);
90         verbose("deleteCollectionObject: status = " + res.getStatus());
91         Assert.assertEquals(res.getStatus(), Response.Status.NO_CONTENT.getStatusCode());
92     }
93
94     private CollectionObject createCollectionObject(long identifier) {
95         CollectionObject collectionObject = createCollectionObject("objectNumber-" + identifier,
96                         "objectName-" + identifier);            
97
98         return collectionObject;
99     }
100
101     private CollectionObject createCollectionObject(String objectNumber, String objectName) {
102         CollectionObject collectionObject = new CollectionObject();
103         
104         collectionObject.setObjectNumber(objectNumber);
105         collectionObject.setObjectName(objectName);
106
107         return collectionObject;
108     }
109
110     private String extractId(ClientResponse<Response> res) {
111         MultivaluedMap mvm = res.getMetadata();
112         String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
113         String[] segments = uri.split("/");
114         String id = segments[segments.length - 1];
115         verbose("id=" + id);
116         return id;
117     }
118
119     private void verbose(String msg) {
120         System.out.println("CollectionObject Test: " + msg);
121     }
122
123     private void verbose(String msg, Object o, Class clazz) {
124         try{
125             verbose(msg);
126             JAXBContext jc = JAXBContext.newInstance(clazz);
127             Marshaller m = jc.createMarshaller();
128             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
129                     Boolean.TRUE);
130             m.marshal(o, System.out);
131         }catch(Exception e){
132             e.printStackTrace();
133         }
134     }
135
136     private void verboseMap(MultivaluedMap map) {
137         for(Object entry : map.entrySet()){
138             MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;
139             verbose("    name=" + mentry.getKey() + " value=" + mentry.getValue());
140         }
141     }
142     
143     private long createIdentifier() {
144         long identifier = System.currentTimeMillis();
145         return identifier;
146     }
147 }