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: 511 $
45 * $LastChangedDate: 2009-08-06 20:16:16 +0000 (Thu, 06 Aug 2009) $
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 knownObjectId = 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 = createIntake(identifier);
73 ClientResponse<Response> res = client.create(intake);
74 int statusCode = res.getStatus();
76 // Check the status code of the response: does it match the expected response(s)?
78 // Does it fall within the set of valid status codes?
79 // Does it exactly match the expected status code?
80 verbose("create: status = " + statusCode);
81 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
82 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
83 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
85 // Store the ID returned from this create operation for additional tests below.
86 knownObjectId = extractId(res);
90 @Test(dependsOnMethods = {"create"})
91 public void createList() {
92 for(int i = 0; i < 3; i++){
100 @Test(dependsOnMethods = {"create"}, expectedExceptions = IllegalArgumentException.class)
101 public void createNull() {
102 ClientResponse<Response> res = client.create(null);
105 // Placeholders until the two tests below can be uncommented. See Issue CSPACE-401.
106 public void createWithMalformedXml() {}
107 public void createWithWrongXmlSchema() {}
111 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
112 public void createWithMalformedXml() {
115 setupCreateWithMalformedXml();
117 // Submit the request to the service and store the response.
118 String method = REQUEST_TYPE.httpMethodName();
119 String url = getServiceRootURL();
120 final String entity = MALFORMED_XML_DATA; // Constant from abstract base class.
121 int statusCode = submitRequest(method, url, entity);
123 // Check the status code of the response: does it match 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 the expected response(s)?
144 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
145 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
146 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
147 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
151 // ---------------------------------------------------------------
152 // CRUD tests : READ tests
153 // ---------------------------------------------------------------
158 @Test(dependsOnMethods = {"create"})
164 // Submit the request to the service and store the response.
165 ClientResponse<Intake> res = client.read(knownObjectId);
166 int statusCode = res.getStatus();
168 // Check the status code of the response: does it match the expected response(s)?
169 verbose("read: status = " + statusCode);
170 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
171 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
172 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
178 @Test(dependsOnMethods = {"read"})
179 public void readNonExistent() {
182 setupReadNonExistent();
184 // Submit the request to the service and store the response.
185 ClientResponse<Intake> res = client.read(NON_EXISTENT_ID);
186 int statusCode = res.getStatus();
188 // Check the status code of the response: does it match the expected response(s)?
189 verbose("readNonExistent: status = " + res.getStatus());
190 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
191 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
192 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
196 // ---------------------------------------------------------------
197 // CRUD tests : READ_LIST tests
198 // ---------------------------------------------------------------
203 @Test(dependsOnMethods = {"createList"})
204 public void readList() {
209 // Submit the request to the service and store the response.
210 ClientResponse<IntakeList> res = client.readList();
211 IntakeList list = res.getEntity();
212 int statusCode = res.getStatus();
214 // Check the status code of the response: does it match the expected response(s)?
215 verbose("readList: status = " + res.getStatus());
216 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
217 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
218 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
220 // Optionally output additional data about list members for debugging.
221 boolean iterateThroughList = false;
222 if (iterateThroughList && logger.isDebugEnabled()) {
223 List<IntakeList.IntakeListItem> items =
224 list.getIntakeListItem();
226 for(IntakeList.IntakeListItem item : items){
227 verbose("readList: list-item[" + i + "] csid=" + item.getCsid());
228 verbose("readList: list-item[" + i + "] objectNumber=" + item.getEntryNumber());
229 verbose("readList: list-item[" + i + "] URI=" + item.getUri());
241 // ---------------------------------------------------------------
242 // CRUD tests : UPDATE tests
243 // ---------------------------------------------------------------
248 @Test(dependsOnMethods = {"create"})
249 public void update() {
254 // Retrieve an existing resource that we can update.
255 ClientResponse<Intake> res = client.read(knownObjectId);
256 verbose("read: status = " + res.getStatus());
257 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
258 Intake intake = res.getEntity();
259 verbose("Got object to update with ID: " + knownObjectId,
260 intake, Intake.class);
262 // Update the content of this resource.
263 intake.setEntryNumber("updated-" + intake.getEntryNumber());
264 intake.setEntryDate("updated-" + intake.getEntryDate());
266 // Submit the request to the service and store the response.
267 res = client.update(knownObjectId, intake);
268 int statusCode = res.getStatus();
269 Intake updatedObject = res.getEntity();
271 // Check the status code of the response: does it match the expected response(s)?
272 verbose("update: status = " + res.getStatus());
273 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
274 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
275 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
277 // Check the contents of the response: does it match what was submitted?
278 verbose("update: ", updatedObject, Intake.class);
279 Assert.assertEquals(updatedObject.getEntryDate(),
280 intake.getEntryDate(),
281 "Data in updated object did not match submitted data.");
286 // Placeholders until the two tests below can be uncommented. See Issue CSPACE-401.
287 public void updateWithMalformedXml() {}
288 public void updateWithWrongXmlSchema() {}
292 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
293 public void updateWithMalformedXml() {
296 setupUpdateWithMalformedXml();
298 // Submit the request to the service and store the response.
299 String method = REQUEST_TYPE.httpMethodName();
300 String url = getResourceURL(knownObjectId);
301 final String entity = MALFORMED_XML_DATA; // Constant from abstract base class.
302 int statusCode = submitRequest(method, url, entity);
304 // Check the status code of the response: does it match the expected response(s)?
305 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
306 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
307 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
308 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
312 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
313 public void updateWithWrongXmlSchema() {
316 setupUpdateWithWrongXmlSchema();
318 // Submit the request to the service and store the response.
319 String method = REQUEST_TYPE.httpMethodName();
320 String url = getResourceURL(knownObjectId);
321 final String entity = WRONG_XML_SCHEMA_DATA; // Constant from abstract base class.
322 int statusCode = submitRequest(method, url, entity);
324 // Check the status code of the response: does it match the expected response(s)?
325 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
326 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
327 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
328 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
333 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
334 public void updateNonExistent() {
337 setupUpdateNonExistent();
339 // Submit the request to the service and store the response.
340 // Note: The ID used in this 'create' call may be arbitrary.
341 // The only relevant ID may be the one used in update(), below.
342 Intake intake = createIntake(NON_EXISTENT_ID);
343 ClientResponse<Intake> res =
344 client.update(NON_EXISTENT_ID, intake);
345 int statusCode = res.getStatus();
347 // Check the status code of the response: does it match the expected response(s)?
348 verbose("updateNonExistent: status = " + res.getStatus());
349 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
350 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
351 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
354 // ---------------------------------------------------------------
355 // CRUD tests : DELETE tests
356 // ---------------------------------------------------------------
361 @Test(dependsOnMethods =
362 {"create", "read", "update"})
363 public void delete() {
368 // Submit the request to the service and store the response.
369 ClientResponse<Response> res = client.delete(knownObjectId);
370 int statusCode = res.getStatus();
372 // Check the status code of the response: does it match the expected response(s)?
373 verbose("delete: status = " + res.getStatus());
374 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
375 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
376 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
382 @Test(dependsOnMethods = {"delete"})
383 public void deleteNonExistent() {
386 setupDeleteNonExistent();
388 // Submit the request to the service and store the response.
389 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
390 int statusCode = res.getStatus();
392 // Check the status code of the response: does it match the expected response(s)?
393 verbose("deleteNonExistent: status = " + res.getStatus());
394 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
395 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
396 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
400 // ---------------------------------------------------------------
401 // Utility tests : tests of code used in tests above
402 // ---------------------------------------------------------------
405 * Tests the code for manually submitting data that is used by several
406 * of the methods above.
408 @Test(dependsOnMethods = {"create", "read"})
409 public void testSubmitRequest() {
411 // Expected status code: 200 OK
412 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
414 // Submit the request to the service and store the response.
415 String method = ServiceRequestType.READ.httpMethodName();
416 String url = getResourceURL(knownObjectId);
417 int statusCode = submitRequest(method, url);
419 // Check the status code of the response: does it match the expected response(s)?
420 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
421 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
425 // ---------------------------------------------------------------
426 // Utility methods used by tests above
427 // ---------------------------------------------------------------
430 public String getServicePathComponent() {
431 // @TODO Determine if it is possible to obtain this
432 // value programmatically.
434 // We set this in an annotation in the CollectionObjectProxy
435 // interface, for instance. We also set service-specific
436 // constants in each service module, which might also
437 // return this value.
438 return SERVICE_PATH_COMPONENT;
441 private Intake createIntake(String identifier) {
444 "entryNumber-" + identifier,
445 "entryDate-" + identifier);
449 private Intake createIntake(String entryNumber, String entryDate) {
450 Intake intake = new Intake();
451 intake.setEntryNumber(entryNumber);
452 intake.setEntryDate(entryDate);