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 (c) 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.List;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
29 import org.collectionspace.services.client.AcquisitionClient;
30 import org.collectionspace.services.client.CollectionSpaceClient;
32 import org.collectionspace.services.acquisition.AcquisitionsCommon;
33 import org.collectionspace.services.acquisition.AcquisitionDateList;
34 import org.collectionspace.services.acquisition.AcquisitionFunding;
35 import org.collectionspace.services.acquisition.AcquisitionFundingList;
36 import org.collectionspace.services.acquisition.AcquisitionSourceList;
37 import org.collectionspace.services.acquisition.OwnerList;
38 import org.jboss.resteasy.client.ClientResponse;
39 import org.collectionspace.services.jaxb.AbstractCommonList;
41 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
42 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
43 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
44 import org.testng.Assert;
45 import org.testng.annotations.Test;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * AcquisitionServiceTest, carries out tests against a
52 * deployed and running Acquisition Service.
54 * $LastChangedRevision: 621 $
55 * $LastChangedDate: 2009-09-02 16:49:01 -0700 (Wed, 02 Sep 2009) $
57 public class AcquisitionServiceTest extends AbstractServiceTestImpl {
60 private final String CLASS_NAME = AcquisitionServiceTest.class.getName();
61 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
63 // Instance variables specific to this test.
64 /** The known resource id. */
65 private String knownResourceId = null;
68 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
71 protected CollectionSpaceClient getClientInstance() {
72 return new AcquisitionClient();
76 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
79 protected AbstractCommonList getAbstractCommonList(
80 ClientResponse<AbstractCommonList> response) {
81 return response.getEntity(AbstractCommonList.class);
84 // ---------------------------------------------------------------
85 // CRUD tests : CREATE tests
86 // ---------------------------------------------------------------
89 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
92 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
93 public void create(String testName) throws Exception {
95 if (logger.isDebugEnabled()) {
96 logger.debug(testBanner(testName, CLASS_NAME));
99 // Perform setup, such as initializing the type of service request
100 // (e.g. CREATE, DELETE), its valid and expected status codes, and
101 // its associated HTTP method name (e.g. POST, DELETE).
104 // Submit the request to the service and store the response.
105 String identifier = createIdentifier();
107 AcquisitionClient client = new AcquisitionClient();
108 MultipartOutput multipart = createAcquisitionInstance(identifier);
109 ClientResponse<Response> res = client.create(multipart);
111 int statusCode = res.getStatus();
113 // Check the status code of the response: does it match
114 // the expected response(s)?
117 // Does it fall within the set of valid status codes?
118 // Does it exactly match the expected status code?
119 if(logger.isDebugEnabled()){
120 logger.debug(testName + ": status = " + statusCode);
122 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
123 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
124 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
126 // Store the ID returned from the first resource created
127 // for additional tests below.
128 if (knownResourceId == null){
129 knownResourceId = extractId(res);
130 if (logger.isDebugEnabled()) {
131 logger.debug(testName + ": knownResourceId=" + knownResourceId);
135 // Store the IDs from every resource created by tests,
136 // so they can be deleted after tests have been run.
137 allResourceIdsCreated.add(extractId(res));
141 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
144 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
145 dependsOnMethods = {"create"})
146 public void createList(String testName) throws Exception {
147 for(int i = 0; i < 3; i++){
153 * Tests to diagnose and fix CSPACE-2578.
155 * This is a bug identified in release 1.0 alpha, after first implementing an
156 * Acquisition Funding repeatable group of fields, in which record creation
157 * fails if there is whitespace (or more generally, a text node) between
158 * the acquisitionFunding container element and its first child field.
161 // Verify that record creation occurs successfully when there is NO whitespace
162 // between the acquisitionFunding tag and its first child element tag
163 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
164 dependsOnMethods = {"create", "testSubmitRequest"}, groups = {"cspace2578group"})
165 public void createFromXmlNoWhitespaceAfterRepeatableGroupTag(String testName) throws Exception {
166 if (logger.isDebugEnabled()) {
167 logger.debug(testBanner(testName, CLASS_NAME));
169 String testDataDir = System.getProperty("test-data.fileName");
171 createFromXmlFile(testName, testDataDir + "/cspace-2578-no-whitespace.xml", false);
172 testSubmitRequest(newId);
175 // Verify that record creation occurs successfully when there is whitespace
176 // between the acquisitionFunding tag and its first child element tag
178 // FIXME: This test currently fails. @Test annotation is currently commented
179 // out for check-in, to prevent service tests from failing. Can uncomment to test
180 // fixes, and also after the issue is resolved, to help detect any regressions.
182 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
183 dependsOnMethods = {"create", "testSubmitRequest"}, groups = {"cspace2578group"})
184 public void createFromXmlWhitespaceAfterRepeatableGroupTag(String testName) throws Exception {
185 if (logger.isDebugEnabled()) {
186 logger.debug(testBanner(testName, CLASS_NAME));
188 String testDataDir = System.getProperty("test-data.fileName");
190 createFromXmlFile(testName, testDataDir + "/cspace-2578-whitespace.xml", false);
191 AcquisitionsCommon acquisition = readAcquisitionCommonPart(newId);
192 testSubmitRequest(newId);
197 // Placeholders until the three tests below can be uncommented.
198 // See Issue CSPACE-401.
200 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
203 public void createWithEmptyEntityBody(String testName) throws Exception {
204 //Should this really be empty?
208 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
211 public void createWithMalformedXml(String testName) throws Exception {
212 //Should this really be empty?
216 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
219 public void createWithWrongXmlSchema(String testName) throws Exception {
220 //Should this really be empty?
225 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
226 dependsOnMethods = {"create", "testSubmitRequest"})
227 public void createWithMalformedXml(String testName) throws Exception {
229 if (logger.isDebugEnabled()) {
230 logger.debug(testBanner(testName, CLASS_NAME));
234 setupCreateWithMalformedXml();
236 // Submit the request to the service and store the response.
237 String method = REQUEST_TYPE.httpMethodName();
238 String url = getServiceRootURL();
239 final String entity = MALFORMED_XML_DATA; // Constant from base class.
240 int statusCode = submitRequest(method, url, entity);
242 // Check the status code of the response: does it match
243 // the expected response(s)?
244 if(logger.isDebugEnabled()){
245 logger.debug(testName + ": url=" + url +
246 " status=" + statusCode);
248 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
249 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
250 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
254 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
255 dependsOnMethods = {"create", "testSubmitRequest"})
256 public void createWithWrongXmlSchema(String testName) throws Exception {
258 if (logger.isDebugEnabled()) {
259 logger.debug(testBanner(testName, CLASS_NAME));
263 setupCreateWithWrongXmlSchema();
265 // Submit the request to the service and store the response.
266 String method = REQUEST_TYPE.httpMethodName();
267 String url = getServiceRootURL();
268 final String entity = WRONG_XML_SCHEMA_DATA;
269 int statusCode = submitRequest(method, url, entity);
271 // Check the status code of the response: does it match
272 // the expected response(s)?
273 if(logger.isDebugEnabled()){
274 logger.debug(testName + ": url=" + url +
275 " status=" + statusCode);
277 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
278 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
279 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
283 // ---------------------------------------------------------------
284 // CRUD tests : READ tests
285 // ---------------------------------------------------------------
288 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
291 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
292 dependsOnMethods = {"create"})
293 public void read(String testName) throws Exception {
295 if (logger.isDebugEnabled()) {
296 logger.debug(testBanner(testName, CLASS_NAME));
302 AcquisitionClient client = new AcquisitionClient();
304 // Submit the request to the service and store the response.
305 ClientResponse<MultipartInput> res = client.read(knownResourceId);
306 int statusCode = res.getStatus();
308 // Check the status code of the response: does it match
309 // the expected response(s)?
310 if(logger.isDebugEnabled()){
311 logger.debug(testName + ": status = " + statusCode);
313 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
314 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
315 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
317 MultipartInput input = (MultipartInput) res.getEntity();
318 AcquisitionsCommon acquisitionObject = (AcquisitionsCommon) extractPart(input,
319 client.getCommonPartName(), AcquisitionsCommon.class);
320 Assert.assertNotNull(acquisitionObject);
322 // Verify the number and contents of values in repeatable fields,
323 // as created in the instance record used for testing.
324 List<String> acqSources =
325 acquisitionObject.getAcquisitionSources().getAcquisitionSource();
326 Assert.assertTrue(acqSources.size() > 0);
327 Assert.assertNotNull(acqSources.get(0));
329 List<String> acqDates =
330 acquisitionObject.getAcquisitionDates().getAcquisitionDate();
331 Assert.assertTrue(acqDates.size() > 0);
332 Assert.assertNotNull(acqDates.get(0));
334 List<String> owners =
335 acquisitionObject.getOwners().getOwner();
336 Assert.assertTrue(owners.size() > 0);
337 Assert.assertNotNull(owners.get(0));
342 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
345 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
346 dependsOnMethods = {"read"})
347 public void readNonExistent(String testName) throws Exception {
349 if (logger.isDebugEnabled()) {
350 logger.debug(testBanner(testName, CLASS_NAME));
354 setupReadNonExistent();
356 // Submit the request to the service and store the response.
357 AcquisitionClient client = new AcquisitionClient();
358 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
359 int statusCode = res.getStatus();
361 // Check the status code of the response: does it match
362 // the expected response(s)?
363 if(logger.isDebugEnabled()){
364 logger.debug(testName + ": status = " + statusCode);
366 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
367 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
368 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
371 // ---------------------------------------------------------------
372 // CRUD tests : READ_LIST tests
373 // ---------------------------------------------------------------
376 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
379 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
380 dependsOnMethods = {"createList", "read"})
381 public void readList(String testName) throws Exception {
383 if (logger.isDebugEnabled()) {
384 logger.debug(testBanner(testName, CLASS_NAME));
390 // Submit the request to the service and store the response.
391 AcquisitionClient client = new AcquisitionClient();
392 ClientResponse<AbstractCommonList> res = client.readList();
393 AbstractCommonList list = res.getEntity();
394 int statusCode = res.getStatus();
396 // Check the status code of the response: does it match
397 // the expected response(s)?
398 if(logger.isDebugEnabled()){
399 logger.debug(testName + ": status = " + statusCode);
401 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
402 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
403 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
405 // Optionally output additional data about list members for debugging.
406 boolean iterateThroughList = false;
407 if(iterateThroughList && logger.isDebugEnabled()){
408 List<AbstractCommonList.ListItem> items =
411 for(AbstractCommonList.ListItem item : items){
412 logger.debug(testName + ": list-item[" + i + "] " +
423 // ---------------------------------------------------------------
424 // CRUD tests : UPDATE tests
425 // ---------------------------------------------------------------
429 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
432 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
433 dependsOnMethods = {"read"})
434 public void update(String testName) throws Exception {
436 if (logger.isDebugEnabled()) {
437 logger.debug(testBanner(testName, CLASS_NAME));
443 // Retrieve the contents of a resource to update.
444 AcquisitionClient client = new AcquisitionClient();
445 ClientResponse<MultipartInput> res = client.read(knownResourceId);
446 if(logger.isDebugEnabled()){
447 logger.debug(testName + ": read status = " + res.getStatus());
449 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
451 if(logger.isDebugEnabled()){
452 logger.debug("got object to update with ID: " + knownResourceId);
454 MultipartInput input = (MultipartInput) res.getEntity();
456 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
457 client.getCommonPartName(), AcquisitionsCommon.class);
458 Assert.assertNotNull(acquisition);
460 // Update the content of this resource.
461 acquisition.setAcquisitionReferenceNumber("updated-" + acquisition.getAcquisitionReferenceNumber());
462 if(logger.isDebugEnabled()){
463 logger.debug("updated object");
464 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
466 // Submit the request to the service and store the response.
467 MultipartOutput output = new MultipartOutput();
468 OutputPart commonPart = output.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
469 commonPart.getHeaders().add("label", client.getCommonPartName());
471 res = client.update(knownResourceId, output);
472 int statusCode = res.getStatus();
473 // Check the status code of the response: does it match the expected response(s)?
474 if(logger.isDebugEnabled()){
475 logger.debug(testName + ": status = " + statusCode);
477 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
478 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
479 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
482 input = (MultipartInput) res.getEntity();
483 AcquisitionsCommon updatedAcquisition =
484 (AcquisitionsCommon) extractPart(input,
485 client.getCommonPartName(), AcquisitionsCommon.class);
486 Assert.assertNotNull(updatedAcquisition);
488 Assert.assertEquals(updatedAcquisition.getAcquisitionReferenceNumber(),
489 acquisition.getAcquisitionReferenceNumber(),
490 "Data in updated object did not match submitted data.");
495 // Placeholders until the three tests below can be uncommented.
496 // See Issue CSPACE-401.
498 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
501 public void updateWithEmptyEntityBody(String testName) throws Exception {
502 //Should this really be empty?
506 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
509 public void updateWithMalformedXml(String testName) throws Exception {
510 //Should this really be empty?
514 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
517 public void updateWithWrongXmlSchema(String testName) throws Exception {
518 //Should this really be empty?
523 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
524 dependsOnMethods = {"create", "update", "testSubmitRequest"})
525 public void updateWithEmptyEntityBody(String testName) throws Exception {
527 if (logger.isDebugEnabled()) {
528 logger.debug(testBanner(testName, CLASS_NAME));
532 setupUpdateWithEmptyEntityBody();
534 // Submit the request to the service and store the response.
535 String method = REQUEST_TYPE.httpMethodName();
536 String url = getResourceURL(knownResourceId);
537 String mediaType = MediaType.APPLICATION_XML;
538 final String entity = "";
539 int statusCode = submitRequest(method, url, mediaType, entity);
541 // Check the status code of the response: does it match
542 // the expected response(s)?
543 if(logger.isDebugEnabled()){
544 (testName + ": url=" + url + " status=" + statusCode);
546 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
547 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
548 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
552 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
553 dependsOnMethods = {"create", "testSubmitRequest"})
554 public void createWithEmptyEntityBody(String testName) throws Exception {
556 if (logger.isDebugEnabled()) {
557 logger.debug(testBanner(testName, CLASS_NAME));
561 setupCreateWithEmptyEntityBody();
563 // Submit the request to the service and store the response.
564 String method = REQUEST_TYPE.httpMethodName();
565 String url = getServiceRootURL();
566 String mediaType = MediaType.APPLICATION_XML;
567 final String entity = "";
568 int statusCode = submitRequest(method, url, mediaType, entity);
570 // Check the status code of the response: does it match
571 // the expected response(s)?
572 if(logger.isDebugEnabled()){
573 logger.debug(testName + ": url=" + url +
574 " status=" + statusCode);
576 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
577 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
578 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
582 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
583 dependsOnMethods = {"create", "update", "testSubmitRequest"})
584 public void updateWithMalformedXml(String testName) throws Exception {
586 if (logger.isDebugEnabled()) {
587 logger.debug(testBanner(testName, CLASS_NAME));
591 setupUpdateWithMalformedXml();
593 // Submit the request to the service and store the response.
594 String method = REQUEST_TYPE.httpMethodName();
595 String url = getResourceURL(knownResourceId);
596 final String entity = MALFORMED_XML_DATA;
597 int statusCode = submitRequest(method, url, entity);
599 // Check the status code of the response: does it match
600 // the expected response(s)?
601 if(logger.isDebugEnabled()){
602 logger.debug(testName + ": url=" + url +
603 " status=" + statusCode);
605 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
606 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
607 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
611 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
612 public void updateWithWrongXmlSchema(String testName) {
614 if (logger.isDebugEnabled()) {
615 logger.debug(testBanner(testName, CLASS_NAME));
619 setupUpdateWithWrongXmlSchema();
621 // Submit the request to the service and store the response.
622 String method = REQUEST_TYPE.httpMethodName();
623 String url = getResourceURL(knownResourceId);
624 final String entity = WRONG_XML_SCHEMA_DATA;
625 int statusCode = submitRequest(method, url, entity);
627 // Check the status code of the response: does it match
628 // the expected response(s)?
629 if(logger.isDebugEnabled()){
630 logger.debug(testName + ": url=" + url +
631 " status=" + statusCode);
633 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
634 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
635 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
640 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
643 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
644 dependsOnMethods = {"update", "testSubmitRequest"})
645 public void updateNonExistent(String testName) throws Exception {
647 if (logger.isDebugEnabled()) {
648 logger.debug(testBanner(testName, CLASS_NAME));
652 setupUpdateNonExistent();
654 // Submit the request to the service and store the response.
655 // Note: The ID used in this 'create' call may be arbitrary.
656 // The only relevant ID may be the one used in update(), below.
657 AcquisitionClient client = new AcquisitionClient();
658 MultipartOutput multipart = createAcquisitionInstance(NON_EXISTENT_ID);
659 ClientResponse<MultipartInput> res =
660 client.update(NON_EXISTENT_ID, multipart);
661 int statusCode = res.getStatus();
663 // Check the status code of the response: does it match
664 // the expected response(s)?
665 if(logger.isDebugEnabled()){
666 logger.debug(testName + ": status = " + statusCode);
668 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
669 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
670 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
673 // ---------------------------------------------------------------
674 // CRUD tests : DELETE tests
675 // ---------------------------------------------------------------
678 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
681 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
682 dependsOnMethods = {"create", "read", "update"})
683 public void delete(String testName) throws Exception {
685 if (logger.isDebugEnabled()) {
686 logger.debug(testBanner(testName, CLASS_NAME));
692 // Submit the request to the service and store the response.
693 AcquisitionClient client = new AcquisitionClient();
694 ClientResponse<Response> res = client.delete(knownResourceId);
695 int statusCode = res.getStatus();
697 // Check the status code of the response: does it match
698 // the expected response(s)?
699 if(logger.isDebugEnabled()){
700 logger.debug(testName + ": status = " + statusCode);
702 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
703 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
704 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
709 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
712 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
713 dependsOnMethods = {"delete"})
714 public void deleteNonExistent(String testName) throws Exception {
716 if (logger.isDebugEnabled()) {
717 logger.debug(testBanner(testName, CLASS_NAME));
721 setupDeleteNonExistent();
723 // Submit the request to the service and store the response.
724 AcquisitionClient client = new AcquisitionClient();
725 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
726 int statusCode = res.getStatus();
728 // Check the status code of the response: does it match
729 // the expected response(s)?
730 if(logger.isDebugEnabled()){
731 logger.debug(testName + ": status = " + statusCode);
733 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
734 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
735 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
738 // ---------------------------------------------------------------
739 // Utility tests : tests of code used in tests above
740 // ---------------------------------------------------------------
742 * Tests the code for manually submitting data that is used by several
743 * of the methods above.
747 @Test(dependsOnMethods = {"create", "read"})
748 public void testSubmitRequest() throws Exception {
749 testSubmitRequest(knownResourceId);
753 * Test submit request.
755 * @param resourceId the resource id
756 * @throws Exception the exception
758 private void testSubmitRequest(String resourceId) throws Exception {
760 // Expected status code: 200 OK
761 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
763 // Submit the request to the service and store the response.
764 String method = ServiceRequestType.READ.httpMethodName();
765 String url = getResourceURL(resourceId);
766 int statusCode = submitRequest(method, url);
768 // Check the status code of the response: does it match
769 // the expected response(s)?
770 if (logger.isDebugEnabled()) {
771 logger.debug("testSubmitRequest: url=" + url
772 + " status=" + statusCode);
774 Assert.assertEquals(statusCode, EXPECTED_STATUS);
778 // ---------------------------------------------------------------
779 // Utility methods used by tests above
780 // ---------------------------------------------------------------
782 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
785 public String getServicePathComponent() {
786 return new AcquisitionClient().getServicePathComponent();
791 * Creates the acquisition instance.
793 * @param identifier the identifier
794 * @return the multipart output
796 private MultipartOutput createAcquisitionInstance(String identifier) {
797 AcquisitionsCommon acquisition = new AcquisitionsCommon();
798 acquisition.setAcquisitionReferenceNumber("acquisitionReferenceNumber-" + identifier);
800 AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
801 List<String> acqSources = acqSourcesList.getAcquisitionSource();
802 // FIXME Use properly formatted refNames for representative acquisition
803 // sources in this example test record. The following are mere placeholders.
804 acqSources.add("Donor Acquisition Source-" + identifier);
805 acqSources.add("Museum Acquisition Source-" + identifier);
806 acquisition.setAcquisitionSources(acqSourcesList);
808 AcquisitionDateList acqDatesList = new AcquisitionDateList();
809 List<String> acqDates = acqDatesList.getAcquisitionDate();
810 // FIXME Use properly timestamps for representative acquisition
811 // dates in this example test record. The following are mere placeholders.
812 acqDates.add("First Acquisition Date -" + identifier);
813 acqDates.add("Second Acquisition Date-" + identifier);
814 acquisition.setAcquisitionDates(acqDatesList);
816 OwnerList ownersList = new OwnerList();
817 List<String> owners = ownersList.getOwner();
818 // FIXME Use properly formatted refNames for representative owners
819 // in this example test record. The following are mere placeholders.
820 owners.add("First Owner -" + identifier);
821 owners.add("Second Owner-" + identifier);
822 acquisition.setOwners(ownersList);
824 MultipartOutput multipart = new MultipartOutput();
825 OutputPart commonPart = multipart.addPart(acquisition,
826 MediaType.APPLICATION_XML_TYPE);
827 commonPart.getHeaders().add("label", new AcquisitionClient().getCommonPartName());
829 if(logger.isDebugEnabled()){
830 logger.debug("to be created, acquisition common");
831 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
836 // FIXME: The following methods might be made generic and moved to a common package.
839 * Retrives an XML document from the given file, and uses
840 * the JAXB unmarshaller to create a Java object representation
841 * and ultimately a multipart payload that can be submitted in
842 * a create or update request.
844 * @param commonPartName
845 * @param commonPartFileName
849 private MultipartOutput createAcquisitionInstanceFromXml(String testName, String commonPartName,
850 String commonPartFileName) throws Exception {
852 AcquisitionsCommon acquisition =
853 (AcquisitionsCommon) getObjectFromFile(AcquisitionsCommon.class,
855 MultipartOutput multipart = new MultipartOutput();
856 OutputPart commonPart = multipart.addPart(acquisition,
857 MediaType.APPLICATION_XML_TYPE);
858 commonPart.getHeaders().add("label", commonPartName);
860 if (logger.isDebugEnabled()) {
861 logger.debug(testName + " to be created, acquisitions common");
862 logger.debug(objectAsXmlString(acquisition,
863 AcquisitionsCommon.class));
870 * Creates a record / resource from the data in an XML file.
872 * @param testName the test name
873 * @param fileName the file name
874 * @param useJaxb the use jaxb
876 * @throws Exception the exception
878 private String createFromXmlFile(String testName, String fileName, boolean useJaxb) throws Exception {
883 MultipartOutput multipart = null;
885 AcquisitionClient client = new AcquisitionClient();
887 multipart = createAcquisitionInstanceFromXml(testName,
888 client.getCommonPartName(), fileName);
891 multipart = createAcquisitionInstanceFromRawXml(testName,
892 client.getCommonPartName(), fileName);
894 ClientResponse<Response> res = client.create(multipart);
895 int statusCode = res.getStatus();
897 if (logger.isDebugEnabled()) {
898 logger.debug(testName + ": status = " + statusCode);
900 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
901 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
902 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
903 String newId = extractId(res);
904 allResourceIdsCreated.add(newId);
909 * Returns a multipart payload that can be submitted with a
910 * create or update request, by reading from an XML file.
912 * @param commonPartName
913 * @param commonPartFileName
917 private MultipartOutput createAcquisitionInstanceFromRawXml(String testName, String commonPartName,
918 String commonPartFileName) throws Exception {
920 MultipartOutput multipart = new MultipartOutput();
921 String stringObject = getXmlDocumentAsString(commonPartFileName);
922 if (logger.isDebugEnabled()) {
923 logger.debug(testName + " to be created, acquisition common " + "\n" + stringObject);
925 OutputPart commonPart = multipart.addPart(stringObject,
926 MediaType.APPLICATION_XML_TYPE);
927 commonPart.getHeaders().add("label", commonPartName);
933 // FIXME: This duplicates code in read(), and should be consolidated.
934 // This is an expedient to support reading and verifying the contents
935 // of resources that have been created from test data XML files.
936 private AcquisitionsCommon readAcquisitionCommonPart(String csid)
939 String testName = "readAcquisitionCommonPart";
943 // Submit the request to the service and store the response.
944 AcquisitionClient client = new AcquisitionClient();
945 ClientResponse<MultipartInput> res = client.read(csid);
946 int statusCode = res.getStatus();
948 // Check the status code of the response: does it match
949 // the expected response(s)?
950 if (logger.isDebugEnabled()) {
951 logger.debug(testName + ": status = " + statusCode);
953 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
954 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
955 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
957 MultipartInput input = (MultipartInput) res.getEntity();
959 if (logger.isDebugEnabled()) {
960 logger.debug(testName + ": Reading Common part ...");
962 AcquisitionsCommon acquisition =
963 (AcquisitionsCommon) extractPart(input,
964 client.getCommonPartName(), AcquisitionsCommon.class);
965 Assert.assertNotNull(acquisition);