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.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.datetime.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.jboss.resteasy.client.ClientResponse;
44 import org.testng.Assert;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * LoaninServiceTest, carries out tests against a
51 * deployed and running Loanin (aka Loans In) Service.
53 * $LastChangedRevision$
56 public class LoaninServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, LoansinCommon> {
59 private final String CLASS_NAME = LoaninServiceTest.class.getName();
60 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
61 // Instance variables specific to this test.
62 /** The service path component. */
63 final String SERVICE_NAME = "loansin";
64 final String SERVICE_PATH_COMPONENT = "loansin";
65 private String LENDER_REF_NAME =
66 "urn:cspace:org.collectionspace.demo:personauthorities:name(TestPersonAuth):item:name(HarryLender)'Harry Lender'";
67 private final static String CURRENT_DATE_UTC =
68 GregorianCalendarDateTimeUtils.currentDateUTC();
71 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
74 protected CollectionSpaceClient getClientInstance() {
75 return new LoaninClient();
79 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
82 protected AbstractCommonList getCommonList(
83 ClientResponse<AbstractCommonList> response) {
84 return response.getEntity(AbstractCommonList.class);
87 // ---------------------------------------------------------------
88 // CRUD tests : CREATE tests
89 // ---------------------------------------------------------------
94 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
97 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
98 public void create(String testName) throws Exception {
99 // Perform setup, such as initializing the type of service request
100 // (e.g. CREATE, DELETE), its valid and expected status codes, and
101 // its associated HTTP method name (e.g. POST, DELETE).
104 // Submit the request to the service and store the response.
105 LoaninClient client = new LoaninClient();
106 String identifier = createIdentifier();
107 PoxPayloadOut multipart = createLoaninInstance(identifier);
109 ClientResponse<Response> res = client.create(multipart);
111 int statusCode = res.getStatus();
113 // Check the status code of the response: does it match
114 // the expected response(s)?
117 // Does it fall within the set of valid status codes?
118 // Does it exactly match the expected status code?
119 if (logger.isDebugEnabled()) {
120 logger.debug(testName + ": status = " + statusCode);
122 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
123 invalidStatusCodeMessage(testRequestType, statusCode));
124 Assert.assertEquals(statusCode, testExpectedStatusCode);
126 newID = extractId(res);
129 res.releaseConnection();
133 // Store the ID returned from the first resource created
134 // for additional tests below.
135 if (knownResourceId == null) {
136 knownResourceId = newID;
137 if (logger.isDebugEnabled()) {
138 logger.debug(testName + ": knownResourceId=" + knownResourceId);
142 // Store the IDs from every resource created by tests,
143 // so they can be deleted after tests have been run.
144 allResourceIdsCreated.add(newID);
148 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
151 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
152 // dependsOnMethods = {"create"})
153 public void createList(String testName) throws Exception {
154 for (int i = 0; i < 3; i++) {
162 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
163 dependsOnMethods = {"create", "testSubmitRequest"})
164 public void createWithEmptyEntityBody(String testName) throws Exception {
166 if (logger.isDebugEnabled()) {
167 logger.debug(testBanner(testName, CLASS_NAME));
170 setupCreateWithEmptyEntityBody();
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 = "";
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("createWithEmptyEntityBody 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 createWithMalformedXml(String testName) throws Exception {
195 if (logger.isDebugEnabled()) {
196 logger.debug(testBanner(testName, CLASS_NAME));
199 setupCreateWithMalformedXml(testName, logger);
201 // Submit the request to the service and store the response.
202 String method = REQUEST_TYPE.httpMethodName();
203 String url = getServiceRootURL();
204 String mediaType = MediaType.APPLICATION_XML;
205 final String entity = MALFORMED_XML_DATA; // Constant from base class.
206 int statusCode = submitRequest(method, url, mediaType, entity);
208 // Check the status code of the response: does it match
209 // the expected response(s)?
210 if(logger.isDebugEnabled()){
211 logger.debug(testName + ": url=" + url +
212 " status=" + statusCode);
214 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
215 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
216 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
220 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
221 dependsOnMethods = {"create", "testSubmitRequest"})
222 public void createWithWrongXmlSchema(String testName) throws Exception {
224 if (logger.isDebugEnabled()) {
225 logger.debug(testBanner(testName, CLASS_NAME));
228 setupCreateWithWrongXmlSchema(testName, logger);
230 // Submit the request to the service and store the response.
231 String method = REQUEST_TYPE.httpMethodName();
232 String url = getServiceRootURL();
233 String mediaType = MediaType.APPLICATION_XML;
234 final String entity = WRONG_XML_SCHEMA_DATA;
235 int statusCode = submitRequest(method, url, mediaType, entity);
237 // Check the status code of the response: does it match
238 // the expected response(s)?
239 if(logger.isDebugEnabled()){
240 logger.debug(testName + ": url=" + url +
241 " status=" + statusCode);
243 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
244 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
245 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
249 // ---------------------------------------------------------------
250 // CRUD tests : READ tests
251 // ---------------------------------------------------------------
256 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
259 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
260 // dependsOnMethods = {"create"})
261 public void read(String testName) throws Exception {
265 // Submit the request to the service and store the response.
266 LoaninClient client = new LoaninClient();
267 ClientResponse<String> res = client.read(knownResourceId);
268 PoxPayloadIn input = null;
270 assertStatusCode(res, testName);
271 input = new PoxPayloadIn(res.getEntity());
274 res.releaseConnection();
278 // Get the common part of the response and verify that it is not null.
279 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
280 LoansinCommon loaninCommon = null;
281 if (payloadInputPart != null) {
282 loaninCommon = (LoansinCommon) payloadInputPart.getBody();
284 Assert.assertNotNull(loaninCommon);
286 // Check selected fields.
287 LenderGroupList lenderGroupList = loaninCommon.getLenderGroupList();
288 Assert.assertNotNull(lenderGroupList);
289 List<LenderGroup> lenderGroups = lenderGroupList.getLenderGroup();
290 Assert.assertNotNull(lenderGroups);
291 Assert.assertTrue(lenderGroups.size() > 0);
292 String lender = lenderGroups.get(0).getLender();
293 Assert.assertEquals(lender, LENDER_REF_NAME);
295 if (logger.isDebugEnabled()) {
296 logger.debug("UTF-8 data sent=" + getUTF8DataFragment() + "\n"
297 + "UTF-8 data received=" + loaninCommon.getLoanInNote());
300 Assert.assertEquals(loaninCommon.getLoanInNote(), getUTF8DataFragment(),
301 "UTF-8 data retrieved '" + loaninCommon.getLoanInNote()
302 + "' does not match expected data '" + getUTF8DataFragment());
308 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
311 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
312 // dependsOnMethods = {"read"})
313 public void readNonExistent(String testName) throws Exception {
315 setupReadNonExistent();
317 // Submit the request to the service and store the response.
318 LoaninClient client = new LoaninClient();
319 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
321 int statusCode = res.getStatus();
323 // Check the status code of the response: does it match
324 // the expected response(s)?
325 if (logger.isDebugEnabled()) {
326 logger.debug(testName + ": status = " + statusCode);
328 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
329 invalidStatusCodeMessage(testRequestType, statusCode));
330 Assert.assertEquals(statusCode, testExpectedStatusCode);
333 res.releaseConnection();
338 // ---------------------------------------------------------------
339 // CRUD tests : READ_LIST tests
340 // ---------------------------------------------------------------
345 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
348 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
349 // dependsOnMethods = {"createList", "read"})
350 public void readList(String testName) throws Exception {
354 // Submit the request to the service and store the response.
355 AbstractCommonList list = null;
356 LoaninClient client = new LoaninClient();
357 ClientResponse<AbstractCommonList> res = client.readList();
358 assertStatusCode(res, testName);
360 int statusCode = res.getStatus();
362 // Check the status code of the response: does it match
363 // the expected response(s)?
364 if (logger.isDebugEnabled()) {
365 logger.debug(testName + ": status = " + statusCode);
367 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
368 invalidStatusCodeMessage(testRequestType, statusCode));
369 Assert.assertEquals(statusCode, testExpectedStatusCode);
371 list = res.getEntity();
374 res.releaseConnection();
378 // Optionally output additional data about list members for debugging.
379 boolean iterateThroughList = true;
380 if(iterateThroughList && logger.isDebugEnabled()){
381 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
389 // ---------------------------------------------------------------
390 // CRUD tests : UPDATE tests
391 // ---------------------------------------------------------------
396 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
399 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
400 // dependsOnMethods = {"read"})
401 public void update(String testName) throws Exception {
405 // Retrieve the contents of a resource to update.
406 LoaninClient client = new LoaninClient();
407 ClientResponse<String> res = client.read(knownResourceId);
408 PoxPayloadIn input = null;
410 assertStatusCode(res, testName);
411 input = new PoxPayloadIn(res.getEntity());
412 if (logger.isDebugEnabled()) {
413 logger.debug("got object to update with ID: " + knownResourceId);
417 res.releaseConnection();
421 // Extract the common part from the response.
422 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
423 LoansinCommon loaninCommon = null;
424 if (payloadInputPart != null) {
425 loaninCommon = (LoansinCommon) payloadInputPart.getBody();
427 Assert.assertNotNull(loaninCommon);
429 // Update the content of this resource.
430 loaninCommon.setLoanInNumber("updated-" + loaninCommon.getLoanInNumber());
431 loaninCommon.setLoanInNote("updated-" + loaninCommon.getLoanInNote());
432 if (logger.isDebugEnabled()) {
433 logger.debug("to be updated object");
434 logger.debug(objectAsXmlString(loaninCommon, LoansinCommon.class));
439 // Submit the updated common part in an update request to the service
440 // and store the response.
441 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
442 PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), loaninCommon);
443 res = client.update(knownResourceId, output);
445 assertStatusCode(res, testName);
446 int statusCode = res.getStatus();
447 // Check the status code of the response: does it match the expected response(s)?
448 if (logger.isDebugEnabled()) {
449 logger.debug(testName + ": status = " + statusCode);
451 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
452 invalidStatusCodeMessage(testRequestType, statusCode));
453 Assert.assertEquals(statusCode, testExpectedStatusCode);
454 input = new PoxPayloadIn(res.getEntity());
457 res.releaseConnection();
461 // Extract the updated common part from the response.
462 payloadInputPart = input.getPart(client.getCommonPartName());
463 LoansinCommon updatedLoaninCommon = null;
464 if (payloadInputPart != null) {
465 updatedLoaninCommon = (LoansinCommon) payloadInputPart.getBody();
467 Assert.assertNotNull(updatedLoaninCommon);
469 // Check selected fields in the updated common part.
470 Assert.assertEquals(updatedLoaninCommon.getLoanInNumber(),
471 loaninCommon.getLoanInNumber(),
472 "Data in updated object did not match submitted data.");
474 if (logger.isDebugEnabled()) {
475 logger.debug("UTF-8 data sent=" + loaninCommon.getLoanInNote() + "\n"
476 + "UTF-8 data received=" + updatedLoaninCommon.getLoanInNote());
478 Assert.assertTrue(updatedLoaninCommon.getLoanInNote().contains(getUTF8DataFragment()),
479 "UTF-8 data retrieved '" + updatedLoaninCommon.getLoanInNote()
480 + "' does not contain expected data '" + getUTF8DataFragment());
481 Assert.assertEquals(updatedLoaninCommon.getLoanInNote(),
482 loaninCommon.getLoanInNote(),
483 "Data in updated object did not match submitted data.");
487 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
488 // dependsOnMethods = {"update", "testSubmitRequest"})
489 public void updateNonExistent(String testName) throws Exception {
491 setupUpdateNonExistent();
493 // Submit the request to the service and store the response.
494 // Note: The ID used in this 'create' call may be arbitrary.
495 // The only relevant ID may be the one used in update(), below.
496 LoaninClient client = new LoaninClient();
497 PoxPayloadOut multipart = createLoaninInstance(NON_EXISTENT_ID);
498 ClientResponse<String> res = client.update(NON_EXISTENT_ID, multipart);
500 int statusCode = res.getStatus();
502 // Check the status code of the response: does it match
503 // the expected response(s)?
504 if (logger.isDebugEnabled()) {
505 logger.debug(testName + ": status = " + statusCode);
507 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
508 invalidStatusCodeMessage(testRequestType, statusCode));
509 Assert.assertEquals(statusCode, testExpectedStatusCode);
512 res.releaseConnection();
517 // ---------------------------------------------------------------
518 // CRUD tests : DELETE tests
519 // ---------------------------------------------------------------
524 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
527 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
528 // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
529 public void delete(String testName) throws Exception {
533 // Submit the request to the service and store the response.
534 LoaninClient client = new LoaninClient();
535 ClientResponse<Response> res = client.delete(knownResourceId);
537 int statusCode = res.getStatus();
539 // Check the status code of the response: does it match
540 // the expected response(s)?
541 if (logger.isDebugEnabled()) {
542 logger.debug(testName + ": status = " + statusCode);
544 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
545 invalidStatusCodeMessage(testRequestType, statusCode));
546 Assert.assertEquals(statusCode, testExpectedStatusCode);
549 res.releaseConnection();
557 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
560 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
561 // dependsOnMethods = {"delete"})
562 public void deleteNonExistent(String testName) throws Exception {
564 setupDeleteNonExistent();
566 // Submit the request to the service and store the response.
567 LoaninClient client = new LoaninClient();
568 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
570 int statusCode = res.getStatus();
572 // Check the status code of the response: does it match
573 // the expected response(s)?
574 if (logger.isDebugEnabled()) {
575 logger.debug(testName + ": status = " + statusCode);
577 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
578 invalidStatusCodeMessage(testRequestType, statusCode));
579 Assert.assertEquals(statusCode, testExpectedStatusCode);
582 res.releaseConnection();
587 // ---------------------------------------------------------------
588 // Utility tests : tests of code used in tests above
589 // ---------------------------------------------------------------
592 * Tests the code for manually submitting data that is used by several
593 * of the methods above.
595 // @Test(dependsOnMethods = {"create", "read"})
596 public void testSubmitRequest() {
598 // Expected status code: 200 OK
599 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
601 // Submit the request to the service and store the response.
602 String method = ServiceRequestType.READ.httpMethodName();
603 String url = getResourceURL(knownResourceId);
604 int statusCode = submitRequest(method, url);
606 // Check the status code of the response: does it match
607 // the expected response(s)?
608 if (logger.isDebugEnabled()) {
609 logger.debug("testSubmitRequest: url=" + url
610 + " status=" + statusCode);
612 Assert.assertEquals(statusCode, EXPECTED_STATUS);
616 // ---------------------------------------------------------------
617 // Utility methods used by tests above
618 // ---------------------------------------------------------------
621 public String getServiceName() {
626 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
629 public String getServicePathComponent() {
630 return SERVICE_PATH_COMPONENT;
634 protected PoxPayloadOut createInstance(String identifier) {
635 return createLoaninInstance(identifier);
639 * Creates the loanin instance.
641 * @param identifier the identifier
642 * @return the multipart output
644 private PoxPayloadOut createLoaninInstance(String identifier) {
645 return createLoaninInstance(
646 "loaninNumber-" + identifier,
647 "returnDate-" + identifier);
651 * Creates the loanin instance.
653 * @param loaninNumber the loanin number
654 * @param returnDate the return date
655 * @return the multipart output
657 private PoxPayloadOut createLoaninInstance(String loaninNumber,
660 LoansinCommon loaninCommon = new LoansinCommon();
661 loaninCommon.setLoanInNumber(loaninNumber);
662 loaninCommon.setLoanReturnDate(CURRENT_DATE_UTC);
663 LenderGroupList lenderGroupList = new LenderGroupList();
664 LenderGroup lenderGroup = new LenderGroup();
665 lenderGroup.setLender(LENDER_REF_NAME);
666 lenderGroupList.getLenderGroup().add(lenderGroup);
667 loaninCommon.setLenderGroupList(lenderGroupList);
668 loaninCommon.setLoanPurpose("For Surfboards of the 1960s exhibition.");
669 loaninCommon.setLoanInNote(getUTF8DataFragment());
671 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
672 PayloadOutputPart commonPart =
673 multipart.addPart(new LoaninClient().getCommonPartName(), loaninCommon);
675 if (logger.isDebugEnabled()) {
676 logger.debug("to be created, loanin common");
677 logger.debug(objectAsXmlString(loaninCommon, LoansinCommon.class));
684 public void CRUDTests(String testName) {
685 // TODO Auto-generated method stub
690 protected PoxPayloadOut createInstance(String commonPartName,
692 PoxPayloadOut result = createLoaninInstance(identifier);
697 protected LoansinCommon updateInstance(LoansinCommon commonPartObject) {
698 // TODO Auto-generated method stub
703 protected void compareUpdatedInstances(LoansinCommon original,
704 LoansinCommon updated) throws Exception {
705 // TODO Auto-generated method stub