2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright © 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 package org.collectionspace.services.client.test;
25 import java.util.List;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
29 //import org.collectionspace.services.client.AbstractServiceClientImpl;
30 import org.collectionspace.services.client.CollectionObjectClient;
31 import org.collectionspace.services.client.CollectionObjectFactory;
32 import org.collectionspace.services.client.CollectionSpaceClient;
33 import org.collectionspace.services.collectionobject.BriefDescriptionList;
34 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
35 import org.collectionspace.services.collectionobject.domain.naturalhistory.CollectionobjectsNaturalhistory;
36 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
37 import org.collectionspace.services.collectionobject.ResponsibleDepartmentList;
38 import org.collectionspace.services.collectionobject.OtherNumber;
39 import org.collectionspace.services.collectionobject.OtherNumberList;
41 import org.collectionspace.services.jaxb.AbstractCommonList;
43 import org.jboss.resteasy.client.ClientResponse;
44 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
45 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
46 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
47 import org.testng.Assert;
48 import org.testng.annotations.Test;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * CollectionObjectServiceTest, carries out tests against a
55 * deployed and running CollectionObject Service.
57 * $LastChangedRevision$
60 public class CollectionObjectServiceTest extends AbstractServiceTestImpl {
63 private final String CLASS_NAME = CollectionObjectServiceTest.class.getName();
64 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
66 // Instance variables specific to this test.
67 /** The known resource id. */
68 private String knownResourceId = null;
71 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
74 protected String getServicePathComponent() {
75 return new CollectionObjectClient().getServicePathComponent();
79 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
82 protected CollectionSpaceClient getClientInstance() {
83 return new CollectionObjectClient();
87 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
90 protected AbstractCommonList getAbstractCommonList(
91 ClientResponse<AbstractCommonList> response) {
92 return response.getEntity(CollectionobjectsCommonList.class);
95 // ---------------------------------------------------------------
96 // CRUD tests : CREATE tests
97 // ---------------------------------------------------------------
100 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
103 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
104 public void create(String testName) throws Exception {
106 if (logger.isDebugEnabled()) {
107 logger.debug(testBanner(testName, CLASS_NAME));
109 // Perform setup, such as initializing the type of service request
110 // (e.g. CREATE, DELETE), its valid and expected status codes, and
111 // its associated HTTP method name (e.g. POST, DELETE).
114 // Submit the request to the service and store the response.
115 CollectionObjectClient client = new CollectionObjectClient();
116 String identifier = createIdentifier();
117 MultipartOutput multipart =
118 createCollectionObjectInstance(client.getCommonPartName(), identifier);
119 ClientResponse<Response> res = client.create(multipart);
120 int statusCode = res.getStatus();
122 // Check the status code of the response: does it match
123 // the expected response(s)?
126 // Does it fall within the set of valid status codes?
127 // Does it exactly match the expected status code?
128 if (logger.isDebugEnabled()) {
129 logger.debug(testName + ": status = " + statusCode);
131 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
132 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
133 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
135 // Store the ID returned from the first resource created
136 // for additional tests below.
137 if (knownResourceId == null) {
138 knownResourceId = extractId(res);
139 if (logger.isDebugEnabled()) {
140 logger.debug(testName + ": knownResourceId=" + knownResourceId);
144 // Store the IDs from every resource created by tests,
145 // so they can be deleted after tests have been run.
146 allResourceIdsCreated.add(extractId(res));
151 * Tests to diagnose and verify the fixed status of CSPACE-1026,
152 * "Whitespace at certain points in payload cause failure"
155 * Creates the from xml cambridge.
157 * @param testName the test name
158 * @throws Exception the exception
160 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
161 dependsOnMethods = {"create", "testSubmitRequest"})
162 public void createFromXmlCambridge(String testName) throws Exception {
164 createFromXmlFile(testName, "./test-data/testCambridge.xml", true);
165 testSubmitRequest(newId);
169 * Creates the from xml rfw s1.
171 * @param testName the test name
172 * @throws Exception the exception
174 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
175 dependsOnMethods = {"create", "testSubmitRequest"})
176 public void createFromXmlRFWS1(String testName) throws Exception {
177 String testDataDir = System.getProperty("test-data.fileName");
179 //createFromXmlFile(testName, "./target/test-classes/test-data/repfield_whitesp1.xml", false);
180 createFromXmlFile(testName, testDataDir + "/repfield_whitesp1.xml", false);
181 testSubmitRequest(newId);
185 * Creates the from xml rfw s2.
187 * @param testName the test name
188 * @throws Exception the exception
190 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
191 dependsOnMethods = {"create", "testSubmitRequest"})
192 public void createFromXmlRFWS2(String testName) throws Exception {
193 String testDataDir = System.getProperty("test-data.fileName");
195 //createFromXmlFile(testName, "./target/test-classes/test-data/repfield_whitesp2.xml", false);
196 createFromXmlFile(testName, testDataDir + "/repfield_whitesp2.xml", false);
197 testSubmitRequest(newId);
201 * Creates the from xml rfw s3.
203 * @param testName the test name
204 * @throws Exception the exception
206 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
207 dependsOnMethods = {"create", "testSubmitRequest"})
208 public void createFromXmlRFWS3(String testName) throws Exception {
209 String testDataDir = System.getProperty("test-data.fileName");
211 //createFromXmlFile(testName, "./target/test-classes/test-data/repfield_whitesp3.xml", false);
212 createFromXmlFile(testName, testDataDir + "/repfield_whitesp3.xml", false);
213 testSubmitRequest(newId);
217 * Creates the from xml rfw s4.
219 * @param testName the test name
220 * @throws Exception the exception
222 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
223 dependsOnMethods = {"create", "testSubmitRequest"})
224 public void createFromXmlRFWS4(String testName) throws Exception {
225 String testDataDir = System.getProperty("test-data.fileName");
227 createFromXmlFile(testName, testDataDir + "/repfield_whitesp4.xml", false);
228 testSubmitRequest(newId);
232 * Tests to diagnose and verify the fixed status of CSPACE-1248,
233 * "Wedged records created!" (i.e. records with child repeatable
234 * fields, which contain null values, can be successfully created
235 * but an error occurs on trying to retrieve those records).
238 * Creates the with null value repeatable field.
240 * @param testName the test name
241 * @throws Exception the exception
243 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
244 dependsOnMethods = {"create", "testSubmitRequest"})
245 public void createWithNullValueRepeatableField(String testName) throws Exception {
246 String testDataDir = System.getProperty("test-data.fileName");
248 createFromXmlFile(testName, testDataDir + "/repfield_null1.xml", false);
249 if (logger.isDebugEnabled()) {
250 logger.debug("Successfully created record with null value repeatable field.");
251 logger.debug("Attempting to retrieve just-created record ...");
253 testSubmitRequest(newId);
257 * @see org.collectionspace.services.client.test.ServiceTest#createList()
260 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
261 dependsOnMethods = {"create"})
262 public void createList(String testName) throws Exception {
263 this.createPaginatedList(testName, DEFAULT_LIST_SIZE);
267 // Placeholders until the three tests below can be uncommented.
268 // See Issue CSPACE-401.
270 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
273 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
274 public void createWithEmptyEntityBody(String testName) throws Exception {
275 //FIXME: Should this test really be empty?
279 * Test how the service handles XML that is not well formed,
280 * when sent in the payload of a Create request.
282 * @param testName The name of this test method. This name is supplied
283 * automatically, via reflection, by a TestNG 'data provider' in
287 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
288 public void createWithMalformedXml(String testName) throws Exception {
292 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
295 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
296 public void createWithWrongXmlSchema(String testName) throws Exception {
297 //FIXME: Should this test really be empty?
303 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
304 dependsOnMethods = {"create", "testSubmitRequest"})
305 public void createWithEmptyEntityBody(String testName) throwsException {
307 if (logger.isDebugEnabled()) {
308 logger.debug(testBanner(testName, CLASS_NAME));
311 setupCreateWithEmptyEntityBody();
313 // Submit the request to the service and store the response.
314 String method = REQUEST_TYPE.httpMethodName();
315 String url = getServiceRootURL();
316 String mediaType = MediaType.APPLICATION_XML;
317 final String entity = "";
318 int statusCode = submitRequest(method, url, mediaType, entity);
320 // Check the status code of the response: does it match
321 // the expected response(s)?
322 if(logger.isDebugEnabled()){
323 logger.debug(testName + ": url=" + url +
324 " status=" + statusCode);
326 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
327 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
328 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
332 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
333 dependsOnMethods = {"create", "testSubmitRequest"})
334 public void createWithMalformedXml(String testName) throws Exception {
336 if (logger.isDebugEnabled()) {
337 logger.debug(testBanner(testName, CLASS_NAME));
340 setupCreateWithMalformedXml();
342 // Submit the request to the service and store the response.
343 String method = REQUEST_TYPE.httpMethodName();
344 String url = getServiceRootURL();
345 String mediaType = MediaType.APPLICATION_XML;
346 final String entity = MALFORMED_XML_DATA; // Constant from base class.
347 int statusCode = submitRequest(method, url, mediaType, entity);
349 // Check the status code of the response: does it match
350 // the expected response(s)?
351 if(logger.isDebugEnabled()){
352 logger.debug(testName + ": url=" + url +
353 " status=" + statusCode);
355 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
356 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
357 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
361 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
362 dependsOnMethods = {"create", "testSubmitRequest"})
363 public void createWithWrongXmlSchema(String testName) throws Exception {
365 if (logger.isDebugEnabled()) {
366 logger.debug(testBanner(testName, CLASS_NAME));
369 setupCreateWithWrongXmlSchema();
371 // Submit the request to the service and store the response.
372 String method = REQUEST_TYPE.httpMethodName();
373 String url = getServiceRootURL();
374 String mediaType = MediaType.APPLICATION_XML;
375 final String entity = WRONG_XML_SCHEMA_DATA;
376 int statusCode = submitRequest(method, url, mediaType, entity);
378 // Check the status code of the response: does it match
379 // the expected response(s)?
380 if(logger.isDebugEnabled()){
381 logger.debug(testName + ": url=" + url +
382 " status=" + statusCode);
384 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
385 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
386 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
391 * Test how the service handles, in a Create request, payloads
392 * containing null values (or, in the case of String fields,
393 * empty String values) in one or more fields which must be
394 * present and are required to contain non-empty values.
396 * This is a test of code and/or configuration in the service's
397 * validation routine(s).
399 * @param testName The name of this test method. This name is supplied
400 * automatically, via reflection, by a TestNG 'data provider' in
404 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
405 public void createWithRequiredValuesNullOrEmpty(String testName) throws Exception {
406 if (logger.isDebugEnabled()) {
407 logger.debug(testBanner(testName, CLASS_NAME));
411 // Build a payload with invalid content, by omitting a
412 // field (objectNumber) which must be present, and in which
413 // a non-empty value is required, as enforced by the service's
414 // validation routine(s).
415 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
416 collectionObject.setTitle("atitle");
417 collectionObject.setObjectName("some name");
419 // Submit the request to the service and store the response.
420 CollectionObjectClient client = new CollectionObjectClient();
421 MultipartOutput multipart =
422 createCollectionObjectInstance(client.getCommonPartName(), collectionObject, null);
423 ClientResponse<Response> res = client.create(multipart);
424 int statusCode = res.getStatus();
426 // Read the response and verify that the create attempt failed.
427 if (logger.isDebugEnabled()) {
428 logger.debug(testName + ": status = " + statusCode);
430 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
431 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
432 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
434 // FIXME: Consider splitting off the following into its own test method.
436 // Build a payload with invalid content, by setting a value to the
437 // empty String, in a field that requires a non-empty value,
438 // as enforced by the service's validation routine(s).
439 collectionObject = new CollectionobjectsCommon();
440 collectionObject.setTitle("atitle");
441 collectionObject.setObjectName("some name");
442 collectionObject.setObjectNumber("");
444 // Submit the request to the service and store the response.
446 createCollectionObjectInstance(client.getCommonPartName(), collectionObject, null);
447 res = client.create(multipart);
448 statusCode = res.getStatus();
450 // Read the response and verify that the create attempt failed.
451 if (logger.isDebugEnabled()) {
452 logger.debug(testName + ": status = " + statusCode);
454 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
455 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
456 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
461 // ---------------------------------------------------------------
462 // CRUD tests : READ tests
463 // ---------------------------------------------------------------
466 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
469 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
470 dependsOnMethods = {"create"})
471 public void read(String testName) throws Exception {
473 if (logger.isDebugEnabled()) {
474 logger.debug(testBanner(testName, CLASS_NAME));
479 // Submit the request to the service and store the response.
480 CollectionObjectClient client = new CollectionObjectClient();
481 ClientResponse<MultipartInput> res = client.read(knownResourceId);
482 int statusCode = res.getStatus();
484 // Check the status code of the response: does it match
485 // the expected response(s)?
486 if (logger.isDebugEnabled()) {
487 logger.debug(testName + ": status = " + statusCode);
489 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
490 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
491 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
493 MultipartInput input = (MultipartInput) res.getEntity();
495 if (logger.isDebugEnabled()) {
496 logger.debug(testName + ": Reading Common part ...");
498 CollectionobjectsCommon collectionObject =
499 (CollectionobjectsCommon) extractPart(input,
500 client.getCommonPartName(), CollectionobjectsCommon.class);
501 Assert.assertNotNull(collectionObject);
503 if (logger.isDebugEnabled()) {
504 logger.debug(testName + ": Reading Natural History part ...");
506 CollectionobjectsNaturalhistory conh =
507 (CollectionobjectsNaturalhistory) extractPart(input,
508 getNHPartName(), CollectionobjectsNaturalhistory.class);
509 Assert.assertNotNull(conh);
514 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
517 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
518 dependsOnMethods = {"read"})
519 public void readNonExistent(String testName) throws Exception {
521 if (logger.isDebugEnabled()) {
522 logger.debug(testBanner(testName, CLASS_NAME));
525 setupReadNonExistent();
527 // Submit the request to the service and store the response.
528 CollectionObjectClient client = new CollectionObjectClient();
529 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
530 int statusCode = res.getStatus();
532 // Check the status code of the response: does it match
533 // the expected response(s)?
534 if (logger.isDebugEnabled()) {
535 logger.debug(testName + ": status = " + statusCode);
537 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
538 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
539 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
543 // ---------------------------------------------------------------
544 // CRUD tests : READ_LIST tests
545 // ---------------------------------------------------------------
548 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
551 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
552 dependsOnMethods = {"createList", "read"})
553 public void readList(String testName) throws Exception {
555 if (logger.isDebugEnabled()) {
556 logger.debug(testBanner(testName, CLASS_NAME));
561 // Submit the request to the service and store the response.
562 CollectionObjectClient client = new CollectionObjectClient();
563 ClientResponse<CollectionobjectsCommonList> res = client.readList();
564 CollectionobjectsCommonList list = res.getEntity();
565 int statusCode = res.getStatus();
567 // Check the status code of the response: does it match
568 // the expected response(s)?
569 if (logger.isDebugEnabled()) {
570 logger.debug(testName + ": status = " + statusCode);
572 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
573 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
574 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
576 // Optionally output additional data about list members for debugging.
577 boolean iterateThroughList = false;
578 if (iterateThroughList && logger.isDebugEnabled()) {
579 List<CollectionobjectsCommonList.CollectionObjectListItem> items =
580 list.getCollectionObjectListItem();
583 for (CollectionobjectsCommonList.CollectionObjectListItem item : items) {
584 logger.debug(testName + ": list-item[" + i + "] csid="
586 logger.debug(testName + ": list-item[" + i + "] objectNumber="
587 + item.getObjectNumber());
588 logger.debug(testName + ": list-item[" + i + "] URI="
598 // ---------------------------------------------------------------
599 // CRUD tests : UPDATE tests
600 // ---------------------------------------------------------------
603 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
606 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
607 dependsOnMethods = {"read"})
608 public void update(String testName) throws Exception {
610 if (logger.isDebugEnabled()) {
611 logger.debug(testBanner(testName, CLASS_NAME));
616 // Read an existing resource that will be updated.
617 ClientResponse<MultipartInput> res = updateRetrieve(testName, knownResourceId);
619 // Extract its common part.
620 CollectionObjectClient client = new CollectionObjectClient();
621 MultipartInput input = (MultipartInput) res.getEntity();
622 CollectionobjectsCommon collectionObject =
623 (CollectionobjectsCommon) extractPart(input,
624 client.getCommonPartName(), CollectionobjectsCommon.class);
625 Assert.assertNotNull(collectionObject);
627 // Change the content of one or more fields in the common part.
628 collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
629 collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
630 if (logger.isDebugEnabled()) {
631 logger.debug("sparse update that will be sent in update request:");
632 logger.debug(objectAsXmlString(collectionObject,
633 CollectionobjectsCommon.class));
636 // Send the changed resource to be updated.
637 res = updateSend(testName, knownResourceId, collectionObject);
638 int statusCode = res.getStatus();
639 // Check the status code of the response: does it match 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);
647 // Read the response and verify that the resource was correctly updated.
648 input = (MultipartInput) res.getEntity();
649 CollectionobjectsCommon updatedCollectionObject =
650 (CollectionobjectsCommon) extractPart(input,
651 client.getCommonPartName(), CollectionobjectsCommon.class);
652 Assert.assertNotNull(updatedCollectionObject);
653 Assert.assertEquals(updatedCollectionObject.getObjectName(),
654 collectionObject.getObjectName(),
655 "Data in updated object did not match submitted data.");
662 * @param testName the test name
664 * @return the client response
666 private ClientResponse<MultipartInput> updateRetrieve(String testName, String id) {
667 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
668 CollectionObjectClient client = new CollectionObjectClient();
669 ClientResponse<MultipartInput> res = client.read(id);
670 if (logger.isDebugEnabled()) {
671 logger.debug("read in updateRetrieve for " + testName + " status = " + res.getStatus());
673 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS);
674 if (logger.isDebugEnabled()) {
675 logger.debug("got object to updateRetrieve for " + testName + " with ID: " + id);
683 * @param testName the test name
685 * @param collectionObject the collection object
686 * @return the client response
688 private ClientResponse<MultipartInput> updateSend(String testName, String id,
689 CollectionobjectsCommon collectionObject) {
690 MultipartOutput output = new MultipartOutput();
691 OutputPart commonPart = output.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
692 CollectionObjectClient client = new CollectionObjectClient();
693 commonPart.getHeaders().add("label", client.getCommonPartName());
694 ClientResponse<MultipartInput> res = client.update(knownResourceId, output);
699 // Placeholders until the three tests below can be uncommented.
700 // See Issue CSPACE-401.
702 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
705 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
706 dependsOnMethods = {"read"})
707 public void updateWithEmptyEntityBody(String testName) throws Exception {
708 //FIXME: Should this test really be empty?
712 * Test how the service handles XML that is not well formed,
713 * when sent in the payload of an Update request.
715 * @param testName The name of this test method. This name is supplied
716 * automatically, via reflection, by a TestNG 'data provider' in
720 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
721 dependsOnMethods = {"read"})
722 public void updateWithMalformedXml(String testName) throws Exception {
723 //FIXME: Should this test really be empty?
727 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
730 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
731 dependsOnMethods = {"read"})
732 public void updateWithWrongXmlSchema(String testName) throws Exception {
733 //FIXME: Should this test really be empty?
738 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
739 dependsOnMethods = {"create", "update", "testSubmitRequest"})
740 public void updateWithEmptyEntityBody(String testName) throws Exception {
742 if (logger.isDebugEnabled()) {
743 logger.debug(testBanner(testName, CLASS_NAME));
746 setupUpdateWithEmptyEntityBody();
748 // Submit the request to the service and store the response.
749 String method = REQUEST_TYPE.httpMethodName();
750 String url = getResourceURL(knownResourceId);
751 String mediaType = MediaType.APPLICATION_XML;
752 final String entity = "";
753 int statusCode = submitRequest(method, url, mediaType, entity);
755 // Check the status code of the response: does it match
756 // the expected response(s)?
757 if(logger.isDebugEnabled()){
758 logger.debug(testName + ": url=" + url +
759 " status=" + statusCode);
761 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
762 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
763 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
767 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
768 dependsOnMethods = {"create", "update", "testSubmitRequest"})
769 public void updateWithMalformedXml() throws Exception {
771 if (logger.isDebugEnabled()) {
772 logger.debug(testBanner(testName, CLASS_NAME));
775 setupUpdateWithMalformedXml();
777 // Submit the request to the service and store the response.
778 String method = REQUEST_TYPE.httpMethodName();
779 String url = getResourceURL(knownResourceId);
780 final String entity = MALFORMED_XML_DATA;
781 String mediaType = MediaType.APPLICATION_XML;
782 int statusCode = submitRequest(method, url, mediaType, entity);
784 // Check the status code of the response: does it match
785 // the expected response(s)?
786 if(logger.isDebugEnabled()){
787 logger.debug(testName + ": url=" + url +
788 " status=" + statusCode);
790 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
791 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
792 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
796 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
797 dependsOnMethods = {"create", "update", "testSubmitRequest"})
798 public void updateWithWrongXmlSchema(String testName) throws Exception {
800 if (logger.isDebugEnabled()) {
801 logger.debug(testBanner(testName, CLASS_NAME));
804 setupUpdateWithWrongXmlSchema();
806 // Submit the request to the service and store the response.
807 String method = REQUEST_TYPE.httpMethodName();
808 String url = getResourceURL(knownResourceId);
809 String mediaType = MediaType.APPLICATION_XML;
810 final String entity = WRONG_XML_SCHEMA_DATA;
811 int statusCode = submitRequest(method, url, mediaType, entity);
813 // Check the status code of the response: does it match
814 // the expected response(s)?
815 if(logger.isDebugEnabled()){
816 logger.debug(testName + ": url=" + url +
817 " status=" + statusCode);
819 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
820 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
821 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
826 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
829 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
830 dependsOnMethods = {"update", "testSubmitRequest"})
831 public void updateNonExistent(String testName) throws Exception {
833 if (logger.isDebugEnabled()) {
834 logger.debug(testBanner(testName, CLASS_NAME));
837 setupUpdateNonExistent();
839 // Submit the request to the service and store the response.
841 // Note: The ID used in this 'create' call may be arbitrary.
842 // The only relevant ID may be the one used in updateCollectionObject(), below.
843 CollectionObjectClient client = new CollectionObjectClient();
844 MultipartOutput multipart =
845 createCollectionObjectInstance(client.getCommonPartName(),
847 ClientResponse<MultipartInput> res =
848 client.update(NON_EXISTENT_ID, multipart);
849 int statusCode = res.getStatus();
851 // Check the status code of the response: does it match
852 // the expected response(s)?
853 if (logger.isDebugEnabled()) {
854 logger.debug(testName + ": status = " + statusCode);
856 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
857 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
858 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
862 * Test how the service handles, in an Update request, payloads
863 * containing null values (or, in the case of String fields,
864 * empty String values) in one or more fields in which non-empty
865 * values are required.
867 * This is a test of code and/or configuration in the service's
868 * validation routine(s).
870 * @param testName The name of this test method. This name is supplied
871 * automatically, via reflection, by a TestNG 'data provider' in
875 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
876 dependsOnMethods = {"read"})
877 public void updateWithRequiredValuesNullOrEmpty(String testName) throws Exception {
879 if (logger.isDebugEnabled()) {
880 logger.debug(testBanner(testName, CLASS_NAME));
884 if (logger.isDebugEnabled()) {
885 logger.debug(testName + " got object to update with ID: " + knownResourceId);
888 // Read an existing record for updating.
889 ClientResponse<MultipartInput> res = updateRetrieve(testName, knownResourceId);
891 CollectionObjectClient client = new CollectionObjectClient();
892 MultipartInput input = (MultipartInput) res.getEntity();
893 CollectionobjectsCommon collectionObject =
894 (CollectionobjectsCommon) extractPart(input,
895 client.getCommonPartName(), CollectionobjectsCommon.class);
896 Assert.assertNotNull(collectionObject);
898 // Update with invalid content, by setting a value to the
899 // empty String, in a field that requires a non-empty value,
900 // as enforced by the service's validation routine(s).
901 collectionObject.setObjectNumber("");
903 if (logger.isDebugEnabled()) {
904 logger.debug(testName + " updated object");
905 logger.debug(objectAsXmlString(collectionObject,
906 CollectionobjectsCommon.class));
909 // Submit the request to the service and store the response.
910 res = updateSend(testName, knownResourceId, collectionObject);
911 int statusCode = res.getStatus();
913 // Read the response and verify that the update attempt failed.
914 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
915 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
916 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
920 // ---------------------------------------------------------------
921 // CRUD tests : DELETE tests
922 // ---------------------------------------------------------------
925 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
928 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
929 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
930 public void delete(String testName) throws Exception {
932 if (logger.isDebugEnabled()) {
933 logger.debug(testBanner(testName, CLASS_NAME));
938 // Submit the request to the service and store the response.
939 CollectionObjectClient client = new CollectionObjectClient();
940 ClientResponse<Response> res = client.delete(knownResourceId);
941 int statusCode = res.getStatus();
943 // Check the status code of the response: does it match
944 // the expected response(s)?
945 if (logger.isDebugEnabled()) {
946 logger.debug(testName + ": status = " + statusCode);
948 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
949 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
950 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
955 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
958 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
959 dependsOnMethods = {"delete"})
960 public void deleteNonExistent(String testName) throws Exception {
962 if (logger.isDebugEnabled()) {
963 logger.debug(testBanner(testName, CLASS_NAME));
966 setupDeleteNonExistent();
968 // Submit the request to the service and store the response.
969 CollectionObjectClient client = new CollectionObjectClient();
970 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
971 int statusCode = res.getStatus();
973 // Check the status code of the response: does it match
974 // the expected response(s)?
975 if (logger.isDebugEnabled()) {
976 logger.debug(testName + ": status = " + statusCode);
978 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
979 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
980 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
983 // ---------------------------------------------------------------
984 // Utility tests : tests of code used in tests above
985 // ---------------------------------------------------------------
987 * Tests the code for manually submitting data that is used by several
988 * of the methods above.
992 @Test(dependsOnMethods = {"create", "read"})
993 public void testSubmitRequest() throws Exception {
994 testSubmitRequest(knownResourceId);
998 * Test submit request.
1000 * @param resourceId the resource id
1001 * @throws Exception the exception
1003 private void testSubmitRequest(String resourceId) throws Exception {
1005 // Expected status code: 200 OK
1006 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1008 // Submit the request to the service and store the response.
1009 String method = ServiceRequestType.READ.httpMethodName();
1010 String url = getResourceURL(resourceId);
1011 int statusCode = submitRequest(method, url);
1013 // Check the status code of the response: does it match
1014 // the expected response(s)?
1015 if (logger.isDebugEnabled()) {
1016 logger.debug("testSubmitRequest: url=" + url
1017 + " status=" + statusCode);
1019 Assert.assertEquals(statusCode, EXPECTED_STATUS);
1023 // ---------------------------------------------------------------
1024 // Utility methods used by tests above
1025 // ---------------------------------------------------------------
1027 * Creates the collection object instance.
1029 * @param commonPartName the common part name
1030 * @param identifier the identifier
1031 * @return the multipart output
1033 private MultipartOutput createCollectionObjectInstance(String commonPartName,
1034 String identifier) {
1035 return createCollectionObjectInstance(commonPartName,
1036 "objectNumber-" + identifier,
1037 "objectName-" + identifier);
1041 * Creates the collection object instance.
1043 * @param commonPartName the common part name
1044 * @param objectNumber the object number
1045 * @param objectName the object name
1046 * @return the multipart output
1048 private MultipartOutput createCollectionObjectInstance(String commonPartName,
1049 String objectNumber, String objectName) {
1050 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
1052 BriefDescriptionList descriptionList = new BriefDescriptionList();
1053 List<String> descriptions = descriptionList.getBriefDescription();
1054 descriptions.add("Papier mache bird cow mask with horns, "
1055 + "painted red with black and yellow spots. "
1056 + "Puerto Rico. ca. 8" high, 6" wide, projects 10" (with horns).");
1057 descriptions.add("Papier mache bird cow mask with horns, "
1058 + "painted red with black and yellow spots. "
1059 + "Puerto Rico. ca. 8" high, 6" wide, projects 10" (with horns).");
1061 ResponsibleDepartmentList deptList = new ResponsibleDepartmentList();
1062 List<String> depts = deptList.getResponsibleDepartment();
1063 // @TODO Use properly formatted refNames for representative departments
1064 // in this example test record. The following are mere placeholders.
1065 depts.add("urn:org.collectionspace.services.department:Registrar");
1066 depts.add("urn:org.walkerart.department:Fine Art");
1068 OtherNumberList otherNumList = new OtherNumberList();
1069 List<OtherNumber> otherNumbers = otherNumList.getOtherNumber();
1071 OtherNumber otherNumber1 = new OtherNumber();
1072 otherNumber1.setNumberValue("101." + objectName);
1073 otherNumber1.setNumberType("integer");
1074 otherNumbers.add(otherNumber1);
1076 OtherNumber otherNumber2 = new OtherNumber();
1077 otherNumber2.setNumberValue("101.502.23.456." + objectName);
1078 otherNumber2.setNumberType("ipaddress");
1079 otherNumbers.add(otherNumber2);
1081 //FIXME: Title does not need to be set.
1082 collectionObject.setTitle("atitle");
1083 collectionObject.setResponsibleDepartments(deptList);
1084 collectionObject.setObjectNumber(objectNumber);
1086 collectionObject.setOtherNumberList(otherNumList);
1088 // FIXME this can be removed when the repeatable other number list
1089 // is supported by the application layers
1090 collectionObject.setOtherNumber("urn:org.walkerart.id:123");
1092 collectionObject.setObjectName(objectName);
1093 collectionObject.setAge(""); //test for null string
1094 collectionObject.setBriefDescriptions(descriptionList);
1096 CollectionobjectsNaturalhistory conh = new CollectionobjectsNaturalhistory();
1097 conh.setNhString("test-string");
1099 conh.setNhLong(9999);
1102 MultipartOutput multipart = createCollectionObjectInstance(commonPartName, collectionObject, conh);
1107 * Creates the collection object instance.
1109 * @param commonPartName the common part name
1110 * @param collectionObject the collection object
1111 * @param conh the conh
1112 * @return the multipart output
1114 private MultipartOutput createCollectionObjectInstance(String commonPartName,
1115 CollectionobjectsCommon collectionObject, CollectionobjectsNaturalhistory conh) {
1117 MultipartOutput multipart = CollectionObjectFactory.createCollectionObjectInstance(
1118 commonPartName, collectionObject, getNHPartName(), conh);
1119 if (logger.isDebugEnabled()) {
1120 logger.debug("to be created, collectionobject common");
1121 logger.debug(objectAsXmlString(collectionObject,
1122 CollectionobjectsCommon.class));
1126 if (logger.isDebugEnabled()) {
1127 logger.debug("to be created, collectionobject nhistory");
1128 logger.debug(objectAsXmlString(conh,
1129 CollectionobjectsNaturalhistory.class));
1137 * createCollectionObjectInstanceFromXml uses JAXB unmarshaller to retrieve
1138 * collectionobject from given file
1139 * @param commonPartName
1140 * @param commonPartFileName
1144 private MultipartOutput createCollectionObjectInstanceFromXml(String testName, String commonPartName,
1145 String commonPartFileName) throws Exception {
1147 CollectionobjectsCommon collectionObject =
1148 (CollectionobjectsCommon) getObjectFromFile(CollectionobjectsCommon.class,
1149 commonPartFileName);
1150 MultipartOutput multipart = new MultipartOutput();
1151 OutputPart commonPart = multipart.addPart(collectionObject,
1152 MediaType.APPLICATION_XML_TYPE);
1153 commonPart.getHeaders().add("label", commonPartName);
1155 if (logger.isDebugEnabled()) {
1156 logger.debug(testName + " to be created, collectionobject common");
1157 logger.debug(objectAsXmlString(collectionObject,
1158 CollectionobjectsCommon.class));
1165 * createCollectionObjectInstanceFromRawXml uses stringified collectionobject
1166 * retrieve from given file
1167 * @param commonPartName
1168 * @param commonPartFileName
1172 private MultipartOutput createCollectionObjectInstanceFromRawXml(String testName, String commonPartName,
1173 String commonPartFileName) throws Exception {
1175 MultipartOutput multipart = new MultipartOutput();
1176 String stringObject = getXmlDocumentAsString(commonPartFileName);
1177 if (logger.isDebugEnabled()) {
1178 logger.debug(testName + " to be created, collectionobject common " + "\n" + stringObject);
1180 OutputPart commonPart = multipart.addPart(stringObject,
1181 MediaType.APPLICATION_XML_TYPE);
1182 commonPart.getHeaders().add("label", commonPartName);
1189 * Gets the nH part name.
1191 * @return the nH part name
1193 private String getNHPartName() {
1194 return "collectionobjects_naturalhistory";
1198 * Creates the from xml file.
1200 * @param testName the test name
1201 * @param fileName the file name
1202 * @param useJaxb the use jaxb
1203 * @return the string
1204 * @throws Exception the exception
1206 private String createFromXmlFile(String testName, String fileName, boolean useJaxb) throws Exception {
1211 MultipartOutput multipart = null;
1213 CollectionObjectClient client = new CollectionObjectClient();
1215 multipart = createCollectionObjectInstanceFromXml(testName,
1216 client.getCommonPartName(), fileName);
1218 multipart = createCollectionObjectInstanceFromRawXml(testName,
1219 client.getCommonPartName(), fileName);
1221 ClientResponse<Response> res = client.create(multipart);
1222 int statusCode = res.getStatus();
1224 if (logger.isDebugEnabled()) {
1225 logger.debug(testName + ": status = " + statusCode);
1227 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1228 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1229 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1230 String newId = extractId(res);
1231 allResourceIdsCreated.add(newId);