]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
923aea877c55341cd7affac1b337dc53f4335e21
[tmp/jakarta-migration.git] /
1 /**
2  * This document is a part of the source code and related artifacts
3  * for CollectionSpace, an open source collections management system
4  * for museums and related institutions:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright © 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
17  *  Unless required by applicable law or agreed to in writing, software
18  *  distributed under the License is distributed on an "AS IS" BASIS,
19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  *  See the License for the specific language governing permissions and
21  *  limitations under the License.
22  */
23  
24 package org.collectionspace.services.client.test;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import javax.ws.rs.core.MultivaluedMap;
29 import javax.ws.rs.core.Response;
30 import javax.xml.bind.JAXBContext;
31 import javax.xml.bind.Marshaller;
32 import org.jboss.resteasy.client.ClientResponse;
33 import org.testng.Assert;
34 import org.testng.annotations.Test;
35
36 import org.collectionspace.services.collectionobject.CollectionObject;
37 import org.collectionspace.services.collectionobject.CollectionObjectList;
38 import org.collectionspace.services.client.CollectionObjectClient;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * CollectionObjectServiceTest, carries out tests against a
44  * deployed and running CollectionObject Service.
45  * 
46  * $LastChangedRevision$
47  * $LastChangedDate$
48  */
49 public class CollectionObjectServiceTest {
50
51   private CollectionObjectClient collectionObjectClient = CollectionObjectClient.getInstance();
52   private String knownCollectionObjectId = null;
53   private final String NON_EXISTENT_ID = createNonExistentIdentifier();
54   final Logger logger = LoggerFactory.getLogger(CollectionObjectServiceTest.class);
55   
56   
57   // ---------------------------------------------------------------
58   // Service Discovery tests
59   // ---------------------------------------------------------------
60
61   // TBA
62   
63   
64   // ---------------------------------------------------------------
65   // CRUD tests : CREATE tests
66   // ---------------------------------------------------------------
67
68   // Success outcomes
69   // ----------------
70   
71   // NOTE The W3C HTTP spec suggests that the URL of the newly-created
72   // resource be returned in the Location header, as well as in the
73   // entity body of the response: <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html>.
74   // If we follow this practice in our service, we might also test for the presence of
75   // these URLs in the response headers (e.g. via res.getMetadata().getFirst("Location"))
76   // and entity body.
77
78   // Create
79   @Test
80   public void createCollectionObject() {
81     String identifier = this.createIdentifier();
82
83     CollectionObject collectionObject = createCollectionObject(identifier);
84     ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
85     verbose("createCollectionObject: status = " + res.getStatus());
86     Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
87
88     // Store the ID returned from this create operation for additional tests below.
89     knownCollectionObjectId = extractId(res);
90   }
91
92   // Create multiple (used for Read multiple tests, below)
93   @Test(dependsOnMethods = {"createCollectionObject"})
94   public void createCollection() {
95     for(int i = 0; i < 3; i++){
96       this.createCollectionObject();
97     }
98   }
99
100   // Failure outcomes
101   // ----------------
102
103   // Create : sending null payload
104
105   // Create : sending wrong schema in payload
106   
107   // Create : sending random data in payload
108
109   // Invalid CollectionObject schema
110   // Question: How can we pass an empty entity body, a different (non-CollectionObject) schema,
111   // and/or 'junk' data to the service via the CollectionObjectClient?
112
113   // Create : with duplicate object ID
114   //
115   // Should fail with a 409 Conflict status code.
116   @Test(dependsOnMethods = {"createCollectionObject"})
117   public void createDuplicateCollectionObject() {
118     CollectionObject collectionObject = createCollectionObject(knownCollectionObjectId);
119     ClientResponse<Response> res = 
120       collectionObjectClient.createCollectionObject(collectionObject);
121     verbose("createDuplicateCollectionObject: status = " + res.getStatus());
122     Assert.assertEquals(res.getStatus(), Response.Status.CONFLICT.getStatusCode());
123   }
124
125
126   // ---------------------------------------------------------------
127   // CRUD tests : READ tests
128   // ---------------------------------------------------------------
129
130   // Success outcomes
131   // ----------------
132   
133   // These two test methods have not yet been tested:
134
135 /*
136   // Read
137   @Test(dependsOnMethods = {"createCollectionObject"})
138   public void getCollectionObject() {
139     ClientResponse<CollectionObject> res = 
140       collectionObjectClient.getCollectionObject(knownCollectionObjectId);
141     verbose("getCollectionObject: status = " + res.getStatus());
142     Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
143   }
144 */
145
146   // Failure outcomes
147   // ----------------
148
149 /*
150   // Read : with non-existent object ID
151   //
152   // Should fail with a 404 Not Found status code. 
153   @Test(dependsOnMethods = {"createCollectionObject"})
154   public void getNonExistentCollectionObject() {
155     ClientResponse<CollectionObject> res = 
156       collectionObjectClient.getCollectionObject(NON_EXISTENT_ID);
157     verbose("getNonExistentCollectionObject: status = " + res.getStatus());
158     Assert.assertEquals(res.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
159   }
160 */  
161
162   // ---------------------------------------------------------------
163   // CRUD tests : READ (list, or multiple) tests
164   // ---------------------------------------------------------------
165
166   // Success outcomes
167   // ----------------
168
169   // Read (multiple)
170   @Test(dependsOnMethods = {"createCollection"})
171   public void getCollectionObjectList() {
172     // The resource method is expected to return at least an empty list
173     ClientResponse<CollectionObjectList> res = collectionObjectClient.getCollectionObjectList();
174     CollectionObjectList coList = res.getEntity();
175     verbose("getCollectionObjectList: status = " + res.getStatus());
176     Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
177
178     List<CollectionObjectList.CollectionObjectListItem> coItemList =
179       coList.getCollectionObjectListItem();
180     int i = 0;
181     for(CollectionObjectList.CollectionObjectListItem pli : coItemList){
182       verbose("getCollectionObjectList: list-item[" + i + "] csid=" + pli.getCsid());
183       verbose("getCollectionObjectList: list-item[" + i + "] objectNumber=" + pli.getObjectNumber());
184       verbose("getCollectionObjectList: list-item[" + i + "] URI=" + pli.getUri());
185       i++;
186     }
187   }
188
189   // Failure outcomes
190   // ----------------
191
192   
193
194   // ---------------------------------------------------------------
195   // CRUD tests : UPDATE tests
196   // ---------------------------------------------------------------
197
198   // Success outcomes
199   // ----------------
200
201   // Update
202   @Test(dependsOnMethods = {"createCollectionObject"})
203   public void updateCollectionObject() {
204     ClientResponse<CollectionObject> res = 
205       collectionObjectClient.getCollectionObject(knownCollectionObjectId);
206     verbose("getCollectionObject: status = " + res.getStatus());
207     Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
208     CollectionObject collectionObject = res.getEntity();
209     verbose("Got CollectionObject to update with ID: " + knownCollectionObjectId,
210         collectionObject, CollectionObject.class);
211
212     //collectionObject.setCsid("updated-" + knownCollectionObjectId);
213     collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
214     collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
215
216     // make call to update service
217     res = 
218       collectionObjectClient.updateCollectionObject(knownCollectionObjectId, collectionObject);
219     verbose("updateCollectionObject: status = " + res.getStatus());
220     Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
221     
222     // check the response
223     CollectionObject updatedCollectionObject = res.getEntity();
224     Assert.assertEquals(updatedCollectionObject.getObjectName(), 
225       collectionObject.getObjectName());
226     verbose("updateCollectionObject: ", updatedCollectionObject, CollectionObject.class);
227   }
228
229   // Failure outcomes
230   // ----------------
231   
232   // Update : with non-existent object ID
233   //
234   // Should fail with a 404 Not Found status code.
235   @Test(dependsOnMethods = {"updateCollectionObject"})
236   public void updateNonExistentCollectionObject() {
237     CollectionObject collectionObject = createCollectionObject(NON_EXISTENT_ID);
238     // make call to update service
239     ClientResponse<CollectionObject> res =
240       collectionObjectClient.updateCollectionObject(NON_EXISTENT_ID, collectionObject);
241     verbose("createCollectionObject: status = " + res.getStatus());
242     Assert.assertEquals(res.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
243   }
244
245
246   // ---------------------------------------------------------------
247   // CRUD tests : DELETE tests
248   // ---------------------------------------------------------------
249
250   // Success outcomes
251   // ----------------
252
253   // Delete
254   @Test(dependsOnMethods = {"createCollectionObject"})
255   public void deleteCollectionObject() {
256     verbose("Calling deleteCollectionObject:" + knownCollectionObjectId);
257     ClientResponse<Response> res = collectionObjectClient.deleteCollectionObject(knownCollectionObjectId);
258     verbose("deleteCollectionObject: status = " + res.getStatus());
259     Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
260   }
261
262   // Failure outcomes
263   // ----------------
264
265   // Delete : with non-existent object ID
266   //
267   // Should fail with a 404 Not Found status code.
268   @Test(dependsOnMethods = {"deleteCollectionObject"})
269   public void deleteNonExistentCollectionObject() {
270     verbose("Calling deleteCollectionObject:" + NON_EXISTENT_ID);
271     ClientResponse<Response> res =
272       collectionObjectClient.deleteCollectionObject(NON_EXISTENT_ID);
273     verbose("deleteCollectionObject: status = " + res.getStatus());
274     Assert.assertEquals(res.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
275   }
276
277
278   // ---------------------------------------------------------------
279   // Utility methods used by tests above
280   // ---------------------------------------------------------------
281
282   private CollectionObject createCollectionObject(String identifier) {
283     CollectionObject collectionObject = createCollectionObject("objectNumber-" + identifier,
284         "objectName-" + identifier);
285
286     return collectionObject;
287   }
288
289   private CollectionObject createCollectionObject(String objectNumber, String objectName) {
290     CollectionObject collectionObject = new CollectionObject();
291
292     collectionObject.setObjectNumber(objectNumber);
293     collectionObject.setObjectName(objectName);
294
295     return collectionObject;
296   }
297
298   private String extractId(ClientResponse<Response> res) {
299     MultivaluedMap mvm = res.getMetadata();
300     String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
301     String[] segments = uri.split("/");
302     String id = segments[segments.length - 1];
303     verbose("id=" + id);
304     return id;
305   }
306
307   private void verbose(String msg) {
308 //    if(logger.isInfoEnabled()){
309 //      logger.debug(msg);
310 //    }
311     System.out.println(msg);
312   }
313
314   private void verbose(String msg, Object o, Class clazz) {
315     try{
316       verbose(msg);
317       JAXBContext jc = JAXBContext.newInstance(clazz);
318       Marshaller m = jc.createMarshaller();
319       m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
320           Boolean.TRUE);
321       m.marshal(o, System.out);
322     }catch(Exception e){
323       e.printStackTrace();
324     }
325   }
326
327   private void verboseMap(MultivaluedMap map) {
328     for(Object entry : map.entrySet()){
329       MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;
330       verbose("  name=" + mentry.getKey() + " value=" + mentry.getValue());
331     }
332   }
333
334   private String createIdentifier() {
335     long identifier = System.currentTimeMillis();
336     return Long.toString(identifier);
337   }
338
339   private String createNonExistentIdentifier() {
340     return Long.toString(Long.MAX_VALUE);
341   }
342   
343 }