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.ConservationClient;
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.jaxb.AbstractCommonList;
38 import org.collectionspace.services.conservation.ConservationCommon;
39 import org.collectionspace.services.conservation.ConservatorsList;
40 import org.collectionspace.services.conservation.ConservationStatusGroup;
41 import org.collectionspace.services.conservation.ConservationStatusGroupList;
43 import org.testng.Assert;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * ConservationServiceTest, carries out tests against a
49 * deployed and running Conservation Service.
51 * $LastChangedRevision$
54 public class ConservationServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, ConservationCommon> {
57 private final String CLASS_NAME = ConservationServiceTest.class.getName();
58 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
59 // Instance variables specific to this test.
60 /** The service path component. */
61 final String SERVICE_NAME = "conservation";
62 final String SERVICE_PATH_COMPONENT = "conservation";
63 private String CONSERVATOR_REF_NAME =
64 "urn:cspace:org.collectionspace.demo:personauthorities:name(TestPersonAuth):item:name(CarlaConservator)'Carla Conservator'";
65 private String STATUS = "Treatment proposed";
68 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
71 protected CollectionSpaceClient getClientInstance() {
72 return new ConservationClient();
76 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
77 return new ConservationClient(clientPropertiesFilename);
81 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
84 protected AbstractCommonList getCommonList(Response response) {
85 return response.readEntity(AbstractCommonList.class);
88 // ---------------------------------------------------------------
89 // CRUD tests : CREATE tests
90 // ---------------------------------------------------------------
95 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
98 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
99 public void create(String testName) throws Exception {
100 // Perform setup, such as initializing the type of service request
101 // (e.g. CREATE, DELETE), its valid and expected status codes, and
102 // its associated HTTP method name (e.g. POST, DELETE).
105 // Submit the request to the service and store the response.
106 ConservationClient client = new ConservationClient();
107 String identifier = createIdentifier();
108 PoxPayloadOut multipart = createConservationInstance(identifier);
110 Response res = client.create(multipart);
112 int statusCode = res.getStatus();
114 // Check the status code of the response: does it match
115 // the expected response(s)?
118 // Does it fall within the set of valid status codes?
119 // Does it exactly match the expected status code?
120 if (logger.isDebugEnabled()) {
121 logger.debug(testName + ": status = " + statusCode);
123 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
124 invalidStatusCodeMessage(testRequestType, statusCode));
125 Assert.assertEquals(statusCode, testExpectedStatusCode);
127 newID = extractId(res);
134 // Store the ID returned from the first resource created
135 // for additional tests below.
136 if (knownResourceId == null) {
137 knownResourceId = newID;
138 if (logger.isDebugEnabled()) {
139 logger.debug(testName + ": knownResourceId=" + knownResourceId);
143 // Store the IDs from every resource created by tests,
144 // so they can be deleted after tests have been run.
145 allResourceIdsCreated.add(newID);
149 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
152 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
153 // dependsOnMethods = {"create"})
154 public void createList(String testName) throws Exception {
155 for (int i = 0; i < 3; i++) {
163 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
164 dependsOnMethods = {"create", "testSubmitRequest"})
165 public void createWithEmptyEntityBody(String testName) throws Exception {
167 if (logger.isDebugEnabled()) {
168 logger.debug(testBanner(testName, CLASS_NAME));
171 setupCreateWithEmptyEntityBody();
173 // Submit the request to the service and store the response.
174 String method = REQUEST_TYPE.httpMethodName();
175 String url = getServiceRootURL();
176 String mediaType = MediaType.APPLICATION_XML;
177 final String entity = "";
178 int statusCode = submitRequest(method, url, mediaType, entity);
180 // Check the status code of the response: does it match
181 // the expected response(s)?
182 if(logger.isDebugEnabled()){
183 logger.debug("createWithEmptyEntityBody url=" + url +
184 " status=" + statusCode);
186 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
187 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
188 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
192 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
193 dependsOnMethods = {"create", "testSubmitRequest"})
194 public void createWithMalformedXml(String testName) throws Exception {
196 if (logger.isDebugEnabled()) {
197 logger.debug(testBanner(testName, CLASS_NAME));
200 setupCreateWithMalformedXml(testName, logger);
202 // Submit the request to the service and store the response.
203 String method = REQUEST_TYPE.httpMethodName();
204 String url = getServiceRootURL();
205 String mediaType = MediaType.APPLICATION_XML;
206 final String entity = MALFORMED_XML_DATA; // Constant from base class.
207 int statusCode = submitRequest(method, url, mediaType, entity);
209 // Check the status code of the response: does it match
210 // the expected response(s)?
211 if(logger.isDebugEnabled()){
212 logger.debug(testName + ": url=" + url +
213 " status=" + statusCode);
215 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
216 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
217 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
221 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
222 dependsOnMethods = {"create", "testSubmitRequest"})
223 public void createWithWrongXmlSchema(String testName) throws Exception {
225 if (logger.isDebugEnabled()) {
226 logger.debug(testBanner(testName, CLASS_NAME));
229 setupCreateWithWrongXmlSchema(testName, logger);
231 // Submit the request to the service and store the response.
232 String method = REQUEST_TYPE.httpMethodName();
233 String url = getServiceRootURL();
234 String mediaType = MediaType.APPLICATION_XML;
235 final String entity = WRONG_XML_SCHEMA_DATA;
236 int statusCode = submitRequest(method, url, mediaType, entity);
238 // Check the status code of the response: does it match
239 // the expected response(s)?
240 if(logger.isDebugEnabled()){
241 logger.debug(testName + ": url=" + url +
242 " status=" + statusCode);
244 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
245 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
246 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
250 // ---------------------------------------------------------------
251 // CRUD tests : READ tests
252 // ---------------------------------------------------------------
257 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
260 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
261 // dependsOnMethods = {"create"})
262 public void read(String testName) throws Exception {
266 // Submit the request to the service and store the response.
267 ConservationClient client = new ConservationClient();
268 Response res = client.read(knownResourceId);
269 PoxPayloadIn input = null;
271 assertStatusCode(res, testName);
272 input = new PoxPayloadIn(res.readEntity(String.class));
279 // Get the common part of the response and verify that it is not null.
280 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
281 ConservationCommon conservationCommon = null;
282 if (payloadInputPart != null) {
283 conservationCommon = (ConservationCommon) payloadInputPart.getBody();
285 Assert.assertNotNull(conservationCommon);
287 // Check selected fields.
288 ConservatorsList conservatorsList = conservationCommon.getConservators();
289 Assert.assertNotNull(conservatorsList);
290 List<String> conservators = conservatorsList.getConservator();
291 Assert.assertTrue(conservators.size() > 0);
292 Assert.assertEquals(conservators.get(0), CONSERVATOR_REF_NAME);
293 ConservationStatusGroupList conservationStatusGroupList = conservationCommon.getConservationStatusGroupList();
294 Assert.assertNotNull(conservationStatusGroupList);
295 List<ConservationStatusGroup> conservationStatusGroups = conservationStatusGroupList.getConservationStatusGroup();
296 Assert.assertNotNull(conservationStatusGroups);
297 Assert.assertTrue(conservationStatusGroups.size() > 0);
298 String status = conservationStatusGroups.get(0).getStatus();
299 Assert.assertEquals(status, STATUS);
301 if (logger.isDebugEnabled()) {
302 logger.debug("UTF-8 data sent=" + getUTF8DataFragment() + "\n"
303 + "UTF-8 data received=" + conservationCommon.getFabricationNote());
306 Assert.assertEquals(conservationCommon.getFabricationNote(), getUTF8DataFragment(),
307 "UTF-8 data retrieved '" + conservationCommon.getFabricationNote()
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 ConservationClient client = new ConservationClient();
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 ConservationClient client = new ConservationClient();
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 ConservationClient client = new ConservationClient();
413 Response res = client.read(knownResourceId);
414 PoxPayloadIn input = null;
416 assertStatusCode(res, testName);
417 input = new PoxPayloadIn(res.readEntity(String.class));
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 ConservationCommon conservationCommon = null;
430 if (payloadInputPart != null) {
431 conservationCommon = (ConservationCommon) payloadInputPart.getBody();
433 Assert.assertNotNull(conservationCommon);
435 // Update the content of this resource.
436 conservationCommon.setConservationNumber("updated-" + conservationCommon.getConservationNumber());
437 conservationCommon.setFabricationNote("updated-" + conservationCommon.getFabricationNote());
438 if (logger.isDebugEnabled()) {
439 logger.debug("to be updated object");
440 logger.debug(objectAsXmlString(conservationCommon, ConservationCommon.class));
445 // Submit the updated common part in an update request to the service
446 // and store the response.
447 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
448 PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), conservationCommon);
449 res = client.update(knownResourceId, output);
451 assertStatusCode(res, testName);
452 int statusCode = res.getStatus();
453 // Check the status code of the response: does it match the expected response(s)?
454 if (logger.isDebugEnabled()) {
455 logger.debug(testName + ": status = " + statusCode);
457 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
458 invalidStatusCodeMessage(testRequestType, statusCode));
459 Assert.assertEquals(statusCode, testExpectedStatusCode);
460 input = new PoxPayloadIn(res.readEntity(String.class));
467 // Extract the updated common part from the response.
468 payloadInputPart = input.getPart(client.getCommonPartName());
469 ConservationCommon updatedConservationCommon = null;
470 if (payloadInputPart != null) {
471 updatedConservationCommon = (ConservationCommon) payloadInputPart.getBody();
473 Assert.assertNotNull(updatedConservationCommon);
475 // Check selected fields in the updated common part.
476 Assert.assertEquals(updatedConservationCommon.getConservationNumber(),
477 conservationCommon.getConservationNumber(),
478 "Data in updated object did not match submitted data.");
480 if (logger.isDebugEnabled()) {
481 logger.debug("UTF-8 data sent=" + conservationCommon.getFabricationNote() + "\n"
482 + "UTF-8 data received=" + updatedConservationCommon.getFabricationNote());
484 Assert.assertTrue(updatedConservationCommon.getFabricationNote().contains(getUTF8DataFragment()),
485 "UTF-8 data retrieved '" + updatedConservationCommon.getFabricationNote()
486 + "' does not contain expected data '" + getUTF8DataFragment());
487 Assert.assertEquals(updatedConservationCommon.getFabricationNote(),
488 conservationCommon.getFabricationNote(),
489 "Data in updated object did not match submitted data.");
493 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
494 // dependsOnMethods = {"update", "testSubmitRequest"})
495 public void updateNonExistent(String testName) throws Exception {
497 setupUpdateNonExistent();
499 // Submit the request to the service and store the response.
500 // Note: The ID used in this 'create' call may be arbitrary.
501 // The only relevant ID may be the one used in update(), below.
502 ConservationClient client = new ConservationClient();
503 PoxPayloadOut multipart = createConservationInstance(NON_EXISTENT_ID);
504 Response res = client.update(NON_EXISTENT_ID, multipart);
506 int statusCode = res.getStatus();
508 // Check the status code of the response: does it match
509 // the expected response(s)?
510 if (logger.isDebugEnabled()) {
511 logger.debug(testName + ": status = " + statusCode);
513 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
514 invalidStatusCodeMessage(testRequestType, statusCode));
515 Assert.assertEquals(statusCode, testExpectedStatusCode);
523 // ---------------------------------------------------------------
524 // CRUD tests : DELETE tests
525 // ---------------------------------------------------------------
530 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
533 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
534 // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
535 public void delete(String testName) throws Exception {
539 // Submit the request to the service and store the response.
540 ConservationClient client = new ConservationClient();
541 Response res = client.delete(knownResourceId);
543 int statusCode = res.getStatus();
545 // Check the status code of the response: does it match
546 // the expected response(s)?
547 if (logger.isDebugEnabled()) {
548 logger.debug(testName + ": status = " + statusCode);
550 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
551 invalidStatusCodeMessage(testRequestType, statusCode));
552 Assert.assertEquals(statusCode, testExpectedStatusCode);
563 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
566 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
567 // dependsOnMethods = {"delete"})
568 public void deleteNonExistent(String testName) throws Exception {
570 setupDeleteNonExistent();
572 // Submit the request to the service and store the response.
573 ConservationClient client = new ConservationClient();
574 Response res = client.delete(NON_EXISTENT_ID);
576 int statusCode = res.getStatus();
578 // Check the status code of the response: does it match
579 // the expected response(s)?
580 if (logger.isDebugEnabled()) {
581 logger.debug(testName + ": status = " + statusCode);
583 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
584 invalidStatusCodeMessage(testRequestType, statusCode));
585 Assert.assertEquals(statusCode, testExpectedStatusCode);
593 // ---------------------------------------------------------------
594 // Utility tests : tests of code used in tests above
595 // ---------------------------------------------------------------
598 * Tests the code for manually submitting data that is used by several
599 * of the methods above.
601 // @Test(dependsOnMethods = {"create", "read"})
602 public void testSubmitRequest() {
604 // Expected status code: 200 OK
605 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
607 // Submit the request to the service and store the response.
608 String method = ServiceRequestType.READ.httpMethodName();
609 String url = getResourceURL(knownResourceId);
610 int statusCode = submitRequest(method, url);
612 // Check the status code of the response: does it match
613 // the expected response(s)?
614 if (logger.isDebugEnabled()) {
615 logger.debug("testSubmitRequest: url=" + url
616 + " status=" + statusCode);
618 Assert.assertEquals(statusCode, EXPECTED_STATUS);
622 // ---------------------------------------------------------------
623 // Utility methods used by tests above
624 // ---------------------------------------------------------------
627 public String getServiceName() {
632 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
635 public String getServicePathComponent() {
636 return SERVICE_PATH_COMPONENT;
640 protected PoxPayloadOut createInstance(String identifier) {
641 return createConservationInstance(identifier);
645 * Creates the conservation instance.
647 * @param conservationNumber the conservation number
648 * @param returnDate the return date
649 * @return the multipart output
651 private PoxPayloadOut createConservationInstance(String identifier) {
652 ConservationCommon conservationCommon = new ConservationCommon();
653 conservationCommon.setConservationNumber("conservationNumber-" + identifier);
655 ConservatorsList conservatorsList = new ConservatorsList();
656 conservatorsList.getConservator().add(CONSERVATOR_REF_NAME);
658 ConservationStatusGroupList conservationStatusGroupList = new ConservationStatusGroupList();
659 ConservationStatusGroup conservationStatusGroup = new ConservationStatusGroup();
660 conservationStatusGroup.setStatus(STATUS);
661 conservationStatusGroupList.getConservationStatusGroup().add(conservationStatusGroup);
663 conservationCommon.setConservators(conservatorsList);
664 conservationCommon.setConservationStatusGroupList(conservationStatusGroupList);
665 conservationCommon.setFabricationNote(getUTF8DataFragment());
667 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
668 PayloadOutputPart commonPart =
669 multipart.addPart(new ConservationClient().getCommonPartName(), conservationCommon);
671 if (logger.isDebugEnabled()) {
672 logger.debug("to be created, conservation common");
673 logger.debug(objectAsXmlString(conservationCommon, ConservationCommon.class));
680 public void CRUDTests(String testName) {
681 // TODO Auto-generated method stub
686 protected PoxPayloadOut createInstance(String commonPartName,
688 PoxPayloadOut result = createConservationInstance(identifier);
693 protected ConservationCommon updateInstance(ConservationCommon commonPartObject) {
694 // TODO Auto-generated method stub
699 protected void compareUpdatedInstances(ConservationCommon original,
700 ConservationCommon updated) throws Exception {
701 // TODO Auto-generated method stub