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