]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f534e4301c7d14d84cc89350b826a4a6a3c63414
[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.ArrayList;
26 import java.util.List;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29
30 import org.collectionspace.services.client.CollectionObjectClient;
31 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
32 import org.collectionspace.services.collectionobject.domain.naturalhistory.CollectionobjectsNaturalhistory;
33 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
34 import org.collectionspace.services.collectionobject.ResponsibleDepartmentList;
35 import org.jboss.resteasy.client.ClientResponse;
36
37 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
38 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
39 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
40 import org.testng.Assert;
41 import org.testng.annotations.AfterClass;
42 import org.testng.annotations.Test;
43
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * CollectionObjectServiceTest, carries out tests against a
49  * deployed and running CollectionObject Service.
50  * 
51  * $LastChangedRevision$
52  * $LastChangedDate$
53  */
54 public class CollectionObjectServiceTest extends AbstractServiceTestImpl {
55
56     private final Logger logger =
57             LoggerFactory.getLogger(CollectionObjectServiceTest.class);
58     // Instance variables specific to this test.
59     private CollectionObjectClient client = new CollectionObjectClient();
60     private String knownResourceId = null;
61     private List<String> allResourceIdsCreated = new ArrayList();
62     private boolean multivalue; //toggle
63
64     /*
65      * This method is called only by the parent class, AbstractServiceTest
66      */
67     @Override
68     protected String getServicePathComponent() {
69         return client.getServicePathComponent();
70     }
71
72     // ---------------------------------------------------------------
73     // CRUD tests : CREATE tests
74     // ---------------------------------------------------------------
75     // Success outcomes
76     @Override
77     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
78     public void create(String testName) throws Exception {
79
80         // Perform setup, such as initializing the type of service request
81         // (e.g. CREATE, DELETE), its valid and expected status codes, and
82         // its associated HTTP method name (e.g. POST, DELETE).
83         setupCreate(testName);
84
85         // Submit the request to the service and store the response.
86         String identifier = createIdentifier();
87         MultipartOutput multipart =
88                 createCollectionObjectInstance(client.getCommonPartName(), identifier);
89         ClientResponse<Response> res = client.create(multipart);
90         int statusCode = res.getStatus();
91
92         // Check the status code of the response: does it match
93         // the expected response(s)?
94         //
95         // Specifically:
96         // Does it fall within the set of valid status codes?
97         // Does it exactly match the expected status code?
98         if (logger.isDebugEnabled()) {
99             logger.debug(testName + ": status = " + statusCode);
100         }
101         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
102                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
103         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
104
105         // Store the ID returned from the first resource created
106         // for additional tests below.
107         if (knownResourceId == null) {
108             knownResourceId = extractId(res);
109             if (logger.isDebugEnabled()) {
110                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
111             }
112         }
113
114         // Store the IDs from every resource created by tests,
115         // so they can be deleted after tests have been run.
116         allResourceIdsCreated.add(extractId(res));
117     }
118
119     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
120     public void createFromXml(String testName) throws Exception {
121
122         // Perform setup, such as initializing the type of service request
123         // (e.g. CREATE, DELETE), its valid and expected status codes, and
124         // its associated HTTP method name (e.g. POST, DELETE).
125         setupCreate(testName);
126
127         // Submit the request to the service and store the response.
128         String identifier = createIdentifier();
129         MultipartOutput multipart =
130                 createCollectionObjectInstanceFromXml(client.getCommonPartName(),
131                 "test-data/testCambridge.xml");
132         ClientResponse<Response> res = client.create(multipart);
133         int statusCode = res.getStatus();
134
135         // Check the status code of the response: does it match
136         // the expected response(s)?
137         //
138         // Specifically:
139         // Does it fall within the set of valid status codes?
140         // Does it exactly match the expected status code?
141         if (logger.isDebugEnabled()) {
142             logger.debug(testName + ": status = " + statusCode);
143         }
144         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
145                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
146         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
147         allResourceIdsCreated.add(extractId(res));
148     }
149
150     /* (non-Javadoc)
151      * @see org.collectionspace.services.client.test.ServiceTest#createList()
152      */
153     @Override
154     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
155     dependsOnMethods = {"create"})
156     public void createList(String testName) throws Exception {
157         for (int i = 0; i < 3; i++) {
158             create(testName);
159         }
160     }
161
162     // Failure outcomes
163     // Placeholders until the three tests below can be uncommented.
164     // See Issue CSPACE-401.
165     @Override
166     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
167     public void createWithEmptyEntityBody(String testName) throws Exception {
168     }
169
170     @Override
171     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
172     public void createWithMalformedXml(String testName) throws Exception {
173         setupCreate(testName);
174
175         CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
176         collectionObject.setTitle("atitle");
177         //don't set objectNumber to check validation
178         collectionObject.setObjectName("some name");
179         MultipartOutput multipart =
180                 createCollectionObjectInstance(client.getCommonPartName(), collectionObject, null);
181         ClientResponse<Response> res = client.create(multipart);
182         int statusCode = res.getStatus();
183
184         if (logger.isDebugEnabled()) {
185             logger.debug(testName + ": status = " + statusCode);
186         }
187         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
188                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
189         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
190     }
191
192     @Override
193     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
194     public void createWithWrongXmlSchema(String testName) throws Exception {
195     }
196
197
198     /*
199     @Override
200     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
201     dependsOnMethods = {"create", "testSubmitRequest"})
202     public void createWithEmptyEntityBody(String testName) throwsException {
203     
204     // Perform setup.
205     setupCreateWithEmptyEntityBody(testName);
206
207     // Submit the request to the service and store the response.
208     String method = REQUEST_TYPE.httpMethodName();
209     String url = getServiceRootURL();
210     String mediaType = MediaType.APPLICATION_XML;
211     final String entity = "";
212     int statusCode = submitRequest(method, url, mediaType, entity);
213
214     // Check the status code of the response: does it match
215     // the expected response(s)?
216     if(logger.isDebugEnabled()){
217     logger.debug(testName + ": url=" + url +
218     " status=" + statusCode);
219     }
220     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
221     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
222     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
223     }
224
225     @Override
226     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
227     dependsOnMethods = {"create", "testSubmitRequest"})
228     public void createWithMalformedXml(String testName) throws Exception {
229     
230     // Perform setup.
231     setupCreateWithMalformedXml(testName);
232
233     // Submit the request to the service and store the response.
234     String method = REQUEST_TYPE.httpMethodName();
235     String url = getServiceRootURL();
236     String mediaType = MediaType.APPLICATION_XML;
237     final String entity = MALFORMED_XML_DATA; // Constant from base class.
238     int statusCode = submitRequest(method, url, mediaType, entity);
239
240     // Check the status code of the response: does it match
241     // the expected response(s)?
242     if(logger.isDebugEnabled()){
243     logger.debug(testName + ": url=" + url +
244     " status=" + statusCode);
245     }
246     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
247     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
248     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
249     }
250
251     @Override
252     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
253     dependsOnMethods = {"create", "testSubmitRequest"})
254     public void createWithWrongXmlSchema(String testName) throws Exception {
255     
256     // Perform setup.
257     setupCreateWithWrongXmlSchema(testName);
258
259     // Submit the request to the service and store the response.
260     String method = REQUEST_TYPE.httpMethodName();
261     String url = getServiceRootURL();
262     String mediaType = MediaType.APPLICATION_XML;
263     final String entity = WRONG_XML_SCHEMA_DATA;
264     int statusCode = submitRequest(method, url, mediaType, entity);
265
266     // Check the status code of the response: does it match
267     // the expected response(s)?
268     if(logger.isDebugEnabled()){
269     logger.debug(testName + ": url=" + url +
270     " status=" + statusCode);
271     }
272     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
273     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
274     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
275     }
276      */
277     // ---------------------------------------------------------------
278     // CRUD tests : READ tests
279     // ---------------------------------------------------------------
280     // Success outcomes
281     @Override
282     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
283     dependsOnMethods = {"create"})
284     public void read(String testName) throws Exception {
285
286         // Perform setup.
287         setupRead(testName);
288
289         // Submit the request to the service and store the response.
290         ClientResponse<MultipartInput> res = client.read(knownResourceId);
291         int statusCode = res.getStatus();
292
293         // Check the status code of the response: does it match
294         // the expected response(s)?
295         if (logger.isDebugEnabled()) {
296             logger.debug(testName + ": status = " + statusCode);
297         }
298         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
299                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
300         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
301
302         MultipartInput input = (MultipartInput) res.getEntity();
303
304         if (logger.isDebugEnabled()) {
305             logger.debug(testName + ": Reading Common part ...");
306         }
307         CollectionobjectsCommon collectionObject =
308                 (CollectionobjectsCommon) extractPart(input,
309                 client.getCommonPartName(), CollectionobjectsCommon.class);
310         Assert.assertNotNull(collectionObject);
311
312         if (logger.isDebugEnabled()) {
313             logger.debug(testName + ": Reading Natural History part ...");
314         }
315         CollectionobjectsNaturalhistory conh =
316                 (CollectionobjectsNaturalhistory) extractPart(input,
317                 getNHPartName(), CollectionobjectsNaturalhistory.class);
318         Assert.assertNotNull(conh);
319     }
320
321     // Failure outcomes
322     @Override
323     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
324     dependsOnMethods = {"read"})
325     public void readNonExistent(String testName) throws Exception {
326
327         // Perform setup.
328         setupReadNonExistent(testName);
329
330         // Submit the request to the service and store the response.
331         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
332         int statusCode = res.getStatus();
333
334         // Check the status code of the response: does it match
335         // the expected response(s)?
336         if (logger.isDebugEnabled()) {
337             logger.debug(testName + ": status = " + statusCode);
338         }
339         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
340                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
341         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
342     }
343
344     // ---------------------------------------------------------------
345     // CRUD tests : READ_LIST tests
346     // ---------------------------------------------------------------
347     // Success outcomes
348     @Override
349     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
350     dependsOnMethods = {"createList", "read"})
351     public void readList(String testName) throws Exception {
352
353         // Perform setup.
354         setupReadList(testName);
355
356         // Submit the request to the service and store the response.
357         ClientResponse<CollectionobjectsCommonList> res = client.readList();
358         CollectionobjectsCommonList list = res.getEntity();
359         int statusCode = res.getStatus();
360
361         // Check the status code of the response: does it match
362         // the expected response(s)?
363         if (logger.isDebugEnabled()) {
364             logger.debug(testName + ": status = " + statusCode);
365         }
366         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
367                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
368         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
369
370         // Optionally output additional data about list members for debugging.
371         boolean iterateThroughList = false;
372         if (iterateThroughList && logger.isDebugEnabled()) {
373             List<CollectionobjectsCommonList.CollectionObjectListItem> items =
374                     list.getCollectionObjectListItem();
375             int i = 0;
376
377             for (CollectionobjectsCommonList.CollectionObjectListItem item : items) {
378                 logger.debug(testName + ": list-item[" + i + "] csid="
379                         + item.getCsid());
380                 logger.debug(testName + ": list-item[" + i + "] objectNumber="
381                         + item.getObjectNumber());
382                 logger.debug(testName + ": list-item[" + i + "] URI="
383                         + item.getUri());
384                 i++;
385
386             }
387         }
388     }
389
390     // Failure outcomes
391     // None at present.
392     // ---------------------------------------------------------------
393     // CRUD tests : UPDATE tests
394     // ---------------------------------------------------------------
395     // Success outcomes
396     @Override
397     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
398     dependsOnMethods = {"read"})
399     public void update(String testName) throws Exception {
400
401         // Perform setup.
402         setupUpdate(testName);
403
404         ClientResponse<MultipartInput> res = updateRetrieve(testName, knownResourceId);
405
406         if (logger.isDebugEnabled()) {
407             logger.debug("got object to update with ID: " + knownResourceId);
408         }
409         MultipartInput input = (MultipartInput) res.getEntity();
410         CollectionobjectsCommon collectionObject =
411                 (CollectionobjectsCommon) extractPart(input,
412                 client.getCommonPartName(), CollectionobjectsCommon.class);
413         Assert.assertNotNull(collectionObject);
414
415         // Update the content of this resource.
416         collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
417         collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
418         if (logger.isDebugEnabled()) {
419             logger.debug("updated object");
420             logger.debug(objectAsXmlString(collectionObject,
421                     CollectionobjectsCommon.class));
422         }
423
424         res = updateSend(testName, knownResourceId, collectionObject);
425
426         int statusCode = res.getStatus();
427         // Check the status code of the response: does it match the expected response(s)?
428         if (logger.isDebugEnabled()) {
429             logger.debug(testName + ": status = " + statusCode);
430         }
431         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
432                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
433         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
434
435
436         input = (MultipartInput) res.getEntity();
437         CollectionobjectsCommon updatedCollectionObject =
438                 (CollectionobjectsCommon) extractPart(input,
439                 client.getCommonPartName(), CollectionobjectsCommon.class);
440         Assert.assertNotNull(updatedCollectionObject);
441
442         Assert.assertEquals(updatedCollectionObject.getObjectName(),
443                 collectionObject.getObjectName(),
444                 "Data in updated object did not match submitted data.");
445
446     }
447
448     private ClientResponse<MultipartInput> updateRetrieve(String testName, String id) {
449         ClientResponse<MultipartInput> res =
450                 client.read(id);
451         if (logger.isDebugEnabled()) {
452             logger.debug("read in updateRetrieve for " + testName + " status = " + res.getStatus());
453         }
454         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
455
456         if (logger.isDebugEnabled()) {
457             logger.debug("got object to updateRetrieve for " + testName + " with ID: " + id);
458         }
459         return res;
460     }
461
462     private ClientResponse<MultipartInput> updateSend(String testName, String id,
463             CollectionobjectsCommon collectionObject) {
464         MultipartOutput output = new MultipartOutput();
465         OutputPart commonPart = output.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
466         commonPart.getHeaders().add("label", client.getCommonPartName());
467
468         ClientResponse<MultipartInput> res = client.update(knownResourceId, output);
469         // Check the status code of the response: does it match the expected response(s)?
470         if (logger.isDebugEnabled()) {
471             logger.debug("updateSend for " + testName + ": status = " + res.getStatus());
472         }
473         return res;
474     }
475
476     // Failure outcomes
477     // Placeholders until the three tests below can be uncommented.
478     // See Issue CSPACE-401.
479     @Override
480     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
481     dependsOnMethods = {"read"})
482     public void updateWithEmptyEntityBody(String testName) throws Exception {
483     }
484
485     @Override
486     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
487     dependsOnMethods = {"read"})
488     public void updateWithMalformedXml(String testName) throws Exception {
489         // Perform setup.
490         setupUpdate(testName);
491         if (logger.isDebugEnabled()) {
492             logger.debug(testName + " got object to update with ID: " + knownResourceId);
493         }
494
495         ClientResponse<MultipartInput> res = updateRetrieve(testName, knownResourceId);
496
497         MultipartInput input = (MultipartInput) res.getEntity();
498         CollectionobjectsCommon collectionObject =
499                 (CollectionobjectsCommon) extractPart(input,
500                 client.getCommonPartName(), CollectionobjectsCommon.class);
501         Assert.assertNotNull(collectionObject);
502
503         //update with invalid content
504         collectionObject.setObjectNumber("");
505
506         if (logger.isDebugEnabled()) {
507             logger.debug(testName + " updated object");
508             logger.debug(objectAsXmlString(collectionObject,
509                     CollectionobjectsCommon.class));
510         }
511
512         // Submit the request to the service and store the response.
513         res = updateSend(testName, knownResourceId, collectionObject);
514         int statusCode = res.getStatus();
515         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
516                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
517         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
518
519     }
520
521     @Override
522     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
523     dependsOnMethods = {"read"})
524     public void updateWithWrongXmlSchema(String testName) throws Exception {
525     }
526
527     /*
528     @Override
529     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
530     dependsOnMethods = {"create", "update", "testSubmitRequest"})
531     public void updateWithEmptyEntityBody(String testName) throws Exception {
532     
533     // Perform setup.
534     setupUpdateWithEmptyEntityBody(testName);
535
536     // Submit the request to the service and store the response.
537     String method = REQUEST_TYPE.httpMethodName();
538     String url = getResourceURL(knownResourceId);
539     String mediaType = MediaType.APPLICATION_XML;
540     final String entity = "";
541     int statusCode = submitRequest(method, url, mediaType, entity);
542
543     // Check the status code of the response: does it match
544     // the expected response(s)?
545     if(logger.isDebugEnabled()){
546     logger.debug(testName + ": url=" + url +
547     " status=" + statusCode);
548     }
549     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
550     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
551     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
552     }
553
554     @Override
555     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
556     dependsOnMethods = {"create", "update", "testSubmitRequest"})
557     public void updateWithMalformedXml() throws Exception {
558
559     // Perform setup.
560     setupUpdateWithMalformedXml(testName);
561
562     // Submit the request to the service and store the response.
563     String method = REQUEST_TYPE.httpMethodName();
564     String url = getResourceURL(knownResourceId);
565     final String entity = MALFORMED_XML_DATA;
566     String mediaType = MediaType.APPLICATION_XML;
567     int statusCode = submitRequest(method, url, mediaType, entity);
568
569     // Check the status code of the response: does it match
570     // the expected response(s)?
571     if(logger.isDebugEnabled()){
572     logger.debug(testName + ": url=" + url +
573     " status=" + statusCode);
574     }
575     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
576     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
577     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
578     }
579
580     @Override
581     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
582     dependsOnMethods = {"create", "update", "testSubmitRequest"})
583     public void updateWithWrongXmlSchema(String testName) throws Exception {
584     
585     // Perform setup.
586     setupUpdateWithWrongXmlSchema(String testName);
587
588     // Submit the request to the service and store the response.
589     String method = REQUEST_TYPE.httpMethodName();
590     String url = getResourceURL(knownResourceId);
591     String mediaType = MediaType.APPLICATION_XML;
592     final String entity = WRONG_XML_SCHEMA_DATA;
593     int statusCode = submitRequest(method, url, mediaType, entity);
594
595     // Check the status code of the response: does it match
596     // the expected response(s)?
597     if(logger.isDebugEnabled()){
598     logger.debug(testName + ": url=" + url +
599     " status=" + statusCode);
600     }
601     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
602     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
603     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
604     }
605      */
606     @Override
607     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
608     dependsOnMethods = {"update", "testSubmitRequest"})
609     public void updateNonExistent(String testName) throws Exception {
610
611         // Perform setup.
612         setupUpdateNonExistent(testName);
613
614         // Submit the request to the service and store the response.
615         //
616         // Note: The ID used in this 'create' call may be arbitrary.
617         // The only relevant ID may be the one used in updateCollectionObject(), below.
618         MultipartOutput multipart =
619                 createCollectionObjectInstance(client.getCommonPartName(),
620                 NON_EXISTENT_ID);
621         ClientResponse<MultipartInput> res =
622                 client.update(NON_EXISTENT_ID, multipart);
623         int statusCode = res.getStatus();
624
625         // Check the status code of the response: does it match
626         // the expected response(s)?
627         if (logger.isDebugEnabled()) {
628             logger.debug(testName + ": status = " + statusCode);
629         }
630         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
631                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
632         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
633     }
634
635     // ---------------------------------------------------------------
636     // CRUD tests : DELETE tests
637     // ---------------------------------------------------------------
638     // Success outcomes
639     @Override
640     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
641     dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
642     public void delete(String testName) throws Exception {
643
644         // Perform setup.
645         setupDelete(testName);
646
647         // Submit the request to the service and store the response.
648         ClientResponse<Response> res = client.delete(knownResourceId);
649         int statusCode = res.getStatus();
650
651         // Check the status code of the response: does it match
652         // the expected response(s)?
653         if (logger.isDebugEnabled()) {
654             logger.debug(testName + ": status = " + statusCode);
655         }
656         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
657                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
658         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
659     }
660
661     // Failure outcomes
662     @Override
663     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
664     dependsOnMethods = {"delete"})
665     public void deleteNonExistent(String testName) throws Exception {
666
667         // Perform setup.
668         setupDeleteNonExistent(testName);
669
670         // Submit the request to the service and store the response.
671         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
672         int statusCode = res.getStatus();
673
674         // Check the status code of the response: does it match
675         // the expected response(s)?
676         if (logger.isDebugEnabled()) {
677             logger.debug(testName + ": status = " + statusCode);
678         }
679         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
680                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
681         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
682     }
683
684     // ---------------------------------------------------------------
685     // Utility tests : tests of code used in tests above
686     // ---------------------------------------------------------------
687     /**
688      * Tests the code for manually submitting data that is used by several
689      * of the methods above.
690      */
691     @Test(dependsOnMethods = {"create", "read"})
692     public void testSubmitRequest() throws Exception {
693
694         // Expected status code: 200 OK
695         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
696
697         // Submit the request to the service and store the response.
698         String method = ServiceRequestType.READ.httpMethodName();
699         String url = getResourceURL(knownResourceId);
700         int statusCode = submitRequest(method, url);
701
702         // Check the status code of the response: does it match
703         // the expected response(s)?
704         if (logger.isDebugEnabled()) {
705             logger.debug("testSubmitRequest: url=" + url
706                     + " status=" + statusCode);
707         }
708         Assert.assertEquals(statusCode, EXPECTED_STATUS);
709
710     }
711
712     // ---------------------------------------------------------------
713     // Cleanup of resources created during testing
714     // ---------------------------------------------------------------
715     /**
716      * Deletes all resources created by tests, after all tests have been run.
717      *
718      * This cleanup method will always be run, even if one or more tests fail.
719      * For this reason, it attempts to remove all resources created
720      * at any point during testing, even if some of those resources
721      * may be expected to be deleted by certain tests.
722      */
723     @AfterClass(alwaysRun = true)
724     public void cleanUp() {
725         if (logger.isDebugEnabled()) {
726             logger.debug("Cleaning up temporary resources created for testing ...");
727         }
728         for (String resourceId : allResourceIdsCreated) {
729             // Note: Any non-success responses are ignored and not reported.
730             ClientResponse<Response> res = client.delete(resourceId);
731         }
732     }
733
734     // ---------------------------------------------------------------
735     // Utility methods used by tests above
736     // ---------------------------------------------------------------
737     private MultipartOutput createCollectionObjectInstance(String commonPartName,
738             String identifier) {
739         return createCollectionObjectInstance(commonPartName,
740                 "objectNumber-" + identifier,
741                 "objectName-" + identifier);
742     }
743
744     private MultipartOutput createCollectionObjectInstance(String commonPartName,
745             String objectNumber, String objectName) {
746         CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
747         ResponsibleDepartmentList deptList = new ResponsibleDepartmentList();
748         List<String> depts = deptList.getResponsibleDepartment();
749         // @TODO Use properly formatted refNames for representative departments
750         // in this example test record. The following are mere placeholders.
751         depts.add("urn:org.collectionspace.services.department:Registrar");
752         if (multivalue) {
753             depts.add("urn:org.walkerart.department:Fine Art");
754         }
755         multivalue = !multivalue;
756         //FIXME: Title does not need to be set.
757         collectionObject.setTitle("atitle");
758         collectionObject.setResponsibleDepartments(deptList);
759         collectionObject.setObjectNumber(objectNumber);
760         collectionObject.setOtherNumber("urn:org.walkerart.id:123");
761         collectionObject.setObjectName(objectName);
762         collectionObject.setAge(""); //test for null string
763         collectionObject.setBriefDescription("Papier mache bird cow mask with horns, "
764                 + "painted red with black and yellow spots. "
765                 + "Puerto Rico. ca. 8&quot; high, 6&quot; wide, projects 10&quot; (with horns).");
766
767         CollectionobjectsNaturalhistory conh = new CollectionobjectsNaturalhistory();
768         conh.setNhString("test-string");
769         conh.setNhInt(999);
770         conh.setNhLong(9999);
771
772
773         MultipartOutput multipart = createCollectionObjectInstance(commonPartName, collectionObject, conh);
774         return multipart;
775     }
776
777     private MultipartOutput createCollectionObjectInstance(String commonPartName,
778             CollectionobjectsCommon collectionObject, CollectionobjectsNaturalhistory conh) {
779
780         MultipartOutput multipart = new MultipartOutput();
781         OutputPart commonPart = multipart.addPart(collectionObject,
782                 MediaType.APPLICATION_XML_TYPE);
783         commonPart.getHeaders().add("label", commonPartName);
784
785         if (logger.isDebugEnabled()) {
786             logger.debug("to be created, collectionobject common");
787             logger.debug(objectAsXmlString(collectionObject,
788                     CollectionobjectsCommon.class));
789         }
790
791         if (conh != null) {
792             OutputPart nhPart = multipart.addPart(conh, MediaType.APPLICATION_XML_TYPE);
793             nhPart.getHeaders().add("label", getNHPartName());
794
795             if (logger.isDebugEnabled()) {
796                 logger.debug("to be created, collectionobject nhistory");
797                 logger.debug(objectAsXmlString(conh,
798                         CollectionobjectsNaturalhistory.class));
799             }
800         }
801         return multipart;
802
803     }
804
805     private MultipartOutput createCollectionObjectInstanceFromXml(String commonPartName,
806             String commonPartFileName) throws Exception {
807
808         CollectionobjectsCommon collectionObject = (CollectionobjectsCommon) getObjectFromFile(CollectionobjectsCommon.class,
809                 commonPartFileName);
810         MultipartOutput multipart = new MultipartOutput();
811         OutputPart commonPart = multipart.addPart(collectionObject,
812                 MediaType.APPLICATION_XML_TYPE);
813         commonPart.getHeaders().add("label", commonPartName);
814
815         if (logger.isDebugEnabled()) {
816             logger.debug("to be created, collectionobject common");
817             logger.debug(objectAsXmlString(collectionObject,
818                     CollectionobjectsCommon.class));
819         }
820
821         return multipart;
822
823     }
824
825     private String getNHPartName() {
826         return "collectionobjects_naturalhistory";
827     }
828 }