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;
32 import org.collectionspace.services.acquisition.AcquisitionsCommon;
33 import org.collectionspace.services.acquisition.AcquisitionsCommonList;
34 import org.collectionspace.services.acquisition.AcquisitionSourceList;
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 * AcquisitionServiceTest, carries out tests against a
49 * deployed and running Acquisition Service.
51 * $LastChangedRevision: 621 $
52 * $LastChangedDate: 2009-09-02 16:49:01 -0700 (Wed, 02 Sep 2009) $
54 public class AcquisitionServiceTest extends AbstractServiceTestImpl {
56 private final Logger logger =
57 LoggerFactory.getLogger(AcquisitionServiceTest.class);
59 // Instance variables specific to this test.
60 private String knownResourceId = null;
61 private List<String> allResourceIdsCreated = new ArrayList();
63 // ---------------------------------------------------------------
64 // CRUD tests : CREATE tests
65 // ---------------------------------------------------------------
68 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
69 public void create(String testName) throws Exception {
71 // Perform setup, such as initializing the type of service request
72 // (e.g. CREATE, DELETE), its valid and expected status codes, and
73 // its associated HTTP method name (e.g. POST, DELETE).
74 setupCreate(testName);
76 // Submit the request to the service and store the response.
77 String identifier = createIdentifier();
79 AcquisitionClient client = new AcquisitionClient();
80 MultipartOutput multipart = createAcquisitionInstance(identifier);
81 ClientResponse<Response> res = client.create(multipart);
83 int statusCode = res.getStatus();
85 // Check the status code of the response: does it match
86 // the expected response(s)?
89 // Does it fall within the set of valid status codes?
90 // Does it exactly match the expected status code?
91 if(logger.isDebugEnabled()){
92 logger.debug(testName + ": status = " + statusCode);
94 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
95 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
96 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
98 // Store the ID returned from the first resource created
99 // for additional tests below.
100 if (knownResourceId == null){
101 knownResourceId = extractId(res);
102 if (logger.isDebugEnabled()) {
103 logger.debug(testName + ": knownResourceId=" + knownResourceId);
107 // Store the IDs from every resource created by tests,
108 // so they can be deleted after tests have been run.
109 allResourceIdsCreated.add(extractId(res));
113 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
114 dependsOnMethods = {"create"})
115 public void createList(String testName) throws Exception {
116 for(int i = 0; i < 3; i++){
122 // Placeholders until the three tests below can be uncommented.
123 // See Issue CSPACE-401.
125 public void createWithEmptyEntityBody(String testName) throws Exception {
129 public void createWithMalformedXml(String testName) throws Exception {
133 public void createWithWrongXmlSchema(String testName) throws Exception {
138 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
139 dependsOnMethods = {"create", "testSubmitRequest"})
140 public void createWithMalformedXml(String testName) throws Exception {
143 setupCreateWithMalformedXml();
145 // Submit the request to the service and store the response.
146 String method = REQUEST_TYPE.httpMethodName();
147 String url = getServiceRootURL();
148 final String entity = MALFORMED_XML_DATA; // Constant from base class.
149 int statusCode = submitRequest(method, url, entity);
151 // Check the status code of the response: does it match
152 // the expected response(s)?
153 if(logger.isDebugEnabled()){
154 logger.debug(testName + ": url=" + url +
155 " status=" + statusCode);
157 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
158 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
159 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
163 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
164 dependsOnMethods = {"create", "testSubmitRequest"})
165 public void createWithWrongXmlSchema() throws Exception {
168 setupCreateWithWrongXmlSchema();
170 // Submit the request to the service and store the response.
171 String method = REQUEST_TYPE.httpMethodName();
172 String url = getServiceRootURL();
173 final String entity = WRONG_XML_SCHEMA_DATA;
174 int statusCode = submitRequest(method, url, entity);
176 // Check the status code of the response: does it match
177 // the expected response(s)?
178 if(logger.isDebugEnabled()){
179 logger.debug(testName + ": url=" + url +
180 " status=" + statusCode);
182 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
183 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
184 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
188 // ---------------------------------------------------------------
189 // CRUD tests : READ tests
190 // ---------------------------------------------------------------
193 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
194 dependsOnMethods = {"create"})
195 public void read(String testName) throws Exception {
200 AcquisitionClient client = new AcquisitionClient();
202 // Submit the request to the service and store the response.
203 ClientResponse<MultipartInput> res = client.read(knownResourceId);
204 int statusCode = res.getStatus();
206 // Check the status code of the response: does it match
207 // the expected response(s)?
208 if(logger.isDebugEnabled()){
209 logger.debug(testName + ": status = " + statusCode);
211 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
212 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
213 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
215 MultipartInput input = (MultipartInput) res.getEntity();
216 AcquisitionsCommon acquisitionObject = (AcquisitionsCommon) extractPart(input,
217 client.getCommonPartName(), AcquisitionsCommon.class);
218 Assert.assertNotNull(acquisitionObject);
224 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
225 dependsOnMethods = {"read"})
226 public void readNonExistent(String testName) throws Exception {
229 setupReadNonExistent(testName);
231 // Submit the request to the service and store the response.
232 AcquisitionClient client = new AcquisitionClient();
233 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
234 int statusCode = res.getStatus();
236 // Check the status code of the response: does it match
237 // the expected response(s)?
238 if(logger.isDebugEnabled()){
239 logger.debug(testName + ": status = " + statusCode);
241 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
242 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
243 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
246 // ---------------------------------------------------------------
247 // CRUD tests : READ_LIST tests
248 // ---------------------------------------------------------------
251 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
252 dependsOnMethods = {"createList", "read"})
253 public void readList(String testName) throws Exception {
256 setupReadList(testName);
258 // Submit the request to the service and store the response.
259 AcquisitionClient client = new AcquisitionClient();
260 ClientResponse<AcquisitionsCommonList> res = client.readList();
261 AcquisitionsCommonList list = res.getEntity();
262 int statusCode = res.getStatus();
264 // Check the status code of the response: does it match
265 // the expected response(s)?
266 if(logger.isDebugEnabled()){
267 logger.debug(testName + ": status = " + statusCode);
269 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
270 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
271 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
273 // Optionally output additional data about list members for debugging.
274 boolean iterateThroughList = false;
275 if(iterateThroughList && logger.isDebugEnabled()){
276 List<AcquisitionsCommonList.AcquisitionListItem> items =
277 list.getAcquisitionListItem();
279 for(AcquisitionsCommonList.AcquisitionListItem item : items){
280 logger.debug(testName + ": list-item[" + i + "] csid=" +
282 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
283 item.getAcquisitionReferenceNumber());
284 logger.debug(testName + ": list-item[" + i + "] acquisitionSources:");
285 AcquisitionSourceList acqSource = item.getAcquisitionSources();
286 for (String acquisitionSource : acqSource.getAcquisitionSource()) {
287 logger.debug("acquisitionSource=" + acquisitionSource);
289 logger.debug(testName + ": list-item[" + i + "] URI=" +
300 // ---------------------------------------------------------------
301 // CRUD tests : UPDATE tests
302 // ---------------------------------------------------------------
306 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
307 dependsOnMethods = {"read"})
308 public void update(String testName) throws Exception {
311 setupUpdate(testName);
313 // Retrieve the contents of a resource to update.
314 AcquisitionClient client = new AcquisitionClient();
315 ClientResponse<MultipartInput> res = client.read(knownResourceId);
316 if(logger.isDebugEnabled()){
317 logger.debug(testName + ": read status = " + res.getStatus());
319 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
321 if(logger.isDebugEnabled()){
322 logger.debug("got object to update with ID: " + knownResourceId);
324 MultipartInput input = (MultipartInput) res.getEntity();
326 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
327 client.getCommonPartName(), AcquisitionsCommon.class);
328 Assert.assertNotNull(acquisition);
330 // Update the content of this resource.
331 acquisition.setAcquisitionReferenceNumber("updated-" + acquisition.getAcquisitionReferenceNumber());
332 if(logger.isDebugEnabled()){
333 logger.debug("updated object");
334 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
336 // Submit the request to the service and store the response.
337 MultipartOutput output = new MultipartOutput();
338 OutputPart commonPart = output.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
339 commonPart.getHeaders().add("label", client.getCommonPartName());
341 res = client.update(knownResourceId, output);
342 int statusCode = res.getStatus();
343 // Check the status code of the response: does it match the expected response(s)?
344 if(logger.isDebugEnabled()){
345 logger.debug(testName + ": status = " + statusCode);
347 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
348 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
349 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
352 input = (MultipartInput) res.getEntity();
353 AcquisitionsCommon updatedAcquisition =
354 (AcquisitionsCommon) extractPart(input,
355 client.getCommonPartName(), AcquisitionsCommon.class);
356 Assert.assertNotNull(updatedAcquisition);
358 Assert.assertEquals(updatedAcquisition.getAcquisitionReferenceNumber(),
359 acquisition.getAcquisitionReferenceNumber(),
360 "Data in updated object did not match submitted data.");
365 // Placeholders until the three tests below can be uncommented.
366 // See Issue CSPACE-401.
368 public void updateWithEmptyEntityBody(String testName) throws Exception {
372 public void updateWithMalformedXml(String testName) throws Exception {
376 public void updateWithWrongXmlSchema(String testName) throws Exception {
381 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
382 dependsOnMethods = {"create", "update", "testSubmitRequest"})
383 public void updateWithEmptyEntityBody(String testName) throws Exception {
386 setupUpdateWithEmptyEntityBody(testName);
388 // Submit the request to the service and store the response.
389 String method = REQUEST_TYPE.httpMethodName();
390 String url = getResourceURL(knownResourceId);
391 String mediaType = MediaType.APPLICATION_XML;
392 final String entity = "";
393 int statusCode = submitRequest(method, url, mediaType, entity);
395 // Check the status code of the response: does it match
396 // the expected response(s)?
397 if(logger.isDebugEnabled()){
398 (testName + ": url=" + url + " status=" + statusCode);
400 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
401 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
402 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
406 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
407 dependsOnMethods = {"create", "testSubmitRequest"})
408 public void createWithEmptyEntityBody() throws Exception {
411 setupCreateWithEmptyEntityBody(testName);
413 // Submit the request to the service and store the response.
414 String method = REQUEST_TYPE.httpMethodName();
415 String url = getServiceRootURL();
416 String mediaType = MediaType.APPLICATION_XML;
417 final String entity = "";
418 int statusCode = submitRequest(method, url, mediaType, entity);
420 // Check the status code of the response: does it match
421 // the expected response(s)?
422 if(logger.isDebugEnabled()){
423 logger.debug(testName + ": url=" + url +
424 " status=" + statusCode);
426 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
427 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
428 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
432 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
433 dependsOnMethods = {"create", "update", "testSubmitRequest"})
434 public void updateWithMalformedXml(String testName) throws Exception {
437 setupUpdateWithMalformedXml(testName);
439 // Submit the request to the service and store the response.
440 String method = REQUEST_TYPE.httpMethodName();
441 String url = getResourceURL(knownResourceId);
442 final String entity = MALFORMED_XML_DATA;
443 int statusCode = submitRequest(method, url, entity);
445 // Check the status code of the response: does it match
446 // the expected response(s)?
447 if(logger.isDebugEnabled()){
448 logger.debug(testName + ": url=" + url +
449 " status=" + statusCode);
451 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
452 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
453 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
457 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
458 public void updateWithWrongXmlSchema(String testName) {
461 setupUpdateWithWrongXmlSchema(testName);
463 // Submit the request to the service and store the response.
464 String method = REQUEST_TYPE.httpMethodName();
465 String url = getResourceURL(knownResourceId);
466 final String entity = WRONG_XML_SCHEMA_DATA;
467 int statusCode = submitRequest(method, url, entity);
469 // Check the status code of the response: does it match
470 // the expected response(s)?
471 if(logger.isDebugEnabled()){
472 logger.debug(testName + ": url=" + url +
473 " status=" + statusCode);
475 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
476 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
477 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
482 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
483 dependsOnMethods = {"update", "testSubmitRequest"})
484 public void updateNonExistent(String testName) throws Exception {
487 setupUpdateNonExistent(testName);
489 // Submit the request to the service and store the response.
490 // Note: The ID used in this 'create' call may be arbitrary.
491 // The only relevant ID may be the one used in update(), below.
492 AcquisitionClient client = new AcquisitionClient();
493 MultipartOutput multipart = createAcquisitionInstance(NON_EXISTENT_ID);
494 ClientResponse<MultipartInput> res =
495 client.update(NON_EXISTENT_ID, multipart);
496 int statusCode = res.getStatus();
498 // Check the status code of the response: does it match
499 // the expected response(s)?
500 if(logger.isDebugEnabled()){
501 logger.debug(testName + ": status = " + statusCode);
503 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
504 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
505 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
508 // ---------------------------------------------------------------
509 // CRUD tests : DELETE tests
510 // ---------------------------------------------------------------
513 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
514 dependsOnMethods = {"create", "read", "update"})
515 public void delete(String testName) throws Exception {
518 setupDelete(testName);
520 // Submit the request to the service and store the response.
521 AcquisitionClient client = new AcquisitionClient();
522 ClientResponse<Response> res = client.delete(knownResourceId);
523 int statusCode = res.getStatus();
525 // Check the status code of the response: does it match
526 // the expected response(s)?
527 if(logger.isDebugEnabled()){
528 logger.debug(testName + ": status = " + statusCode);
530 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
531 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
532 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
537 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
538 dependsOnMethods = {"delete"})
539 public void deleteNonExistent(String testName) throws Exception {
542 setupDeleteNonExistent(testName);
544 // Submit the request to the service and store the response.
545 AcquisitionClient client = new AcquisitionClient();
546 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
547 int statusCode = res.getStatus();
549 // Check the status code of the response: does it match
550 // the expected response(s)?
551 if(logger.isDebugEnabled()){
552 logger.debug(testName + ": status = " + statusCode);
554 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
555 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
556 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
559 // ---------------------------------------------------------------
560 // Utility tests : tests of code used in tests above
561 // ---------------------------------------------------------------
563 * Tests the code for manually submitting data that is used by several
564 * of the methods above.
566 @Test(dependsOnMethods = {"create", "read"})
567 public void testSubmitRequest() throws Exception {
569 // Expected status code: 200 OK
570 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
572 // Submit the request to the service and store the response.
573 String method = ServiceRequestType.READ.httpMethodName();
574 String url = getResourceURL(knownResourceId);
575 int statusCode = submitRequest(method, url);
577 // Check the status code of the response: does it match
578 // the expected response(s)?
579 if(logger.isDebugEnabled()){
580 logger.debug("testSubmitRequest: url=" + url +
581 " status=" + statusCode);
583 Assert.assertEquals(statusCode, EXPECTED_STATUS);
587 // ---------------------------------------------------------------
588 // Cleanup of resources created during testing
589 // ---------------------------------------------------------------
592 * Deletes all resources created by tests, after all tests have been run.
594 * This cleanup method will always be run, even if one or more tests fail.
595 * For this reason, it attempts to remove all resources created
596 * at any point during testing, even if some of those resources
597 * may be expected to be deleted by certain tests.
599 @AfterClass(alwaysRun=true)
600 public void cleanUp() {
601 String noTest = System.getProperty("noTestCleanup");
602 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
603 if (logger.isDebugEnabled()) {
604 logger.debug("Skipping Cleanup phase ...");
608 if (logger.isDebugEnabled()) {
609 logger.debug("Cleaning up temporary resources created for testing ...");
611 AcquisitionClient client = new AcquisitionClient();
612 for (String resourceId : allResourceIdsCreated) {
613 // Note: Any non-success responses are ignored and not reported.
614 ClientResponse<Response> res = client.delete(resourceId);
618 // ---------------------------------------------------------------
619 // Utility methods used by tests above
620 // ---------------------------------------------------------------
622 public String getServicePathComponent() {
623 return new AcquisitionClient().getServicePathComponent();
627 private MultipartOutput createAcquisitionInstance(String identifier) {
628 AcquisitionsCommon acquisition = new AcquisitionsCommon();
629 acquisition.setAcquisitionReferenceNumber("acquisitionReferenceNumber-" + identifier);
630 AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
631 List<String> sources = acqSourcesList.getAcquisitionSource();
632 // @TODO Use properly formatted refNames for representative acquisition
633 // sources in this example test record. The following are mere placeholders.
634 sources.add("Donor Acquisition Source-" + identifier);
635 sources.add("Museum Acquisition Source-" + identifier);
636 acquisition.setAcquisitionSources(acqSourcesList);
637 MultipartOutput multipart = new MultipartOutput();
638 OutputPart commonPart = multipart.addPart(acquisition,
639 MediaType.APPLICATION_XML_TYPE);
640 commonPart.getHeaders().add("label", new AcquisitionClient().getCommonPartName());
642 if(logger.isDebugEnabled()){
643 logger.debug("to be created, acquisition common");
644 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));