]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7de255f64a59f2096061b75213680a6e4f2f297a
[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   /**
72    * Tests creation of a new CollectionObject.
73    *
74    * Expected status code: 200 OK
75    *
76    * Also expected: The 'Location' header contains the URL for the newly created object.
77    * This is required by the extractId() utility method, below.
78    *
79    * The newly-created CollectionObject is also used by other test(s)
80    * (e.g. update, delete) which follow, below.
81    */
82   @Test
83   public void createCollectionObject() {
84     String identifier = this.createIdentifier();
85
86     CollectionObject collectionObject = createCollectionObject(identifier);
87     ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
88     verbose("createCollectionObject: status = " + res.getStatus());
89     Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
90
91     // Store the ID returned from this create operation for additional tests below.
92     knownCollectionObjectId = extractId(res);
93   }
94
95   /**
96    * Tests creation of two or more new CollectionObjects.
97    *
98    * Expected status code: 200 OK
99    *
100    * The newly-created CollectionObjects are also used by other test(s)
101    * (e.g. read multiple/list) which follow, below.
102    */
103   @Test(dependsOnMethods = {"createCollectionObject"})
104   public void createCollection() {
105     for(int i = 0; i < 3; i++){
106       this.createCollectionObject();
107     }
108   }
109
110   // Failure outcomes
111   // ----------------
112
113   /**
114    * Tests creation of a CollectionObject by sending a null to the client proxy.
115    *
116    * Expected status code: (none)
117    *
118    * Expected result: NullPointerException
119    * (Make sure this is a reported exception in the called class.)
120    */
121 /*
122   @Test(dependsOnMethods = {"createCollectionObject"}, expectedExceptions = NullPointerException.class)
123   public void createNullCollectionObject() {
124     ClientResponse<Response> res = collectionObjectClient.createCollectionObject(null);
125   }
126 */
127                 
128   /**
129    * Tests creation of a CollectionObject by sending data in the wrong format
130    * (i.e. any format that doesn't match the CollectionObject schema) in the
131    * entity body of the request.
132    *
133    * Expected status code: 400 Bad Request
134    */
135 /*
136   @Test(dependsOnMethods = {"createCollectionObject"})
137   public void createCollectionObjectWithWrongDataFormat() {
138     // Currently only a stub
139   }
140 */
141   
142   /**
143    * Tests creation of a duplicate CollectionObject, whose unique resource identifier
144    * duplicates that of an existing CollectionObject.
145    * 
146    * Expected status code: 409 Conflict
147    */
148 /*
149   @Test(dependsOnMethods = {"createCollectionObject"})
150   public void createDuplicateCollectionObject() {
151     CollectionObject collectionObject = createCollectionObject(knownCollectionObjectId);
152     ClientResponse<Response> res = 
153       collectionObjectClient.createCollectionObject(collectionObject);
154     verbose("createDuplicateCollectionObject: status = " + res.getStatus());
155     Assert.assertEquals(res.getStatus(), Response.Status.CONFLICT.getStatusCode());
156   }
157 */
158
159   // ---------------------------------------------------------------
160   // CRUD tests : READ tests
161   // ---------------------------------------------------------------
162
163   // Success outcomes
164   // ----------------
165   
166   /**
167    * Tests reading (i.e. retrieval) of a CollectionObject.
168    *
169    * Expected status code: 200 OK
170    */
171 /*
172   @Test(dependsOnMethods = {"createCollectionObject"})
173   public void getCollectionObject() {
174     ClientResponse<CollectionObject> res = 
175       collectionObjectClient.getCollectionObject(knownCollectionObjectId);
176     verbose("getCollectionObject: status = " + res.getStatus());
177     Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
178   }
179 */
180
181   // Failure outcomes
182   // ----------------
183
184   /**
185    * Tests reading (i.e. retrieval) of a non-existent CollectionObject,
186    * whose resource identifier does not exist at the specified URL.
187    *
188    * Expected status code: 404 Not Found
189    */
190 /*
191   @Test(dependsOnMethods = {"createCollectionObject"})
192   public void getNonExistentCollectionObject() {
193     ClientResponse<CollectionObject> res = 
194       collectionObjectClient.getCollectionObject(NON_EXISTENT_ID);
195     verbose("getNonExistentCollectionObject: status = " + res.getStatus());
196     Assert.assertEquals(res.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
197   }
198 */  
199
200   // ---------------------------------------------------------------
201   // CRUD tests : READ (list, or multiple) tests
202   // ---------------------------------------------------------------
203
204   // Success outcomes
205   // ----------------
206
207   /**
208    * Tests reading (i.e. retrieval) of a list of multiple CollectionObjects.
209    *
210    * Expected status code: 200 OK
211    *
212    * Also expected: The entity body in the response contains
213    * a representation of the list of CollectionObjects.
214    */
215   @Test(dependsOnMethods = {"createCollection"})
216   public void getCollectionObjectList() {
217     // The resource method is expected to return at least an empty list
218     ClientResponse<CollectionObjectList> res = collectionObjectClient.getCollectionObjectList();
219     CollectionObjectList coList = res.getEntity();
220     verbose("getCollectionObjectList: status = " + res.getStatus());
221     Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
222
223     List<CollectionObjectList.CollectionObjectListItem> coItemList =
224       coList.getCollectionObjectListItem();
225     int i = 0;
226     for(CollectionObjectList.CollectionObjectListItem pli : coItemList){
227       verbose("getCollectionObjectList: list-item[" + i + "] csid=" + pli.getCsid());
228       verbose("getCollectionObjectList: list-item[" + i + "] objectNumber=" + pli.getObjectNumber());
229       verbose("getCollectionObjectList: list-item[" + i + "] URI=" + pli.getUri());
230       i++;
231     }
232   }
233
234   /**
235    * Tests reading (i.e. retrieval) of a list of multiple CollectionObjects
236    * when the contents of the list are expected to be empty.
237    *
238    * Expected status code: 200 OK
239    * (Note: *not* 204 No Content)
240    *
241    * Also expected: The entity body in the response contains
242    * a representation of an empty list of CollectionObjects.
243    */
244 /*
245   @Test(dependsOnMethods = {"createCollection"})
246   public void getCollectionObjectEmptyList() {
247   }
248 */
249
250   // Failure outcomes
251   // ----------------
252   
253   // None known at present.
254   
255
256   // ---------------------------------------------------------------
257   // CRUD tests : UPDATE tests
258   // ---------------------------------------------------------------
259
260   // Success outcomes
261   // ----------------
262
263   /**
264    * Tests updating the content of a CollectionObject.
265    *
266    * Expected status code: 200 OK
267    *
268    * Also expected: The entity body in the response contains
269    * a representation of the updated CollectionObject.
270    */
271   @Test(dependsOnMethods = {"createCollectionObject"})
272   public void updateCollectionObject() {
273     ClientResponse<CollectionObject> res = 
274       collectionObjectClient.getCollectionObject(knownCollectionObjectId);
275     verbose("getCollectionObject: status = " + res.getStatus());
276     Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
277     CollectionObject collectionObject = res.getEntity();
278     verbose("Got CollectionObject to update with ID: " + knownCollectionObjectId,
279         collectionObject, CollectionObject.class);
280
281     //collectionObject.setCsid("updated-" + knownCollectionObjectId);
282     collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
283     collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
284
285     // make call to update service
286     res = 
287       collectionObjectClient.updateCollectionObject(knownCollectionObjectId, collectionObject);
288     verbose("updateCollectionObject: status = " + res.getStatus());
289     Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
290     
291     // check the response
292     CollectionObject updatedCollectionObject = res.getEntity();
293     Assert.assertEquals(updatedCollectionObject.getObjectName(), 
294       collectionObject.getObjectName());
295     verbose("updateCollectionObject: ", updatedCollectionObject, CollectionObject.class);
296   }
297
298   // Failure outcomes
299   // ----------------
300
301   /**
302    * Tests updating the content of a non-existent CollectionObject, whose
303    * resource identifier does not exist.
304    *
305    * Expected status code: 404 Not Found
306    */
307   @Test(dependsOnMethods = {"updateCollectionObject"})
308   public void updateNonExistentCollectionObject() {
309     // Note: The ID used in this call may not be relevant, only the ID used
310     // in updateCollectionObject(), below.
311     CollectionObject collectionObject = createCollectionObject(NON_EXISTENT_ID);
312     // make call to update service
313     ClientResponse<CollectionObject> res =
314       collectionObjectClient.updateCollectionObject(NON_EXISTENT_ID, collectionObject);
315     verbose("createCollectionObject: status = " + res.getStatus());
316     Assert.assertEquals(res.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
317   }
318
319
320   // ---------------------------------------------------------------
321   // CRUD tests : DELETE tests
322   // ---------------------------------------------------------------
323
324   // Success outcomes
325   // ----------------
326
327   /**
328    * Tests deleting a CollectionObject.
329    *
330    * Expected status code: 200 OK
331    */
332   @Test(dependsOnMethods = {"createCollectionObject"})
333   public void deleteCollectionObject() {
334     verbose("Calling deleteCollectionObject:" + knownCollectionObjectId);
335     ClientResponse<Response> res = collectionObjectClient.deleteCollectionObject(knownCollectionObjectId);
336     verbose("deleteCollectionObject: status = " + res.getStatus());
337     Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
338   }
339
340   // Failure outcomes
341   // ----------------
342
343   /**
344    * Tests deleting a non-existent CollectionObject, whose
345    * resource identifier does not exist at the specified URL.
346    *
347    * Expected status code: 404 Not Found
348    */
349   @Test(dependsOnMethods = {"deleteCollectionObject"})
350   public void deleteNonExistentCollectionObject() {
351     verbose("Calling deleteCollectionObject:" + NON_EXISTENT_ID);
352     ClientResponse<Response> res =
353       collectionObjectClient.deleteCollectionObject(NON_EXISTENT_ID);
354     verbose("deleteCollectionObject: status = " + res.getStatus());
355     Assert.assertEquals(res.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
356   }
357
358
359   // ---------------------------------------------------------------
360   // Utility methods used by tests above
361   // ---------------------------------------------------------------
362
363   private CollectionObject createCollectionObject(String identifier) {
364     CollectionObject collectionObject = createCollectionObject("objectNumber-" + identifier,
365         "objectName-" + identifier);
366
367     return collectionObject;
368   }
369
370   private CollectionObject createCollectionObject(String objectNumber, String objectName) {
371     CollectionObject collectionObject = new CollectionObject();
372
373     collectionObject.setObjectNumber(objectNumber);
374     collectionObject.setObjectName(objectName);
375
376     return collectionObject;
377   }
378
379   private String extractId(ClientResponse<Response> res) {
380     MultivaluedMap mvm = res.getMetadata();
381     String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
382     verbose("extractId:uri=" + uri);
383     String[] segments = uri.split("/");
384     String id = segments[segments.length - 1];
385     verbose("id=" + id);
386     return id;
387   }
388
389   private void verbose(String msg) {
390 //    if(logger.isInfoEnabled()){
391 //      logger.debug(msg);
392 //    }
393     System.out.println(msg);
394   }
395
396   private void verbose(String msg, Object o, Class clazz) {
397     try{
398       verbose(msg);
399       JAXBContext jc = JAXBContext.newInstance(clazz);
400       Marshaller m = jc.createMarshaller();
401       m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
402           Boolean.TRUE);
403       m.marshal(o, System.out);
404     }catch(Exception e){
405       e.printStackTrace();
406     }
407   }
408
409   private void verboseMap(MultivaluedMap map) {
410     for(Object entry : map.entrySet()){
411       MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;
412       verbose("  name=" + mentry.getKey() + " value=" + mentry.getValue());
413     }
414   }
415
416   private String createIdentifier() {
417     long identifier = System.currentTimeMillis();
418     return Long.toString(identifier);
419   }
420
421   private String createNonExistentIdentifier() {
422     return Long.toString(Long.MAX_VALUE);
423   }
424   
425 }