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 createFromXml(String testName) throws Exception {
122 // Perform setup, such as initializing the type of service request
123 // (e.g. CREATE, DELETE), its valid and expected status codes, and
124 // its associated HTTP method name (e.g. POST, DELETE).
125 setupCreate(testName);
127 // Submit the request to the service and store the response.
128 String identifier = createIdentifier();
129 MultipartOutput multipart =
130 createCollectionObjectInstanceFromXml(client.getCommonPartName(),
131 "test-data/testCambridge.xml");
132 ClientResponse<Response> res = client.create(multipart);
133 int statusCode = res.getStatus();
135 // Check the status code of the response: does it match
136 // the expected response(s)?
139 // Does it fall within the set of valid status codes?
140 // Does it exactly match the expected status code?
141 if (logger.isDebugEnabled()) {
142 logger.debug(testName + ": status = " + statusCode);
144 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
145 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
146 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
147 allResourceIdsCreated.add(extractId(res));
151 * @see org.collectionspace.services.client.test.ServiceTest#createList()
154 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
155 dependsOnMethods = {"create"})
156 public void createList(String testName) throws Exception {
157 for (int i = 0; i < 3; i++) {
163 // Placeholders until the three tests below can be uncommented.
164 // See Issue CSPACE-401.
166 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
167 public void createWithEmptyEntityBody(String testName) throws Exception {
171 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
172 public void createWithMalformedXml(String testName) throws Exception {
173 setupCreate(testName);
175 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
176 collectionObject.setTitle("atitle");
177 //don't set objectNumber to check validation
178 collectionObject.setObjectName("some name");
179 MultipartOutput multipart =
180 createCollectionObjectInstance(client.getCommonPartName(), collectionObject, null);
181 ClientResponse<Response> res = client.create(multipart);
182 int statusCode = res.getStatus();
184 if (logger.isDebugEnabled()) {
185 logger.debug(testName + ": status = " + statusCode);
187 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
188 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
189 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
193 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
194 public void createWithWrongXmlSchema(String testName) throws Exception {
200 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
201 dependsOnMethods = {"create", "testSubmitRequest"})
202 public void createWithEmptyEntityBody(String testName) throwsException {
205 setupCreateWithEmptyEntityBody(testName);
207 // Submit the request to the service and store the response.
208 String method = REQUEST_TYPE.httpMethodName();
209 String url = getServiceRootURL();
210 String mediaType = MediaType.APPLICATION_XML;
211 final String entity = "";
212 int statusCode = submitRequest(method, url, mediaType, entity);
214 // Check the status code of the response: does it match
215 // the expected response(s)?
216 if(logger.isDebugEnabled()){
217 logger.debug(testName + ": url=" + url +
218 " status=" + statusCode);
220 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
221 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
222 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
226 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
227 dependsOnMethods = {"create", "testSubmitRequest"})
228 public void createWithMalformedXml(String testName) throws Exception {
231 setupCreateWithMalformedXml(testName);
233 // Submit the request to the service and store the response.
234 String method = REQUEST_TYPE.httpMethodName();
235 String url = getServiceRootURL();
236 String mediaType = MediaType.APPLICATION_XML;
237 final String entity = MALFORMED_XML_DATA; // Constant from base class.
238 int statusCode = submitRequest(method, url, mediaType, entity);
240 // Check the status code of the response: does it match
241 // the expected response(s)?
242 if(logger.isDebugEnabled()){
243 logger.debug(testName + ": url=" + url +
244 " status=" + statusCode);
246 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
247 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
248 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
252 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
253 dependsOnMethods = {"create", "testSubmitRequest"})
254 public void createWithWrongXmlSchema(String testName) throws Exception {
257 setupCreateWithWrongXmlSchema(testName);
259 // Submit the request to the service and store the response.
260 String method = REQUEST_TYPE.httpMethodName();
261 String url = getServiceRootURL();
262 String mediaType = MediaType.APPLICATION_XML;
263 final String entity = WRONG_XML_SCHEMA_DATA;
264 int statusCode = submitRequest(method, url, mediaType, entity);
266 // Check the status code of the response: does it match
267 // the expected response(s)?
268 if(logger.isDebugEnabled()){
269 logger.debug(testName + ": url=" + url +
270 " status=" + statusCode);
272 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
273 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
274 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
277 // ---------------------------------------------------------------
278 // CRUD tests : READ tests
279 // ---------------------------------------------------------------
282 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
283 dependsOnMethods = {"create"})
284 public void read(String testName) throws Exception {
289 // Submit the request to the service and store the response.
290 ClientResponse<MultipartInput> res = client.read(knownResourceId);
291 int statusCode = res.getStatus();
293 // Check the status code of the response: does it match
294 // the expected response(s)?
295 if (logger.isDebugEnabled()) {
296 logger.debug(testName + ": status = " + statusCode);
298 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
299 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
300 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
302 MultipartInput input = (MultipartInput) res.getEntity();
304 if (logger.isDebugEnabled()) {
305 logger.debug(testName + ": Reading Common part ...");
307 CollectionobjectsCommon collectionObject =
308 (CollectionobjectsCommon) extractPart(input,
309 client.getCommonPartName(), CollectionobjectsCommon.class);
310 Assert.assertNotNull(collectionObject);
312 if (logger.isDebugEnabled()) {
313 logger.debug(testName + ": Reading Natural History part ...");
315 CollectionobjectsNaturalhistory conh =
316 (CollectionobjectsNaturalhistory) extractPart(input,
317 getNHPartName(), CollectionobjectsNaturalhistory.class);
318 Assert.assertNotNull(conh);
323 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
324 dependsOnMethods = {"read"})
325 public void readNonExistent(String testName) throws Exception {
328 setupReadNonExistent(testName);
330 // Submit the request to the service and store the response.
331 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
332 int statusCode = res.getStatus();
334 // Check the status code of the response: does it match
335 // the expected response(s)?
336 if (logger.isDebugEnabled()) {
337 logger.debug(testName + ": status = " + statusCode);
339 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
340 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
341 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
344 // ---------------------------------------------------------------
345 // CRUD tests : READ_LIST tests
346 // ---------------------------------------------------------------
349 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
350 dependsOnMethods = {"createList", "read"})
351 public void readList(String testName) throws Exception {
354 setupReadList(testName);
356 // Submit the request to the service and store the response.
357 ClientResponse<CollectionobjectsCommonList> res = client.readList();
358 CollectionobjectsCommonList list = res.getEntity();
359 int statusCode = res.getStatus();
361 // Check the status code of the response: does it match
362 // the expected response(s)?
363 if (logger.isDebugEnabled()) {
364 logger.debug(testName + ": status = " + statusCode);
366 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
367 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
368 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
370 // Optionally output additional data about list members for debugging.
371 boolean iterateThroughList = false;
372 if (iterateThroughList && logger.isDebugEnabled()) {
373 List<CollectionobjectsCommonList.CollectionObjectListItem> items =
374 list.getCollectionObjectListItem();
377 for (CollectionobjectsCommonList.CollectionObjectListItem item : items) {
378 logger.debug(testName + ": list-item[" + i + "] csid="
380 logger.debug(testName + ": list-item[" + i + "] objectNumber="
381 + item.getObjectNumber());
382 logger.debug(testName + ": list-item[" + i + "] URI="
392 // ---------------------------------------------------------------
393 // CRUD tests : UPDATE tests
394 // ---------------------------------------------------------------
397 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
398 dependsOnMethods = {"read"})
399 public void update(String testName) throws Exception {
402 setupUpdate(testName);
404 ClientResponse<MultipartInput> res = updateRetrieve(testName, knownResourceId);
406 if (logger.isDebugEnabled()) {
407 logger.debug("got object to update with ID: " + knownResourceId);
409 MultipartInput input = (MultipartInput) res.getEntity();
410 CollectionobjectsCommon collectionObject =
411 (CollectionobjectsCommon) extractPart(input,
412 client.getCommonPartName(), CollectionobjectsCommon.class);
413 Assert.assertNotNull(collectionObject);
415 // Update the content of this resource.
416 collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
417 collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
418 if (logger.isDebugEnabled()) {
419 logger.debug("updated object");
420 logger.debug(objectAsXmlString(collectionObject,
421 CollectionobjectsCommon.class));
424 res = updateSend(testName, knownResourceId, collectionObject);
426 int statusCode = res.getStatus();
427 // Check the status code of the response: does it match the expected response(s)?
428 if (logger.isDebugEnabled()) {
429 logger.debug(testName + ": status = " + statusCode);
431 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
432 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
433 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
436 input = (MultipartInput) res.getEntity();
437 CollectionobjectsCommon updatedCollectionObject =
438 (CollectionobjectsCommon) extractPart(input,
439 client.getCommonPartName(), CollectionobjectsCommon.class);
440 Assert.assertNotNull(updatedCollectionObject);
442 Assert.assertEquals(updatedCollectionObject.getObjectName(),
443 collectionObject.getObjectName(),
444 "Data in updated object did not match submitted data.");
448 private ClientResponse<MultipartInput> updateRetrieve(String testName, String id) {
449 ClientResponse<MultipartInput> res =
451 if (logger.isDebugEnabled()) {
452 logger.debug("read in updateRetrieve for " + testName + " status = " + res.getStatus());
454 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
456 if (logger.isDebugEnabled()) {
457 logger.debug("got object to updateRetrieve for " + testName + " with ID: " + id);
462 private ClientResponse<MultipartInput> updateSend(String testName, String id,
463 CollectionobjectsCommon collectionObject) {
464 MultipartOutput output = new MultipartOutput();
465 OutputPart commonPart = output.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
466 commonPart.getHeaders().add("label", client.getCommonPartName());
468 ClientResponse<MultipartInput> res = client.update(knownResourceId, output);
469 // Check the status code of the response: does it match the expected response(s)?
470 if (logger.isDebugEnabled()) {
471 logger.debug("updateSend for " + testName + ": status = " + res.getStatus());
477 // Placeholders until the three tests below can be uncommented.
478 // See Issue CSPACE-401.
480 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
481 dependsOnMethods = {"read"})
482 public void updateWithEmptyEntityBody(String testName) throws Exception {
486 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
487 dependsOnMethods = {"read"})
488 public void updateWithMalformedXml(String testName) throws Exception {
490 setupUpdate(testName);
491 if (logger.isDebugEnabled()) {
492 logger.debug(testName + " got object to update with ID: " + knownResourceId);
495 ClientResponse<MultipartInput> res = updateRetrieve(testName, knownResourceId);
497 MultipartInput input = (MultipartInput) res.getEntity();
498 CollectionobjectsCommon collectionObject =
499 (CollectionobjectsCommon) extractPart(input,
500 client.getCommonPartName(), CollectionobjectsCommon.class);
501 Assert.assertNotNull(collectionObject);
503 //update with invalid content
504 collectionObject.setObjectNumber("");
506 if (logger.isDebugEnabled()) {
507 logger.debug(testName + " updated object");
508 logger.debug(objectAsXmlString(collectionObject,
509 CollectionobjectsCommon.class));
512 // Submit the request to the service and store the response.
513 res = updateSend(testName, knownResourceId, collectionObject);
514 int statusCode = res.getStatus();
515 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
516 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
517 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
522 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
523 dependsOnMethods = {"read"})
524 public void updateWithWrongXmlSchema(String testName) throws Exception {
529 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
530 dependsOnMethods = {"create", "update", "testSubmitRequest"})
531 public void updateWithEmptyEntityBody(String testName) throws Exception {
534 setupUpdateWithEmptyEntityBody(testName);
536 // Submit the request to the service and store the response.
537 String method = REQUEST_TYPE.httpMethodName();
538 String url = getResourceURL(knownResourceId);
539 String mediaType = MediaType.APPLICATION_XML;
540 final String entity = "";
541 int statusCode = submitRequest(method, url, mediaType, entity);
543 // Check the status code of the response: does it match
544 // the expected response(s)?
545 if(logger.isDebugEnabled()){
546 logger.debug(testName + ": url=" + url +
547 " status=" + statusCode);
549 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
550 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
551 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
555 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
556 dependsOnMethods = {"create", "update", "testSubmitRequest"})
557 public void updateWithMalformedXml() throws Exception {
560 setupUpdateWithMalformedXml(testName);
562 // Submit the request to the service and store the response.
563 String method = REQUEST_TYPE.httpMethodName();
564 String url = getResourceURL(knownResourceId);
565 final String entity = MALFORMED_XML_DATA;
566 String mediaType = MediaType.APPLICATION_XML;
567 int statusCode = submitRequest(method, url, mediaType, entity);
569 // Check the status code of the response: does it match
570 // the expected response(s)?
571 if(logger.isDebugEnabled()){
572 logger.debug(testName + ": url=" + url +
573 " status=" + statusCode);
575 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
576 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
577 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
581 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
582 dependsOnMethods = {"create", "update", "testSubmitRequest"})
583 public void updateWithWrongXmlSchema(String testName) throws Exception {
586 setupUpdateWithWrongXmlSchema(String testName);
588 // Submit the request to the service and store the response.
589 String method = REQUEST_TYPE.httpMethodName();
590 String url = getResourceURL(knownResourceId);
591 String mediaType = MediaType.APPLICATION_XML;
592 final String entity = WRONG_XML_SCHEMA_DATA;
593 int statusCode = submitRequest(method, url, mediaType, entity);
595 // Check the status code of the response: does it match
596 // the expected response(s)?
597 if(logger.isDebugEnabled()){
598 logger.debug(testName + ": url=" + url +
599 " status=" + statusCode);
601 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
602 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
603 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
607 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
608 dependsOnMethods = {"update", "testSubmitRequest"})
609 public void updateNonExistent(String testName) throws Exception {
612 setupUpdateNonExistent(testName);
614 // Submit the request to the service and store the response.
616 // Note: The ID used in this 'create' call may be arbitrary.
617 // The only relevant ID may be the one used in updateCollectionObject(), below.
618 MultipartOutput multipart =
619 createCollectionObjectInstance(client.getCommonPartName(),
621 ClientResponse<MultipartInput> res =
622 client.update(NON_EXISTENT_ID, multipart);
623 int statusCode = res.getStatus();
625 // Check the status code of the response: does it match
626 // the expected response(s)?
627 if (logger.isDebugEnabled()) {
628 logger.debug(testName + ": status = " + statusCode);
630 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
631 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
632 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
635 // ---------------------------------------------------------------
636 // CRUD tests : DELETE tests
637 // ---------------------------------------------------------------
640 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
641 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
642 public void delete(String testName) throws Exception {
645 setupDelete(testName);
647 // Submit the request to the service and store the response.
648 ClientResponse<Response> res = client.delete(knownResourceId);
649 int statusCode = res.getStatus();
651 // Check the status code of the response: does it match
652 // the expected response(s)?
653 if (logger.isDebugEnabled()) {
654 logger.debug(testName + ": status = " + statusCode);
656 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
657 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
658 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
663 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
664 dependsOnMethods = {"delete"})
665 public void deleteNonExistent(String testName) throws Exception {
668 setupDeleteNonExistent(testName);
670 // Submit the request to the service and store the response.
671 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
672 int statusCode = res.getStatus();
674 // Check the status code of the response: does it match
675 // the expected response(s)?
676 if (logger.isDebugEnabled()) {
677 logger.debug(testName + ": status = " + statusCode);
679 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
680 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
681 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
684 // ---------------------------------------------------------------
685 // Utility tests : tests of code used in tests above
686 // ---------------------------------------------------------------
688 * Tests the code for manually submitting data that is used by several
689 * of the methods above.
691 @Test(dependsOnMethods = {"create", "read"})
692 public void testSubmitRequest() throws Exception {
694 // Expected status code: 200 OK
695 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
697 // Submit the request to the service and store the response.
698 String method = ServiceRequestType.READ.httpMethodName();
699 String url = getResourceURL(knownResourceId);
700 int statusCode = submitRequest(method, url);
702 // Check the status code of the response: does it match
703 // the expected response(s)?
704 if (logger.isDebugEnabled()) {
705 logger.debug("testSubmitRequest: url=" + url
706 + " status=" + statusCode);
708 Assert.assertEquals(statusCode, EXPECTED_STATUS);
712 // ---------------------------------------------------------------
713 // Cleanup of resources created during testing
714 // ---------------------------------------------------------------
716 * Deletes all resources created by tests, after all tests have been run.
718 * This cleanup method will always be run, even if one or more tests fail.
719 * For this reason, it attempts to remove all resources created
720 * at any point during testing, even if some of those resources
721 * may be expected to be deleted by certain tests.
723 @AfterClass(alwaysRun = true)
724 public void cleanUp() {
725 if (logger.isDebugEnabled()) {
726 logger.debug("Cleaning up temporary resources created for testing ...");
728 for (String resourceId : allResourceIdsCreated) {
729 // Note: Any non-success responses are ignored and not reported.
730 ClientResponse<Response> res = client.delete(resourceId);
734 // ---------------------------------------------------------------
735 // Utility methods used by tests above
736 // ---------------------------------------------------------------
737 private MultipartOutput createCollectionObjectInstance(String commonPartName,
739 return createCollectionObjectInstance(commonPartName,
740 "objectNumber-" + identifier,
741 "objectName-" + identifier);
744 private MultipartOutput createCollectionObjectInstance(String commonPartName,
745 String objectNumber, String objectName) {
746 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
747 ResponsibleDepartmentList deptList = new ResponsibleDepartmentList();
748 List<String> depts = deptList.getResponsibleDepartment();
749 // @TODO Use properly formatted refNames for representative departments
750 // in this example test record. The following are mere placeholders.
751 depts.add("urn:org.collectionspace.services.department:Registrar");
753 depts.add("urn:org.walkerart.department:Fine Art");
755 multivalue = !multivalue;
756 //FIXME: Title does not need to be set.
757 collectionObject.setTitle("atitle");
758 collectionObject.setResponsibleDepartments(deptList);
759 collectionObject.setObjectNumber(objectNumber);
760 collectionObject.setOtherNumber("urn:org.walkerart.id:123");
761 collectionObject.setObjectName(objectName);
762 collectionObject.setAge(""); //test for null string
763 collectionObject.setBriefDescription("Papier mache bird cow mask with horns, "
764 + "painted red with black and yellow spots. "
765 + "Puerto Rico. ca. 8" high, 6" wide, projects 10" (with horns).");
767 CollectionobjectsNaturalhistory conh = new CollectionobjectsNaturalhistory();
768 conh.setNhString("test-string");
770 conh.setNhLong(9999);
773 MultipartOutput multipart = createCollectionObjectInstance(commonPartName, collectionObject, conh);
777 private MultipartOutput createCollectionObjectInstance(String commonPartName,
778 CollectionobjectsCommon collectionObject, CollectionobjectsNaturalhistory conh) {
780 MultipartOutput multipart = new MultipartOutput();
781 OutputPart commonPart = multipart.addPart(collectionObject,
782 MediaType.APPLICATION_XML_TYPE);
783 commonPart.getHeaders().add("label", commonPartName);
785 if (logger.isDebugEnabled()) {
786 logger.debug("to be created, collectionobject common");
787 logger.debug(objectAsXmlString(collectionObject,
788 CollectionobjectsCommon.class));
792 OutputPart nhPart = multipart.addPart(conh, MediaType.APPLICATION_XML_TYPE);
793 nhPart.getHeaders().add("label", getNHPartName());
795 if (logger.isDebugEnabled()) {
796 logger.debug("to be created, collectionobject nhistory");
797 logger.debug(objectAsXmlString(conh,
798 CollectionobjectsNaturalhistory.class));
805 private MultipartOutput createCollectionObjectInstanceFromXml(String commonPartName,
806 String commonPartFileName) throws Exception {
808 CollectionobjectsCommon collectionObject = (CollectionobjectsCommon) getObjectFromFile(CollectionobjectsCommon.class,
810 MultipartOutput multipart = new MultipartOutput();
811 OutputPart commonPart = multipart.addPart(collectionObject,
812 MediaType.APPLICATION_XML_TYPE);
813 commonPart.getHeaders().add("label", commonPartName);
815 if (logger.isDebugEnabled()) {
816 logger.debug("to be created, collectionobject common");
817 logger.debug(objectAsXmlString(collectionObject,
818 CollectionobjectsCommon.class));
825 private String getNHPartName() {
826 return "collectionobjects_naturalhistory";