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