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.ConditioncheckClient;
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.conditioncheck.ConditionchecksCommon;
40 import org.collectionspace.services.conditioncheck.HazardGroupList;
41 import org.collectionspace.services.conditioncheck.HazardGroup;
43 import org.testng.Assert;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * ConditioncheckServiceTest, carries out tests against a
49 * deployed and running Conditioncheck (aka Condition Checks) Service.
51 public class ConditioncheckServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, ConditionchecksCommon> {
54 private final String CLASS_NAME = ConditioncheckServiceTest.class.getName();
55 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
56 // Instance variables specific to this test.
57 /** The service path component. */
58 final String SERVICE_NAME = "conditionchecks";
59 final String SERVICE_PATH_COMPONENT = "conditionchecks";
60 private String CONDITIONCHECKER_REF_NAME =
61 "urn:cspace:org.collectionspace.demo:personauthorities:name(TestPersonAuth):item:name(HarryLender)'Harry Lender'";
62 private String CONDITIONCHECK_HAZARD =
63 "urn:cspace:core.collectionspace.org:vocabularies:name(TestTermList):item:name(hazard1)'multi-dimensional'";
64 private String CONDITIONCHECK_HAZARD_NOTE = "Object needs to be extinguished occasionally.";
65 private final static String CURRENT_DATE_UTC =
66 GregorianCalendarDateTimeUtils.timestampUTC();
69 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
72 protected CollectionSpaceClient getClientInstance() {
73 return new ConditioncheckClient();
77 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
78 return new ConditioncheckClient(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 ConditioncheckClient client = new ConditioncheckClient();
108 String identifier = createIdentifier();
109 PoxPayloadOut multipart = createConditioncheckInstance(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 ConditioncheckClient client = new ConditioncheckClient();
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 ConditionchecksCommon conditioncheckCommon = null;
283 if (payloadInputPart != null) {
284 conditioncheckCommon = (ConditionchecksCommon) payloadInputPart.getBody();
286 Assert.assertNotNull(conditioncheckCommon);
288 // Check selected fields.
289 HazardGroupList hazardGroupList = conditioncheckCommon.getHazardGroupList();
290 Assert.assertNotNull(hazardGroupList);
291 List<HazardGroup> hazardGroups = hazardGroupList.getHazardGroup();
292 Assert.assertNotNull(hazardGroups);
293 String hazard = hazardGroups.get(0).getHazard();
294 Assert.assertEquals(hazard, CONDITIONCHECK_HAZARD);
295 String hazardNote = hazardGroups.get(0).getHazardNote();
296 Assert.assertEquals(hazardNote, CONDITIONCHECK_HAZARD_NOTE);
297 String hazardDate = hazardGroups.get(0).getHazardDate();
298 Assert.assertEquals(hazardDate, CURRENT_DATE_UTC);
299 String conditionChecker = conditioncheckCommon.getConditionChecker();
300 Assert.assertEquals(conditionChecker, CONDITIONCHECKER_REF_NAME);
302 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
303 String conditionCheckNote = conditioncheckCommon.getConditionCheckNote();
305 if (logger.isDebugEnabled()) {
306 logger.debug("UTF-8 data sent=" + getUTF8DataFragment() + "\n"
307 + "UTF-8 data received=" + conditionCheckNote);
310 Assert.assertEquals(conditionCheckNote, getUTF8DataFragment(),
311 "UTF-8 data retrieved '" + conditionCheckNote
312 + "' does not match expected data '" + getUTF8DataFragment());
318 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
321 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
322 // dependsOnMethods = {"read"})
323 public void readNonExistent(String testName) throws Exception {
325 setupReadNonExistent();
327 // Submit the request to the service and store the response.
328 ConditioncheckClient client = new ConditioncheckClient();
329 Response res = client.read(NON_EXISTENT_ID);
331 int statusCode = res.getStatus();
333 // Check the status code of the response: does it match
334 // the expected response(s)?
335 if (logger.isDebugEnabled()) {
336 logger.debug(testName + ": status = " + statusCode);
338 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
339 invalidStatusCodeMessage(testRequestType, statusCode));
340 Assert.assertEquals(statusCode, testExpectedStatusCode);
348 // ---------------------------------------------------------------
349 // CRUD tests : READ_LIST tests
350 // ---------------------------------------------------------------
355 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
358 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
359 // dependsOnMethods = {"createList", "read"})
360 public void readList(String testName) throws Exception {
364 // Submit the request to the service and store the response.
365 AbstractCommonList list = null;
366 ConditioncheckClient client = new ConditioncheckClient();
367 Response res = client.readList();
368 assertStatusCode(res, testName);
370 int statusCode = res.getStatus();
372 // Check the status code of the response: does it match
373 // the expected response(s)?
374 if (logger.isDebugEnabled()) {
375 logger.debug(testName + ": status = " + statusCode);
377 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
378 invalidStatusCodeMessage(testRequestType, statusCode));
379 Assert.assertEquals(statusCode, testExpectedStatusCode);
381 list = res.readEntity(getCommonListType());
388 // Optionally output additional data about list members for debugging.
389 boolean iterateThroughList = true;
390 if(iterateThroughList && logger.isDebugEnabled()){
391 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
399 // ---------------------------------------------------------------
400 // CRUD tests : UPDATE tests
401 // ---------------------------------------------------------------
406 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
409 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
410 // dependsOnMethods = {"read"})
411 public void update(String testName) throws Exception {
415 // Retrieve the contents of a resource to update.
416 ConditioncheckClient client = new ConditioncheckClient();
417 Response res = client.read(knownResourceId);
418 PoxPayloadIn input = null;
420 assertStatusCode(res, testName);
421 input = new PoxPayloadIn(res.readEntity(String.class));
422 if (logger.isDebugEnabled()) {
423 logger.debug("got object to update with ID: " + knownResourceId);
431 // Extract the common part from the response.
432 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
433 ConditionchecksCommon conditioncheckCommon = null;
434 if (payloadInputPart != null) {
435 conditioncheckCommon = (ConditionchecksCommon) payloadInputPart.getBody();
437 Assert.assertNotNull(conditioncheckCommon);
439 // Update the content of this resource.
440 conditioncheckCommon.setConditionCheckRefNumber("updated-" + conditioncheckCommon.getConditionCheckRefNumber());
442 String conditionCheckNote = conditioncheckCommon.getConditionCheckNote();
443 conditioncheckCommon.setConditionCheckNote("updated-conditionCheckNote-" + conditionCheckNote);
445 if (logger.isDebugEnabled()) {
446 logger.debug("to be updated object");
447 logger.debug(objectAsXmlString(conditioncheckCommon, ConditionchecksCommon.class));
452 // Submit the updated common part in an update request to the service
453 // and store the response.
454 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
455 PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), conditioncheckCommon);
456 res = client.update(knownResourceId, output);
458 assertStatusCode(res, testName);
459 int statusCode = res.getStatus();
460 // Check the status code of the response: does it match the expected response(s)?
461 if (logger.isDebugEnabled()) {
462 logger.debug(testName + ": status = " + statusCode);
464 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
465 invalidStatusCodeMessage(testRequestType, statusCode));
466 Assert.assertEquals(statusCode, testExpectedStatusCode);
467 input = new PoxPayloadIn(res.readEntity(String.class));
474 // Extract the updated common part from the response.
475 payloadInputPart = input.getPart(client.getCommonPartName());
476 ConditionchecksCommon updatedConditioncheckCommon = null;
477 if (payloadInputPart != null) {
478 updatedConditioncheckCommon = (ConditionchecksCommon) payloadInputPart.getBody();
480 Assert.assertNotNull(updatedConditioncheckCommon);
482 // Check selected fields in the updated common part.
483 Assert.assertEquals(updatedConditioncheckCommon.getConditionCheckRefNumber(),
484 conditioncheckCommon.getConditionCheckRefNumber(),
485 "Data in updated object did not match submitted data.");
487 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
488 String originalConditionCheckNote = conditioncheckCommon.getConditionCheckNote();
489 String updatedConditionCheckNote = updatedConditioncheckCommon.getConditionCheckNote();
491 Assert.assertEquals(updatedConditionCheckNote, originalConditionCheckNote,
492 "Data in updated object did not match submitted data.");
494 if(logger.isDebugEnabled()){
495 logger.debug("UTF-8 data sent=" + originalConditionCheckNote + "\n"
496 + "UTF-8 data received=" + updatedConditionCheckNote);
498 Assert.assertTrue(updatedConditionCheckNote.contains(getUTF8DataFragment()),
499 "UTF-8 data retrieved '" + updatedConditionCheckNote
500 + "' does not match expected data '" + getUTF8DataFragment());
504 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
505 // dependsOnMethods = {"update", "testSubmitRequest"})
506 public void updateNonExistent(String testName) throws Exception {
508 setupUpdateNonExistent();
510 // Submit the request to the service and store the response.
511 // Note: The ID used in this 'create' call may be arbitrary.
512 // The only relevant ID may be the one used in update(), below.
513 ConditioncheckClient client = new ConditioncheckClient();
514 PoxPayloadOut multipart = createConditioncheckInstance(NON_EXISTENT_ID);
515 Response res = client.update(NON_EXISTENT_ID, multipart);
517 int statusCode = res.getStatus();
519 // Check the status code of the response: does it match
520 // the expected response(s)?
521 if (logger.isDebugEnabled()) {
522 logger.debug(testName + ": status = " + statusCode);
524 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
525 invalidStatusCodeMessage(testRequestType, statusCode));
526 Assert.assertEquals(statusCode, testExpectedStatusCode);
534 // ---------------------------------------------------------------
535 // CRUD tests : DELETE tests
536 // ---------------------------------------------------------------
541 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
544 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
545 // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
546 public void delete(String testName) throws Exception {
550 // Submit the request to the service and store the response.
551 ConditioncheckClient client = new ConditioncheckClient();
552 Response res = client.delete(knownResourceId);
554 int statusCode = res.getStatus();
556 // Check the status code of the response: does it match
557 // the expected response(s)?
558 if (logger.isDebugEnabled()) {
559 logger.debug(testName + ": status = " + statusCode);
561 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
562 invalidStatusCodeMessage(testRequestType, statusCode));
563 Assert.assertEquals(statusCode, testExpectedStatusCode);
574 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
577 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
578 // dependsOnMethods = {"delete"})
579 public void deleteNonExistent(String testName) throws Exception {
581 setupDeleteNonExistent();
583 // Submit the request to the service and store the response.
584 ConditioncheckClient client = new ConditioncheckClient();
585 Response res = client.delete(NON_EXISTENT_ID);
587 int statusCode = res.getStatus();
589 // Check the status code of the response: does it match
590 // the expected response(s)?
591 if (logger.isDebugEnabled()) {
592 logger.debug(testName + ": status = " + statusCode);
594 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
595 invalidStatusCodeMessage(testRequestType, statusCode));
596 Assert.assertEquals(statusCode, testExpectedStatusCode);
604 // ---------------------------------------------------------------
605 // Utility tests : tests of code used in tests above
606 // ---------------------------------------------------------------
609 * Tests the code for manually submitting data that is used by several
610 * of the methods above.
612 // @Test(dependsOnMethods = {"create", "read"})
613 public void testSubmitRequest() {
615 // Expected status code: 200 OK
616 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
618 // Submit the request to the service and store the response.
619 String method = ServiceRequestType.READ.httpMethodName();
620 String url = getResourceURL(knownResourceId);
621 int statusCode = submitRequest(method, url);
623 // Check the status code of the response: does it match
624 // the expected response(s)?
625 if (logger.isDebugEnabled()) {
626 logger.debug("testSubmitRequest: url=" + url
627 + " status=" + statusCode);
629 Assert.assertEquals(statusCode, EXPECTED_STATUS);
633 // ---------------------------------------------------------------
634 // Utility methods used by tests above
635 // ---------------------------------------------------------------
638 public String getServiceName() {
643 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
646 public String getServicePathComponent() {
647 return SERVICE_PATH_COMPONENT;
651 protected PoxPayloadOut createInstance(String identifier) {
652 return createConditioncheckInstance(identifier);
656 * Creates the conditioncheck instance.
658 * @param identifier the identifier
659 * @return the multipart output
661 //private PoxPayloadOut createConditioncheckInstance(String identifier) {
662 // return createConditioncheckInstance("conditionCheckRefNumber-" + identifier);
666 * Creates the conditioncheck instance.
668 * @param conditioncheckRefNumber the conditioncheck number
669 * @return the multipart output
671 private PoxPayloadOut createConditioncheckInstance(String conditionCheckRefNumber) {
673 ConditionchecksCommon conditioncheckCommon = new ConditionchecksCommon();
674 conditioncheckCommon.setConditionCheckRefNumber(conditionCheckRefNumber);
675 conditioncheckCommon.setConditionChecker(CONDITIONCHECKER_REF_NAME);
676 conditioncheckCommon.setConditionCheckNote(getUTF8DataFragment());
678 HazardGroupList hazardGroupList = new HazardGroupList();
679 List<HazardGroup> hazardGroups = hazardGroupList.getHazardGroup();
680 HazardGroup hazardGroup = new HazardGroup();
681 hazardGroup.setHazard(CONDITIONCHECK_HAZARD);
682 hazardGroup.setHazardDate(CURRENT_DATE_UTC);
683 hazardGroup.setHazardNote(CONDITIONCHECK_HAZARD_NOTE);
684 hazardGroups.add(hazardGroup);
685 conditioncheckCommon.setHazardGroupList(hazardGroupList);
687 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
688 PayloadOutputPart commonPart =
689 multipart.addPart(new ConditioncheckClient().getCommonPartName(), conditioncheckCommon);
691 if (logger.isDebugEnabled()) {
692 logger.debug("to be created, conditioncheck common");
693 logger.debug(objectAsXmlString(conditioncheckCommon, ConditionchecksCommon.class));
700 public void CRUDTests(String testName) {
701 // TODO Auto-generated method stub
706 protected PoxPayloadOut createInstance(String commonPartName,
708 PoxPayloadOut result = createConditioncheckInstance(identifier);
713 protected ConditionchecksCommon updateInstance(ConditionchecksCommon commonPartObject) {
714 // TODO Auto-generated method stub
719 protected void compareUpdatedInstances(ConditionchecksCommon original,
720 ConditionchecksCommon updated) throws Exception {
721 // TODO Auto-generated method stub