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.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
32 import org.collectionspace.services.LocationJAXBSchema;
33 import org.collectionspace.services.client.CollectionSpaceClient;
34 import org.collectionspace.services.client.ContactClient;
35 import org.collectionspace.services.client.ContactClientUtils;
36 import org.collectionspace.services.contact.ContactsCommon;
37 import org.collectionspace.services.contact.ContactsCommonList;
38 import org.collectionspace.services.client.LocationAuthorityClient;
39 import org.collectionspace.services.client.LocationAuthorityClientUtils;
40 import org.collectionspace.services.jaxb.AbstractCommonList;
41 import org.collectionspace.services.location.LocationauthoritiesCommon;
42 import org.collectionspace.services.location.LocationauthoritiesCommonList;
43 import org.collectionspace.services.location.LocationsCommon;
44 import org.collectionspace.services.location.LocationsCommonList;
45 import org.jboss.resteasy.client.ClientResponse;
46 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
47 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
48 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.testng.Assert;
52 import org.testng.annotations.AfterClass;
53 import org.testng.annotations.Test;
56 * LocationAuthorityServiceTest, carries out tests against a
57 * deployed and running LocationAuthority Service.
59 * $LastChangedRevision: 753 $
60 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
62 public class LocationAuthorityServiceTest extends AbstractServiceTestImpl {
65 private final Logger logger =
66 LoggerFactory.getLogger(LocationAuthorityServiceTest.class);
68 // Instance variables specific to this test.
69 /** The SERVICE path component. */
70 final String SERVICE_PATH_COMPONENT = "locationauthorities";
72 /** The ITEM servicE path component. */
73 final String ITEM_SERVICE_PATH_COMPONENT = "items";
75 /** The CONTACT servicE path component. */
76 final String CONTACT_SERVICE_PATH_COMPONENT = "contacts";
79 final String TEST_NAME = "Shelf 1";
81 /** The TEST conditionNote. */
82 final String TEST_CONDITION_NOTE = "Basically clean";
84 /** The TEST death date. */
85 final String TEST_CONDITION_NOTE_DATE = "June 11, 1979";
87 /** The TEST securityNote. */
88 final String TEST_SECURITY_NOTE = "Kind of safe";
90 /** The TEST location type. */
91 // TODO Make loc type be a controlled vocab term.
92 final String TEST_LOCATION_TYPE = "Shelf";
94 /** The TEST location type. */
95 // TODO Make status type be a controlled vocab term.
96 final String TEST_STATUS = "Approved";
98 /** The known resource id. */
99 private String knownResourceId = null;
101 /** The known resource display name. */
102 private String knownResourceDisplayName = null;
104 /** The known resource ref name. */
105 private String knownResourceRefName = null;
107 /** The known locationType ref name. */
108 private String knownLocationTypeRefName = null;
110 /** The known item resource id. */
111 private String knownItemResourceId = null;
113 /** The known contact resource id. */
114 private String knownContactResourceId = null;
116 /** The n items to create in list. */
117 private int nItemsToCreateInList = 3;
119 /** The all resource ids created. */
120 private List<String> allResourceIdsCreated = new ArrayList<String>();
122 /** The all item resource ids created. */
123 private Map<String, String> allItemResourceIdsCreated =
124 new HashMap<String, String>();
127 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
130 protected CollectionSpaceClient getClientInstance() {
131 return new LocationAuthorityClient();
135 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
138 protected AbstractCommonList getAbstractCommonList(
139 ClientResponse<AbstractCommonList> response) {
140 return response.getEntity(LocationsCommonList.class);
143 // ---------------------------------------------------------------
144 // CRUD tests : CREATE tests
145 // ---------------------------------------------------------------
148 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
151 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
153 public void create(String testName) throws Exception {
155 // Perform setup, such as initializing the type of service request
156 // (e.g. CREATE, DELETE), its valid and expected status codes, and
157 // its associated HTTP method name (e.g. POST, DELETE).
158 setupCreate(testName);
160 // Submit the request to the service and store the response.
161 LocationAuthorityClient client = new LocationAuthorityClient();
162 String identifier = createIdentifier();
163 String displayName = "displayName-" + identifier;
164 String baseRefName = LocationAuthorityClientUtils.createLocationAuthRefName(displayName, false);
165 String fullRefName = LocationAuthorityClientUtils.createLocationAuthRefName(displayName, true);
166 MultipartOutput multipart =
167 LocationAuthorityClientUtils.createLocationAuthorityInstance(
168 displayName, fullRefName, client.getCommonPartName());
169 ClientResponse<Response> res = client.create(multipart);
170 int statusCode = res.getStatus();
172 // Check the status code of the response: does it match
173 // the expected response(s)?
176 // Does it fall within the set of valid status codes?
177 // Does it exactly match the expected status code?
178 if(logger.isDebugEnabled()){
179 logger.debug(testName + ": status = " + statusCode);
181 Assert.assertTrue(this.REQUEST_TYPE.isValidStatusCode(statusCode),
182 invalidStatusCodeMessage(this.REQUEST_TYPE, statusCode));
183 Assert.assertEquals(statusCode, this.EXPECTED_STATUS_CODE);
185 // Store the refname from the first resource created
186 // for additional tests below.
187 knownResourceRefName = baseRefName;
189 String newID = LocationAuthorityClientUtils.extractId(res);
190 // Store the ID returned from the first resource created
191 // for additional tests below.
192 if (knownResourceId == null){
193 knownResourceId = newID;
194 knownResourceDisplayName = displayName;
195 if (logger.isDebugEnabled()) {
196 logger.debug(testName + ": knownResourceId=" + knownResourceId);
199 // Store the IDs from every resource created by tests,
200 // so they can be deleted after tests have been run.
201 allResourceIdsCreated.add(newID);
207 * @param testName the test name
209 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
210 groups = {"create"}, dependsOnMethods = {"create"})
211 public void createItem(String testName) {
212 setupCreate(testName);
213 String newID = createItemInAuthority(knownResourceId, knownResourceRefName);
217 * Creates the item in authority.
219 * @param vcsid the vcsid
220 * @param authRefName the auth ref name
223 private String createItemInAuthority(String vcsid, String authRefName) {
225 final String testName = "createItemInAuthority";
226 if(logger.isDebugEnabled()){
227 logger.debug(testName + ":...");
230 // Submit the request to the service and store the response.
231 LocationAuthorityClient client = new LocationAuthorityClient();
232 String identifier = createIdentifier();
233 String refName = LocationAuthorityClientUtils.createLocationRefName(authRefName, TEST_NAME, true);
234 Map<String, String> shelf1Map = new HashMap<String,String>();
235 // TODO Make loc type and status be controlled vocabs.
236 shelf1Map.put(LocationJAXBSchema.NAME, TEST_NAME);
237 shelf1Map.put(LocationJAXBSchema.CONDITION_NOTE, TEST_CONDITION_NOTE);
238 shelf1Map.put(LocationJAXBSchema.CONDITION_NOTE_DATE, TEST_CONDITION_NOTE_DATE);
239 shelf1Map.put(LocationJAXBSchema.SECURITY_NOTE, TEST_SECURITY_NOTE);
240 shelf1Map.put(LocationJAXBSchema.LOCATION_TYPE, TEST_LOCATION_TYPE);
241 shelf1Map.put(LocationJAXBSchema.STATUS, TEST_STATUS);
242 MultipartOutput multipart =
243 LocationAuthorityClientUtils.createLocationInstance(vcsid, refName, shelf1Map,
244 client.getItemCommonPartName() );
245 ClientResponse<Response> res = client.createItem(vcsid, multipart);
246 int statusCode = res.getStatus();
247 String newID = LocationAuthorityClientUtils.extractId(res);
249 // Check the status code of the response: does it match
250 // the expected response(s)?
251 if(logger.isDebugEnabled()){
252 logger.debug(testName + ": status = " + statusCode);
254 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
255 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
256 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
258 // Store the ID returned from the first item resource created
259 // for additional tests below.
260 if (knownItemResourceId == null){
261 knownItemResourceId = newID;
262 if (logger.isDebugEnabled()) {
263 logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
267 // Store the IDs from any item resources created
268 // by tests, along with the IDs of their parents, so these items
269 // can be deleted after all tests have been run.
270 allItemResourceIdsCreated.put(newID, vcsid);
279 // Placeholders until the three tests below can be uncommented.
280 // See Issue CSPACE-401.
282 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
285 public void createWithEmptyEntityBody(String testName) throws Exception {
289 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
292 public void createWithMalformedXml(String testName) throws Exception {
296 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
299 public void createWithWrongXmlSchema(String testName) throws Exception {
303 // ---------------------------------------------------------------
304 // CRUD tests : CREATE LIST tests
305 // ---------------------------------------------------------------
308 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
311 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
312 groups = {"createList"}, dependsOnGroups = {"create"})
313 public void createList(String testName) throws Exception {
314 for (int i = 0; i < nItemsToCreateInList; i++) {
320 * Creates the item list.
322 * @param testName the test name
323 * @throws Exception the exception
325 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
326 groups = {"createList"}, dependsOnMethods = {"createList"})
327 public void createItemList(String testName) throws Exception {
328 // Add items to the initially-created, known parent record.
329 for (int j = 0; j < nItemsToCreateInList; j++) {
330 createItem(testName);
334 // ---------------------------------------------------------------
335 // CRUD tests : READ tests
336 // ---------------------------------------------------------------
339 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
342 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
343 groups = {"read"}, dependsOnGroups = {"create"})
344 public void read(String testName) throws Exception {
349 // Submit the request to the service and store the response.
350 LocationAuthorityClient client = new LocationAuthorityClient();
351 ClientResponse<MultipartInput> res = client.read(knownResourceId);
352 int statusCode = res.getStatus();
354 // Check the status code of the response: does it match
355 // the expected response(s)?
356 if(logger.isDebugEnabled()){
357 logger.debug(testName + ": status = " + statusCode);
359 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
360 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
361 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
362 //FIXME: remove the following try catch once Aron fixes signatures
364 MultipartInput input = (MultipartInput) res.getEntity();
365 LocationauthoritiesCommon locationAuthority = (LocationauthoritiesCommon) extractPart(input,
366 client.getCommonPartName(), LocationauthoritiesCommon.class);
367 Assert.assertNotNull(locationAuthority);
368 } catch (Exception e) {
369 throw new RuntimeException(e);
376 * @param testName the test name
377 * @throws Exception the exception
379 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
380 groups = {"read"}, dependsOnGroups = {"create"})
381 public void readByName(String testName) throws Exception {
386 // Submit the request to the service and store the response.
387 LocationAuthorityClient client = new LocationAuthorityClient();
388 ClientResponse<MultipartInput> res = client.readByName(knownResourceDisplayName);
389 int statusCode = res.getStatus();
391 // Check the status code of the response: does it match
392 // the expected response(s)?
393 if(logger.isDebugEnabled()){
394 logger.debug(testName + ": status = " + statusCode);
396 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
397 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
398 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
399 //FIXME: remove the following try catch once Aron fixes signatures
401 MultipartInput input = (MultipartInput) res.getEntity();
402 LocationauthoritiesCommon locationAuthority = (LocationauthoritiesCommon) extractPart(input,
403 client.getCommonPartName(), LocationauthoritiesCommon.class);
404 Assert.assertNotNull(locationAuthority);
405 } catch (Exception e) {
406 throw new RuntimeException(e);
414 * @param testName the test name
415 * @throws Exception the exception
417 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
418 groups = {"read"}, dependsOnMethods = {"read"})
419 public void readItem(String testName) throws Exception {
424 // Submit the request to the service and store the response.
425 LocationAuthorityClient client = new LocationAuthorityClient();
426 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
427 int statusCode = res.getStatus();
429 // Check the status code of the response: does it match
430 // the expected response(s)?
431 if(logger.isDebugEnabled()){
432 logger.debug(testName + ": status = " + statusCode);
434 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
435 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
436 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
438 // Check whether we've received a location.
439 MultipartInput input = (MultipartInput) res.getEntity();
440 LocationsCommon location = (LocationsCommon) extractPart(input,
441 client.getItemCommonPartName(), LocationsCommon.class);
442 Assert.assertNotNull(location);
443 boolean showFull = true;
444 if(showFull && logger.isDebugEnabled()){
445 logger.debug(testName + ": returned payload:");
446 logger.debug(objectAsXmlString(location, LocationsCommon.class));
448 Assert.assertEquals(location.getInAuthority(), knownResourceId);
453 * Verify item display name.
455 * @param testName the test name
456 * @throws Exception the exception
458 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
459 dependsOnMethods = {"readItem", "updateItem"})
460 public void verifyItemDisplayName(String testName) throws Exception {
463 setupUpdate(testName);
465 // Submit the request to the service and store the response.
466 LocationAuthorityClient client = new LocationAuthorityClient();
467 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
468 int statusCode = res.getStatus();
470 // Check the status code of the response: does it match
471 // the expected response(s)?
472 if(logger.isDebugEnabled()){
473 logger.debug(testName + ": status = " + statusCode);
475 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
476 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
477 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
479 // Check whether location has expected displayName.
480 MultipartInput input = (MultipartInput) res.getEntity();
481 LocationsCommon location = (LocationsCommon) extractPart(input,
482 client.getItemCommonPartName(), LocationsCommon.class);
483 Assert.assertNotNull(location);
484 String displayName = location.getDisplayName();
485 // Make sure displayName matches computed form
486 String expectedDisplayName =
487 LocationAuthorityClientUtils.prepareDefaultDisplayName(TEST_NAME);
488 Assert.assertNotNull(displayName, expectedDisplayName);
490 // Update the shortName and verify the computed name is updated.
491 location.setCsid(null);
492 location.setDisplayNameComputed(true);
493 location.setName("updated-" + TEST_NAME);
494 expectedDisplayName =
495 LocationAuthorityClientUtils.prepareDefaultDisplayName("updated-" + TEST_NAME);
497 // Submit the updated resource to the service and store the response.
498 MultipartOutput output = new MultipartOutput();
499 OutputPart commonPart = output.addPart(location, MediaType.APPLICATION_XML_TYPE);
500 commonPart.getHeaders().add("label", client.getItemCommonPartName());
501 res = client.updateItem(knownResourceId, knownItemResourceId, output);
502 statusCode = res.getStatus();
504 // Check the status code of the response: does it match the expected response(s)?
505 if(logger.isDebugEnabled()){
506 logger.debug("updateItem: status = " + statusCode);
508 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
509 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
510 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
512 // Retrieve the updated resource and verify that its contents exist.
513 input = (MultipartInput) res.getEntity();
514 LocationsCommon updatedLocation =
515 (LocationsCommon) extractPart(input,
516 client.getItemCommonPartName(), LocationsCommon.class);
517 Assert.assertNotNull(updatedLocation);
519 // Verify that the updated resource received the correct data.
520 Assert.assertEquals(updatedLocation.getName(), location.getName(),
521 "Updated ForeName in Location did not match submitted data.");
522 // Verify that the updated resource computes the right displayName.
523 Assert.assertEquals(updatedLocation.getDisplayName(), expectedDisplayName,
524 "Updated ForeName in Location not reflected in computed DisplayName.");
526 // Now Update the displayName, not computed and verify the computed name is overriden.
527 location.setDisplayNameComputed(false);
528 expectedDisplayName = "TestName";
529 location.setDisplayName(expectedDisplayName);
531 // Submit the updated resource to the service and store the response.
532 output = new MultipartOutput();
533 commonPart = output.addPart(location, MediaType.APPLICATION_XML_TYPE);
534 commonPart.getHeaders().add("label", client.getItemCommonPartName());
535 res = client.updateItem(knownResourceId, knownItemResourceId, output);
536 statusCode = res.getStatus();
538 // Check the status code of the response: does it match the expected response(s)?
539 if(logger.isDebugEnabled()){
540 logger.debug("updateItem: status = " + statusCode);
542 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
543 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
544 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
546 // Retrieve the updated resource and verify that its contents exist.
547 input = (MultipartInput) res.getEntity();
549 (LocationsCommon) extractPart(input,
550 client.getItemCommonPartName(), LocationsCommon.class);
551 Assert.assertNotNull(updatedLocation);
553 // Verify that the updated resource received the correct data.
554 Assert.assertEquals(updatedLocation.isDisplayNameComputed(), false,
555 "Updated displayNameComputed in Location did not match submitted data.");
556 // Verify that the updated resource computes the right displayName.
557 Assert.assertEquals(updatedLocation.getDisplayName(),
559 "Updated DisplayName (not computed) in Location not stored.");
563 * Verify illegal item display name.
565 * @param testName the test name
566 * @throws Exception the exception
568 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
569 dependsOnMethods = {"verifyItemDisplayName"})
570 public void verifyIllegalItemDisplayName(String testName) throws Exception {
573 setupUpdateWithWrongXmlSchema(testName);
575 // Submit the request to the service and store the response.
576 LocationAuthorityClient client = new LocationAuthorityClient();
577 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
578 int statusCode = res.getStatus();
580 // Check the status code of the response: does it match
581 // the expected response(s)?
582 if(logger.isDebugEnabled()){
583 logger.debug(testName + ": status = " + statusCode);
585 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
586 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
587 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
589 // Check whether Location has expected displayName.
590 MultipartInput input = (MultipartInput) res.getEntity();
591 LocationsCommon location = (LocationsCommon) extractPart(input,
592 client.getItemCommonPartName(), LocationsCommon.class);
593 Assert.assertNotNull(location);
594 // Try to Update with computed false and no displayName
595 location.setDisplayNameComputed(false);
596 location.setDisplayName(null);
598 // Submit the updated resource to the service and store the response.
599 MultipartOutput output = new MultipartOutput();
600 OutputPart commonPart = output.addPart(location, MediaType.APPLICATION_XML_TYPE);
601 commonPart.getHeaders().add("label", client.getItemCommonPartName());
602 res = client.updateItem(knownResourceId, knownItemResourceId, output);
603 statusCode = res.getStatus();
605 // Check the status code of the response: does it match the expected response(s)?
606 if(logger.isDebugEnabled()){
607 logger.debug("updateItem: status = " + statusCode);
609 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
610 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
611 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
617 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
620 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
621 groups = {"read"}, dependsOnMethods = {"read"})
622 public void readNonExistent(String testName) {
625 setupReadNonExistent(testName);
627 // Submit the request to the service and store the response.
628 LocationAuthorityClient client = new LocationAuthorityClient();
629 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
630 int statusCode = res.getStatus();
632 // Check the status code of the response: does it match
633 // the expected response(s)?
634 if(logger.isDebugEnabled()){
635 logger.debug(testName + ": status = " + statusCode);
637 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
638 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
639 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
643 * Read item non existent.
645 * @param testName the test name
647 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
648 groups = {"read"}, dependsOnMethods = {"readItem"})
649 public void readItemNonExistent(String testName) {
652 setupReadNonExistent(testName);
654 // Submit the request to the service and store the response.
655 LocationAuthorityClient client = new LocationAuthorityClient();
656 ClientResponse<MultipartInput> res = client.readItem(knownResourceId, NON_EXISTENT_ID);
657 int statusCode = res.getStatus();
659 // Check the status code of the response: does it match
660 // the expected response(s)?
661 if(logger.isDebugEnabled()){
662 logger.debug(testName + ": status = " + statusCode);
664 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
665 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
666 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
670 // ---------------------------------------------------------------
671 // CRUD tests : READ_LIST tests
672 // ---------------------------------------------------------------
676 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
679 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
680 groups = {"readList"}, dependsOnGroups = {"createList", "read"})
681 public void readList(String testName) throws Exception {
684 setupReadList(testName);
686 // Submit the request to the service and store the response.
687 LocationAuthorityClient client = new LocationAuthorityClient();
688 ClientResponse<LocationauthoritiesCommonList> res = client.readList();
689 LocationauthoritiesCommonList list = res.getEntity();
690 int statusCode = res.getStatus();
692 // Check the status code of the response: does it match
693 // the expected response(s)?
694 if(logger.isDebugEnabled()){
695 logger.debug(testName + ": status = " + statusCode);
697 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
698 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
699 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
701 // Optionally output additional data about list members for debugging.
702 boolean iterateThroughList = false;
703 if (iterateThroughList && logger.isDebugEnabled()) {
704 List<LocationauthoritiesCommonList.LocationauthorityListItem> items =
705 list.getLocationauthorityListItem();
707 for (LocationauthoritiesCommonList.LocationauthorityListItem item : items) {
708 String csid = item.getCsid();
709 logger.debug(testName + ": list-item[" + i + "] csid=" +
711 logger.debug(testName + ": list-item[" + i + "] displayName=" +
712 item.getDisplayName());
713 logger.debug(testName + ": list-item[" + i + "] URI=" +
715 readItemList(csid, null);
724 @Test(groups = {"readList"}, dependsOnMethods = {"readList"})
725 public void readItemList() {
726 readItemList(knownResourceId, null);
730 * Read item list by authority name.
732 @Test(groups = {"readList"}, dependsOnMethods = {"readItemList"})
733 public void readItemListByAuthorityName() {
734 readItemList(null, knownResourceDisplayName);
740 * @param vcsid the vcsid
741 * @param name the name
743 private void readItemList(String vcsid, String name) {
745 final String testName = "readItemList";
748 setupReadList(testName);
750 // Submit the request to the service and store the response.
751 LocationAuthorityClient client = new LocationAuthorityClient();
752 ClientResponse<LocationsCommonList> res = null;
754 res = client.readItemList(vcsid);
755 } else if(name!= null) {
756 res = client.readItemListForNamedAuthority(name);
758 Assert.fail("readItemList passed null csid and name!");
760 LocationsCommonList list = res.getEntity();
761 int statusCode = res.getStatus();
763 // Check the status code of the response: does it match
764 // the expected response(s)?
765 if(logger.isDebugEnabled()){
766 logger.debug(testName + ": status = " + statusCode);
768 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
769 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
770 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
772 List<LocationsCommonList.LocationListItem> items =
773 list.getLocationListItem();
774 int nItemsReturned = items.size();
775 // There will be one item created, associated with a
776 // known parent resource, by the createItem test.
778 // In addition, there will be 'nItemsToCreateInList'
779 // additional items created by the createItemList test,
780 // all associated with the same parent resource.
781 int nExpectedItems = nItemsToCreateInList + 1;
782 if(logger.isDebugEnabled()){
783 logger.debug(testName + ": Expected "
784 + nExpectedItems +" items; got: "+nItemsReturned);
786 Assert.assertEquals(nItemsReturned, nExpectedItems);
789 for (LocationsCommonList.LocationListItem item : items) {
790 Assert.assertTrue((null != item.getRefName()), "Item refName is null!");
791 Assert.assertTrue((null != item.getDisplayName()), "Item displayName is null!");
792 // Optionally output additional data about list members for debugging.
793 boolean showDetails = true;
794 if (showDetails && logger.isDebugEnabled()) {
795 logger.debug(" " + testName + ": list-item[" + i + "] csid=" +
797 logger.debug(" " + testName + ": list-item[" + i + "] refName=" +
799 logger.debug(" " + testName + ": list-item[" + i + "] displayName=" +
800 item.getDisplayName());
801 logger.debug(" " + testName + ": list-item[" + i + "] URI=" +
812 // ---------------------------------------------------------------
813 // CRUD tests : UPDATE tests
814 // ---------------------------------------------------------------
817 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
820 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
821 groups = {"update"}, dependsOnGroups = {"read", "readList"})
822 public void update(String testName) throws Exception {
825 setupUpdate(testName);
827 // Retrieve the contents of a resource to update.
828 LocationAuthorityClient client = new LocationAuthorityClient();
829 ClientResponse<MultipartInput> res =
830 client.read(knownResourceId);
831 if(logger.isDebugEnabled()){
832 logger.debug(testName + ": read status = " + res.getStatus());
834 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
836 if(logger.isDebugEnabled()){
837 logger.debug("got LocationAuthority to update with ID: " + knownResourceId);
839 MultipartInput input = (MultipartInput) res.getEntity();
840 LocationauthoritiesCommon locationAuthority = (LocationauthoritiesCommon) extractPart(input,
841 client.getCommonPartName(), LocationauthoritiesCommon.class);
842 Assert.assertNotNull(locationAuthority);
844 // Update the contents of this resource.
845 locationAuthority.setDisplayName("updated-" + locationAuthority.getDisplayName());
846 locationAuthority.setVocabType("updated-" + locationAuthority.getVocabType());
847 if(logger.isDebugEnabled()){
848 logger.debug("to be updated LocationAuthority");
849 logger.debug(objectAsXmlString(locationAuthority, LocationauthoritiesCommon.class));
852 // Submit the updated resource to the service and store the response.
853 MultipartOutput output = new MultipartOutput();
854 OutputPart commonPart = output.addPart(locationAuthority, MediaType.APPLICATION_XML_TYPE);
855 commonPart.getHeaders().add("label", client.getCommonPartName());
856 res = client.update(knownResourceId, output);
857 int statusCode = res.getStatus();
859 // Check the status code of the response: does it match the expected response(s)?
860 if(logger.isDebugEnabled()){
861 logger.debug(testName + ": status = " + statusCode);
863 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
864 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
865 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
867 // Retrieve the updated resource and verify that its contents exist.
868 input = (MultipartInput) res.getEntity();
869 LocationauthoritiesCommon updatedLocationAuthority =
870 (LocationauthoritiesCommon) extractPart(input,
871 client.getCommonPartName(), LocationauthoritiesCommon.class);
872 Assert.assertNotNull(updatedLocationAuthority);
874 // Verify that the updated resource received the correct data.
875 Assert.assertEquals(updatedLocationAuthority.getDisplayName(),
876 locationAuthority.getDisplayName(),
877 "Data in updated object did not match submitted data.");
883 * @param testName the test name
884 * @throws Exception the exception
886 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
887 groups = {"update"}, dependsOnMethods = {"update"})
888 public void updateItem(String testName) throws Exception {
891 setupUpdate(testName);
893 // Retrieve the contents of a resource to update.
894 LocationAuthorityClient client = new LocationAuthorityClient();
895 ClientResponse<MultipartInput> res =
896 client.readItem(knownResourceId, knownItemResourceId);
897 if(logger.isDebugEnabled()){
898 logger.debug(testName + ": read status = " + res.getStatus());
900 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
902 if(logger.isDebugEnabled()){
903 logger.debug("got Location to update with ID: " +
904 knownItemResourceId +
905 " in LocationAuthority: " + knownResourceId );
907 MultipartInput input = (MultipartInput) res.getEntity();
908 LocationsCommon location = (LocationsCommon) extractPart(input,
909 client.getItemCommonPartName(), LocationsCommon.class);
910 Assert.assertNotNull(location);
912 // Update the contents of this resource.
913 location.setCsid(null);
914 location.setName("updated-" + location.getName());
915 if(logger.isDebugEnabled()){
916 logger.debug("to be updated Location");
917 logger.debug(objectAsXmlString(location,
918 LocationsCommon.class));
921 // Submit the updated resource to the service and store the response.
922 MultipartOutput output = new MultipartOutput();
923 OutputPart commonPart = output.addPart(location, MediaType.APPLICATION_XML_TYPE);
924 commonPart.getHeaders().add("label", client.getItemCommonPartName());
925 res = client.updateItem(knownResourceId, knownItemResourceId, output);
926 int statusCode = res.getStatus();
928 // Check the status code of the response: does it match the expected response(s)?
929 if(logger.isDebugEnabled()){
930 logger.debug(testName + ": status = " + statusCode);
932 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
933 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
934 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
936 // Retrieve the updated resource and verify that its contents exist.
937 input = (MultipartInput) res.getEntity();
938 LocationsCommon updatedLocation =
939 (LocationsCommon) extractPart(input,
940 client.getItemCommonPartName(), LocationsCommon.class);
941 Assert.assertNotNull(updatedLocation);
943 // Verify that the updated resource received the correct data.
944 Assert.assertEquals(updatedLocation.getName(), location.getName(),
945 "Data in updated Location did not match submitted data.");
949 // Placeholders until the three tests below can be uncommented.
950 // See Issue CSPACE-401.
952 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
955 public void updateWithEmptyEntityBody(String testName) throws Exception {
959 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
962 public void updateWithMalformedXml(String testName) throws Exception {
966 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
969 public void updateWithWrongXmlSchema(String testName) throws Exception {
974 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
977 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
978 groups = {"update"}, dependsOnMethods = {"update", "testSubmitRequest"})
979 public void updateNonExistent(String testName) throws Exception {
982 setupUpdateNonExistent(testName);
984 // Submit the request to the service and store the response.
985 // Note: The ID(s) used when creating the request payload may be arbitrary.
986 // The only relevant ID may be the one used in update(), below.
987 LocationAuthorityClient client = new LocationAuthorityClient();
988 String displayName = "displayName-NON_EXISTENT_ID";
989 String fullRefName = LocationAuthorityClientUtils.createLocationAuthRefName(displayName, true);
990 MultipartOutput multipart = LocationAuthorityClientUtils.createLocationAuthorityInstance(
991 displayName, fullRefName, client.getCommonPartName());
992 ClientResponse<MultipartInput> res =
993 client.update(NON_EXISTENT_ID, multipart);
994 int statusCode = res.getStatus();
996 // Check the status code of the response: does it match
997 // the expected response(s)?
998 if(logger.isDebugEnabled()){
999 logger.debug(testName + ": status = " + statusCode);
1001 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1002 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1003 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1007 * Update non existent item.
1009 * @param testName the test name
1010 * @throws Exception the exception
1012 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1013 groups = {"update"}, dependsOnMethods = {"updateItem", "testItemSubmitRequest"})
1014 public void updateNonExistentItem(String testName) throws Exception {
1017 setupUpdateNonExistent(testName);
1019 // Submit the request to the service and store the response.
1020 // Note: The ID used in this 'create' call may be arbitrary.
1021 // The only relevant ID may be the one used in update(), below.
1022 LocationAuthorityClient client = new LocationAuthorityClient();
1023 Map<String, String> nonexMap = new HashMap<String,String>();
1024 nonexMap.put(LocationJAXBSchema.NAME, TEST_NAME);
1025 nonexMap.put(LocationJAXBSchema.LOCATION_TYPE, TEST_LOCATION_TYPE);
1026 nonexMap.put(LocationJAXBSchema.STATUS, TEST_STATUS);
1027 MultipartOutput multipart =
1028 LocationAuthorityClientUtils.createLocationInstance(NON_EXISTENT_ID,
1029 LocationAuthorityClientUtils.createLocationRefName(NON_EXISTENT_ID, NON_EXISTENT_ID, true), nonexMap,
1030 client.getItemCommonPartName() );
1031 ClientResponse<MultipartInput> res =
1032 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
1033 int statusCode = res.getStatus();
1035 // Check the status code of the response: does it match
1036 // the expected response(s)?
1037 if(logger.isDebugEnabled()){
1038 logger.debug(testName + ": status = " + statusCode);
1040 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1041 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1042 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1045 // ---------------------------------------------------------------
1046 // CRUD tests : DELETE tests
1047 // ---------------------------------------------------------------
1050 // Note: delete sub-resources in ascending hierarchical order,
1051 // before deleting their parents.
1056 * @param testName the test name
1057 * @throws Exception the exception
1059 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1060 //groups = {"delete"}, dependsOnGroups = {"create", "read", "readList", "readListByPartialTerm", "update"})
1061 groups = {"delete"}, dependsOnGroups = {"create", "read", "readList", "update"})
1062 public void deleteItem(String testName) throws Exception {
1065 setupDelete(testName);
1067 if(logger.isDebugEnabled()){
1068 logger.debug("parentcsid =" + knownResourceId +
1069 " itemcsid = " + knownItemResourceId);
1072 // Submit the request to the service and store the response.
1073 LocationAuthorityClient client = new LocationAuthorityClient();
1074 ClientResponse<Response> res = client.deleteItem(knownResourceId, knownItemResourceId);
1075 int statusCode = res.getStatus();
1077 // Check the status code of the response: does it match
1078 // the expected response(s)?
1079 if(logger.isDebugEnabled()){
1080 logger.debug(testName + ": status = " + statusCode);
1082 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1083 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1084 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1088 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
1091 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1092 groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1093 public void delete(String testName) throws Exception {
1096 setupDelete(testName);
1098 if(logger.isDebugEnabled()){
1099 logger.debug("parentcsid =" + knownResourceId);
1102 // Submit the request to the service and store the response.
1103 LocationAuthorityClient client = new LocationAuthorityClient();
1104 ClientResponse<Response> res = client.delete(knownResourceId);
1105 int statusCode = res.getStatus();
1107 // Check the status code of the response: does it match
1108 // the expected response(s)?
1109 if(logger.isDebugEnabled()){
1110 logger.debug(testName + ": status = " + statusCode);
1112 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1113 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1114 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1119 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
1122 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1123 groups = {"delete"}, dependsOnMethods = {"delete"})
1124 public void deleteNonExistent(String testName) throws Exception {
1127 setupDeleteNonExistent(testName);
1129 // Submit the request to the service and store the response.
1130 LocationAuthorityClient client = new LocationAuthorityClient();
1131 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
1132 int statusCode = res.getStatus();
1134 // Check the status code of the response: does it match
1135 // the expected response(s)?
1136 if(logger.isDebugEnabled()){
1137 logger.debug(testName + ": status = " + statusCode);
1139 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1140 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1141 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1145 * Delete non existent item.
1147 * @param testName the test name
1149 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
1150 groups = {"delete"}, dependsOnMethods = {"deleteItem"})
1151 public void deleteNonExistentItem(String testName) {
1154 setupDeleteNonExistent(testName);
1156 // Submit the request to the service and store the response.
1157 LocationAuthorityClient client = new LocationAuthorityClient();
1158 ClientResponse<Response> res = client.deleteItem(knownResourceId, NON_EXISTENT_ID);
1159 int statusCode = res.getStatus();
1161 // Check the status code of the response: does it match
1162 // the expected response(s)?
1163 if(logger.isDebugEnabled()){
1164 logger.debug(testName + ": status = " + statusCode);
1166 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1167 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1168 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1171 // ---------------------------------------------------------------
1172 // Utility tests : tests of code used in tests above
1173 // ---------------------------------------------------------------
1175 * Tests the code for manually submitting data that is used by several
1176 * of the methods above.
1178 @Test(dependsOnMethods = {"create", "read"})
1179 public void testSubmitRequest() {
1181 // Expected status code: 200 OK
1182 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1184 // Submit the request to the service and store the response.
1185 String method = ServiceRequestType.READ.httpMethodName();
1186 String url = getResourceURL(knownResourceId);
1187 int statusCode = submitRequest(method, url);
1189 // Check the status code of the response: does it match
1190 // the expected response(s)?
1191 if(logger.isDebugEnabled()){
1192 logger.debug("testSubmitRequest: url=" + url +
1193 " status=" + statusCode);
1195 Assert.assertEquals(statusCode, EXPECTED_STATUS);
1200 * Test item submit request.
1202 @Test(dependsOnMethods = {"createItem", "readItem", "testSubmitRequest"})
1203 public void testItemSubmitRequest() {
1205 // Expected status code: 200 OK
1206 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
1208 // Submit the request to the service and store the response.
1209 String method = ServiceRequestType.READ.httpMethodName();
1210 String url = getItemResourceURL(knownResourceId, knownItemResourceId);
1211 int statusCode = submitRequest(method, url);
1213 // Check the status code of the response: does it match
1214 // the expected response(s)?
1215 if(logger.isDebugEnabled()){
1216 logger.debug("testItemSubmitRequest: url=" + url +
1217 " status=" + statusCode);
1219 Assert.assertEquals(statusCode, EXPECTED_STATUS);
1223 // ---------------------------------------------------------------
1224 // Cleanup of resources created during testing
1225 // ---------------------------------------------------------------
1228 * Deletes all resources created by tests, after all tests have been run.
1230 * This cleanup method will always be run, even if one or more tests fail.
1231 * For this reason, it attempts to remove all resources created
1232 * at any point during testing, even if some of those resources
1233 * may be expected to be deleted by certain tests.
1236 @AfterClass(alwaysRun=true)
1237 public void cleanUp() {
1238 String noTest = System.getProperty("noTestCleanup");
1239 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
1240 if (logger.isDebugEnabled()) {
1241 logger.debug("Skipping Cleanup phase ...");
1245 if (logger.isDebugEnabled()) {
1246 logger.debug("Cleaning up temporary resources created for testing ...");
1248 String parentResourceId;
1249 String itemResourceId;
1250 // Clean up contact resources.
1251 LocationAuthorityClient client = new LocationAuthorityClient();
1252 parentResourceId = knownResourceId;
1253 // Clean up item resources.
1254 for (Map.Entry<String, String> entry : allItemResourceIdsCreated.entrySet()) {
1255 itemResourceId = entry.getKey();
1256 parentResourceId = entry.getValue();
1257 // Note: Any non-success responses from the delete operation
1258 // below are ignored and not reported.
1259 ClientResponse<Response> res =
1260 client.deleteItem(parentResourceId, itemResourceId);
1262 // Clean up parent resources.
1263 for (String resourceId : allResourceIdsCreated) {
1264 // Note: Any non-success responses from the delete operation
1265 // below are ignored and not reported.
1266 ClientResponse<Response> res = client.delete(resourceId);
1270 // ---------------------------------------------------------------
1271 // Utility methods used by tests above
1272 // ---------------------------------------------------------------
1274 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
1277 public String getServicePathComponent() {
1278 return SERVICE_PATH_COMPONENT;
1282 * Gets the item service path component.
1284 * @return the item service path component
1286 public String getItemServicePathComponent() {
1287 return ITEM_SERVICE_PATH_COMPONENT;
1291 * Returns the root URL for the item service.
1293 * This URL consists of a base URL for all services, followed by
1294 * a path component for the owning parent, followed by the
1295 * path component for the items.
1297 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1298 * parent authority resource of the relevant item resource.
1300 * @return The root URL for the item service.
1302 protected String getItemServiceRootURL(String parentResourceIdentifier) {
1303 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
1307 * Returns the URL of a specific item resource managed by a service, and
1308 * designated by an identifier (such as a universally unique ID, or UUID).
1310 * @param parentResourceIdentifier An identifier (such as a UUID) for the
1311 * parent authority resource of the relevant item resource.
1313 * @param itemResourceIdentifier An identifier (such as a UUID) for an
1316 * @return The URL of a specific item resource managed by a service.
1318 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
1319 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;