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.jboss.resteasy.client.ClientResponse;
36 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
37 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
38 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
39 import org.testng.Assert;
40 import org.testng.annotations.AfterClass;
41 import org.testng.annotations.Test;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * AcquisitionServiceTest, carries out tests against a
48 * deployed and running Acquisition Service.
50 * $LastChangedRevision: 621 $
51 * $LastChangedDate: 2009-09-02 16:49:01 -0700 (Wed, 02 Sep 2009) $
53 public class AcquisitionServiceTest extends AbstractServiceTest {
55 private final Logger logger =
56 LoggerFactory.getLogger(AcquisitionServiceTest.class);
58 // Instance variables specific to this test.
59 private AcquisitionClient client = new AcquisitionClient();
60 private String knownResourceId = null;
61 private List<String> allResourceIdsCreated = new ArrayList();
63 // ---------------------------------------------------------------
64 // CRUD tests : CREATE tests
65 // ---------------------------------------------------------------
68 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.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 MultipartOutput multipart = createAcquisitionInstance(identifier);
80 ClientResponse<Response> res = client.create(multipart);
82 int statusCode = res.getStatus();
84 // Check the status code of the response: does it match
85 // the expected response(s)?
88 // Does it fall within the set of valid status codes?
89 // Does it exactly match the expected status code?
90 if(logger.isDebugEnabled()){
91 logger.debug(testName + ": status = " + statusCode);
93 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
94 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
95 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
97 // Store the ID returned from the first resource created
98 // for additional tests below.
99 if (knownResourceId == null){
100 knownResourceId = extractId(res);
101 if (logger.isDebugEnabled()) {
102 logger.debug(testName + ": knownResourceId=" + knownResourceId);
106 // Store the IDs from every resource created by tests,
107 // so they can be deleted after tests have been run.
108 allResourceIdsCreated.add(extractId(res));
112 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
113 dependsOnMethods = {"create"})
114 public void createList(String testName) throws Exception {
115 for(int i = 0; i < 3; i++){
121 // Placeholders until the three tests below can be uncommented.
122 // See Issue CSPACE-401.
124 public void createWithEmptyEntityBody(String testName) throws Exception {
128 public void createWithMalformedXml(String testName) throws Exception {
132 public void createWithWrongXmlSchema(String testName) throws Exception {
137 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
138 dependsOnMethods = {"create", "testSubmitRequest"})
139 public void createWithMalformedXml(String testName) throws Exception {
142 setupCreateWithMalformedXml();
144 // Submit the request to the service and store the response.
145 String method = REQUEST_TYPE.httpMethodName();
146 String url = getServiceRootURL();
147 final String entity = MALFORMED_XML_DATA; // Constant from base class.
148 int statusCode = submitRequest(method, url, entity);
150 // Check the status code of the response: does it match
151 // the expected response(s)?
152 if(logger.isDebugEnabled()){
153 logger.debug(testName + ": url=" + url +
154 " status=" + statusCode);
156 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
157 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
158 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
162 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
163 dependsOnMethods = {"create", "testSubmitRequest"})
164 public void createWithWrongXmlSchema() throws Exception {
167 setupCreateWithWrongXmlSchema();
169 // Submit the request to the service and store the response.
170 String method = REQUEST_TYPE.httpMethodName();
171 String url = getServiceRootURL();
172 final String entity = WRONG_XML_SCHEMA_DATA;
173 int statusCode = submitRequest(method, url, entity);
175 // Check the status code of the response: does it match
176 // the expected response(s)?
177 if(logger.isDebugEnabled()){
178 logger.debug(testName + ": url=" + url +
179 " status=" + statusCode);
181 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
182 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
183 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
187 // ---------------------------------------------------------------
188 // CRUD tests : READ tests
189 // ---------------------------------------------------------------
192 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
193 dependsOnMethods = {"create"})
194 public void read(String testName) throws Exception {
199 // Submit the request to the service and store the response.
200 ClientResponse<MultipartInput> res = client.read(knownResourceId);
201 int statusCode = res.getStatus();
203 // Check the status code of the response: does it match
204 // the expected response(s)?
205 if(logger.isDebugEnabled()){
206 logger.debug(testName + ": status = " + statusCode);
208 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
209 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
210 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
212 MultipartInput input = (MultipartInput) res.getEntity();
213 AcquisitionsCommon acquisitionObject = (AcquisitionsCommon) extractPart(input,
214 client.getCommonPartName(), AcquisitionsCommon.class);
215 Assert.assertNotNull(acquisitionObject);
221 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
222 dependsOnMethods = {"read"})
223 public void readNonExistent(String testName) throws Exception {
226 setupReadNonExistent(testName);
228 // Submit the request to the service and store the response.
229 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
230 int statusCode = res.getStatus();
232 // Check the status code of the response: does it match
233 // the expected response(s)?
234 if(logger.isDebugEnabled()){
235 logger.debug(testName + ": status = " + statusCode);
237 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
238 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
239 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
242 // ---------------------------------------------------------------
243 // CRUD tests : READ_LIST tests
244 // ---------------------------------------------------------------
247 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
248 dependsOnMethods = {"createList", "read"})
249 public void readList(String testName) throws Exception {
252 setupReadList(testName);
254 // Submit the request to the service and store the response.
255 ClientResponse<AcquisitionsCommonList> res = client.readList();
256 AcquisitionsCommonList list = res.getEntity();
257 int statusCode = res.getStatus();
259 // Check the status code of the response: does it match
260 // the expected response(s)?
261 if(logger.isDebugEnabled()){
262 logger.debug(testName + ": status = " + statusCode);
264 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
265 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
266 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
268 // Optionally output additional data about list members for debugging.
269 boolean iterateThroughList = false;
270 if(iterateThroughList && logger.isDebugEnabled()){
271 List<AcquisitionsCommonList.AcquisitionListItem> items =
272 list.getAcquisitionListItem();
274 for(AcquisitionsCommonList.AcquisitionListItem item : items){
275 logger.debug(testName + ": list-item[" + i + "] csid=" +
277 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
278 item.getAccessionDate());
279 logger.debug(testName + ": list-item[" + i + "] URI=" +
290 // ---------------------------------------------------------------
291 // CRUD tests : UPDATE tests
292 // ---------------------------------------------------------------
296 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
297 dependsOnMethods = {"read"})
298 public void update(String testName) throws Exception {
301 setupUpdate(testName);
303 ClientResponse<MultipartInput> res =
304 client.read(knownResourceId);
305 if(logger.isDebugEnabled()){
306 logger.debug(testName + ": read status = " + res.getStatus());
308 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
310 if(logger.isDebugEnabled()){
311 logger.debug("got object to update with ID: " + knownResourceId);
313 MultipartInput input = (MultipartInput) res.getEntity();
314 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
315 client.getCommonPartName(), AcquisitionsCommon.class);
316 Assert.assertNotNull(acquisition);
318 // Update the content of this resource.
319 acquisition.setAccessionDate("updated-" + acquisition.getAccessionDate());
320 if(logger.isDebugEnabled()){
321 logger.debug("updated object");
322 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
324 // Submit the request to the service and store the response.
325 MultipartOutput output = new MultipartOutput();
326 OutputPart commonPart = output.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
327 commonPart.getHeaders().add("label", client.getCommonPartName());
329 res = client.update(knownResourceId, output);
330 int statusCode = res.getStatus();
331 // Check the status code of the response: does it match the expected response(s)?
332 if(logger.isDebugEnabled()){
333 logger.debug(testName + ": status = " + statusCode);
335 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
336 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
337 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
340 input = (MultipartInput) res.getEntity();
341 AcquisitionsCommon updatedAcquisition =
342 (AcquisitionsCommon) extractPart(input,
343 client.getCommonPartName(), AcquisitionsCommon.class);
344 Assert.assertNotNull(updatedAcquisition);
346 Assert.assertEquals(updatedAcquisition.getAccessionDate(),
347 acquisition.getAccessionDate(),
348 "Data in updated object did not match submitted data.");
353 // Placeholders until the three tests below can be uncommented.
354 // See Issue CSPACE-401.
356 public void updateWithEmptyEntityBody(String testName) throws Exception {
360 public void updateWithMalformedXml(String testName) throws Exception {
364 public void updateWithWrongXmlSchema(String testName) throws Exception {
369 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
370 dependsOnMethods = {"create", "update", "testSubmitRequest"})
371 public void updateWithEmptyEntityBody(String testName) throws Exception {
374 setupUpdateWithEmptyEntityBody(testName);
376 // Submit the request to the service and store the response.
377 String method = REQUEST_TYPE.httpMethodName();
378 String url = getResourceURL(knownResourceId);
379 String mediaType = MediaType.APPLICATION_XML;
380 final String entity = "";
381 int statusCode = submitRequest(method, url, mediaType, entity);
383 // Check the status code of the response: does it match
384 // the expected response(s)?
385 if(logger.isDebugEnabled()){
386 (testName + ": url=" + url + " status=" + statusCode);
388 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
389 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
390 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
394 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
395 dependsOnMethods = {"create", "testSubmitRequest"})
396 public void createWithEmptyEntityBody() throws Exception {
399 setupCreateWithEmptyEntityBody(testName);
401 // Submit the request to the service and store the response.
402 String method = REQUEST_TYPE.httpMethodName();
403 String url = getServiceRootURL();
404 String mediaType = MediaType.APPLICATION_XML;
405 final String entity = "";
406 int statusCode = submitRequest(method, url, mediaType, entity);
408 // Check the status code of the response: does it match
409 // the expected response(s)?
410 if(logger.isDebugEnabled()){
411 logger.debug(testName + ": url=" + url +
412 " status=" + statusCode);
414 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
415 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
416 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
420 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
421 dependsOnMethods = {"create", "update", "testSubmitRequest"})
422 public void updateWithMalformedXml(String testName) throws Exception {
425 setupUpdateWithMalformedXml(testName);
427 // Submit the request to the service and store the response.
428 String method = REQUEST_TYPE.httpMethodName();
429 String url = getResourceURL(knownResourceId);
430 final String entity = MALFORMED_XML_DATA;
431 int statusCode = submitRequest(method, url, entity);
433 // Check the status code of the response: does it match
434 // the expected response(s)?
435 if(logger.isDebugEnabled()){
436 logger.debug(testName + ": url=" + url +
437 " status=" + statusCode);
439 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
440 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
441 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
445 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
446 public void updateWithWrongXmlSchema(String testName) {
449 setupUpdateWithWrongXmlSchema(testName);
451 // Submit the request to the service and store the response.
452 String method = REQUEST_TYPE.httpMethodName();
453 String url = getResourceURL(knownResourceId);
454 final String entity = WRONG_XML_SCHEMA_DATA;
455 int statusCode = submitRequest(method, url, entity);
457 // Check the status code of the response: does it match
458 // the expected response(s)?
459 if(logger.isDebugEnabled()){
460 logger.debug(testName + ": url=" + url +
461 " status=" + statusCode);
463 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
464 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
465 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
470 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
471 dependsOnMethods = {"update", "testSubmitRequest"})
472 public void updateNonExistent(String testName) throws Exception {
475 setupUpdateNonExistent(testName);
477 // Submit the request to the service and store the response.
478 // Note: The ID used in this 'create' call may be arbitrary.
479 // The only relevant ID may be the one used in update(), below.
480 MultipartOutput multipart = createAcquisitionInstance(NON_EXISTENT_ID);
481 ClientResponse<MultipartInput> res =
482 client.update(NON_EXISTENT_ID, multipart);
483 int statusCode = res.getStatus();
485 // Check the status code of the response: does it match
486 // the expected response(s)?
487 if(logger.isDebugEnabled()){
488 logger.debug(testName + ": status = " + statusCode);
490 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
491 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
492 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
495 // ---------------------------------------------------------------
496 // CRUD tests : DELETE tests
497 // ---------------------------------------------------------------
500 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
501 dependsOnMethods = {"create", "read", "update"})
502 public void delete(String testName) throws Exception {
505 setupDelete(testName);
507 // Submit the request to the service and store the response.
508 ClientResponse<Response> res = client.delete(knownResourceId);
509 int statusCode = res.getStatus();
511 // Check the status code of the response: does it match
512 // the expected response(s)?
513 if(logger.isDebugEnabled()){
514 logger.debug(testName + ": status = " + statusCode);
516 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
517 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
518 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
523 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
524 dependsOnMethods = {"delete"})
525 public void deleteNonExistent(String testName) throws Exception {
528 setupDeleteNonExistent(testName);
530 // Submit the request to the service and store the response.
531 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
532 int statusCode = res.getStatus();
534 // Check the status code of the response: does it match
535 // the expected response(s)?
536 if(logger.isDebugEnabled()){
537 logger.debug(testName + ": status = " + statusCode);
539 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
540 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
541 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
544 // ---------------------------------------------------------------
545 // Utility tests : tests of code used in tests above
546 // ---------------------------------------------------------------
548 * Tests the code for manually submitting data that is used by several
549 * of the methods above.
551 @Test(dependsOnMethods = {"create", "read"})
552 public void testSubmitRequest() throws Exception {
554 // Expected status code: 200 OK
555 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
557 // Submit the request to the service and store the response.
558 String method = ServiceRequestType.READ.httpMethodName();
559 String url = getResourceURL(knownResourceId);
560 int statusCode = submitRequest(method, url);
562 // Check the status code of the response: does it match
563 // the expected response(s)?
564 if(logger.isDebugEnabled()){
565 logger.debug("testSubmitRequest: url=" + url +
566 " status=" + statusCode);
568 Assert.assertEquals(statusCode, EXPECTED_STATUS);
572 // ---------------------------------------------------------------
573 // Cleanup of resources created during testing
574 // ---------------------------------------------------------------
576 // ---------------------------------------------------------------
577 // Cleanup of resources created during testing
578 // ---------------------------------------------------------------
581 * Deletes all resources created by tests, after all tests have been run.
583 * This cleanup method will always be run, even if one or more tests fail.
584 * For this reason, it attempts to remove all resources created
585 * at any point during testing, even if some of those resources
586 * may be expected to be deleted by certain tests.
588 @AfterClass(alwaysRun=true)
589 public void cleanUp() {
590 if (logger.isDebugEnabled()) {
591 logger.debug("Cleaning up temporary resources created for testing ...");
593 for (String resourceId : allResourceIdsCreated) {
594 // Note: Any non-success responses are ignored and not reported.
595 ClientResponse<Response> res = client.delete(resourceId);
599 // ---------------------------------------------------------------
600 // Utility methods used by tests above
601 // ---------------------------------------------------------------
603 public String getServicePathComponent() {
604 return client.getServicePathComponent();
608 private MultipartOutput createAcquisitionInstance(String identifier) {
609 AcquisitionsCommon acquisition = new AcquisitionsCommon();
610 acquisition.setAccessionDate("accessionDate-" + identifier);
611 MultipartOutput multipart = new MultipartOutput();
612 OutputPart commonPart = multipart.addPart(acquisition,
613 MediaType.APPLICATION_XML_TYPE);
614 commonPart.getHeaders().add("label", client.getCommonPartName());
616 if(logger.isDebugEnabled()){
617 logger.debug("to be created, acquisition common");
618 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));