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.MediaType;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.Response.Status;
31 import org.collectionspace.services.client.IntakeClient;
32 import org.collectionspace.services.client.test.ServiceRequestType;
33 import org.collectionspace.services.intake.Intake;
34 import org.collectionspace.services.intake.IntakeList;
36 import org.jboss.resteasy.client.ClientResponse;
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
42 * IntakeServiceTest, carries out tests against a
43 * deployed and running Intake Service.
45 * $LastChangedRevision$
48 public class IntakeServiceTest extends AbstractServiceTest {
50 // Instance variables specific to this test.
51 private IntakeClient client = new IntakeClient();
52 final String SERVICE_PATH_COMPONENT = "intakes";
53 private String knownResourceId = null;
56 // ---------------------------------------------------------------
57 // CRUD tests : CREATE tests
58 // ---------------------------------------------------------------
64 public void create() {
66 // Perform setup, such as initializing the type of service request
67 // (e.g. CREATE, DELETE), its valid and expected status codes, and
68 // its associated HTTP method name (e.g. POST, DELETE).
71 // Submit the request to the service and store the response.
72 String identifier = createIdentifier();
73 Intake intake = createIntakeInstance(identifier);
74 ClientResponse<Response> res = client.create(intake);
75 int statusCode = res.getStatus();
77 // Check the status code of the response: does it match
78 // the expected response(s)?
81 // Does it fall within the set of valid status codes?
82 // Does it exactly match the expected status code?
83 verbose("create: status = " + statusCode);
84 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
85 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
86 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
88 // Store the ID returned from this create operation for
89 // additional tests below.
90 knownResourceId = extractId(res);
94 @Test(dependsOnMethods = {"create"})
95 public void createList() {
96 for(int i = 0; i < 3; i++){
103 // Placeholders until the three tests below can be uncommented.
104 // See Issue CSPACE-401.
105 public void createWithEmptyEntityBody() {}
106 public void createWithMalformedXml() {}
107 public void createWithWrongXmlSchema() {}
111 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
112 public void createWithEmptyEntityBody() {
115 setupCreateWithEmptyEntityBody();
117 // Submit the request to the service and store the response.
118 String method = REQUEST_TYPE.httpMethodName();
119 String url = getServiceRootURL();
120 String mediaType = MediaType.APPLICATION_XML;
121 final String entity = "";
122 int statusCode = submitRequest(method, url, mediaType, entity);
124 // Check the status code of the response: does it match
125 // the expected response(s)?
126 verbose("createWithEmptyEntityBody url=" + url + " status=" + statusCode);
127 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
128 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
129 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
133 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
134 public void createWithMalformedXml() {
137 setupCreateWithMalformedXml();
139 // Submit the request to the service and store the response.
140 String method = REQUEST_TYPE.httpMethodName();
141 String url = getServiceRootURL();
142 String mediaType = MediaType.APPLICATION_XML;
143 final String entity = MALFORMED_XML_DATA; // Constant from base class.
144 int statusCode = submitRequest(method, url, mediaType, entity);
146 // Check the status code of the response: does it match
147 // the expected response(s)?
148 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
149 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
150 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
151 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
155 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
156 public void createWithWrongXmlSchema() {
159 setupCreateWithWrongXmlSchema();
161 // Submit the request to the service and store the response.
162 String method = REQUEST_TYPE.httpMethodName();
163 String url = getServiceRootURL();
164 String mediaType = MediaType.APPLICATION_XML;
165 final String entity = WRONG_XML_SCHEMA_DATA;
166 int statusCode = submitRequest(method, url, mediaType, entity);
168 // Check the status code of the response: does it match
169 // the expected response(s)?
170 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
171 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
172 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
173 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
177 // ---------------------------------------------------------------
178 // CRUD tests : READ tests
179 // ---------------------------------------------------------------
184 @Test(dependsOnMethods = {"create"})
190 // Submit the request to the service and store the response.
191 ClientResponse<Intake> res = client.read(knownResourceId);
192 int statusCode = res.getStatus();
194 // Check the status code of the response: does it match
195 // the expected response(s)?
196 verbose("read: status = " + statusCode);
197 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
198 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
199 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
205 @Test(dependsOnMethods = {"read"})
206 public void readNonExistent() {
209 setupReadNonExistent();
211 // Submit the request to the service and store the response.
212 ClientResponse<Intake> res = client.read(NON_EXISTENT_ID);
213 int statusCode = res.getStatus();
215 // Check the status code of the response: does it match
216 // the expected response(s)?
217 verbose("readNonExistent: status = " + res.getStatus());
218 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
219 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
220 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
224 // ---------------------------------------------------------------
225 // CRUD tests : READ_LIST tests
226 // ---------------------------------------------------------------
231 @Test(dependsOnMethods = {"createList"})
232 public void readList() {
237 // Submit the request to the service and store the response.
238 ClientResponse<IntakeList> res = client.readList();
239 IntakeList list = res.getEntity();
240 int statusCode = res.getStatus();
242 // Check the status code of the response: does it match
243 // the expected response(s)?
244 verbose("readList: status = " + res.getStatus());
245 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
246 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
247 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
249 // Optionally output additional data about list members for debugging.
250 boolean iterateThroughList = false;
251 if (iterateThroughList && logger.isDebugEnabled()) {
252 List<IntakeList.IntakeListItem> items =
253 list.getIntakeListItem();
255 for(IntakeList.IntakeListItem item : items){
256 verbose("readList: list-item[" + i + "] csid=" +
258 verbose("readList: list-item[" + i + "] objectNumber=" +
259 item.getEntryNumber());
260 verbose("readList: list-item[" + i + "] URI=" +
273 // ---------------------------------------------------------------
274 // CRUD tests : UPDATE tests
275 // ---------------------------------------------------------------
280 @Test(dependsOnMethods = {"create"})
281 public void update() {
286 // Retrieve an existing resource that we can update.
287 ClientResponse<Intake> res = client.read(knownResourceId);
288 verbose("read: status = " + res.getStatus());
289 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
290 Intake intake = res.getEntity();
291 verbose("Got object to update with ID: " + knownResourceId,
292 intake, Intake.class);
294 // Update the content of this resource.
295 intake.setEntryNumber("updated-" + intake.getEntryNumber());
296 intake.setEntryDate("updated-" + intake.getEntryDate());
298 // Submit the request to the service and store the response.
299 res = client.update(knownResourceId, intake);
300 int statusCode = res.getStatus();
301 Intake updatedObject = res.getEntity();
303 // Check the status code of the response: does it match
304 // the expected response(s)?
305 verbose("update: status = " + res.getStatus());
306 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
307 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
308 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
310 // Check the contents of the response: does it match
311 // what was submitted?
312 verbose("update: ", updatedObject, Intake.class);
313 Assert.assertEquals(updatedObject.getEntryDate(),
314 intake.getEntryDate(),
315 "Data in updated object did not match submitted data.");
320 // Placeholders until the three tests below can be uncommented.
321 // See Issue CSPACE-401.
322 public void updateWithEmptyEntityBody() {}
323 public void updateWithMalformedXml() {}
324 public void updateWithWrongXmlSchema() {}
328 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
329 public void updateWithEmptyEntityBody() {
332 setupUpdateWithEmptyEntityBody();
334 // Submit the request to the service and store the response.
335 String method = REQUEST_TYPE.httpMethodName();
336 String url = getResourceURL(knownResourceId);
337 String mediaType = MediaType.APPLICATION_XML;
338 final String entity = "";
339 int statusCode = submitRequest(method, url, mediaType, entity);
341 // Check the status code of the response: does it match
342 // the expected response(s)?
343 verbose("updateWithEmptyEntityBody url=" + url + " status=" + statusCode);
344 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
345 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
346 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
350 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
351 public void updateWithMalformedXml() {
354 setupUpdateWithMalformedXml();
356 // Submit the request to the service and store the response.
357 String method = REQUEST_TYPE.httpMethodName();
358 String url = getResourceURL(knownResourceId);
359 String mediaType = MediaType.APPLICATION_XML;
360 final String entity = MALFORMED_XML_DATA;
361 int statusCode = submitRequest(method, url, mediaType, entity);
363 // Check the status code of the response: does it match
364 // the expected response(s)?
365 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
366 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
367 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
368 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
372 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
373 public void updateWithWrongXmlSchema() {
376 setupUpdateWithWrongXmlSchema();
378 // Submit the request to the service and store the response.
379 String method = REQUEST_TYPE.httpMethodName();
380 String url = getResourceURL(knownResourceId);
381 String mediaType = MediaType.APPLICATION_XML;
382 final String entity = WRONG_XML_SCHEMA_DATA;
383 int statusCode = submitRequest(method, url, mediaType, entity);
385 // Check the status code of the response: does it match
386 // the expected response(s)?
387 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
388 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
389 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
390 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
395 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
396 public void updateNonExistent() {
399 setupUpdateNonExistent();
401 // Submit the request to the service and store the response.
402 // Note: The ID used in this 'create' call may be arbitrary.
403 // The only relevant ID may be the one used in update(), below.
404 Intake intake = createIntakeInstance(NON_EXISTENT_ID);
405 ClientResponse<Intake> res =
406 client.update(NON_EXISTENT_ID, intake);
407 int statusCode = res.getStatus();
409 // Check the status code of the response: does it match
410 // the expected response(s)?
411 verbose("updateNonExistent: status = " + res.getStatus());
412 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
413 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
414 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
417 // ---------------------------------------------------------------
418 // CRUD tests : DELETE tests
419 // ---------------------------------------------------------------
424 @Test(dependsOnMethods =
425 {"create", "read", "update"})
426 public void delete() {
431 // Submit the request to the service and store the response.
432 ClientResponse<Response> res = client.delete(knownResourceId);
433 int statusCode = res.getStatus();
435 // Check the status code of the response: does it match
436 // the expected response(s)?
437 verbose("delete: status = " + res.getStatus());
438 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
439 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
440 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
446 @Test(dependsOnMethods = {"delete"})
447 public void deleteNonExistent() {
450 setupDeleteNonExistent();
452 // Submit the request to the service and store the response.
453 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
454 int statusCode = res.getStatus();
456 // Check the status code of the response: does it match
457 // the expected response(s)?
458 verbose("deleteNonExistent: status = " + res.getStatus());
459 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
460 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
461 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
465 // ---------------------------------------------------------------
466 // Utility tests : tests of code used in tests above
467 // ---------------------------------------------------------------
470 * Tests the code for manually submitting data that is used by several
471 * of the methods above.
473 @Test(dependsOnMethods = {"create", "read"})
474 public void testSubmitRequest() {
476 // Expected status code: 200 OK
477 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
479 // Submit the request to the service and store the response.
480 String method = ServiceRequestType.READ.httpMethodName();
481 String url = getResourceURL(knownResourceId);
482 int statusCode = submitRequest(method, url);
484 // Check the status code of the response: does it match
485 // the expected response(s)?
486 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
487 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
491 // ---------------------------------------------------------------
492 // Utility methods used by tests above
493 // ---------------------------------------------------------------
496 public String getServicePathComponent() {
497 return SERVICE_PATH_COMPONENT;
500 private Intake createIntakeInstance(String identifier) {
502 createIntakeInstance(
503 "entryNumber-" + identifier,
504 "entryDate-" + identifier);
508 private Intake createIntakeInstance(String entryNumber, String entryDate) {
509 Intake intake = new Intake();
510 intake.setEntryNumber(entryNumber);
511 intake.setEntryDate(entryDate);