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 * Tests to diagnose and fix CSPACE-2242.
171 * This is a bug identified in release 0.8 in which value instances of a
172 * repeatable field are not stored when the first value instance of that
176 // Verify that record creation occurs successfully when the first value instance
177 // of a single, repeatable String scalar field is non-blank.
178 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
179 dependsOnMethods = {"create", "testSubmitRequest"}, groups = {"cspace2242group"})
180 public void createFromXmlNonBlankFirstValueInstance(String testName) throws Exception {
181 if (logger.isDebugEnabled()) {
182 logger.debug(testBanner(testName, CLASS_NAME));
185 createFromXmlFile(testName, "./test-data/cspace-2242-first-value-instance-nonblank.xml", true);
186 CollectionobjectsCommon collectionObject = readCollectionObjectCommonPart(newId);
187 // Verify that at least one value instance of the repeatable field was successfully persisted.
188 BriefDescriptionList descriptionList = collectionObject.getBriefDescriptions();
189 List<String> descriptions = descriptionList.getBriefDescription();
190 Assert.assertTrue(descriptions.size() > 0);
193 // Verify that record creation occurs successfully when the first value instance
194 // of a single, repeatable String scalar field is blank.
195 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
196 dependsOnMethods = {"create", "testSubmitRequest"}, groups = {"cspace2242group"})
197 public void createFromXmlBlankFirstValueInstance(String testName) throws Exception {
198 if (logger.isDebugEnabled()) {
199 logger.debug(testBanner(testName, CLASS_NAME));
202 createFromXmlFile(testName, "./test-data/cspace-2242-first-value-instance-blank.xml", true);
203 CollectionobjectsCommon collectionObject = readCollectionObjectCommonPart(newId);
204 // Verify that at least one value instance of the repeatable field was successfully persisted.
205 BriefDescriptionList descriptionList = collectionObject.getBriefDescriptions();
206 List<String> descriptions = descriptionList.getBriefDescription();
207 Assert.assertTrue(descriptions.size() > 0);
211 * Creates the from xml rfw s1.
213 * @param testName the test name
214 * @throws Exception the exception
216 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
217 dependsOnMethods = {"create", "testSubmitRequest"})
218 public void createFromXmlRFWS1(String testName) throws Exception {
219 String testDataDir = System.getProperty("test-data.fileName");
221 //createFromXmlFile(testName, "./target/test-classes/test-data/repfield_whitesp1.xml", false);
222 createFromXmlFile(testName, testDataDir + "/repfield_whitesp1.xml", false);
223 testSubmitRequest(newId);
227 * Creates the from xml rfw s2.
229 * @param testName the test name
230 * @throws Exception the exception
232 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
233 dependsOnMethods = {"create", "testSubmitRequest"})
234 public void createFromXmlRFWS2(String testName) throws Exception {
235 String testDataDir = System.getProperty("test-data.fileName");
237 //createFromXmlFile(testName, "./target/test-classes/test-data/repfield_whitesp2.xml", false);
238 createFromXmlFile(testName, testDataDir + "/repfield_whitesp2.xml", false);
239 testSubmitRequest(newId);
243 * Creates the from xml rfw s3.
245 * @param testName the test name
246 * @throws Exception the exception
248 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
249 dependsOnMethods = {"create", "testSubmitRequest"})
250 public void createFromXmlRFWS3(String testName) throws Exception {
251 String testDataDir = System.getProperty("test-data.fileName");
253 //createFromXmlFile(testName, "./target/test-classes/test-data/repfield_whitesp3.xml", false);
254 createFromXmlFile(testName, testDataDir + "/repfield_whitesp3.xml", false);
255 testSubmitRequest(newId);
259 * Creates the from xml rfw s4.
261 * @param testName the test name
262 * @throws Exception the exception
264 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
265 dependsOnMethods = {"create", "testSubmitRequest"})
266 public void createFromXmlRFWS4(String testName) throws Exception {
267 String testDataDir = System.getProperty("test-data.fileName");
269 createFromXmlFile(testName, testDataDir + "/repfield_whitesp4.xml", false);
270 testSubmitRequest(newId);
274 * Tests to diagnose and verify the fixed status of CSPACE-1248,
275 * "Wedged records created!" (i.e. records with child repeatable
276 * fields, which contain null values, can be successfully created
277 * but an error occurs on trying to retrieve those records).
281 * Creates a CollectionObject resource with a null value repeatable field.
283 * @param testName the test name
284 * @throws Exception the exception
286 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
287 dependsOnMethods = {"create", "testSubmitRequest"})
288 public void createWithNullValueRepeatableField(String testName) throws Exception {
289 String testDataDir = System.getProperty("test-data.fileName");
291 createFromXmlFile(testName, testDataDir + "/repfield_null1.xml", false);
292 if (logger.isDebugEnabled()) {
293 logger.debug("Successfully created record with null value repeatable field.");
294 logger.debug("Attempting to retrieve just-created record ...");
296 testSubmitRequest(newId);
300 * @see org.collectionspace.services.client.test.ServiceTest#createList()
303 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
304 dependsOnMethods = {"create"})
305 public void createList(String testName) throws Exception {
306 this.createPaginatedList(testName, DEFAULT_LIST_SIZE);
310 // Placeholders until the three tests below can be uncommented.
311 // See Issue CSPACE-401.
313 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
316 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
317 public void createWithEmptyEntityBody(String testName) throws Exception {
318 //FIXME: Should this test really be empty?
322 * Test how the service handles XML that is not well formed,
323 * when sent in the payload of a Create request.
325 * @param testName The name of this test method. This name is supplied
326 * automatically, via reflection, by a TestNG 'data provider' in
330 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
331 public void createWithMalformedXml(String testName) throws Exception {
335 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
338 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
339 public void createWithWrongXmlSchema(String testName) throws Exception {
340 //FIXME: Should this test really be empty?
346 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
347 dependsOnMethods = {"create", "testSubmitRequest"})
348 public void createWithEmptyEntityBody(String testName) throwsException {
350 if (logger.isDebugEnabled()) {
351 logger.debug(testBanner(testName, CLASS_NAME));
354 setupCreateWithEmptyEntityBody();
356 // Submit the request to the service and store the response.
357 String method = REQUEST_TYPE.httpMethodName();
358 String url = getServiceRootURL();
359 String mediaType = MediaType.APPLICATION_XML;
360 final String entity = "";
361 int statusCode = submitRequest(method, url, mediaType, entity);
363 // Check the status code of the response: does it match
364 // the expected response(s)?
365 if(logger.isDebugEnabled()){
366 logger.debug(testName + ": url=" + url +
367 " status=" + statusCode);
369 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
370 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
371 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
375 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
376 dependsOnMethods = {"create", "testSubmitRequest"})
377 public void createWithMalformedXml(String testName) throws Exception {
379 if (logger.isDebugEnabled()) {
380 logger.debug(testBanner(testName, CLASS_NAME));
383 setupCreateWithMalformedXml();
385 // Submit the request to the service and store the response.
386 String method = REQUEST_TYPE.httpMethodName();
387 String url = getServiceRootURL();
388 String mediaType = MediaType.APPLICATION_XML;
389 final String entity = MALFORMED_XML_DATA; // Constant from base class.
390 int statusCode = submitRequest(method, url, mediaType, entity);
392 // Check the status code of the response: does it match
393 // the expected response(s)?
394 if(logger.isDebugEnabled()){
395 logger.debug(testName + ": url=" + url +
396 " status=" + statusCode);
398 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
399 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
400 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
404 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
405 dependsOnMethods = {"create", "testSubmitRequest"})
406 public void createWithWrongXmlSchema(String testName) throws Exception {
408 if (logger.isDebugEnabled()) {
409 logger.debug(testBanner(testName, CLASS_NAME));
412 setupCreateWithWrongXmlSchema();
414 // Submit the request to the service and store the response.
415 String method = REQUEST_TYPE.httpMethodName();
416 String url = getServiceRootURL();
417 String mediaType = MediaType.APPLICATION_XML;
418 final String entity = WRONG_XML_SCHEMA_DATA;
419 int statusCode = submitRequest(method, url, mediaType, entity);
421 // Check the status code of the response: does it match
422 // the expected response(s)?
423 if(logger.isDebugEnabled()){
424 logger.debug(testName + ": url=" + url +
425 " status=" + statusCode);
427 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
428 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
429 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
434 * Test how the service handles, in a Create request, payloads
435 * containing null values (or, in the case of String fields,
436 * empty String values) in one or more fields which must be
437 * present and are required to contain non-empty values.
439 * This is a test of code and/or configuration in the service's
440 * validation routine(s).
442 * @param testName The name of this test method. This name is supplied
443 * automatically, via reflection, by a TestNG 'data provider' in
447 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
448 public void createWithRequiredValuesNullOrEmpty(String testName) throws Exception {
449 if (logger.isDebugEnabled()) {
450 logger.debug(testBanner(testName, CLASS_NAME));
454 // Build a payload with invalid content, by omitting a
455 // field (objectNumber) which must be present, and in which
456 // a non-empty value is required, as enforced by the service's
457 // validation routine(s).
458 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
459 collectionObject.setTitle("atitle");
460 collectionObject.setObjectName("some name");
462 // Submit the request to the service and store the response.
463 CollectionObjectClient client = new CollectionObjectClient();
464 MultipartOutput multipart =
465 createCollectionObjectInstance(client.getCommonPartName(), collectionObject, null);
466 ClientResponse<Response> res = client.create(multipart);
467 int statusCode = res.getStatus();
469 // Read the response and verify that the create attempt failed.
470 if (logger.isDebugEnabled()) {
471 logger.debug(testName + ": status = " + statusCode);
473 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
474 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
475 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
477 // FIXME: Consider splitting off the following into its own test method.
479 // Build a payload with invalid content, by setting a value to the
480 // empty String, in a field that requires a non-empty value,
481 // as enforced by the service's validation routine(s).
482 collectionObject = new CollectionobjectsCommon();
483 collectionObject.setTitle("atitle");
484 collectionObject.setObjectName("some name");
485 collectionObject.setObjectNumber("");
487 // Submit the request to the service and store the response.
489 createCollectionObjectInstance(client.getCommonPartName(), collectionObject, null);
490 res = client.create(multipart);
491 statusCode = res.getStatus();
493 // Read the response and verify that the create attempt failed.
494 if (logger.isDebugEnabled()) {
495 logger.debug(testName + ": status = " + statusCode);
497 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
498 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
499 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
504 // ---------------------------------------------------------------
505 // CRUD tests : READ tests
506 // ---------------------------------------------------------------
509 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
512 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
513 dependsOnMethods = {"create"})
514 public void read(String testName) throws Exception {
516 if (logger.isDebugEnabled()) {
517 logger.debug(testBanner(testName, CLASS_NAME));
522 // Submit the request to the service and store the response.
523 CollectionObjectClient client = new CollectionObjectClient();
524 ClientResponse<MultipartInput> res = client.read(knownResourceId);
525 int statusCode = res.getStatus();
527 // Check the status code of the response: does it match
528 // the expected response(s)?
529 if (logger.isDebugEnabled()) {
530 logger.debug(testName + ": status = " + statusCode);
532 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
533 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
534 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
536 MultipartInput input = (MultipartInput) res.getEntity();
538 if (logger.isDebugEnabled()) {
539 logger.debug(testName + ": Reading Common part ...");
541 CollectionobjectsCommon collectionObject =
542 (CollectionobjectsCommon) extractPart(input,
543 client.getCommonPartName(), CollectionobjectsCommon.class);
544 Assert.assertNotNull(collectionObject);
546 if (logger.isDebugEnabled()) {
547 logger.debug(testName + ": Reading Natural History part ...");
549 CollectionobjectsNaturalhistory conh =
550 (CollectionobjectsNaturalhistory) extractPart(input,
551 getNHPartName(), CollectionobjectsNaturalhistory.class);
552 Assert.assertNotNull(conh);
557 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
560 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
561 dependsOnMethods = {"read"})
562 public void readNonExistent(String testName) throws Exception {
564 if (logger.isDebugEnabled()) {
565 logger.debug(testBanner(testName, CLASS_NAME));
568 setupReadNonExistent();
570 // Submit the request to the service and store the response.
571 CollectionObjectClient client = new CollectionObjectClient();
572 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
573 int statusCode = res.getStatus();
575 // Check the status code of the response: does it match
576 // the expected response(s)?
577 if (logger.isDebugEnabled()) {
578 logger.debug(testName + ": status = " + statusCode);
580 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
581 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
582 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
586 // ---------------------------------------------------------------
587 // CRUD tests : READ_LIST tests
588 // ---------------------------------------------------------------
591 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
594 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
595 dependsOnMethods = {"createList", "read"})
596 public void readList(String testName) throws Exception {
598 if (logger.isDebugEnabled()) {
599 logger.debug(testBanner(testName, CLASS_NAME));
604 // Submit the request to the service and store the response.
605 CollectionObjectClient client = new CollectionObjectClient();
606 ClientResponse<CollectionobjectsCommonList> res = client.readList();
607 CollectionobjectsCommonList list = res.getEntity();
608 int statusCode = res.getStatus();
610 // Check the status code of the response: does it match
611 // the expected response(s)?
612 if (logger.isDebugEnabled()) {
613 logger.debug(testName + ": status = " + statusCode);
615 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
616 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
617 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
619 // Optionally output additional data about list members for debugging.
620 boolean iterateThroughList = false;
621 if (iterateThroughList && logger.isDebugEnabled()) {
622 List<CollectionobjectsCommonList.CollectionObjectListItem> items =
623 list.getCollectionObjectListItem();
626 for (CollectionobjectsCommonList.CollectionObjectListItem item : items) {
627 logger.debug(testName + ": list-item[" + i + "] csid="
629 logger.debug(testName + ": list-item[" + i + "] objectNumber="
630 + item.getObjectNumber());
631 logger.debug(testName + ": list-item[" + i + "] URI="
641 // ---------------------------------------------------------------
642 // CRUD tests : UPDATE tests
643 // ---------------------------------------------------------------
646 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
649 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
650 dependsOnMethods = {"read"})
651 public void update(String testName) throws Exception {
653 if (logger.isDebugEnabled()) {
654 logger.debug(testBanner(testName, CLASS_NAME));
659 // Read an existing resource that will be updated.
660 ClientResponse<MultipartInput> res = updateRetrieve(testName, knownResourceId);
662 // Extract its common part.
663 CollectionObjectClient client = new CollectionObjectClient();
664 MultipartInput input = (MultipartInput) res.getEntity();
665 CollectionobjectsCommon collectionObject =
666 (CollectionobjectsCommon) extractPart(input,
667 client.getCommonPartName(), CollectionobjectsCommon.class);
668 Assert.assertNotNull(collectionObject);
670 // Change the content of one or more fields in the common part.
671 collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
672 collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
673 if (logger.isDebugEnabled()) {
674 logger.debug("sparse update that will be sent in update request:");
675 logger.debug(objectAsXmlString(collectionObject,
676 CollectionobjectsCommon.class));
679 // Send the changed resource to be updated.
680 res = updateSend(testName, knownResourceId, collectionObject);
681 int statusCode = res.getStatus();
682 // Check the status code of the response: does it match the expected response(s)?
683 if (logger.isDebugEnabled()) {
684 logger.debug(testName + ": status = " + statusCode);
686 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
687 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
688 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
690 // Read the response and verify that the resource was correctly updated.
691 input = (MultipartInput) res.getEntity();
692 CollectionobjectsCommon updatedCollectionObject =
693 (CollectionobjectsCommon) extractPart(input,
694 client.getCommonPartName(), CollectionobjectsCommon.class);
695 Assert.assertNotNull(updatedCollectionObject);
696 Assert.assertEquals(updatedCollectionObject.getObjectName(),
697 collectionObject.getObjectName(),
698 "Data in updated object did not match submitted data.");
705 * @param testName the test name
707 * @return the client response
709 private ClientResponse<MultipartInput> updateRetrieve(String testName, String id) {
710 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
711 CollectionObjectClient client = new CollectionObjectClient();
712 ClientResponse<MultipartInput> res = client.read(id);
713 if (logger.isDebugEnabled()) {
714 logger.debug("read in updateRetrieve for " + testName + " status = " + res.getStatus());
716 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS);
717 if (logger.isDebugEnabled()) {
718 logger.debug("got object to updateRetrieve for " + testName + " with ID: " + id);
726 * @param testName the test name
728 * @param collectionObject the collection object
729 * @return the client response
731 private ClientResponse<MultipartInput> updateSend(String testName, String id,
732 CollectionobjectsCommon collectionObject) {
733 MultipartOutput output = new MultipartOutput();
734 OutputPart commonPart = output.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
735 CollectionObjectClient client = new CollectionObjectClient();
736 commonPart.getHeaders().add("label", client.getCommonPartName());
737 ClientResponse<MultipartInput> res = client.update(knownResourceId, output);
742 // Placeholders until the three tests below can be uncommented.
743 // See Issue CSPACE-401.
745 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
748 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
749 dependsOnMethods = {"read"})
750 public void updateWithEmptyEntityBody(String testName) throws Exception {
751 //FIXME: Should this test really be empty?
755 * Test how the service handles XML that is not well formed,
756 * when sent in the payload of an Update request.
758 * @param testName The name of this test method. This name is supplied
759 * automatically, via reflection, by a TestNG 'data provider' in
763 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
764 dependsOnMethods = {"read"})
765 public void updateWithMalformedXml(String testName) throws Exception {
766 //FIXME: Should this test really be empty?
770 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
773 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
774 dependsOnMethods = {"read"})
775 public void updateWithWrongXmlSchema(String testName) throws Exception {
776 //FIXME: Should this test really be empty?
781 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
782 dependsOnMethods = {"create", "update", "testSubmitRequest"})
783 public void updateWithEmptyEntityBody(String testName) throws Exception {
785 if (logger.isDebugEnabled()) {
786 logger.debug(testBanner(testName, CLASS_NAME));
789 setupUpdateWithEmptyEntityBody();
791 // Submit the request to the service and store the response.
792 String method = REQUEST_TYPE.httpMethodName();
793 String url = getResourceURL(knownResourceId);
794 String mediaType = MediaType.APPLICATION_XML;
795 final String entity = "";
796 int statusCode = submitRequest(method, url, mediaType, entity);
798 // Check the status code of the response: does it match
799 // the expected response(s)?
800 if(logger.isDebugEnabled()){
801 logger.debug(testName + ": url=" + url +
802 " status=" + statusCode);
804 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
805 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
806 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
810 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
811 dependsOnMethods = {"create", "update", "testSubmitRequest"})
812 public void updateWithMalformedXml() throws Exception {
814 if (logger.isDebugEnabled()) {
815 logger.debug(testBanner(testName, CLASS_NAME));
818 setupUpdateWithMalformedXml();
820 // Submit the request to the service and store the response.
821 String method = REQUEST_TYPE.httpMethodName();
822 String url = getResourceURL(knownResourceId);
823 final String entity = MALFORMED_XML_DATA;
824 String mediaType = MediaType.APPLICATION_XML;
825 int statusCode = submitRequest(method, url, mediaType, entity);
827 // Check the status code of the response: does it match
828 // the expected response(s)?
829 if(logger.isDebugEnabled()){
830 logger.debug(testName + ": url=" + url +
831 " status=" + statusCode);
833 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
834 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
835 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
839 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
840 dependsOnMethods = {"create", "update", "testSubmitRequest"})
841 public void updateWithWrongXmlSchema(String testName) throws Exception {
843 if (logger.isDebugEnabled()) {
844 logger.debug(testBanner(testName, CLASS_NAME));
847 setupUpdateWithWrongXmlSchema();
849 // Submit the request to the service and store the response.
850 String method = REQUEST_TYPE.httpMethodName();
851 String url = getResourceURL(knownResourceId);
852 String mediaType = MediaType.APPLICATION_XML;
853 final String entity = WRONG_XML_SCHEMA_DATA;
854 int statusCode = submitRequest(method, url, mediaType, entity);
856 // Check the status code of the response: does it match
857 // the expected response(s)?
858 if(logger.isDebugEnabled()){
859 logger.debug(testName + ": url=" + url +
860 " status=" + statusCode);
862 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
863 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
864 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
869 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
872 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
873 dependsOnMethods = {"update", "testSubmitRequest"})
874 public void updateNonExistent(String testName) throws Exception {
876 if (logger.isDebugEnabled()) {
877 logger.debug(testBanner(testName, CLASS_NAME));
880 setupUpdateNonExistent();
882 // Submit the request to the service and store the response.
884 // Note: The ID used in this 'create' call may be arbitrary.
885 // The only relevant ID may be the one used in updateCollectionObject(), below.
886 CollectionObjectClient client = new CollectionObjectClient();
887 MultipartOutput multipart =
888 createCollectionObjectInstance(client.getCommonPartName(),
890 ClientResponse<MultipartInput> res =
891 client.update(NON_EXISTENT_ID, multipart);
892 int statusCode = res.getStatus();
894 // Check the status code of the response: does it match
895 // the expected response(s)?
896 if (logger.isDebugEnabled()) {
897 logger.debug(testName + ": status = " + statusCode);
899 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
900 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
901 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
905 * Test how the service handles, in an Update request, payloads
906 * containing null values (or, in the case of String fields,
907 * empty String values) in one or more fields in which non-empty
908 * values are required.
910 * This is a test of code and/or configuration in the service's
911 * validation routine(s).
913 * @param testName The name of this test method. This name is supplied
914 * automatically, via reflection, by a TestNG 'data provider' in
918 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
919 dependsOnMethods = {"read"})
920 public void updateWithRequiredValuesNullOrEmpty(String testName) throws Exception {
922 if (logger.isDebugEnabled()) {
923 logger.debug(testBanner(testName, CLASS_NAME));
927 if (logger.isDebugEnabled()) {
928 logger.debug(testName + " got object to update with ID: " + knownResourceId);
931 // Read an existing record for updating.
932 ClientResponse<MultipartInput> res = updateRetrieve(testName, knownResourceId);
934 CollectionObjectClient client = new CollectionObjectClient();
935 MultipartInput input = (MultipartInput) res.getEntity();
936 CollectionobjectsCommon collectionObject =
937 (CollectionobjectsCommon) extractPart(input,
938 client.getCommonPartName(), CollectionobjectsCommon.class);
939 Assert.assertNotNull(collectionObject);
941 // Update with invalid content, by setting a value to the
942 // empty String, in a field that requires a non-empty value,
943 // as enforced by the service's validation routine(s).
944 collectionObject.setObjectNumber("");
946 if (logger.isDebugEnabled()) {
947 logger.debug(testName + " updated object");
948 logger.debug(objectAsXmlString(collectionObject,
949 CollectionobjectsCommon.class));
952 // Submit the request to the service and store the response.
953 res = updateSend(testName, knownResourceId, collectionObject);
954 int statusCode = res.getStatus();
956 // Read the response and verify that the update attempt failed.
957 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
958 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
959 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
963 // ---------------------------------------------------------------
964 // CRUD tests : DELETE tests
965 // ---------------------------------------------------------------
968 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
971 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
972 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
973 public void delete(String testName) throws Exception {
975 if (logger.isDebugEnabled()) {
976 logger.debug(testBanner(testName, CLASS_NAME));
981 // Submit the request to the service and store the response.
982 CollectionObjectClient client = new CollectionObjectClient();
983 ClientResponse<Response> res = client.delete(knownResourceId);
984 int statusCode = res.getStatus();
986 // Check the status code of the response: does it match
987 // the expected response(s)?
988 if (logger.isDebugEnabled()) {
989 logger.debug(testName + ": status = " + statusCode);
991 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
992 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
993 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
998 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
1001 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
1002 dependsOnMethods = {"delete"})
1003 public void deleteNonExistent(String testName) throws Exception {
1005 if (logger.isDebugEnabled()) {
1006 logger.debug(testBanner(testName, CLASS_NAME));
1009 setupDeleteNonExistent();
1011 // Submit the request to the service and store the response.
1012 CollectionObjectClient client = new CollectionObjectClient();
1013 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
1014 int statusCode = res.getStatus();
1016 // Check the status code of the response: does it match
1017 // the expected response(s)?
1018 if (logger.isDebugEnabled()) {
1019 logger.debug(testName + ": status = " + statusCode);
1021 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1022 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1023 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1026 // ---------------------------------------------------------------
1027 // Utility tests : tests of code used in tests above
1028 // ---------------------------------------------------------------
1030 * Tests the code for manually submitting data that is used by several
1031 * of the methods above.
1035 @Test(dependsOnMethods = {"create", "read"})
1036 public void testSubmitRequest() throws Exception {
1037 testSubmitRequest(knownResourceId);
1041 * Test submit request.
1043 * @param resourceId the resource id
1044 * @throws Exception the exception
1046 private void testSubmitRequest(String resourceId) throws Exception {
1048 // Expected status code: 200 OK
1049 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1051 // Submit the request to the service and store the response.
1052 String method = ServiceRequestType.READ.httpMethodName();
1053 String url = getResourceURL(resourceId);
1054 int statusCode = submitRequest(method, url);
1056 // Check the status code of the response: does it match
1057 // the expected response(s)?
1058 if (logger.isDebugEnabled()) {
1059 logger.debug("testSubmitRequest: url=" + url
1060 + " status=" + statusCode);
1062 Assert.assertEquals(statusCode, EXPECTED_STATUS);
1066 // ---------------------------------------------------------------
1067 // Utility methods used by tests above
1068 // ---------------------------------------------------------------
1070 * Creates the collection object instance.
1072 * @param commonPartName the common part name
1073 * @param identifier the identifier
1074 * @return the multipart output
1076 private MultipartOutput createCollectionObjectInstance(String commonPartName,
1077 String identifier) {
1078 return createCollectionObjectInstance(commonPartName,
1079 "objectNumber-" + identifier,
1080 "objectName-" + identifier);
1084 * Creates the collection object instance.
1086 * @param commonPartName the common part name
1087 * @param objectNumber the object number
1088 * @param objectName the object name
1089 * @return the multipart output
1091 private MultipartOutput createCollectionObjectInstance(String commonPartName,
1092 String objectNumber, String objectName) {
1093 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
1095 BriefDescriptionList descriptionList = new BriefDescriptionList();
1096 List<String> descriptions = descriptionList.getBriefDescription();
1097 descriptions.add("Papier mache bird cow mask with horns, "
1098 + "painted red with black and yellow spots. "
1099 + "Puerto Rico. ca. 8" high, 6" wide, projects 10" (with horns).");
1100 descriptions.add("Papier mache bird cow mask with horns, "
1101 + "painted red with black and yellow spots. "
1102 + "Puerto Rico. ca. 8" high, 6" wide, projects 10" (with horns).");
1104 ResponsibleDepartmentList deptList = new ResponsibleDepartmentList();
1105 List<String> depts = deptList.getResponsibleDepartment();
1106 // @TODO Use properly formatted refNames for representative departments
1107 // in this example test record. The following are mere placeholders.
1108 depts.add("urn:org.collectionspace.services.department:Registrar");
1109 depts.add("urn:org.walkerart.department:Fine Art");
1111 OtherNumberList otherNumList = new OtherNumberList();
1112 List<OtherNumber> otherNumbers = otherNumList.getOtherNumber();
1114 OtherNumber otherNumber1 = new OtherNumber();
1115 otherNumber1.setNumberValue("101." + objectName);
1116 otherNumber1.setNumberType("integer");
1117 otherNumbers.add(otherNumber1);
1119 OtherNumber otherNumber2 = new OtherNumber();
1120 otherNumber2.setNumberValue("101.502.23.456." + objectName);
1121 otherNumber2.setNumberType("ipaddress");
1122 otherNumbers.add(otherNumber2);
1124 //FIXME: Title does not need to be set.
1125 collectionObject.setTitle("atitle");
1126 collectionObject.setResponsibleDepartments(deptList);
1127 collectionObject.setObjectNumber(objectNumber);
1129 collectionObject.setOtherNumberList(otherNumList);
1131 // FIXME this can be removed when the repeatable other number list
1132 // is supported by the application layers
1133 collectionObject.setOtherNumber("urn:org.walkerart.id:123");
1135 collectionObject.setObjectName(objectName);
1136 collectionObject.setAge(""); //test for null string
1137 collectionObject.setBriefDescriptions(descriptionList);
1139 CollectionobjectsNaturalhistory conh = new CollectionobjectsNaturalhistory();
1140 conh.setNhString("test-string");
1142 conh.setNhLong(9999);
1145 MultipartOutput multipart = createCollectionObjectInstance(commonPartName, collectionObject, conh);
1150 * Creates the collection object instance.
1152 * @param commonPartName the common part name
1153 * @param collectionObject the collection object
1154 * @param conh the conh
1155 * @return the multipart output
1157 private MultipartOutput createCollectionObjectInstance(String commonPartName,
1158 CollectionobjectsCommon collectionObject, CollectionobjectsNaturalhistory conh) {
1160 MultipartOutput multipart = CollectionObjectFactory.createCollectionObjectInstance(
1161 commonPartName, collectionObject, getNHPartName(), conh);
1162 if (logger.isDebugEnabled()) {
1163 logger.debug("to be created, collectionobject common");
1164 logger.debug(objectAsXmlString(collectionObject,
1165 CollectionobjectsCommon.class));
1169 if (logger.isDebugEnabled()) {
1170 logger.debug("to be created, collectionobject nhistory");
1171 logger.debug(objectAsXmlString(conh,
1172 CollectionobjectsNaturalhistory.class));
1180 * createCollectionObjectInstanceFromXml uses JAXB unmarshaller to retrieve
1181 * collectionobject from given file
1182 * @param commonPartName
1183 * @param commonPartFileName
1187 private MultipartOutput createCollectionObjectInstanceFromXml(String testName, String commonPartName,
1188 String commonPartFileName) throws Exception {
1190 CollectionobjectsCommon collectionObject =
1191 (CollectionobjectsCommon) getObjectFromFile(CollectionobjectsCommon.class,
1192 commonPartFileName);
1193 MultipartOutput multipart = new MultipartOutput();
1194 OutputPart commonPart = multipart.addPart(collectionObject,
1195 MediaType.APPLICATION_XML_TYPE);
1196 commonPart.getHeaders().add("label", commonPartName);
1198 if (logger.isDebugEnabled()) {
1199 logger.debug(testName + " to be created, collectionobject common");
1200 logger.debug(objectAsXmlString(collectionObject,
1201 CollectionobjectsCommon.class));
1208 * createCollectionObjectInstanceFromRawXml uses stringified collectionobject
1209 * retrieve from given file
1210 * @param commonPartName
1211 * @param commonPartFileName
1215 private MultipartOutput createCollectionObjectInstanceFromRawXml(String testName, String commonPartName,
1216 String commonPartFileName) throws Exception {
1218 MultipartOutput multipart = new MultipartOutput();
1219 String stringObject = getXmlDocumentAsString(commonPartFileName);
1220 if (logger.isDebugEnabled()) {
1221 logger.debug(testName + " to be created, collectionobject common " + "\n" + stringObject);
1223 OutputPart commonPart = multipart.addPart(stringObject,
1224 MediaType.APPLICATION_XML_TYPE);
1225 commonPart.getHeaders().add("label", commonPartName);
1232 * Gets the nH part name.
1234 * @return the nH part name
1236 private String getNHPartName() {
1237 return "collectionobjects_naturalhistory";
1241 * Creates the from xml file.
1243 * @param testName the test name
1244 * @param fileName the file name
1245 * @param useJaxb the use jaxb
1246 * @return the string
1247 * @throws Exception the exception
1249 private String createFromXmlFile(String testName, String fileName, boolean useJaxb) throws Exception {
1254 MultipartOutput multipart = null;
1256 CollectionObjectClient client = new CollectionObjectClient();
1258 multipart = createCollectionObjectInstanceFromXml(testName,
1259 client.getCommonPartName(), fileName);
1261 multipart = createCollectionObjectInstanceFromRawXml(testName,
1262 client.getCommonPartName(), fileName);
1264 ClientResponse<Response> res = client.create(multipart);
1265 int statusCode = res.getStatus();
1267 if (logger.isDebugEnabled()) {
1268 logger.debug(testName + ": status = " + statusCode);
1270 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1271 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1272 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1273 String newId = extractId(res);
1274 allResourceIdsCreated.add(newId);
1278 // FIXME: This duplicates code in read(), and should be consolidated.
1279 // This is an expedient to support reading and verifying the contents
1280 // of resources that have been created from test data XML files.
1281 private CollectionobjectsCommon readCollectionObjectCommonPart(String csid)
1284 String testName = "readCollectionObjectCommonPart";
1288 // Submit the request to the service and store the response.
1289 CollectionObjectClient client = new CollectionObjectClient();
1290 ClientResponse<MultipartInput> res = client.read(csid);
1291 int statusCode = res.getStatus();
1293 // Check the status code of the response: does it match
1294 // the expected response(s)?
1295 if (logger.isDebugEnabled()) {
1296 logger.debug(testName + ": status = " + statusCode);
1298 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1299 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1300 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1302 MultipartInput input = (MultipartInput) res.getEntity();
1304 if (logger.isDebugEnabled()) {
1305 logger.debug(testName + ": Reading Common part ...");
1307 CollectionobjectsCommon collectionObject =
1308 (CollectionobjectsCommon) extractPart(input,
1309 client.getCommonPartName(), CollectionobjectsCommon.class);
1310 Assert.assertNotNull(collectionObject);
1312 return collectionObject;