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 AcquisitionClient client = new AcquisitionClient();
61 private String knownResourceId = null;
62 private List<String> allResourceIdsCreated = new ArrayList();
64 // ---------------------------------------------------------------
65 // CRUD tests : CREATE tests
66 // ---------------------------------------------------------------
69 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
70 public void create(String testName) throws Exception {
72 // Perform setup, such as initializing the type of service request
73 // (e.g. CREATE, DELETE), its valid and expected status codes, and
74 // its associated HTTP method name (e.g. POST, DELETE).
75 setupCreate(testName);
77 // Submit the request to the service and store the response.
78 String identifier = createIdentifier();
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 // Submit the request to the service and store the response.
201 ClientResponse<MultipartInput> res = client.read(knownResourceId);
202 int statusCode = res.getStatus();
204 // Check the status code of the response: does it match
205 // the expected response(s)?
206 if(logger.isDebugEnabled()){
207 logger.debug(testName + ": status = " + statusCode);
209 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
210 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
211 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
213 MultipartInput input = (MultipartInput) res.getEntity();
214 AcquisitionsCommon acquisitionObject = (AcquisitionsCommon) extractPart(input,
215 client.getCommonPartName(), AcquisitionsCommon.class);
216 Assert.assertNotNull(acquisitionObject);
222 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
223 dependsOnMethods = {"read"})
224 public void readNonExistent(String testName) throws Exception {
227 setupReadNonExistent(testName);
229 // Submit the request to the service and store the response.
230 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
231 int statusCode = res.getStatus();
233 // Check the status code of the response: does it match
234 // the expected response(s)?
235 if(logger.isDebugEnabled()){
236 logger.debug(testName + ": status = " + statusCode);
238 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
239 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
240 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
243 // ---------------------------------------------------------------
244 // CRUD tests : READ_LIST tests
245 // ---------------------------------------------------------------
248 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
249 dependsOnMethods = {"createList", "read"})
250 public void readList(String testName) throws Exception {
253 setupReadList(testName);
255 // Submit the request to the service and store the response.
256 ClientResponse<AcquisitionsCommonList> res = client.readList();
257 AcquisitionsCommonList list = res.getEntity();
258 int statusCode = res.getStatus();
260 // Check the status code of the response: does it match
261 // the expected response(s)?
262 if(logger.isDebugEnabled()){
263 logger.debug(testName + ": status = " + statusCode);
265 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
266 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
267 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
269 // Optionally output additional data about list members for debugging.
270 boolean iterateThroughList = false;
271 if(iterateThroughList && logger.isDebugEnabled()){
272 List<AcquisitionsCommonList.AcquisitionListItem> items =
273 list.getAcquisitionListItem();
275 for(AcquisitionsCommonList.AcquisitionListItem item : items){
276 logger.debug(testName + ": list-item[" + i + "] csid=" +
278 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
279 item.getAcquisitionReferenceNumber());
280 logger.debug(testName + ": list-item[" + i + "] URI=" +
291 // ---------------------------------------------------------------
292 // CRUD tests : UPDATE tests
293 // ---------------------------------------------------------------
297 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
298 dependsOnMethods = {"read"})
299 public void update(String testName) throws Exception {
302 setupUpdate(testName);
304 ClientResponse<MultipartInput> res =
305 client.read(knownResourceId);
306 if(logger.isDebugEnabled()){
307 logger.debug(testName + ": read status = " + res.getStatus());
309 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
311 if(logger.isDebugEnabled()){
312 logger.debug("got object to update with ID: " + knownResourceId);
314 MultipartInput input = (MultipartInput) res.getEntity();
315 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
316 client.getCommonPartName(), AcquisitionsCommon.class);
317 Assert.assertNotNull(acquisition);
319 // Update the content of this resource.
320 acquisition.setAcquisitionReferenceNumber("updated-" + acquisition.getAcquisitionReferenceNumber());
321 if(logger.isDebugEnabled()){
322 logger.debug("updated object");
323 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
325 // Submit the request to the service and store the response.
326 MultipartOutput output = new MultipartOutput();
327 OutputPart commonPart = output.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
328 commonPart.getHeaders().add("label", client.getCommonPartName());
330 res = client.update(knownResourceId, output);
331 int statusCode = res.getStatus();
332 // Check the status code of the response: does it match the expected response(s)?
333 if(logger.isDebugEnabled()){
334 logger.debug(testName + ": status = " + statusCode);
336 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
337 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
338 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
341 input = (MultipartInput) res.getEntity();
342 AcquisitionsCommon updatedAcquisition =
343 (AcquisitionsCommon) extractPart(input,
344 client.getCommonPartName(), AcquisitionsCommon.class);
345 Assert.assertNotNull(updatedAcquisition);
347 Assert.assertEquals(updatedAcquisition.getAcquisitionReferenceNumber(),
348 acquisition.getAcquisitionReferenceNumber(),
349 "Data in updated object did not match submitted data.");
354 // Placeholders until the three tests below can be uncommented.
355 // See Issue CSPACE-401.
357 public void updateWithEmptyEntityBody(String testName) throws Exception {
361 public void updateWithMalformedXml(String testName) throws Exception {
365 public void updateWithWrongXmlSchema(String testName) throws Exception {
370 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
371 dependsOnMethods = {"create", "update", "testSubmitRequest"})
372 public void updateWithEmptyEntityBody(String testName) throws Exception {
375 setupUpdateWithEmptyEntityBody(testName);
377 // Submit the request to the service and store the response.
378 String method = REQUEST_TYPE.httpMethodName();
379 String url = getResourceURL(knownResourceId);
380 String mediaType = MediaType.APPLICATION_XML;
381 final String entity = "";
382 int statusCode = submitRequest(method, url, mediaType, entity);
384 // Check the status code of the response: does it match
385 // the expected response(s)?
386 if(logger.isDebugEnabled()){
387 (testName + ": url=" + url + " status=" + statusCode);
389 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
390 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
391 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
395 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
396 dependsOnMethods = {"create", "testSubmitRequest"})
397 public void createWithEmptyEntityBody() throws Exception {
400 setupCreateWithEmptyEntityBody(testName);
402 // Submit the request to the service and store the response.
403 String method = REQUEST_TYPE.httpMethodName();
404 String url = getServiceRootURL();
405 String mediaType = MediaType.APPLICATION_XML;
406 final String entity = "";
407 int statusCode = submitRequest(method, url, mediaType, entity);
409 // Check the status code of the response: does it match
410 // the expected response(s)?
411 if(logger.isDebugEnabled()){
412 logger.debug(testName + ": url=" + url +
413 " status=" + statusCode);
415 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
416 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
417 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
421 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
422 dependsOnMethods = {"create", "update", "testSubmitRequest"})
423 public void updateWithMalformedXml(String testName) throws Exception {
426 setupUpdateWithMalformedXml(testName);
428 // Submit the request to the service and store the response.
429 String method = REQUEST_TYPE.httpMethodName();
430 String url = getResourceURL(knownResourceId);
431 final String entity = MALFORMED_XML_DATA;
432 int statusCode = submitRequest(method, url, entity);
434 // Check the status code of the response: does it match
435 // the expected response(s)?
436 if(logger.isDebugEnabled()){
437 logger.debug(testName + ": url=" + url +
438 " status=" + statusCode);
440 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
441 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
442 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
446 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
447 public void updateWithWrongXmlSchema(String testName) {
450 setupUpdateWithWrongXmlSchema(testName);
452 // Submit the request to the service and store the response.
453 String method = REQUEST_TYPE.httpMethodName();
454 String url = getResourceURL(knownResourceId);
455 final String entity = WRONG_XML_SCHEMA_DATA;
456 int statusCode = submitRequest(method, url, entity);
458 // Check the status code of the response: does it match
459 // the expected response(s)?
460 if(logger.isDebugEnabled()){
461 logger.debug(testName + ": url=" + url +
462 " status=" + statusCode);
464 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
465 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
466 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
471 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
472 dependsOnMethods = {"update", "testSubmitRequest"})
473 public void updateNonExistent(String testName) throws Exception {
476 setupUpdateNonExistent(testName);
478 // Submit the request to the service and store the response.
479 // Note: The ID used in this 'create' call may be arbitrary.
480 // The only relevant ID may be the one used in update(), below.
481 MultipartOutput multipart = createAcquisitionInstance(NON_EXISTENT_ID);
482 ClientResponse<MultipartInput> res =
483 client.update(NON_EXISTENT_ID, multipart);
484 int statusCode = res.getStatus();
486 // Check the status code of the response: does it match
487 // the expected response(s)?
488 if(logger.isDebugEnabled()){
489 logger.debug(testName + ": status = " + statusCode);
491 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
492 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
493 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
496 // ---------------------------------------------------------------
497 // CRUD tests : DELETE tests
498 // ---------------------------------------------------------------
501 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
502 dependsOnMethods = {"create", "read", "update"})
503 public void delete(String testName) throws Exception {
506 setupDelete(testName);
508 // Submit the request to the service and store the response.
509 ClientResponse<Response> res = client.delete(knownResourceId);
510 int statusCode = res.getStatus();
512 // Check the status code of the response: does it match
513 // the expected response(s)?
514 if(logger.isDebugEnabled()){
515 logger.debug(testName + ": status = " + statusCode);
517 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
518 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
519 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
524 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
525 dependsOnMethods = {"delete"})
526 public void deleteNonExistent(String testName) throws Exception {
529 setupDeleteNonExistent(testName);
531 // Submit the request to the service and store the response.
532 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
533 int statusCode = res.getStatus();
535 // Check the status code of the response: does it match
536 // the expected response(s)?
537 if(logger.isDebugEnabled()){
538 logger.debug(testName + ": status = " + statusCode);
540 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
541 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
542 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
545 // ---------------------------------------------------------------
546 // Utility tests : tests of code used in tests above
547 // ---------------------------------------------------------------
549 * Tests the code for manually submitting data that is used by several
550 * of the methods above.
552 @Test(dependsOnMethods = {"create", "read"})
553 public void testSubmitRequest() throws Exception {
555 // Expected status code: 200 OK
556 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
558 // Submit the request to the service and store the response.
559 String method = ServiceRequestType.READ.httpMethodName();
560 String url = getResourceURL(knownResourceId);
561 int statusCode = submitRequest(method, url);
563 // Check the status code of the response: does it match
564 // the expected response(s)?
565 if(logger.isDebugEnabled()){
566 logger.debug("testSubmitRequest: url=" + url +
567 " status=" + statusCode);
569 Assert.assertEquals(statusCode, EXPECTED_STATUS);
573 // ---------------------------------------------------------------
574 // Cleanup of resources created during testing
575 // ---------------------------------------------------------------
578 * Deletes all resources created by tests, after all tests have been run.
580 * This cleanup method will always be run, even if one or more tests fail.
581 * For this reason, it attempts to remove all resources created
582 * at any point during testing, even if some of those resources
583 * may be expected to be deleted by certain tests.
585 @AfterClass(alwaysRun=true)
586 public void cleanUp() {
587 if (logger.isDebugEnabled()) {
588 logger.debug("Cleaning up temporary resources created for testing ...");
590 for (String resourceId : allResourceIdsCreated) {
591 // Note: Any non-success responses are ignored and not reported.
592 ClientResponse<Response> res = client.delete(resourceId);
596 // ---------------------------------------------------------------
597 // Utility methods used by tests above
598 // ---------------------------------------------------------------
600 public String getServicePathComponent() {
601 return client.getServicePathComponent();
605 private MultipartOutput createAcquisitionInstance(String identifier) {
606 AcquisitionsCommon acquisition = new AcquisitionsCommon();
607 acquisition.setAcquisitionReferenceNumber("acquisitionReferenceNumber-" + identifier);
608 AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
609 List<String> sources = acqSourcesList.getAcquisitionSource();
610 // @TODO Use properly formatted refNames for representative acquisition
611 // sources in this example test record. The following are mere placeholders.
612 sources.add("Donor Acquisition Source " + identifier);
613 sources.add("Museum Acquisition Source" + identifier);
614 MultipartOutput multipart = new MultipartOutput();
615 OutputPart commonPart = multipart.addPart(acquisition,
616 MediaType.APPLICATION_XML_TYPE);
617 commonPart.getHeaders().add("label", client.getCommonPartName());
619 if(logger.isDebugEnabled()){
620 logger.debug("to be created, acquisition common");
621 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));