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.IntakeClient;
31 import org.collectionspace.services.client.test.ServiceRequestType;
32 import org.collectionspace.services.intake.Intake;
33 import org.collectionspace.services.intake.IntakeList;
35 import org.jboss.resteasy.client.ClientResponse;
37 import org.testng.Assert;
38 import org.testng.annotations.Test;
41 * IntakeServiceTest, carries out tests against a
42 * deployed and running Intake Service.
44 * $LastChangedRevision$
47 public class IntakeServiceTest extends AbstractServiceTest {
49 // Instance variables specific to this test.
50 private IntakeClient client = new IntakeClient();
51 final String SERVICE_PATH_COMPONENT = "intakes";
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 Intake intake = createIntakeInstance(identifier);
73 ClientResponse<Response> res = client.create(intake);
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++){
103 @Test(dependsOnMethods = {"create"},
104 expectedExceptions = IllegalArgumentException.class)
105 public void createNull() {
106 ClientResponse<Response> res = client.create(null);
109 // Placeholders until the two tests below can be uncommented.
110 // See Issue CSPACE-401.
111 public void createWithMalformedXml() {}
112 public void createWithWrongXmlSchema() {}
116 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
117 public void createWithMalformedXml() {
120 setupCreateWithMalformedXml();
122 // Submit the request to the service and store the response.
123 String method = REQUEST_TYPE.httpMethodName();
124 String url = getServiceRootURL();
125 final String entity = MALFORMED_XML_DATA; // Constant from base class.
126 int statusCode = submitRequest(method, url, entity);
128 // Check the status code of the response: does it match
129 // the expected response(s)?
130 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
131 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
132 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
133 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
137 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
138 public void createWithWrongXmlSchema() {
141 setupCreateWithWrongXmlSchema();
143 // Submit the request to the service and store the response.
144 String method = REQUEST_TYPE.httpMethodName();
145 String url = getServiceRootURL();
146 final String entity = WRONG_XML_SCHEMA_DATA;
147 int statusCode = submitRequest(method, url, entity);
149 // Check the status code of the response: does it match
150 // the expected response(s)?
151 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
152 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
153 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
154 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
158 // ---------------------------------------------------------------
159 // CRUD tests : READ tests
160 // ---------------------------------------------------------------
165 @Test(dependsOnMethods = {"create"})
171 // Submit the request to the service and store the response.
172 ClientResponse<Intake> res = client.read(knownResourceId);
173 int statusCode = res.getStatus();
175 // Check the status code of the response: does it match
176 // the expected response(s)?
177 verbose("read: status = " + statusCode);
178 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
179 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
180 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
186 @Test(dependsOnMethods = {"read"})
187 public void readNonExistent() {
190 setupReadNonExistent();
192 // Submit the request to the service and store the response.
193 ClientResponse<Intake> res = client.read(NON_EXISTENT_ID);
194 int statusCode = res.getStatus();
196 // Check the status code of the response: does it match
197 // the expected response(s)?
198 verbose("readNonExistent: status = " + res.getStatus());
199 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
200 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
201 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
205 // ---------------------------------------------------------------
206 // CRUD tests : READ_LIST tests
207 // ---------------------------------------------------------------
212 @Test(dependsOnMethods = {"createList"})
213 public void readList() {
218 // Submit the request to the service and store the response.
219 ClientResponse<IntakeList> res = client.readList();
220 IntakeList list = res.getEntity();
221 int statusCode = res.getStatus();
223 // Check the status code of the response: does it match
224 // the expected response(s)?
225 verbose("readList: status = " + res.getStatus());
226 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
227 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
228 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
230 // Optionally output additional data about list members for debugging.
231 boolean iterateThroughList = false;
232 if (iterateThroughList && logger.isDebugEnabled()) {
233 List<IntakeList.IntakeListItem> items =
234 list.getIntakeListItem();
236 for(IntakeList.IntakeListItem item : items){
237 verbose("readList: list-item[" + i + "] csid=" +
239 verbose("readList: list-item[" + i + "] objectNumber=" +
240 item.getEntryNumber());
241 verbose("readList: list-item[" + i + "] URI=" +
254 // ---------------------------------------------------------------
255 // CRUD tests : UPDATE tests
256 // ---------------------------------------------------------------
261 @Test(dependsOnMethods = {"create"})
262 public void update() {
267 // Retrieve an existing resource that we can update.
268 ClientResponse<Intake> res = client.read(knownResourceId);
269 verbose("read: status = " + res.getStatus());
270 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
271 Intake intake = res.getEntity();
272 verbose("Got object to update with ID: " + knownResourceId,
273 intake, Intake.class);
275 // Update the content of this resource.
276 intake.setEntryNumber("updated-" + intake.getEntryNumber());
277 intake.setEntryDate("updated-" + intake.getEntryDate());
279 // Submit the request to the service and store the response.
280 res = client.update(knownResourceId, intake);
281 int statusCode = res.getStatus();
282 Intake updatedObject = res.getEntity();
284 // Check the status code of the response: does it match
285 // the expected response(s)?
286 verbose("update: status = " + res.getStatus());
287 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
288 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
289 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
291 // Check the contents of the response: does it match
292 // what was submitted?
293 verbose("update: ", updatedObject, Intake.class);
294 Assert.assertEquals(updatedObject.getEntryDate(),
295 intake.getEntryDate(),
296 "Data in updated object did not match submitted data.");
301 // Placeholders until the two tests below can be uncommented.
302 // See Issue CSPACE-401.
303 public void updateWithMalformedXml() {}
304 public void updateWithWrongXmlSchema() {}
308 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
309 public void updateWithMalformedXml() {
312 setupUpdateWithMalformedXml();
314 // Submit the request to the service and store the response.
315 String method = REQUEST_TYPE.httpMethodName();
316 String url = getResourceURL(knownResourceId);
317 final String entity = MALFORMED_XML_DATA;
318 int statusCode = submitRequest(method, url, entity);
320 // Check the status code of the response: does it match
321 // the expected response(s)?
322 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
323 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
324 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
325 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
329 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
330 public void updateWithWrongXmlSchema() {
333 setupUpdateWithWrongXmlSchema();
335 // Submit the request to the service and store the response.
336 String method = REQUEST_TYPE.httpMethodName();
337 String url = getResourceURL(knownResourceId);
338 final String entity = WRONG_XML_SCHEMA_DATA;
339 int statusCode = submitRequest(method, url, entity);
341 // Check the status code of the response: does it match
342 // the expected response(s)?
343 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
344 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
345 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
346 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
351 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
352 public void updateNonExistent() {
355 setupUpdateNonExistent();
357 // Submit the request to the service and store the response.
358 // Note: The ID used in this 'create' call may be arbitrary.
359 // The only relevant ID may be the one used in update(), below.
360 Intake intake = createIntakeInstance(NON_EXISTENT_ID);
361 ClientResponse<Intake> res =
362 client.update(NON_EXISTENT_ID, intake);
363 int statusCode = res.getStatus();
365 // Check the status code of the response: does it match
366 // the expected response(s)?
367 verbose("updateNonExistent: status = " + res.getStatus());
368 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
369 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
370 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
373 // ---------------------------------------------------------------
374 // CRUD tests : DELETE tests
375 // ---------------------------------------------------------------
380 @Test(dependsOnMethods =
381 {"create", "read", "update"})
382 public void delete() {
387 // Submit the request to the service and store the response.
388 ClientResponse<Response> res = client.delete(knownResourceId);
389 int statusCode = res.getStatus();
391 // Check the status code of the response: does it match
392 // the expected response(s)?
393 verbose("delete: status = " + res.getStatus());
394 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
395 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
396 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
402 @Test(dependsOnMethods = {"delete"})
403 public void deleteNonExistent() {
406 setupDeleteNonExistent();
408 // Submit the request to the service and store the response.
409 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
410 int statusCode = res.getStatus();
412 // Check the status code of the response: does it match
413 // the expected response(s)?
414 verbose("deleteNonExistent: status = " + res.getStatus());
415 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
416 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
417 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
421 // ---------------------------------------------------------------
422 // Utility tests : tests of code used in tests above
423 // ---------------------------------------------------------------
426 * Tests the code for manually submitting data that is used by several
427 * of the methods above.
429 @Test(dependsOnMethods = {"create", "read"})
430 public void testSubmitRequest() {
432 // Expected status code: 200 OK
433 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
435 // Submit the request to the service and store the response.
436 String method = ServiceRequestType.READ.httpMethodName();
437 String url = getResourceURL(knownResourceId);
438 int statusCode = submitRequest(method, url);
440 // Check the status code of the response: does it match
441 // the expected response(s)?
442 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
443 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
447 // ---------------------------------------------------------------
448 // Utility methods used by tests above
449 // ---------------------------------------------------------------
452 public String getServicePathComponent() {
453 return SERVICE_PATH_COMPONENT;
456 private Intake createIntakeInstance(String identifier) {
458 createIntakeInstance(
459 "entryNumber-" + identifier,
460 "entryDate-" + identifier);
464 private Intake createIntakeInstance(String entryNumber, String entryDate) {
465 Intake intake = new Intake();
466 intake.setEntryNumber(entryNumber);
467 intake.setEntryDate(entryDate);