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.
24 package org.collectionspace.services.client.test;
26 import java.util.List;
27 import javax.ws.rs.core.Response;
28 import javax.ws.rs.core.Response.Status;
30 import org.collectionspace.services.client.AcquisitionClient;
31 import org.collectionspace.services.client.test.ServiceRequestType;
32 import org.collectionspace.services.acquisition.Acquisition;
33 import org.collectionspace.services.acquisition.AcquisitionList;
35 import org.jboss.resteasy.client.ClientResponse;
37 import org.testng.Assert;
38 import org.testng.annotations.Test;
41 * AcquisitionServiceTest, carries out tests against a
42 * deployed and running Acquisition Service.
44 * $LastChangedRevision: 621 $
45 * $LastChangedDate: 2009-09-02 16:49:01 -0700 (Wed, 02 Sep 2009) $
47 public class AcquisitionServiceTest extends AbstractServiceTest {
49 // Instance variables specific to this test.
50 private AcquisitionClient client = new AcquisitionClient();
51 final String SERVICE_PATH_COMPONENT = "acquisitions";
52 private String knownResourceId = null;
55 // ---------------------------------------------------------------
56 // CRUD tests : CREATE tests
57 // ---------------------------------------------------------------
63 public void create() {
65 // Perform setup, such as initializing the type of service request
66 // (e.g. CREATE, DELETE), its valid and expected status codes, and
67 // its associated HTTP method name (e.g. POST, DELETE).
70 // Submit the request to the service and store the response.
71 String identifier = createIdentifier();
72 Acquisition acquisition = createAcquisitionInstance(identifier);
73 ClientResponse<Response> res = client.create(acquisition);
74 int statusCode = res.getStatus();
76 // Check the status code of the response: does it match
77 // the expected response(s)?
80 // Does it fall within the set of valid status codes?
81 // Does it exactly match the expected status code?
82 verbose("create: status = " + statusCode);
83 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
84 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
85 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
87 // Store the ID returned from this create operation for
88 // additional tests below.
89 knownResourceId = extractId(res);
93 @Test(dependsOnMethods = {"create"})
94 public void createList() {
95 for(int i = 0; i < 3; i++){
102 // Placeholders until the three tests below can be uncommented.
103 // See Issue CSPACE-401.
104 public void createWithEmptyEntityBody() {}
105 public void createWithMalformedXml() {}
106 public void createWithWrongXmlSchema() {}
110 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
111 public void createWithMalformedXml() {
114 setupCreateWithMalformedXml();
116 // Submit the request to the service and store the response.
117 String method = REQUEST_TYPE.httpMethodName();
118 String url = getServiceRootURL();
119 final String entity = MALFORMED_XML_DATA; // Constant from base class.
120 int statusCode = submitRequest(method, url, entity);
122 // Check the status code of the response: does it match
123 // the expected response(s)?
124 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
125 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
126 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
127 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
131 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
132 public void createWithWrongXmlSchema() {
135 setupCreateWithWrongXmlSchema();
137 // Submit the request to the service and store the response.
138 String method = REQUEST_TYPE.httpMethodName();
139 String url = getServiceRootURL();
140 final String entity = WRONG_XML_SCHEMA_DATA;
141 int statusCode = submitRequest(method, url, entity);
143 // Check the status code of the response: does it match
144 // the expected response(s)?
145 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
146 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
147 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
148 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
152 // ---------------------------------------------------------------
153 // CRUD tests : READ tests
154 // ---------------------------------------------------------------
159 @Test(dependsOnMethods = {"create"})
165 // Submit the request to the service and store the response.
166 ClientResponse<Acquisition> res = client.read(knownResourceId);
167 int statusCode = res.getStatus();
169 // Check the status code of the response: does it match
170 // the expected response(s)?
171 verbose("read: status = " + statusCode);
172 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
173 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
174 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
180 @Test(dependsOnMethods = {"read"})
181 public void readNonExistent() {
184 setupReadNonExistent();
186 // Submit the request to the service and store the response.
187 ClientResponse<Acquisition> res = client.read(NON_EXISTENT_ID);
188 int statusCode = res.getStatus();
190 // Check the status code of the response: does it match
191 // the expected response(s)?
192 verbose("readNonExistent: status = " + res.getStatus());
193 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
194 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
195 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
199 // ---------------------------------------------------------------
200 // CRUD tests : READ_LIST tests
201 // ---------------------------------------------------------------
206 @Test(dependsOnMethods = {"createList"})
207 public void readList() {
212 // Submit the request to the service and store the response.
213 ClientResponse<AcquisitionList> res = client.readList();
214 AcquisitionList list = res.getEntity();
215 int statusCode = res.getStatus();
217 // Check the status code of the response: does it match
218 // the expected response(s)?
219 verbose("readList: status = " + res.getStatus());
220 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
221 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
222 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
224 // Optionally output additional data about list members for debugging.
225 boolean iterateThroughList = false;
226 if (iterateThroughList && logger.isDebugEnabled()) {
227 List<AcquisitionList.AcquisitionListItem> items =
228 list.getAcquisitionListItem();
230 for(AcquisitionList.AcquisitionListItem item : items){
231 verbose("readList: list-item[" + i + "] csid=" +
233 verbose("readList: list-item[" + i + "] objectNumber=" +
234 item.getAccessiondate());
235 verbose("readList: list-item[" + i + "] URI=" +
248 // ---------------------------------------------------------------
249 // CRUD tests : UPDATE tests
250 // ---------------------------------------------------------------
255 @Test(dependsOnMethods = {"create"})
256 public void update() {
261 // Retrieve an existing resource that we can update.
262 ClientResponse<Acquisition> res = client.read(knownResourceId);
263 verbose("read: status = " + res.getStatus());
264 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
265 Acquisition acquisition = res.getEntity();
266 verbose("Got object to update with ID: " + knownResourceId,
267 acquisition, Acquisition.class);
269 // Update the content of this resource.
270 acquisition.setAccessiondate("updated-" + acquisition.getAccessiondate());
271 // acquisition.setEntryDate("updated-" + acquisition.getEntryDate());
273 // Submit the request to the service and store the response.
274 res = client.update(knownResourceId, acquisition);
275 int statusCode = res.getStatus();
276 Acquisition updatedObject = res.getEntity();
278 // Check the status code of the response: does it match
279 // the expected response(s)?
280 verbose("update: status = " + res.getStatus());
281 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
282 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
283 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
285 // Check the contents of the response: does it match
286 // what was submitted?
287 verbose("update: ", updatedObject, Acquisition.class);
288 Assert.assertEquals(updatedObject.getAccessiondate(),
289 acquisition.getAccessiondate(),
290 "Data in updated object did not match submitted data.");
295 // Placeholders until the three tests below can be uncommented.
296 // See Issue CSPACE-401.
297 public void updateWithEmptyEntityBody() {}
298 public void updateWithMalformedXml() {}
299 public void updateWithWrongXmlSchema() {}
303 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
304 public void updateWithEmptyEntityBody() {
307 setupUpdateWithEmptyEntityBody();
309 // Submit the request to the service and store the response.
310 String method = REQUEST_TYPE.httpMethodName();
311 String url = getResourceURL(knownResourceId);
312 String mediaType = MediaType.APPLICATION_XML;
313 final String entity = "";
314 int statusCode = submitRequest(method, url, mediaType, entity);
316 // Check the status code of the response: does it match
317 // the expected response(s)?
318 verbose("updateWithEmptyEntityBody url=" + url + " status=" + statusCode);
319 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
320 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
321 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
325 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
326 public void createWithEmptyEntityBody() {
329 setupCreateWithEmptyEntityBody();
331 // Submit the request to the service and store the response.
332 String method = REQUEST_TYPE.httpMethodName();
333 String url = getServiceRootURL();
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("createWithEmptyEntityBody 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", "update", "testSubmitRequest"})
348 public void updateWithMalformedXml() {
351 setupUpdateWithMalformedXml();
353 // Submit the request to the service and store the response.
354 String method = REQUEST_TYPE.httpMethodName();
355 String url = getResourceURL(knownResourceId);
356 final String entity = MALFORMED_XML_DATA;
357 int statusCode = submitRequest(method, url, entity);
359 // Check the status code of the response: does it match
360 // the expected response(s)?
361 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
362 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
363 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
364 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
368 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
369 public void updateWithWrongXmlSchema() {
372 setupUpdateWithWrongXmlSchema();
374 // Submit the request to the service and store the response.
375 String method = REQUEST_TYPE.httpMethodName();
376 String url = getResourceURL(knownResourceId);
377 final String entity = WRONG_XML_SCHEMA_DATA;
378 int statusCode = submitRequest(method, url, entity);
380 // Check the status code of the response: does it match
381 // the expected response(s)?
382 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
383 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
384 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
385 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
390 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
391 public void updateNonExistent() {
394 setupUpdateNonExistent();
396 // Submit the request to the service and store the response.
397 // Note: The ID used in this 'create' call may be arbitrary.
398 // The only relevant ID may be the one used in update(), below.
399 Acquisition acquisition = createAcquisitionInstance(NON_EXISTENT_ID);
400 ClientResponse<Acquisition> res =
401 client.update(NON_EXISTENT_ID, acquisition);
402 int statusCode = res.getStatus();
404 // Check the status code of the response: does it match
405 // the expected response(s)?
406 verbose("updateNonExistent: status = " + res.getStatus());
407 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
408 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
409 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
412 // ---------------------------------------------------------------
413 // CRUD tests : DELETE tests
414 // ---------------------------------------------------------------
419 @Test(dependsOnMethods =
420 {"create", "read", "update"})
421 public void delete() {
426 // Submit the request to the service and store the response.
427 ClientResponse<Response> res = client.delete(knownResourceId);
428 int statusCode = res.getStatus();
430 // Check the status code of the response: does it match
431 // the expected response(s)?
432 verbose("delete: status = " + res.getStatus());
433 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
434 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
435 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
441 @Test(dependsOnMethods = {"delete"})
442 public void deleteNonExistent() {
445 setupDeleteNonExistent();
447 // Submit the request to the service and store the response.
448 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
449 int statusCode = res.getStatus();
451 // Check the status code of the response: does it match
452 // the expected response(s)?
453 verbose("deleteNonExistent: status = " + res.getStatus());
454 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
455 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
456 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
460 // ---------------------------------------------------------------
461 // Utility tests : tests of code used in tests above
462 // ---------------------------------------------------------------
465 * Tests the code for manually submitting data that is used by several
466 * of the methods above.
468 @Test(dependsOnMethods = {"create", "read"})
469 public void testSubmitRequest() {
471 // Expected status code: 200 OK
472 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
474 // Submit the request to the service and store the response.
475 String method = ServiceRequestType.READ.httpMethodName();
476 String url = getResourceURL(knownResourceId);
477 int statusCode = submitRequest(method, url);
479 // Check the status code of the response: does it match
480 // the expected response(s)?
481 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
482 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
486 // ---------------------------------------------------------------
487 // Utility methods used by tests above
488 // ---------------------------------------------------------------
491 public String getServicePathComponent() {
492 return SERVICE_PATH_COMPONENT;
495 private Acquisition createAcquisitionInstance(String identifier) {
496 Acquisition acquisition =
497 createAcquisitionInstance(
498 "entryNumber-" + identifier,
499 "entryDate-" + identifier);
503 private Acquisition createAcquisitionInstance(String entryNumber, String entryDate) {
504 Acquisition acquisition = new Acquisition();
505 // acquisition.setEntryNumber(entryNumber);
506 acquisition.setAccessiondate(entryDate);