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.LoaninClient;
31 import org.collectionspace.services.loanin.LoansinCommon;
32 import org.collectionspace.services.loanin.LoansinCommonList;
33 import org.collectionspace.services.loanin.LenderList;
36 import org.jboss.resteasy.client.ClientResponse;
38 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
39 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
40 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
41 import org.testng.Assert;
42 import org.testng.annotations.AfterClass;
43 import org.testng.annotations.Test;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * LoaninServiceTest, carries out tests against a
50 * deployed and running Loanin (aka Loans In) Service.
52 * $LastChangedRevision: 1327 $
53 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
55 public class LoaninServiceTest extends AbstractServiceTestImpl {
57 private final Logger logger =
58 LoggerFactory.getLogger(LoaninServiceTest.class);
60 // Instance variables specific to this test.
61 final String SERVICE_PATH_COMPONENT = "loansin";
62 private String knownResourceId = null;
63 private List<String> allResourceIdsCreated = new ArrayList();
65 // ---------------------------------------------------------------
66 // CRUD tests : CREATE tests
67 // ---------------------------------------------------------------
70 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
71 public void create(String testName) throws Exception {
73 // Perform setup, such as initializing the type of service request
74 // (e.g. CREATE, DELETE), its valid and expected status codes, and
75 // its associated HTTP method name (e.g. POST, DELETE).
76 setupCreate(testName);
78 // Submit the request to the service and store the response.
79 LoaninClient client = new LoaninClient();
80 String identifier = createIdentifier();
81 MultipartOutput multipart = createLoaninInstance(identifier);
82 ClientResponse<Response> res = client.create(multipart);
84 int statusCode = res.getStatus();
86 // Check the status code of the response: does it match
87 // the expected response(s)?
90 // Does it fall within the set of valid status codes?
91 // Does it exactly match the expected status code?
92 if(logger.isDebugEnabled()){
93 logger.debug(testName + ": status = " + statusCode);
95 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
96 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
97 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
99 // Store the ID returned from the first resource created
100 // for additional tests below.
101 if (knownResourceId == null){
102 knownResourceId = extractId(res);
103 if (logger.isDebugEnabled()) {
104 logger.debug(testName + ": knownResourceId=" + knownResourceId);
108 // Store the IDs from every resource created by tests,
109 // so they can be deleted after tests have been run.
110 allResourceIdsCreated.add(extractId(res));
114 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
115 dependsOnMethods = {"create"})
116 public void createList(String testName) throws Exception {
117 for(int i = 0; i < 3; i++){
123 // Placeholders until the three tests below can be uncommented.
124 // See Issue CSPACE-401.
126 public void createWithEmptyEntityBody(String testName) throws Exception {
130 public void createWithMalformedXml(String testName) throws Exception {
134 public void createWithWrongXmlSchema(String testName) throws Exception {
139 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
140 dependsOnMethods = {"create", "testSubmitRequest"})
141 public void createWithEmptyEntityBody(String testName) throws Exception {
144 setupCreateWithEmptyEntityBody(testName);
146 // Submit the request to the service and store the response.
147 String method = REQUEST_TYPE.httpMethodName();
148 String url = getServiceRootURL();
149 String mediaType = MediaType.APPLICATION_XML;
150 final String entity = "";
151 int statusCode = submitRequest(method, url, mediaType, entity);
153 // Check the status code of the response: does it match
154 // the expected response(s)?
155 if(logger.isDebugEnabled()){
156 logger.debug("createWithEmptyEntityBody url=" + url +
157 " status=" + statusCode);
159 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
160 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
161 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
165 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
166 dependsOnMethods = {"create", "testSubmitRequest"})
167 public void createWithMalformedXml(String testName) throws Exception {
170 setupCreateWithMalformedXml(testName);
172 // Submit the request to the service and store the response.
173 String method = REQUEST_TYPE.httpMethodName();
174 String url = getServiceRootURL();
175 String mediaType = MediaType.APPLICATION_XML;
176 final String entity = MALFORMED_XML_DATA; // Constant from base class.
177 int statusCode = submitRequest(method, url, mediaType, entity);
179 // Check the status code of the response: does it match
180 // the expected response(s)?
181 if(logger.isDebugEnabled()){
182 logger.debug(testName + ": url=" + url +
183 " status=" + statusCode);
185 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
186 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
187 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
191 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
192 dependsOnMethods = {"create", "testSubmitRequest"})
193 public void createWithWrongXmlSchema(String testName) throws Exception {
196 setupCreateWithWrongXmlSchema(testName);
198 // Submit the request to the service and store the response.
199 String method = REQUEST_TYPE.httpMethodName();
200 String url = getServiceRootURL();
201 String mediaType = MediaType.APPLICATION_XML;
202 final String entity = WRONG_XML_SCHEMA_DATA;
203 int statusCode = submitRequest(method, url, mediaType, entity);
205 // Check the status code of the response: does it match
206 // the expected response(s)?
207 if(logger.isDebugEnabled()){
208 logger.debug(testName + ": url=" + url +
209 " status=" + statusCode);
211 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
212 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
213 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
217 // ---------------------------------------------------------------
218 // CRUD tests : READ tests
219 // ---------------------------------------------------------------
222 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
223 dependsOnMethods = {"create"})
224 public void read(String testName) throws Exception {
229 // Submit the request to the service and store the response.
230 LoaninClient client = new LoaninClient();
231 ClientResponse<MultipartInput> res = client.read(knownResourceId);
232 int statusCode = res.getStatus();
234 // Check the status code of the response: does it match
235 // the expected response(s)?
236 if(logger.isDebugEnabled()){
237 logger.debug(testName + ": status = " + statusCode);
239 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
240 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
241 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
243 MultipartInput input = (MultipartInput) res.getEntity();
244 LoansinCommon loanin = (LoansinCommon) extractPart(input,
245 client.getCommonPartName(), LoansinCommon.class);
246 Assert.assertNotNull(loanin);
251 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
252 dependsOnMethods = {"read"})
253 public void readNonExistent(String testName) throws Exception {
256 setupReadNonExistent(testName);
258 // Submit the request to the service and store the response.
259 LoaninClient client = new LoaninClient();
260 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
261 int statusCode = res.getStatus();
263 // Check the status code of the response: does it match
264 // the expected response(s)?
265 if(logger.isDebugEnabled()){
266 logger.debug(testName + ": status = " + statusCode);
268 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
269 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
270 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
273 // ---------------------------------------------------------------
274 // CRUD tests : READ_LIST tests
275 // ---------------------------------------------------------------
278 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
279 dependsOnMethods = {"createList", "read"})
280 public void readList(String testName) throws Exception {
283 setupReadList(testName);
285 // Submit the request to the service and store the response.
286 LoaninClient client = new LoaninClient();
287 ClientResponse<LoansinCommonList> res = client.readList();
288 LoansinCommonList list = res.getEntity();
289 int statusCode = res.getStatus();
291 // Check the status code of the response: does it match
292 // the expected response(s)?
293 if(logger.isDebugEnabled()){
294 logger.debug(testName + ": status = " + statusCode);
296 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
297 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
298 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
300 // Optionally output additional data about list members for debugging.
301 boolean iterateThroughList = false;
302 if(iterateThroughList && logger.isDebugEnabled()){
303 List<LoansinCommonList.LoaninListItem> items =
304 list.getLoaninListItem();
306 for(LoansinCommonList.LoaninListItem item : items){
307 logger.debug(testName + ": list-item[" + i + "] csid=" +
309 logger.debug(testName + ": list-item[" + i + "] loanInNumber=" +
310 item.getLoanInNumber());
311 logger.debug(testName + ": list-item[" + i + "] URI=" +
321 // ---------------------------------------------------------------
322 // CRUD tests : UPDATE tests
323 // ---------------------------------------------------------------
326 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
327 dependsOnMethods = {"read"})
328 public void update(String testName) throws Exception {
331 setupUpdate(testName);
333 // Retrieve the contents of a resource to update.
334 LoaninClient client = new LoaninClient();
335 ClientResponse<MultipartInput> res =
336 client.read(knownResourceId);
337 if(logger.isDebugEnabled()){
338 logger.debug(testName + ": read status = " + res.getStatus());
340 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
342 if(logger.isDebugEnabled()){
343 logger.debug("got object to update with ID: " + knownResourceId);
345 MultipartInput input = (MultipartInput) res.getEntity();
346 LoansinCommon loanin = (LoansinCommon) extractPart(input,
347 client.getCommonPartName(), LoansinCommon.class);
348 Assert.assertNotNull(loanin);
350 // Update the content of this resource.
351 loanin.setLoanInNumber("updated-" + loanin.getLoanInNumber());
352 loanin.setLoanReturnDate("updated-" + loanin.getLoanReturnDate());
353 if(logger.isDebugEnabled()){
354 logger.debug("to be updated object");
355 logger.debug(objectAsXmlString(loanin, LoansinCommon.class));
357 // Submit the request to the service and store the response.
358 MultipartOutput output = new MultipartOutput();
359 OutputPart commonPart = output.addPart(loanin, MediaType.APPLICATION_XML_TYPE);
360 commonPart.getHeaders().add("label", client.getCommonPartName());
362 res = client.update(knownResourceId, output);
363 int statusCode = res.getStatus();
364 // Check the status code of the response: does it match the expected response(s)?
365 if(logger.isDebugEnabled()){
366 logger.debug(testName + ": status = " + statusCode);
368 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
369 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
370 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
373 input = (MultipartInput) res.getEntity();
374 LoansinCommon updatedLoanin =
375 (LoansinCommon) extractPart(input,
376 client.getCommonPartName(), LoansinCommon.class);
377 Assert.assertNotNull(updatedLoanin);
379 Assert.assertEquals(updatedLoanin.getLoanReturnDate(),
380 loanin.getLoanReturnDate(),
381 "Data in updated object did not match submitted data.");
386 // Placeholders until the three tests below can be uncommented.
387 // See Issue CSPACE-401.
389 public void updateWithEmptyEntityBody(String testName) throws Exception{
392 public void updateWithMalformedXml(String testName) throws Exception {
395 public void updateWithWrongXmlSchema(String testName) throws Exception {
400 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
401 dependsOnMethods = {"create", "update", "testSubmitRequest"})
402 public void updateWithEmptyEntityBody(String testName) throws Exception {
405 setupUpdateWithEmptyEntityBody(testName);
407 // Submit the request to the service and store the response.
408 String method = REQUEST_TYPE.httpMethodName();
409 String url = getResourceURL(knownResourceId);
410 String mediaType = MediaType.APPLICATION_XML;
411 final String entity = "";
412 int statusCode = submitRequest(method, url, mediaType, entity);
414 // Check the status code of the response: does it match
415 // the expected response(s)?
416 if(logger.isDebugEnabled()){
417 logger.debug(testName + ": url=" + url +
418 " status=" + statusCode);
420 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
421 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
422 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
426 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
427 dependsOnMethods = {"create", "update", "testSubmitRequest"})
428 public void updateWithMalformedXml(String testName) throws Exception {
431 setupUpdateWithMalformedXml(testName);
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 = MALFORMED_XML_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(testName + ": url=" + url +
444 " status=" + statusCode);
446 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
447 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
448 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
452 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
453 dependsOnMethods = {"create", "update", "testSubmitRequest"})
454 public void updateWithWrongXmlSchema(String testName) throws Exception {
457 setupUpdateWithWrongXmlSchema(testName);
459 // Submit the request to the service and store the response.
460 String method = REQUEST_TYPE.httpMethodName();
461 String url = getResourceURL(knownResourceId);
462 String mediaType = MediaType.APPLICATION_XML;
463 final String entity = WRONG_XML_SCHEMA_DATA;
464 int statusCode = submitRequest(method, url, mediaType, entity);
466 // Check the status code of the response: does it match
467 // the expected response(s)?
468 if(logger.isDebugEnabled()){
469 logger.debug(testName + ": url=" + url +
470 " status=" + statusCode);
472 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
473 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
474 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
479 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
480 dependsOnMethods = {"update", "testSubmitRequest"})
481 public void updateNonExistent(String testName) throws Exception {
484 setupUpdateNonExistent(testName);
486 // Submit the request to the service and store the response.
487 // Note: The ID used in this 'create' call may be arbitrary.
488 // The only relevant ID may be the one used in update(), below.
489 LoaninClient client = new LoaninClient();
490 MultipartOutput multipart = createLoaninInstance(NON_EXISTENT_ID);
491 ClientResponse<MultipartInput> res =
492 client.update(NON_EXISTENT_ID, multipart);
493 int statusCode = res.getStatus();
495 // Check the status code of the response: does it match
496 // the expected response(s)?
497 if(logger.isDebugEnabled()){
498 logger.debug(testName + ": status = " + statusCode);
500 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
501 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
502 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
505 // ---------------------------------------------------------------
506 // CRUD tests : DELETE tests
507 // ---------------------------------------------------------------
510 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
511 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
512 public void delete(String testName) throws Exception {
515 setupDelete(testName);
517 // Submit the request to the service and store the response.
518 LoaninClient client = new LoaninClient();
519 ClientResponse<Response> res = client.delete(knownResourceId);
520 int statusCode = res.getStatus();
522 // Check the status code of the response: does it match
523 // the expected response(s)?
524 if(logger.isDebugEnabled()){
525 logger.debug(testName + ": status = " + statusCode);
527 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
528 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
529 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
534 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
535 dependsOnMethods = {"delete"})
536 public void deleteNonExistent(String testName) throws Exception {
539 setupDeleteNonExistent(testName);
541 // Submit the request to the service and store the response.
542 LoaninClient client = new LoaninClient();
543 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
544 int statusCode = res.getStatus();
546 // Check the status code of the response: does it match
547 // the expected response(s)?
548 if(logger.isDebugEnabled()){
549 logger.debug(testName + ": status = " + statusCode);
551 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
552 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
553 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
556 // ---------------------------------------------------------------
557 // Utility tests : tests of code used in tests above
558 // ---------------------------------------------------------------
560 * Tests the code for manually submitting data that is used by several
561 * of the methods above.
563 @Test(dependsOnMethods = {"create", "read"})
564 public void testSubmitRequest() {
566 // Expected status code: 200 OK
567 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
569 // Submit the request to the service and store the response.
570 String method = ServiceRequestType.READ.httpMethodName();
571 String url = getResourceURL(knownResourceId);
572 int statusCode = submitRequest(method, url);
574 // Check the status code of the response: does it match
575 // the expected response(s)?
576 if(logger.isDebugEnabled()){
577 logger.debug("testSubmitRequest: url=" + url +
578 " status=" + statusCode);
580 Assert.assertEquals(statusCode, EXPECTED_STATUS);
584 // ---------------------------------------------------------------
585 // Cleanup of resources created during testing
586 // ---------------------------------------------------------------
589 * Deletes all resources created by tests, after all tests have been run.
591 * This cleanup method will always be run, even if one or more tests fail.
592 * For this reason, it attempts to remove all resources created
593 * at any point during testing, even if some of those resources
594 * may be expected to be deleted by certain tests.
596 @AfterClass(alwaysRun=true)
597 public void cleanUp() {
598 if (logger.isDebugEnabled()) {
599 logger.debug("Cleaning up temporary resources created for testing ...");
601 LoaninClient client = new LoaninClient();
602 for (String resourceId : allResourceIdsCreated) {
603 // Note: Any non-success responses are ignored and not reported.
604 ClientResponse<Response> res = client.delete(resourceId);
608 // ---------------------------------------------------------------
609 // Utility methods used by tests above
610 // ---------------------------------------------------------------
612 public String getServicePathComponent() {
613 return SERVICE_PATH_COMPONENT;
616 private MultipartOutput createLoaninInstance(String identifier) {
617 return createLoaninInstance(
618 "entryNumber-" + identifier,
619 "entryDate-" + identifier);
622 private MultipartOutput createLoaninInstance(String loanInNumber,
624 LoansinCommon loanin = new LoansinCommon();
625 loanin.setLoanInNumber(loanInNumber);
626 loanin.setLoanReturnDate(returnDate);
627 LenderList lenderList = new LenderList();
628 lenderList.getLender().add(
629 "urn:cspace:org.collectionspace.demo:personauthority:name(TestPersonAuth):person:name(Harry Lender)'Harry Lender'");
630 lenderList.getLender().add(
631 "urn:cspace:org.collectionspace.demo:personauthority:name(TestPersonAuth):person:name(Sally Lender)'Sally Lender'");
632 loanin.setLenders(lenderList);
633 loanin.setLoanPurpose("For Surfboards of the 1960s exhibition.");
634 MultipartOutput multipart = new MultipartOutput();
635 OutputPart commonPart =
636 multipart.addPart(loanin, MediaType.APPLICATION_XML_TYPE);
637 commonPart.getHeaders().add("label", new LoaninClient().getCommonPartName());
639 if(logger.isDebugEnabled()){
640 logger.debug("to be created, loanin common");
641 logger.debug(objectAsXmlString(loanin, LoansinCommon.class));