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