]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1f947f34e366789af979d7f0d9201032de6f981a
[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.AbstractCommonListUtils;
30 import org.collectionspace.services.client.AcquisitionClient;
31 import org.collectionspace.services.client.CollectionSpaceClient;
32 import org.collectionspace.services.client.PayloadOutputPart;
33 import org.collectionspace.services.client.PoxPayloadIn;
34 import org.collectionspace.services.client.PoxPayloadOut;
35 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
36 import org.collectionspace.services.jaxb.AbstractCommonList;
37
38 import org.collectionspace.services.acquisition.AcquisitionsCommon;
39 import org.collectionspace.services.acquisition.AcquisitionDateGroupList;
40 import org.collectionspace.services.acquisition.AcquisitionSourceList;
41 import org.collectionspace.services.acquisition.OwnerList;
42 import org.jboss.resteasy.client.ClientResponse;
43 import org.collectionspace.services.acquisition.StructuredDateGroup;
44
45 import org.testng.Assert;
46 import org.testng.annotations.Test;
47
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 /**
52  * AcquisitionServiceTest, carries out tests against a
53  * deployed and running Acquisition Service.
54  * 
55  * $LastChangedRevision: 621 $
56  * $LastChangedDate: 2009-09-02 16:49:01 -0700 (Wed, 02 Sep 2009) $
57  */
58 public class AcquisitionServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, AcquisitionsCommon> {
59
60     /** The logger. */
61     private final String CLASS_NAME = AcquisitionServiceTest.class.getName();
62     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
63
64     // Instance variables specific to this test.
65     private final static String CURRENT_DATE_UTC =
66             GregorianCalendarDateTimeUtils.timestampUTC();
67
68     @Override
69     public String getServicePathComponent() {
70         return AcquisitionClient.SERVICE_PATH_COMPONENT;
71     }
72
73     @Override
74     protected String getServiceName() {
75         return AcquisitionClient.SERVICE_NAME;
76     }
77     
78     /* (non-Javadoc)
79      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
80      */
81     @Override
82     protected CollectionSpaceClient getClientInstance() {
83         return new AcquisitionClient();
84     }
85     
86     // ---------------------------------------------------------------
87     // CRUD tests : CREATE tests
88     // ---------------------------------------------------------------
89     // Success outcomes
90     /* (non-Javadoc)
91      * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
92      */
93     @Override
94 //    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
95     public void create(String testName) throws Exception {
96         // Perform setup, such as initializing the type of service request
97         // (e.g. CREATE, DELETE), its valid and expected status codes, and
98         // its associated HTTP method name (e.g. POST, DELETE).
99         setupCreate();
100
101         // Submit the request to the service and store the response.
102         String identifier = createIdentifier();
103
104         AcquisitionClient client = new AcquisitionClient();
105         PoxPayloadOut multipart = createAcquisitionInstance(identifier);
106         Response res = client.create(multipart);
107
108         int statusCode = res.getStatus();
109
110         // Check the status code of the response: does it match
111         // the expected response(s)?
112         //
113         // Specifically:
114         // Does it fall within the set of valid status codes?
115         // Does it exactly match the expected status code?
116         if(logger.isDebugEnabled()){
117             logger.debug(testName + ": status = " + statusCode);
118         }
119         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
120                 invalidStatusCodeMessage(testRequestType, statusCode));
121         Assert.assertEquals(statusCode, testExpectedStatusCode);
122
123         // Store the ID returned from the first resource created
124         // for additional tests below.
125         if (knownResourceId == null){
126             knownResourceId = extractId(res);
127             if (logger.isDebugEnabled()) {
128                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
129             }
130         }
131         
132         // Store the IDs from every resource created by tests,
133         // so they can be deleted after tests have been run.
134         allResourceIdsCreated.add(extractId(res));
135     }
136
137     /* (non-Javadoc)
138      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
139      */
140     @Override
141 //    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
142 //       dependsOnMethods = {"create"})
143     public void createList(String testName) throws Exception {
144         for(int i = 0; i < 3; i++){
145             create(testName);
146         }
147     }
148
149     /*
150     * Tests to diagnose and fix CSPACE-2578.
151     *
152     * This is a bug identified in release 1.0 alpha, after first implementing an
153     * Acquisition Funding repeatable group of fields, in which record creation
154     * fails if there is whitespace (or more generally, a text node) between
155     * the acquisitionFunding container element and its first child field.
156     */
157
158     // Verify that record creation occurs successfully when there is NO whitespace
159     // between the acquisitionFunding tag and its first child element tag
160     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
161         dependsOnMethods = {"CRUDTests"}, groups = {"cspace2578group"})
162     public void createFromXmlNoWhitespaceAfterRepeatableGroupTag(String testName) throws Exception {
163         String testDataDir = System.getProperty("test-data.fileName");
164         String newId =
165             createFromXmlFile(testName, testDataDir + "/cspace-2578-no-whitespace.xml", false);
166         testSubmitRequest(newId);
167     }
168
169     // Verify that record creation occurs successfully when there is whitespace
170     // between the acquisitionFunding tag and its first child element tag
171
172     // FIXME: This test currently fails.  @Test annotation is currently commented
173     // out for check-in, to prevent service tests from failing.  Can uncomment to test
174     // fixes, and also after the issue is resolved, to help detect any regressions.
175
176     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
177         dependsOnMethods = {"CRUDTests"}, groups = {"cspace2578group"})
178     public void createFromXmlWhitespaceAfterRepeatableGroupTag(String testName) throws Exception {
179         String testDataDir = System.getProperty("test-data.fileName");
180         String newId =
181             createFromXmlFile(testName, testDataDir + "/cspace-2578-whitespace.xml", false);
182         AcquisitionsCommon acquisition = readAcquisitionCommonPart(newId);
183         testSubmitRequest(newId);
184     }
185
186     // Failure outcomes
187
188     // Placeholders until the three tests below can be uncommented.
189     // See Issue CSPACE-401.
190     /* (non-Javadoc)
191      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
192      */
193     @Override
194     public void createWithEmptyEntityBody(String testName) throws Exception {
195         //Should this really be empty?
196     }
197
198     /* (non-Javadoc)
199      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
200      */
201     @Override
202     public void createWithMalformedXml(String testName) throws Exception {
203         //Should this really be empty?
204     }
205
206     /* (non-Javadoc)
207      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
208      */
209     @Override
210     public void createWithWrongXmlSchema(String testName) throws Exception {
211         //Should this really be empty?
212     }
213
214     /*
215     @Override
216     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
217         dependsOnMethods = {"create", "testSubmitRequest"})
218     public void createWithMalformedXml(String testName) throws Exception {
219     
220         if (logger.isDebugEnabled()) {
221             logger.debug(testBanner(testName, CLASS_NAME));
222         };
223             
224         // Perform setup.
225         setupCreateWithMalformedXml();
226
227         // Submit the request to the service and store the response.
228         String method = REQUEST_TYPE.httpMethodName();
229         String url = getServiceRootURL();
230         final String entity = MALFORMED_XML_DATA; // Constant from base class.
231         int statusCode = submitRequest(method, url, entity);
232
233         // Check the status code of the response: does it match
234         // the expected response(s)?
235         if(logger.isDebugEnabled()){
236             logger.debug(testName + ": url=" + url +
237                 " status=" + statusCode);
238          }
239         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
240         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
241         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
242     }
243
244     @Override
245     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
246         dependsOnMethods = {"create", "testSubmitRequest"})
247     public void createWithWrongXmlSchema(String testName) throws Exception {
248     
249         if (logger.isDebugEnabled()) {
250             logger.debug(testBanner(testName, CLASS_NAME));
251         };
252         
253         // Perform setup.
254         setupCreateWithWrongXmlSchema();
255
256         // Submit the request to the service and store the response.
257         String method = REQUEST_TYPE.httpMethodName();
258         String url = getServiceRootURL();
259         final String entity = WRONG_XML_SCHEMA_DATA;
260         int statusCode = submitRequest(method, url, entity);
261
262         // Check the status code of the response: does it match
263         // the expected response(s)?
264         if(logger.isDebugEnabled()){
265             logger.debug(testName + ": url=" + url +
266                 " status=" + statusCode);
267          }
268         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
269         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
270         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
271     }
272      */
273
274     // ---------------------------------------------------------------
275     // CRUD tests : READ tests
276     // ---------------------------------------------------------------
277     // Success outcomes
278     /* (non-Javadoc)
279      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
280      */
281     @Override
282 //    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
283 //        dependsOnMethods = {"create"})
284     public void read(String testName) throws Exception {
285         // Perform setup.
286         setupRead();
287
288         AcquisitionClient client = new AcquisitionClient();
289
290         // Submit the request to the service and store the response.
291         ClientResponse<String> res = client.read(knownResourceId);
292         int statusCode = res.getStatus();
293
294         // Check the status code of the response: does it match
295         // the expected response(s)?
296         if(logger.isDebugEnabled()){
297             logger.debug(testName + ": status = " + statusCode);
298         }
299         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
300                 invalidStatusCodeMessage(testRequestType, statusCode));
301         Assert.assertEquals(statusCode, testExpectedStatusCode);
302
303         PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
304         AcquisitionsCommon acquisitionObject = (AcquisitionsCommon) extractPart(input,
305                 client.getCommonPartName(), AcquisitionsCommon.class);
306         Assert.assertNotNull(acquisitionObject);
307
308         // Verify the number and contents of values in repeatable fields,
309         // as created in the instance record used for testing.
310         List<String> acqSources =
311                 acquisitionObject.getAcquisitionSources().getAcquisitionSource();
312         Assert.assertTrue(acqSources.size() > 0);
313         Assert.assertNotNull(acqSources.get(0));
314
315         List<StructuredDateGroup> acqDates = acquisitionObject.getAcquisitionDateGroupList().getAcquisitionDateGroup();
316         Assert.assertTrue(acqDates.size() > 0);
317         Assert.assertNotNull(acqDates.get(0));
318
319         List<String> owners =
320                 acquisitionObject.getOwners().getOwner();
321         Assert.assertTrue(owners.size() > 0);
322         Assert.assertNotNull(owners.get(0));
323     }
324
325     // Failure outcomes
326     /* (non-Javadoc)
327      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
328      */
329     @Override
330 //    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
331 //        dependsOnMethods = {"read"})
332     public void readNonExistent(String testName) throws Exception {
333         // Perform setup.
334         setupReadNonExistent();
335
336         // Submit the request to the service and store the response.
337         AcquisitionClient client = new AcquisitionClient();
338         ClientResponse<String> res = client.read(NON_EXISTENT_ID);
339         int statusCode = res.getStatus();
340
341         // Check the status code of the response: does it match
342         // the expected response(s)?
343         if(logger.isDebugEnabled()){
344             logger.debug(testName + ": status = " + statusCode);
345         }
346         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
347                 invalidStatusCodeMessage(testRequestType, statusCode));
348         Assert.assertEquals(statusCode, testExpectedStatusCode);
349     }
350
351     // ---------------------------------------------------------------
352     // CRUD tests : READ_LIST tests
353     // ---------------------------------------------------------------
354     // Success outcomes
355     /* (non-Javadoc)
356      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
357      */
358     @Override
359 //    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
360 //        dependsOnMethods = {"createList", "read"})
361     public void readList(String testName) throws Exception {
362         // Perform setup.
363         setupReadList();
364
365         // Submit the request to the service and store the response.
366         AcquisitionClient client = new AcquisitionClient();
367         ClientResponse<AbstractCommonList> res = client.readList();
368         try {
369                 // Check the status code of the response: does it match
370                 // the expected response(s)?
371                 assertStatusCode(res, testName);
372                 AbstractCommonList list = res.getEntity();
373         
374                 // Optionally output additional data about list members for debugging.
375                 if (logger.isTraceEnabled() == true){
376                         AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
377                 }
378         } finally {
379                 if (res != null) {
380                 res.releaseConnection();
381             }
382         }
383     }
384     
385     // Failure outcomes
386     // None at present.
387
388     // ---------------------------------------------------------------
389     // CRUD tests : UPDATE tests
390     // ---------------------------------------------------------------
391
392     // Success outcomes
393     /* (non-Javadoc)
394      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
395      */
396     @Override
397 //    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
398 //        dependsOnMethods = {"read"})
399     public void update(String testName) throws Exception {
400         // Perform setup.
401         setupUpdate();
402
403         // Retrieve the contents of a resource to update.
404         AcquisitionClient client = new AcquisitionClient();
405         ClientResponse<String> res = client.read(knownResourceId);
406         if(logger.isDebugEnabled()){
407             logger.debug(testName + ": read status = " + res.getStatus());
408         }
409         Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
410
411         PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
412
413         AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
414                 client.getCommonPartName(), AcquisitionsCommon.class);
415         Assert.assertNotNull(acquisition);
416
417         // Update the content of this resource.
418         acquisition.setAcquisitionReferenceNumber("updated-" + acquisition.getAcquisitionReferenceNumber());
419         if(logger.isDebugEnabled()){
420             logger.debug("updated object");
421             logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
422         }
423         // Submit the request to the service and store the response.
424         PoxPayloadOut output = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
425         PayloadOutputPart commonPart = output.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
426         commonPart.setLabel(client.getCommonPartName());
427
428         res = client.update(knownResourceId, output);
429         int statusCode = res.getStatus();
430         // Check the status code of the response: does it match the expected response(s)?
431         if(logger.isDebugEnabled()){
432             logger.debug(testName + ": status = " + statusCode);
433         }
434         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
435                 invalidStatusCodeMessage(testRequestType, statusCode));
436         Assert.assertEquals(statusCode, testExpectedStatusCode);
437
438         input = new PoxPayloadIn(res.getEntity());
439         AcquisitionsCommon updatedAcquisition =
440                 (AcquisitionsCommon) extractPart(input,
441                         client.getCommonPartName(), AcquisitionsCommon.class);
442         Assert.assertNotNull(updatedAcquisition);
443
444         Assert.assertEquals(updatedAcquisition.getAcquisitionReferenceNumber(),
445                 acquisition.getAcquisitionReferenceNumber(),
446                 "Data in updated object did not match submitted data.");
447
448     }
449
450     // Failure outcomes
451     // Placeholders until the three tests below can be uncommented.
452     // See Issue CSPACE-401.
453     /* (non-Javadoc)
454      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
455      */
456     @Override
457     public void updateWithEmptyEntityBody(String testName) throws Exception {
458         //Should this really be empty?
459     }
460
461     /* (non-Javadoc)
462      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
463      */
464     @Override
465     public void updateWithMalformedXml(String testName) throws Exception {
466         //Should this really be empty?
467     }
468
469     /* (non-Javadoc)
470      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
471      */
472     @Override
473     public void updateWithWrongXmlSchema(String testName) throws Exception {
474         //Should this really be empty?
475     }
476
477     /*
478     @Override
479     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
480         dependsOnMethods = {"create", "update", "testSubmitRequest"})
481     public void updateWithEmptyEntityBody(String testName) throws Exception {
482     
483         if (logger.isDebugEnabled()) {
484             logger.debug(testBanner(testName, CLASS_NAME));
485         };
486             
487         // Perform setup.
488         setupUpdateWithEmptyEntityBody();
489
490         // Submit the request to the service and store the response.
491         String method = REQUEST_TYPE.httpMethodName();
492         String url = getResourceURL(knownResourceId);
493         String mediaType = MediaType.APPLICATION_XML;
494         final String entity = "";
495         int statusCode = submitRequest(method, url, mediaType, entity);
496
497         // Check the status code of the response: does it match
498         // the expected response(s)?
499         if(logger.isDebugEnabled()){
500             (testName + ": url=" + url + " status=" + statusCode);
501         }
502         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
503         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
504         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
505         }
506
507     @Override
508     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
509         dependsOnMethods = {"create", "testSubmitRequest"})
510     public void createWithEmptyEntityBody(String testName) throws Exception {
511     
512         if (logger.isDebugEnabled()) {
513             logger.debug(testBanner(testName, CLASS_NAME));
514         };
515         
516         // Perform setup.
517         setupCreateWithEmptyEntityBody();
518
519         // Submit the request to the service and store the response.
520         String method = REQUEST_TYPE.httpMethodName();
521         String url = getServiceRootURL();
522         String mediaType = MediaType.APPLICATION_XML;
523         final String entity = "";
524         int statusCode = submitRequest(method, url, mediaType, entity);
525
526         // Check the status code of the response: does it match
527         // the expected response(s)?
528         if(logger.isDebugEnabled()){
529             logger.debug(testName + ": url=" + url +
530                 " status=" + statusCode);
531         }
532         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
533         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
534         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
535     }
536
537     @Override
538     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
539         dependsOnMethods = {"create", "update", "testSubmitRequest"})
540     public void updateWithMalformedXml(String testName) throws Exception {
541
542         if (logger.isDebugEnabled()) {
543             logger.debug(testBanner(testName, CLASS_NAME));
544         };
545         
546         // Perform setup.
547         setupUpdateWithMalformedXml();
548
549         // Submit the request to the service and store the response.
550         String method = REQUEST_TYPE.httpMethodName();
551         String url = getResourceURL(knownResourceId);
552         final String entity = MALFORMED_XML_DATA;
553         int statusCode = submitRequest(method, url, entity);
554
555         // Check the status code of the response: does it match
556         // the expected response(s)?
557         if(logger.isDebugEnabled()){
558             logger.debug(testName + ": url=" + url +
559                 " status=" + statusCode);
560          }
561         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
562         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
563         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
564     }
565
566     @Override
567     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
568     public void updateWithWrongXmlSchema(String testName) {
569     
570         if (logger.isDebugEnabled()) {
571             logger.debug(testBanner(testName, CLASS_NAME));
572         };
573         
574         // Perform setup.
575         setupUpdateWithWrongXmlSchema();
576
577         // Submit the request to the service and store the response.
578         String method = REQUEST_TYPE.httpMethodName();
579         String url = getResourceURL(knownResourceId);
580         final String entity = WRONG_XML_SCHEMA_DATA;
581         int statusCode = submitRequest(method, url, entity);
582
583         // Check the status code of the response: does it match
584         // the expected response(s)?
585         if(logger.isDebugEnabled()){
586             logger.debug(testName + ": url=" + url +
587                 " status=" + statusCode);
588          }
589         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
590         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
591         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
592     }
593      */
594     
595     /* (non-Javadoc)
596      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
597      */
598     @Override
599 //    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
600 //        dependsOnMethods = {"update", "testSubmitRequest"})
601     public void updateNonExistent(String testName) throws Exception {
602         // Perform setup.
603         setupUpdateNonExistent();
604
605         // Submit the request to the service and store the response.
606         // Note: The ID used in this 'create' call may be arbitrary.
607         // The only relevant ID may be the one used in update(), below.
608         AcquisitionClient client = new AcquisitionClient();
609         PoxPayloadOut multipart = createAcquisitionInstance(NON_EXISTENT_ID);
610         ClientResponse<String> res =
611             client.update(NON_EXISTENT_ID, multipart);
612         int statusCode = res.getStatus();
613
614         // Check the status code of the response: does it match
615         // the expected response(s)?
616         if(logger.isDebugEnabled()){
617             logger.debug(testName + ": status = " + statusCode);
618         }
619         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
620                 invalidStatusCodeMessage(testRequestType, statusCode));
621         Assert.assertEquals(statusCode, testExpectedStatusCode);
622     }
623
624     // ---------------------------------------------------------------
625     // CRUD tests : DELETE tests
626     // ---------------------------------------------------------------
627     // Success outcomes
628     /* (non-Javadoc)
629      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
630      */
631     @Override
632 //    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
633 //        dependsOnMethods = {"create", "read", "update"})
634     public void delete(String testName) throws Exception {
635         // Perform setup.
636         setupDelete();
637
638         // Submit the request to the service and store the response.
639         AcquisitionClient client = new AcquisitionClient();
640         Response res = client.delete(knownResourceId);
641         int statusCode;
642         try {
643                 statusCode = res.getStatus();
644         } finally {
645                 res.close();
646         }
647
648         // Check the status code of the response: does it match
649         // the expected response(s)?
650         if(logger.isDebugEnabled()){
651             logger.debug(testName + ": status = " + statusCode);
652         }
653         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
654                 invalidStatusCodeMessage(testRequestType, statusCode));
655         Assert.assertEquals(statusCode, testExpectedStatusCode);
656     }
657
658     // Failure outcomes
659     /* (non-Javadoc)
660      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
661      */
662     @Override
663 //    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
664 //        dependsOnMethods = {"delete"})
665     public void deleteNonExistent(String testName) throws Exception {
666         // Perform setup.
667         setupDeleteNonExistent();
668
669         // Submit the request to the service and store the response.
670         AcquisitionClient client = new AcquisitionClient();
671         Response res = client.delete(NON_EXISTENT_ID);
672         int statusCode;
673         try {
674                 statusCode = res.getStatus();
675         } finally {
676                 res.close();
677         }
678
679         // Check the status code of the response: does it match
680         // the expected response(s)?
681         if(logger.isDebugEnabled()){
682             logger.debug(testName + ": status = " + statusCode);
683         }
684         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
685                 invalidStatusCodeMessage(testRequestType, statusCode));
686         Assert.assertEquals(statusCode, testExpectedStatusCode);
687     }
688
689     // ---------------------------------------------------------------
690     // Utility tests : tests of code used in tests above
691     // ---------------------------------------------------------------
692
693     @Override
694     protected PoxPayloadOut createInstance(String identifier) {
695         return createAcquisitionInstance(identifier);
696     }
697         
698     /**
699      * Creates the acquisition instance.
700      *
701      * @param identifier the identifier
702      * @return the multipart output
703      */
704     private PoxPayloadOut createAcquisitionInstance(String identifier) {
705         AcquisitionsCommon acquisition = new AcquisitionsCommon();
706         acquisition.setAcquisitionReferenceNumber("acquisitionReferenceNumber-"  + identifier);
707
708         AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
709         List<String> acqSources = acqSourcesList.getAcquisitionSource();
710         // FIXME Use properly formatted refNames for representative acquisition
711         // sources in this example test record. The following are mere placeholders.
712         acqSources.add("Donor Acquisition Source-" + identifier);
713         acqSources.add("Museum Acquisition Source-" + identifier);
714         acquisition.setAcquisitionSources(acqSourcesList);
715
716         AcquisitionDateGroupList acqDatesList = new AcquisitionDateGroupList();
717         List<StructuredDateGroup> acqDates = acqDatesList.getAcquisitionDateGroup();
718         StructuredDateGroup acqDate1 = new StructuredDateGroup();
719         acqDate1.setDateDisplayDate(CURRENT_DATE_UTC);
720         StructuredDateGroup acqDate2 = new StructuredDateGroup();
721         acqDate2.setDateDisplayDate(CURRENT_DATE_UTC);
722         acqDates.add(acqDate1);
723         acqDates.add(acqDate2);
724         acquisition.setAcquisitionDateGroupList(acqDatesList);
725
726         OwnerList ownersList = new OwnerList();
727         List<String> owners = ownersList.getOwner();
728         // FIXME Use properly formatted refNames for representative owners
729         // in this example test record. The following are mere placeholders.
730         owners.add("First Owner -" + identifier);
731         owners.add("Second Owner-" + identifier);
732         acquisition.setOwners(ownersList);
733
734         PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
735         PayloadOutputPart commonPart = multipart.addPart(acquisition,
736             MediaType.APPLICATION_XML_TYPE);
737         commonPart.setLabel(new AcquisitionClient().getCommonPartName());
738
739         if(logger.isDebugEnabled()){
740             logger.debug("to be created, acquisition common");
741             logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
742         }
743         return multipart;
744     }
745
746     // FIXME: The following methods might be made generic and moved to a common package.
747
748     /**
749      * Retrives an XML document from the given file, and uses
750      * the JAXB unmarshaller to create a Java object representation
751      * and ultimately a multipart payload that can be submitted in
752      * a create or update request.
753      *
754      * @param commonPartName
755      * @param commonPartFileName
756      * @return
757      * @throws Exception
758      */
759     private PoxPayloadOut createAcquisitionInstanceFromXml(String testName, String commonPartName,
760             String commonPartFileName) throws Exception {
761
762         AcquisitionsCommon acquisition =
763                 (AcquisitionsCommon) getObjectFromFile(AcquisitionsCommon.class,
764                 commonPartFileName);
765         PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
766         PayloadOutputPart commonPart = multipart.addPart(acquisition,
767                 MediaType.APPLICATION_XML_TYPE);
768         commonPart.setLabel(commonPartName);
769
770         if (logger.isDebugEnabled()) {
771             logger.debug(testName + " to be created, acquisitions common");
772             logger.debug(objectAsXmlString(acquisition,
773                     AcquisitionsCommon.class));
774         }
775         return multipart;
776
777     }
778
779     /**
780      * Creates a record / resource from the data in an XML file.
781      *
782      * @param testName the test name
783      * @param fileName the file name
784      * @param useJaxb the use jaxb
785      * @return the string
786      * @throws Exception the exception
787      */
788     private String createFromXmlFile(String testName, String fileName, boolean useJaxb) throws Exception {
789
790         // Perform setup.
791         setupCreate();
792
793         PoxPayloadOut multipart = null;
794
795         AcquisitionClient client = new AcquisitionClient();
796         if (useJaxb) {
797             multipart = createAcquisitionInstanceFromXml(testName,
798                     client.getCommonPartName(), fileName);
799         } else {
800
801             multipart = createAcquisitionInstanceFromRawXml(testName,
802                     client.getCommonPartName(), fileName);
803         }
804         Response res = client.create(multipart);
805         int statusCode = res.getStatus();
806
807         if (logger.isDebugEnabled()) {
808             logger.debug(testName + ": status = " + statusCode);
809         }
810         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
811                 invalidStatusCodeMessage(testRequestType, statusCode));
812         Assert.assertEquals(statusCode, testExpectedStatusCode);
813         String newId = extractId(res);
814         allResourceIdsCreated.add(newId);
815         return newId;
816     }
817
818      /**
819      * Returns a multipart payload that can be submitted with a
820      * create or update request, by reading from an XML file.
821      *
822      * @param commonPartName
823      * @param commonPartFileName
824      * @return
825      * @throws Exception
826      */
827     private PoxPayloadOut createAcquisitionInstanceFromRawXml(String testName, String commonPartName,
828             String commonPartFileName) throws Exception {
829
830         PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
831         String stringObject = getXmlDocumentAsString(commonPartFileName);
832         if (logger.isDebugEnabled()) {
833             logger.debug(testName + " to be created, acquisition common " + "\n" + stringObject);
834         }
835         PayloadOutputPart commonPart = multipart.addPart(stringObject, stringObject);
836         commonPart.setLabel(commonPartName);
837
838         return multipart;
839
840     }
841
842     // FIXME: This duplicates code in read(), and should be consolidated.
843     // This is an expedient to support reading and verifying the contents
844     // of resources that have been created from test data XML files.
845     private AcquisitionsCommon readAcquisitionCommonPart(String csid)
846         throws Exception {
847
848         String testName = "readAcquisitionCommonPart";
849
850         setupRead();
851
852         // Submit the request to the service and store the response.
853         AcquisitionClient client = new AcquisitionClient();
854         ClientResponse<String> res = client.read(csid);
855         AcquisitionsCommon acquisition = null;
856         try {
857                 // Check the status code of the response: does it match
858                 // the expected response(s)?
859                 assertStatusCode(res, testName);
860                 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
861         
862                 if (logger.isDebugEnabled()) {
863                     logger.debug(testName + ": Reading Common part ...");
864                 }
865                 acquisition = (AcquisitionsCommon) extractPart(input,
866                         client.getCommonPartName(), AcquisitionsCommon.class);
867                 Assert.assertNotNull(acquisition);
868         } finally {
869                 if (res != null) {
870                 res.releaseConnection();
871             }
872         }
873
874         return acquisition;
875      }
876
877
878         @Override
879         protected PoxPayloadOut createInstance(String commonPartName,
880                         String identifier) {
881                 // TODO Auto-generated method stub
882                 return null;
883         }
884
885         @Override
886         protected AcquisitionsCommon updateInstance(
887                         AcquisitionsCommon commonPartObject) {
888                 // TODO Auto-generated method stub
889                 return null;
890         }
891
892         @Override
893         protected void compareUpdatedInstances(AcquisitionsCommon original,
894                         AcquisitionsCommon updated) throws Exception {
895                 // TODO Auto-generated method stub
896                 
897         }
898
899     /*
900      * For convenience and terseness, this test method is the base of the test execution dependency chain.  Other test methods may
901      * refer to this method in their @Test annotation declarations.
902      */
903     @Override
904     @Test(dataProvider = "testName",
905                 dependsOnMethods = {
906                         "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})    
907     public void CRUDTests(String testName) {
908         // Do nothing.  Simply here to for a TestNG execution order for our tests
909     }   
910 }
911