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;
31 import org.collectionspace.services.jaxb.AbstractCommonList;
33 import org.collectionspace.services.acquisition.AcquisitionsCommon;
34 import org.collectionspace.services.acquisition.AcquisitionsCommonList;
35 import org.collectionspace.services.acquisition.AcquisitionDateList;
36 import org.collectionspace.services.acquisition.AcquisitionFunding;
37 import org.collectionspace.services.acquisition.AcquisitionFundingList;
38 import org.collectionspace.services.acquisition.AcquisitionSourceList;
39 import org.collectionspace.services.acquisition.OwnerList;
40 import org.jboss.resteasy.client.ClientResponse;
42 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
43 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
44 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
45 import org.testng.Assert;
46 import org.testng.annotations.Test;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * AcquisitionServiceTest, carries out tests against a
53 * deployed and running Acquisition Service.
55 * $LastChangedRevision: 621 $
56 * $LastChangedDate: 2009-09-02 16:49:01 -0700 (Wed, 02 Sep 2009) $
58 public class AcquisitionServiceTest extends AbstractServiceTestImpl {
61 private final String CLASS_NAME = AcquisitionServiceTest.class.getName();
62 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
64 // Instance variables specific to this test.
65 /** The known resource id. */
66 private String knownResourceId = null;
69 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
72 protected CollectionSpaceClient getClientInstance() {
73 return new AcquisitionClient();
77 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
80 protected AbstractCommonList getAbstractCommonList(
81 ClientResponse<AbstractCommonList> response) {
82 return response.getEntity(AcquisitionsCommonList.class);
85 // ---------------------------------------------------------------
86 // CRUD tests : CREATE tests
87 // ---------------------------------------------------------------
90 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
93 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
94 public void create(String testName) throws Exception {
96 if (logger.isDebugEnabled()) {
97 logger.debug(testBanner(testName, CLASS_NAME));
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 String identifier = createIdentifier();
108 AcquisitionClient client = new AcquisitionClient();
109 MultipartOutput multipart = createAcquisitionInstance(identifier);
110 ClientResponse<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(REQUEST_TYPE.isValidStatusCode(statusCode),
124 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
125 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
127 // Store the ID returned from the first resource created
128 // for additional tests below.
129 if (knownResourceId == null){
130 knownResourceId = extractId(res);
131 if (logger.isDebugEnabled()) {
132 logger.debug(testName + ": knownResourceId=" + knownResourceId);
136 // Store the IDs from every resource created by tests,
137 // so they can be deleted after tests have been run.
138 allResourceIdsCreated.add(extractId(res));
142 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
145 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
146 dependsOnMethods = {"create"})
147 public void createList(String testName) throws Exception {
148 for(int i = 0; i < 3; i++){
154 * Tests to diagnose and fix CSPACE-2578.
156 * This is a bug identified in release 1.0 alpha, after first implementing an
157 * Acquisition Funding repeatable group of fields, in which record creation
158 * fails if there is whitespace (or more generally, a text node) between
159 * the acquisitionFunding container element and its first child field.
162 // Verify that record creation occurs successfully when there is NO whitespace
163 // between the acquisitionFunding tag and its first child element tag
164 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
165 dependsOnMethods = {"create", "testSubmitRequest"}, groups = {"cspace2578group"})
166 public void createFromXmlNoWhitespaceAfterRepeatableGroupTag(String testName) throws Exception {
167 if (logger.isDebugEnabled()) {
168 logger.debug(testBanner(testName, CLASS_NAME));
170 String testDataDir = System.getProperty("test-data.fileName");
172 createFromXmlFile(testName, testDataDir + "/cspace-2578-no-whitespace.xml", false);
173 testSubmitRequest(newId);
176 // Verify that record creation occurs successfully when there is whitespace
177 // between the acquisitionFunding tag and its first child element tag
179 // FIXME: This test currently fails. @Test annotation is currently commented
180 // out for check-in, to prevent service tests from failing. Can uncomment to test
181 // fixes, and also after the issue is resolved, to help detect any regressions.
183 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
184 dependsOnMethods = {"create", "testSubmitRequest"}, groups = {"cspace2578group"})
185 public void createFromXmlWhitespaceAfterRepeatableGroupTag(String testName) throws Exception {
186 if (logger.isDebugEnabled()) {
187 logger.debug(testBanner(testName, CLASS_NAME));
189 String testDataDir = System.getProperty("test-data.fileName");
191 createFromXmlFile(testName, testDataDir + "/cspace-2578-whitespace.xml", false);
192 AcquisitionsCommon acquisition = readAcquisitionCommonPart(newId);
193 testSubmitRequest(newId);
198 // Placeholders until the three tests below can be uncommented.
199 // See Issue CSPACE-401.
201 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
204 public void createWithEmptyEntityBody(String testName) throws Exception {
205 //Should this really be empty?
209 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
212 public void createWithMalformedXml(String testName) throws Exception {
213 //Should this really be empty?
217 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
220 public void createWithWrongXmlSchema(String testName) throws Exception {
221 //Should this really be empty?
226 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
227 dependsOnMethods = {"create", "testSubmitRequest"})
228 public void createWithMalformedXml(String testName) throws Exception {
230 if (logger.isDebugEnabled()) {
231 logger.debug(testBanner(testName, CLASS_NAME));
235 setupCreateWithMalformedXml();
237 // Submit the request to the service and store the response.
238 String method = REQUEST_TYPE.httpMethodName();
239 String url = getServiceRootURL();
240 final String entity = MALFORMED_XML_DATA; // Constant from base class.
241 int statusCode = submitRequest(method, url, entity);
243 // Check the status code of the response: does it match
244 // the expected response(s)?
245 if(logger.isDebugEnabled()){
246 logger.debug(testName + ": url=" + url +
247 " status=" + statusCode);
249 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
250 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
251 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
255 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
256 dependsOnMethods = {"create", "testSubmitRequest"})
257 public void createWithWrongXmlSchema(String testName) throws Exception {
259 if (logger.isDebugEnabled()) {
260 logger.debug(testBanner(testName, CLASS_NAME));
264 setupCreateWithWrongXmlSchema();
266 // Submit the request to the service and store the response.
267 String method = REQUEST_TYPE.httpMethodName();
268 String url = getServiceRootURL();
269 final String entity = WRONG_XML_SCHEMA_DATA;
270 int statusCode = submitRequest(method, url, entity);
272 // Check the status code of the response: does it match
273 // the expected response(s)?
274 if(logger.isDebugEnabled()){
275 logger.debug(testName + ": url=" + url +
276 " status=" + statusCode);
278 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
279 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
280 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
284 // ---------------------------------------------------------------
285 // CRUD tests : READ tests
286 // ---------------------------------------------------------------
289 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
292 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
293 dependsOnMethods = {"create"})
294 public void read(String testName) throws Exception {
296 if (logger.isDebugEnabled()) {
297 logger.debug(testBanner(testName, CLASS_NAME));
303 AcquisitionClient client = new AcquisitionClient();
305 // Submit the request to the service and store the response.
306 ClientResponse<MultipartInput> res = client.read(knownResourceId);
307 int statusCode = res.getStatus();
309 // Check the status code of the response: does it match
310 // the expected response(s)?
311 if(logger.isDebugEnabled()){
312 logger.debug(testName + ": status = " + statusCode);
314 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
315 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
316 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
318 MultipartInput input = (MultipartInput) res.getEntity();
319 AcquisitionsCommon acquisitionObject = (AcquisitionsCommon) extractPart(input,
320 client.getCommonPartName(), AcquisitionsCommon.class);
321 Assert.assertNotNull(acquisitionObject);
323 // Verify the number and contents of values in repeatable fields,
324 // as created in the instance record used for testing.
325 List<String> acqSources =
326 acquisitionObject.getAcquisitionSources().getAcquisitionSource();
327 Assert.assertTrue(acqSources.size() > 0);
328 Assert.assertNotNull(acqSources.get(0));
330 List<String> acqDates =
331 acquisitionObject.getAcquisitionDates().getAcquisitionDate();
332 Assert.assertTrue(acqDates.size() > 0);
333 Assert.assertNotNull(acqDates.get(0));
335 List<String> owners =
336 acquisitionObject.getOwners().getOwner();
337 Assert.assertTrue(owners.size() > 0);
338 Assert.assertNotNull(owners.get(0));
343 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
346 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
347 dependsOnMethods = {"read"})
348 public void readNonExistent(String testName) throws Exception {
350 if (logger.isDebugEnabled()) {
351 logger.debug(testBanner(testName, CLASS_NAME));
355 setupReadNonExistent();
357 // Submit the request to the service and store the response.
358 AcquisitionClient client = new AcquisitionClient();
359 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
360 int statusCode = res.getStatus();
362 // Check the status code of the response: does it match
363 // the expected response(s)?
364 if(logger.isDebugEnabled()){
365 logger.debug(testName + ": status = " + statusCode);
367 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
368 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
369 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
372 // ---------------------------------------------------------------
373 // CRUD tests : READ_LIST tests
374 // ---------------------------------------------------------------
377 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
380 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
381 dependsOnMethods = {"createList", "read"})
382 public void readList(String testName) throws Exception {
384 if (logger.isDebugEnabled()) {
385 logger.debug(testBanner(testName, CLASS_NAME));
391 // Submit the request to the service and store the response.
392 AcquisitionClient client = new AcquisitionClient();
393 ClientResponse<AcquisitionsCommonList> res = client.readList();
394 AcquisitionsCommonList list = res.getEntity();
395 int statusCode = res.getStatus();
397 // Check the status code of the response: does it match
398 // the expected response(s)?
399 if(logger.isDebugEnabled()){
400 logger.debug(testName + ": status = " + statusCode);
402 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
403 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
404 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
406 // Optionally output additional data about list members for debugging.
407 boolean iterateThroughList = false;
408 if(iterateThroughList && logger.isDebugEnabled()){
409 List<AcquisitionsCommonList.AcquisitionListItem> items =
410 list.getAcquisitionListItem();
412 for(AcquisitionsCommonList.AcquisitionListItem item : items){
413 logger.debug(testName + ": list-item[" + i + "] csid=" +
415 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
416 item.getAcquisitionReferenceNumber());
417 logger.debug(testName + ": list-item[" + i + "] acquisitionSources:");
418 AcquisitionSourceList acqSource = item.getAcquisitionSources();
419 for (String acquisitionSource : acqSource.getAcquisitionSource()) {
420 logger.debug("acquisitionSource=" + acquisitionSource);
422 logger.debug(testName + ": list-item[" + i + "] URI=" +
433 // ---------------------------------------------------------------
434 // CRUD tests : UPDATE tests
435 // ---------------------------------------------------------------
439 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
442 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
443 dependsOnMethods = {"read"})
444 public void update(String testName) throws Exception {
446 if (logger.isDebugEnabled()) {
447 logger.debug(testBanner(testName, CLASS_NAME));
453 // Retrieve the contents of a resource to update.
454 AcquisitionClient client = new AcquisitionClient();
455 ClientResponse<MultipartInput> res = client.read(knownResourceId);
456 if(logger.isDebugEnabled()){
457 logger.debug(testName + ": read status = " + res.getStatus());
459 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
461 if(logger.isDebugEnabled()){
462 logger.debug("got object to update with ID: " + knownResourceId);
464 MultipartInput input = (MultipartInput) res.getEntity();
466 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
467 client.getCommonPartName(), AcquisitionsCommon.class);
468 Assert.assertNotNull(acquisition);
470 // Update the content of this resource.
471 acquisition.setAcquisitionReferenceNumber("updated-" + acquisition.getAcquisitionReferenceNumber());
472 if(logger.isDebugEnabled()){
473 logger.debug("updated object");
474 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
476 // Submit the request to the service and store the response.
477 MultipartOutput output = new MultipartOutput();
478 OutputPart commonPart = output.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
479 commonPart.getHeaders().add("label", client.getCommonPartName());
481 res = client.update(knownResourceId, output);
482 int statusCode = res.getStatus();
483 // Check the status code of the response: does it match the expected response(s)?
484 if(logger.isDebugEnabled()){
485 logger.debug(testName + ": status = " + statusCode);
487 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
488 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
489 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
492 input = (MultipartInput) res.getEntity();
493 AcquisitionsCommon updatedAcquisition =
494 (AcquisitionsCommon) extractPart(input,
495 client.getCommonPartName(), AcquisitionsCommon.class);
496 Assert.assertNotNull(updatedAcquisition);
498 Assert.assertEquals(updatedAcquisition.getAcquisitionReferenceNumber(),
499 acquisition.getAcquisitionReferenceNumber(),
500 "Data in updated object did not match submitted data.");
505 // Placeholders until the three tests below can be uncommented.
506 // See Issue CSPACE-401.
508 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
511 public void updateWithEmptyEntityBody(String testName) throws Exception {
512 //Should this really be empty?
516 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
519 public void updateWithMalformedXml(String testName) throws Exception {
520 //Should this really be empty?
524 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
527 public void updateWithWrongXmlSchema(String testName) throws Exception {
528 //Should this really be empty?
533 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
534 dependsOnMethods = {"create", "update", "testSubmitRequest"})
535 public void updateWithEmptyEntityBody(String testName) throws Exception {
537 if (logger.isDebugEnabled()) {
538 logger.debug(testBanner(testName, CLASS_NAME));
542 setupUpdateWithEmptyEntityBody();
544 // Submit the request to the service and store the response.
545 String method = REQUEST_TYPE.httpMethodName();
546 String url = getResourceURL(knownResourceId);
547 String mediaType = MediaType.APPLICATION_XML;
548 final String entity = "";
549 int statusCode = submitRequest(method, url, mediaType, entity);
551 // Check the status code of the response: does it match
552 // the expected response(s)?
553 if(logger.isDebugEnabled()){
554 (testName + ": url=" + url + " status=" + statusCode);
556 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
557 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
558 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
562 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
563 dependsOnMethods = {"create", "testSubmitRequest"})
564 public void createWithEmptyEntityBody(String testName) throws Exception {
566 if (logger.isDebugEnabled()) {
567 logger.debug(testBanner(testName, CLASS_NAME));
571 setupCreateWithEmptyEntityBody();
573 // Submit the request to the service and store the response.
574 String method = REQUEST_TYPE.httpMethodName();
575 String url = getServiceRootURL();
576 String mediaType = MediaType.APPLICATION_XML;
577 final String entity = "";
578 int statusCode = submitRequest(method, url, mediaType, entity);
580 // Check the status code of the response: does it match
581 // the expected response(s)?
582 if(logger.isDebugEnabled()){
583 logger.debug(testName + ": url=" + url +
584 " status=" + statusCode);
586 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
587 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
588 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
592 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
593 dependsOnMethods = {"create", "update", "testSubmitRequest"})
594 public void updateWithMalformedXml(String testName) throws Exception {
596 if (logger.isDebugEnabled()) {
597 logger.debug(testBanner(testName, CLASS_NAME));
601 setupUpdateWithMalformedXml();
603 // Submit the request to the service and store the response.
604 String method = REQUEST_TYPE.httpMethodName();
605 String url = getResourceURL(knownResourceId);
606 final String entity = MALFORMED_XML_DATA;
607 int statusCode = submitRequest(method, url, entity);
609 // Check the status code of the response: does it match
610 // the expected response(s)?
611 if(logger.isDebugEnabled()){
612 logger.debug(testName + ": url=" + url +
613 " status=" + statusCode);
615 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
616 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
617 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
621 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
622 public void updateWithWrongXmlSchema(String testName) {
624 if (logger.isDebugEnabled()) {
625 logger.debug(testBanner(testName, CLASS_NAME));
629 setupUpdateWithWrongXmlSchema();
631 // Submit the request to the service and store the response.
632 String method = REQUEST_TYPE.httpMethodName();
633 String url = getResourceURL(knownResourceId);
634 final String entity = WRONG_XML_SCHEMA_DATA;
635 int statusCode = submitRequest(method, url, entity);
637 // Check the status code of the response: does it match
638 // the expected response(s)?
639 if(logger.isDebugEnabled()){
640 logger.debug(testName + ": url=" + url +
641 " status=" + statusCode);
643 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
644 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
645 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
650 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
653 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
654 dependsOnMethods = {"update", "testSubmitRequest"})
655 public void updateNonExistent(String testName) throws Exception {
657 if (logger.isDebugEnabled()) {
658 logger.debug(testBanner(testName, CLASS_NAME));
662 setupUpdateNonExistent();
664 // Submit the request to the service and store the response.
665 // Note: The ID used in this 'create' call may be arbitrary.
666 // The only relevant ID may be the one used in update(), below.
667 AcquisitionClient client = new AcquisitionClient();
668 MultipartOutput multipart = createAcquisitionInstance(NON_EXISTENT_ID);
669 ClientResponse<MultipartInput> res =
670 client.update(NON_EXISTENT_ID, multipart);
671 int statusCode = res.getStatus();
673 // Check the status code of the response: does it match
674 // the expected response(s)?
675 if(logger.isDebugEnabled()){
676 logger.debug(testName + ": status = " + statusCode);
678 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
679 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
680 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
683 // ---------------------------------------------------------------
684 // CRUD tests : DELETE tests
685 // ---------------------------------------------------------------
688 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
691 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
692 dependsOnMethods = {"create", "read", "update"})
693 public void delete(String testName) throws Exception {
695 if (logger.isDebugEnabled()) {
696 logger.debug(testBanner(testName, CLASS_NAME));
702 // Submit the request to the service and store the response.
703 AcquisitionClient client = new AcquisitionClient();
704 ClientResponse<Response> res = client.delete(knownResourceId);
705 int statusCode = res.getStatus();
707 // Check the status code of the response: does it match
708 // the expected response(s)?
709 if(logger.isDebugEnabled()){
710 logger.debug(testName + ": status = " + statusCode);
712 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
713 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
714 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
719 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
722 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
723 dependsOnMethods = {"delete"})
724 public void deleteNonExistent(String testName) throws Exception {
726 if (logger.isDebugEnabled()) {
727 logger.debug(testBanner(testName, CLASS_NAME));
731 setupDeleteNonExistent();
733 // Submit the request to the service and store the response.
734 AcquisitionClient client = new AcquisitionClient();
735 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
736 int statusCode = res.getStatus();
738 // Check the status code of the response: does it match
739 // the expected response(s)?
740 if(logger.isDebugEnabled()){
741 logger.debug(testName + ": status = " + statusCode);
743 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
744 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
745 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
748 // ---------------------------------------------------------------
749 // Utility tests : tests of code used in tests above
750 // ---------------------------------------------------------------
752 * Tests the code for manually submitting data that is used by several
753 * of the methods above.
757 @Test(dependsOnMethods = {"create", "read"})
758 public void testSubmitRequest() throws Exception {
759 testSubmitRequest(knownResourceId);
763 * Test submit request.
765 * @param resourceId the resource id
766 * @throws Exception the exception
768 private void testSubmitRequest(String resourceId) throws Exception {
770 // Expected status code: 200 OK
771 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
773 // Submit the request to the service and store the response.
774 String method = ServiceRequestType.READ.httpMethodName();
775 String url = getResourceURL(resourceId);
776 int statusCode = submitRequest(method, url);
778 // Check the status code of the response: does it match
779 // the expected response(s)?
780 if (logger.isDebugEnabled()) {
781 logger.debug("testSubmitRequest: url=" + url
782 + " status=" + statusCode);
784 Assert.assertEquals(statusCode, EXPECTED_STATUS);
788 // ---------------------------------------------------------------
789 // Utility methods used by tests above
790 // ---------------------------------------------------------------
792 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
795 public String getServicePathComponent() {
796 return new AcquisitionClient().getServicePathComponent();
801 * Creates the acquisition instance.
803 * @param identifier the identifier
804 * @return the multipart output
806 private MultipartOutput createAcquisitionInstance(String identifier) {
807 AcquisitionsCommon acquisition = new AcquisitionsCommon();
808 acquisition.setAcquisitionReferenceNumber("acquisitionReferenceNumber-" + identifier);
810 AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
811 List<String> acqSources = acqSourcesList.getAcquisitionSource();
812 // FIXME Use properly formatted refNames for representative acquisition
813 // sources in this example test record. The following are mere placeholders.
814 acqSources.add("Donor Acquisition Source-" + identifier);
815 acqSources.add("Museum Acquisition Source-" + identifier);
816 acquisition.setAcquisitionSources(acqSourcesList);
818 AcquisitionDateList acqDatesList = new AcquisitionDateList();
819 List<String> acqDates = acqDatesList.getAcquisitionDate();
820 // FIXME Use properly timestamps for representative acquisition
821 // dates in this example test record. The following are mere placeholders.
822 acqDates.add("First Acquisition Date -" + identifier);
823 acqDates.add("Second Acquisition Date-" + identifier);
824 acquisition.setAcquisitionDates(acqDatesList);
826 OwnerList ownersList = new OwnerList();
827 List<String> owners = ownersList.getOwner();
828 // FIXME Use properly formatted refNames for representative owners
829 // in this example test record. The following are mere placeholders.
830 owners.add("First Owner -" + identifier);
831 owners.add("Second Owner-" + identifier);
832 acquisition.setOwners(ownersList);
834 MultipartOutput multipart = new MultipartOutput();
835 OutputPart commonPart = multipart.addPart(acquisition,
836 MediaType.APPLICATION_XML_TYPE);
837 commonPart.getHeaders().add("label", new AcquisitionClient().getCommonPartName());
839 if(logger.isDebugEnabled()){
840 logger.debug("to be created, acquisition common");
841 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
846 // FIXME: The following methods might be made generic and moved to a common package.
849 * Retrives an XML document from the given file, and uses
850 * the JAXB unmarshaller to create a Java object representation
851 * and ultimately a multipart payload that can be submitted in
852 * a create or update request.
854 * @param commonPartName
855 * @param commonPartFileName
859 private MultipartOutput createAcquisitionInstanceFromXml(String testName, String commonPartName,
860 String commonPartFileName) throws Exception {
862 AcquisitionsCommon acquisition =
863 (AcquisitionsCommon) getObjectFromFile(AcquisitionsCommon.class,
865 MultipartOutput multipart = new MultipartOutput();
866 OutputPart commonPart = multipart.addPart(acquisition,
867 MediaType.APPLICATION_XML_TYPE);
868 commonPart.getHeaders().add("label", commonPartName);
870 if (logger.isDebugEnabled()) {
871 logger.debug(testName + " to be created, acquisitions common");
872 logger.debug(objectAsXmlString(acquisition,
873 AcquisitionsCommon.class));
880 * Creates a record / resource from the data in an XML file.
882 * @param testName the test name
883 * @param fileName the file name
884 * @param useJaxb the use jaxb
886 * @throws Exception the exception
888 private String createFromXmlFile(String testName, String fileName, boolean useJaxb) throws Exception {
893 MultipartOutput multipart = null;
895 AcquisitionClient client = new AcquisitionClient();
897 multipart = createAcquisitionInstanceFromXml(testName,
898 client.getCommonPartName(), fileName);
901 multipart = createAcquisitionInstanceFromRawXml(testName,
902 client.getCommonPartName(), fileName);
904 ClientResponse<Response> res = client.create(multipart);
905 int statusCode = res.getStatus();
907 if (logger.isDebugEnabled()) {
908 logger.debug(testName + ": status = " + statusCode);
910 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
911 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
912 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
913 String newId = extractId(res);
914 allResourceIdsCreated.add(newId);
919 * Returns a multipart payload that can be submitted with a
920 * create or update request, by reading from an XML file.
922 * @param commonPartName
923 * @param commonPartFileName
927 private MultipartOutput createAcquisitionInstanceFromRawXml(String testName, String commonPartName,
928 String commonPartFileName) throws Exception {
930 MultipartOutput multipart = new MultipartOutput();
931 String stringObject = getXmlDocumentAsString(commonPartFileName);
932 if (logger.isDebugEnabled()) {
933 logger.debug(testName + " to be created, acquisition common " + "\n" + stringObject);
935 OutputPart commonPart = multipart.addPart(stringObject,
936 MediaType.APPLICATION_XML_TYPE);
937 commonPart.getHeaders().add("label", commonPartName);
943 // FIXME: This duplicates code in read(), and should be consolidated.
944 // This is an expedient to support reading and verifying the contents
945 // of resources that have been created from test data XML files.
946 private AcquisitionsCommon readAcquisitionCommonPart(String csid)
949 String testName = "readAcquisitionCommonPart";
953 // Submit the request to the service and store the response.
954 AcquisitionClient client = new AcquisitionClient();
955 ClientResponse<MultipartInput> res = client.read(csid);
956 int statusCode = res.getStatus();
958 // Check the status code of the response: does it match
959 // the expected response(s)?
960 if (logger.isDebugEnabled()) {
961 logger.debug(testName + ": status = " + statusCode);
963 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
964 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
965 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
967 MultipartInput input = (MultipartInput) res.getEntity();
969 if (logger.isDebugEnabled()) {
970 logger.debug(testName + ": Reading Common part ...");
972 AcquisitionsCommon acquisition =
973 (AcquisitionsCommon) extractPart(input,
974 client.getCommonPartName(), AcquisitionsCommon.class);
975 Assert.assertNotNull(acquisition);