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