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.client.PayloadOutputPart;
32 import org.collectionspace.services.client.PoxPayloadIn;
33 import org.collectionspace.services.client.PoxPayloadOut;
34 import org.collectionspace.services.common.AbstractCommonListUtils;
35 import org.collectionspace.services.common.datetime.GregorianCalendarDateTimeUtils;
36 import org.collectionspace.services.jaxb.AbstractCommonList;
38 import org.collectionspace.services.acquisition.AcquisitionsCommon;
39 import org.collectionspace.services.acquisition.AcquisitionDateList;
40 import org.collectionspace.services.acquisition.AcquisitionSourceList;
41 import org.collectionspace.services.acquisition.OwnerList;
42 import org.jboss.resteasy.client.ClientResponse;
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;
66 private final static String CURRENT_DATE_UTC =
67 GregorianCalendarDateTimeUtils.timestampUTC();
71 public String getServicePathComponent() {
72 return AcquisitionClient.SERVICE_PATH_COMPONENT;
77 protected String getServiceName() {
78 return AcquisitionClient.SERVICE_NAME;
82 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
85 protected CollectionSpaceClient getClientInstance() {
86 return new AcquisitionClient();
89 // ---------------------------------------------------------------
90 // CRUD tests : CREATE tests
91 // ---------------------------------------------------------------
94 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
97 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
98 public void create(String testName) throws Exception {
100 if (logger.isDebugEnabled()) {
101 logger.debug(testBanner(testName, CLASS_NAME));
104 // Perform setup, such as initializing the type of service request
105 // (e.g. CREATE, DELETE), its valid and expected status codes, and
106 // its associated HTTP method name (e.g. POST, DELETE).
109 // Submit the request to the service and store the response.
110 String identifier = createIdentifier();
112 AcquisitionClient client = new AcquisitionClient();
113 PoxPayloadOut multipart = createAcquisitionInstance(identifier);
114 ClientResponse<Response> res = client.create(multipart);
116 int statusCode = res.getStatus();
118 // Check the status code of the response: does it match
119 // the expected response(s)?
122 // Does it fall within the set of valid status codes?
123 // Does it exactly match the expected status code?
124 if(logger.isDebugEnabled()){
125 logger.debug(testName + ": status = " + statusCode);
127 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
128 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
129 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
131 // Store the ID returned from the first resource created
132 // for additional tests below.
133 if (knownResourceId == null){
134 knownResourceId = extractId(res);
135 if (logger.isDebugEnabled()) {
136 logger.debug(testName + ": knownResourceId=" + knownResourceId);
140 // Store the IDs from every resource created by tests,
141 // so they can be deleted after tests have been run.
142 allResourceIdsCreated.add(extractId(res));
146 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
149 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
150 dependsOnMethods = {"create"})
151 public void createList(String testName) throws Exception {
152 for(int i = 0; i < 3; i++){
158 * Tests to diagnose and fix CSPACE-2578.
160 * This is a bug identified in release 1.0 alpha, after first implementing an
161 * Acquisition Funding repeatable group of fields, in which record creation
162 * fails if there is whitespace (or more generally, a text node) between
163 * the acquisitionFunding container element and its first child field.
166 // Verify that record creation occurs successfully when there is NO whitespace
167 // between the acquisitionFunding tag and its first child element tag
168 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
169 dependsOnMethods = {"create", "testSubmitRequest"}, groups = {"cspace2578group"})
170 public void createFromXmlNoWhitespaceAfterRepeatableGroupTag(String testName) throws Exception {
171 if (logger.isDebugEnabled()) {
172 logger.debug(testBanner(testName, CLASS_NAME));
174 String testDataDir = System.getProperty("test-data.fileName");
176 createFromXmlFile(testName, testDataDir + "/cspace-2578-no-whitespace.xml", false);
177 testSubmitRequest(newId);
180 // Verify that record creation occurs successfully when there is whitespace
181 // between the acquisitionFunding tag and its first child element tag
183 // FIXME: This test currently fails. @Test annotation is currently commented
184 // out for check-in, to prevent service tests from failing. Can uncomment to test
185 // fixes, and also after the issue is resolved, to help detect any regressions.
187 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
188 dependsOnMethods = {"create", "testSubmitRequest"}, groups = {"cspace2578group"})
189 public void createFromXmlWhitespaceAfterRepeatableGroupTag(String testName) throws Exception {
190 if (logger.isDebugEnabled()) {
191 logger.debug(testBanner(testName, CLASS_NAME));
193 String testDataDir = System.getProperty("test-data.fileName");
195 createFromXmlFile(testName, testDataDir + "/cspace-2578-whitespace.xml", false);
196 AcquisitionsCommon acquisition = readAcquisitionCommonPart(newId);
197 testSubmitRequest(newId);
202 // Placeholders until the three tests below can be uncommented.
203 // See Issue CSPACE-401.
205 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
208 public void createWithEmptyEntityBody(String testName) throws Exception {
209 //Should this really be empty?
213 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
216 public void createWithMalformedXml(String testName) throws Exception {
217 //Should this really be empty?
221 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
224 public void createWithWrongXmlSchema(String testName) throws Exception {
225 //Should this really be empty?
230 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
231 dependsOnMethods = {"create", "testSubmitRequest"})
232 public void createWithMalformedXml(String testName) throws Exception {
234 if (logger.isDebugEnabled()) {
235 logger.debug(testBanner(testName, CLASS_NAME));
239 setupCreateWithMalformedXml();
241 // Submit the request to the service and store the response.
242 String method = REQUEST_TYPE.httpMethodName();
243 String url = getServiceRootURL();
244 final String entity = MALFORMED_XML_DATA; // Constant from base class.
245 int statusCode = submitRequest(method, url, entity);
247 // Check the status code of the response: does it match
248 // the expected response(s)?
249 if(logger.isDebugEnabled()){
250 logger.debug(testName + ": url=" + url +
251 " status=" + statusCode);
253 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
254 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
255 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
259 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
260 dependsOnMethods = {"create", "testSubmitRequest"})
261 public void createWithWrongXmlSchema(String testName) throws Exception {
263 if (logger.isDebugEnabled()) {
264 logger.debug(testBanner(testName, CLASS_NAME));
268 setupCreateWithWrongXmlSchema();
270 // Submit the request to the service and store the response.
271 String method = REQUEST_TYPE.httpMethodName();
272 String url = getServiceRootURL();
273 final String entity = WRONG_XML_SCHEMA_DATA;
274 int statusCode = submitRequest(method, url, entity);
276 // Check the status code of the response: does it match
277 // the expected response(s)?
278 if(logger.isDebugEnabled()){
279 logger.debug(testName + ": url=" + url +
280 " status=" + statusCode);
282 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
283 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
284 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
288 // ---------------------------------------------------------------
289 // CRUD tests : READ tests
290 // ---------------------------------------------------------------
293 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
296 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
297 dependsOnMethods = {"create"})
298 public void read(String testName) throws Exception {
300 if (logger.isDebugEnabled()) {
301 logger.debug(testBanner(testName, CLASS_NAME));
307 AcquisitionClient client = new AcquisitionClient();
309 // Submit the request to the service and store the response.
310 ClientResponse<String> res = client.read(knownResourceId);
311 int statusCode = res.getStatus();
313 // Check the status code of the response: does it match
314 // the expected response(s)?
315 if(logger.isDebugEnabled()){
316 logger.debug(testName + ": status = " + statusCode);
318 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
319 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
320 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
322 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
323 AcquisitionsCommon acquisitionObject = (AcquisitionsCommon) extractPart(input,
324 client.getCommonPartName(), AcquisitionsCommon.class);
325 Assert.assertNotNull(acquisitionObject);
327 // Verify the number and contents of values in repeatable fields,
328 // as created in the instance record used for testing.
329 List<String> acqSources =
330 acquisitionObject.getAcquisitionSources().getAcquisitionSource();
331 Assert.assertTrue(acqSources.size() > 0);
332 Assert.assertNotNull(acqSources.get(0));
334 List<String> acqDates =
335 acquisitionObject.getAcquisitionDates().getAcquisitionDate();
336 Assert.assertTrue(acqDates.size() > 0);
337 Assert.assertNotNull(acqDates.get(0));
339 List<String> owners =
340 acquisitionObject.getOwners().getOwner();
341 Assert.assertTrue(owners.size() > 0);
342 Assert.assertNotNull(owners.get(0));
347 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
350 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
351 dependsOnMethods = {"read"})
352 public void readNonExistent(String testName) throws Exception {
354 if (logger.isDebugEnabled()) {
355 logger.debug(testBanner(testName, CLASS_NAME));
359 setupReadNonExistent();
361 // Submit the request to the service and store the response.
362 AcquisitionClient client = new AcquisitionClient();
363 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
364 int statusCode = res.getStatus();
366 // Check the status code of the response: does it match
367 // the expected response(s)?
368 if(logger.isDebugEnabled()){
369 logger.debug(testName + ": status = " + statusCode);
371 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
372 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
373 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
376 // ---------------------------------------------------------------
377 // CRUD tests : READ_LIST tests
378 // ---------------------------------------------------------------
381 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
384 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
385 dependsOnMethods = {"createList", "read"})
386 public void readList(String testName) throws Exception {
388 if (logger.isDebugEnabled()) {
389 logger.debug(testBanner(testName, CLASS_NAME));
395 // Submit the request to the service and store the response.
396 AcquisitionClient client = new AcquisitionClient();
397 ClientResponse<AbstractCommonList> res = client.readList();
398 assertStatusCode(res, testName);
399 AbstractCommonList list = res.getEntity();
401 // Optionally output additional data about list members for debugging.
402 if(logger.isTraceEnabled()){
403 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
411 // ---------------------------------------------------------------
412 // CRUD tests : UPDATE tests
413 // ---------------------------------------------------------------
417 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
420 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
421 dependsOnMethods = {"read"})
422 public void update(String testName) throws Exception {
424 if (logger.isDebugEnabled()) {
425 logger.debug(testBanner(testName, CLASS_NAME));
431 // Retrieve the contents of a resource to update.
432 AcquisitionClient client = new AcquisitionClient();
433 ClientResponse<String> res = client.read(knownResourceId);
434 assertStatusCode(res, testName);
436 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
438 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
439 client.getCommonPartName(), AcquisitionsCommon.class);
440 Assert.assertNotNull(acquisition);
442 // Update the content of this resource.
443 acquisition.setAcquisitionReferenceNumber("updated-" + acquisition.getAcquisitionReferenceNumber());
444 if(logger.isDebugEnabled()){
445 logger.debug("updated object");
446 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
448 // Submit the request to the service and store the response.
449 PoxPayloadOut output = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
450 PayloadOutputPart commonPart = output.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
451 commonPart.setLabel(client.getCommonPartName());
453 res = client.update(knownResourceId, output);
454 int statusCode = res.getStatus();
455 assertStatusCode(res, testName);
457 input = new PoxPayloadIn(res.getEntity());
458 AcquisitionsCommon updatedAcquisition =
459 (AcquisitionsCommon) extractPart(input,
460 client.getCommonPartName(), AcquisitionsCommon.class);
461 Assert.assertNotNull(updatedAcquisition);
463 Assert.assertEquals(updatedAcquisition.getAcquisitionReferenceNumber(),
464 acquisition.getAcquisitionReferenceNumber(),
465 "Data in updated object did not match submitted data.");
470 // Placeholders until the three tests below can be uncommented.
471 // See Issue CSPACE-401.
473 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
476 public void updateWithEmptyEntityBody(String testName) throws Exception {
477 //Should this really be empty?
481 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
484 public void updateWithMalformedXml(String testName) throws Exception {
485 //Should this really be empty?
489 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
492 public void updateWithWrongXmlSchema(String testName) throws Exception {
493 //Should this really be empty?
498 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
499 dependsOnMethods = {"create", "update", "testSubmitRequest"})
500 public void updateWithEmptyEntityBody(String testName) throws Exception {
502 if (logger.isDebugEnabled()) {
503 logger.debug(testBanner(testName, CLASS_NAME));
507 setupUpdateWithEmptyEntityBody();
509 // Submit the request to the service and store the response.
510 String method = REQUEST_TYPE.httpMethodName();
511 String url = getResourceURL(knownResourceId);
512 String mediaType = MediaType.APPLICATION_XML;
513 final String entity = "";
514 int statusCode = submitRequest(method, url, mediaType, entity);
516 // Check the status code of the response: does it match
517 // the expected response(s)?
518 if(logger.isDebugEnabled()){
519 (testName + ": url=" + url + " status=" + statusCode);
521 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
522 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
523 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
527 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
528 dependsOnMethods = {"create", "testSubmitRequest"})
529 public void createWithEmptyEntityBody(String testName) throws Exception {
531 if (logger.isDebugEnabled()) {
532 logger.debug(testBanner(testName, CLASS_NAME));
536 setupCreateWithEmptyEntityBody();
538 // Submit the request to the service and store the response.
539 String method = REQUEST_TYPE.httpMethodName();
540 String url = getServiceRootURL();
541 String mediaType = MediaType.APPLICATION_XML;
542 final String entity = "";
543 int statusCode = submitRequest(method, url, mediaType, entity);
545 // Check the status code of the response: does it match
546 // the expected response(s)?
547 if(logger.isDebugEnabled()){
548 logger.debug(testName + ": url=" + url +
549 " status=" + statusCode);
551 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
552 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
553 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
557 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
558 dependsOnMethods = {"create", "update", "testSubmitRequest"})
559 public void updateWithMalformedXml(String testName) throws Exception {
561 if (logger.isDebugEnabled()) {
562 logger.debug(testBanner(testName, CLASS_NAME));
566 setupUpdateWithMalformedXml();
568 // Submit the request to the service and store the response.
569 String method = REQUEST_TYPE.httpMethodName();
570 String url = getResourceURL(knownResourceId);
571 final String entity = MALFORMED_XML_DATA;
572 int statusCode = submitRequest(method, url, entity);
574 // Check the status code of the response: does it match
575 // the expected response(s)?
576 if(logger.isDebugEnabled()){
577 logger.debug(testName + ": url=" + url +
578 " status=" + statusCode);
580 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
581 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
582 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
586 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
587 public void updateWithWrongXmlSchema(String testName) {
589 if (logger.isDebugEnabled()) {
590 logger.debug(testBanner(testName, CLASS_NAME));
594 setupUpdateWithWrongXmlSchema();
596 // Submit the request to the service and store the response.
597 String method = REQUEST_TYPE.httpMethodName();
598 String url = getResourceURL(knownResourceId);
599 final String entity = WRONG_XML_SCHEMA_DATA;
600 int statusCode = submitRequest(method, url, entity);
602 // Check the status code of the response: does it match
603 // the expected response(s)?
604 if(logger.isDebugEnabled()){
605 logger.debug(testName + ": url=" + url +
606 " status=" + statusCode);
608 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
609 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
610 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
615 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
618 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
619 dependsOnMethods = {"update", "testSubmitRequest"})
620 public void updateNonExistent(String testName) throws Exception {
622 if (logger.isDebugEnabled()) {
623 logger.debug(testBanner(testName, CLASS_NAME));
627 setupUpdateNonExistent();
629 // Submit the request to the service and store the response.
630 // Note: The ID used in this 'create' call may be arbitrary.
631 // The only relevant ID may be the one used in update(), below.
632 AcquisitionClient client = new AcquisitionClient();
633 PoxPayloadOut multipart = createAcquisitionInstance(NON_EXISTENT_ID);
634 ClientResponse<String> res =
635 client.update(NON_EXISTENT_ID, multipart);
636 int statusCode = res.getStatus();
638 // Check the status code of the response: does it match
639 // the expected response(s)?
640 if(logger.isDebugEnabled()){
641 logger.debug(testName + ": status = " + statusCode);
643 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
644 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
645 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
648 // ---------------------------------------------------------------
649 // CRUD tests : DELETE tests
650 // ---------------------------------------------------------------
653 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
656 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
657 dependsOnMethods = {"create", "read", "update"})
658 public void delete(String testName) throws Exception {
660 if (logger.isDebugEnabled()) {
661 logger.debug(testBanner(testName, CLASS_NAME));
667 // Submit the request to the service and store the response.
668 AcquisitionClient client = new AcquisitionClient();
669 ClientResponse<Response> res = client.delete(knownResourceId);
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);
684 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
687 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
688 dependsOnMethods = {"delete"})
689 public void deleteNonExistent(String testName) throws Exception {
691 if (logger.isDebugEnabled()) {
692 logger.debug(testBanner(testName, CLASS_NAME));
696 setupDeleteNonExistent();
698 // Submit the request to the service and store the response.
699 AcquisitionClient client = new AcquisitionClient();
700 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
701 int statusCode = res.getStatus();
703 // Check the status code of the response: does it match
704 // the expected response(s)?
705 if(logger.isDebugEnabled()){
706 logger.debug(testName + ": status = " + statusCode);
708 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
709 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
710 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
713 // ---------------------------------------------------------------
714 // Utility tests : tests of code used in tests above
715 // ---------------------------------------------------------------
717 * Tests the code for manually submitting data that is used by several
718 * of the methods above.
722 @Test(dependsOnMethods = {"create", "read"})
723 public void testSubmitRequest() throws Exception {
724 testSubmitRequest(knownResourceId);
728 * Test submit request.
730 * @param resourceId the resource id
731 * @throws Exception the exception
733 private void testSubmitRequest(String resourceId) throws Exception {
735 // Expected status code: 200 OK
736 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
738 // Submit the request to the service and store the response.
739 String method = ServiceRequestType.READ.httpMethodName();
740 String url = getResourceURL(resourceId);
741 int statusCode = submitRequest(method, url);
743 // Check the status code of the response: does it match
744 // the expected response(s)?
745 if (logger.isDebugEnabled()) {
746 logger.debug("testSubmitRequest: url=" + url
747 + " status=" + statusCode);
749 Assert.assertEquals(statusCode, EXPECTED_STATUS);
753 // ---------------------------------------------------------------
754 // Utility methods used by tests above
755 // ---------------------------------------------------------------
758 protected PoxPayloadOut createInstance(String identifier) {
759 return createAcquisitionInstance(identifier);
763 * Creates the acquisition instance.
765 * @param identifier the identifier
766 * @return the multipart output
768 private PoxPayloadOut createAcquisitionInstance(String identifier) {
769 AcquisitionsCommon acquisition = new AcquisitionsCommon();
770 acquisition.setAcquisitionReferenceNumber("acquisitionReferenceNumber-" + identifier);
772 AcquisitionSourceList acqSourcesList = new AcquisitionSourceList();
773 List<String> acqSources = acqSourcesList.getAcquisitionSource();
774 // FIXME Use properly formatted refNames for representative acquisition
775 // sources in this example test record. The following are mere placeholders.
776 acqSources.add("Donor Acquisition Source-" + identifier);
777 acqSources.add("Museum Acquisition Source-" + identifier);
778 acquisition.setAcquisitionSources(acqSourcesList);
780 AcquisitionDateList acqDatesList = new AcquisitionDateList();
781 List<String> acqDates = acqDatesList.getAcquisitionDate();
782 acqDates.add(CURRENT_DATE_UTC);
783 acqDates.add(CURRENT_DATE_UTC);
784 acquisition.setAcquisitionDates(acqDatesList);
786 OwnerList ownersList = new OwnerList();
787 List<String> owners = ownersList.getOwner();
788 // FIXME Use properly formatted refNames for representative owners
789 // in this example test record. The following are mere placeholders.
790 owners.add("First Owner -" + identifier);
791 owners.add("Second Owner-" + identifier);
792 acquisition.setOwners(ownersList);
794 PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
795 PayloadOutputPart commonPart = multipart.addPart(acquisition,
796 MediaType.APPLICATION_XML_TYPE);
797 commonPart.setLabel(new AcquisitionClient().getCommonPartName());
799 if(logger.isDebugEnabled()){
800 logger.debug("to be created, acquisition common");
801 logger.debug(objectAsXmlString(acquisition, AcquisitionsCommon.class));
806 // FIXME: The following methods might be made generic and moved to a common package.
809 * Retrives an XML document from the given file, and uses
810 * the JAXB unmarshaller to create a Java object representation
811 * and ultimately a multipart payload that can be submitted in
812 * a create or update request.
814 * @param commonPartName
815 * @param commonPartFileName
819 private PoxPayloadOut createAcquisitionInstanceFromXml(String testName, String commonPartName,
820 String commonPartFileName) throws Exception {
822 AcquisitionsCommon acquisition =
823 (AcquisitionsCommon) getObjectFromFile(AcquisitionsCommon.class,
825 PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
826 PayloadOutputPart commonPart = multipart.addPart(acquisition,
827 MediaType.APPLICATION_XML_TYPE);
828 commonPart.setLabel(commonPartName);
830 if (logger.isDebugEnabled()) {
831 logger.debug(testName + " to be created, acquisitions common");
832 logger.debug(objectAsXmlString(acquisition,
833 AcquisitionsCommon.class));
840 * Creates a record / resource from the data in an XML file.
842 * @param testName the test name
843 * @param fileName the file name
844 * @param useJaxb the use jaxb
846 * @throws Exception the exception
848 private String createFromXmlFile(String testName, String fileName, boolean useJaxb) throws Exception {
853 PoxPayloadOut multipart = null;
855 AcquisitionClient client = new AcquisitionClient();
857 multipart = createAcquisitionInstanceFromXml(testName,
858 client.getCommonPartName(), fileName);
861 multipart = createAcquisitionInstanceFromRawXml(testName,
862 client.getCommonPartName(), fileName);
864 ClientResponse<Response> res = client.create(multipart);
865 int statusCode = res.getStatus();
867 if (logger.isDebugEnabled()) {
868 logger.debug(testName + ": status = " + statusCode);
870 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
871 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
872 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
873 String newId = extractId(res);
874 allResourceIdsCreated.add(newId);
879 * Returns a multipart payload that can be submitted with a
880 * create or update request, by reading from an XML file.
882 * @param commonPartName
883 * @param commonPartFileName
887 private PoxPayloadOut createAcquisitionInstanceFromRawXml(String testName, String commonPartName,
888 String commonPartFileName) throws Exception {
890 PoxPayloadOut multipart = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
891 String stringObject = getXmlDocumentAsString(commonPartFileName);
892 if (logger.isDebugEnabled()) {
893 logger.debug(testName + " to be created, acquisition common " + "\n" + stringObject);
895 PayloadOutputPart commonPart = multipart.addPart(stringObject, stringObject);
896 commonPart.setLabel(commonPartName);
902 // FIXME: This duplicates code in read(), and should be consolidated.
903 // This is an expedient to support reading and verifying the contents
904 // of resources that have been created from test data XML files.
905 private AcquisitionsCommon readAcquisitionCommonPart(String csid)
908 String testName = "readAcquisitionCommonPart";
912 // Submit the request to the service and store the response.
913 AcquisitionClient client = new AcquisitionClient();
914 ClientResponse<String> res = client.read(csid);
915 assertStatusCode(res, testName);
917 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
919 if (logger.isDebugEnabled()) {
920 logger.debug(testName + ": Reading Common part ...");
922 AcquisitionsCommon acquisition =
923 (AcquisitionsCommon) extractPart(input,
924 client.getCommonPartName(), AcquisitionsCommon.class);
925 Assert.assertNotNull(acquisition);