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.List;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
29 import org.collectionspace.services.client.AcquisitionClient;
31 import org.collectionspace.services.acquisition.AcquisitionsCommon;
32 import org.collectionspace.services.acquisition.AcquisitionsCommonList;
33 import org.jboss.resteasy.client.ClientResponse;
35 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
36 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
37 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * AcquisitionServiceTest, carries out tests against a
46 * deployed and running Acquisition Service.
48 * $LastChangedRevision: 621 $
49 * $LastChangedDate: 2009-09-02 16:49:01 -0700 (Wed, 02 Sep 2009) $
51 public class AcquisitionServiceTest extends AbstractServiceTest {
53 private final Logger logger =
54 LoggerFactory.getLogger(AcquisitionServiceTest.class);
56 // Instance variables specific to this test.
57 private AcquisitionClient client = new AcquisitionClient();
58 private String knownResourceId = null;
60 // ---------------------------------------------------------------
61 // CRUD tests : CREATE tests
62 // ---------------------------------------------------------------
65 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
66 public void create(String testName) throws Exception {
68 // Perform setup, such as initializing the type of service request
69 // (e.g. CREATE, DELETE), its valid and expected status codes, and
70 // its associated HTTP method name (e.g. POST, DELETE).
71 setupCreate(testName);
73 // Submit the request to the service and store the response.
74 String identifier = createIdentifier();
76 MultipartOutput multipart = createAcquisitionInstance(identifier);
77 ClientResponse<Response> res = client.create(multipart);
79 int statusCode = res.getStatus();
81 // Check the status code of the response: does it match
82 // the expected response(s)?
85 // Does it fall within the set of valid status codes?
86 // Does it exactly match the expected status code?
87 if(logger.isDebugEnabled()){
88 logger.debug(testName + ": status = " + statusCode);
90 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
91 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
92 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
94 // Store the ID returned from this create operation for
95 // additional tests below.
96 knownResourceId = extractId(res);
97 if(logger.isDebugEnabled()){
98 logger.debug("create: knownResourceId=" + knownResourceId);
103 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
104 dependsOnMethods = {"create"})
105 public void createList(String testName) throws Exception {
106 for(int i = 0; i < 3; i++){
112 // Placeholders until the three tests below can be uncommented.
113 // See Issue CSPACE-401.
115 public void createWithEmptyEntityBody(String testName) throws Exception {
119 public void createWithMalformedXml(String testName) throws Exception {
123 public void createWithWrongXmlSchema(String testName) throws Exception {
128 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
129 dependsOnMethods = {"create", "testSubmitRequest"})
130 public void createWithMalformedXml(String testName) throws Exception {
133 setupCreateWithMalformedXml();
135 // Submit the request to the service and store the response.
136 String method = REQUEST_TYPE.httpMethodName();
137 String url = getServiceRootURL();
138 final String entity = MALFORMED_XML_DATA; // Constant from base class.
139 int statusCode = submitRequest(method, url, entity);
141 // Check the status code of the response: does it match
142 // the expected response(s)?
143 if(logger.isDebugEnabled()){
144 logger.debug(testName + ": url=" + url +
145 " status=" + statusCode);
147 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
148 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
149 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
153 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
154 dependsOnMethods = {"create", "testSubmitRequest"})
155 public void createWithWrongXmlSchema() throws Exception {
158 setupCreateWithWrongXmlSchema();
160 // Submit the request to the service and store the response.
161 String method = REQUEST_TYPE.httpMethodName();
162 String url = getServiceRootURL();
163 final String entity = WRONG_XML_SCHEMA_DATA;
164 int statusCode = submitRequest(method, url, entity);
166 // Check the status code of the response: does it match
167 // the expected response(s)?
168 if(logger.isDebugEnabled()){
169 logger.debug(testName + ": url=" + url +
170 " status=" + statusCode);
172 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
173 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
174 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
178 // ---------------------------------------------------------------
179 // CRUD tests : READ tests
180 // ---------------------------------------------------------------
183 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
184 dependsOnMethods = {"create"})
185 public void read(String testName) throws Exception {
190 // Submit the request to the service and store the response.
191 ClientResponse<MultipartInput> res = client.read(knownResourceId);
192 int statusCode = res.getStatus();
194 // Check the status code of the response: does it match
195 // the expected response(s)?
196 if(logger.isDebugEnabled()){
197 logger.debug(testName + ": status = " + statusCode);
199 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
200 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
201 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
203 MultipartInput input = (MultipartInput) res.getEntity();
204 AcquisitionsCommon acquisitionObject = (AcquisitionsCommon) extractPart(input,
205 client.getCommonPartName(), AcquisitionsCommon.class);
206 Assert.assertNotNull(acquisitionObject);
212 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
213 dependsOnMethods = {"read"})
214 public void readNonExistent(String testName) throws Exception {
217 setupReadNonExistent(testName);
219 // Submit the request to the service and store the response.
220 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
221 int statusCode = res.getStatus();
223 // Check the status code of the response: does it match
224 // the expected response(s)?
225 if(logger.isDebugEnabled()){
226 logger.debug(testName + ": status = " + statusCode);
228 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
229 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
230 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
233 // ---------------------------------------------------------------
234 // CRUD tests : READ_LIST tests
235 // ---------------------------------------------------------------
238 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
239 dependsOnMethods = {"createList", "read"})
240 public void readList(String testName) throws Exception {
243 setupReadList(testName);
245 // Submit the request to the service and store the response.
246 ClientResponse<AcquisitionsCommonList> res = client.readList();
247 AcquisitionsCommonList list = res.getEntity();
248 int statusCode = res.getStatus();
250 // Check the status code of the response: does it match
251 // the expected response(s)?
252 if(logger.isDebugEnabled()){
253 logger.debug(testName + ": status = " + statusCode);
255 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
256 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
257 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
259 // Optionally output additional data about list members for debugging.
260 boolean iterateThroughList = false;
261 if(iterateThroughList && logger.isDebugEnabled()){
262 List<AcquisitionsCommonList.AcquisitionListItem> items =
263 list.getAcquisitionListItem();
265 for(AcquisitionsCommonList.AcquisitionListItem item : items){
266 logger.debug(testName + ": list-item[" + i + "] csid=" +
268 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
269 item.getAccessionDate());
270 logger.debug(testName + ": list-item[" + i + "] URI=" +
281 // ---------------------------------------------------------------
282 // CRUD tests : UPDATE tests
283 // ---------------------------------------------------------------
287 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
288 dependsOnMethods = {"read"})
289 public void update(String testName) throws Exception {
292 setupUpdate(testName);
294 ClientResponse<MultipartInput> res =
295 client.read(knownResourceId);
296 if(logger.isDebugEnabled()){
297 logger.debug(testName + ": read status = " + res.getStatus());
299 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
301 if(logger.isDebugEnabled()){
302 logger.debug("got object to update with ID: " + knownResourceId);
304 MultipartInput input = (MultipartInput) res.getEntity();
305 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
306 client.getCommonPartName(), AcquisitionsCommon.class);
307 Assert.assertNotNull(acquisition);
309 // Update the content of this resource.
310 acquisition.setAccessionDate("updated-" + acquisition.getAccessionDate());
311 if(logger.isDebugEnabled()){
312 verbose("updated object", acquisition, AcquisitionsCommon.class);
314 // Submit the request to the service and store the response.
315 MultipartOutput output = new MultipartOutput();
316 OutputPart commonPart = output.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
317 commonPart.getHeaders().add("label", client.getCommonPartName());
319 res = client.update(knownResourceId, output);
320 int statusCode = res.getStatus();
321 // Check the status code of the response: does it match the expected response(s)?
322 if(logger.isDebugEnabled()){
323 logger.debug(testName + ": status = " + statusCode);
325 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
326 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
327 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
330 input = (MultipartInput) res.getEntity();
331 AcquisitionsCommon updatedAcquisition =
332 (AcquisitionsCommon) extractPart(input,
333 client.getCommonPartName(), AcquisitionsCommon.class);
334 Assert.assertNotNull(updatedAcquisition);
336 Assert.assertEquals(updatedAcquisition.getAccessionDate(),
337 acquisition.getAccessionDate(),
338 "Data in updated object did not match submitted data.");
343 // Placeholders until the three tests below can be uncommented.
344 // See Issue CSPACE-401.
346 public void updateWithEmptyEntityBody(String testName) throws Exception {
350 public void updateWithMalformedXml(String testName) throws Exception {
354 public void updateWithWrongXmlSchema(String testName) throws Exception {
359 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
360 dependsOnMethods = {"create", "update", "testSubmitRequest"})
361 public void updateWithEmptyEntityBody(String testName) throws Exception {
364 setupUpdateWithEmptyEntityBody(testName);
366 // Submit the request to the service and store the response.
367 String method = REQUEST_TYPE.httpMethodName();
368 String url = getResourceURL(knownResourceId);
369 String mediaType = MediaType.APPLICATION_XML;
370 final String entity = "";
371 int statusCode = submitRequest(method, url, mediaType, entity);
373 // Check the status code of the response: does it match
374 // the expected response(s)?
375 if(logger.isDebugEnabled()){
376 (testName + ": url=" + url + " status=" + statusCode);
378 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
379 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
380 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
384 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
385 dependsOnMethods = {"create", "testSubmitRequest"})
386 public void createWithEmptyEntityBody() throws Exception {
389 setupCreateWithEmptyEntityBody(testName);
391 // Submit the request to the service and store the response.
392 String method = REQUEST_TYPE.httpMethodName();
393 String url = getServiceRootURL();
394 String mediaType = MediaType.APPLICATION_XML;
395 final String entity = "";
396 int statusCode = submitRequest(method, url, mediaType, entity);
398 // Check the status code of the response: does it match
399 // the expected response(s)?
400 if(logger.isDebugEnabled()){
401 logger.debug(testName + ": url=" + url +
402 " status=" + statusCode);
404 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
405 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
406 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
410 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
411 dependsOnMethods = {"create", "update", "testSubmitRequest"})
412 public void updateWithMalformedXml(String testName) throws Exception {
415 setupUpdateWithMalformedXml(testName);
417 // Submit the request to the service and store the response.
418 String method = REQUEST_TYPE.httpMethodName();
419 String url = getResourceURL(knownResourceId);
420 final String entity = MALFORMED_XML_DATA;
421 int statusCode = submitRequest(method, url, entity);
423 // Check the status code of the response: does it match
424 // the expected response(s)?
425 if(logger.isDebugEnabled()){
426 logger.debug(testName + ": url=" + url +
427 " status=" + statusCode);
429 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
430 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
431 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
435 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
436 public void updateWithWrongXmlSchema(String testName) {
439 setupUpdateWithWrongXmlSchema(testName);
441 // Submit the request to the service and store the response.
442 String method = REQUEST_TYPE.httpMethodName();
443 String url = getResourceURL(knownResourceId);
444 final String entity = WRONG_XML_SCHEMA_DATA;
445 int statusCode = submitRequest(method, url, entity);
447 // Check the status code of the response: does it match
448 // the expected response(s)?
449 if(logger.isDebugEnabled()){
450 logger.debug(testName + ": url=" + url +
451 " status=" + statusCode);
453 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
454 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
455 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
460 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
461 dependsOnMethods = {"update", "testSubmitRequest"})
462 public void updateNonExistent(String testName) throws Exception {
465 setupUpdateNonExistent(testName);
467 // Submit the request to the service and store the response.
468 // Note: The ID used in this 'create' call may be arbitrary.
469 // The only relevant ID may be the one used in update(), below.
470 MultipartOutput multipart = createAcquisitionInstance(NON_EXISTENT_ID);
471 ClientResponse<MultipartInput> res =
472 client.update(NON_EXISTENT_ID, multipart);
473 int statusCode = res.getStatus();
475 // Check the status code of the response: does it match
476 // the expected response(s)?
477 if(logger.isDebugEnabled()){
478 logger.debug(testName + ": status = " + statusCode);
480 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
481 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
482 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
485 // ---------------------------------------------------------------
486 // CRUD tests : DELETE tests
487 // ---------------------------------------------------------------
490 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
491 dependsOnMethods = {"create", "read", "update"})
492 public void delete(String testName) throws Exception {
495 setupDelete(testName);
497 // Submit the request to the service and store the response.
498 ClientResponse<Response> res = client.delete(knownResourceId);
499 int statusCode = res.getStatus();
501 // Check the status code of the response: does it match
502 // the expected response(s)?
503 if(logger.isDebugEnabled()){
504 logger.debug(testName + ": status = " + statusCode);
506 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
507 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
508 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
513 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
514 dependsOnMethods = {"delete"})
515 public void deleteNonExistent(String testName) throws Exception {
518 setupDeleteNonExistent(testName);
520 // Submit the request to the service and store the response.
521 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
522 int statusCode = res.getStatus();
524 // Check the status code of the response: does it match
525 // the expected response(s)?
526 if(logger.isDebugEnabled()){
527 logger.debug(testName + ": status = " + statusCode);
529 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
530 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
531 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
534 // ---------------------------------------------------------------
535 // Utility tests : tests of code used in tests above
536 // ---------------------------------------------------------------
538 * Tests the code for manually submitting data that is used by several
539 * of the methods above.
541 @Test(dependsOnMethods = {"create", "read"})
542 public void testSubmitRequest() throws Exception {
544 // Expected status code: 200 OK
545 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
547 // Submit the request to the service and store the response.
548 String method = ServiceRequestType.READ.httpMethodName();
549 String url = getResourceURL(knownResourceId);
550 int statusCode = submitRequest(method, url);
552 // Check the status code of the response: does it match
553 // the expected response(s)?
554 if(logger.isDebugEnabled()){
555 logger.debug("testSubmitRequest: url=" + url +
556 " status=" + statusCode);
558 Assert.assertEquals(statusCode, EXPECTED_STATUS);
562 // ---------------------------------------------------------------
563 // Utility methods used by tests above
564 // ---------------------------------------------------------------
566 public String getServicePathComponent() {
567 return client.getServicePathComponent();
571 private MultipartOutput createAcquisitionInstance(String identifier) {
572 AcquisitionsCommon acquisition = new AcquisitionsCommon();
573 acquisition.setAccessionDate("accessionDate-" + identifier);
574 MultipartOutput multipart = new MultipartOutput();
575 OutputPart commonPart = multipart.addPart(acquisition,
576 MediaType.APPLICATION_XML_TYPE);
577 commonPart.getHeaders().add("label", client.getCommonPartName());
579 if(logger.isDebugEnabled()){
580 verbose("to be created, acquisition common ",
581 acquisition, AcquisitionsCommon.class);