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