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.OtherNumberList;
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 AbstractServiceTest {
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 = AbstractServiceTest.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));
120 * @see org.collectionspace.services.client.test.ServiceTest#createList()
123 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
124 dependsOnMethods = {"create"})
125 public void createList(String testName) throws Exception {
126 for (int i = 0; i < 3; i++) {
132 // Placeholders until the three tests below can be uncommented.
133 // See Issue CSPACE-401.
135 public void createWithEmptyEntityBody(String testName) throws Exception {
139 public void createWithMalformedXml(String testName) throws Exception {
143 public void createWithWrongXmlSchema(String testName) throws Exception {
149 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
150 dependsOnMethods = {"create", "testSubmitRequest"})
151 public void createWithEmptyEntityBody(String testName) throwsException {
154 setupCreateWithEmptyEntityBody(testName);
156 // Submit the request to the service and store the response.
157 String method = REQUEST_TYPE.httpMethodName();
158 String url = getServiceRootURL();
159 String mediaType = MediaType.APPLICATION_XML;
160 final String entity = "";
161 int statusCode = submitRequest(method, url, mediaType, entity);
163 // Check the status code of the response: does it match
164 // the expected response(s)?
165 if(logger.isDebugEnabled()){
166 logger.debug(testName + ": url=" + url +
167 " status=" + statusCode);
169 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
170 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
171 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
175 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
176 dependsOnMethods = {"create", "testSubmitRequest"})
177 public void createWithMalformedXml(String testName) throws Exception {
180 setupCreateWithMalformedXml(testName);
182 // Submit the request to the service and store the response.
183 String method = REQUEST_TYPE.httpMethodName();
184 String url = getServiceRootURL();
185 String mediaType = MediaType.APPLICATION_XML;
186 final String entity = MALFORMED_XML_DATA; // Constant from base class.
187 int statusCode = submitRequest(method, url, mediaType, entity);
189 // Check the status code of the response: does it match
190 // the expected response(s)?
191 if(logger.isDebugEnabled()){
192 logger.debug(testName + ": url=" + url +
193 " status=" + statusCode);
195 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
196 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
197 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
201 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
202 dependsOnMethods = {"create", "testSubmitRequest"})
203 public void createWithWrongXmlSchema(String testName) throws Exception {
206 setupCreateWithWrongXmlSchema(testName);
208 // Submit the request to the service and store the response.
209 String method = REQUEST_TYPE.httpMethodName();
210 String url = getServiceRootURL();
211 String mediaType = MediaType.APPLICATION_XML;
212 final String entity = WRONG_XML_SCHEMA_DATA;
213 int statusCode = submitRequest(method, url, mediaType, entity);
215 // Check the status code of the response: does it match
216 // the expected response(s)?
217 if(logger.isDebugEnabled()){
218 logger.debug(testName + ": url=" + url +
219 " status=" + statusCode);
221 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
222 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
223 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
226 // ---------------------------------------------------------------
227 // CRUD tests : READ tests
228 // ---------------------------------------------------------------
231 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
232 dependsOnMethods = {"create"})
233 public void read(String testName) throws Exception {
238 // Submit the request to the service and store the response.
239 ClientResponse<MultipartInput> res = client.read(knownResourceId);
240 int statusCode = res.getStatus();
242 // Check the status code of the response: does it match
243 // the expected response(s)?
244 if (logger.isDebugEnabled()) {
245 logger.debug(testName + ": status = " + statusCode);
247 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
248 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
249 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
251 MultipartInput input = (MultipartInput) res.getEntity();
253 if (logger.isDebugEnabled()) {
254 logger.debug(testName + ": Reading Common part ...");
256 CollectionobjectsCommon collectionObject =
257 (CollectionobjectsCommon) extractPart(input,
258 client.getCommonPartName(), CollectionobjectsCommon.class);
259 Assert.assertNotNull(collectionObject);
261 if (logger.isDebugEnabled()) {
262 logger.debug(testName + ": Reading Natural History part ...");
264 CollectionobjectsNaturalhistory conh =
265 (CollectionobjectsNaturalhistory) extractPart(input,
266 getNHPartName(), CollectionobjectsNaturalhistory.class);
267 Assert.assertNotNull(conh);
272 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
273 dependsOnMethods = {"read"})
274 public void readNonExistent(String testName) throws Exception {
277 setupReadNonExistent(testName);
279 // Submit the request to the service and store the response.
280 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
281 int statusCode = res.getStatus();
283 // Check the status code of the response: does it match
284 // the expected response(s)?
285 if (logger.isDebugEnabled()) {
286 logger.debug(testName + ": status = " + statusCode);
288 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
289 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
290 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
293 // ---------------------------------------------------------------
294 // CRUD tests : READ_LIST tests
295 // ---------------------------------------------------------------
298 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
299 dependsOnMethods = {"createList", "read"})
300 public void readList(String testName) throws Exception {
303 setupReadList(testName);
305 // Submit the request to the service and store the response.
306 ClientResponse<CollectionobjectsCommonList> res = client.readList();
307 CollectionobjectsCommonList list = res.getEntity();
308 int statusCode = res.getStatus();
310 // Check the status code of the response: does it match
311 // the expected response(s)?
312 if (logger.isDebugEnabled()) {
313 logger.debug(testName + ": status = " + statusCode);
315 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
316 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
317 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
319 // Optionally output additional data about list members for debugging.
320 boolean iterateThroughList = false;
321 if (iterateThroughList && logger.isDebugEnabled()) {
322 List<CollectionobjectsCommonList.CollectionObjectListItem> items =
323 list.getCollectionObjectListItem();
326 for (CollectionobjectsCommonList.CollectionObjectListItem item : items) {
327 logger.debug(testName + ": list-item[" + i + "] csid="
329 logger.debug(testName + ": list-item[" + i + "] objectNumber="
330 + item.getObjectNumber());
331 logger.debug(testName + ": list-item[" + i + "] URI="
341 // ---------------------------------------------------------------
342 // CRUD tests : UPDATE tests
343 // ---------------------------------------------------------------
346 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
347 dependsOnMethods = {"read"})
348 public void update(String testName) throws Exception {
351 setupUpdate(testName);
353 ClientResponse<MultipartInput> res =
354 client.read(knownResourceId);
355 if (logger.isDebugEnabled()) {
356 logger.debug(testName + ": read status = " + res.getStatus());
358 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
360 if (logger.isDebugEnabled()) {
361 logger.debug("got object to update with ID: " + knownResourceId);
363 MultipartInput input = (MultipartInput) res.getEntity();
364 CollectionobjectsCommon collectionObject =
365 (CollectionobjectsCommon) extractPart(input,
366 client.getCommonPartName(), CollectionobjectsCommon.class);
367 Assert.assertNotNull(collectionObject);
369 // Update the content of this resource.
370 collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
371 collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
372 if (logger.isDebugEnabled()) {
373 logger.debug("updated object");
374 logger.debug(objectAsXmlString(collectionObject,
375 CollectionobjectsCommon.class));
378 // Submit the request to the service and store the response.
379 MultipartOutput output = new MultipartOutput();
380 OutputPart commonPart = output.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
381 commonPart.getHeaders().add("label", client.getCommonPartName());
383 res = client.update(knownResourceId, output);
384 int statusCode = res.getStatus();
385 // Check the status code of the response: does it match the expected response(s)?
386 if (logger.isDebugEnabled()) {
387 logger.debug(testName + ": status = " + statusCode);
389 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
390 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
391 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
394 input = (MultipartInput) res.getEntity();
395 CollectionobjectsCommon updatedCollectionObject =
396 (CollectionobjectsCommon) extractPart(input,
397 client.getCommonPartName(), CollectionobjectsCommon.class);
398 Assert.assertNotNull(updatedCollectionObject);
400 Assert.assertEquals(updatedCollectionObject.getObjectName(),
401 collectionObject.getObjectName(),
402 "Data in updated object did not match submitted data.");
407 // Placeholders until the three tests below can be uncommented.
408 // See Issue CSPACE-401.
410 public void updateWithEmptyEntityBody(String testName) throws Exception {
414 public void updateWithMalformedXml(String testName) throws Exception {
418 public void updateWithWrongXmlSchema(String testName) throws Exception {
423 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
424 dependsOnMethods = {"create", "update", "testSubmitRequest"})
425 public void updateWithEmptyEntityBody(String testName) throws Exception {
428 setupUpdateWithEmptyEntityBody(testName);
430 // Submit the request to the service and store the response.
431 String method = REQUEST_TYPE.httpMethodName();
432 String url = getResourceURL(knownResourceId);
433 String mediaType = MediaType.APPLICATION_XML;
434 final String entity = "";
435 int statusCode = submitRequest(method, url, mediaType, entity);
437 // Check the status code of the response: does it match
438 // the expected response(s)?
439 if(logger.isDebugEnabled()){
440 logger.debug(testName + ": url=" + url +
441 " status=" + statusCode);
443 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
444 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
445 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
449 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
450 dependsOnMethods = {"create", "update", "testSubmitRequest"})
451 public void updateWithMalformedXml() throws Exception {
454 setupUpdateWithMalformedXml(testName);
456 // Submit the request to the service and store the response.
457 String method = REQUEST_TYPE.httpMethodName();
458 String url = getResourceURL(knownResourceId);
459 final String entity = MALFORMED_XML_DATA;
460 String mediaType = MediaType.APPLICATION_XML;
461 int statusCode = submitRequest(method, url, mediaType, entity);
463 // Check the status code of the response: does it match
464 // the expected response(s)?
465 if(logger.isDebugEnabled()){
466 logger.debug(testName + ": url=" + url +
467 " status=" + statusCode);
469 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
470 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
471 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
475 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
476 dependsOnMethods = {"create", "update", "testSubmitRequest"})
477 public void updateWithWrongXmlSchema(String testName) throws Exception {
480 setupUpdateWithWrongXmlSchema(String testName);
482 // Submit the request to the service and store the response.
483 String method = REQUEST_TYPE.httpMethodName();
484 String url = getResourceURL(knownResourceId);
485 String mediaType = MediaType.APPLICATION_XML;
486 final String entity = WRONG_XML_SCHEMA_DATA;
487 int statusCode = submitRequest(method, url, mediaType, entity);
489 // Check the status code of the response: does it match
490 // the expected response(s)?
491 if(logger.isDebugEnabled()){
492 logger.debug(testName + ": url=" + url +
493 " status=" + statusCode);
495 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
496 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
497 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
501 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
502 dependsOnMethods = {"update", "testSubmitRequest"})
503 public void updateNonExistent(String testName) throws Exception {
506 setupUpdateNonExistent(testName);
508 // Submit the request to the service and store the response.
510 // Note: The ID used in this 'create' call may be arbitrary.
511 // The only relevant ID may be the one used in updateCollectionObject(), below.
512 MultipartOutput multipart =
513 createCollectionObjectInstance(client.getCommonPartName(),
515 ClientResponse<MultipartInput> res =
516 client.update(NON_EXISTENT_ID, multipart);
517 int statusCode = res.getStatus();
519 // Check the status code of the response: does it match
520 // the expected response(s)?
521 if (logger.isDebugEnabled()) {
522 logger.debug(testName + ": status = " + statusCode);
524 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
525 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
526 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
529 // ---------------------------------------------------------------
530 // CRUD tests : DELETE tests
531 // ---------------------------------------------------------------
534 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
535 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
536 public void delete(String testName) throws Exception {
539 setupDelete(testName);
541 // Submit the request to the service and store the response.
542 ClientResponse<Response> res = client.delete(knownResourceId);
543 int statusCode = res.getStatus();
545 // Check the status code of the response: does it match
546 // the expected response(s)?
547 if (logger.isDebugEnabled()) {
548 logger.debug(testName + ": status = " + statusCode);
550 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
551 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
552 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
557 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
558 dependsOnMethods = {"delete"})
559 public void deleteNonExistent(String testName) throws Exception {
562 setupDeleteNonExistent(testName);
564 // Submit the request to the service and store the response.
565 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
566 int statusCode = res.getStatus();
568 // Check the status code of the response: does it match
569 // the expected response(s)?
570 if (logger.isDebugEnabled()) {
571 logger.debug(testName + ": status = " + statusCode);
573 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
574 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
575 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
578 // ---------------------------------------------------------------
579 // Utility tests : tests of code used in tests above
580 // ---------------------------------------------------------------
582 * Tests the code for manually submitting data that is used by several
583 * of the methods above.
585 @Test(dependsOnMethods = {"create", "read"})
586 public void testSubmitRequest() throws Exception {
588 // Expected status code: 200 OK
589 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
591 // Submit the request to the service and store the response.
592 String method = ServiceRequestType.READ.httpMethodName();
593 String url = getResourceURL(knownResourceId);
594 int statusCode = submitRequest(method, url);
596 // Check the status code of the response: does it match
597 // the expected response(s)?
598 if (logger.isDebugEnabled()) {
599 logger.debug("testSubmitRequest: url=" + url
600 + " status=" + statusCode);
602 Assert.assertEquals(statusCode, EXPECTED_STATUS);
606 // ---------------------------------------------------------------
607 // Cleanup of resources created during testing
608 // ---------------------------------------------------------------
610 * Deletes all resources created by tests, after all tests have been run.
612 * This cleanup method will always be run, even if one or more tests fail.
613 * For this reason, it attempts to remove all resources created
614 * at any point during testing, even if some of those resources
615 * may be expected to be deleted by certain tests.
617 @AfterClass(alwaysRun = true)
618 public void cleanUp() {
619 if (logger.isDebugEnabled()) {
620 logger.debug("Cleaning up temporary resources created for testing ...");
622 for (String resourceId : allResourceIdsCreated) {
623 // Note: Any non-success responses are ignored and not reported.
624 ClientResponse<Response> res = client.delete(resourceId);
628 // ---------------------------------------------------------------
629 // Utility methods used by tests above
630 // ---------------------------------------------------------------
631 private MultipartOutput createCollectionObjectInstance(String commonPartName,
633 return createCollectionObjectInstance(commonPartName,
634 "objectNumber-" + identifier,
635 "objectName-" + identifier);
638 private MultipartOutput createCollectionObjectInstance(String commonPartName,
639 String objectNumber, String objectName) {
640 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
641 OtherNumberList onList = new OtherNumberList();
642 List<String> ons = onList.getOtherNumber();
643 ons.add("urn:org.collectionspace.id:24082390");
645 ons.add("urn:org.walkerart.id:123");
647 multivalue = !multivalue;
648 collectionObject.setOtherNumbers(onList);
649 collectionObject.setObjectNumber(objectNumber);
650 collectionObject.setObjectName(objectName);
651 collectionObject.setAge(""); //test for null string
652 collectionObject.setBriefDescription("Papier mache bird mask with horns, "
653 + "painted red with black and yellow spots. "
654 + "Puerto Rico. ca. 8" high, 6" wide, projects 10" (with horns).");
655 MultipartOutput multipart = new MultipartOutput();
656 OutputPart commonPart = multipart.addPart(collectionObject,
657 MediaType.APPLICATION_XML_TYPE);
658 commonPart.getHeaders().add("label", commonPartName);
660 if (logger.isDebugEnabled()) {
661 logger.debug("to be created, collectionobject common");
662 logger.debug(objectAsXmlString(collectionObject,
663 CollectionobjectsCommon.class));
666 CollectionobjectsNaturalhistory conh = new CollectionobjectsNaturalhistory();
667 conh.setNhString("test-string");
669 conh.setNhLong(9999);
670 OutputPart nhPart = multipart.addPart(conh, MediaType.APPLICATION_XML_TYPE);
671 nhPart.getHeaders().add("label", getNHPartName());
673 if (logger.isDebugEnabled()) {
674 logger.debug("to be created, collectionobject nhistory");
675 logger.debug(objectAsXmlString(conh,
676 CollectionobjectsNaturalhistory.class));
682 private String getNHPartName() {
683 return "collectionobjects_naturalhistory";