]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
19746a564cead89d65b9b2893d2153ddb6d04941
[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 package org.collectionspace.services.client.test;
24
25 import java.util.List;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28
29 import org.collectionspace.services.client.CollectionObjectClient;
30 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
31 import org.collectionspace.services.collectionobject.domain.naturalhistory.CollectionObjectNaturalhistory;
32 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
33
34 import org.jboss.resteasy.client.ClientResponse;
35
36 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
37 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
38 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
39 import org.testng.Assert;
40 import org.testng.annotations.Test;
41
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * CollectionObjectServiceTest, carries out tests against a
47  * deployed and running CollectionObject Service.
48  * 
49  * $LastChangedRevision$
50  * $LastChangedDate$
51  */
52 public class CollectionObjectServiceTest extends AbstractServiceTest {
53
54     private final Logger logger =
55         LoggerFactory.getLogger(CollectionObjectServiceTest.class);
56
57     // Instance variables specific to this test.
58     private CollectionObjectClient client = new CollectionObjectClient();
59     private String knownResourceId = null; 
60     
61     /*
62      * This method is called only by the parent class, AbstractServiceTest
63      */
64     @Override
65     public String getServicePathComponent() {
66         return client.getServicePathComponent();
67     }
68
69     // ---------------------------------------------------------------
70     // CRUD tests : CREATE tests
71     // ---------------------------------------------------------------
72
73     // Success outcomes
74     @Override
75     @Test
76     public void create() throws Exception {
77
78         // Perform setup, such as initializing the type of service request
79         // (e.g. CREATE, DELETE), its valid and expected status codes, and
80         // its associated HTTP method name (e.g. POST, DELETE).
81         setupCreate();
82
83         // Submit the request to the service and store the response.
84         String identifier = createIdentifier();
85
86         MultipartOutput multipart = createCollectionObjectInstance(client.getCommonPartName(), identifier);
87         ClientResponse<Response> res = client.create(multipart);
88
89         int statusCode = res.getStatus();
90
91         // Check the status code of the response: does it match
92         // the expected response(s)?
93         //
94         // Specifically:
95         // Does it fall within the set of valid status codes?
96         // Does it exactly match the expected status code?
97         if(logger.isDebugEnabled()){
98             logger.debug("create: status = " + statusCode);
99         }
100         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
101                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
102         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
103
104         // Store the ID returned from this create operation
105         // for additional tests below.
106         knownResourceId = extractId(res);
107         if(logger.isDebugEnabled()){
108             logger.debug("create: knownResourceId=" + knownResourceId);
109         }
110     }
111
112         /* (non-Javadoc)
113          * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
114          */
115     @Override
116     @Test(dependsOnMethods = {"create"})
117     public void createList() throws Exception {
118         for(int i = 0; i < 3; i++){
119             create();
120         }
121     }
122
123     // Failure outcomes
124
125     // Placeholders until the three tests below can be uncommented.
126     // See Issue CSPACE-401.
127     @Override
128     public void createWithEmptyEntityBody() throws Exception {}
129     @Override
130     public void createWithMalformedXml() throws Exception {}
131     @Override
132     public void createWithWrongXmlSchema() throws Exception {}
133
134
135 /*
136     @Override
137     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
138     public void createWithEmptyEntityBody() throwsException {
139     
140         // Perform setup.
141         setupCreateWithEmptyEntityBody();
142
143         // Submit the request to the service and store the response.
144         String method = REQUEST_TYPE.httpMethodName();
145         String url = getServiceRootURL();
146         String mediaType = MediaType.APPLICATION_XML;
147         final String entity = "";
148         int statusCode = submitRequest(method, url, mediaType, entity);
149         
150         // Check the status code of the response: does it match
151         // the expected response(s)?
152         if(logger.isDebugEnabled()){
153             logger.debug("createWithEmptyEntityBody url=" + url +
154                 " status=" + statusCode);
155         }
156         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
157             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
158         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
159     }
160
161     @Override
162     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
163     public void createWithMalformedXml() throws Exception {
164     
165         // Perform setup.
166         setupCreateWithMalformedXml();
167
168         // Submit the request to the service and store the response.
169         String method = REQUEST_TYPE.httpMethodName();
170         String url = getServiceRootURL();
171         String mediaType = MediaType.APPLICATION_XML;
172         final String entity = MALFORMED_XML_DATA; // Constant from base class.
173         int statusCode = submitRequest(method, url, mediaType, entity);
174         
175         // Check the status code of the response: does it match
176         // the expected response(s)?
177         if(logger.isDebugEnabled()){
178             logger.debug("createWithMalformedXml url=" + url +
179                 " status=" + statusCode);
180         }
181         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
182             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
183         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
184     }
185
186     @Override
187     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
188     public void createWithWrongXmlSchema() throws Exception {
189     
190         // Perform setup.
191         setupCreateWithWrongXmlSchema();
192      
193         // Submit the request to the service and store the response.
194         String method = REQUEST_TYPE.httpMethodName();
195         String url = getServiceRootURL();
196         String mediaType = MediaType.APPLICATION_XML;
197         final String entity = WRONG_XML_SCHEMA_DATA;
198         int statusCode = submitRequest(method, url, mediaType, entity);
199         
200         // Check the status code of the response: does it match
201         // the expected response(s)?
202         if(logger.isDebugEnabled()){
203             logger.debug("createWithWrongXmlSchema url=" + url +
204                 " status=" + statusCode);
205         }
206         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
207             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
208         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
209     }
210 */
211
212     // ---------------------------------------------------------------
213     // CRUD tests : READ tests
214     // ---------------------------------------------------------------
215
216     // Success outcomes
217     @Override
218     @Test(dependsOnMethods = {"create"})
219     public void read() throws Exception {
220
221         // Perform setup.
222         setupRead();
223
224         // Submit the request to the service and store the response.
225         ClientResponse<MultipartInput> res = client.read(knownResourceId);
226         int statusCode = res.getStatus();
227
228         // Check the status code of the response: does it match
229         // the expected response(s)?
230         if(logger.isDebugEnabled()){
231             logger.debug("read: status = " + statusCode);
232         }
233         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
234                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
235         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
236         
237         MultipartInput input = (MultipartInput) res.getEntity();
238         CollectionobjectsCommon collectionObject =
239             (CollectionobjectsCommon) extractPart(input,
240             client.getCommonPartName(), CollectionobjectsCommon.class);
241         Assert.assertNotNull(collectionObject);
242     }
243     
244     // Failure outcomes
245
246     @Override
247     @Test(dependsOnMethods = {"read"})
248     public void readNonExistent() throws Exception {
249
250         // Perform setup.
251         setupReadNonExistent();
252
253         // Submit the request to the service and store the response.
254         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
255         int statusCode = res.getStatus();
256
257         // Check the status code of the response: does it match
258         // the expected response(s)?
259         if(logger.isDebugEnabled()){
260             logger.debug("readNonExistent: status = " + res.getStatus());
261         }
262         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
263                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
264         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
265     }
266
267     // ---------------------------------------------------------------
268     // CRUD tests : READ_LIST tests
269     // ---------------------------------------------------------------
270     // Success outcomes
271     @Override
272     @Test(dependsOnMethods = {"createList", "read"})
273     public void readList() throws Exception {
274         // Perform setup.
275         setupReadList();
276
277         // Submit the request to the service and store the response.
278         ClientResponse<CollectionobjectsCommonList> res = client.readList();
279         CollectionobjectsCommonList list = res.getEntity();
280
281         int statusCode = res.getStatus();
282
283         // Check the status code of the response: does it match
284         // the expected response(s)?
285         if(logger.isDebugEnabled()){
286             logger.debug("readList: status = " + res.getStatus());
287         }
288         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
289                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
290         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
291
292         // Optionally output additional data about list members for debugging.
293         boolean iterateThroughList = false;
294         if (iterateThroughList && logger.isDebugEnabled()) {
295             List<CollectionobjectsCommonList.CollectionObjectListItem> items =
296                 list.getCollectionObjectListItem();
297             int i = 0;
298
299             for(CollectionobjectsCommonList.CollectionObjectListItem item : items){
300                 logger.debug("readList: list-item[" + i + "] csid=" +
301                     item.getCsid());
302                 logger.debug("readList: list-item[" + i + "] objectNumber=" +
303                     item.getObjectNumber());
304                 logger.debug("readList: list-item[" + i + "] URI=" +
305                     item.getUri());
306                 i++;
307
308             }
309         }
310     }
311
312     // Failure outcomes
313     // None at present.
314     // ---------------------------------------------------------------
315     // CRUD tests : UPDATE tests
316     // ---------------------------------------------------------------
317     // Success outcomes
318     @Override
319     @Test(dependsOnMethods = {"read"})
320     public void update() throws Exception {
321
322         // Perform setup.
323         setupUpdate();
324
325         ClientResponse<MultipartInput> res =
326                 client.read(knownResourceId);
327         if(logger.isDebugEnabled()){
328             logger.debug("update: read status = " + res.getStatus());
329         }
330         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
331
332         if(logger.isDebugEnabled()){
333             logger.debug("got object to update with ID: " + knownResourceId);
334         }
335         MultipartInput input = (MultipartInput) res.getEntity();
336         CollectionobjectsCommon collectionObject =
337             (CollectionobjectsCommon) extractPart(input,
338                 client.getCommonPartName(), CollectionobjectsCommon.class);
339         Assert.assertNotNull(collectionObject);
340
341         // Update the content of this resource.
342         collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
343         collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
344         if(logger.isDebugEnabled()){
345             verbose("updated object", collectionObject,
346                 CollectionobjectsCommon.class);
347         }
348         // Submit the request to the service and store the response.
349         MultipartOutput output = new MultipartOutput();
350         OutputPart commonPart = output.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
351         commonPart.getHeaders().add("label", client.getCommonPartName());
352
353         res = client.update(knownResourceId, output);
354         int statusCode = res.getStatus();
355         // Check the status code of the response: does it match the expected response(s)?
356         if(logger.isDebugEnabled()){
357             logger.debug("update: status = " + res.getStatus());
358         }
359         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
360                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
361         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
362
363
364         input = (MultipartInput) res.getEntity();
365         CollectionobjectsCommon updatedCollectionObject =
366                 (CollectionobjectsCommon) extractPart(input,
367                         client.getCommonPartName(), CollectionobjectsCommon.class);
368         Assert.assertNotNull(updatedCollectionObject);
369
370         Assert.assertEquals(updatedCollectionObject.getObjectName(),
371                 collectionObject.getObjectName(),
372                 "Data in updated object did not match submitted data.");
373
374     }
375     
376     // Failure outcomes
377
378     // Placeholders until the three tests below can be uncommented.
379     // See Issue CSPACE-401.
380     @Override
381     public void updateWithEmptyEntityBody() throws Exception {}
382     @Override
383     public void updateWithMalformedXml() throws Exception {}
384     @Override
385     public void updateWithWrongXmlSchema() throws Exception {}
386
387 /*
388     @Override
389
390     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
391     public void updateWithEmptyEntityBody() throws Exception {
392     
393         // Perform setup.
394         setupUpdateWithEmptyEntityBody();
395
396         // Submit the request to the service and store the response.
397         String method = REQUEST_TYPE.httpMethodName();
398         String url = getResourceURL(knownResourceId);
399         String mediaType = MediaType.APPLICATION_XML;
400         final String entity = "";
401         int statusCode = submitRequest(method, url, mediaType, entity);
402         
403         // Check the status code of the response: does it match
404         // the expected response(s)?
405         if(logger.isDebugEnabled()){
406             logger.debug("updateWithEmptyEntityBody url=" + url +
407                 " status=" + statusCode);
408         }
409         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
410             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
411         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
412     }
413
414     @Override
415     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
416     public void updateWithMalformedXml() throws Exception {
417
418         // Perform setup.
419         setupUpdateWithMalformedXml();
420
421         // Submit the request to the service and store the response.
422         String method = REQUEST_TYPE.httpMethodName();
423         String url = getResourceURL(knownResourceId);
424         final String entity = MALFORMED_XML_DATA;
425         String mediaType = MediaType.APPLICATION_XML;
426         int statusCode = submitRequest(method, url, mediaType, entity);
427         
428         // Check the status code of the response: does it match
429         // the expected response(s)?
430         if(logger.isDebugEnabled()){
431             logger.debug("updateWithMalformedXml: url=" + url +
432                 " status=" + statusCode);
433         }
434         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
435             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
436         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
437     }
438
439     @Override
440     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
441     public void updateWithWrongXmlSchema() throws Exception {
442     
443         // Perform setup.
444         setupUpdateWithWrongXmlSchema();
445         
446         // Submit the request to the service and store the response.
447         String method = REQUEST_TYPE.httpMethodName();
448         String url = getResourceURL(knownResourceId);
449         String mediaType = MediaType.APPLICATION_XML;
450         final String entity = WRONG_XML_SCHEMA_DATA;
451         int statusCode = submitRequest(method, url, mediaType, entity);
452         
453         // Check the status code of the response: does it match
454         // the expected response(s)?
455         if(logger.isDebugEnabled()){
456             logger.debug("updateWithWrongXmlSchema: url=" + url +
457                 " status=" + statusCode);
458         }
459         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
460             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
461         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
462     }
463 */
464
465     @Override
466     @Test(dependsOnMethods = {"update", "testSubmitRequest"})
467     public void updateNonExistent() throws Exception {
468
469         // Perform setup.
470         setupUpdateNonExistent();
471
472         // Submit the request to the service and store the response.
473         // Note: The ID used in this 'create' call may be arbitrary.
474
475         // The only relevant ID may be the one used in updateCollectionObject(), below.
476         MultipartOutput multipart =
477             createCollectionObjectInstance(client.getCommonPartName(),
478                 NON_EXISTENT_ID);
479         ClientResponse<MultipartInput> res =
480             client.update(NON_EXISTENT_ID, multipart);
481
482         int statusCode = res.getStatus();
483
484         // Check the status code of the response: does it match
485         // the expected response(s)?
486         if(logger.isDebugEnabled()){
487             logger.debug("updateNonExistent: status = " + res.getStatus());
488         }
489         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
490                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
491         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
492     }
493
494     // ---------------------------------------------------------------
495     // CRUD tests : DELETE tests
496     // ---------------------------------------------------------------
497     // Success outcomes
498     @Override
499     @Test(dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
500     public void delete() throws Exception {
501
502         // Perform setup.
503         setupDelete();
504
505         // Submit the request to the service and store the response.
506         ClientResponse<Response> res = client.delete(knownResourceId);
507         int statusCode = res.getStatus();
508
509         // Check the status code of the response: does it match
510         // the expected response(s)?
511         if(logger.isDebugEnabled()){
512             logger.debug("delete: status = " + res.getStatus());
513         }
514         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
515                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
516         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
517     }
518
519     // Failure outcomes
520     @Override
521     @Test(dependsOnMethods = {"delete"})
522     public void deleteNonExistent() throws Exception {
523
524         // Perform setup.
525         setupDeleteNonExistent();
526
527         // Submit the request to the service and store the response.
528         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
529         int statusCode = res.getStatus();
530
531         // Check the status code of the response: does it match
532         // the expected response(s)?
533         if(logger.isDebugEnabled()){
534             logger.debug("deleteNonExistent: status = " + res.getStatus());
535         }
536         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
537                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
538         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
539     }
540
541     // ---------------------------------------------------------------
542     // Utility tests : tests of code used in tests above
543     // ---------------------------------------------------------------
544     /**
545      * Tests the code for manually submitting data that is used by several
546      * of the methods above.
547      */
548     @Test(dependsOnMethods = {"create", "read"})
549     public void testSubmitRequest() throws Exception {
550
551         // Expected status code: 200 OK
552         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
553
554         // Submit the request to the service and store the response.
555         String method = ServiceRequestType.READ.httpMethodName();
556         String url = getResourceURL(knownResourceId);
557         int statusCode = submitRequest(method, url);
558
559         // Check the status code of the response: does it match
560         // the expected response(s)?
561         if(logger.isDebugEnabled()){
562             logger.debug("testSubmitRequest: url=" + url +
563                 " status=" + statusCode);
564         }
565         Assert.assertEquals(statusCode, EXPECTED_STATUS);
566
567     }           
568
569     // ---------------------------------------------------------------
570     // Utility methods used by tests above
571     // ---------------------------------------------------------------
572
573     private MultipartOutput createCollectionObjectInstance(String commonPartName,
574         String identifier) {
575         return createCollectionObjectInstance(commonPartName,
576             "objectNumber-" + identifier,
577             "objectName-" + identifier);
578     }
579
580     private MultipartOutput createCollectionObjectInstance(String commonPartName,
581         String objectNumber, String objectName) {
582         CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
583
584         collectionObject.setObjectNumber(objectNumber);
585         collectionObject.setObjectName(objectName);
586         MultipartOutput multipart = new MultipartOutput();
587         OutputPart commonPart = multipart.addPart(collectionObject,
588             MediaType.APPLICATION_XML_TYPE);
589         commonPart.getHeaders().add("label", commonPartName);
590
591         if(logger.isDebugEnabled()){
592             verbose("to be created, collectionobject common ",
593                collectionObject, CollectionobjectsCommon.class);
594         }
595
596         CollectionObjectNaturalhistory conh = new CollectionObjectNaturalhistory();
597         conh.setNhString("test-string");
598         conh.setNhInt(999);
599         conh.setNhLong(9999);
600         OutputPart nhPart = multipart.addPart(conh, MediaType.APPLICATION_XML_TYPE);
601         nhPart.getHeaders().add("label", getNHPartName());
602
603         if(logger.isDebugEnabled()){
604             verbose("to be created, collectionobject nhistory",
605                 conh, CollectionObjectNaturalhistory.class);
606         }
607         return multipart;
608
609     }
610
611     private String getNHPartName() {
612         return "collectionobjects-naturalhistory";
613     }
614 }