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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright © 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
23 package org.collectionspace.services.client.test;
25 import java.util.ArrayList;
26 import java.util.List;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
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;
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;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * CollectionObjectServiceTest, carries out tests against a
49 * deployed and running CollectionObject Service.
51 * $LastChangedRevision$
54 public class CollectionObjectServiceTest extends AbstractServiceTestImpl {
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
65 * This method is called only by the parent class, AbstractServiceTest
68 protected String getServicePathComponent() {
69 return client.getServicePathComponent();
72 // ---------------------------------------------------------------
73 // CRUD tests : CREATE tests
74 // ---------------------------------------------------------------
77 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
78 public void create(String testName) throws Exception {
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);
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();
92 // Check the status code of the response: does it match
93 // the expected response(s)?
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);
101 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
102 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
103 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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);
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));
119 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
120 public void createFromXmlCambridge(String testName) throws Exception {
121 createFromXmlFile(testName, "./test-data/testCambridge.xml", true);
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);
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);
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);
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);
145 * @see org.collectionspace.services.client.test.ServiceTest#createList()
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++) {
157 // Placeholders until the three tests below can be uncommented.
158 // See Issue CSPACE-401.
160 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
161 public void createWithEmptyEntityBody(String testName) throws Exception {
165 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
166 public void createWithMalformedXml(String testName) throws Exception {
167 setupCreate(testName);
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();
178 if (logger.isDebugEnabled()) {
179 logger.debug(testName + ": status = " + statusCode);
181 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
182 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
183 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
187 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
188 public void createWithWrongXmlSchema(String testName) throws Exception {
194 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
195 dependsOnMethods = {"create", "testSubmitRequest"})
196 public void createWithEmptyEntityBody(String testName) throwsException {
199 setupCreateWithEmptyEntityBody(testName);
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);
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);
214 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
215 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
216 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
220 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
221 dependsOnMethods = {"create", "testSubmitRequest"})
222 public void createWithMalformedXml(String testName) throws Exception {
225 setupCreateWithMalformedXml(testName);
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);
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);
240 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
241 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
242 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
246 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
247 dependsOnMethods = {"create", "testSubmitRequest"})
248 public void createWithWrongXmlSchema(String testName) throws Exception {
251 setupCreateWithWrongXmlSchema(testName);
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);
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);
266 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
267 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
268 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
271 // ---------------------------------------------------------------
272 // CRUD tests : READ tests
273 // ---------------------------------------------------------------
276 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
277 dependsOnMethods = {"create"})
278 public void read(String testName) throws Exception {
283 // Submit the request to the service and store the response.
284 ClientResponse<MultipartInput> res = client.read(knownResourceId);
285 int statusCode = res.getStatus();
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);
292 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
293 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
294 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
296 MultipartInput input = (MultipartInput) res.getEntity();
298 if (logger.isDebugEnabled()) {
299 logger.debug(testName + ": Reading Common part ...");
301 CollectionobjectsCommon collectionObject =
302 (CollectionobjectsCommon) extractPart(input,
303 client.getCommonPartName(), CollectionobjectsCommon.class);
304 Assert.assertNotNull(collectionObject);
306 if (logger.isDebugEnabled()) {
307 logger.debug(testName + ": Reading Natural History part ...");
309 CollectionobjectsNaturalhistory conh =
310 (CollectionobjectsNaturalhistory) extractPart(input,
311 getNHPartName(), CollectionobjectsNaturalhistory.class);
312 Assert.assertNotNull(conh);
317 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
318 dependsOnMethods = {"read"})
319 public void readNonExistent(String testName) throws Exception {
322 setupReadNonExistent(testName);
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();
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);
333 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
334 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
335 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
338 // ---------------------------------------------------------------
339 // CRUD tests : READ_LIST tests
340 // ---------------------------------------------------------------
343 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
344 dependsOnMethods = {"createList", "read"})
345 public void readList(String testName) throws Exception {
348 setupReadList(testName);
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();
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);
360 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
361 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
362 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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();
371 for (CollectionobjectsCommonList.CollectionObjectListItem item : items) {
372 logger.debug(testName + ": list-item[" + i + "] csid="
374 logger.debug(testName + ": list-item[" + i + "] objectNumber="
375 + item.getObjectNumber());
376 logger.debug(testName + ": list-item[" + i + "] URI="
386 // ---------------------------------------------------------------
387 // CRUD tests : UPDATE tests
388 // ---------------------------------------------------------------
391 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
392 dependsOnMethods = {"read"})
393 public void update(String testName) throws Exception {
396 setupUpdate(testName);
398 ClientResponse<MultipartInput> res = updateRetrieve(testName, knownResourceId);
400 if (logger.isDebugEnabled()) {
401 logger.debug("got object to update with ID: " + knownResourceId);
403 MultipartInput input = (MultipartInput) res.getEntity();
404 CollectionobjectsCommon collectionObject =
405 (CollectionobjectsCommon) extractPart(input,
406 client.getCommonPartName(), CollectionobjectsCommon.class);
407 Assert.assertNotNull(collectionObject);
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));
418 res = updateSend(testName, knownResourceId, collectionObject);
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);
425 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
426 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
427 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
430 input = (MultipartInput) res.getEntity();
431 CollectionobjectsCommon updatedCollectionObject =
432 (CollectionobjectsCommon) extractPart(input,
433 client.getCommonPartName(), CollectionobjectsCommon.class);
434 Assert.assertNotNull(updatedCollectionObject);
436 Assert.assertEquals(updatedCollectionObject.getObjectName(),
437 collectionObject.getObjectName(),
438 "Data in updated object did not match submitted data.");
442 private ClientResponse<MultipartInput> updateRetrieve(String testName, String id) {
443 ClientResponse<MultipartInput> res =
445 if (logger.isDebugEnabled()) {
446 logger.debug("read in updateRetrieve for " + testName + " status = " + res.getStatus());
448 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
450 if (logger.isDebugEnabled()) {
451 logger.debug("got object to updateRetrieve for " + testName + " with ID: " + id);
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());
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());
471 // Placeholders until the three tests below can be uncommented.
472 // See Issue CSPACE-401.
474 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
475 dependsOnMethods = {"read"})
476 public void updateWithEmptyEntityBody(String testName) throws Exception {
480 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
481 dependsOnMethods = {"read"})
482 public void updateWithMalformedXml(String testName) throws Exception {
484 setupUpdate(testName);
485 if (logger.isDebugEnabled()) {
486 logger.debug(testName + " got object to update with ID: " + knownResourceId);
489 ClientResponse<MultipartInput> res = updateRetrieve(testName, knownResourceId);
491 MultipartInput input = (MultipartInput) res.getEntity();
492 CollectionobjectsCommon collectionObject =
493 (CollectionobjectsCommon) extractPart(input,
494 client.getCommonPartName(), CollectionobjectsCommon.class);
495 Assert.assertNotNull(collectionObject);
497 //update with invalid content
498 collectionObject.setObjectNumber("");
500 if (logger.isDebugEnabled()) {
501 logger.debug(testName + " updated object");
502 logger.debug(objectAsXmlString(collectionObject,
503 CollectionobjectsCommon.class));
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());
516 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
517 dependsOnMethods = {"read"})
518 public void updateWithWrongXmlSchema(String testName) throws Exception {
523 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
524 dependsOnMethods = {"create", "update", "testSubmitRequest"})
525 public void updateWithEmptyEntityBody(String testName) throws Exception {
528 setupUpdateWithEmptyEntityBody(testName);
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);
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);
543 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
544 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
545 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
549 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
550 dependsOnMethods = {"create", "update", "testSubmitRequest"})
551 public void updateWithMalformedXml() throws Exception {
554 setupUpdateWithMalformedXml(testName);
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);
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);
569 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
570 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
571 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
575 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
576 dependsOnMethods = {"create", "update", "testSubmitRequest"})
577 public void updateWithWrongXmlSchema(String testName) throws Exception {
580 setupUpdateWithWrongXmlSchema(String testName);
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);
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);
595 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
596 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
597 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
601 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
602 dependsOnMethods = {"update", "testSubmitRequest"})
603 public void updateNonExistent(String testName) throws Exception {
606 setupUpdateNonExistent(testName);
608 // Submit the request to the service and store the response.
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(),
615 ClientResponse<MultipartInput> res =
616 client.update(NON_EXISTENT_ID, multipart);
617 int statusCode = res.getStatus();
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);
624 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
625 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
626 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
629 // ---------------------------------------------------------------
630 // CRUD tests : DELETE tests
631 // ---------------------------------------------------------------
634 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
635 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
636 public void delete(String testName) throws Exception {
639 setupDelete(testName);
641 // Submit the request to the service and store the response.
642 ClientResponse<Response> res = client.delete(knownResourceId);
643 int statusCode = res.getStatus();
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);
650 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
651 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
652 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
657 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
658 dependsOnMethods = {"delete"})
659 public void deleteNonExistent(String testName) throws Exception {
662 setupDeleteNonExistent(testName);
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();
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);
673 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
674 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
675 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
678 // ---------------------------------------------------------------
679 // Utility tests : tests of code used in tests above
680 // ---------------------------------------------------------------
682 * Tests the code for manually submitting data that is used by several
683 * of the methods above.
685 @Test(dependsOnMethods = {"create", "read"})
686 public void testSubmitRequest() throws Exception {
688 // Expected status code: 200 OK
689 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
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);
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);
702 Assert.assertEquals(statusCode, EXPECTED_STATUS);
706 // ---------------------------------------------------------------
707 // Cleanup of resources created during testing
708 // ---------------------------------------------------------------
710 * Deletes all resources created by tests, after all tests have been run.
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.
717 @AfterClass(alwaysRun = true)
718 public void cleanUp() {
719 if (logger.isDebugEnabled()) {
720 logger.debug("Cleaning up temporary resources created for testing ...");
722 for (String resourceId : allResourceIdsCreated) {
723 // Note: Any non-success responses are ignored and not reported.
724 ClientResponse<Response> res = client.delete(resourceId);
728 // ---------------------------------------------------------------
729 // Utility methods used by tests above
730 // ---------------------------------------------------------------
731 private MultipartOutput createCollectionObjectInstance(String commonPartName,
733 return createCollectionObjectInstance(commonPartName,
734 "objectNumber-" + identifier,
735 "objectName-" + identifier);
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");
747 depts.add("urn:org.walkerart.department:Fine Art");
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" high, 6" wide, projects 10" (with horns).");
761 CollectionobjectsNaturalhistory conh = new CollectionobjectsNaturalhistory();
762 conh.setNhString("test-string");
764 conh.setNhLong(9999);
767 MultipartOutput multipart = createCollectionObjectInstance(commonPartName, collectionObject, conh);
771 private MultipartOutput createCollectionObjectInstance(String commonPartName,
772 CollectionobjectsCommon collectionObject, CollectionobjectsNaturalhistory conh) {
774 MultipartOutput multipart = new MultipartOutput();
775 OutputPart commonPart = multipart.addPart(collectionObject,
776 MediaType.APPLICATION_XML_TYPE);
777 commonPart.getHeaders().add("label", commonPartName);
779 if (logger.isDebugEnabled()) {
780 logger.debug("to be created, collectionobject common");
781 logger.debug(objectAsXmlString(collectionObject,
782 CollectionobjectsCommon.class));
786 OutputPart nhPart = multipart.addPart(conh, MediaType.APPLICATION_XML_TYPE);
787 nhPart.getHeaders().add("label", getNHPartName());
789 if (logger.isDebugEnabled()) {
790 logger.debug("to be created, collectionobject nhistory");
791 logger.debug(objectAsXmlString(conh,
792 CollectionobjectsNaturalhistory.class));
800 * createCollectionObjectInstanceFromXml uses JAXB unmarshaller to retrieve
801 * collectionobject from given file
802 * @param commonPartName
803 * @param commonPartFileName
807 private MultipartOutput createCollectionObjectInstanceFromXml(String testName, String commonPartName,
808 String commonPartFileName) throws Exception {
810 CollectionobjectsCommon collectionObject =
811 (CollectionobjectsCommon) getObjectFromFile(CollectionobjectsCommon.class,
813 MultipartOutput multipart = new MultipartOutput();
814 OutputPart commonPart = multipart.addPart(collectionObject,
815 MediaType.APPLICATION_XML_TYPE);
816 commonPart.getHeaders().add("label", commonPartName);
818 if (logger.isDebugEnabled()) {
819 logger.debug(testName + " to be created, collectionobject common");
820 logger.debug(objectAsXmlString(collectionObject,
821 CollectionobjectsCommon.class));
828 * createCollectionObjectInstanceFromRawXml uses stringified collectionobject
829 * retrieve from given file
830 * @param commonPartName
831 * @param commonPartFileName
835 private MultipartOutput createCollectionObjectInstanceFromRawXml(String testName, String commonPartName,
836 String commonPartFileName) throws Exception {
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);
843 OutputPart commonPart = multipart.addPart(stringObject,
844 MediaType.APPLICATION_XML_TYPE);
845 commonPart.getHeaders().add("label", commonPartName);
851 private String getNHPartName() {
852 return "collectionobjects_naturalhistory";
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);
861 MultipartOutput multipart = null;
864 multipart = createCollectionObjectInstanceFromXml(testName,
865 client.getCommonPartName(), fileName);
867 multipart = createCollectionObjectInstanceFromRawXml(testName,
868 client.getCommonPartName(), fileName);
870 ClientResponse<Response> res = client.create(multipart);
871 int statusCode = res.getStatus();
873 if (logger.isDebugEnabled()) {
874 logger.debug(testName + ": status = " + statusCode);
876 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
877 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
878 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
879 allResourceIdsCreated.add(extractId(res));