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.IntakeClient;
30 import org.collectionspace.services.intake.IntakesCommon;
31 import org.collectionspace.services.intake.IntakesCommonList;
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 * 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;
55 // ---------------------------------------------------------------
56 // CRUD tests : CREATE tests
57 // ---------------------------------------------------------------
61 public void create() {
63 // Perform setup, such as initializing the type of service request
64 // (e.g. CREATE, DELETE), its valid and expected status codes, and
65 // its associated HTTP method name (e.g. POST, DELETE).
68 // Submit the request to the service and store the response.
69 String identifier = createIdentifier();
71 MultipartOutput multipart = createIntakeInstance(identifier);
72 ClientResponse<Response> res = client.create(multipart);
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
88 // for additional tests below.
89 knownResourceId = extractId(res);
90 verbose("create: knownResourceId=" + knownResourceId);
94 @Test(dependsOnMethods = {"create"})
95 public void createList() {
96 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() {
107 public void createWithMalformedXml() {
110 public void createWithWrongXmlSchema() {
115 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
116 public void createWithEmptyEntityBody() {
119 setupCreateWithEmptyEntityBody();
121 // Submit the request to the service and store the response.
122 String method = REQUEST_TYPE.httpMethodName();
123 String url = getServiceRootURL();
124 String mediaType = MediaType.APPLICATION_XML;
125 final String entity = "";
126 int statusCode = submitRequest(method, url, mediaType, entity);
128 // Check the status code of the response: does it match
129 // the expected response(s)?
130 verbose("createWithEmptyEntityBody 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 createWithMalformedXml() {
141 setupCreateWithMalformedXml();
143 // Submit the request to the service and store the response.
144 String method = REQUEST_TYPE.httpMethodName();
145 String url = getServiceRootURL();
146 String mediaType = MediaType.APPLICATION_XML;
147 final String entity = MALFORMED_XML_DATA; // Constant from base class.
148 int statusCode = submitRequest(method, url, mediaType, entity);
150 // Check the status code of the response: does it match
151 // the expected response(s)?
152 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
153 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
154 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
155 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
159 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
160 public void createWithWrongXmlSchema() {
163 setupCreateWithWrongXmlSchema();
165 // Submit the request to the service and store the response.
166 String method = REQUEST_TYPE.httpMethodName();
167 String url = getServiceRootURL();
168 String mediaType = MediaType.APPLICATION_XML;
169 final String entity = WRONG_XML_SCHEMA_DATA;
170 int statusCode = submitRequest(method, url, mediaType, entity);
172 // Check the status code of the response: does it match
173 // the expected response(s)?
174 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
175 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
176 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
177 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
180 // ---------------------------------------------------------------
181 // CRUD tests : READ tests
182 // ---------------------------------------------------------------
185 @Test(dependsOnMethods = {"create"})
191 // Submit the request to the service and store the response.
192 ClientResponse<MultipartInput> res = client.read(knownResourceId);
193 int statusCode = res.getStatus();
195 // Check the status code of the response: does it match
196 // the expected response(s)?
197 verbose("read: status = " + statusCode);
198 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
199 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
200 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
201 //FIXME: remove the following try catch once Aron fixes signatures
203 MultipartInput input = (MultipartInput) res.getEntity();
204 IntakesCommon intake = (IntakesCommon) extractPart(input,
205 client.getCommonPartName(), IntakesCommon.class);
206 Assert.assertNotNull(intake);
208 throw new RuntimeException(e);
214 @Test(dependsOnMethods = {"read"})
215 public void readNonExistent() {
218 setupReadNonExistent();
220 // Submit the request to the service and store the response.
221 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
222 int statusCode = res.getStatus();
224 // Check the status code of the response: does it match
225 // the expected response(s)?
226 verbose("readNonExistent: status = " + res.getStatus());
227 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
228 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
229 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
232 // ---------------------------------------------------------------
233 // CRUD tests : READ_LIST tests
234 // ---------------------------------------------------------------
237 @Test(dependsOnMethods = {"read"})
238 public void readList() {
243 // Submit the request to the service and store the response.
244 ClientResponse<IntakesCommonList> res = client.readList();
245 IntakesCommonList list = res.getEntity();
246 int statusCode = res.getStatus();
248 // Check the status code of the response: does it match
249 // the expected response(s)?
250 verbose("readList: status = " + res.getStatus());
251 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
252 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
253 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
255 // Optionally output additional data about list members for debugging.
256 boolean iterateThroughList = false;
257 if(iterateThroughList && logger.isDebugEnabled()){
258 List<IntakesCommonList.IntakeListItem> items =
259 list.getIntakeListItem();
261 for(IntakesCommonList.IntakeListItem item : items){
262 verbose("readList: list-item[" + i + "] csid=" +
264 verbose("readList: list-item[" + i + "] objectNumber=" +
265 item.getEntryNumber());
266 verbose("readList: list-item[" + i + "] URI=" +
276 // ---------------------------------------------------------------
277 // CRUD tests : UPDATE tests
278 // ---------------------------------------------------------------
281 @Test(dependsOnMethods = {"read"})
282 public void update() {
287 try{ //ideally, just remove try-catch and let the exception bubble up
288 // Retrieve an existing resource that we can update.
289 ClientResponse<MultipartInput> res =
290 client.read(knownResourceId);
291 verbose("update: read status = " + res.getStatus());
292 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
294 verbose("got object to update with ID: " + knownResourceId);
295 MultipartInput input = (MultipartInput) res.getEntity();
296 IntakesCommon intake = (IntakesCommon) extractPart(input,
297 client.getCommonPartName(), IntakesCommon.class);
298 Assert.assertNotNull(intake);
300 // Update the content of this resource.
301 // Update the content of this resource.
302 intake.setEntryNumber("updated-" + intake.getEntryNumber());
303 intake.setEntryDate("updated-" + intake.getEntryDate());
304 verbose("to be updated object", intake, IntakesCommon.class);
305 // Submit the request to the service and store the response.
306 MultipartOutput output = new MultipartOutput();
307 OutputPart commonPart = output.addPart(intake, MediaType.APPLICATION_XML_TYPE);
308 commonPart.getHeaders().add("label", client.getCommonPartName());
310 res = client.update(knownResourceId, output);
311 int statusCode = res.getStatus();
312 // Check the status code of the response: does it match the expected response(s)?
313 verbose("update: status = " + res.getStatus());
314 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
315 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
316 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
319 input = (MultipartInput) res.getEntity();
320 IntakesCommon updatedIntake =
321 (IntakesCommon) extractPart(input,
322 client.getCommonPartName(), IntakesCommon.class);
323 Assert.assertNotNull(updatedIntake);
325 Assert.assertEquals(updatedIntake.getEntryDate(),
326 intake.getEntryDate(),
327 "Data in updated object did not match submitted data.");
334 // Placeholders until the three tests below can be uncommented.
335 // See Issue CSPACE-401.
336 public void updateWithEmptyEntityBody() {
339 public void updateWithMalformedXml() {
342 public void updateWithWrongXmlSchema() {
347 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
348 public void updateWithEmptyEntityBody() {
351 setupUpdateWithEmptyEntityBody();
353 // Submit the request to the service and store the response.
354 String method = REQUEST_TYPE.httpMethodName();
355 String url = getResourceURL(knownResourceId);
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("updateWithEmptyEntityBody 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 String mediaType = MediaType.APPLICATION_XML;
379 final String entity = MALFORMED_XML_DATA;
380 int statusCode = submitRequest(method, url, mediaType, entity);
382 // Check the status code of the response: does it match
383 // the expected response(s)?
384 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
385 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
386 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
387 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
391 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
392 public void updateWithWrongXmlSchema() {
395 setupUpdateWithWrongXmlSchema();
397 // Submit the request to the service and store the response.
398 String method = REQUEST_TYPE.httpMethodName();
399 String url = getResourceURL(knownResourceId);
400 String mediaType = MediaType.APPLICATION_XML;
401 final String entity = WRONG_XML_SCHEMA_DATA;
402 int statusCode = submitRequest(method, url, mediaType, entity);
404 // Check the status code of the response: does it match
405 // the expected response(s)?
406 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
407 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
408 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
409 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
413 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
414 public void updateNonExistent() {
417 setupUpdateNonExistent();
419 // Submit the request to the service and store the response.
420 // Note: The ID used in this 'create' call may be arbitrary.
421 // The only relevant ID may be the one used in update(), below.
423 // The only relevant ID may be the one used in update(), below.
424 MultipartOutput multipart = createIntakeInstance(NON_EXISTENT_ID);
425 ClientResponse<MultipartInput> res =
426 client.update(NON_EXISTENT_ID, multipart);
427 int statusCode = res.getStatus();
429 // Check the status code of the response: does it match
430 // the expected response(s)?
431 verbose("updateNonExistent: status = " + res.getStatus());
432 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
433 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
434 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
437 // ---------------------------------------------------------------
438 // CRUD tests : DELETE tests
439 // ---------------------------------------------------------------
442 @Test(dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
443 public void delete() {
448 // Submit the request to the service and store the response.
449 ClientResponse<Response> res = client.delete(knownResourceId);
450 int statusCode = res.getStatus();
452 // Check the status code of the response: does it match
453 // the expected response(s)?
454 verbose("delete: status = " + res.getStatus());
455 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
456 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
457 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
462 @Test(dependsOnMethods = {"delete"})
463 public void deleteNonExistent() {
466 setupDeleteNonExistent();
468 // Submit the request to the service and store the response.
469 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
470 int statusCode = res.getStatus();
472 // Check the status code of the response: does it match
473 // the expected response(s)?
474 verbose("deleteNonExistent: status = " + res.getStatus());
475 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
476 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
477 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
480 // ---------------------------------------------------------------
481 // Utility tests : tests of code used in tests above
482 // ---------------------------------------------------------------
484 * Tests the code for manually submitting data that is used by several
485 * of the methods above.
487 @Test(dependsOnMethods = {"create", "read"})
488 public void testSubmitRequest() {
490 // Expected status code: 200 OK
491 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
493 // Submit the request to the service and store the response.
494 String method = ServiceRequestType.READ.httpMethodName();
495 String url = getResourceURL(knownResourceId);
496 int statusCode = submitRequest(method, url);
498 // Check the status code of the response: does it match
499 // the expected response(s)?
500 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
501 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
505 // ---------------------------------------------------------------
506 // Utility methods used by tests above
507 // ---------------------------------------------------------------
509 public String getServicePathComponent() {
510 return SERVICE_PATH_COMPONENT;
513 private MultipartOutput createIntakeInstance(String identifier) {
514 return createIntakeInstance(
515 "entryNumber-" + identifier,
516 "entryDate-" + identifier);
519 private MultipartOutput createIntakeInstance(String entryNumber, String entryDate) {
520 IntakesCommon intake = new IntakesCommon();
521 intake.setEntryNumber(entryNumber);
522 intake.setEntryDate(entryDate);
523 MultipartOutput multipart = new MultipartOutput();
524 OutputPart commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
525 commonPart.getHeaders().add("label", client.getCommonPartName());
527 verbose("to be created, intake common ", intake, IntakesCommon.class);