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;
28 import javax.ws.rs.core.Response;
30 import org.collectionspace.services.client.AbstractCommonListUtils;
31 import org.collectionspace.services.client.CollectionSpaceClient;
32 import org.collectionspace.services.client.LoaninClient;
33 import org.collectionspace.services.client.PayloadInputPart;
34 import org.collectionspace.services.client.PayloadOutputPart;
35 import org.collectionspace.services.client.PoxPayloadIn;
36 import org.collectionspace.services.client.PoxPayloadOut;
37 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
38 import org.collectionspace.services.jaxb.AbstractCommonList;
39 import org.collectionspace.services.loanin.LenderGroup;
40 import org.collectionspace.services.loanin.LenderGroupList;
41 import org.collectionspace.services.loanin.LoansinCommon;
43 import org.testng.Assert;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * LoaninServiceTest, carries out tests against a
49 * deployed and running Loanin (aka Loans In) Service.
51 * $LastChangedRevision$
54 public class LoaninServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, LoansinCommon> {
57 private final String CLASS_NAME = LoaninServiceTest.class.getName();
58 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
59 // Instance variables specific to this test.
60 /** The service path component. */
61 final String SERVICE_NAME = "loansin";
62 final String SERVICE_PATH_COMPONENT = "loansin";
63 private String LENDER_REF_NAME =
64 "urn:cspace:org.collectionspace.demo:personauthorities:name(TestPersonAuth):item:name(HarryLender)'Harry Lender'";
65 private final static String CURRENT_DATE_UTC =
66 GregorianCalendarDateTimeUtils.currentDateUTC();
69 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
72 protected CollectionSpaceClient getClientInstance() {
73 return new LoaninClient();
77 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
78 return new LoaninClient(clientPropertiesFilename);
82 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
85 protected AbstractCommonList getCommonList(Response response) {
86 return response.readEntity(AbstractCommonList.class);
89 // ---------------------------------------------------------------
90 // CRUD tests : CREATE tests
91 // ---------------------------------------------------------------
96 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
99 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
100 public void create(String testName) throws Exception {
101 // Perform setup, such as initializing the type of service request
102 // (e.g. CREATE, DELETE), its valid and expected status codes, and
103 // its associated HTTP method name (e.g. POST, DELETE).
106 // Submit the request to the service and store the response.
107 LoaninClient client = new LoaninClient();
108 String identifier = createIdentifier();
109 PoxPayloadOut multipart = createLoaninInstance(identifier);
111 Response res = client.create(multipart);
113 int statusCode = res.getStatus();
115 // Check the status code of the response: does it match
116 // the expected response(s)?
119 // Does it fall within the set of valid status codes?
120 // Does it exactly match the expected status code?
121 if (logger.isDebugEnabled()) {
122 logger.debug(testName + ": status = " + statusCode);
124 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
125 invalidStatusCodeMessage(testRequestType, statusCode));
126 Assert.assertEquals(statusCode, testExpectedStatusCode);
128 newID = extractId(res);
135 // Store the ID returned from the first resource created
136 // for additional tests below.
137 if (knownResourceId == null) {
138 knownResourceId = newID;
139 if (logger.isDebugEnabled()) {
140 logger.debug(testName + ": knownResourceId=" + knownResourceId);
144 // Store the IDs from every resource created by tests,
145 // so they can be deleted after tests have been run.
146 allResourceIdsCreated.add(newID);
150 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
153 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
154 // dependsOnMethods = {"create"})
155 public void createList(String testName) throws Exception {
156 for (int i = 0; i < 3; i++) {
164 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
165 dependsOnMethods = {"create", "testSubmitRequest"})
166 public void createWithEmptyEntityBody(String testName) throws Exception {
168 if (logger.isDebugEnabled()) {
169 logger.debug(testBanner(testName, CLASS_NAME));
172 setupCreateWithEmptyEntityBody();
174 // Submit the request to the service and store the response.
175 String method = REQUEST_TYPE.httpMethodName();
176 String url = getServiceRootURL();
177 String mediaType = MediaType.APPLICATION_XML;
178 final String entity = "";
179 int statusCode = submitRequest(method, url, mediaType, entity);
181 // Check the status code of the response: does it match
182 // the expected response(s)?
183 if(logger.isDebugEnabled()){
184 logger.debug("createWithEmptyEntityBody url=" + url +
185 " status=" + statusCode);
187 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
188 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
189 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
193 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
194 dependsOnMethods = {"create", "testSubmitRequest"})
195 public void createWithMalformedXml(String testName) throws Exception {
197 if (logger.isDebugEnabled()) {
198 logger.debug(testBanner(testName, CLASS_NAME));
201 setupCreateWithMalformedXml(testName, logger);
203 // Submit the request to the service and store the response.
204 String method = REQUEST_TYPE.httpMethodName();
205 String url = getServiceRootURL();
206 String mediaType = MediaType.APPLICATION_XML;
207 final String entity = MALFORMED_XML_DATA; // Constant from base class.
208 int statusCode = submitRequest(method, url, mediaType, entity);
210 // Check the status code of the response: does it match
211 // the expected response(s)?
212 if(logger.isDebugEnabled()){
213 logger.debug(testName + ": url=" + url +
214 " status=" + statusCode);
216 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
217 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
218 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
222 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
223 dependsOnMethods = {"create", "testSubmitRequest"})
224 public void createWithWrongXmlSchema(String testName) throws Exception {
226 if (logger.isDebugEnabled()) {
227 logger.debug(testBanner(testName, CLASS_NAME));
230 setupCreateWithWrongXmlSchema(testName, logger);
232 // Submit the request to the service and store the response.
233 String method = REQUEST_TYPE.httpMethodName();
234 String url = getServiceRootURL();
235 String mediaType = MediaType.APPLICATION_XML;
236 final String entity = WRONG_XML_SCHEMA_DATA;
237 int statusCode = submitRequest(method, url, mediaType, entity);
239 // Check the status code of the response: does it match
240 // the expected response(s)?
241 if(logger.isDebugEnabled()){
242 logger.debug(testName + ": url=" + url +
243 " status=" + statusCode);
245 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
246 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
247 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
251 // ---------------------------------------------------------------
252 // CRUD tests : READ tests
253 // ---------------------------------------------------------------
258 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
261 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
262 // dependsOnMethods = {"create"})
263 public void read(String testName) throws Exception {
267 // Submit the request to the service and store the response.
268 LoaninClient client = new LoaninClient();
269 Response res = client.read(knownResourceId);
270 PoxPayloadIn input = null;
272 assertStatusCode(res, testName);
273 input = new PoxPayloadIn(res.readEntity(String.class));
280 // Get the common part of the response and verify that it is not null.
281 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
282 LoansinCommon loaninCommon = null;
283 if (payloadInputPart != null) {
284 loaninCommon = (LoansinCommon) payloadInputPart.getBody();
286 Assert.assertNotNull(loaninCommon);
288 // Check selected fields.
289 LenderGroupList lenderGroupList = loaninCommon.getLenderGroupList();
290 Assert.assertNotNull(lenderGroupList);
291 List<LenderGroup> lenderGroups = lenderGroupList.getLenderGroup();
292 Assert.assertNotNull(lenderGroups);
293 Assert.assertTrue(lenderGroups.size() > 0);
294 String lender = lenderGroups.get(0).getLender();
295 Assert.assertEquals(lender, LENDER_REF_NAME);
297 if (logger.isDebugEnabled()) {
298 logger.debug("UTF-8 data sent=" + getUTF8DataFragment() + "\n"
299 + "UTF-8 data received=" + loaninCommon.getLoanInNote());
302 Assert.assertEquals(loaninCommon.getLoanInNote(), getUTF8DataFragment(),
303 "UTF-8 data retrieved '" + loaninCommon.getLoanInNote()
304 + "' does not match expected data '" + getUTF8DataFragment());
310 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
313 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
314 // dependsOnMethods = {"read"})
315 public void readNonExistent(String testName) throws Exception {
317 setupReadNonExistent();
319 // Submit the request to the service and store the response.
320 LoaninClient client = new LoaninClient();
321 Response res = client.read(NON_EXISTENT_ID);
323 int statusCode = res.getStatus();
325 // Check the status code of the response: does it match
326 // the expected response(s)?
327 if (logger.isDebugEnabled()) {
328 logger.debug(testName + ": status = " + statusCode);
330 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
331 invalidStatusCodeMessage(testRequestType, statusCode));
332 Assert.assertEquals(statusCode, testExpectedStatusCode);
340 // ---------------------------------------------------------------
341 // CRUD tests : READ_LIST tests
342 // ---------------------------------------------------------------
347 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
350 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
351 // dependsOnMethods = {"createList", "read"})
352 public void readList(String testName) throws Exception {
356 // Submit the request to the service and store the response.
357 AbstractCommonList list = null;
358 LoaninClient client = new LoaninClient();
359 Response res = client.readList();
360 assertStatusCode(res, testName);
362 int statusCode = res.getStatus();
364 // Check the status code of the response: does it match
365 // the expected response(s)?
366 if (logger.isDebugEnabled()) {
367 logger.debug(testName + ": status = " + statusCode);
369 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
370 invalidStatusCodeMessage(testRequestType, statusCode));
371 Assert.assertEquals(statusCode, testExpectedStatusCode);
373 list = res.readEntity(getCommonListType());
380 // Optionally output additional data about list members for debugging.
381 boolean iterateThroughList = true;
382 if(iterateThroughList && logger.isDebugEnabled()){
383 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
391 // ---------------------------------------------------------------
392 // CRUD tests : UPDATE tests
393 // ---------------------------------------------------------------
398 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
401 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
402 // dependsOnMethods = {"read"})
403 public void update(String testName) throws Exception {
407 // Retrieve the contents of a resource to update.
408 LoaninClient client = new LoaninClient();
409 Response res = client.read(knownResourceId);
410 PoxPayloadIn input = null;
412 assertStatusCode(res, testName);
413 input = new PoxPayloadIn(res.readEntity(String.class));
414 if (logger.isDebugEnabled()) {
415 logger.debug("got object to update with ID: " + knownResourceId);
423 // Extract the common part from the response.
424 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
425 LoansinCommon loaninCommon = null;
426 if (payloadInputPart != null) {
427 loaninCommon = (LoansinCommon) payloadInputPart.getBody();
429 Assert.assertNotNull(loaninCommon);
431 // Update the content of this resource.
432 loaninCommon.setLoanInNumber("updated-" + loaninCommon.getLoanInNumber());
433 loaninCommon.setLoanInNote("updated-" + loaninCommon.getLoanInNote());
434 if (logger.isDebugEnabled()) {
435 logger.debug("to be updated object");
436 logger.debug(objectAsXmlString(loaninCommon, LoansinCommon.class));
441 // Submit the updated common part in an update request to the service
442 // and store the response.
443 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
444 PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), loaninCommon);
445 res = client.update(knownResourceId, output);
447 assertStatusCode(res, testName);
448 int statusCode = res.getStatus();
449 // Check the status code of the response: does it match the expected response(s)?
450 if (logger.isDebugEnabled()) {
451 logger.debug(testName + ": status = " + statusCode);
453 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
454 invalidStatusCodeMessage(testRequestType, statusCode));
455 Assert.assertEquals(statusCode, testExpectedStatusCode);
456 input = new PoxPayloadIn(res.readEntity(String.class));
463 // Extract the updated common part from the response.
464 payloadInputPart = input.getPart(client.getCommonPartName());
465 LoansinCommon updatedLoaninCommon = null;
466 if (payloadInputPart != null) {
467 updatedLoaninCommon = (LoansinCommon) payloadInputPart.getBody();
469 Assert.assertNotNull(updatedLoaninCommon);
471 // Check selected fields in the updated common part.
472 Assert.assertEquals(updatedLoaninCommon.getLoanInNumber(),
473 loaninCommon.getLoanInNumber(),
474 "Data in updated object did not match submitted data.");
476 if (logger.isDebugEnabled()) {
477 logger.debug("UTF-8 data sent=" + loaninCommon.getLoanInNote() + "\n"
478 + "UTF-8 data received=" + updatedLoaninCommon.getLoanInNote());
480 Assert.assertTrue(updatedLoaninCommon.getLoanInNote().contains(getUTF8DataFragment()),
481 "UTF-8 data retrieved '" + updatedLoaninCommon.getLoanInNote()
482 + "' does not contain expected data '" + getUTF8DataFragment());
483 Assert.assertEquals(updatedLoaninCommon.getLoanInNote(),
484 loaninCommon.getLoanInNote(),
485 "Data in updated object did not match submitted data.");
489 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
490 // dependsOnMethods = {"update", "testSubmitRequest"})
491 public void updateNonExistent(String testName) throws Exception {
493 setupUpdateNonExistent();
495 // Submit the request to the service and store the response.
496 // Note: The ID used in this 'create' call may be arbitrary.
497 // The only relevant ID may be the one used in update(), below.
498 LoaninClient client = new LoaninClient();
499 PoxPayloadOut multipart = createLoaninInstance(NON_EXISTENT_ID);
500 Response res = client.update(NON_EXISTENT_ID, multipart);
502 int statusCode = res.getStatus();
504 // Check the status code of the response: does it match
505 // the expected response(s)?
506 if (logger.isDebugEnabled()) {
507 logger.debug(testName + ": status = " + statusCode);
509 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
510 invalidStatusCodeMessage(testRequestType, statusCode));
511 Assert.assertEquals(statusCode, testExpectedStatusCode);
519 // ---------------------------------------------------------------
520 // CRUD tests : DELETE tests
521 // ---------------------------------------------------------------
526 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
529 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
530 // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
531 public void delete(String testName) throws Exception {
535 // Submit the request to the service and store the response.
536 LoaninClient client = new LoaninClient();
537 Response res = client.delete(knownResourceId);
539 int statusCode = res.getStatus();
541 // Check the status code of the response: does it match
542 // the expected response(s)?
543 if (logger.isDebugEnabled()) {
544 logger.debug(testName + ": status = " + statusCode);
546 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
547 invalidStatusCodeMessage(testRequestType, statusCode));
548 Assert.assertEquals(statusCode, testExpectedStatusCode);
559 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
562 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
563 // dependsOnMethods = {"delete"})
564 public void deleteNonExistent(String testName) throws Exception {
566 setupDeleteNonExistent();
568 // Submit the request to the service and store the response.
569 LoaninClient client = new LoaninClient();
570 Response res = client.delete(NON_EXISTENT_ID);
572 int statusCode = res.getStatus();
574 // Check the status code of the response: does it match
575 // the expected response(s)?
576 if (logger.isDebugEnabled()) {
577 logger.debug(testName + ": status = " + statusCode);
579 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
580 invalidStatusCodeMessage(testRequestType, statusCode));
581 Assert.assertEquals(statusCode, testExpectedStatusCode);
589 // ---------------------------------------------------------------
590 // Utility tests : tests of code used in tests above
591 // ---------------------------------------------------------------
594 * Tests the code for manually submitting data that is used by several
595 * of the methods above.
597 // @Test(dependsOnMethods = {"create", "read"})
598 public void testSubmitRequest() {
600 // Expected status code: 200 OK
601 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
603 // Submit the request to the service and store the response.
604 String method = ServiceRequestType.READ.httpMethodName();
605 String url = getResourceURL(knownResourceId);
606 int statusCode = submitRequest(method, url);
608 // Check the status code of the response: does it match
609 // the expected response(s)?
610 if (logger.isDebugEnabled()) {
611 logger.debug("testSubmitRequest: url=" + url
612 + " status=" + statusCode);
614 Assert.assertEquals(statusCode, EXPECTED_STATUS);
618 // ---------------------------------------------------------------
619 // Utility methods used by tests above
620 // ---------------------------------------------------------------
623 public String getServiceName() {
628 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
631 public String getServicePathComponent() {
632 return SERVICE_PATH_COMPONENT;
636 protected PoxPayloadOut createInstance(String identifier) {
637 return createLoaninInstance(identifier);
641 * Creates the loanin instance.
643 * @param identifier the identifier
644 * @return the multipart output
646 private PoxPayloadOut createLoaninInstance(String identifier) {
647 return createLoaninInstance(
648 "loaninNumber-" + identifier,
649 "returnDate-" + identifier);
653 * Creates the loanin instance.
655 * @param loaninNumber the loanin number
656 * @param returnDate the return date
657 * @return the multipart output
659 private PoxPayloadOut createLoaninInstance(String loaninNumber,
662 LoansinCommon loaninCommon = new LoansinCommon();
663 loaninCommon.setLoanInNumber(loaninNumber);
664 loaninCommon.setLoanReturnDate(CURRENT_DATE_UTC);
665 LenderGroupList lenderGroupList = new LenderGroupList();
666 LenderGroup lenderGroup = new LenderGroup();
667 lenderGroup.setLender(LENDER_REF_NAME);
668 lenderGroupList.getLenderGroup().add(lenderGroup);
669 loaninCommon.setLenderGroupList(lenderGroupList);
670 loaninCommon.setLoanPurpose("For Surfboards of the 1960s exhibition.");
671 loaninCommon.setLoanInNote(getUTF8DataFragment());
673 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
674 PayloadOutputPart commonPart =
675 multipart.addPart(new LoaninClient().getCommonPartName(), loaninCommon);
677 if (logger.isDebugEnabled()) {
678 logger.debug("to be created, loanin common");
679 logger.debug(objectAsXmlString(loaninCommon, LoansinCommon.class));
686 public void CRUDTests(String testName) {
687 // TODO Auto-generated method stub
692 protected PoxPayloadOut createInstance(String commonPartName,
694 PoxPayloadOut result = createLoaninInstance(identifier);
699 protected LoansinCommon updateInstance(LoansinCommon commonPartObject) {
700 // TODO Auto-generated method stub
705 protected void compareUpdatedInstances(LoansinCommon original,
706 LoansinCommon updated) throws Exception {
707 // TODO Auto-generated method stub