]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
cadcaedc96e0706b826ab3484b0230a1bf3908ea
[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.jaxb.AbstractCommonList;
32
33 import org.collectionspace.services.acquisition.AcquisitionsCommon;
34 import org.collectionspace.services.acquisition.AcquisitionsCommonList;
35 import org.collectionspace.services.acquisition.AcquisitionDateList;
36 import org.collectionspace.services.acquisition.AcquisitionFunding;
37 import org.collectionspace.services.acquisition.AcquisitionFundingList;
38 import org.collectionspace.services.acquisition.AcquisitionSourceList;
39 import org.collectionspace.services.acquisition.OwnerList;
40 import org.jboss.resteasy.client.ClientResponse;
41
42 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
43 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
44 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
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 AbstractServiceTestImpl {
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     /** The known resource id. */
66     private String knownResourceId = null;
67
68     /* (non-Javadoc)
69      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
70      */
71     @Override
72     protected CollectionSpaceClient getClientInstance() {
73         return new AcquisitionClient();
74     }
75     
76     /* (non-Javadoc)
77      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
78      */
79     @Override
80         protected AbstractCommonList getAbstractCommonList(
81                         ClientResponse<AbstractCommonList> response) {
82         return response.getEntity(AcquisitionsCommonList.class);
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         MultipartOutput 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<MultipartInput> 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         MultipartInput input = (MultipartInput) 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<MultipartInput> 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<AcquisitionsCommonList> res = client.readList();
394         AcquisitionsCommonList 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         boolean iterateThroughList = false;
408         if(iterateThroughList && logger.isDebugEnabled()){
409             List<AcquisitionsCommonList.AcquisitionListItem> items =
410                     list.getAcquisitionListItem();
411             int i = 0;
412             for(AcquisitionsCommonList.AcquisitionListItem item : items){
413                 logger.debug(testName + ": list-item[" + i + "] csid=" +
414                     item.getCsid());
415                 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
416                     item.getAcquisitionReferenceNumber());
417                 logger.debug(testName + ": list-item[" + i + "] acquisitionSources:");
418                 AcquisitionSourceList acqSource = item.getAcquisitionSources();
419                 for (String acquisitionSource : acqSource.getAcquisitionSource()) {
420                     logger.debug("acquisitionSource=" + acquisitionSource);
421                 }
422                 logger.debug(testName + ": list-item[" + i + "] URI=" +
423                     item.getUri());
424                 i++;
425             }
426         }
427
428     }
429
430     // Failure outcomes
431     // None at present.
432
433     // ---------------------------------------------------------------
434     // CRUD tests : UPDATE tests
435     // ---------------------------------------------------------------
436
437     // Success outcomes
438     /* (non-Javadoc)
439      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
440      */
441     @Override
442     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
443         dependsOnMethods = {"read"})
444     public void update(String testName) throws Exception {
445
446         if (logger.isDebugEnabled()) {
447             logger.debug(testBanner(testName, CLASS_NAME));
448         }
449         
450         // Perform setup.
451         setupUpdate();
452
453         // Retrieve the contents of a resource to update.
454         AcquisitionClient client = new AcquisitionClient();
455         ClientResponse<MultipartInput> res = client.read(knownResourceId);
456         if(logger.isDebugEnabled()){
457             logger.debug(testName + ": read status = " + res.getStatus());
458         }
459         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
460
461         if(logger.isDebugEnabled()){
462             logger.debug("got object to update with ID: " + knownResourceId);
463         }
464         MultipartInput input = (MultipartInput) res.getEntity();
465
466         AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
467                 client.getCommonPartName(), AcquisitionsCommon.class);
468         Assert.assertNotNull(acquisition);
469
470         // Update the content of this resource.
471         acquisition.setAcquisitionReferenceNumber("updated-" + acquisition.getAcquisitionReferenceNumber());
472         if(logger.isDebugEnabled()){
473             logger.debug("updated object");
474             logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
475         }
476         // Submit the request to the service and store the response.
477         MultipartOutput output = new MultipartOutput();
478         OutputPart commonPart = output.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
479         commonPart.getHeaders().add("label", client.getCommonPartName());
480
481         res = client.update(knownResourceId, output);
482         int statusCode = res.getStatus();
483         // Check the status code of the response: does it match the expected response(s)?
484         if(logger.isDebugEnabled()){
485             logger.debug(testName + ": status = " + statusCode);
486         }
487         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
488                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
489         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
490
491
492         input = (MultipartInput) res.getEntity();
493         AcquisitionsCommon updatedAcquisition =
494                 (AcquisitionsCommon) extractPart(input,
495                         client.getCommonPartName(), AcquisitionsCommon.class);
496         Assert.assertNotNull(updatedAcquisition);
497
498         Assert.assertEquals(updatedAcquisition.getAcquisitionReferenceNumber(),
499                 acquisition.getAcquisitionReferenceNumber(),
500                 "Data in updated object did not match submitted data.");
501
502     }
503
504     // Failure outcomes
505     // Placeholders until the three tests below can be uncommented.
506     // See Issue CSPACE-401.
507     /* (non-Javadoc)
508      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
509      */
510     @Override
511     public void updateWithEmptyEntityBody(String testName) throws Exception {
512         //Should this really be empty?
513     }
514
515     /* (non-Javadoc)
516      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
517      */
518     @Override
519     public void updateWithMalformedXml(String testName) throws Exception {
520         //Should this really be empty?
521     }
522
523     /* (non-Javadoc)
524      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
525      */
526     @Override
527     public void updateWithWrongXmlSchema(String testName) throws Exception {
528         //Should this really be empty?
529     }
530
531     /*
532     @Override
533     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
534         dependsOnMethods = {"create", "update", "testSubmitRequest"})
535     public void updateWithEmptyEntityBody(String testName) throws Exception {
536     
537         if (logger.isDebugEnabled()) {
538             logger.debug(testBanner(testName, CLASS_NAME));
539         };
540             
541         // Perform setup.
542         setupUpdateWithEmptyEntityBody();
543
544         // Submit the request to the service and store the response.
545         String method = REQUEST_TYPE.httpMethodName();
546         String url = getResourceURL(knownResourceId);
547         String mediaType = MediaType.APPLICATION_XML;
548         final String entity = "";
549         int statusCode = submitRequest(method, url, mediaType, entity);
550
551         // Check the status code of the response: does it match
552         // the expected response(s)?
553         if(logger.isDebugEnabled()){
554             (testName + ": url=" + url + " status=" + statusCode);
555         }
556         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
557         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
558         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
559         }
560
561     @Override
562     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
563         dependsOnMethods = {"create", "testSubmitRequest"})
564     public void createWithEmptyEntityBody(String testName) throws Exception {
565     
566         if (logger.isDebugEnabled()) {
567             logger.debug(testBanner(testName, CLASS_NAME));
568         };
569         
570         // Perform setup.
571         setupCreateWithEmptyEntityBody();
572
573         // Submit the request to the service and store the response.
574         String method = REQUEST_TYPE.httpMethodName();
575         String url = getServiceRootURL();
576         String mediaType = MediaType.APPLICATION_XML;
577         final String entity = "";
578         int statusCode = submitRequest(method, url, mediaType, entity);
579
580         // Check the status code of the response: does it match
581         // the expected response(s)?
582         if(logger.isDebugEnabled()){
583             logger.debug(testName + ": url=" + url +
584                 " status=" + statusCode);
585         }
586         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
587         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
588         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
589     }
590
591     @Override
592     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
593         dependsOnMethods = {"create", "update", "testSubmitRequest"})
594     public void updateWithMalformedXml(String testName) throws Exception {
595
596         if (logger.isDebugEnabled()) {
597             logger.debug(testBanner(testName, CLASS_NAME));
598         };
599         
600         // Perform setup.
601         setupUpdateWithMalformedXml();
602
603         // Submit the request to the service and store the response.
604         String method = REQUEST_TYPE.httpMethodName();
605         String url = getResourceURL(knownResourceId);
606         final String entity = MALFORMED_XML_DATA;
607         int statusCode = submitRequest(method, url, entity);
608
609         // Check the status code of the response: does it match
610         // the expected response(s)?
611         if(logger.isDebugEnabled()){
612             logger.debug(testName + ": url=" + url +
613                 " status=" + statusCode);
614          }
615         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
616         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
617         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
618     }
619
620     @Override
621     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
622     public void updateWithWrongXmlSchema(String testName) {
623     
624         if (logger.isDebugEnabled()) {
625             logger.debug(testBanner(testName, CLASS_NAME));
626         };
627         
628         // Perform setup.
629         setupUpdateWithWrongXmlSchema();
630
631         // Submit the request to the service and store the response.
632         String method = REQUEST_TYPE.httpMethodName();
633         String url = getResourceURL(knownResourceId);
634         final String entity = WRONG_XML_SCHEMA_DATA;
635         int statusCode = submitRequest(method, url, entity);
636
637         // Check the status code of the response: does it match
638         // the expected response(s)?
639         if(logger.isDebugEnabled()){
640             logger.debug(testName + ": url=" + url +
641                 " status=" + statusCode);
642          }
643         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
644         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
645         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
646     }
647      */
648     
649     /* (non-Javadoc)
650      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
651      */
652     @Override
653     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
654         dependsOnMethods = {"update", "testSubmitRequest"})
655     public void updateNonExistent(String testName) throws Exception {
656
657         if (logger.isDebugEnabled()) {
658             logger.debug(testBanner(testName, CLASS_NAME));
659         }
660         
661         // Perform setup.
662         setupUpdateNonExistent();
663
664         // Submit the request to the service and store the response.
665         // Note: The ID used in this 'create' call may be arbitrary.
666         // The only relevant ID may be the one used in update(), below.
667         AcquisitionClient client = new AcquisitionClient();
668         MultipartOutput multipart = createAcquisitionInstance(NON_EXISTENT_ID);
669         ClientResponse<MultipartInput> res =
670             client.update(NON_EXISTENT_ID, multipart);
671         int statusCode = res.getStatus();
672
673         // Check the status code of the response: does it match
674         // the expected response(s)?
675         if(logger.isDebugEnabled()){
676             logger.debug(testName + ": status = " + statusCode);
677         }
678         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
679                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
680         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
681     }
682
683     // ---------------------------------------------------------------
684     // CRUD tests : DELETE tests
685     // ---------------------------------------------------------------
686     // Success outcomes
687     /* (non-Javadoc)
688      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
689      */
690     @Override
691     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
692         dependsOnMethods = {"create", "read", "update"})
693     public void delete(String testName) throws Exception {
694
695         if (logger.isDebugEnabled()) {
696             logger.debug(testBanner(testName, CLASS_NAME));
697         }
698         
699         // Perform setup.
700         setupDelete();
701
702         // Submit the request to the service and store the response.
703         AcquisitionClient client = new AcquisitionClient();
704         ClientResponse<Response> res = client.delete(knownResourceId);
705         int statusCode = res.getStatus();
706
707         // Check the status code of the response: does it match
708         // the expected response(s)?
709         if(logger.isDebugEnabled()){
710             logger.debug(testName + ": status = " + statusCode);
711         }
712         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
713                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
714         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
715     }
716
717     // Failure outcomes
718     /* (non-Javadoc)
719      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
720      */
721     @Override
722     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
723         dependsOnMethods = {"delete"})
724     public void deleteNonExistent(String testName) throws Exception {
725
726         if (logger.isDebugEnabled()) {
727             logger.debug(testBanner(testName, CLASS_NAME));
728         }
729         
730         // Perform setup.
731         setupDeleteNonExistent();
732
733         // Submit the request to the service and store the response.
734         AcquisitionClient client = new AcquisitionClient();
735         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
736         int statusCode = res.getStatus();
737
738         // Check the status code of the response: does it match
739         // the expected response(s)?
740         if(logger.isDebugEnabled()){
741             logger.debug(testName + ": status = " + statusCode);
742         }
743         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
744                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
745         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
746     }
747
748     // ---------------------------------------------------------------
749     // Utility tests : tests of code used in tests above
750     // ---------------------------------------------------------------
751     /**
752      * Tests the code for manually submitting data that is used by several
753      * of the methods above.
754      * @throws Exception
755      */
756
757     @Test(dependsOnMethods = {"create", "read"})
758     public void testSubmitRequest() throws Exception {
759         testSubmitRequest(knownResourceId);
760     }
761
762     /**
763      * Test submit request.
764      *
765      * @param resourceId the resource id
766      * @throws Exception the exception
767      */
768     private void testSubmitRequest(String resourceId) throws Exception {
769
770         // Expected status code: 200 OK
771         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
772
773         // Submit the request to the service and store the response.
774         String method = ServiceRequestType.READ.httpMethodName();
775         String url = getResourceURL(resourceId);
776         int statusCode = submitRequest(method, url);
777
778         // Check the status code of the response: does it match
779         // the expected response(s)?
780         if (logger.isDebugEnabled()) {
781             logger.debug("testSubmitRequest: url=" + url
782                     + " status=" + statusCode);
783         }
784         Assert.assertEquals(statusCode, EXPECTED_STATUS);
785
786     }
787
788     // ---------------------------------------------------------------
789     // Utility methods used by tests above
790     // ---------------------------------------------------------------
791     /* (non-Javadoc)
792      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
793      */
794     @Override
795     public String getServicePathComponent() {
796         return new AcquisitionClient().getServicePathComponent();
797     }
798
799
800     /**
801      * Creates the acquisition instance.
802      *
803      * @param identifier the identifier
804      * @return the multipart output
805      */
806     private MultipartOutput createAcquisitionInstance(String identifier) {
807         AcquisitionsCommon acquisition = new AcquisitionsCommon();
808         acquisition.setAcquisitionReferenceNumber("acquisitionReferenceNumber-"  + identifier);
809
810         AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
811         List<String> acqSources = acqSourcesList.getAcquisitionSource();
812         // FIXME Use properly formatted refNames for representative acquisition
813         // sources in this example test record. The following are mere placeholders.
814         acqSources.add("Donor Acquisition Source-" + identifier);
815         acqSources.add("Museum Acquisition Source-" + identifier);
816         acquisition.setAcquisitionSources(acqSourcesList);
817
818         AcquisitionDateList acqDatesList = new AcquisitionDateList();
819         List<String> acqDates = acqDatesList.getAcquisitionDate();
820         // FIXME Use properly timestamps for representative acquisition
821         // dates in this example test record. The following are mere placeholders.
822         acqDates.add("First Acquisition Date -" + identifier);
823         acqDates.add("Second Acquisition Date-" + identifier);
824         acquisition.setAcquisitionDates(acqDatesList);
825
826         OwnerList ownersList = new OwnerList();
827         List<String> owners = ownersList.getOwner();
828         // FIXME Use properly formatted refNames for representative owners
829         // in this example test record. The following are mere placeholders.
830         owners.add("First Owner -" + identifier);
831         owners.add("Second Owner-" + identifier);
832         acquisition.setOwners(ownersList);
833
834         MultipartOutput multipart = new MultipartOutput();
835         OutputPart commonPart = multipart.addPart(acquisition,
836             MediaType.APPLICATION_XML_TYPE);
837         commonPart.getHeaders().add("label", new AcquisitionClient().getCommonPartName());
838
839         if(logger.isDebugEnabled()){
840             logger.debug("to be created, acquisition common");
841             logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
842         }
843         return multipart;
844     }
845
846     // FIXME: The following methods might be made generic and moved to a common package.
847
848     /**
849      * Retrives an XML document from the given file, and uses
850      * the JAXB unmarshaller to create a Java object representation
851      * and ultimately a multipart payload that can be submitted in
852      * a create or update request.
853      *
854      * @param commonPartName
855      * @param commonPartFileName
856      * @return
857      * @throws Exception
858      */
859     private MultipartOutput createAcquisitionInstanceFromXml(String testName, String commonPartName,
860             String commonPartFileName) throws Exception {
861
862         AcquisitionsCommon acquisition =
863                 (AcquisitionsCommon) getObjectFromFile(AcquisitionsCommon.class,
864                 commonPartFileName);
865         MultipartOutput multipart = new MultipartOutput();
866         OutputPart commonPart = multipart.addPart(acquisition,
867                 MediaType.APPLICATION_XML_TYPE);
868         commonPart.getHeaders().add("label", commonPartName);
869
870         if (logger.isDebugEnabled()) {
871             logger.debug(testName + " to be created, acquisitions common");
872             logger.debug(objectAsXmlString(acquisition,
873                     AcquisitionsCommon.class));
874         }
875         return multipart;
876
877     }
878
879     /**
880      * Creates a record / resource from the data in an XML file.
881      *
882      * @param testName the test name
883      * @param fileName the file name
884      * @param useJaxb the use jaxb
885      * @return the string
886      * @throws Exception the exception
887      */
888     private String createFromXmlFile(String testName, String fileName, boolean useJaxb) throws Exception {
889
890         // Perform setup.
891         setupCreate();
892
893         MultipartOutput multipart = null;
894
895         AcquisitionClient client = new AcquisitionClient();
896         if (useJaxb) {
897             multipart = createAcquisitionInstanceFromXml(testName,
898                     client.getCommonPartName(), fileName);
899         } else {
900
901             multipart = createAcquisitionInstanceFromRawXml(testName,
902                     client.getCommonPartName(), fileName);
903         }
904         ClientResponse<Response> res = client.create(multipart);
905         int statusCode = res.getStatus();
906
907         if (logger.isDebugEnabled()) {
908             logger.debug(testName + ": status = " + statusCode);
909         }
910         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
911                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
912         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
913         String newId = extractId(res);
914         allResourceIdsCreated.add(newId);
915         return newId;
916     }
917
918      /**
919      * Returns a multipart payload that can be submitted with a
920      * create or update request, by reading from an XML file.
921      *
922      * @param commonPartName
923      * @param commonPartFileName
924      * @return
925      * @throws Exception
926      */
927     private MultipartOutput createAcquisitionInstanceFromRawXml(String testName, String commonPartName,
928             String commonPartFileName) throws Exception {
929
930         MultipartOutput multipart = new MultipartOutput();
931         String stringObject = getXmlDocumentAsString(commonPartFileName);
932         if (logger.isDebugEnabled()) {
933             logger.debug(testName + " to be created, acquisition common " + "\n" + stringObject);
934         }
935         OutputPart commonPart = multipart.addPart(stringObject,
936                 MediaType.APPLICATION_XML_TYPE);
937         commonPart.getHeaders().add("label", commonPartName);
938
939         return multipart;
940
941     }
942
943     // FIXME: This duplicates code in read(), and should be consolidated.
944     // This is an expedient to support reading and verifying the contents
945     // of resources that have been created from test data XML files.
946     private AcquisitionsCommon readAcquisitionCommonPart(String csid)
947         throws Exception {
948
949         String testName = "readAcquisitionCommonPart";
950
951         setupRead();
952
953         // Submit the request to the service and store the response.
954         AcquisitionClient client = new AcquisitionClient();
955         ClientResponse<MultipartInput> res = client.read(csid);
956         int statusCode = res.getStatus();
957
958         // Check the status code of the response: does it match
959         // the expected response(s)?
960         if (logger.isDebugEnabled()) {
961             logger.debug(testName + ": status = " + statusCode);
962         }
963         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
964                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
965         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
966
967         MultipartInput input = (MultipartInput) res.getEntity();
968
969         if (logger.isDebugEnabled()) {
970             logger.debug(testName + ": Reading Common part ...");
971         }
972         AcquisitionsCommon acquisition =
973                 (AcquisitionsCommon) extractPart(input,
974                 client.getCommonPartName(), AcquisitionsCommon.class);
975         Assert.assertNotNull(acquisition);
976
977         return acquisition;
978      }
979
980 }
981