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