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 + "] acquisitionSource=" +
418 item.getAcquisitionSource());
419 logger.debug(testName + ": list-item[" + i + "] owner=" +
421 logger.debug(testName + ": list-item[" + i + "] URI=" +
432 // ---------------------------------------------------------------
433 // CRUD tests : UPDATE tests
434 // ---------------------------------------------------------------
438 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
441 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
442 dependsOnMethods = {"read"})
443 public void update(String testName) throws Exception {
445 if (logger.isDebugEnabled()) {
446 logger.debug(testBanner(testName, CLASS_NAME));
452 // Retrieve the contents of a resource to update.
453 AcquisitionClient client = new AcquisitionClient();
454 ClientResponse<MultipartInput> res = client.read(knownResourceId);
455 if(logger.isDebugEnabled()){
456 logger.debug(testName + ": read status = " + res.getStatus());
458 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
460 if(logger.isDebugEnabled()){
461 logger.debug("got object to update with ID: " + knownResourceId);
463 MultipartInput input = (MultipartInput) res.getEntity();
465 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
466 client.getCommonPartName(), AcquisitionsCommon.class);
467 Assert.assertNotNull(acquisition);
469 // Update the content of this resource.
470 acquisition.setAcquisitionReferenceNumber("updated-" + acquisition.getAcquisitionReferenceNumber());
471 if(logger.isDebugEnabled()){
472 logger.debug("updated object");
473 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
475 // Submit the request to the service and store the response.
476 MultipartOutput output = new MultipartOutput();
477 OutputPart commonPart = output.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
478 commonPart.getHeaders().add("label", client.getCommonPartName());
480 res = client.update(knownResourceId, output);
481 int statusCode = res.getStatus();
482 // Check the status code of the response: does it match the expected response(s)?
483 if(logger.isDebugEnabled()){
484 logger.debug(testName + ": status = " + statusCode);
486 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
487 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
488 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
491 input = (MultipartInput) res.getEntity();
492 AcquisitionsCommon updatedAcquisition =
493 (AcquisitionsCommon) extractPart(input,
494 client.getCommonPartName(), AcquisitionsCommon.class);
495 Assert.assertNotNull(updatedAcquisition);
497 Assert.assertEquals(updatedAcquisition.getAcquisitionReferenceNumber(),
498 acquisition.getAcquisitionReferenceNumber(),
499 "Data in updated object did not match submitted data.");
504 // Placeholders until the three tests below can be uncommented.
505 // See Issue CSPACE-401.
507 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
510 public void updateWithEmptyEntityBody(String testName) throws Exception {
511 //Should this really be empty?
515 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
518 public void updateWithMalformedXml(String testName) throws Exception {
519 //Should this really be empty?
523 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
526 public void updateWithWrongXmlSchema(String testName) throws Exception {
527 //Should this really be empty?
532 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
533 dependsOnMethods = {"create", "update", "testSubmitRequest"})
534 public void updateWithEmptyEntityBody(String testName) throws Exception {
536 if (logger.isDebugEnabled()) {
537 logger.debug(testBanner(testName, CLASS_NAME));
541 setupUpdateWithEmptyEntityBody();
543 // Submit the request to the service and store the response.
544 String method = REQUEST_TYPE.httpMethodName();
545 String url = getResourceURL(knownResourceId);
546 String mediaType = MediaType.APPLICATION_XML;
547 final String entity = "";
548 int statusCode = submitRequest(method, url, mediaType, entity);
550 // Check the status code of the response: does it match
551 // the expected response(s)?
552 if(logger.isDebugEnabled()){
553 (testName + ": url=" + url + " status=" + statusCode);
555 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
556 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
557 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
561 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
562 dependsOnMethods = {"create", "testSubmitRequest"})
563 public void createWithEmptyEntityBody(String testName) throws Exception {
565 if (logger.isDebugEnabled()) {
566 logger.debug(testBanner(testName, CLASS_NAME));
570 setupCreateWithEmptyEntityBody();
572 // Submit the request to the service and store the response.
573 String method = REQUEST_TYPE.httpMethodName();
574 String url = getServiceRootURL();
575 String mediaType = MediaType.APPLICATION_XML;
576 final String entity = "";
577 int statusCode = submitRequest(method, url, mediaType, entity);
579 // Check the status code of the response: does it match
580 // the expected response(s)?
581 if(logger.isDebugEnabled()){
582 logger.debug(testName + ": url=" + url +
583 " status=" + statusCode);
585 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
586 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
587 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
591 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
592 dependsOnMethods = {"create", "update", "testSubmitRequest"})
593 public void updateWithMalformedXml(String testName) throws Exception {
595 if (logger.isDebugEnabled()) {
596 logger.debug(testBanner(testName, CLASS_NAME));
600 setupUpdateWithMalformedXml();
602 // Submit the request to the service and store the response.
603 String method = REQUEST_TYPE.httpMethodName();
604 String url = getResourceURL(knownResourceId);
605 final String entity = MALFORMED_XML_DATA;
606 int statusCode = submitRequest(method, url, entity);
608 // Check the status code of the response: does it match
609 // the expected response(s)?
610 if(logger.isDebugEnabled()){
611 logger.debug(testName + ": url=" + url +
612 " status=" + statusCode);
614 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
615 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
616 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
620 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
621 public void updateWithWrongXmlSchema(String testName) {
623 if (logger.isDebugEnabled()) {
624 logger.debug(testBanner(testName, CLASS_NAME));
628 setupUpdateWithWrongXmlSchema();
630 // Submit the request to the service and store the response.
631 String method = REQUEST_TYPE.httpMethodName();
632 String url = getResourceURL(knownResourceId);
633 final String entity = WRONG_XML_SCHEMA_DATA;
634 int statusCode = submitRequest(method, url, entity);
636 // Check the status code of the response: does it match
637 // the expected response(s)?
638 if(logger.isDebugEnabled()){
639 logger.debug(testName + ": url=" + url +
640 " status=" + statusCode);
642 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
643 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
644 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
649 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
652 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
653 dependsOnMethods = {"update", "testSubmitRequest"})
654 public void updateNonExistent(String testName) throws Exception {
656 if (logger.isDebugEnabled()) {
657 logger.debug(testBanner(testName, CLASS_NAME));
661 setupUpdateNonExistent();
663 // Submit the request to the service and store the response.
664 // Note: The ID used in this 'create' call may be arbitrary.
665 // The only relevant ID may be the one used in update(), below.
666 AcquisitionClient client = new AcquisitionClient();
667 MultipartOutput multipart = createAcquisitionInstance(NON_EXISTENT_ID);
668 ClientResponse<MultipartInput> res =
669 client.update(NON_EXISTENT_ID, multipart);
670 int statusCode = res.getStatus();
672 // Check the status code of the response: does it match
673 // the expected response(s)?
674 if(logger.isDebugEnabled()){
675 logger.debug(testName + ": status = " + statusCode);
677 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
678 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
679 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
682 // ---------------------------------------------------------------
683 // CRUD tests : DELETE tests
684 // ---------------------------------------------------------------
687 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
690 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
691 dependsOnMethods = {"create", "read", "update"})
692 public void delete(String testName) throws Exception {
694 if (logger.isDebugEnabled()) {
695 logger.debug(testBanner(testName, CLASS_NAME));
701 // Submit the request to the service and store the response.
702 AcquisitionClient client = new AcquisitionClient();
703 ClientResponse<Response> res = client.delete(knownResourceId);
704 int statusCode = res.getStatus();
706 // Check the status code of the response: does it match
707 // the expected response(s)?
708 if(logger.isDebugEnabled()){
709 logger.debug(testName + ": status = " + statusCode);
711 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
712 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
713 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
718 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
721 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
722 dependsOnMethods = {"delete"})
723 public void deleteNonExistent(String testName) throws Exception {
725 if (logger.isDebugEnabled()) {
726 logger.debug(testBanner(testName, CLASS_NAME));
730 setupDeleteNonExistent();
732 // Submit the request to the service and store the response.
733 AcquisitionClient client = new AcquisitionClient();
734 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
735 int statusCode = res.getStatus();
737 // Check the status code of the response: does it match
738 // the expected response(s)?
739 if(logger.isDebugEnabled()){
740 logger.debug(testName + ": status = " + statusCode);
742 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
743 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
744 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
747 // ---------------------------------------------------------------
748 // Utility tests : tests of code used in tests above
749 // ---------------------------------------------------------------
751 * Tests the code for manually submitting data that is used by several
752 * of the methods above.
756 @Test(dependsOnMethods = {"create", "read"})
757 public void testSubmitRequest() throws Exception {
758 testSubmitRequest(knownResourceId);
762 * Test submit request.
764 * @param resourceId the resource id
765 * @throws Exception the exception
767 private void testSubmitRequest(String resourceId) throws Exception {
769 // Expected status code: 200 OK
770 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
772 // Submit the request to the service and store the response.
773 String method = ServiceRequestType.READ.httpMethodName();
774 String url = getResourceURL(resourceId);
775 int statusCode = submitRequest(method, url);
777 // Check the status code of the response: does it match
778 // the expected response(s)?
779 if (logger.isDebugEnabled()) {
780 logger.debug("testSubmitRequest: url=" + url
781 + " status=" + statusCode);
783 Assert.assertEquals(statusCode, EXPECTED_STATUS);
787 // ---------------------------------------------------------------
788 // Utility methods used by tests above
789 // ---------------------------------------------------------------
791 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
794 public String getServicePathComponent() {
795 return new AcquisitionClient().getServicePathComponent();
800 * Creates the acquisition instance.
802 * @param identifier the identifier
803 * @return the multipart output
805 private MultipartOutput createAcquisitionInstance(String identifier) {
806 AcquisitionsCommon acquisition = new AcquisitionsCommon();
807 acquisition.setAcquisitionReferenceNumber("acquisitionReferenceNumber-" + identifier);
809 AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
810 List<String> acqSources = acqSourcesList.getAcquisitionSource();
811 // FIXME Use properly formatted refNames for representative acquisition
812 // sources in this example test record. The following are mere placeholders.
813 acqSources.add("Donor Acquisition Source-" + identifier);
814 acqSources.add("Museum Acquisition Source-" + identifier);
815 acquisition.setAcquisitionSources(acqSourcesList);
817 AcquisitionDateList acqDatesList = new AcquisitionDateList();
818 List<String> acqDates = acqDatesList.getAcquisitionDate();
819 // FIXME Use properly timestamps for representative acquisition
820 // dates in this example test record. The following are mere placeholders.
821 acqDates.add("First Acquisition Date -" + identifier);
822 acqDates.add("Second Acquisition Date-" + identifier);
823 acquisition.setAcquisitionDates(acqDatesList);
825 OwnerList ownersList = new OwnerList();
826 List<String> owners = ownersList.getOwner();
827 // FIXME Use properly formatted refNames for representative owners
828 // in this example test record. The following are mere placeholders.
829 owners.add("First Owner -" + identifier);
830 owners.add("Second Owner-" + identifier);
831 acquisition.setOwners(ownersList);
833 MultipartOutput multipart = new MultipartOutput();
834 OutputPart commonPart = multipart.addPart(acquisition,
835 MediaType.APPLICATION_XML_TYPE);
836 commonPart.getHeaders().add("label", new AcquisitionClient().getCommonPartName());
838 if(logger.isDebugEnabled()){
839 logger.debug("to be created, acquisition common");
840 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
845 // FIXME: The following methods might be made generic and moved to a common package.
848 * Retrives an XML document from the given file, and uses
849 * the JAXB unmarshaller to create a Java object representation
850 * and ultimately a multipart payload that can be submitted in
851 * a create or update request.
853 * @param commonPartName
854 * @param commonPartFileName
858 private MultipartOutput createAcquisitionInstanceFromXml(String testName, String commonPartName,
859 String commonPartFileName) throws Exception {
861 AcquisitionsCommon acquisition =
862 (AcquisitionsCommon) getObjectFromFile(AcquisitionsCommon.class,
864 MultipartOutput multipart = new MultipartOutput();
865 OutputPart commonPart = multipart.addPart(acquisition,
866 MediaType.APPLICATION_XML_TYPE);
867 commonPart.getHeaders().add("label", commonPartName);
869 if (logger.isDebugEnabled()) {
870 logger.debug(testName + " to be created, acquisitions common");
871 logger.debug(objectAsXmlString(acquisition,
872 AcquisitionsCommon.class));
879 * Creates a record / resource from the data in an XML file.
881 * @param testName the test name
882 * @param fileName the file name
883 * @param useJaxb the use jaxb
885 * @throws Exception the exception
887 private String createFromXmlFile(String testName, String fileName, boolean useJaxb) throws Exception {
892 MultipartOutput multipart = null;
894 AcquisitionClient client = new AcquisitionClient();
896 multipart = createAcquisitionInstanceFromXml(testName,
897 client.getCommonPartName(), fileName);
900 multipart = createAcquisitionInstanceFromRawXml(testName,
901 client.getCommonPartName(), fileName);
903 ClientResponse<Response> res = client.create(multipart);
904 int statusCode = res.getStatus();
906 if (logger.isDebugEnabled()) {
907 logger.debug(testName + ": status = " + statusCode);
909 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
910 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
911 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
912 String newId = extractId(res);
913 allResourceIdsCreated.add(newId);
918 * Returns a multipart payload that can be submitted with a
919 * create or update request, by reading from an XML file.
921 * @param commonPartName
922 * @param commonPartFileName
926 private MultipartOutput createAcquisitionInstanceFromRawXml(String testName, String commonPartName,
927 String commonPartFileName) throws Exception {
929 MultipartOutput multipart = new MultipartOutput();
930 String stringObject = getXmlDocumentAsString(commonPartFileName);
931 if (logger.isDebugEnabled()) {
932 logger.debug(testName + " to be created, acquisition common " + "\n" + stringObject);
934 OutputPart commonPart = multipart.addPart(stringObject,
935 MediaType.APPLICATION_XML_TYPE);
936 commonPart.getHeaders().add("label", commonPartName);
942 // FIXME: This duplicates code in read(), and should be consolidated.
943 // This is an expedient to support reading and verifying the contents
944 // of resources that have been created from test data XML files.
945 private AcquisitionsCommon readAcquisitionCommonPart(String csid)
948 String testName = "readAcquisitionCommonPart";
952 // Submit the request to the service and store the response.
953 AcquisitionClient client = new AcquisitionClient();
954 ClientResponse<MultipartInput> res = client.read(csid);
955 int statusCode = res.getStatus();
957 // Check the status code of the response: does it match
958 // the expected response(s)?
959 if (logger.isDebugEnabled()) {
960 logger.debug(testName + ": status = " + statusCode);
962 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
963 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
964 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
966 MultipartInput input = (MultipartInput) res.getEntity();
968 if (logger.isDebugEnabled()) {
969 logger.debug(testName + ": Reading Common part ...");
971 AcquisitionsCommon acquisition =
972 (AcquisitionsCommon) extractPart(input,
973 client.getCommonPartName(), AcquisitionsCommon.class);
974 Assert.assertNotNull(acquisition);