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.Response;
29 import org.collectionspace.services.client.AbstractCommonListUtils;
30 import org.collectionspace.services.client.CollectionSpaceClient;
31 import org.collectionspace.services.client.LoaninClient;
32 import org.collectionspace.services.client.PayloadInputPart;
33 import org.collectionspace.services.client.PayloadOutputPart;
34 import org.collectionspace.services.client.PoxPayloadIn;
35 import org.collectionspace.services.client.PoxPayloadOut;
36 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
37 import org.collectionspace.services.jaxb.AbstractCommonList;
38 import org.collectionspace.services.loanin.LenderGroup;
39 import org.collectionspace.services.loanin.LenderGroupList;
40 import org.collectionspace.services.loanin.LoansinCommon;
42 import org.jboss.resteasy.client.ClientResponse;
43 import org.testng.Assert;
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$
55 public class LoaninServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, LoansinCommon> {
58 private final String CLASS_NAME = LoaninServiceTest.class.getName();
59 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
60 // Instance variables specific to this test.
61 /** The service path component. */
62 final String SERVICE_NAME = "loansin";
63 final String SERVICE_PATH_COMPONENT = "loansin";
64 private String LENDER_REF_NAME =
65 "urn:cspace:org.collectionspace.demo:personauthorities:name(TestPersonAuth):item:name(HarryLender)'Harry Lender'";
66 private final static String CURRENT_DATE_UTC =
67 GregorianCalendarDateTimeUtils.currentDateUTC();
70 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
73 protected CollectionSpaceClient getClientInstance() {
74 return new LoaninClient();
78 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
81 protected AbstractCommonList getCommonList(Response response) {
82 return response.readEntity(AbstractCommonList.class);
85 // ---------------------------------------------------------------
86 // CRUD tests : CREATE tests
87 // ---------------------------------------------------------------
92 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
95 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
96 public void create(String testName) throws Exception {
97 // Perform setup, such as initializing the type of service request
98 // (e.g. CREATE, DELETE), its valid and expected status codes, and
99 // its associated HTTP method name (e.g. POST, DELETE).
102 // Submit the request to the service and store the response.
103 LoaninClient client = new LoaninClient();
104 String identifier = createIdentifier();
105 PoxPayloadOut multipart = createLoaninInstance(identifier);
107 Response res = client.create(multipart);
109 int statusCode = res.getStatus();
111 // Check the status code of the response: does it match
112 // the expected response(s)?
115 // Does it fall within the set of valid status codes?
116 // Does it exactly match the expected status code?
117 if (logger.isDebugEnabled()) {
118 logger.debug(testName + ": status = " + statusCode);
120 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
121 invalidStatusCodeMessage(testRequestType, statusCode));
122 Assert.assertEquals(statusCode, testExpectedStatusCode);
124 newID = extractId(res);
131 // Store the ID returned from the first resource created
132 // for additional tests below.
133 if (knownResourceId == null) {
134 knownResourceId = newID;
135 if (logger.isDebugEnabled()) {
136 logger.debug(testName + ": knownResourceId=" + knownResourceId);
140 // Store the IDs from every resource created by tests,
141 // so they can be deleted after tests have been run.
142 allResourceIdsCreated.add(newID);
146 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
149 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
150 // dependsOnMethods = {"create"})
151 public void createList(String testName) throws Exception {
152 for (int i = 0; i < 3; i++) {
160 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
161 dependsOnMethods = {"create", "testSubmitRequest"})
162 public void createWithEmptyEntityBody(String testName) throws Exception {
164 if (logger.isDebugEnabled()) {
165 logger.debug(testBanner(testName, CLASS_NAME));
168 setupCreateWithEmptyEntityBody();
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 = "";
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("createWithEmptyEntityBody 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 createWithMalformedXml(String testName) throws Exception {
193 if (logger.isDebugEnabled()) {
194 logger.debug(testBanner(testName, CLASS_NAME));
197 setupCreateWithMalformedXml(testName, logger);
199 // Submit the request to the service and store the response.
200 String method = REQUEST_TYPE.httpMethodName();
201 String url = getServiceRootURL();
202 String mediaType = MediaType.APPLICATION_XML;
203 final String entity = MALFORMED_XML_DATA; // Constant from base class.
204 int statusCode = submitRequest(method, url, mediaType, entity);
206 // Check the status code of the response: does it match
207 // the expected response(s)?
208 if(logger.isDebugEnabled()){
209 logger.debug(testName + ": url=" + url +
210 " status=" + statusCode);
212 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
213 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
214 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
218 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
219 dependsOnMethods = {"create", "testSubmitRequest"})
220 public void createWithWrongXmlSchema(String testName) throws Exception {
222 if (logger.isDebugEnabled()) {
223 logger.debug(testBanner(testName, CLASS_NAME));
226 setupCreateWithWrongXmlSchema(testName, logger);
228 // Submit the request to the service and store the response.
229 String method = REQUEST_TYPE.httpMethodName();
230 String url = getServiceRootURL();
231 String mediaType = MediaType.APPLICATION_XML;
232 final String entity = WRONG_XML_SCHEMA_DATA;
233 int statusCode = submitRequest(method, url, mediaType, entity);
235 // Check the status code of the response: does it match
236 // the expected response(s)?
237 if(logger.isDebugEnabled()){
238 logger.debug(testName + ": url=" + url +
239 " status=" + statusCode);
241 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
242 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
243 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
247 // ---------------------------------------------------------------
248 // CRUD tests : READ tests
249 // ---------------------------------------------------------------
254 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
257 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
258 // dependsOnMethods = {"create"})
259 public void read(String testName) throws Exception {
263 // Submit the request to the service and store the response.
264 LoaninClient client = new LoaninClient();
265 Response res = client.read(knownResourceId);
266 PoxPayloadIn input = null;
268 assertStatusCode(res, testName);
269 input = new PoxPayloadIn((String)res.getEntity());
276 // Get the common part of the response and verify that it is not null.
277 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
278 LoansinCommon loaninCommon = null;
279 if (payloadInputPart != null) {
280 loaninCommon = (LoansinCommon) payloadInputPart.getBody();
282 Assert.assertNotNull(loaninCommon);
284 // Check selected fields.
285 LenderGroupList lenderGroupList = loaninCommon.getLenderGroupList();
286 Assert.assertNotNull(lenderGroupList);
287 List<LenderGroup> lenderGroups = lenderGroupList.getLenderGroup();
288 Assert.assertNotNull(lenderGroups);
289 Assert.assertTrue(lenderGroups.size() > 0);
290 String lender = lenderGroups.get(0).getLender();
291 Assert.assertEquals(lender, LENDER_REF_NAME);
293 if (logger.isDebugEnabled()) {
294 logger.debug("UTF-8 data sent=" + getUTF8DataFragment() + "\n"
295 + "UTF-8 data received=" + loaninCommon.getLoanInNote());
298 Assert.assertEquals(loaninCommon.getLoanInNote(), getUTF8DataFragment(),
299 "UTF-8 data retrieved '" + loaninCommon.getLoanInNote()
300 + "' does not match expected data '" + getUTF8DataFragment());
306 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
309 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
310 // dependsOnMethods = {"read"})
311 public void readNonExistent(String testName) throws Exception {
313 setupReadNonExistent();
315 // Submit the request to the service and store the response.
316 LoaninClient client = new LoaninClient();
317 Response res = client.read(NON_EXISTENT_ID);
319 int statusCode = res.getStatus();
321 // Check the status code of the response: does it match
322 // the expected response(s)?
323 if (logger.isDebugEnabled()) {
324 logger.debug(testName + ": status = " + statusCode);
326 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
327 invalidStatusCodeMessage(testRequestType, statusCode));
328 Assert.assertEquals(statusCode, testExpectedStatusCode);
336 // ---------------------------------------------------------------
337 // CRUD tests : READ_LIST tests
338 // ---------------------------------------------------------------
343 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
346 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
347 // dependsOnMethods = {"createList", "read"})
348 public void readList(String testName) throws Exception {
352 // Submit the request to the service and store the response.
353 AbstractCommonList list = null;
354 LoaninClient client = new LoaninClient();
355 Response res = client.readList();
356 assertStatusCode(res, testName);
358 int statusCode = res.getStatus();
360 // Check the status code of the response: does it match
361 // the expected response(s)?
362 if (logger.isDebugEnabled()) {
363 logger.debug(testName + ": status = " + statusCode);
365 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
366 invalidStatusCodeMessage(testRequestType, statusCode));
367 Assert.assertEquals(statusCode, testExpectedStatusCode);
369 list = res.readEntity(getCommonListType());
376 // Optionally output additional data about list members for debugging.
377 boolean iterateThroughList = true;
378 if(iterateThroughList && logger.isDebugEnabled()){
379 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
387 // ---------------------------------------------------------------
388 // CRUD tests : UPDATE tests
389 // ---------------------------------------------------------------
394 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
397 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
398 // dependsOnMethods = {"read"})
399 public void update(String testName) throws Exception {
403 // Retrieve the contents of a resource to update.
404 LoaninClient client = new LoaninClient();
405 Response res = client.read(knownResourceId);
406 PoxPayloadIn input = null;
408 assertStatusCode(res, testName);
409 input = new PoxPayloadIn((String)res.getEntity());
410 if (logger.isDebugEnabled()) {
411 logger.debug("got object to update with ID: " + knownResourceId);
419 // Extract the common part from the response.
420 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
421 LoansinCommon loaninCommon = null;
422 if (payloadInputPart != null) {
423 loaninCommon = (LoansinCommon) payloadInputPart.getBody();
425 Assert.assertNotNull(loaninCommon);
427 // Update the content of this resource.
428 loaninCommon.setLoanInNumber("updated-" + loaninCommon.getLoanInNumber());
429 loaninCommon.setLoanInNote("updated-" + loaninCommon.getLoanInNote());
430 if (logger.isDebugEnabled()) {
431 logger.debug("to be updated object");
432 logger.debug(objectAsXmlString(loaninCommon, LoansinCommon.class));
437 // Submit the updated common part in an update request to the service
438 // and store the response.
439 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
440 PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), loaninCommon);
441 res = client.update(knownResourceId, output);
443 assertStatusCode(res, testName);
444 int statusCode = res.getStatus();
445 // Check the status code of the response: does it match the expected response(s)?
446 if (logger.isDebugEnabled()) {
447 logger.debug(testName + ": status = " + statusCode);
449 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
450 invalidStatusCodeMessage(testRequestType, statusCode));
451 Assert.assertEquals(statusCode, testExpectedStatusCode);
452 input = new PoxPayloadIn((String)res.getEntity());
459 // Extract the updated common part from the response.
460 payloadInputPart = input.getPart(client.getCommonPartName());
461 LoansinCommon updatedLoaninCommon = null;
462 if (payloadInputPart != null) {
463 updatedLoaninCommon = (LoansinCommon) payloadInputPart.getBody();
465 Assert.assertNotNull(updatedLoaninCommon);
467 // Check selected fields in the updated common part.
468 Assert.assertEquals(updatedLoaninCommon.getLoanInNumber(),
469 loaninCommon.getLoanInNumber(),
470 "Data in updated object did not match submitted data.");
472 if (logger.isDebugEnabled()) {
473 logger.debug("UTF-8 data sent=" + loaninCommon.getLoanInNote() + "\n"
474 + "UTF-8 data received=" + updatedLoaninCommon.getLoanInNote());
476 Assert.assertTrue(updatedLoaninCommon.getLoanInNote().contains(getUTF8DataFragment()),
477 "UTF-8 data retrieved '" + updatedLoaninCommon.getLoanInNote()
478 + "' does not contain expected data '" + getUTF8DataFragment());
479 Assert.assertEquals(updatedLoaninCommon.getLoanInNote(),
480 loaninCommon.getLoanInNote(),
481 "Data in updated object did not match submitted data.");
485 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
486 // dependsOnMethods = {"update", "testSubmitRequest"})
487 public void updateNonExistent(String testName) throws Exception {
489 setupUpdateNonExistent();
491 // Submit the request to the service and store the response.
492 // Note: The ID used in this 'create' call may be arbitrary.
493 // The only relevant ID may be the one used in update(), below.
494 LoaninClient client = new LoaninClient();
495 PoxPayloadOut multipart = createLoaninInstance(NON_EXISTENT_ID);
496 Response res = client.update(NON_EXISTENT_ID, multipart);
498 int statusCode = res.getStatus();
500 // Check the status code of the response: does it match
501 // the expected response(s)?
502 if (logger.isDebugEnabled()) {
503 logger.debug(testName + ": status = " + statusCode);
505 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
506 invalidStatusCodeMessage(testRequestType, statusCode));
507 Assert.assertEquals(statusCode, testExpectedStatusCode);
515 // ---------------------------------------------------------------
516 // CRUD tests : DELETE tests
517 // ---------------------------------------------------------------
522 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
525 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
526 // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
527 public void delete(String testName) throws Exception {
531 // Submit the request to the service and store the response.
532 LoaninClient client = new LoaninClient();
533 Response res = client.delete(knownResourceId);
535 int statusCode = res.getStatus();
537 // Check the status code of the response: does it match
538 // the expected response(s)?
539 if (logger.isDebugEnabled()) {
540 logger.debug(testName + ": status = " + statusCode);
542 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
543 invalidStatusCodeMessage(testRequestType, statusCode));
544 Assert.assertEquals(statusCode, testExpectedStatusCode);
555 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
558 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
559 // dependsOnMethods = {"delete"})
560 public void deleteNonExistent(String testName) throws Exception {
562 setupDeleteNonExistent();
564 // Submit the request to the service and store the response.
565 LoaninClient client = new LoaninClient();
566 Response res = client.delete(NON_EXISTENT_ID);
568 int statusCode = res.getStatus();
570 // Check the status code of the response: does it match
571 // the expected response(s)?
572 if (logger.isDebugEnabled()) {
573 logger.debug(testName + ": status = " + statusCode);
575 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
576 invalidStatusCodeMessage(testRequestType, statusCode));
577 Assert.assertEquals(statusCode, testExpectedStatusCode);
585 // ---------------------------------------------------------------
586 // Utility tests : tests of code used in tests above
587 // ---------------------------------------------------------------
590 * Tests the code for manually submitting data that is used by several
591 * of the methods above.
593 // @Test(dependsOnMethods = {"create", "read"})
594 public void testSubmitRequest() {
596 // Expected status code: 200 OK
597 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
599 // Submit the request to the service and store the response.
600 String method = ServiceRequestType.READ.httpMethodName();
601 String url = getResourceURL(knownResourceId);
602 int statusCode = submitRequest(method, url);
604 // Check the status code of the response: does it match
605 // the expected response(s)?
606 if (logger.isDebugEnabled()) {
607 logger.debug("testSubmitRequest: url=" + url
608 + " status=" + statusCode);
610 Assert.assertEquals(statusCode, EXPECTED_STATUS);
614 // ---------------------------------------------------------------
615 // Utility methods used by tests above
616 // ---------------------------------------------------------------
619 public String getServiceName() {
624 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
627 public String getServicePathComponent() {
628 return SERVICE_PATH_COMPONENT;
632 protected PoxPayloadOut createInstance(String identifier) {
633 return createLoaninInstance(identifier);
637 * Creates the loanin instance.
639 * @param identifier the identifier
640 * @return the multipart output
642 private PoxPayloadOut createLoaninInstance(String identifier) {
643 return createLoaninInstance(
644 "loaninNumber-" + identifier,
645 "returnDate-" + identifier);
649 * Creates the loanin instance.
651 * @param loaninNumber the loanin number
652 * @param returnDate the return date
653 * @return the multipart output
655 private PoxPayloadOut createLoaninInstance(String loaninNumber,
658 LoansinCommon loaninCommon = new LoansinCommon();
659 loaninCommon.setLoanInNumber(loaninNumber);
660 loaninCommon.setLoanReturnDate(CURRENT_DATE_UTC);
661 LenderGroupList lenderGroupList = new LenderGroupList();
662 LenderGroup lenderGroup = new LenderGroup();
663 lenderGroup.setLender(LENDER_REF_NAME);
664 lenderGroupList.getLenderGroup().add(lenderGroup);
665 loaninCommon.setLenderGroupList(lenderGroupList);
666 loaninCommon.setLoanPurpose("For Surfboards of the 1960s exhibition.");
667 loaninCommon.setLoanInNote(getUTF8DataFragment());
669 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
670 PayloadOutputPart commonPart =
671 multipart.addPart(new LoaninClient().getCommonPartName(), loaninCommon);
673 if (logger.isDebugEnabled()) {
674 logger.debug("to be created, loanin common");
675 logger.debug(objectAsXmlString(loaninCommon, LoansinCommon.class));
682 public void CRUDTests(String testName) {
683 // TODO Auto-generated method stub
688 protected PoxPayloadOut createInstance(String commonPartName,
690 PoxPayloadOut result = createLoaninInstance(identifier);
695 protected LoansinCommon updateInstance(LoansinCommon commonPartObject) {
696 // TODO Auto-generated method stub
701 protected void compareUpdatedInstances(LoansinCommon original,
702 LoansinCommon updated) throws Exception {
703 // TODO Auto-generated method stub