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