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;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * IntakeServiceTest, carries out tests against a
46 * deployed and running Intake Service.
48 * $LastChangedRevision$
51 public class IntakeServiceTest extends AbstractServiceTest {
53 private final Logger logger =
54 LoggerFactory.getLogger(IntakeServiceTest.class);
56 // Instance variables specific to this test.
57 private IntakeClient client = new IntakeClient();
58 final String SERVICE_PATH_COMPONENT = "intakes";
59 private String knownResourceId = null;
61 // ---------------------------------------------------------------
62 // CRUD tests : CREATE tests
63 // ---------------------------------------------------------------
67 public void create() throws Exception {
69 // Perform setup, such as initializing the type of service request
70 // (e.g. CREATE, DELETE), its valid and expected status codes, and
71 // its associated HTTP method name (e.g. POST, DELETE).
74 // Submit the request to the service and store the response.
75 String identifier = createIdentifier();
77 MultipartOutput multipart = createIntakeInstance(identifier);
78 ClientResponse<Response> res = client.create(multipart);
80 int statusCode = res.getStatus();
82 // Check the status code of the response: does it match
83 // the expected response(s)?
86 // Does it fall within the set of valid status codes?
87 // Does it exactly match the expected status code?
88 if(logger.isDebugEnabled()){
89 logger.debug("create: status = " + statusCode);
91 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
92 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
93 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
95 // Store the ID returned from this create operation
96 // for additional tests below.
97 knownResourceId = extractId(res);
98 if(logger.isDebugEnabled()){
99 logger.debug("create: knownResourceId=" + knownResourceId);
104 @Test(dependsOnMethods = {"create"})
105 public void createList() throws Exception {
106 for(int i = 0; i < 3; i++){
112 // Placeholders until the three tests below can be uncommented.
113 // See Issue CSPACE-401.
115 public void createWithEmptyEntityBody() throws Exception {
119 public void createWithMalformedXml() throws Exception {
123 public void createWithWrongXmlSchema() throws Exception {
128 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
129 public void createWithEmptyEntityBody() throws Exception {
132 setupCreateWithEmptyEntityBody();
134 // Submit the request to the service and store the response.
135 String method = REQUEST_TYPE.httpMethodName();
136 String url = getServiceRootURL();
137 String mediaType = MediaType.APPLICATION_XML;
138 final String entity = "";
139 int statusCode = submitRequest(method, url, mediaType, entity);
141 // Check the status code of the response: does it match
142 // the expected response(s)?
143 if(logger.isDebugEnabled()){
144 logger.debug("createWithEmptyEntityBody url=" + url +
145 " status=" + statusCode);
147 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
148 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
149 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
153 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
154 public void createWithMalformedXml() throws Exception {
157 setupCreateWithMalformedXml();
159 // Submit the request to the service and store the response.
160 String method = REQUEST_TYPE.httpMethodName();
161 String url = getServiceRootURL();
162 String mediaType = MediaType.APPLICATION_XML;
163 final String entity = MALFORMED_XML_DATA; // Constant from base class.
164 int statusCode = submitRequest(method, url, mediaType, entity);
166 // Check the status code of the response: does it match
167 // the expected response(s)?
168 if(logger.isDebugEnabled()){
169 logger.debug("createWithMalformedXml url=" + url +
170 " status=" + statusCode);
172 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
173 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
174 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
178 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
179 public void createWithWrongXmlSchema() throws Exception {
182 setupCreateWithWrongXmlSchema();
184 // Submit the request to the service and store the response.
185 String method = REQUEST_TYPE.httpMethodName();
186 String url = getServiceRootURL();
187 String mediaType = MediaType.APPLICATION_XML;
188 final String entity = WRONG_XML_SCHEMA_DATA;
189 int statusCode = submitRequest(method, url, mediaType, entity);
191 // Check the status code of the response: does it match
192 // the expected response(s)?
193 if(logger.isDebugEnabled()){
194 logger.debug("createWithWrongSchema url=" + url +
195 " status=" + statusCode);
197 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
198 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
199 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
203 // ---------------------------------------------------------------
204 // CRUD tests : READ tests
205 // ---------------------------------------------------------------
208 @Test(dependsOnMethods = {"create"})
209 public void read() throws Exception {
214 // Submit the request to the service and store the response.
215 ClientResponse<MultipartInput> res = client.read(knownResourceId);
216 int statusCode = res.getStatus();
218 // Check the status code of the response: does it match
219 // the expected response(s)?
220 if(logger.isDebugEnabled()){
221 logger.debug("read: status = " + statusCode);
223 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
224 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
225 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
227 MultipartInput input = (MultipartInput) res.getEntity();
228 IntakesCommon intake = (IntakesCommon) extractPart(input,
229 client.getCommonPartName(), IntakesCommon.class);
230 Assert.assertNotNull(intake);
235 @Test(dependsOnMethods = {"read"})
236 public void readNonExistent() throws Exception {
239 setupReadNonExistent();
241 // Submit the request to the service and store the response.
242 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
243 int statusCode = res.getStatus();
245 // Check the status code of the response: does it match
246 // the expected response(s)?
247 if(logger.isDebugEnabled()){
248 logger.debug("readNonExistent: status = " + res.getStatus());
250 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
251 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
252 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
255 // ---------------------------------------------------------------
256 // CRUD tests : READ_LIST tests
257 // ---------------------------------------------------------------
260 @Test(dependsOnMethods = {"read"})
261 public void readList() throws Exception {
266 // Submit the request to the service and store the response.
267 ClientResponse<IntakesCommonList> res = client.readList();
268 IntakesCommonList list = res.getEntity();
269 int statusCode = res.getStatus();
271 // Check the status code of the response: does it match
272 // the expected response(s)?
273 if(logger.isDebugEnabled()){
274 logger.debug("readList: status = " + res.getStatus());
276 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
277 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
278 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
280 // Optionally output additional data about list members for debugging.
281 boolean iterateThroughList = false;
282 if(iterateThroughList && logger.isDebugEnabled()){
283 List<IntakesCommonList.IntakeListItem> items =
284 list.getIntakeListItem();
286 for(IntakesCommonList.IntakeListItem item : items){
287 logger.debug("readList: list-item[" + i + "] csid=" +
289 logger.debug("readList: list-item[" + i + "] objectNumber=" +
290 item.getEntryNumber());
291 logger.debug("readList: list-item[" + i + "] URI=" +
301 // ---------------------------------------------------------------
302 // CRUD tests : UPDATE tests
303 // ---------------------------------------------------------------
306 @Test(dependsOnMethods = {"read"})
307 public void update() throws Exception {
312 ClientResponse<MultipartInput> res =
313 client.read(knownResourceId);
314 if(logger.isDebugEnabled()){
315 logger.debug("update: read status = " + res.getStatus());
317 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
319 if(logger.isDebugEnabled()){
320 logger.debug("got object to update with ID: " + knownResourceId);
322 MultipartInput input = (MultipartInput) res.getEntity();
323 IntakesCommon intake = (IntakesCommon) extractPart(input,
324 client.getCommonPartName(), IntakesCommon.class);
325 Assert.assertNotNull(intake);
327 // Update the content of this resource.
328 // Update the content of this resource.
329 intake.setEntryNumber("updated-" + intake.getEntryNumber());
330 intake.setEntryDate("updated-" + intake.getEntryDate());
331 if(logger.isDebugEnabled()){
332 verbose("to be updated object", intake, IntakesCommon.class);
334 // Submit the request to the service and store the response.
335 MultipartOutput output = new MultipartOutput();
336 OutputPart commonPart = output.addPart(intake, MediaType.APPLICATION_XML_TYPE);
337 commonPart.getHeaders().add("label", client.getCommonPartName());
339 res = client.update(knownResourceId, output);
340 int statusCode = res.getStatus();
341 // Check the status code of the response: does it match the expected response(s)?
342 if(logger.isDebugEnabled()){
343 logger.debug("update: status = " + res.getStatus());
345 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
346 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
347 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
350 input = (MultipartInput) res.getEntity();
351 IntakesCommon updatedIntake =
352 (IntakesCommon) extractPart(input,
353 client.getCommonPartName(), IntakesCommon.class);
354 Assert.assertNotNull(updatedIntake);
356 Assert.assertEquals(updatedIntake.getEntryDate(),
357 intake.getEntryDate(),
358 "Data in updated object did not match submitted data.");
363 // Placeholders until the three tests below can be uncommented.
364 // See Issue CSPACE-401.
366 public void updateWithEmptyEntityBody() throws Exception{
369 public void updateWithMalformedXml() throws Exception {
372 public void updateWithWrongXmlSchema() throws Exception {
377 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
378 public void updateWithEmptyEntityBody() throws Exception {
381 setupUpdateWithEmptyEntityBody();
383 // Submit the request to the service and store the response.
384 String method = REQUEST_TYPE.httpMethodName();
385 String url = getResourceURL(knownResourceId);
386 String mediaType = MediaType.APPLICATION_XML;
387 final String entity = "";
388 int statusCode = submitRequest(method, url, mediaType, entity);
390 // Check the status code of the response: does it match
391 // the expected response(s)?
392 if(logger.isDebugEnabled()){
393 logger.debug("updateWithEmptyEntityBody url=" + url +
394 " status=" + statusCode);
396 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
397 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
398 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
402 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
403 public void updateWithMalformedXml() throws Exception {
406 setupUpdateWithMalformedXml();
408 // Submit the request to the service and store the response.
409 String method = REQUEST_TYPE.httpMethodName();
410 String url = getResourceURL(knownResourceId);
411 String mediaType = MediaType.APPLICATION_XML;
412 final String entity = MALFORMED_XML_DATA;
413 int statusCode = submitRequest(method, url, mediaType, entity);
415 // Check the status code of the response: does it match
416 // the expected response(s)?
417 if(logger.isDebugEnabled()){
418 logger.debug("updateWithMalformedXml: url=" + url +
419 " status=" + statusCode);
421 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
422 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
423 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
427 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
428 public void updateWithWrongXmlSchema() throws Exception {
431 setupUpdateWithWrongXmlSchema();
433 // Submit the request to the service and store the response.
434 String method = REQUEST_TYPE.httpMethodName();
435 String url = getResourceURL(knownResourceId);
436 String mediaType = MediaType.APPLICATION_XML;
437 final String entity = WRONG_XML_SCHEMA_DATA;
438 int statusCode = submitRequest(method, url, mediaType, entity);
440 // Check the status code of the response: does it match
441 // the expected response(s)?
442 if(logger.isDebugEnabled()){
443 logger.debug("updateWithWrongXmlSchema: url=" + url +
444 " status=" + statusCode);
446 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
447 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
448 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
453 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
454 public void updateNonExistent() throws Exception {
457 setupUpdateNonExistent();
459 // Submit the request to the service and store the response.
460 // Note: The ID used in this 'create' call may be arbitrary.
461 // The only relevant ID may be the one used in update(), below.
463 // The only relevant ID may be the one used in update(), below.
464 MultipartOutput multipart = createIntakeInstance(NON_EXISTENT_ID);
465 ClientResponse<MultipartInput> res =
466 client.update(NON_EXISTENT_ID, multipart);
467 int statusCode = res.getStatus();
469 // Check the status code of the response: does it match
470 // the expected response(s)?
471 if(logger.isDebugEnabled()){
472 logger.debug("updateNonExistent: status = " + res.getStatus());
474 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
475 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
476 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
479 // ---------------------------------------------------------------
480 // CRUD tests : DELETE tests
481 // ---------------------------------------------------------------
484 @Test(dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
485 public void delete() throws Exception {
490 // Submit the request to the service and store the response.
491 ClientResponse<Response> res = client.delete(knownResourceId);
492 int statusCode = res.getStatus();
494 // Check the status code of the response: does it match
495 // the expected response(s)?
496 if(logger.isDebugEnabled()){
497 logger.debug("delete: status = " + res.getStatus());
499 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
500 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
501 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
506 @Test(dependsOnMethods = {"delete"})
507 public void deleteNonExistent() throws Exception {
510 setupDeleteNonExistent();
512 // Submit the request to the service and store the response.
513 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
514 int statusCode = res.getStatus();
516 // Check the status code of the response: does it match
517 // the expected response(s)?
518 if(logger.isDebugEnabled()){
519 logger.debug("deleteNonExistent: status = " + res.getStatus());
521 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
522 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
523 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
526 // ---------------------------------------------------------------
527 // Utility tests : tests of code used in tests above
528 // ---------------------------------------------------------------
530 * Tests the code for manually submitting data that is used by several
531 * of the methods above.
533 @Test(dependsOnMethods = {"create", "read"})
534 public void testSubmitRequest() {
536 // Expected status code: 200 OK
537 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
539 // Submit the request to the service and store the response.
540 String method = ServiceRequestType.READ.httpMethodName();
541 String url = getResourceURL(knownResourceId);
542 int statusCode = submitRequest(method, url);
544 // Check the status code of the response: does it match
545 // the expected response(s)?
546 if(logger.isDebugEnabled()){
547 logger.debug("testSubmitRequest: url=" + url +
548 " status=" + statusCode);
550 Assert.assertEquals(statusCode, EXPECTED_STATUS);
554 // ---------------------------------------------------------------
555 // Utility methods used by tests above
556 // ---------------------------------------------------------------
558 public String getServicePathComponent() {
559 return SERVICE_PATH_COMPONENT;
562 private MultipartOutput createIntakeInstance(String identifier) {
563 return createIntakeInstance(
564 "entryNumber-" + identifier,
565 "entryDate-" + identifier);
568 private MultipartOutput createIntakeInstance(String entryNumber, String entryDate) {
569 IntakesCommon intake = new IntakesCommon();
570 intake.setEntryNumber(entryNumber);
571 intake.setEntryDate(entryDate);
572 MultipartOutput multipart = new MultipartOutput();
573 OutputPart commonPart =
574 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
575 commonPart.getHeaders().add("label", client.getCommonPartName());
577 if(logger.isDebugEnabled()){
578 verbose("to be created, intake common ", intake, IntakesCommon.class);