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 // ---------------------------------------------------------------
66 public void create() 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).
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("create: 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(dependsOnMethods = {"create"})
104 public void createList() throws Exception {
105 for(int i = 0; i < 3; i++){
111 // Placeholders until the three tests below can be uncommented.
112 // See Issue CSPACE-401.
114 public void createWithEmptyEntityBody() throws Exception {
118 public void createWithMalformedXml() throws Exception {
122 public void createWithWrongXmlSchema() throws Exception {
127 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
128 public void createWithMalformedXml() throws Exception {
131 setupCreateWithMalformedXml();
133 // Submit the request to the service and store the response.
134 String method = REQUEST_TYPE.httpMethodName();
135 String url = getServiceRootURL();
136 final String entity = MALFORMED_XML_DATA; // Constant from base class.
137 int statusCode = submitRequest(method, url, entity);
139 // Check the status code of the response: does it match
140 // the expected response(s)?
141 if(logger.isDebugEnabled()){
142 logger.debug("createWithMalformedXml url=" + url +
143 " status=" + statusCode);
145 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
146 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
147 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
151 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
152 public void createWithWrongXmlSchema() throws Exception {
155 setupCreateWithWrongXmlSchema();
157 // Submit the request to the service and store the response.
158 String method = REQUEST_TYPE.httpMethodName();
159 String url = getServiceRootURL();
160 final String entity = WRONG_XML_SCHEMA_DATA;
161 int statusCode = submitRequest(method, url, entity);
163 // Check the status code of the response: does it match
164 // the expected response(s)?
165 if(logger.isDebugEnabled()){
166 logger.debug("createWithWrongXmlSchema url=" + url +
167 " status=" + statusCode);
169 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
170 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
171 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
175 // ---------------------------------------------------------------
176 // CRUD tests : READ tests
177 // ---------------------------------------------------------------
180 @Test(dependsOnMethods = {"create"})
181 public void read() throws Exception {
186 // Submit the request to the service and store the response.
187 ClientResponse<MultipartInput> res = client.read(knownResourceId);
188 int statusCode = res.getStatus();
190 // Check the status code of the response: does it match
191 // the expected response(s)?
192 if(logger.isDebugEnabled()){
193 logger.debug("read: status = " + statusCode);
195 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
196 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
197 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
199 MultipartInput input = (MultipartInput) res.getEntity();
200 AcquisitionsCommon acquistionObject = (AcquisitionsCommon) extractPart(input,
201 client.getCommonPartName(), AcquisitionsCommon.class);
202 Assert.assertNotNull(acquistionObject);
208 @Test(dependsOnMethods = {"read"})
209 public void readNonExistent() throws Exception {
212 setupReadNonExistent();
214 // Submit the request to the service and store the response.
215 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
216 int statusCode = res.getStatus();
218 // Check the status code of the response: does it match
219 // the expected response(s)?
220 if(logger.isDebugEnabled()){
221 logger.debug("readNonExistent: status = " + res.getStatus());
223 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
224 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
225 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
228 // ---------------------------------------------------------------
229 // CRUD tests : READ_LIST tests
230 // ---------------------------------------------------------------
233 @Test(dependsOnMethods = {"createList", "read"})
234 public void readList() throws Exception {
239 // Submit the request to the service and store the response.
240 ClientResponse<AcquisitionsCommonList> res = client.readList();
241 AcquisitionsCommonList list = res.getEntity();
242 int statusCode = res.getStatus();
244 // Check the status code of the response: does it match
245 // the expected response(s)?
246 if(logger.isDebugEnabled()){
247 logger.debug("readList: status = " + res.getStatus());
249 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
250 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
251 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
253 // Optionally output additional data about list members for debugging.
254 boolean iterateThroughList = false;
255 if(iterateThroughList && logger.isDebugEnabled()){
256 List<AcquisitionsCommonList.AcquisitionListItem> items =
257 list.getAcquisitionListItem();
259 for(AcquisitionsCommonList.AcquisitionListItem item : items){
260 logger.debug("readList: list-item[" + i + "] csid=" +
262 logger.debug("readList: list-item[" + i + "] objectNumber=" +
263 item.getAccessionDate());
264 logger.debug("readList: list-item[" + i + "] URI=" +
275 // ---------------------------------------------------------------
276 // CRUD tests : UPDATE tests
277 // ---------------------------------------------------------------
281 @Test(dependsOnMethods = {"read"})
282 public void update() throws Exception {
287 ClientResponse<MultipartInput> res =
288 client.read(knownResourceId);
289 if(logger.isDebugEnabled()){
290 logger.debug("update: read status = " + res.getStatus());
292 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
294 if(logger.isDebugEnabled()){
295 logger.debug("got object to update with ID: " + knownResourceId);
297 MultipartInput input = (MultipartInput) res.getEntity();
298 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
299 client.getCommonPartName(), AcquisitionsCommon.class);
300 Assert.assertNotNull(acquisition);
302 // Update the content of this resource.
303 acquisition.setAccessionDate("updated-" + acquisition.getAccessionDate());
304 if(logger.isDebugEnabled()){
305 verbose("updated object", acquisition, AcquisitionsCommon.class);
307 // Submit the request to the service and store the response.
308 MultipartOutput output = new MultipartOutput();
309 OutputPart commonPart = output.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
310 commonPart.getHeaders().add("label", client.getCommonPartName());
312 res = client.update(knownResourceId, output);
313 int statusCode = res.getStatus();
314 // Check the status code of the response: does it match the expected response(s)?
315 if(logger.isDebugEnabled()){
316 logger.debug("update: status = " + res.getStatus());
318 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
319 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
320 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
323 input = (MultipartInput) res.getEntity();
324 AcquisitionsCommon updatedAcquisition =
325 (AcquisitionsCommon) extractPart(input,
326 client.getCommonPartName(), AcquisitionsCommon.class);
327 Assert.assertNotNull(updatedAcquisition);
329 Assert.assertEquals(updatedAcquisition.getAccessionDate(),
330 acquisition.getAccessionDate(),
331 "Data in updated object did not match submitted data.");
336 // Placeholders until the three tests below can be uncommented.
337 // See Issue CSPACE-401.
338 public void updateWithEmptyEntityBody() throws Exception {
341 public void updateWithMalformedXml() throws Exception {
344 public void updateWithWrongXmlSchema() throws Exception {
349 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
350 public void updateWithEmptyEntityBody() throws Exception {
353 setupUpdateWithEmptyEntityBody();
355 // Submit the request to the service and store the response.
356 String method = REQUEST_TYPE.httpMethodName();
357 String url = getResourceURL(knownResourceId);
358 String mediaType = MediaType.APPLICATION_XML;
359 final String entity = "";
360 int statusCode = submitRequest(method, url, mediaType, entity);
362 // Check the status code of the response: does it match
363 // the expected response(s)?
364 if(logger.isDebugEnabled()){
365 ("updateWithEmptyEntityBody url=" + url + " status=" + statusCode);
367 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
368 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
369 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
373 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
374 public void createWithEmptyEntityBody() throws Exception {
377 setupCreateWithEmptyEntityBody();
379 // Submit the request to the service and store the response.
380 String method = REQUEST_TYPE.httpMethodName();
381 String url = getServiceRootURL();
382 String mediaType = MediaType.APPLICATION_XML;
383 final String entity = "";
384 int statusCode = submitRequest(method, url, mediaType, entity);
386 // Check the status code of the response: does it match
387 // the expected response(s)?
388 if(logger.isDebugEnabled()){
389 logger.debug("createWithEmptyEntityBody url=" + url +
390 " status=" + statusCode);
392 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
393 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
394 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
398 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
399 public void updateWithMalformedXml() throws Exception {
402 setupUpdateWithMalformedXml();
404 // Submit the request to the service and store the response.
405 String method = REQUEST_TYPE.httpMethodName();
406 String url = getResourceURL(knownResourceId);
407 final String entity = MALFORMED_XML_DATA;
408 int statusCode = submitRequest(method, url, entity);
410 // Check the status code of the response: does it match
411 // the expected response(s)?
412 if(logger.isDebugEnabled()){
413 logger.debug("updateWithMalformedXml: url=" + url +
414 " status=" + statusCode);
416 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
417 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
418 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
422 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
423 public void updateWithWrongXmlSchema() {
426 setupUpdateWithWrongXmlSchema();
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 = WRONG_XML_SCHEMA_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("updateWithWrongXmlSchema: url=" + url +
438 " status=" + statusCode);
440 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
441 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
442 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
447 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
448 public void updateNonExistent() throws Exception {
451 setupUpdateNonExistent();
453 // Submit the request to the service and store the response.
454 // Note: The ID used in this 'create' call may be arbitrary.
455 // The only relevant ID may be the one used in update(), below.
456 MultipartOutput multipart = createAcquisitionInstance(NON_EXISTENT_ID);
457 ClientResponse<MultipartInput> res =
458 client.update(NON_EXISTENT_ID, multipart);
459 int statusCode = res.getStatus();
461 // Check the status code of the response: does it match
462 // the expected response(s)?
463 if(logger.isDebugEnabled()){
464 logger.debug("updateNonExistent: status = " + res.getStatus());
466 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
467 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
468 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
471 // ---------------------------------------------------------------
472 // CRUD tests : DELETE tests
473 // ---------------------------------------------------------------
476 @Test(dependsOnMethods = {"create", "read", "update"})
477 public void delete() throws Exception {
482 // Submit the request to the service and store the response.
483 ClientResponse<Response> res = client.delete(knownResourceId);
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("delete: status = " + res.getStatus());
491 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
492 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
493 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
498 @Test(dependsOnMethods = {"delete"})
499 public void deleteNonExistent() throws Exception {
502 setupDeleteNonExistent();
504 // Submit the request to the service and store the response.
505 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
506 int statusCode = res.getStatus();
508 // Check the status code of the response: does it match
509 // the expected response(s)?
510 if(logger.isDebugEnabled()){
511 logger.debug("deleteNonExistent: status = " + res.getStatus());
513 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
514 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
515 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
518 // ---------------------------------------------------------------
519 // Utility tests : tests of code used in tests above
520 // ---------------------------------------------------------------
522 * Tests the code for manually submitting data that is used by several
523 * of the methods above.
525 @Test(dependsOnMethods = {"create", "read"})
526 public void testSubmitRequest() throws Exception {
528 // Expected status code: 200 OK
529 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
531 // Submit the request to the service and store the response.
532 String method = ServiceRequestType.READ.httpMethodName();
533 String url = getResourceURL(knownResourceId);
534 int statusCode = submitRequest(method, url);
536 // Check the status code of the response: does it match
537 // the expected response(s)?
538 if(logger.isDebugEnabled()){
539 logger.debug("testSubmitRequest: url=" + url +
540 " status=" + statusCode);
542 Assert.assertEquals(statusCode, EXPECTED_STATUS);
546 // ---------------------------------------------------------------
547 // Utility methods used by tests above
548 // ---------------------------------------------------------------
550 public String getServicePathComponent() {
551 return client.getServicePathComponent();
555 private MultipartOutput createAcquisitionInstance(String identifier) {
556 AcquisitionsCommon acquisition = new AcquisitionsCommon();
557 acquisition.setAccessionDate("accessionDate-" + identifier);
558 MultipartOutput multipart = new MultipartOutput();
559 OutputPart commonPart = multipart.addPart(acquisition,
560 MediaType.APPLICATION_XML_TYPE);
561 commonPart.getHeaders().add("label", client.getCommonPartName());
563 if(logger.isDebugEnabled()){
564 verbose("to be created, acquisition common ",
565 acquisition, AcquisitionsCommon.class);