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 AbstractServiceTestImpl {
55 private final Logger logger =
56 LoggerFactory.getLogger(IntakeServiceTest.class);
58 // Instance variables specific to this test.
59 final String SERVICE_PATH_COMPONENT = "intakes";
60 private String knownResourceId = null;
61 private List<String> allResourceIdsCreated = new ArrayList();
63 // ---------------------------------------------------------------
64 // CRUD tests : CREATE tests
65 // ---------------------------------------------------------------
68 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
69 public void create(String testName) throws Exception {
71 // Perform setup, such as initializing the type of service request
72 // (e.g. CREATE, DELETE), its valid and expected status codes, and
73 // its associated HTTP method name (e.g. POST, DELETE).
74 setupCreate(testName);
76 // Submit the request to the service and store the response.
77 IntakeClient client = new IntakeClient();
78 String identifier = createIdentifier();
79 MultipartOutput multipart = createIntakeInstance(identifier);
80 ClientResponse<Response> res = client.create(multipart);
82 int statusCode = res.getStatus();
84 // Check the status code of the response: does it match
85 // the expected response(s)?
88 // Does it fall within the set of valid status codes?
89 // Does it exactly match the expected status code?
90 if(logger.isDebugEnabled()){
91 logger.debug(testName + ": status = " + statusCode);
93 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
94 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
95 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
97 // Store the ID returned from the first resource created
98 // for additional tests below.
99 if (knownResourceId == null){
100 knownResourceId = extractId(res);
101 if (logger.isDebugEnabled()) {
102 logger.debug(testName + ": knownResourceId=" + knownResourceId);
106 // Store the IDs from every resource created by tests,
107 // so they can be deleted after tests have been run.
108 allResourceIdsCreated.add(extractId(res));
112 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
113 dependsOnMethods = {"create"})
114 public void createList(String testName) throws Exception {
115 for(int i = 0; i < 3; i++){
121 // Placeholders until the three tests below can be uncommented.
122 // See Issue CSPACE-401.
124 public void createWithEmptyEntityBody(String testName) throws Exception {
128 public void createWithMalformedXml(String testName) throws Exception {
132 public void createWithWrongXmlSchema(String testName) throws Exception {
137 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
138 dependsOnMethods = {"create", "testSubmitRequest"})
139 public void createWithEmptyEntityBody(String testName) throws Exception {
142 setupCreateWithEmptyEntityBody(testName);
144 // Submit the request to the service and store the response.
145 String method = REQUEST_TYPE.httpMethodName();
146 String url = getServiceRootURL();
147 String mediaType = MediaType.APPLICATION_XML;
148 final String entity = "";
149 int statusCode = submitRequest(method, url, mediaType, entity);
151 // Check the status code of the response: does it match
152 // the expected response(s)?
153 if(logger.isDebugEnabled()){
154 logger.debug("createWithEmptyEntityBody url=" + url +
155 " status=" + statusCode);
157 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
158 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
159 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
163 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
164 dependsOnMethods = {"create", "testSubmitRequest"})
165 public void createWithMalformedXml(String testName) throws Exception {
168 setupCreateWithMalformedXml(testName);
170 // Submit the request to the service and store the response.
171 String method = REQUEST_TYPE.httpMethodName();
172 String url = getServiceRootURL();
173 String mediaType = MediaType.APPLICATION_XML;
174 final String entity = MALFORMED_XML_DATA; // Constant from base class.
175 int statusCode = submitRequest(method, url, mediaType, entity);
177 // Check the status code of the response: does it match
178 // the expected response(s)?
179 if(logger.isDebugEnabled()){
180 logger.debug(testName + ": url=" + url +
181 " status=" + statusCode);
183 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
184 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
185 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
189 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
190 dependsOnMethods = {"create", "testSubmitRequest"})
191 public void createWithWrongXmlSchema(String testName) throws Exception {
194 setupCreateWithWrongXmlSchema(testName);
196 // Submit the request to the service and store the response.
197 String method = REQUEST_TYPE.httpMethodName();
198 String url = getServiceRootURL();
199 String mediaType = MediaType.APPLICATION_XML;
200 final String entity = WRONG_XML_SCHEMA_DATA;
201 int statusCode = submitRequest(method, url, mediaType, entity);
203 // Check the status code of the response: does it match
204 // the expected response(s)?
205 if(logger.isDebugEnabled()){
206 logger.debug(testName + ": url=" + url +
207 " status=" + statusCode);
209 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
210 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
211 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
215 // ---------------------------------------------------------------
216 // CRUD tests : READ tests
217 // ---------------------------------------------------------------
220 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
221 dependsOnMethods = {"create"})
222 public void read(String testName) throws Exception {
227 // Submit the request to the service and store the response.
228 IntakeClient client = new IntakeClient();
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=AbstractServiceTestImpl.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 IntakeClient client = new IntakeClient();
258 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
259 int statusCode = res.getStatus();
261 // Check the status code of the response: does it match
262 // the expected response(s)?
263 if(logger.isDebugEnabled()){
264 logger.debug(testName + ": status = " + statusCode);
266 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
267 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
268 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
271 // ---------------------------------------------------------------
272 // CRUD tests : READ_LIST tests
273 // ---------------------------------------------------------------
276 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
277 dependsOnMethods = {"createList", "read"})
278 public void readList(String testName) throws Exception {
281 setupReadList(testName);
283 // Submit the request to the service and store the response.
284 IntakeClient client = new IntakeClient();
285 ClientResponse<IntakesCommonList> res = client.readList();
286 IntakesCommonList list = res.getEntity();
287 int statusCode = res.getStatus();
289 // Check the status code of the response: does it match
290 // the expected response(s)?
291 if(logger.isDebugEnabled()){
292 logger.debug(testName + ": status = " + statusCode);
294 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
295 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
296 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
298 // Optionally output additional data about list members for debugging.
299 boolean iterateThroughList = false;
300 if(iterateThroughList && logger.isDebugEnabled()){
301 List<IntakesCommonList.IntakeListItem> items =
302 list.getIntakeListItem();
304 for(IntakesCommonList.IntakeListItem item : items){
305 logger.debug(testName + ": list-item[" + i + "] csid=" +
307 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
308 item.getEntryNumber());
309 logger.debug(testName + ": list-item[" + i + "] URI=" +
319 // ---------------------------------------------------------------
320 // CRUD tests : UPDATE tests
321 // ---------------------------------------------------------------
324 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
325 dependsOnMethods = {"read"})
326 public void update(String testName) throws Exception {
329 setupUpdate(testName);
331 // Retrieve the contents of a resource to update.
332 IntakeClient client = new IntakeClient();
333 ClientResponse<MultipartInput> res =
334 client.read(knownResourceId);
335 if(logger.isDebugEnabled()){
336 logger.debug(testName + ": read status = " + res.getStatus());
338 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
340 if(logger.isDebugEnabled()){
341 logger.debug("got object to update with ID: " + knownResourceId);
343 MultipartInput input = (MultipartInput) res.getEntity();
344 IntakesCommon intake = (IntakesCommon) extractPart(input,
345 client.getCommonPartName(), IntakesCommon.class);
346 Assert.assertNotNull(intake);
348 // Update the content of this resource.
349 // Update the content of this resource.
350 intake.setEntryNumber("updated-" + intake.getEntryNumber());
351 intake.setEntryDate("updated-" + intake.getEntryDate());
352 if(logger.isDebugEnabled()){
353 logger.debug("to be updated object");
354 logger.debug(objectAsXmlString(intake, IntakesCommon.class));
356 // Submit the request to the service and store the response.
357 MultipartOutput output = new MultipartOutput();
358 OutputPart commonPart = output.addPart(intake, MediaType.APPLICATION_XML_TYPE);
359 commonPart.getHeaders().add("label", client.getCommonPartName());
361 res = client.update(knownResourceId, output);
362 int statusCode = res.getStatus();
363 // Check the status code of the response: does it match the expected response(s)?
364 if(logger.isDebugEnabled()){
365 logger.debug(testName + ": status = " + statusCode);
367 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
368 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
369 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
372 input = (MultipartInput) res.getEntity();
373 IntakesCommon updatedIntake =
374 (IntakesCommon) extractPart(input,
375 client.getCommonPartName(), IntakesCommon.class);
376 Assert.assertNotNull(updatedIntake);
378 Assert.assertEquals(updatedIntake.getEntryDate(),
379 intake.getEntryDate(),
380 "Data in updated object did not match submitted data.");
385 // Placeholders until the three tests below can be uncommented.
386 // See Issue CSPACE-401.
388 public void updateWithEmptyEntityBody(String testName) throws Exception{
391 public void updateWithMalformedXml(String testName) throws Exception {
394 public void updateWithWrongXmlSchema(String testName) throws Exception {
399 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
400 dependsOnMethods = {"create", "update", "testSubmitRequest"})
401 public void updateWithEmptyEntityBody(String testName) throws Exception {
404 setupUpdateWithEmptyEntityBody(testName);
406 // Submit the request to the service and store the response.
407 String method = REQUEST_TYPE.httpMethodName();
408 String url = getResourceURL(knownResourceId);
409 String mediaType = MediaType.APPLICATION_XML;
410 final String entity = "";
411 int statusCode = submitRequest(method, url, mediaType, entity);
413 // Check the status code of the response: does it match
414 // the expected response(s)?
415 if(logger.isDebugEnabled()){
416 logger.debug(testName + ": url=" + url +
417 " status=" + statusCode);
419 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
420 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
421 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
425 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
426 dependsOnMethods = {"create", "update", "testSubmitRequest"})
427 public void updateWithMalformedXml(String testName) throws Exception {
430 setupUpdateWithMalformedXml(testName);
432 // Submit the request to the service and store the response.
433 String method = REQUEST_TYPE.httpMethodName();
434 String url = getResourceURL(knownResourceId);
435 String mediaType = MediaType.APPLICATION_XML;
436 final String entity = MALFORMED_XML_DATA;
437 int statusCode = submitRequest(method, url, mediaType, entity);
439 // Check the status code of the response: does it match
440 // the expected response(s)?
441 if(logger.isDebugEnabled()){
442 logger.debug(testName + ": url=" + url +
443 " status=" + statusCode);
445 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
446 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
447 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
451 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
452 dependsOnMethods = {"create", "update", "testSubmitRequest"})
453 public void updateWithWrongXmlSchema(String testName) throws Exception {
456 setupUpdateWithWrongXmlSchema(testName);
458 // Submit the request to the service and store the response.
459 String method = REQUEST_TYPE.httpMethodName();
460 String url = getResourceURL(knownResourceId);
461 String mediaType = MediaType.APPLICATION_XML;
462 final String entity = WRONG_XML_SCHEMA_DATA;
463 int statusCode = submitRequest(method, url, mediaType, entity);
465 // Check the status code of the response: does it match
466 // the expected response(s)?
467 if(logger.isDebugEnabled()){
468 logger.debug(testName + ": url=" + url +
469 " status=" + statusCode);
471 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
472 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
473 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
478 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
479 dependsOnMethods = {"update", "testSubmitRequest"})
480 public void updateNonExistent(String testName) throws Exception {
483 setupUpdateNonExistent(testName);
485 // Submit the request to the service and store the response.
486 // Note: The ID used in this 'create' call may be arbitrary.
487 // The only relevant ID may be the one used in update(), below.
488 IntakeClient client = new IntakeClient();
489 MultipartOutput multipart = createIntakeInstance(NON_EXISTENT_ID);
490 ClientResponse<MultipartInput> res =
491 client.update(NON_EXISTENT_ID, multipart);
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(testName + ": status = " + statusCode);
499 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
500 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
501 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
504 // ---------------------------------------------------------------
505 // CRUD tests : DELETE tests
506 // ---------------------------------------------------------------
509 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
510 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
511 public void delete(String testName) throws Exception {
514 setupDelete(testName);
516 // Submit the request to the service and store the response.
517 IntakeClient client = new IntakeClient();
518 ClientResponse<Response> res = client.delete(knownResourceId);
519 int statusCode = res.getStatus();
521 // Check the status code of the response: does it match
522 // the expected response(s)?
523 if(logger.isDebugEnabled()){
524 logger.debug(testName + ": status = " + statusCode);
526 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
527 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
528 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
533 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
534 dependsOnMethods = {"delete"})
535 public void deleteNonExistent(String testName) throws Exception {
538 setupDeleteNonExistent(testName);
540 // Submit the request to the service and store the response.
541 IntakeClient client = new IntakeClient();
542 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
543 int statusCode = res.getStatus();
545 // Check the status code of the response: does it match
546 // the expected response(s)?
547 if(logger.isDebugEnabled()){
548 logger.debug(testName + ": status = " + statusCode);
550 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
551 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
552 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
555 // ---------------------------------------------------------------
556 // Utility tests : tests of code used in tests above
557 // ---------------------------------------------------------------
559 * Tests the code for manually submitting data that is used by several
560 * of the methods above.
562 @Test(dependsOnMethods = {"create", "read"})
563 public void testSubmitRequest() {
565 // Expected status code: 200 OK
566 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
568 // Submit the request to the service and store the response.
569 String method = ServiceRequestType.READ.httpMethodName();
570 String url = getResourceURL(knownResourceId);
571 int statusCode = submitRequest(method, url);
573 // Check the status code of the response: does it match
574 // the expected response(s)?
575 if(logger.isDebugEnabled()){
576 logger.debug("testSubmitRequest: url=" + url +
577 " status=" + statusCode);
579 Assert.assertEquals(statusCode, EXPECTED_STATUS);
583 // ---------------------------------------------------------------
584 // Cleanup of resources created during testing
585 // ---------------------------------------------------------------
588 * Deletes all resources created by tests, after all tests have been run.
590 * This cleanup method will always be run, even if one or more tests fail.
591 * For this reason, it attempts to remove all resources created
592 * at any point during testing, even if some of those resources
593 * may be expected to be deleted by certain tests.
595 @AfterClass(alwaysRun=true)
596 public void cleanUp() {
597 if (logger.isDebugEnabled()) {
598 logger.debug("Cleaning up temporary resources created for testing ...");
600 IntakeClient client = new IntakeClient();
601 for (String resourceId : allResourceIdsCreated) {
602 // Note: Any non-success responses are ignored and not reported.
603 ClientResponse<Response> res = client.delete(resourceId);
607 // ---------------------------------------------------------------
608 // Utility methods used by tests above
609 // ---------------------------------------------------------------
611 public String getServicePathComponent() {
612 return SERVICE_PATH_COMPONENT;
615 private MultipartOutput createIntakeInstance(String identifier) {
616 return createIntakeInstance(
617 "entryNumber-" + identifier,
618 "entryDate-" + identifier,
619 "depositor-" + identifier);
622 private MultipartOutput createIntakeInstance(String entryNumber,
625 IntakesCommon intake = new IntakesCommon();
626 intake.setEntryNumber(entryNumber);
627 intake.setEntryDate(entryDate);
628 intake.setDepositor(depositor);
629 MultipartOutput multipart = new MultipartOutput();
630 OutputPart commonPart =
631 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
632 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
634 if(logger.isDebugEnabled()){
635 logger.debug("to be created, intake common");
636 logger.debug(objectAsXmlString(intake, IntakesCommon.class));