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.HashMap;
26 import java.util.List;
29 import org.collectionspace.services.PlaceJAXBSchema;
30 import org.collectionspace.services.client.AbstractCommonListUtils;
31 import org.collectionspace.services.client.AuthorityClient;
32 import org.collectionspace.services.client.CollectionSpaceClient;
33 import org.collectionspace.services.client.PayloadOutputPart;
34 import org.collectionspace.services.client.PoxPayloadIn;
35 import org.collectionspace.services.client.PoxPayloadOut;
36 import org.collectionspace.services.client.PlaceAuthorityClient;
37 import org.collectionspace.services.client.PlaceAuthorityClientUtils;
38 import org.collectionspace.services.jaxb.AbstractCommonList;
39 import org.collectionspace.services.place.PlaceauthoritiesCommon;
40 import org.collectionspace.services.place.PlacesCommon;
41 import org.collectionspace.services.place.PlaceNameGroup;
42 import org.collectionspace.services.place.PlaceNameGroupList;
44 import org.jboss.resteasy.client.ClientResponse;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 import org.testng.Assert;
50 import org.testng.annotations.AfterClass;
51 import org.testng.annotations.Test;
54 * PlaceAuthorityServiceTest, carries out tests against a
55 * deployed and running PlaceAuthority Service.
57 * $LastChangedRevision: 753 $
58 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
60 public class PlaceAuthorityServiceTest extends AbstractAuthorityServiceTest<PlaceauthoritiesCommon, PlacesCommon> {
63 private final String CLASS_NAME = PlaceAuthorityServiceTest.class.getName();
64 private final Logger logger = LoggerFactory.getLogger(PlaceAuthorityServiceTest.class);
65 private final String REFNAME = "refName";
66 private final static String EMPTY_REFNAME = "";
67 private final String DISPLAYNAME = "displayName";
70 public String getServicePathComponent() {
71 return PlaceAuthorityClient.SERVICE_PATH_COMPONENT;
75 protected String getServiceName() {
76 return PlaceAuthorityClient.SERVICE_NAME;
79 public String getItemServicePathComponent() {
80 return AuthorityClient.ITEMS;
83 // Instance variables specific to this test.
85 // /** The SERVICE path component. */
86 // final String SERVICE_PATH_COMPONENT = "placeauthorities";
88 // /** The ITEM service path component. */
89 // final String ITEM_SERVICE_PATH_COMPONENT = "items";
92 final String TEST_DNAME = "San Jose, CA";
93 final String TEST_NAME = "San Jose";
94 final String TEST_SHORTID = "sanjose";
95 // TODO Make place type be a controlled vocab term.
96 final String TEST_PLACE_TYPE = "City";
97 // TODO Make status type be a controlled vocab term.
98 final String TEST_STATUS = "Approved";
99 final String TEST_NOTE = "My hometown";
100 final String TEST_SOURCE = "Peralta's Places of California";
101 final String TEST_SOURCE_PAGE = "p.21";
102 final String TEST_DISPLAY_DATE = "This year";
103 final String TEST_EARLIEST_SINGLE_YEAR = "2012";
105 /** The known resource id. */
106 private String knownResourceShortIdentifer = null;
107 private String knownResourceRefName = null;
109 private String knownPlaceTypeRefName = null;
112 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
115 protected CollectionSpaceClient getClientInstance() {
116 return new PlaceAuthorityClient();
120 * Creates the item in authority.
122 * @param vcsid the vcsid
123 * @param authRefName the auth ref name
126 private String createItemInAuthority(String vcsid, String authRefName) {
127 final String testName = "createItemInAuthority("+vcsid+","+authRefName+")";
129 // Submit the request to the service and store the response.
130 PlaceAuthorityClient client = new PlaceAuthorityClient();
131 Map<String, String> sanjoseMap = new HashMap<String,String>();
132 // TODO Make place type and status be controlled vocabs.
133 sanjoseMap.put(PlaceJAXBSchema.DISPLAY_NAME, TEST_DNAME);
134 sanjoseMap.put(PlaceJAXBSchema.SHORT_IDENTIFIER, TEST_SHORTID);
135 sanjoseMap.put(PlaceJAXBSchema.NAME, TEST_NAME);
136 sanjoseMap.put(PlaceJAXBSchema.NOTE, TEST_NOTE);
137 sanjoseMap.put(PlaceJAXBSchema.SOURCE, TEST_SOURCE);
138 sanjoseMap.put(PlaceJAXBSchema.SOURCE_PAGE, TEST_SOURCE_PAGE);
139 sanjoseMap.put(PlaceJAXBSchema.PLACE_TYPE, TEST_PLACE_TYPE);
140 sanjoseMap.put(PlaceJAXBSchema.TERM_STATUS, TEST_STATUS);
142 String newID = PlaceAuthorityClientUtils.createItemInAuthority(vcsid,
143 authRefName, sanjoseMap, client );
145 // Store the ID returned from the first item resource created
146 // for additional tests below.
147 if (knownItemResourceId == null){
148 setKnownItemResource(newID, TEST_SHORTID);
149 if (logger.isDebugEnabled()) {
150 logger.debug(testName + ": knownItemResourceId=" + newID);
154 // Store the IDs from any item resources created
155 // by tests, along with the IDs of their parents, so these items
156 // can be deleted after all tests have been run.
157 allResourceItemIdsCreated.put(newID, vcsid);
163 * Verify item display name.
165 * @param testName the test name
166 * @throws Exception the exception
168 @Test(dataProvider="testName",
169 dependsOnMethods = {"readItem", "updateItem"})
170 public void verifyItemDisplayName(String testName) throws Exception {
174 // First, read our known item resource
176 PlaceAuthorityClient client = new PlaceAuthorityClient();
177 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
178 PlacesCommon place = null;
180 assertStatusCode(res, testName);
181 // Check whether place has expected displayName.
182 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
183 place = (PlacesCommon) extractPart(input,
184 client.getItemCommonPartName(), PlacesCommon.class);
185 Assert.assertNotNull(place);
188 res.releaseConnection();
192 // Now prepare an updated payload.
194 String displayName = place.getDisplayName();
195 // Make sure displayName matches computed form
196 String expectedDisplayName =
197 PlaceAuthorityClientUtils.prepareDefaultDisplayName(TEST_NAME);
198 Assert.assertNotNull(displayName, expectedDisplayName);
200 // Update the shortName and verify the computed name is updated.
202 place.setDisplayNameComputed(true);
204 // Verify the contents of this resource
205 PlaceNameGroupList placeNameGroupList = place.getPlaceNameGroupList();
206 Assert.assertNotNull(placeNameGroupList);
207 List<PlaceNameGroup> placeNameGroups = placeNameGroupList.getPlaceNameGroup();
208 Assert.assertNotNull(placeNameGroups);
209 Assert.assertTrue(placeNameGroups.size() > 0);
210 String name = placeNameGroups.get(0).getName();
211 Assert.assertNotNull(name);
213 // Update the contents of this resource.
214 final String PLACE_NAME_ADDITION = "verify-item-place-name-updated";
215 placeNameGroups.get(0).setName(PLACE_NAME_ADDITION + TEST_NAME);
216 place.setPlaceNameGroupList(placeNameGroupList);
217 if (logger.isDebugEnabled()) {
218 logger.debug("to be updated Place");
219 logger.debug(objectAsXmlString(place,
220 PlacesCommon.class));
222 expectedDisplayName =
223 PlaceAuthorityClientUtils.prepareDefaultDisplayName(PLACE_NAME_ADDITION + TEST_NAME);
225 // Submit the updated resource to the service and store the response.
226 PoxPayloadOut output = new PoxPayloadOut(PlaceAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
227 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), place);
230 res = client.updateItem(knownResourceId, knownItemResourceId, output);
231 PlacesCommon updatedPlace = null;
233 assertStatusCode(res, testName);
234 // Retrieve the updated resource and verify that its contents exist.
235 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
236 updatedPlace = (PlacesCommon) extractPart(input,
237 client.getItemCommonPartName(), PlacesCommon.class);
238 Assert.assertNotNull(updatedPlace);
241 res.releaseConnection();
245 // Verify that the updated resource computes the right displayName.
246 Assert.assertEquals(updatedPlace.getDisplayName(), expectedDisplayName,
247 "Updated DisplayName in Place not reflected in computed DisplayName.");
249 // Now Update the displayName, not computed and verify the computed name is overriden.
251 place.setDisplayNameComputed(false);
252 expectedDisplayName = "TestName";
253 place.setDisplayName(expectedDisplayName);
255 // Submit the updated resource to the service and store the response.
256 output = new PoxPayloadOut(PlaceAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
257 commonPart = output.addPart(client.getItemCommonPartName(), place);
259 res = client.updateItem(knownResourceId, knownItemResourceId, output);
261 assertStatusCode(res, testName);
262 // Retrieve the updated resource and verify that its contents exist.
263 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
264 updatedPlace = (PlacesCommon) extractPart(input,
265 client.getItemCommonPartName(), PlacesCommon.class);
266 Assert.assertNotNull(updatedPlace);
269 res.releaseConnection();
273 // Verify that the updated resource received the correct data.
274 Assert.assertEquals(updatedPlace.isDisplayNameComputed(), false,
275 "Updated displayNameComputed in Place did not match submitted data.");
276 // Verify that the updated resource computes the right displayName.
277 Assert.assertEquals(updatedPlace.getDisplayName(),
279 "Updated DisplayName (not computed) in Place not stored.");
283 * Verify illegal item display name.
285 * @param testName the test name
286 * @throws Exception the exception
288 @Test(dataProvider="testName",
289 dependsOnMethods = {"verifyItemDisplayName"})
290 public void verifyIllegalItemDisplayName(String testName) throws Exception {
291 // Perform setup for read.
294 // Submit the request to the service and store the response.
295 PlaceAuthorityClient client = new PlaceAuthorityClient();
296 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
297 PlacesCommon place= null;
299 assertStatusCode(res, testName);
300 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
301 place = (PlacesCommon) extractPart(input,
302 client.getItemCommonPartName(), PlacesCommon.class);
303 Assert.assertNotNull(place);
306 res.releaseConnection();
310 // Try to Update with computed false and no displayName
311 place.setDisplayNameComputed(false);
312 place.setDisplayName(null);
314 // Submit the updated resource to the service and store the response.
315 PoxPayloadOut output = new PoxPayloadOut(PlaceAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
316 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), place);
317 setupUpdateWithInvalidBody(); // we expected a failure here.
318 res = client.updateItem(knownResourceId, knownItemResourceId, output);
320 assertStatusCode(res, testName);
323 res.releaseConnection();
331 @Test(dataProvider = "testName", groups = {"readList"},
332 dependsOnMethods = {"readList"})
333 public void readItemList(String testName) {
334 readItemList(knownAuthorityWithItems, null);
338 * Read item list by authority name.
340 @Test(dataProvider = "testName", groups = {"readList"},
341 dependsOnMethods = {"readItemList"})
342 public void readItemListByAuthorityName(String testName) {
343 readItemList(null, READITEMS_SHORT_IDENTIFIER);
354 private void readItemList(String vcsid, String shortId) {
355 String testName = "readItemList";
360 // Submit the request to the service and store the response.
361 PlaceAuthorityClient client = new PlaceAuthorityClient();
362 ClientResponse<AbstractCommonList> res = null;
364 res = client.readItemList(vcsid, null, null);
365 } else if (shortId != null) {
366 res = client.readItemListForNamedAuthority(shortId, null, null);
368 Assert.fail("readItemList passed null csid and name!");
371 AbstractCommonList list = null;
373 assertStatusCode(res, testName);
374 list = res.getEntity();
377 res.releaseConnection();
381 List<AbstractCommonList.ListItem> items = list.getListItem();
382 int nItemsReturned = items.size();
383 // There will be 'nItemsToCreateInList'
384 // items created by the createItemList test,
385 // all associated with the same parent resource.
386 int nExpectedItems = nItemsToCreateInList;
387 if (logger.isDebugEnabled()) {
388 logger.debug(testName + ": Expected " + nExpectedItems
389 + " items; got: " + nItemsReturned);
391 Assert.assertEquals(nItemsReturned, nExpectedItems);
393 for (AbstractCommonList.ListItem item : items) {
394 String value = AbstractCommonListUtils.ListItemGetElementValue(
396 Assert.assertTrue((null != value), "Item refName is null!");
397 value = AbstractCommonListUtils.ListItemGetElementValue(item,
399 Assert.assertTrue((null != value), "Item displayName is null!");
401 if (logger.isTraceEnabled()) {
402 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger,
408 public void delete(String testName) throws Exception {
409 // Do nothing. See localDelete(). This ensure proper test order.
412 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
413 public void localDelete(String testName) throws Exception {
414 super.delete(testName);
418 public void deleteItem(String testName) throws Exception {
419 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
420 // its dependencies will get run first and then we can call the base class' delete method.
423 @Test(dataProvider = "testName", groups = {"delete"},
424 dependsOnMethods = {"verifyIllegalItemDisplayName"})
425 public void localDeleteItem(String testName) throws Exception {
426 super.deleteItem(testName);
429 // ---------------------------------------------------------------
430 // Cleanup of resources created during testing
431 // ---------------------------------------------------------------
434 * Deletes all resources created by tests, after all tests have been run.
436 * This cleanup method will always be run, even if one or more tests fail.
437 * For this reason, it attempts to remove all resources created
438 * at any point during testing, even if some of those resources
439 * may be expected to be deleted by certain tests.
442 @AfterClass(alwaysRun=true)
443 public void cleanUp() {
444 String noTest = System.getProperty("noTestCleanup");
445 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
446 if (logger.isDebugEnabled()) {
447 logger.debug("Skipping Cleanup phase ...");
451 if (logger.isDebugEnabled()) {
452 logger.debug("Cleaning up temporary resources created for testing ...");
454 String parentResourceId;
455 String itemResourceId;
456 // Clean up contact resources.
457 PlaceAuthorityClient client = new PlaceAuthorityClient();
458 parentResourceId = knownResourceId;
459 // Clean up item resources.
460 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
461 itemResourceId = entry.getKey();
462 parentResourceId = entry.getValue();
463 // Note: Any non-success responses from the delete operation
464 // below are ignored and not reported.
465 client.deleteItem(parentResourceId, itemResourceId).releaseConnection();
467 // Clean up parent resources.
468 for (String resourceId : allResourceIdsCreated) {
469 // Note: Any non-success responses from the delete operation
470 // below are ignored and not reported.
471 client.delete(resourceId).releaseConnection();
475 // ---------------------------------------------------------------
476 // Utility methods used by tests above
477 // ---------------------------------------------------------------
479 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
483 * Returns the root URL for the item service.
485 * This URL consists of a base URL for all services, followed by
486 * a path component for the owning parent, followed by the
487 * path component for the items.
489 * @param parentResourceIdentifier An identifier (such as a UUID) for the
490 * parent authority resource of the relevant item resource.
492 * @return The root URL for the item service.
494 protected String getItemServiceRootURL(String parentResourceIdentifier) {
495 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
499 * Returns the URL of a specific item resource managed by a service, and
500 * designated by an identifier (such as a universally unique ID, or UUID).
502 * @param parentResourceIdentifier An identifier (such as a UUID) for the
503 * parent authority resource of the relevant item resource.
505 * @param itemResourceIdentifier An identifier (such as a UUID) for an
508 * @return The URL of a specific item resource managed by a service.
510 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
511 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
515 public void authorityTests(String testName) {
516 // TODO Auto-generated method stub
521 // Place specific overrides
525 protected PoxPayloadOut createInstance(String commonPartName,
527 // Submit the request to the service and store the response.
528 String shortId = identifier;
529 String displayName = "displayName-" + shortId;
530 // String baseRefName = PlaceAuthorityClientUtils.createPlaceAuthRefName(shortId, null);
531 PoxPayloadOut result =
532 PlaceAuthorityClientUtils.createPlaceAuthorityInstance(
533 displayName, shortId, commonPartName);
538 protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
539 String displayName = "displayName-NON_EXISTENT_ID";
540 PoxPayloadOut result = PlaceAuthorityClientUtils.createPlaceAuthorityInstance(
541 displayName, "nonEx", commonPartName);
546 protected PlaceauthoritiesCommon updateInstance(PlaceauthoritiesCommon placeauthoritiesCommon) {
547 PlaceauthoritiesCommon result = new PlaceauthoritiesCommon();
549 result.setDisplayName("updated-" + placeauthoritiesCommon.getDisplayName());
550 result.setVocabType("updated-" + placeauthoritiesCommon.getVocabType());
556 protected void compareUpdatedInstances(PlaceauthoritiesCommon original,
557 PlaceauthoritiesCommon updated) throws Exception {
558 Assert.assertEquals(updated.getDisplayName(),
559 original.getDisplayName(),
560 "Display name in updated object did not match submitted data.");
563 protected void compareReadInstances(PlaceauthoritiesCommon original,
564 PlaceauthoritiesCommon fromRead) throws Exception {
565 Assert.assertNotNull(fromRead.getDisplayName());
566 Assert.assertNotNull(fromRead.getShortIdentifier());
567 Assert.assertNotNull(fromRead.getRefName());
571 // Authority item specific overrides
575 protected String createItemInAuthority(String authorityId) {
576 return createItemInAuthority(authorityId, null /*refname*/);
580 protected PlacesCommon updateItemInstance(PlacesCommon placesCommon) {
582 // Get the Name field from the resource passed in
583 PlaceNameGroupList placeNameGroupList = placesCommon.getPlaceNameGroupList();
584 Assert.assertNotNull(placeNameGroupList);
585 List<PlaceNameGroup> placeNameGroups = placeNameGroupList.getPlaceNameGroup();
586 Assert.assertNotNull(placeNameGroups);
587 Assert.assertTrue(placeNameGroups.size() > 0);
588 String originalName = placeNameGroups.get(0).getName();
589 Assert.assertNotNull(originalName);
591 // Update the contents of the new resource based on original value
592 PlaceNameGroupList updatedPlaceNameGroupList = new PlaceNameGroupList();
593 List<PlaceNameGroup> updatedPlaceNameGroups = updatedPlaceNameGroupList.getPlaceNameGroup();
594 PlaceNameGroup updatedPlaceNameGroup = new PlaceNameGroup();
595 updatedPlaceNameGroup.setName("updated-" + originalName);
596 updatedPlaceNameGroups.add(updatedPlaceNameGroup);
597 PlacesCommon result = new PlacesCommon();
598 result.setPlaceNameGroupList(updatedPlaceNameGroupList);
600 result.setDisplayName("updated-" + placesCommon.getDisplayName());
606 protected void compareUpdatedItemInstances(PlacesCommon original,
607 PlacesCommon updated) throws Exception {
609 // Get the Name fields each resource passed in
610 PlaceNameGroupList placeNameGroupList = original.getPlaceNameGroupList();
611 Assert.assertNotNull(placeNameGroupList);
612 List<PlaceNameGroup> placeNameGroups = placeNameGroupList.getPlaceNameGroup();
613 Assert.assertNotNull(placeNameGroups);
614 Assert.assertTrue(placeNameGroups.size() > 0);
615 String originalName = placeNameGroups.get(0).getName();
616 Assert.assertNotNull(originalName);
618 PlaceNameGroupList updatedPlaceNameGroupList = updated.getPlaceNameGroupList();
619 Assert.assertNotNull(updatedPlaceNameGroupList);
620 List<PlaceNameGroup> updatedPlaceNameGroups = updatedPlaceNameGroupList.getPlaceNameGroup();
621 Assert.assertNotNull(updatedPlaceNameGroups);
622 Assert.assertTrue(updatedPlaceNameGroups.size() > 0);
623 String updatedName = updatedPlaceNameGroups.get(0).getName();
624 Assert.assertNotNull(updatedName);
629 Assert.assertEquals(updatedName, originalName,
630 "Data in updated Place did not match submitted data.");
634 protected void verifyReadItemInstance(PlacesCommon item)
636 // TODO Auto-generated method stub
641 protected PoxPayloadOut createNonExistenceItemInstance(
642 String commonPartName, String identifier) {
643 Map<String, String> nonexMap = new HashMap<String,String>();
644 nonexMap.put(PlaceJAXBSchema.NAME, TEST_NAME);
645 nonexMap.put(PlaceJAXBSchema.SHORT_IDENTIFIER, "nonEx");
646 nonexMap.put(PlaceJAXBSchema.PLACE_TYPE, TEST_PLACE_TYPE);
647 nonexMap.put(PlaceJAXBSchema.TERM_STATUS, TEST_STATUS);
648 // PoxPayloadOut multipart =
649 // PlaceAuthorityClientUtils.createPlaceInstance(
650 // PlaceAuthorityClientUtils.createPlaceRefName(knownResourceRefName, "nonEx", "Non Existent"),
651 // nonexMap, client.getItemCommonPartName() );
652 final String EMPTY_REFNAME = "";
653 PoxPayloadOut result =
654 PlaceAuthorityClientUtils.createPlaceInstance(EMPTY_REFNAME,
655 nonexMap, commonPartName);