]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
70d459ede0e1c0f46981567c841faa7e0e06d0da
[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 © 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.AbstractServiceClientImpl;
30 import org.collectionspace.services.client.CollectionObjectClient;
31 import org.collectionspace.services.client.CollectionObjectFactory;
32 import org.collectionspace.services.client.CollectionSpaceClient;
33 import org.collectionspace.services.collectionobject.BriefDescriptionList;
34 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
35 import org.collectionspace.services.collectionobject.domain.naturalhistory.CollectionobjectsNaturalhistory;
36 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
37 import org.collectionspace.services.collectionobject.ResponsibleDepartmentList;
38 import org.collectionspace.services.collectionobject.OtherNumber;
39 import org.collectionspace.services.collectionobject.OtherNumberList;
40
41 import org.collectionspace.services.jaxb.AbstractCommonList;
42
43 import org.jboss.resteasy.client.ClientResponse;
44 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
45 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
46 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
47 import org.testng.Assert;
48 import org.testng.annotations.Test;
49
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 /**
54  * CollectionObjectServiceTest, carries out tests against a
55  * deployed and running CollectionObject Service.
56  *
57  * $LastChangedRevision$
58  * $LastChangedDate$
59  */
60 public class CollectionObjectServiceTest extends AbstractServiceTestImpl {
61
62     /** The logger. */
63     private final String CLASS_NAME = CollectionObjectServiceTest.class.getName();
64     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
65     
66     // Instance variables specific to this test.
67     /** The known resource id. */
68     private String knownResourceId = null;
69
70     /* (non-Javadoc)
71      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
72      */
73     @Override
74     protected String getServicePathComponent() {
75         return new CollectionObjectClient().getServicePathComponent();
76     }
77     
78     /* (non-Javadoc)
79      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
80      */
81     @Override
82     protected CollectionSpaceClient getClientInstance() {
83         return new CollectionObjectClient();
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(CollectionobjectsCommonList.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         // Perform setup, such as initializing the type of service request
110         // (e.g. CREATE, DELETE), its valid and expected status codes, and
111         // its associated HTTP method name (e.g. POST, DELETE).
112         setupCreate();
113
114         // Submit the request to the service and store the response.
115         CollectionObjectClient client = new CollectionObjectClient();
116         String identifier = createIdentifier();
117         MultipartOutput multipart =
118                 createCollectionObjectInstance(client.getCommonPartName(), identifier);
119         ClientResponse<Response> res = client.create(multipart);
120         int statusCode = res.getStatus();
121
122         // Check the status code of the response: does it match
123         // the expected response(s)?
124         //
125         // Specifically:
126         // Does it fall within the set of valid status codes?
127         // Does it exactly match the expected status code?
128         if (logger.isDebugEnabled()) {
129             logger.debug(testName + ": status = " + statusCode);
130         }
131         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
132                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
133         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
134
135         // Store the ID returned from the first resource created
136         // for additional tests below.
137         if (knownResourceId == null) {
138             knownResourceId = extractId(res);
139             if (logger.isDebugEnabled()) {
140                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
141             }
142         }
143
144         // Store the IDs from every resource created by tests,
145         // so they can be deleted after tests have been run.
146         allResourceIdsCreated.add(extractId(res));
147     }
148
149
150     /*
151      * Tests to diagnose and verify the fixed status of CSPACE-1026,
152      * "Whitespace at certain points in payload cause failure"
153      */
154     /**
155      * Creates the from xml cambridge.
156      *
157      * @param testName the test name
158      * @throws Exception the exception
159      */
160     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
161         dependsOnMethods = {"create", "testSubmitRequest"})
162     public void createFromXmlCambridge(String testName) throws Exception {
163         String newId =
164             createFromXmlFile(testName, "./test-data/testCambridge.xml", true);
165         testSubmitRequest(newId);
166     }
167
168    /*
169     * Tests to diagnose and fix CSPACE-2242.
170     *
171     * This is a bug identified in release 0.8 in which value instances of a
172     * repeatable field are not stored when the first value instance of that
173     * field is blank.
174     */
175
176     // Verify that record creation occurs successfully when the first value instance
177     // of a single, repeatable String scalar field is non-blank.
178     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
179         dependsOnMethods = {"create", "testSubmitRequest"}, groups = {"cspace2242group"})
180     public void createFromXmlNonBlankFirstValueInstance(String testName) throws Exception {
181         if (logger.isDebugEnabled()) {
182             logger.debug(testBanner(testName, CLASS_NAME));
183         }
184         String newId =
185             createFromXmlFile(testName, "./test-data/cspace-2242-first-value-instance-nonblank.xml", true);
186         CollectionobjectsCommon collectionObject = readCollectionObjectCommonPart(newId);
187         // Verify that at least one value instance of the repeatable field was successfully persisted.
188         BriefDescriptionList descriptionList = collectionObject.getBriefDescriptions();
189         List<String> descriptions = descriptionList.getBriefDescription();
190         Assert.assertTrue(descriptions.size() > 0);
191     }
192
193     // Verify that record creation occurs successfully when the first value instance
194     // of a single, repeatable String scalar field is blank.
195     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
196         dependsOnMethods = {"create", "testSubmitRequest"}, groups = {"cspace2242group"})
197     public void createFromXmlBlankFirstValueInstance(String testName) throws Exception {
198         if (logger.isDebugEnabled()) {
199             logger.debug(testBanner(testName, CLASS_NAME));
200         }
201         String newId =
202             createFromXmlFile(testName, "./test-data/cspace-2242-first-value-instance-blank.xml", true);
203         CollectionobjectsCommon collectionObject = readCollectionObjectCommonPart(newId);
204         // Verify that at least one value instance of the repeatable field was successfully persisted.
205         BriefDescriptionList descriptionList = collectionObject.getBriefDescriptions();
206         List<String> descriptions = descriptionList.getBriefDescription();
207         Assert.assertTrue(descriptions.size() > 0);
208     }
209
210     /**
211      * Creates the from xml rfw s1.
212      *
213      * @param testName the test name
214      * @throws Exception the exception
215      */
216     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
217         dependsOnMethods = {"create", "testSubmitRequest"})
218     public void createFromXmlRFWS1(String testName) throws Exception {
219         String testDataDir = System.getProperty("test-data.fileName");
220         String newId =
221             //createFromXmlFile(testName, "./target/test-classes/test-data/repfield_whitesp1.xml", false);
222                 createFromXmlFile(testName, testDataDir + "/repfield_whitesp1.xml", false);
223         testSubmitRequest(newId);
224     }
225
226     /**
227      * Creates the from xml rfw s2.
228      *
229      * @param testName the test name
230      * @throws Exception the exception
231      */
232     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
233         dependsOnMethods = {"create", "testSubmitRequest"})
234     public void createFromXmlRFWS2(String testName) throws Exception {
235         String testDataDir = System.getProperty("test-data.fileName");
236         String newId =
237             //createFromXmlFile(testName, "./target/test-classes/test-data/repfield_whitesp2.xml", false);
238                 createFromXmlFile(testName, testDataDir + "/repfield_whitesp2.xml", false);
239         testSubmitRequest(newId);
240     }
241
242     /**
243      * Creates the from xml rfw s3.
244      *
245      * @param testName the test name
246      * @throws Exception the exception
247      */
248     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
249         dependsOnMethods = {"create", "testSubmitRequest"})
250     public void createFromXmlRFWS3(String testName) throws Exception {
251         String testDataDir = System.getProperty("test-data.fileName");
252         String newId =
253             //createFromXmlFile(testName, "./target/test-classes/test-data/repfield_whitesp3.xml", false);
254                 createFromXmlFile(testName, testDataDir + "/repfield_whitesp3.xml", false);
255         testSubmitRequest(newId);
256     }
257
258     /**
259      * Creates the from xml rfw s4.
260      *
261      * @param testName the test name
262      * @throws Exception the exception
263      */
264     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
265         dependsOnMethods = {"create", "testSubmitRequest"})
266     public void createFromXmlRFWS4(String testName) throws Exception {
267         String testDataDir = System.getProperty("test-data.fileName");
268         String newId =
269             createFromXmlFile(testName, testDataDir + "/repfield_whitesp4.xml", false);
270         testSubmitRequest(newId);
271     }
272
273     /*
274      * Tests to diagnose and verify the fixed status of CSPACE-1248,
275      * "Wedged records created!" (i.e. records with child repeatable
276      * fields, which contain null values, can be successfully created
277      * but an error occurs on trying to retrieve those records).
278      */
279
280     /**
281      * Creates a CollectionObject resource with a null value repeatable field.
282      *
283      * @param testName the test name
284      * @throws Exception the exception
285      */
286     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
287         dependsOnMethods = {"create", "testSubmitRequest"})
288     public void createWithNullValueRepeatableField(String testName) throws Exception {
289         String testDataDir = System.getProperty("test-data.fileName");
290         String newId =
291             createFromXmlFile(testName, testDataDir + "/repfield_null1.xml", false);
292         if (logger.isDebugEnabled()) {
293             logger.debug("Successfully created record with null value repeatable field.");
294             logger.debug("Attempting to retrieve just-created record ...");
295         }
296         testSubmitRequest(newId);
297     }
298     
299     /* (non-Javadoc)
300      * @see org.collectionspace.services.client.test.ServiceTest#createList()
301      */
302     @Override
303     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
304     dependsOnMethods = {"create"})
305     public void createList(String testName) throws Exception {
306         this.createPaginatedList(testName, DEFAULT_LIST_SIZE);
307     }
308
309     // Failure outcomes
310     // Placeholders until the three tests below can be uncommented.
311     // See Issue CSPACE-401.
312     /* (non-Javadoc)
313      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
314      */
315     @Override
316     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
317     public void createWithEmptyEntityBody(String testName) throws Exception {
318         //FIXME: Should this test really be empty?
319     }
320
321    /**
322     * Test how the service handles XML that is not well formed,
323     * when sent in the payload of a Create request.
324     *
325     * @param testName  The name of this test method.  This name is supplied
326     *     automatically, via reflection, by a TestNG 'data provider' in
327     *     a base class.
328     */
329     @Override
330     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
331     public void createWithMalformedXml(String testName) throws Exception {
332     }
333
334     /* (non-Javadoc)
335      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
336      */
337     @Override
338     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
339     public void createWithWrongXmlSchema(String testName) throws Exception {
340         //FIXME: Should this test really be empty?
341     }
342
343
344 /*
345     @Override
346     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
347     dependsOnMethods = {"create", "testSubmitRequest"})
348     public void createWithEmptyEntityBody(String testName) throwsException {
349
350         if (logger.isDebugEnabled()) {
351             logger.debug(testBanner(testName, CLASS_NAME));
352         }
353         // Perform setup.
354         setupCreateWithEmptyEntityBody();
355
356         // Submit the request to the service and store the response.
357         String method = REQUEST_TYPE.httpMethodName();
358         String url = getServiceRootURL();
359         String mediaType = MediaType.APPLICATION_XML;
360         final String entity = "";
361         int statusCode = submitRequest(method, url, mediaType, entity);
362
363         // Check the status code of the response: does it match
364         // the expected response(s)?
365         if(logger.isDebugEnabled()){
366         logger.debug(testName + ": url=" + url +
367         " status=" + statusCode);
368         }
369         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
370         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
371         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
372     }
373
374     @Override
375     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
376     dependsOnMethods = {"create", "testSubmitRequest"})
377     public void createWithMalformedXml(String testName) throws Exception {
378
379         if (logger.isDebugEnabled()) {
380             logger.debug(testBanner(testName, CLASS_NAME));
381         }
382         // Perform setup.
383         setupCreateWithMalformedXml();
384
385         // Submit the request to the service and store the response.
386         String method = REQUEST_TYPE.httpMethodName();
387         String url = getServiceRootURL();
388         String mediaType = MediaType.APPLICATION_XML;
389         final String entity = MALFORMED_XML_DATA; // Constant from base class.
390         int statusCode = submitRequest(method, url, mediaType, entity);
391
392         // Check the status code of the response: does it match
393         // the expected response(s)?
394         if(logger.isDebugEnabled()){
395         logger.debug(testName + ": url=" + url +
396         " status=" + statusCode);
397         }
398         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
399         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
400         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
401     }
402
403     @Override
404     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
405     dependsOnMethods = {"create", "testSubmitRequest"})
406     public void createWithWrongXmlSchema(String testName) throws Exception {
407
408         if (logger.isDebugEnabled()) {
409             logger.debug(testBanner(testName, CLASS_NAME));
410         }
411         // Perform setup.
412         setupCreateWithWrongXmlSchema();
413
414         // Submit the request to the service and store the response.
415         String method = REQUEST_TYPE.httpMethodName();
416         String url = getServiceRootURL();
417         String mediaType = MediaType.APPLICATION_XML;
418         final String entity = WRONG_XML_SCHEMA_DATA;
419         int statusCode = submitRequest(method, url, mediaType, entity);
420
421         // Check the status code of the response: does it match
422         // the expected response(s)?
423         if(logger.isDebugEnabled()){
424         logger.debug(testName + ": url=" + url +
425         " status=" + statusCode);
426         }
427         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
428         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
429         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
430     }
431 */
432
433    /**
434     * Test how the service handles, in a Create request, payloads
435     * containing null values (or, in the case of String fields,
436     * empty String values) in one or more fields which must be
437     * present and are required to contain non-empty values.
438     *
439     * This is a test of code and/or configuration in the service's
440     * validation routine(s).
441     *
442     * @param testName  The name of this test method.  This name is supplied
443     *     automatically, via reflection, by a TestNG 'data provider' in
444     *     a base class.
445     * @throws Exception 
446     */
447     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
448     public void createWithRequiredValuesNullOrEmpty(String testName) throws Exception {
449         if (logger.isDebugEnabled()) {
450             logger.debug(testBanner(testName, CLASS_NAME));
451         }
452         setupCreate();
453
454         // Build a payload with invalid content, by omitting a
455         // field (objectNumber) which must be present, and in which
456         // a non-empty value is required, as enforced by the service's
457         // validation routine(s).
458         CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
459         collectionObject.setTitle("atitle");
460         collectionObject.setObjectName("some name");
461
462         // Submit the request to the service and store the response.
463         CollectionObjectClient client = new CollectionObjectClient();
464         MultipartOutput multipart =
465                 createCollectionObjectInstance(client.getCommonPartName(), collectionObject, null);
466         ClientResponse<Response> res = client.create(multipart);
467         int statusCode = res.getStatus();
468
469         // Read the response and verify that the create attempt failed.
470         if (logger.isDebugEnabled()) {
471             logger.debug(testName + ": status = " + statusCode);
472         }
473         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
474                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
475         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
476
477         // FIXME: Consider splitting off the following into its own test method.
478         
479         // Build a payload with invalid content, by setting a value to the
480         // empty String, in a field that requires a non-empty value,
481         // as enforced by the service's validation routine(s).
482         collectionObject = new CollectionobjectsCommon();
483         collectionObject.setTitle("atitle");
484         collectionObject.setObjectName("some name");
485         collectionObject.setObjectNumber("");
486
487         // Submit the request to the service and store the response.
488         multipart =
489             createCollectionObjectInstance(client.getCommonPartName(), collectionObject, null);
490         res = client.create(multipart);
491         statusCode = res.getStatus();
492
493         // Read the response and verify that the create attempt failed.
494         if (logger.isDebugEnabled()) {
495             logger.debug(testName + ": status = " + statusCode);
496         }
497         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
498                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
499         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
500
501     }
502
503
504     // ---------------------------------------------------------------
505     // CRUD tests : READ tests
506     // ---------------------------------------------------------------
507     // Success outcomes
508     /* (non-Javadoc)
509      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
510      */
511     @Override
512     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
513     dependsOnMethods = {"create"})
514     public void read(String testName) throws Exception {
515
516         if (logger.isDebugEnabled()) {
517             logger.debug(testBanner(testName, CLASS_NAME));
518         }
519         // Perform setup.
520         setupRead();
521
522         // Submit the request to the service and store the response.
523         CollectionObjectClient client = new CollectionObjectClient();
524         ClientResponse<MultipartInput> res = client.read(knownResourceId);
525         int statusCode = res.getStatus();
526
527         // Check the status code of the response: does it match
528         // the expected response(s)?
529         if (logger.isDebugEnabled()) {
530             logger.debug(testName + ": status = " + statusCode);
531         }
532         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
533                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
534         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
535
536         MultipartInput input = (MultipartInput) res.getEntity();
537
538         if (logger.isDebugEnabled()) {
539             logger.debug(testName + ": Reading Common part ...");
540         }
541         CollectionobjectsCommon collectionObject =
542                 (CollectionobjectsCommon) extractPart(input,
543                 client.getCommonPartName(), CollectionobjectsCommon.class);
544         Assert.assertNotNull(collectionObject);
545
546         if (logger.isDebugEnabled()) {
547             logger.debug(testName + ": Reading Natural History part ...");
548         }
549         CollectionobjectsNaturalhistory conh =
550                 (CollectionobjectsNaturalhistory) extractPart(input,
551                 getNHPartName(), CollectionobjectsNaturalhistory.class);
552         Assert.assertNotNull(conh);
553     }
554
555     // Failure outcomes
556     /* (non-Javadoc)
557      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
558      */
559     @Override
560     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
561     dependsOnMethods = {"read"})
562     public void readNonExistent(String testName) throws Exception {
563
564         if (logger.isDebugEnabled()) {
565             logger.debug(testBanner(testName, CLASS_NAME));
566         }
567         // Perform setup.
568         setupReadNonExistent();
569
570         // Submit the request to the service and store the response.
571         CollectionObjectClient client = new CollectionObjectClient();
572         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
573         int statusCode = res.getStatus();
574
575         // Check the status code of the response: does it match
576         // the expected response(s)?
577         if (logger.isDebugEnabled()) {
578             logger.debug(testName + ": status = " + statusCode);
579         }
580         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
581                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
582         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
583     }
584
585     
586     // ---------------------------------------------------------------
587     // CRUD tests : READ_LIST tests
588     // ---------------------------------------------------------------
589     // Success outcomes
590     /* (non-Javadoc)
591      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
592      */
593     @Override
594     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
595     dependsOnMethods = {"createList", "read"})
596     public void readList(String testName) throws Exception {
597
598         if (logger.isDebugEnabled()) {
599             logger.debug(testBanner(testName, CLASS_NAME));
600         }
601         // Perform setup.
602         setupReadList();
603
604         // Submit the request to the service and store the response.
605         CollectionObjectClient client = new CollectionObjectClient();
606         ClientResponse<CollectionobjectsCommonList> res = client.readList();
607         CollectionobjectsCommonList list = res.getEntity();
608         int statusCode = res.getStatus();
609
610         // Check the status code of the response: does it match
611         // the expected response(s)?
612         if (logger.isDebugEnabled()) {
613             logger.debug(testName + ": status = " + statusCode);
614         }
615         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
616                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
617         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
618
619         // Optionally output additional data about list members for debugging.
620         boolean iterateThroughList = false;
621         if (iterateThroughList && logger.isDebugEnabled()) {
622             List<CollectionobjectsCommonList.CollectionObjectListItem> items =
623                     list.getCollectionObjectListItem();
624             int i = 0;
625
626             for (CollectionobjectsCommonList.CollectionObjectListItem item : items) {
627                 logger.debug(testName + ": list-item[" + i + "] csid="
628                         + item.getCsid());
629                 logger.debug(testName + ": list-item[" + i + "] objectNumber="
630                         + item.getObjectNumber());
631                 logger.debug(testName + ": list-item[" + i + "] URI="
632                         + item.getUri());
633                 i++;
634
635             }
636         }
637     }
638
639     // Failure outcomes
640     // None at present.
641     // ---------------------------------------------------------------
642     // CRUD tests : UPDATE tests
643     // ---------------------------------------------------------------
644     // Success outcomes
645     /* (non-Javadoc)
646      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
647      */
648     @Override
649     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
650     dependsOnMethods = {"read"})
651     public void update(String testName) throws Exception {
652
653         if (logger.isDebugEnabled()) {
654             logger.debug(testBanner(testName, CLASS_NAME));
655         }
656         // Perform setup.
657         setupUpdate();
658
659         // Read an existing resource that will be updated.
660         ClientResponse<MultipartInput> res = updateRetrieve(testName, knownResourceId);
661
662         // Extract its common part.
663         CollectionObjectClient client = new CollectionObjectClient();
664         MultipartInput input = (MultipartInput) res.getEntity();
665         CollectionobjectsCommon collectionObject =
666                 (CollectionobjectsCommon) extractPart(input,
667                 client.getCommonPartName(), CollectionobjectsCommon.class);
668         Assert.assertNotNull(collectionObject);
669
670         // Change the content of one or more fields in the common part.
671         collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
672         collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
673         if (logger.isDebugEnabled()) {
674             logger.debug("sparse update that will be sent in update request:");
675             logger.debug(objectAsXmlString(collectionObject,
676                     CollectionobjectsCommon.class));
677         }
678
679         // Send the changed resource to be updated.
680         res = updateSend(testName, knownResourceId, collectionObject);
681         int statusCode = res.getStatus();
682         // Check the status code of the response: does it match the expected response(s)?
683         if (logger.isDebugEnabled()) {
684             logger.debug(testName + ": status = " + statusCode);
685         }
686         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
687                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
688         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
689
690         // Read the response and verify that the resource was correctly updated.
691         input = (MultipartInput) res.getEntity();
692         CollectionobjectsCommon updatedCollectionObject =
693                 (CollectionobjectsCommon) extractPart(input,
694                 client.getCommonPartName(), CollectionobjectsCommon.class);
695         Assert.assertNotNull(updatedCollectionObject);
696         Assert.assertEquals(updatedCollectionObject.getObjectName(),
697                 collectionObject.getObjectName(),
698                 "Data in updated object did not match submitted data.");
699
700     }
701
702     /**
703      * Update retrieve.
704      *
705      * @param testName the test name
706      * @param id the id
707      * @return the client response
708      */
709     private ClientResponse<MultipartInput> updateRetrieve(String testName, String id) {
710         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
711         CollectionObjectClient client = new CollectionObjectClient();
712         ClientResponse<MultipartInput> res = client.read(id);
713         if (logger.isDebugEnabled()) {
714             logger.debug("read in updateRetrieve for " + testName + " status = " + res.getStatus());
715         }
716         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS);
717         if (logger.isDebugEnabled()) {
718             logger.debug("got object to updateRetrieve for " + testName + " with ID: " + id);
719         }
720         return res;
721     }
722
723     /**
724      * Update send.
725      *
726      * @param testName the test name
727      * @param id the id
728      * @param collectionObject the collection object
729      * @return the client response
730      */
731     private ClientResponse<MultipartInput> updateSend(String testName, String id,
732             CollectionobjectsCommon collectionObject) {
733         MultipartOutput output = new MultipartOutput();
734         OutputPart commonPart = output.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
735         CollectionObjectClient client = new CollectionObjectClient();
736         commonPart.getHeaders().add("label", client.getCommonPartName());
737         ClientResponse<MultipartInput> res = client.update(knownResourceId, output);
738         return res;
739     }
740
741     // Failure outcomes
742     // Placeholders until the three tests below can be uncommented.
743     // See Issue CSPACE-401.
744     /* (non-Javadoc)
745      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
746      */
747     @Override
748     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
749     dependsOnMethods = {"read"})
750     public void updateWithEmptyEntityBody(String testName) throws Exception {
751         //FIXME: Should this test really be empty?
752     }
753
754    /**
755     * Test how the service handles XML that is not well formed,
756     * when sent in the payload of an Update request.
757     *
758     * @param testName  The name of this test method.  This name is supplied
759     *     automatically, via reflection, by a TestNG 'data provider' in
760     *     a base class.
761     */
762     @Override
763     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
764     dependsOnMethods = {"read"})
765     public void updateWithMalformedXml(String testName) throws Exception {
766         //FIXME: Should this test really be empty?
767     }
768
769     /* (non-Javadoc)
770      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
771      */
772     @Override
773     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
774     dependsOnMethods = {"read"})
775     public void updateWithWrongXmlSchema(String testName) throws Exception {
776         //FIXME: Should this test really be empty?
777     }
778
779 /*
780     @Override
781     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
782     dependsOnMethods = {"create", "update", "testSubmitRequest"})
783     public void updateWithEmptyEntityBody(String testName) throws Exception {
784
785         if (logger.isDebugEnabled()) {
786             logger.debug(testBanner(testName, CLASS_NAME));
787         }
788         // Perform setup.
789         setupUpdateWithEmptyEntityBody();
790
791         // Submit the request to the service and store the response.
792         String method = REQUEST_TYPE.httpMethodName();
793         String url = getResourceURL(knownResourceId);
794         String mediaType = MediaType.APPLICATION_XML;
795         final String entity = "";
796         int statusCode = submitRequest(method, url, mediaType, entity);
797
798         // Check the status code of the response: does it match
799         // the expected response(s)?
800         if(logger.isDebugEnabled()){
801         logger.debug(testName + ": url=" + url +
802         " status=" + statusCode);
803         }
804         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
805         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
806         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
807     }
808
809     @Override
810     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
811     dependsOnMethods = {"create", "update", "testSubmitRequest"})
812     public void updateWithMalformedXml() throws Exception {
813
814         if (logger.isDebugEnabled()) {
815             logger.debug(testBanner(testName, CLASS_NAME));
816         }
817         // Perform setup.
818         setupUpdateWithMalformedXml();
819
820         // Submit the request to the service and store the response.
821         String method = REQUEST_TYPE.httpMethodName();
822         String url = getResourceURL(knownResourceId);
823         final String entity = MALFORMED_XML_DATA;
824         String mediaType = MediaType.APPLICATION_XML;
825         int statusCode = submitRequest(method, url, mediaType, entity);
826
827         // Check the status code of the response: does it match
828         // the expected response(s)?
829         if(logger.isDebugEnabled()){
830         logger.debug(testName + ": url=" + url +
831         " status=" + statusCode);
832         }
833         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
834         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
835         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
836     }
837
838     @Override
839     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
840     dependsOnMethods = {"create", "update", "testSubmitRequest"})
841     public void updateWithWrongXmlSchema(String testName) throws Exception {
842
843         if (logger.isDebugEnabled()) {
844             logger.debug(testBanner(testName, CLASS_NAME));
845         }
846         // Perform setup.
847         setupUpdateWithWrongXmlSchema();
848
849         // Submit the request to the service and store the response.
850         String method = REQUEST_TYPE.httpMethodName();
851         String url = getResourceURL(knownResourceId);
852         String mediaType = MediaType.APPLICATION_XML;
853         final String entity = WRONG_XML_SCHEMA_DATA;
854         int statusCode = submitRequest(method, url, mediaType, entity);
855
856         // Check the status code of the response: does it match
857         // the expected response(s)?
858         if(logger.isDebugEnabled()){
859         logger.debug(testName + ": url=" + url +
860         " status=" + statusCode);
861         }
862         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
863         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
864         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
865     }
866 */
867
868     /* (non-Javadoc)
869  * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
870  */
871 @Override
872     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
873     dependsOnMethods = {"update", "testSubmitRequest"})
874     public void updateNonExistent(String testName) throws Exception {
875
876         if (logger.isDebugEnabled()) {
877             logger.debug(testBanner(testName, CLASS_NAME));
878         }
879         // Perform setup.
880         setupUpdateNonExistent();
881
882         // Submit the request to the service and store the response.
883         //
884         // Note: The ID used in this 'create' call may be arbitrary.
885         // The only relevant ID may be the one used in updateCollectionObject(), below.
886         CollectionObjectClient client = new CollectionObjectClient();
887         MultipartOutput multipart =
888                 createCollectionObjectInstance(client.getCommonPartName(),
889                 NON_EXISTENT_ID);
890         ClientResponse<MultipartInput> res =
891                 client.update(NON_EXISTENT_ID, multipart);
892         int statusCode = res.getStatus();
893
894         // Check the status code of the response: does it match
895         // the expected response(s)?
896         if (logger.isDebugEnabled()) {
897             logger.debug(testName + ": status = " + statusCode);
898         }
899         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
900                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
901         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
902     }
903
904    /**
905     * Test how the service handles, in an Update request, payloads
906     * containing null values (or, in the case of String fields,
907     * empty String values) in one or more fields in which non-empty
908     * values are required.
909     *
910     * This is a test of code and/or configuration in the service's
911     * validation routine(s).
912     *
913     * @param testName  The name of this test method.  This name is supplied
914     *     automatically, via reflection, by a TestNG 'data provider' in
915     *     a base class.
916  * @throws Exception 
917     */
918     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
919     dependsOnMethods = {"read"})
920     public void updateWithRequiredValuesNullOrEmpty(String testName) throws Exception {
921   
922         if (logger.isDebugEnabled()) {
923             logger.debug(testBanner(testName, CLASS_NAME));
924         }
925         // Perform setup.
926         setupUpdate();
927         if (logger.isDebugEnabled()) {
928             logger.debug(testName + " got object to update with ID: " + knownResourceId);
929         }
930
931         // Read an existing record for updating.
932         ClientResponse<MultipartInput> res = updateRetrieve(testName, knownResourceId);
933
934         CollectionObjectClient client = new CollectionObjectClient();
935         MultipartInput input = (MultipartInput) res.getEntity();
936         CollectionobjectsCommon collectionObject =
937                 (CollectionobjectsCommon) extractPart(input,
938                 client.getCommonPartName(), CollectionobjectsCommon.class);
939         Assert.assertNotNull(collectionObject);
940
941         // Update with invalid content, by setting a value to the
942         // empty String, in a field that requires a non-empty value,
943         // as enforced by the service's validation routine(s).
944         collectionObject.setObjectNumber("");
945
946         if (logger.isDebugEnabled()) {
947             logger.debug(testName + " updated object");
948             logger.debug(objectAsXmlString(collectionObject,
949                     CollectionobjectsCommon.class));
950         }
951
952         // Submit the request to the service and store the response.
953         res = updateSend(testName, knownResourceId, collectionObject);
954         int statusCode = res.getStatus();
955
956         // Read the response and verify that the update attempt failed.
957         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
958                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
959         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
960
961     }
962
963     // ---------------------------------------------------------------
964     // CRUD tests : DELETE tests
965     // ---------------------------------------------------------------
966     // Success outcomes
967     /* (non-Javadoc)
968      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
969      */
970     @Override
971     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
972     dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
973     public void delete(String testName) throws Exception {
974
975         if (logger.isDebugEnabled()) {
976             logger.debug(testBanner(testName, CLASS_NAME));
977         }
978         // Perform setup.
979         setupDelete();
980
981         // Submit the request to the service and store the response.
982         CollectionObjectClient client = new CollectionObjectClient();
983         ClientResponse<Response> res = client.delete(knownResourceId);
984         int statusCode = res.getStatus();
985
986         // Check the status code of the response: does it match
987         // the expected response(s)?
988         if (logger.isDebugEnabled()) {
989             logger.debug(testName + ": status = " + statusCode);
990         }
991         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
992                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
993         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
994     }
995
996     // Failure outcomes
997     /* (non-Javadoc)
998      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
999      */
1000     @Override
1001     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1002     dependsOnMethods = {"delete"})
1003     public void deleteNonExistent(String testName) throws Exception {
1004
1005         if (logger.isDebugEnabled()) {
1006             logger.debug(testBanner(testName, CLASS_NAME));
1007         }
1008         // Perform setup.
1009         setupDeleteNonExistent();
1010
1011         // Submit the request to the service and store the response.
1012         CollectionObjectClient client = new CollectionObjectClient();
1013         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
1014         int statusCode = res.getStatus();
1015
1016         // Check the status code of the response: does it match
1017         // the expected response(s)?
1018         if (logger.isDebugEnabled()) {
1019             logger.debug(testName + ": status = " + statusCode);
1020         }
1021         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1022                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1023         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1024     }
1025
1026     // ---------------------------------------------------------------
1027     // Utility tests : tests of code used in tests above
1028     // ---------------------------------------------------------------
1029     /**
1030      * Tests the code for manually submitting data that is used by several
1031      * of the methods above.
1032      * @throws Exception 
1033      */
1034
1035     @Test(dependsOnMethods = {"create", "read"})
1036     public void testSubmitRequest() throws Exception {
1037         testSubmitRequest(knownResourceId);
1038     }
1039
1040     /**
1041      * Test submit request.
1042      *
1043      * @param resourceId the resource id
1044      * @throws Exception the exception
1045      */
1046     private void testSubmitRequest(String resourceId) throws Exception {
1047
1048         // Expected status code: 200 OK
1049         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1050
1051         // Submit the request to the service and store the response.
1052         String method = ServiceRequestType.READ.httpMethodName();
1053         String url = getResourceURL(resourceId);
1054         int statusCode = submitRequest(method, url);
1055
1056         // Check the status code of the response: does it match
1057         // the expected response(s)?
1058         if (logger.isDebugEnabled()) {
1059             logger.debug("testSubmitRequest: url=" + url
1060                     + " status=" + statusCode);
1061         }
1062         Assert.assertEquals(statusCode, EXPECTED_STATUS);
1063
1064     }
1065
1066     // ---------------------------------------------------------------
1067     // Utility methods used by tests above
1068     // ---------------------------------------------------------------
1069     /**
1070      * Creates the collection object instance.
1071      *
1072      * @param commonPartName the common part name
1073      * @param identifier the identifier
1074      * @return the multipart output
1075      */
1076     private MultipartOutput createCollectionObjectInstance(String commonPartName,
1077             String identifier) {
1078         return createCollectionObjectInstance(commonPartName,
1079                 "objectNumber-" + identifier,
1080                 "objectName-" + identifier);
1081     }
1082
1083     /**
1084      * Creates the collection object instance.
1085      *
1086      * @param commonPartName the common part name
1087      * @param objectNumber the object number
1088      * @param objectName the object name
1089      * @return the multipart output
1090      */
1091     private MultipartOutput createCollectionObjectInstance(String commonPartName,
1092             String objectNumber, String objectName) {
1093         CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
1094
1095         BriefDescriptionList descriptionList = new BriefDescriptionList();
1096         List<String> descriptions = descriptionList.getBriefDescription();
1097         descriptions.add("Papier mache bird cow mask with horns, "
1098                 + "painted red with black and yellow spots. "
1099                 + "Puerto Rico. ca. 8&quot; high, 6&quot; wide, projects 10&quot; (with horns).");
1100         descriptions.add("Papier mache bird cow mask with horns, "
1101                 + "painted red with black and yellow spots. "
1102                 + "Puerto Rico. ca. 8&quot; high, 6&quot; wide, projects 10&quot; (with horns).");
1103
1104         ResponsibleDepartmentList deptList = new ResponsibleDepartmentList();
1105         List<String> depts = deptList.getResponsibleDepartment();
1106         // @TODO Use properly formatted refNames for representative departments
1107         // in this example test record. The following are mere placeholders.
1108         depts.add("urn:org.collectionspace.services.department:Registrar");
1109         depts.add("urn:org.walkerart.department:Fine Art");
1110
1111         OtherNumberList otherNumList = new OtherNumberList();
1112         List<OtherNumber> otherNumbers = otherNumList.getOtherNumber();
1113         
1114         OtherNumber otherNumber1 = new OtherNumber();        
1115         otherNumber1.setNumberValue("101." + objectName);
1116         otherNumber1.setNumberType("integer");
1117         otherNumbers.add(otherNumber1);
1118         
1119         OtherNumber otherNumber2 = new OtherNumber();
1120         otherNumber2.setNumberValue("101.502.23.456." + objectName);
1121         otherNumber2.setNumberType("ipaddress");
1122         otherNumbers.add(otherNumber2);        
1123         
1124         //FIXME: Title does not need to be set.
1125         collectionObject.setTitle("atitle");
1126         collectionObject.setResponsibleDepartments(deptList);
1127         collectionObject.setObjectNumber(objectNumber);
1128         
1129         collectionObject.setOtherNumberList(otherNumList);
1130
1131         // FIXME this can be removed when the repeatable other number list
1132         // is supported by the application layers
1133         collectionObject.setOtherNumber("urn:org.walkerart.id:123");
1134         
1135         collectionObject.setObjectName(objectName);
1136         collectionObject.setAge(""); //test for null string
1137         collectionObject.setBriefDescriptions(descriptionList);
1138
1139         CollectionobjectsNaturalhistory conh = new CollectionobjectsNaturalhistory();
1140         conh.setNhString("test-string");
1141         conh.setNhInt(999);
1142         conh.setNhLong(9999);
1143
1144
1145         MultipartOutput multipart = createCollectionObjectInstance(commonPartName, collectionObject, conh);
1146         return multipart;
1147     }
1148
1149     /**
1150      * Creates the collection object instance.
1151      *
1152      * @param commonPartName the common part name
1153      * @param collectionObject the collection object
1154      * @param conh the conh
1155      * @return the multipart output
1156      */
1157     private MultipartOutput createCollectionObjectInstance(String commonPartName,
1158             CollectionobjectsCommon collectionObject, CollectionobjectsNaturalhistory conh) {
1159
1160         MultipartOutput multipart = CollectionObjectFactory.createCollectionObjectInstance(
1161                 commonPartName, collectionObject, getNHPartName(), conh);
1162         if (logger.isDebugEnabled()) {
1163             logger.debug("to be created, collectionobject common");
1164             logger.debug(objectAsXmlString(collectionObject,
1165                     CollectionobjectsCommon.class));
1166         }
1167
1168         if (conh != null) {
1169             if (logger.isDebugEnabled()) {
1170                 logger.debug("to be created, collectionobject nhistory");
1171                 logger.debug(objectAsXmlString(conh,
1172                         CollectionobjectsNaturalhistory.class));
1173             }
1174         }
1175         return multipart;
1176
1177     }
1178
1179     /**
1180      * createCollectionObjectInstanceFromXml uses JAXB unmarshaller to retrieve
1181      * collectionobject from given file
1182      * @param commonPartName
1183      * @param commonPartFileName
1184      * @return
1185      * @throws Exception
1186      */
1187     private MultipartOutput createCollectionObjectInstanceFromXml(String testName, String commonPartName,
1188             String commonPartFileName) throws Exception {
1189
1190         CollectionobjectsCommon collectionObject =
1191                 (CollectionobjectsCommon) getObjectFromFile(CollectionobjectsCommon.class,
1192                 commonPartFileName);
1193         MultipartOutput multipart = new MultipartOutput();
1194         OutputPart commonPart = multipart.addPart(collectionObject,
1195                 MediaType.APPLICATION_XML_TYPE);
1196         commonPart.getHeaders().add("label", commonPartName);
1197
1198         if (logger.isDebugEnabled()) {
1199             logger.debug(testName + " to be created, collectionobject common");
1200             logger.debug(objectAsXmlString(collectionObject,
1201                     CollectionobjectsCommon.class));
1202         }
1203         return multipart;
1204
1205     }
1206
1207     /**
1208      * createCollectionObjectInstanceFromRawXml uses stringified collectionobject
1209      * retrieve from given file
1210      * @param commonPartName
1211      * @param commonPartFileName
1212      * @return
1213      * @throws Exception
1214      */
1215     private MultipartOutput createCollectionObjectInstanceFromRawXml(String testName, String commonPartName,
1216             String commonPartFileName) throws Exception {
1217
1218         MultipartOutput multipart = new MultipartOutput();
1219         String stringObject = getXmlDocumentAsString(commonPartFileName);
1220         if (logger.isDebugEnabled()) {
1221             logger.debug(testName + " to be created, collectionobject common " + "\n" + stringObject);
1222         }
1223         OutputPart commonPart = multipart.addPart(stringObject,
1224                 MediaType.APPLICATION_XML_TYPE);
1225         commonPart.getHeaders().add("label", commonPartName);
1226
1227         return multipart;
1228
1229     }
1230
1231     /**
1232      * Gets the nH part name.
1233      *
1234      * @return the nH part name
1235      */
1236     private String getNHPartName() {
1237         return "collectionobjects_naturalhistory";
1238     }
1239
1240     /**
1241      * Creates the from xml file.
1242      *
1243      * @param testName the test name
1244      * @param fileName the file name
1245      * @param useJaxb the use jaxb
1246      * @return the string
1247      * @throws Exception the exception
1248      */
1249     private String createFromXmlFile(String testName, String fileName, boolean useJaxb) throws Exception {
1250   
1251         // Perform setup.
1252         setupCreate();
1253
1254         MultipartOutput multipart = null;
1255
1256         CollectionObjectClient client = new CollectionObjectClient();
1257         if (useJaxb) {
1258             multipart = createCollectionObjectInstanceFromXml(testName,
1259                     client.getCommonPartName(), fileName);
1260         } else {
1261             multipart = createCollectionObjectInstanceFromRawXml(testName,
1262                     client.getCommonPartName(), fileName);
1263         }
1264         ClientResponse<Response> res = client.create(multipart);
1265         int statusCode = res.getStatus();
1266
1267         if (logger.isDebugEnabled()) {
1268             logger.debug(testName + ": status = " + statusCode);
1269         }
1270         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1271                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1272         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1273         String newId = extractId(res);
1274         allResourceIdsCreated.add(newId);
1275         return newId;
1276     }
1277
1278     // FIXME: This duplicates code in read(), and should be consolidated.
1279     // This is an expedient to support reading and verifying the contents
1280     // of resources that have been created from test data XML files.
1281     private CollectionobjectsCommon readCollectionObjectCommonPart(String csid)
1282         throws Exception {
1283
1284         String testName = "readCollectionObjectCommonPart";
1285
1286         setupRead();
1287
1288         // Submit the request to the service and store the response.
1289         CollectionObjectClient client = new CollectionObjectClient();
1290         ClientResponse<MultipartInput> res = client.read(csid);
1291         int statusCode = res.getStatus();
1292
1293         // Check the status code of the response: does it match
1294         // the expected response(s)?
1295         if (logger.isDebugEnabled()) {
1296             logger.debug(testName + ": status = " + statusCode);
1297         }
1298         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1299                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1300         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1301
1302         MultipartInput input = (MultipartInput) res.getEntity();
1303
1304         if (logger.isDebugEnabled()) {
1305             logger.debug(testName + ": Reading Common part ...");
1306         }
1307         CollectionobjectsCommon collectionObject =
1308                 (CollectionobjectsCommon) extractPart(input,
1309                 client.getCommonPartName(), CollectionobjectsCommon.class);
1310         Assert.assertNotNull(collectionObject);
1311
1312         return collectionObject;
1313      }
1314 }