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 (c) 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.AcquisitionClient;
31 import org.collectionspace.services.client.CollectionSpaceClient;
32 import org.collectionspace.services.jaxb.AbstractCommonList;
34 import org.collectionspace.services.acquisition.AcquisitionsCommon;
35 import org.collectionspace.services.acquisition.AcquisitionsCommonList;
36 import org.collectionspace.services.acquisition.AcquisitionSourceList;
37 import org.jboss.resteasy.client.ClientResponse;
39 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
40 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
41 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
42 import org.testng.Assert;
43 import org.testng.annotations.AfterClass;
44 import org.testng.annotations.Test;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * AcquisitionServiceTest, carries out tests against a
51 * deployed and running Acquisition Service.
53 * $LastChangedRevision: 621 $
54 * $LastChangedDate: 2009-09-02 16:49:01 -0700 (Wed, 02 Sep 2009) $
56 public class AcquisitionServiceTest extends AbstractServiceTestImpl {
58 private final Logger logger =
59 LoggerFactory.getLogger(AcquisitionServiceTest.class);
61 // Instance variables specific to this test.
62 private String knownResourceId = null;
63 private List<String> allResourceIdsCreated = new ArrayList();
66 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
69 protected CollectionSpaceClient getClientInstance() {
70 return new AcquisitionClient();
74 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
77 protected AbstractCommonList getAbstractCommonList(
78 ClientResponse<AbstractCommonList> response) {
79 return response.getEntity(AcquisitionsCommonList.class);
82 // ---------------------------------------------------------------
83 // CRUD tests : CREATE tests
84 // ---------------------------------------------------------------
87 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
88 public void create(String testName) throws Exception {
90 // Perform setup, such as initializing the type of service request
91 // (e.g. CREATE, DELETE), its valid and expected status codes, and
92 // its associated HTTP method name (e.g. POST, DELETE).
93 setupCreate(testName);
95 // Submit the request to the service and store the response.
96 String identifier = createIdentifier();
98 AcquisitionClient client = new AcquisitionClient();
99 MultipartOutput multipart = createAcquisitionInstance(identifier);
100 ClientResponse<Response> res = client.create(multipart);
102 int statusCode = res.getStatus();
104 // Check the status code of the response: does it match
105 // the expected response(s)?
108 // Does it fall within the set of valid status codes?
109 // Does it exactly match the expected status code?
110 if(logger.isDebugEnabled()){
111 logger.debug(testName + ": status = " + statusCode);
113 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
114 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
115 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
117 // Store the ID returned from the first resource created
118 // for additional tests below.
119 if (knownResourceId == null){
120 knownResourceId = extractId(res);
121 if (logger.isDebugEnabled()) {
122 logger.debug(testName + ": knownResourceId=" + knownResourceId);
126 // Store the IDs from every resource created by tests,
127 // so they can be deleted after tests have been run.
128 allResourceIdsCreated.add(extractId(res));
132 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
133 dependsOnMethods = {"create"})
134 public void createList(String testName) throws Exception {
135 for(int i = 0; i < 3; i++){
141 // Placeholders until the three tests below can be uncommented.
142 // See Issue CSPACE-401.
144 public void createWithEmptyEntityBody(String testName) throws Exception {
148 public void createWithMalformedXml(String testName) throws Exception {
152 public void createWithWrongXmlSchema(String testName) throws Exception {
157 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
158 dependsOnMethods = {"create", "testSubmitRequest"})
159 public void createWithMalformedXml(String testName) throws Exception {
162 setupCreateWithMalformedXml();
164 // Submit the request to the service and store the response.
165 String method = REQUEST_TYPE.httpMethodName();
166 String url = getServiceRootURL();
167 final String entity = MALFORMED_XML_DATA; // Constant from base class.
168 int statusCode = submitRequest(method, url, entity);
170 // Check the status code of the response: does it match
171 // the expected response(s)?
172 if(logger.isDebugEnabled()){
173 logger.debug(testName + ": url=" + url +
174 " status=" + statusCode);
176 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
177 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
178 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
182 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
183 dependsOnMethods = {"create", "testSubmitRequest"})
184 public void createWithWrongXmlSchema() throws Exception {
187 setupCreateWithWrongXmlSchema();
189 // Submit the request to the service and store the response.
190 String method = REQUEST_TYPE.httpMethodName();
191 String url = getServiceRootURL();
192 final String entity = WRONG_XML_SCHEMA_DATA;
193 int statusCode = submitRequest(method, url, entity);
195 // Check the status code of the response: does it match
196 // the expected response(s)?
197 if(logger.isDebugEnabled()){
198 logger.debug(testName + ": url=" + url +
199 " status=" + statusCode);
201 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
202 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
203 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
207 // ---------------------------------------------------------------
208 // CRUD tests : READ tests
209 // ---------------------------------------------------------------
212 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
213 dependsOnMethods = {"create"})
214 public void read(String testName) throws Exception {
219 AcquisitionClient client = new AcquisitionClient();
221 // Submit the request to the service and store the response.
222 ClientResponse<MultipartInput> res = client.read(knownResourceId);
223 int statusCode = res.getStatus();
225 // Check the status code of the response: does it match
226 // the expected response(s)?
227 if(logger.isDebugEnabled()){
228 logger.debug(testName + ": status = " + statusCode);
230 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
231 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
232 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
234 MultipartInput input = (MultipartInput) res.getEntity();
235 AcquisitionsCommon acquisitionObject = (AcquisitionsCommon) extractPart(input,
236 client.getCommonPartName(), AcquisitionsCommon.class);
237 Assert.assertNotNull(acquisitionObject);
243 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
244 dependsOnMethods = {"read"})
245 public void readNonExistent(String testName) throws Exception {
248 setupReadNonExistent(testName);
250 // Submit the request to the service and store the response.
251 AcquisitionClient client = new AcquisitionClient();
252 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
253 int statusCode = res.getStatus();
255 // Check the status code of the response: does it match
256 // the expected response(s)?
257 if(logger.isDebugEnabled()){
258 logger.debug(testName + ": status = " + statusCode);
260 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
261 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
262 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
265 // ---------------------------------------------------------------
266 // CRUD tests : READ_LIST tests
267 // ---------------------------------------------------------------
270 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
271 dependsOnMethods = {"createList", "read"})
272 public void readList(String testName) throws Exception {
275 setupReadList(testName);
277 // Submit the request to the service and store the response.
278 AcquisitionClient client = new AcquisitionClient();
279 ClientResponse<AcquisitionsCommonList> res = client.readList();
280 AcquisitionsCommonList list = res.getEntity();
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);
292 // Optionally output additional data about list members for debugging.
293 boolean iterateThroughList = false;
294 if(iterateThroughList && logger.isDebugEnabled()){
295 List<AcquisitionsCommonList.AcquisitionListItem> items =
296 list.getAcquisitionListItem();
298 for(AcquisitionsCommonList.AcquisitionListItem item : items){
299 logger.debug(testName + ": list-item[" + i + "] csid=" +
301 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
302 item.getAcquisitionReferenceNumber());
303 logger.debug(testName + ": list-item[" + i + "] acquisitionSources:");
304 AcquisitionSourceList acqSource = item.getAcquisitionSources();
305 for (String acquisitionSource : acqSource.getAcquisitionSource()) {
306 logger.debug("acquisitionSource=" + acquisitionSource);
308 logger.debug(testName + ": list-item[" + i + "] URI=" +
319 // ---------------------------------------------------------------
320 // CRUD tests : UPDATE tests
321 // ---------------------------------------------------------------
325 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
326 dependsOnMethods = {"read"})
327 public void update(String testName) throws Exception {
330 setupUpdate(testName);
332 // Retrieve the contents of a resource to update.
333 AcquisitionClient client = new AcquisitionClient();
334 ClientResponse<MultipartInput> res = client.read(knownResourceId);
335 if(logger.isDebugEnabled()){
336 logger.debug(testName + ": read status = " + res.getStatus());
338 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
340 if(logger.isDebugEnabled()){
341 logger.debug("got object to update with ID: " + knownResourceId);
343 MultipartInput input = (MultipartInput) res.getEntity();
345 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
346 client.getCommonPartName(), AcquisitionsCommon.class);
347 Assert.assertNotNull(acquisition);
349 // Update the content of this resource.
350 acquisition.setAcquisitionReferenceNumber("updated-" + acquisition.getAcquisitionReferenceNumber());
351 if(logger.isDebugEnabled()){
352 logger.debug("updated object");
353 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
355 // Submit the request to the service and store the response.
356 MultipartOutput output = new MultipartOutput();
357 OutputPart commonPart = output.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
358 commonPart.getHeaders().add("label", client.getCommonPartName());
360 res = client.update(knownResourceId, output);
361 int statusCode = res.getStatus();
362 // Check the status code of the response: does it match 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);
371 input = (MultipartInput) res.getEntity();
372 AcquisitionsCommon updatedAcquisition =
373 (AcquisitionsCommon) extractPart(input,
374 client.getCommonPartName(), AcquisitionsCommon.class);
375 Assert.assertNotNull(updatedAcquisition);
377 Assert.assertEquals(updatedAcquisition.getAcquisitionReferenceNumber(),
378 acquisition.getAcquisitionReferenceNumber(),
379 "Data in updated object did not match submitted data.");
384 // Placeholders until the three tests below can be uncommented.
385 // See Issue CSPACE-401.
387 public void updateWithEmptyEntityBody(String testName) throws Exception {
391 public void updateWithMalformedXml(String testName) throws Exception {
395 public void updateWithWrongXmlSchema(String testName) throws Exception {
400 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
401 dependsOnMethods = {"create", "update", "testSubmitRequest"})
402 public void updateWithEmptyEntityBody(String testName) throws Exception {
405 setupUpdateWithEmptyEntityBody(testName);
407 // Submit the request to the service and store the response.
408 String method = REQUEST_TYPE.httpMethodName();
409 String url = getResourceURL(knownResourceId);
410 String mediaType = MediaType.APPLICATION_XML;
411 final String entity = "";
412 int statusCode = submitRequest(method, url, mediaType, entity);
414 // Check the status code of the response: does it match
415 // the expected response(s)?
416 if(logger.isDebugEnabled()){
417 (testName + ": url=" + url + " status=" + statusCode);
419 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
420 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
421 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
425 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
426 dependsOnMethods = {"create", "testSubmitRequest"})
427 public void createWithEmptyEntityBody() throws Exception {
430 setupCreateWithEmptyEntityBody(testName);
432 // Submit the request to the service and store the response.
433 String method = REQUEST_TYPE.httpMethodName();
434 String url = getServiceRootURL();
435 String mediaType = MediaType.APPLICATION_XML;
436 final String entity = "";
437 int statusCode = submitRequest(method, url, mediaType, entity);
439 // Check the status code of the response: does it match
440 // the expected response(s)?
441 if(logger.isDebugEnabled()){
442 logger.debug(testName + ": url=" + url +
443 " status=" + statusCode);
445 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
446 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
447 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
451 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
452 dependsOnMethods = {"create", "update", "testSubmitRequest"})
453 public void updateWithMalformedXml(String testName) throws Exception {
456 setupUpdateWithMalformedXml(testName);
458 // Submit the request to the service and store the response.
459 String method = REQUEST_TYPE.httpMethodName();
460 String url = getResourceURL(knownResourceId);
461 final String entity = MALFORMED_XML_DATA;
462 int statusCode = submitRequest(method, url, entity);
464 // Check the status code of the response: does it match
465 // the expected response(s)?
466 if(logger.isDebugEnabled()){
467 logger.debug(testName + ": url=" + url +
468 " status=" + statusCode);
470 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
471 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
472 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
476 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
477 public void updateWithWrongXmlSchema(String testName) {
480 setupUpdateWithWrongXmlSchema(testName);
482 // Submit the request to the service and store the response.
483 String method = REQUEST_TYPE.httpMethodName();
484 String url = getResourceURL(knownResourceId);
485 final String entity = WRONG_XML_SCHEMA_DATA;
486 int statusCode = submitRequest(method, url, entity);
488 // Check the status code of the response: does it match
489 // the expected response(s)?
490 if(logger.isDebugEnabled()){
491 logger.debug(testName + ": url=" + url +
492 " status=" + statusCode);
494 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
495 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
496 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
501 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.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.
509 // Note: The ID used in this 'create' call may be arbitrary.
510 // The only relevant ID may be the one used in update(), below.
511 AcquisitionClient client = new AcquisitionClient();
512 MultipartOutput multipart = createAcquisitionInstance(NON_EXISTENT_ID);
513 ClientResponse<MultipartInput> res =
514 client.update(NON_EXISTENT_ID, multipart);
515 int statusCode = res.getStatus();
517 // Check the status code of the response: does it match
518 // the expected response(s)?
519 if(logger.isDebugEnabled()){
520 logger.debug(testName + ": status = " + statusCode);
522 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
523 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
524 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
527 // ---------------------------------------------------------------
528 // CRUD tests : DELETE tests
529 // ---------------------------------------------------------------
532 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
533 dependsOnMethods = {"create", "read", "update"})
534 public void delete(String testName) throws Exception {
537 setupDelete(testName);
539 // Submit the request to the service and store the response.
540 AcquisitionClient client = new AcquisitionClient();
541 ClientResponse<Response> res = client.delete(knownResourceId);
542 int statusCode = res.getStatus();
544 // Check the status code of the response: does it match
545 // the expected response(s)?
546 if(logger.isDebugEnabled()){
547 logger.debug(testName + ": status = " + statusCode);
549 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
550 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
551 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
556 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
557 dependsOnMethods = {"delete"})
558 public void deleteNonExistent(String testName) throws Exception {
561 setupDeleteNonExistent(testName);
563 // Submit the request to the service and store the response.
564 AcquisitionClient client = new AcquisitionClient();
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 // ---------------------------------------------------------------
611 * Deletes all resources created by tests, after all tests have been run.
613 * This cleanup method will always be run, even if one or more tests fail.
614 * For this reason, it attempts to remove all resources created
615 * at any point during testing, even if some of those resources
616 * may be expected to be deleted by certain tests.
618 @AfterClass(alwaysRun=true)
619 public void cleanUp() {
620 String noTest = System.getProperty("noTestCleanup");
621 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
622 if (logger.isDebugEnabled()) {
623 logger.debug("Skipping Cleanup phase ...");
627 if (logger.isDebugEnabled()) {
628 logger.debug("Cleaning up temporary resources created for testing ...");
630 AcquisitionClient client = new AcquisitionClient();
631 for (String resourceId : allResourceIdsCreated) {
632 // Note: Any non-success responses are ignored and not reported.
633 ClientResponse<Response> res = client.delete(resourceId);
637 // ---------------------------------------------------------------
638 // Utility methods used by tests above
639 // ---------------------------------------------------------------
641 public String getServicePathComponent() {
642 return new AcquisitionClient().getServicePathComponent();
646 private MultipartOutput createAcquisitionInstance(String identifier) {
647 AcquisitionsCommon acquisition = new AcquisitionsCommon();
648 acquisition.setAcquisitionReferenceNumber("acquisitionReferenceNumber-" + identifier);
649 AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
650 List<String> sources = acqSourcesList.getAcquisitionSource();
651 // @TODO Use properly formatted refNames for representative acquisition
652 // sources in this example test record. The following are mere placeholders.
653 sources.add("Donor Acquisition Source-" + identifier);
654 sources.add("Museum Acquisition Source-" + identifier);
655 acquisition.setAcquisitionSources(acqSourcesList);
656 MultipartOutput multipart = new MultipartOutput();
657 OutputPart commonPart = multipart.addPart(acquisition,
658 MediaType.APPLICATION_XML_TYPE);
659 commonPart.getHeaders().add("label", new AcquisitionClient().getCommonPartName());
661 if(logger.isDebugEnabled()){
662 logger.debug("to be created, acquisition common");
663 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));