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 © 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;
42 * AcquisitionServiceTest, carries out tests against a
43 * deployed and running Acquisition Service.
45 * $LastChangedRevision: 621 $
46 * $LastChangedDate: 2009-09-02 16:49:01 -0700 (Wed, 02 Sep 2009) $
48 public class AcquisitionServiceTest extends AbstractServiceTest {
50 // Instance variables specific to this test.
51 private AcquisitionClient client = new AcquisitionClient();
52 final String SERVICE_PATH_COMPONENT = "acquisitions";
53 private String knownResourceId = null;
55 //FIXME: Remove this method once ALL the services use "_common" instead of "-common"
56 public String getCommonPartName() {
57 return getServicePathComponent() + "_common";
60 // ---------------------------------------------------------------
61 // CRUD tests : CREATE tests
62 // ---------------------------------------------------------------
66 public void create() {
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 verbose("create: status = " + statusCode);
88 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
89 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
90 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
92 // Store the ID returned from this create operation for
93 // additional tests below.
94 knownResourceId = extractId(res);
95 verbose("create: knownResourceId=" + knownResourceId);
99 @Test(dependsOnMethods = {"create"})
100 public void createList() {
101 for(int i = 0; i < 3; i++){
107 // Placeholders until the three tests below can be uncommented.
108 // See Issue CSPACE-401.
109 public void createWithEmptyEntityBody() {
112 public void createWithMalformedXml() {
115 public void createWithWrongXmlSchema() {
120 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
121 public void createWithMalformedXml() {
124 setupCreateWithMalformedXml();
126 // Submit the request to the service and store the response.
127 String method = REQUEST_TYPE.httpMethodName();
128 String url = getServiceRootURL();
129 final String entity = MALFORMED_XML_DATA; // Constant from base class.
130 int statusCode = submitRequest(method, url, entity);
132 // Check the status code of the response: does it match
133 // the expected response(s)?
134 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
135 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
136 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
137 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
141 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
142 public void createWithWrongXmlSchema() {
145 setupCreateWithWrongXmlSchema();
147 // Submit the request to the service and store the response.
148 String method = REQUEST_TYPE.httpMethodName();
149 String url = getServiceRootURL();
150 final String entity = WRONG_XML_SCHEMA_DATA;
151 int statusCode = submitRequest(method, url, entity);
153 // Check the status code of the response: does it match
154 // the expected response(s)?
155 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
156 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
157 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
158 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
161 // ---------------------------------------------------------------
162 // CRUD tests : READ tests
163 // ---------------------------------------------------------------
166 @Test(dependsOnMethods = {"create"})
172 // Submit the request to the service and store the response.
173 ClientResponse<MultipartInput> res = client.read(knownResourceId);
174 int statusCode = res.getStatus();
176 // Check the status code of the response: does it match
177 // the expected response(s)?
178 verbose("read: status = " + statusCode);
179 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
180 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
181 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
182 //FIXME: remove the following try catch once Aron fixes signatures
184 MultipartInput input = (MultipartInput) res.getEntity();
185 AcquisitionsCommon acquistionObject = (AcquisitionsCommon) extractPart(input,
186 getCommonPartName(), AcquisitionsCommon.class);
187 Assert.assertNotNull(acquistionObject);
189 throw new RuntimeException(e);
195 @Test(dependsOnMethods = {"read"})
196 public void readNonExistent() {
199 setupReadNonExistent();
201 // Submit the request to the service and store the response.
202 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
203 int statusCode = res.getStatus();
205 // Check the status code of the response: does it match
206 // the expected response(s)?
207 verbose("readNonExistent: status = " + res.getStatus());
208 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
209 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
210 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
213 // ---------------------------------------------------------------
214 // CRUD tests : READ_LIST tests
215 // ---------------------------------------------------------------
218 @Test(dependsOnMethods = {"createList", "read"})
219 public void readList() {
224 // Submit the request to the service and store the response.
225 ClientResponse<AcquisitionsCommonList> res = client.readList();
226 AcquisitionsCommonList list = res.getEntity();
227 int statusCode = res.getStatus();
229 // Check the status code of the response: does it match
230 // the expected response(s)?
231 verbose("readList: status = " + res.getStatus());
232 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
233 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
234 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
236 // Optionally output additional data about list members for debugging.
237 boolean iterateThroughList = false;
238 if(iterateThroughList && logger.isDebugEnabled()){
239 List<AcquisitionsCommonList.AcquisitionListItem> items =
240 list.getAcquisitionListItem();
242 for(AcquisitionsCommonList.AcquisitionListItem item : items){
243 verbose("readList: list-item[" + i + "] csid=" +
245 verbose("readList: list-item[" + i + "] objectNumber=" +
246 item.getAccessiondate());
247 verbose("readList: list-item[" + i + "] URI=" +
257 // ---------------------------------------------------------------
258 // CRUD tests : UPDATE tests
259 // ---------------------------------------------------------------
262 @Test(dependsOnMethods = {"read"})
263 public void update() {
267 try{ //ideally, just remove try-catch and let the exception bubble up
268 // Retrieve an existing resource that we can update.
269 ClientResponse<MultipartInput> res =
270 client.read(knownResourceId);
271 verbose("update: read status = " + res.getStatus());
272 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
274 verbose("got object to update with ID: " + knownResourceId);
275 MultipartInput input = (MultipartInput) res.getEntity();
276 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
277 getCommonPartName(), AcquisitionsCommon.class);
278 Assert.assertNotNull(acquisition);
280 // Update the content of this resource.
281 acquisition.setAccessionDate("updated-" + acquisition.getAccessionDate());
282 verbose("updated object", acquisition, AcquisitionsCommon.class);
283 // Submit the request to the service and store the response.
284 MultipartOutput output = new MultipartOutput();
285 OutputPart commonPart = output.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
286 commonPart.getHeaders().add("label", getCommonPartName());
288 res = client.update(knownResourceId, output);
289 int statusCode = res.getStatus();
290 // Check the status code of the response: does it match the expected response(s)?
291 verbose("update: status = " + res.getStatus());
292 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
293 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
294 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
297 input = (MultipartInput) res.getEntity();
298 AcquisitionsCommon updatedAcquisition =
299 (AcquisitionsCommon) extractPart(input,
300 getCommonPartName(), AcquisitionsCommon.class);
301 Assert.assertNotNull(updatedAcquisition);
303 Assert.assertEquals(updatedAcquisition.getAccessionDate(),
304 acquisition.getAccessionDate(),
305 "Data in updated object did not match submitted data.");
312 // Placeholders until the three tests below can be uncommented.
313 // See Issue CSPACE-401.
314 public void updateWithEmptyEntityBody() {
317 public void updateWithMalformedXml() {
320 public void updateWithWrongXmlSchema() {
325 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
326 public void updateWithEmptyEntityBody() {
329 setupUpdateWithEmptyEntityBody();
331 // Submit the request to the service and store the response.
332 String method = REQUEST_TYPE.httpMethodName();
333 String url = getResourceURL(knownResourceId);
334 String mediaType = MediaType.APPLICATION_XML;
335 final String entity = "";
336 int statusCode = submitRequest(method, url, mediaType, entity);
338 // Check the status code of the response: does it match
339 // the expected response(s)?
340 verbose("updateWithEmptyEntityBody url=" + url + " status=" + statusCode);
341 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
342 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
343 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
347 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
348 public void createWithEmptyEntityBody() {
351 setupCreateWithEmptyEntityBody();
353 // Submit the request to the service and store the response.
354 String method = REQUEST_TYPE.httpMethodName();
355 String url = getServiceRootURL();
356 String mediaType = MediaType.APPLICATION_XML;
357 final String entity = "";
358 int statusCode = submitRequest(method, url, mediaType, entity);
360 // Check the status code of the response: does it match
361 // the expected response(s)?
362 verbose("createWithEmptyEntityBody url=" + url + " status=" + statusCode);
363 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
364 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
365 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
369 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
370 public void updateWithMalformedXml() {
373 setupUpdateWithMalformedXml();
375 // Submit the request to the service and store the response.
376 String method = REQUEST_TYPE.httpMethodName();
377 String url = getResourceURL(knownResourceId);
378 final String entity = MALFORMED_XML_DATA;
379 int statusCode = submitRequest(method, url, entity);
381 // Check the status code of the response: does it match
382 // the expected response(s)?
383 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
384 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
385 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
386 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
390 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
391 public void updateWithWrongXmlSchema() {
394 setupUpdateWithWrongXmlSchema();
396 // Submit the request to the service and store the response.
397 String method = REQUEST_TYPE.httpMethodName();
398 String url = getResourceURL(knownResourceId);
399 final String entity = WRONG_XML_SCHEMA_DATA;
400 int statusCode = submitRequest(method, url, entity);
402 // Check the status code of the response: does it match
403 // the expected response(s)?
404 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
405 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
406 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
407 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
411 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
412 public void updateNonExistent() {
415 setupUpdateNonExistent();
417 // Submit the request to the service and store the response.
418 // Note: The ID used in this 'create' call may be arbitrary.
419 // The only relevant ID may be the one used in update(), below.
420 MultipartOutput multipart = createAcquisitionInstance(NON_EXISTENT_ID);
421 ClientResponse<MultipartInput> res =
422 client.update(NON_EXISTENT_ID, multipart);
423 int statusCode = res.getStatus();
425 // Check the status code of the response: does it match
426 // the expected response(s)?
427 verbose("updateNonExistent: status = " + res.getStatus());
428 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
429 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
430 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
433 // ---------------------------------------------------------------
434 // CRUD tests : DELETE tests
435 // ---------------------------------------------------------------
438 @Test(dependsOnMethods = {"create", "read", "update"})
439 public void delete() {
444 // Submit the request to the service and store the response.
445 ClientResponse<Response> res = client.delete(knownResourceId);
446 int statusCode = res.getStatus();
448 // Check the status code of the response: does it match
449 // the expected response(s)?
450 verbose("delete: status = " + res.getStatus());
451 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
452 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
453 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
458 @Test(dependsOnMethods = {"delete"})
459 public void deleteNonExistent() {
462 setupDeleteNonExistent();
464 // Submit the request to the service and store the response.
465 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
466 int statusCode = res.getStatus();
468 // Check the status code of the response: does it match
469 // the expected response(s)?
470 verbose("deleteNonExistent: status = " + res.getStatus());
471 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
472 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
473 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
476 // ---------------------------------------------------------------
477 // Utility tests : tests of code used in tests above
478 // ---------------------------------------------------------------
480 * Tests the code for manually submitting data that is used by several
481 * of the methods above.
483 @Test(dependsOnMethods = {"create", "read"})
484 public void testSubmitRequest() {
486 // Expected status code: 200 OK
487 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
489 // Submit the request to the service and store the response.
490 String method = ServiceRequestType.READ.httpMethodName();
491 String url = getResourceURL(knownResourceId);
492 int statusCode = submitRequest(method, url);
494 // Check the status code of the response: does it match
495 // the expected response(s)?
496 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
497 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
501 // ---------------------------------------------------------------
502 // Utility methods used by tests above
503 // ---------------------------------------------------------------
505 public String getServicePathComponent() {
506 return SERVICE_PATH_COMPONENT;
510 private MultipartOutput createAcquisitionInstance(String identifier) {
511 AcquisitionsCommon acquisition = new AcquisitionsCommon();
512 acquisition.setAccessionDate("accessionDate-" + identifier);
513 MultipartOutput multipart = new MultipartOutput();
514 OutputPart commonPart = multipart.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
515 commonPart.getHeaders().add("label", getCommonPartName());
517 verbose("to be created, acquisition common ", acquisition, AcquisitionsCommon.class);