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.ConditioncheckClient;
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.conditioncheck.ConditionchecksCommon;
39 import org.collectionspace.services.conditioncheck.HazardGroupList;
40 import org.collectionspace.services.conditioncheck.HazardGroup;
42 import org.jboss.resteasy.client.ClientResponse;
43 import org.testng.Assert;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * ConditioncheckServiceTest, carries out tests against a
50 * deployed and running Conditioncheck (aka Condition Checks) Service.
52 public class ConditioncheckServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, ConditionchecksCommon> {
55 private final String CLASS_NAME = ConditioncheckServiceTest.class.getName();
56 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
57 // Instance variables specific to this test.
58 /** The service path component. */
59 final String SERVICE_NAME = "conditionchecks";
60 final String SERVICE_PATH_COMPONENT = "conditionchecks";
61 private String CONDITIONCHECKER_REF_NAME =
62 "urn:cspace:org.collectionspace.demo:personauthorities:name(TestPersonAuth):item:name(HarryLender)'Harry Lender'";
63 private String CONDITIONCHECK_HAZARD =
64 "urn:cspace:core.collectionspace.org:vocabularies:name(TestTermList):item:name(hazard1)'multi-dimensional'";
65 private String CONDITIONCHECK_HAZARD_NOTE = "Object needs to be extinguished occasionally.";
66 private final static String CURRENT_DATE_UTC =
67 GregorianCalendarDateTimeUtils.timestampUTC();
70 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
73 protected CollectionSpaceClient getClientInstance() {
74 return new ConditioncheckClient();
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 ConditioncheckClient client = new ConditioncheckClient();
104 String identifier = createIdentifier();
105 PoxPayloadOut multipart = createConditioncheckInstance(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 ConditioncheckClient client = new ConditioncheckClient();
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 ConditionchecksCommon conditioncheckCommon = null;
279 if (payloadInputPart != null) {
280 conditioncheckCommon = (ConditionchecksCommon) payloadInputPart.getBody();
282 Assert.assertNotNull(conditioncheckCommon);
284 // Check selected fields.
285 HazardGroupList hazardGroupList = conditioncheckCommon.getHazardGroupList();
286 Assert.assertNotNull(hazardGroupList);
287 List<HazardGroup> hazardGroups = hazardGroupList.getHazardGroup();
288 Assert.assertNotNull(hazardGroups);
289 String hazard = hazardGroups.get(0).getHazard();
290 Assert.assertEquals(hazard, CONDITIONCHECK_HAZARD);
291 String hazardNote = hazardGroups.get(0).getHazardNote();
292 Assert.assertEquals(hazardNote, CONDITIONCHECK_HAZARD_NOTE);
293 String hazardDate = hazardGroups.get(0).getHazardDate();
294 Assert.assertEquals(hazardDate, CURRENT_DATE_UTC);
295 String conditionChecker = conditioncheckCommon.getConditionChecker();
296 Assert.assertEquals(conditionChecker, CONDITIONCHECKER_REF_NAME);
298 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
299 String conditionCheckNote = conditioncheckCommon.getConditionCheckNote();
301 if (logger.isDebugEnabled()) {
302 logger.debug("UTF-8 data sent=" + getUTF8DataFragment() + "\n"
303 + "UTF-8 data received=" + conditionCheckNote);
306 Assert.assertEquals(conditionCheckNote, getUTF8DataFragment(),
307 "UTF-8 data retrieved '" + conditionCheckNote
308 + "' does not match expected data '" + getUTF8DataFragment());
314 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
317 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
318 // dependsOnMethods = {"read"})
319 public void readNonExistent(String testName) throws Exception {
321 setupReadNonExistent();
323 // Submit the request to the service and store the response.
324 ConditioncheckClient client = new ConditioncheckClient();
325 Response res = client.read(NON_EXISTENT_ID);
327 int statusCode = res.getStatus();
329 // Check the status code of the response: does it match
330 // the expected response(s)?
331 if (logger.isDebugEnabled()) {
332 logger.debug(testName + ": status = " + statusCode);
334 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
335 invalidStatusCodeMessage(testRequestType, statusCode));
336 Assert.assertEquals(statusCode, testExpectedStatusCode);
344 // ---------------------------------------------------------------
345 // CRUD tests : READ_LIST tests
346 // ---------------------------------------------------------------
351 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
354 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
355 // dependsOnMethods = {"createList", "read"})
356 public void readList(String testName) throws Exception {
360 // Submit the request to the service and store the response.
361 AbstractCommonList list = null;
362 ConditioncheckClient client = new ConditioncheckClient();
363 Response res = client.readList();
364 assertStatusCode(res, testName);
366 int statusCode = res.getStatus();
368 // Check the status code of the response: does it match
369 // the expected response(s)?
370 if (logger.isDebugEnabled()) {
371 logger.debug(testName + ": status = " + statusCode);
373 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
374 invalidStatusCodeMessage(testRequestType, statusCode));
375 Assert.assertEquals(statusCode, testExpectedStatusCode);
377 list = res.readEntity(getCommonListType());
384 // Optionally output additional data about list members for debugging.
385 boolean iterateThroughList = true;
386 if(iterateThroughList && logger.isDebugEnabled()){
387 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
395 // ---------------------------------------------------------------
396 // CRUD tests : UPDATE tests
397 // ---------------------------------------------------------------
402 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
405 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
406 // dependsOnMethods = {"read"})
407 public void update(String testName) throws Exception {
411 // Retrieve the contents of a resource to update.
412 ConditioncheckClient client = new ConditioncheckClient();
413 Response res = client.read(knownResourceId);
414 PoxPayloadIn input = null;
416 assertStatusCode(res, testName);
417 input = new PoxPayloadIn((String)res.getEntity());
418 if (logger.isDebugEnabled()) {
419 logger.debug("got object to update with ID: " + knownResourceId);
427 // Extract the common part from the response.
428 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
429 ConditionchecksCommon conditioncheckCommon = null;
430 if (payloadInputPart != null) {
431 conditioncheckCommon = (ConditionchecksCommon) payloadInputPart.getBody();
433 Assert.assertNotNull(conditioncheckCommon);
435 // Update the content of this resource.
436 conditioncheckCommon.setConditionCheckRefNumber("updated-" + conditioncheckCommon.getConditionCheckRefNumber());
438 String conditionCheckNote = conditioncheckCommon.getConditionCheckNote();
439 conditioncheckCommon.setConditionCheckNote("updated-conditionCheckNote-" + conditionCheckNote);
441 if (logger.isDebugEnabled()) {
442 logger.debug("to be updated object");
443 logger.debug(objectAsXmlString(conditioncheckCommon, ConditionchecksCommon.class));
448 // Submit the updated common part in an update request to the service
449 // and store the response.
450 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
451 PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), conditioncheckCommon);
452 res = client.update(knownResourceId, output);
454 assertStatusCode(res, testName);
455 int statusCode = res.getStatus();
456 // Check the status code of the response: does it match the expected response(s)?
457 if (logger.isDebugEnabled()) {
458 logger.debug(testName + ": status = " + statusCode);
460 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
461 invalidStatusCodeMessage(testRequestType, statusCode));
462 Assert.assertEquals(statusCode, testExpectedStatusCode);
463 input = new PoxPayloadIn((String)res.getEntity());
470 // Extract the updated common part from the response.
471 payloadInputPart = input.getPart(client.getCommonPartName());
472 ConditionchecksCommon updatedConditioncheckCommon = null;
473 if (payloadInputPart != null) {
474 updatedConditioncheckCommon = (ConditionchecksCommon) payloadInputPart.getBody();
476 Assert.assertNotNull(updatedConditioncheckCommon);
478 // Check selected fields in the updated common part.
479 Assert.assertEquals(updatedConditioncheckCommon.getConditionCheckRefNumber(),
480 conditioncheckCommon.getConditionCheckRefNumber(),
481 "Data in updated object did not match submitted data.");
483 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
484 String originalConditionCheckNote = conditioncheckCommon.getConditionCheckNote();
485 String updatedConditionCheckNote = updatedConditioncheckCommon.getConditionCheckNote();
487 Assert.assertEquals(updatedConditionCheckNote, originalConditionCheckNote,
488 "Data in updated object did not match submitted data.");
490 if(logger.isDebugEnabled()){
491 logger.debug("UTF-8 data sent=" + originalConditionCheckNote + "\n"
492 + "UTF-8 data received=" + updatedConditionCheckNote);
494 Assert.assertTrue(updatedConditionCheckNote.contains(getUTF8DataFragment()),
495 "UTF-8 data retrieved '" + updatedConditionCheckNote
496 + "' does not match expected data '" + getUTF8DataFragment());
500 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
501 // dependsOnMethods = {"update", "testSubmitRequest"})
502 public void updateNonExistent(String testName) throws Exception {
504 setupUpdateNonExistent();
506 // Submit the request to the service and store the response.
507 // Note: The ID used in this 'create' call may be arbitrary.
508 // The only relevant ID may be the one used in update(), below.
509 ConditioncheckClient client = new ConditioncheckClient();
510 PoxPayloadOut multipart = createConditioncheckInstance(NON_EXISTENT_ID);
511 Response res = client.update(NON_EXISTENT_ID, multipart);
513 int statusCode = res.getStatus();
515 // Check the status code of the response: does it match
516 // the expected response(s)?
517 if (logger.isDebugEnabled()) {
518 logger.debug(testName + ": status = " + statusCode);
520 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
521 invalidStatusCodeMessage(testRequestType, statusCode));
522 Assert.assertEquals(statusCode, testExpectedStatusCode);
530 // ---------------------------------------------------------------
531 // CRUD tests : DELETE tests
532 // ---------------------------------------------------------------
537 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
540 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
541 // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
542 public void delete(String testName) throws Exception {
546 // Submit the request to the service and store the response.
547 ConditioncheckClient client = new ConditioncheckClient();
548 Response res = client.delete(knownResourceId);
550 int statusCode = res.getStatus();
552 // Check the status code of the response: does it match
553 // the expected response(s)?
554 if (logger.isDebugEnabled()) {
555 logger.debug(testName + ": status = " + statusCode);
557 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
558 invalidStatusCodeMessage(testRequestType, statusCode));
559 Assert.assertEquals(statusCode, testExpectedStatusCode);
570 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
573 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
574 // dependsOnMethods = {"delete"})
575 public void deleteNonExistent(String testName) throws Exception {
577 setupDeleteNonExistent();
579 // Submit the request to the service and store the response.
580 ConditioncheckClient client = new ConditioncheckClient();
581 Response res = client.delete(NON_EXISTENT_ID);
583 int statusCode = res.getStatus();
585 // Check the status code of the response: does it match
586 // the expected response(s)?
587 if (logger.isDebugEnabled()) {
588 logger.debug(testName + ": status = " + statusCode);
590 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
591 invalidStatusCodeMessage(testRequestType, statusCode));
592 Assert.assertEquals(statusCode, testExpectedStatusCode);
600 // ---------------------------------------------------------------
601 // Utility tests : tests of code used in tests above
602 // ---------------------------------------------------------------
605 * Tests the code for manually submitting data that is used by several
606 * of the methods above.
608 // @Test(dependsOnMethods = {"create", "read"})
609 public void testSubmitRequest() {
611 // Expected status code: 200 OK
612 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
614 // Submit the request to the service and store the response.
615 String method = ServiceRequestType.READ.httpMethodName();
616 String url = getResourceURL(knownResourceId);
617 int statusCode = submitRequest(method, url);
619 // Check the status code of the response: does it match
620 // the expected response(s)?
621 if (logger.isDebugEnabled()) {
622 logger.debug("testSubmitRequest: url=" + url
623 + " status=" + statusCode);
625 Assert.assertEquals(statusCode, EXPECTED_STATUS);
629 // ---------------------------------------------------------------
630 // Utility methods used by tests above
631 // ---------------------------------------------------------------
634 public String getServiceName() {
639 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
642 public String getServicePathComponent() {
643 return SERVICE_PATH_COMPONENT;
647 protected PoxPayloadOut createInstance(String identifier) {
648 return createConditioncheckInstance(identifier);
652 * Creates the conditioncheck instance.
654 * @param identifier the identifier
655 * @return the multipart output
657 //private PoxPayloadOut createConditioncheckInstance(String identifier) {
658 // return createConditioncheckInstance("conditionCheckRefNumber-" + identifier);
662 * Creates the conditioncheck instance.
664 * @param conditioncheckRefNumber the conditioncheck number
665 * @return the multipart output
667 private PoxPayloadOut createConditioncheckInstance(String conditionCheckRefNumber) {
669 ConditionchecksCommon conditioncheckCommon = new ConditionchecksCommon();
670 conditioncheckCommon.setConditionCheckRefNumber(conditionCheckRefNumber);
671 conditioncheckCommon.setConditionChecker(CONDITIONCHECKER_REF_NAME);
672 conditioncheckCommon.setConditionCheckNote(getUTF8DataFragment());
674 HazardGroupList hazardGroupList = new HazardGroupList();
675 List<HazardGroup> hazardGroups = hazardGroupList.getHazardGroup();
676 HazardGroup hazardGroup = new HazardGroup();
677 hazardGroup.setHazard(CONDITIONCHECK_HAZARD);
678 hazardGroup.setHazardDate(CURRENT_DATE_UTC);
679 hazardGroup.setHazardNote(CONDITIONCHECK_HAZARD_NOTE);
680 hazardGroups.add(hazardGroup);
681 conditioncheckCommon.setHazardGroupList(hazardGroupList);
683 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
684 PayloadOutputPart commonPart =
685 multipart.addPart(new ConditioncheckClient().getCommonPartName(), conditioncheckCommon);
687 if (logger.isDebugEnabled()) {
688 logger.debug("to be created, conditioncheck common");
689 logger.debug(objectAsXmlString(conditioncheckCommon, ConditionchecksCommon.class));
696 public void CRUDTests(String testName) {
697 // TODO Auto-generated method stub
702 protected PoxPayloadOut createInstance(String commonPartName,
704 PoxPayloadOut result = createConditioncheckInstance(identifier);
709 protected ConditionchecksCommon updateInstance(ConditionchecksCommon commonPartObject) {
710 // TODO Auto-generated method stub
715 protected void compareUpdatedInstances(ConditionchecksCommon original,
716 ConditionchecksCommon updated) throws Exception {
717 // TODO Auto-generated method stub