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.ArrayList;
26 import java.util.List;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
30 import org.collectionspace.services.client.IntakeClient;
31 import org.collectionspace.services.intake.IntakesCommon;
32 import org.collectionspace.services.intake.IntakesCommonList;
34 import org.jboss.resteasy.client.ClientResponse;
36 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
37 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
38 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
39 import org.testng.Assert;
40 import org.testng.annotations.AfterClass;
41 import org.testng.annotations.Test;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * IntakeServiceTest, carries out tests against a
48 * deployed and running Intake Service.
50 * $LastChangedRevision$
53 public class IntakeServiceTest extends AbstractServiceTest {
55 private final Logger logger =
56 LoggerFactory.getLogger(IntakeServiceTest.class);
58 // Instance variables specific to this test.
59 private IntakeClient client = new IntakeClient();
60 final String SERVICE_PATH_COMPONENT = "intakes";
61 private String knownResourceId = null;
62 private List<String> additionalResourceIds = new ArrayList();
64 // ---------------------------------------------------------------
65 // CRUD tests : CREATE tests
66 // ---------------------------------------------------------------
69 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
70 public void create(String testName) throws Exception {
72 // Perform setup, such as initializing the type of service request
73 // (e.g. CREATE, DELETE), its valid and expected status codes, and
74 // its associated HTTP method name (e.g. POST, DELETE).
75 setupCreate(testName);
77 // Submit the request to the service and store the response.
78 String identifier = createIdentifier();
80 MultipartOutput multipart = createIntakeInstance(identifier);
81 ClientResponse<Response> res = client.create(multipart);
83 int statusCode = res.getStatus();
85 // Check the status code of the response: does it match
86 // the expected response(s)?
89 // Does it fall within the set of valid status codes?
90 // Does it exactly match the expected status code?
91 if(logger.isDebugEnabled()){
92 logger.debug(testName + ": status = " + statusCode);
94 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
95 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
96 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
98 // Store the ID returned from the first resource created
99 // for additional tests below.
100 if (knownResourceId == null){
101 knownResourceId = extractId(res);
102 if (logger.isDebugEnabled()) {
103 logger.debug(testName + ": knownResourceId=" + knownResourceId);
105 // Store the IDs from any additional resources created by tests,
106 // so they can be deleted after all tests have been run.
108 additionalResourceIds.add(extractId(res));
113 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
114 dependsOnMethods = {"create"})
115 public void createList(String testName) throws Exception {
116 for(int i = 0; i < 3; i++){
122 // Placeholders until the three tests below can be uncommented.
123 // See Issue CSPACE-401.
125 public void createWithEmptyEntityBody(String testName) throws Exception {
129 public void createWithMalformedXml(String testName) throws Exception {
133 public void createWithWrongXmlSchema(String testName) throws Exception {
138 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
139 dependsOnMethods = {"create", "testSubmitRequest"})
140 public void createWithEmptyEntityBody(String testName) throws Exception {
143 setupCreateWithEmptyEntityBody(testName);
145 // Submit the request to the service and store the response.
146 String method = REQUEST_TYPE.httpMethodName();
147 String url = getServiceRootURL();
148 String mediaType = MediaType.APPLICATION_XML;
149 final String entity = "";
150 int statusCode = submitRequest(method, url, mediaType, entity);
152 // Check the status code of the response: does it match
153 // the expected response(s)?
154 if(logger.isDebugEnabled()){
155 logger.debug("createWithEmptyEntityBody url=" + url +
156 " status=" + statusCode);
158 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
159 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
160 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
164 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
165 dependsOnMethods = {"create", "testSubmitRequest"})
166 public void createWithMalformedXml(String testName) throws Exception {
169 setupCreateWithMalformedXml(testName);
171 // Submit the request to the service and store the response.
172 String method = REQUEST_TYPE.httpMethodName();
173 String url = getServiceRootURL();
174 String mediaType = MediaType.APPLICATION_XML;
175 final String entity = MALFORMED_XML_DATA; // Constant from base class.
176 int statusCode = submitRequest(method, url, mediaType, entity);
178 // Check the status code of the response: does it match
179 // the expected response(s)?
180 if(logger.isDebugEnabled()){
181 logger.debug(testName + ": url=" + url +
182 " status=" + statusCode);
184 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
185 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
186 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
190 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
191 dependsOnMethods = {"create", "testSubmitRequest"})
192 public void createWithWrongXmlSchema(String testName) throws Exception {
195 setupCreateWithWrongXmlSchema(testName);
197 // Submit the request to the service and store the response.
198 String method = REQUEST_TYPE.httpMethodName();
199 String url = getServiceRootURL();
200 String mediaType = MediaType.APPLICATION_XML;
201 final String entity = WRONG_XML_SCHEMA_DATA;
202 int statusCode = submitRequest(method, url, mediaType, entity);
204 // Check the status code of the response: does it match
205 // the expected response(s)?
206 if(logger.isDebugEnabled()){
207 logger.debug(testName + ": url=" + url +
208 " status=" + statusCode);
210 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
211 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
212 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
216 // ---------------------------------------------------------------
217 // CRUD tests : READ tests
218 // ---------------------------------------------------------------
221 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
222 dependsOnMethods = {"create"})
223 public void read(String testName) throws Exception {
228 // Submit the request to the service and store the response.
229 ClientResponse<MultipartInput> res = client.read(knownResourceId);
230 int statusCode = res.getStatus();
232 // Check the status code of the response: does it match
233 // the expected response(s)?
234 if(logger.isDebugEnabled()){
235 logger.debug(testName + ": status = " + statusCode);
237 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
238 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
239 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
241 MultipartInput input = (MultipartInput) res.getEntity();
242 IntakesCommon intake = (IntakesCommon) extractPart(input,
243 client.getCommonPartName(), IntakesCommon.class);
244 Assert.assertNotNull(intake);
249 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
250 dependsOnMethods = {"read"})
251 public void readNonExistent(String testName) throws Exception {
254 setupReadNonExistent(testName);
256 // Submit the request to the service and store the response.
257 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
258 int statusCode = res.getStatus();
260 // Check the status code of the response: does it match
261 // the expected response(s)?
262 if(logger.isDebugEnabled()){
263 logger.debug(testName + ": status = " + statusCode);
265 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
266 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
267 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
270 // ---------------------------------------------------------------
271 // CRUD tests : READ_LIST tests
272 // ---------------------------------------------------------------
275 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
276 dependsOnMethods = {"read"})
277 public void readList(String testName) throws Exception {
280 setupReadList(testName);
282 // Submit the request to the service and store the response.
283 ClientResponse<IntakesCommonList> res = client.readList();
284 IntakesCommonList list = res.getEntity();
285 int statusCode = res.getStatus();
287 // Check the status code of the response: does it match
288 // the expected response(s)?
289 if(logger.isDebugEnabled()){
290 logger.debug(testName + ": status = " + statusCode);
292 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
293 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
294 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
296 // Optionally output additional data about list members for debugging.
297 boolean iterateThroughList = false;
298 if(iterateThroughList && logger.isDebugEnabled()){
299 List<IntakesCommonList.IntakeListItem> items =
300 list.getIntakeListItem();
302 for(IntakesCommonList.IntakeListItem item : items){
303 logger.debug(testName + ": list-item[" + i + "] csid=" +
305 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
306 item.getEntryNumber());
307 logger.debug(testName + ": list-item[" + i + "] URI=" +
317 // ---------------------------------------------------------------
318 // CRUD tests : UPDATE tests
319 // ---------------------------------------------------------------
322 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
323 dependsOnMethods = {"read"})
324 public void update(String testName) throws Exception {
327 setupUpdate(testName);
329 ClientResponse<MultipartInput> res =
330 client.read(knownResourceId);
331 if(logger.isDebugEnabled()){
332 logger.debug(testName + ": read status = " + res.getStatus());
334 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
336 if(logger.isDebugEnabled()){
337 logger.debug("got object to update with ID: " + knownResourceId);
339 MultipartInput input = (MultipartInput) res.getEntity();
340 IntakesCommon intake = (IntakesCommon) extractPart(input,
341 client.getCommonPartName(), IntakesCommon.class);
342 Assert.assertNotNull(intake);
344 // Update the content of this resource.
345 // Update the content of this resource.
346 intake.setEntryNumber("updated-" + intake.getEntryNumber());
347 intake.setEntryDate("updated-" + intake.getEntryDate());
348 if(logger.isDebugEnabled()){
349 logger.debug("to be updated object");
350 logger.debug(objectAsXmlString(intake, IntakesCommon.class));
352 // Submit the request to the service and store the response.
353 MultipartOutput output = new MultipartOutput();
354 OutputPart commonPart = output.addPart(intake, MediaType.APPLICATION_XML_TYPE);
355 commonPart.getHeaders().add("label", client.getCommonPartName());
357 res = client.update(knownResourceId, output);
358 int statusCode = res.getStatus();
359 // Check the status code of the response: does it match the expected response(s)?
360 if(logger.isDebugEnabled()){
361 logger.debug(testName + ": status = " + statusCode);
363 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
364 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
365 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
368 input = (MultipartInput) res.getEntity();
369 IntakesCommon updatedIntake =
370 (IntakesCommon) extractPart(input,
371 client.getCommonPartName(), IntakesCommon.class);
372 Assert.assertNotNull(updatedIntake);
374 Assert.assertEquals(updatedIntake.getEntryDate(),
375 intake.getEntryDate(),
376 "Data in updated object did not match submitted data.");
381 // Placeholders until the three tests below can be uncommented.
382 // See Issue CSPACE-401.
384 public void updateWithEmptyEntityBody(String testName) throws Exception{
387 public void updateWithMalformedXml(String testName) throws Exception {
390 public void updateWithWrongXmlSchema(String testName) throws Exception {
395 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
396 dependsOnMethods = {"create", "update", "testSubmitRequest"})
397 public void updateWithEmptyEntityBody(String testName) throws Exception {
400 setupUpdateWithEmptyEntityBody(testName);
402 // Submit the request to the service and store the response.
403 String method = REQUEST_TYPE.httpMethodName();
404 String url = getResourceURL(knownResourceId);
405 String mediaType = MediaType.APPLICATION_XML;
406 final String entity = "";
407 int statusCode = submitRequest(method, url, mediaType, entity);
409 // Check the status code of the response: does it match
410 // the expected response(s)?
411 if(logger.isDebugEnabled()){
412 logger.debug(testName + ": url=" + url +
413 " status=" + statusCode);
415 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
416 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
417 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
421 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
422 dependsOnMethods = {"create", "update", "testSubmitRequest"})
423 public void updateWithMalformedXml(String testName) throws Exception {
426 setupUpdateWithMalformedXml(testName);
428 // Submit the request to the service and store the response.
429 String method = REQUEST_TYPE.httpMethodName();
430 String url = getResourceURL(knownResourceId);
431 String mediaType = MediaType.APPLICATION_XML;
432 final String entity = MALFORMED_XML_DATA;
433 int statusCode = submitRequest(method, url, mediaType, entity);
435 // Check the status code of the response: does it match
436 // the expected response(s)?
437 if(logger.isDebugEnabled()){
438 logger.debug(testName + ": url=" + url +
439 " status=" + statusCode);
441 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
442 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
443 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
447 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
448 dependsOnMethods = {"create", "update", "testSubmitRequest"})
449 public void updateWithWrongXmlSchema(String testName) throws Exception {
452 setupUpdateWithWrongXmlSchema(testName);
454 // Submit the request to the service and store the response.
455 String method = REQUEST_TYPE.httpMethodName();
456 String url = getResourceURL(knownResourceId);
457 String mediaType = MediaType.APPLICATION_XML;
458 final String entity = WRONG_XML_SCHEMA_DATA;
459 int statusCode = submitRequest(method, url, mediaType, entity);
461 // Check the status code of the response: does it match
462 // the expected response(s)?
463 if(logger.isDebugEnabled()){
464 logger.debug(testName + ": url=" + url +
465 " status=" + statusCode);
467 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
468 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
469 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
474 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
475 dependsOnMethods = {"update", "testSubmitRequest"})
476 public void updateNonExistent(String testName) throws Exception {
479 setupUpdateNonExistent(testName);
481 // Submit the request to the service and store the response.
482 // Note: The ID used in this 'create' call may be arbitrary.
483 // The only relevant ID may be the one used in update(), below.
485 // The only relevant ID may be the one used in update(), below.
486 MultipartOutput multipart = createIntakeInstance(NON_EXISTENT_ID);
487 ClientResponse<MultipartInput> res =
488 client.update(NON_EXISTENT_ID, multipart);
489 int statusCode = res.getStatus();
491 // Check the status code of the response: does it match
492 // the expected response(s)?
493 if(logger.isDebugEnabled()){
494 logger.debug(testName + ": status = " + statusCode);
496 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
497 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
498 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
501 // ---------------------------------------------------------------
502 // CRUD tests : DELETE tests
503 // ---------------------------------------------------------------
506 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
507 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
508 public void delete(String testName) throws Exception {
511 setupDelete(testName);
513 // Submit the request to the service and store the response.
514 ClientResponse<Response> res = client.delete(knownResourceId);
515 int statusCode = res.getStatus();
517 // Check the status code of the response: does it match
518 // the expected response(s)?
519 if(logger.isDebugEnabled()){
520 logger.debug(testName + ": status = " + statusCode);
522 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
523 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
524 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
529 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
530 dependsOnMethods = {"delete"})
531 public void deleteNonExistent(String testName) throws Exception {
534 setupDeleteNonExistent(testName);
536 // Submit the request to the service and store the response.
537 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
538 int statusCode = res.getStatus();
540 // Check the status code of the response: does it match
541 // the expected response(s)?
542 if(logger.isDebugEnabled()){
543 logger.debug(testName + ": status = " + statusCode);
545 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
546 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
547 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
550 // ---------------------------------------------------------------
551 // Utility tests : tests of code used in tests above
552 // ---------------------------------------------------------------
554 * Tests the code for manually submitting data that is used by several
555 * of the methods above.
557 @Test(dependsOnMethods = {"create", "read"})
558 public void testSubmitRequest() {
560 // Expected status code: 200 OK
561 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
563 // Submit the request to the service and store the response.
564 String method = ServiceRequestType.READ.httpMethodName();
565 String url = getResourceURL(knownResourceId);
566 int statusCode = submitRequest(method, url);
568 // Check the status code of the response: does it match
569 // the expected response(s)?
570 if(logger.isDebugEnabled()){
571 logger.debug("testSubmitRequest: url=" + url +
572 " status=" + statusCode);
574 Assert.assertEquals(statusCode, EXPECTED_STATUS);
578 // ---------------------------------------------------------------
579 // Cleanup of resources created during testing
580 // ---------------------------------------------------------------
583 * Deletes any additional resources created by tests,
584 * after all tests have been run.
587 public void cleanUp() {
588 if (logger.isDebugEnabled()) {
589 logger.debug("Cleaning up temporary resources created for testing ...");
591 for (String resourceId : additionalResourceIds) {
592 ClientResponse<Response> res = client.delete(resourceId);
596 // ---------------------------------------------------------------
597 // Utility methods used by tests above
598 // ---------------------------------------------------------------
600 public String getServicePathComponent() {
601 return SERVICE_PATH_COMPONENT;
604 private MultipartOutput createIntakeInstance(String identifier) {
605 return createIntakeInstance(
606 "entryNumber-" + identifier,
607 "entryDate-" + identifier);
610 private MultipartOutput createIntakeInstance(String entryNumber, String entryDate) {
611 IntakesCommon intake = new IntakesCommon();
612 intake.setEntryNumber(entryNumber);
613 intake.setEntryDate(entryDate);
614 MultipartOutput multipart = new MultipartOutput();
615 OutputPart commonPart =
616 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
617 commonPart.getHeaders().add("label", client.getCommonPartName());
619 if(logger.isDebugEnabled()){
620 logger.debug("to be created, intake common");
621 logger.debug(objectAsXmlString(intake, IntakesCommon.class));