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