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