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;
30 import javax.ws.rs.core.Response;
32 import org.collectionspace.services.PlaceJAXBSchema;
33 import org.collectionspace.services.client.AbstractCommonListUtils;
34 import org.collectionspace.services.client.AuthorityClient;
35 import org.collectionspace.services.client.CollectionSpaceClient;
36 import org.collectionspace.services.client.PayloadOutputPart;
37 import org.collectionspace.services.client.PoxPayloadIn;
38 import org.collectionspace.services.client.PoxPayloadOut;
39 import org.collectionspace.services.client.PlaceAuthorityClient;
40 import org.collectionspace.services.client.PlaceAuthorityClientUtils;
41 import org.collectionspace.services.jaxb.AbstractCommonList;
42 import org.collectionspace.services.place.PlaceTermGroup;
43 import org.collectionspace.services.place.PlaceTermGroupList;
44 import org.collectionspace.services.place.PlaceauthoritiesCommon;
45 import org.collectionspace.services.place.PlacesCommon;
46 import org.jboss.resteasy.client.ClientResponse;
47 import org.slf4j.Logger;
48 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);
67 public String getServicePathComponent() {
68 return PlaceAuthorityClient.SERVICE_PATH_COMPONENT;
72 protected String getServiceName() {
73 return PlaceAuthorityClient.SERVICE_NAME;
76 public String getItemServicePathComponent() {
77 return AuthorityClient.ITEMS;
80 // Instance variables specific to this test.
82 // /** The SERVICE path component. */
83 // final String SERVICE_PATH_COMPONENT = "placeauthorities";
85 // /** The ITEM service path component. */
86 // final String ITEM_SERVICE_PATH_COMPONENT = "items";
89 final String TEST_DNAME = "San Jose, CA";
90 final String TEST_NAME = "San Jose";
91 final String TEST_SHORTID = "sanjose";
92 // TODO Make place type be a controlled vocab term.
93 final String TEST_PLACE_TYPE = "City";
94 // TODO Make status type be a controlled vocab term.
95 final String TEST_STATUS = "Approved";
96 final String TEST_NOTE = "My hometown";
97 final String TEST_SOURCE = "Peralta's Places of California";
98 final String TEST_SOURCE_PAGE = "p.21";
99 final String TEST_DISPLAY_DATE = "This year";
100 final String TEST_EARLIEST_SINGLE_YEAR = "2012";
102 /** The known resource id. */
103 private String knownResourceShortIdentifer = null;
104 private String knownResourceRefName = null;
106 private String knownPlaceTypeRefName = null;
109 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
112 protected CollectionSpaceClient getClientInstance() {
113 return new PlaceAuthorityClient();
117 * Creates the item in authority.
119 * @param vcsid the vcsid
120 * @param authRefName the auth ref name
123 private String createItemInAuthority(String vcsid, String authRefName) {
124 final String testName = "createItemInAuthority("+vcsid+","+authRefName+")";
126 // Submit the request to the service and store the response.
127 PlaceAuthorityClient client = new PlaceAuthorityClient();
128 Map<String, String> sanjoseMap = new HashMap<String,String>();
129 // TODO Make place type and status be controlled vocabs.
130 sanjoseMap.put(PlaceJAXBSchema.SHORT_IDENTIFIER, TEST_SHORTID);
131 sanjoseMap.put(PlaceJAXBSchema.PLACE_TYPE, TEST_PLACE_TYPE);
132 sanjoseMap.put(PlaceJAXBSchema.NOTE, TEST_NOTE);
134 List<PlaceTermGroup> terms = new ArrayList<PlaceTermGroup>();
135 PlaceTermGroup term = new PlaceTermGroup();
136 term.setTermDisplayName(TEST_DNAME);
137 term.setTermName(TEST_NAME);
138 term.setTermSource(TEST_SOURCE);
139 term.setTermSourceDetail(TEST_SOURCE_PAGE);
140 term.setTermStatus(TEST_STATUS);
143 String newID = PlaceAuthorityClientUtils.createItemInAuthority(vcsid,
144 authRefName, sanjoseMap, terms, client );
146 // Store the ID returned from the first item resource created
147 // for additional tests below.
148 if (knownItemResourceId == null){
149 setKnownItemResource(newID, TEST_SHORTID);
150 if (logger.isDebugEnabled()) {
151 logger.debug(testName + ": knownItemResourceId=" + newID);
155 // Store the IDs from any item resources created
156 // by tests, along with the IDs of their parents, so these items
157 // can be deleted after all tests have been run.
158 allResourceItemIdsCreated.put(newID, vcsid);
164 * Verify illegal item display name.
166 * @param testName the test name
167 * @throws Exception the exception
169 @Test(dataProvider="testName")
170 public void verifyIllegalItemDisplayName(String testName) throws Exception {
171 // Perform setup for read.
174 // Submit the request to the service and store the response.
175 PlaceAuthorityClient client = new PlaceAuthorityClient();
176 Response res = client.readItem(knownResourceId, knownItemResourceId);
177 PlacesCommon place= null;
179 assertStatusCode(res, testName);
180 PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
181 place = (PlacesCommon) extractPart(input,
182 client.getItemCommonPartName(), PlacesCommon.class);
183 Assert.assertNotNull(place);
191 // Make an invalid UPDATE request, without a display name
193 PlaceTermGroupList termList = place.getPlaceTermGroupList();
194 Assert.assertNotNull(termList);
195 List<PlaceTermGroup> terms = termList.getPlaceTermGroup();
196 Assert.assertNotNull(terms);
197 Assert.assertTrue(terms.size() > 0);
198 terms.get(0).setTermDisplayName(null);
199 terms.get(0).setTermName(null);
201 setupUpdateWithInvalidBody(); // we expect a failure
203 // Submit the updated resource to the service and store the response.
204 PoxPayloadOut output = new PoxPayloadOut(PlaceAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
205 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), place);
206 setupUpdateWithInvalidBody(); // we expected a failure here.
207 res = client.updateItem(knownResourceId, knownItemResourceId, output);
209 assertStatusCode(res, testName);
220 @Test(dataProvider = "testName", groups = {"readList"},
221 dependsOnMethods = {"readList"})
222 public void readItemList(String testName) {
223 readItemList(knownAuthorityWithItems, null);
227 * Read item list by authority name.
229 @Test(dataProvider = "testName", groups = {"readList"},
230 dependsOnMethods = {"readItemList"})
231 public void readItemListByAuthorityName(String testName) {
232 readItemList(null, READITEMS_SHORT_IDENTIFIER);
243 private void readItemList(String vcsid, String shortId) {
244 String testName = "readItemList";
249 // Submit the request to the service and store the response.
250 PlaceAuthorityClient client = new PlaceAuthorityClient();
253 res = client.readItemList(vcsid, null, null);
254 } else if (shortId != null) {
255 res = client.readItemListForNamedAuthority(shortId, null, null);
257 Assert.fail("readItemList passed null csid and name!");
260 AbstractCommonList list = null;
262 assertStatusCode(res, testName);
263 list = res.readEntity(AbstractCommonList.class);
270 List<AbstractCommonList.ListItem> items = list.getListItem();
271 int nItemsReturned = items.size();
272 // There will be 'nItemsToCreateInList'
273 // items created by the createItemList test,
274 // all associated with the same parent resource.
275 int nExpectedItems = nItemsToCreateInList;
276 if (logger.isDebugEnabled()) {
277 logger.debug(testName + ": Expected " + nExpectedItems
278 + " items; got: " + nItemsReturned);
280 Assert.assertEquals(nItemsReturned, nExpectedItems);
282 for (AbstractCommonList.ListItem item : items) {
283 String value = AbstractCommonListUtils.ListItemGetElementValue(
284 item, PlaceJAXBSchema.REF_NAME);
285 Assert.assertTrue((null != value), "Item refName is null!");
286 value = AbstractCommonListUtils.ListItemGetElementValue(item,
287 PlaceJAXBSchema.TERM_DISPLAY_NAME);
288 Assert.assertTrue((null != value), "Item termDisplayName is null!");
290 if (logger.isTraceEnabled()) {
291 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger,
297 public void delete(String testName) throws Exception {
298 // Do nothing. See localDelete(). This ensure proper test order.
301 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
302 public void localDelete(String testName) throws Exception {
303 super.delete(testName);
307 public void deleteItem(String testName) throws Exception {
308 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
309 // its dependencies will get run first and then we can call the base class' delete method.
312 @Test(dataProvider = "testName", groups = {"delete"},
313 dependsOnMethods = {"verifyIllegalItemDisplayName"})
314 public void localDeleteItem(String testName) throws Exception {
315 super.deleteItem(testName);
318 // ---------------------------------------------------------------
319 // Cleanup of resources created during testing
320 // ---------------------------------------------------------------
323 * Deletes all resources created by tests, after all tests have been run.
325 * This cleanup method will always be run, even if one or more tests fail.
326 * For this reason, it attempts to remove all resources created
327 * at any point during testing, even if some of those resources
328 * may be expected to be deleted by certain tests.
331 @AfterClass(alwaysRun=true)
332 public void cleanUp() {
333 String noTest = System.getProperty("noTestCleanup");
334 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
335 if (logger.isDebugEnabled()) {
336 logger.debug("Skipping Cleanup phase ...");
340 if (logger.isDebugEnabled()) {
341 logger.debug("Cleaning up temporary resources created for testing ...");
343 String parentResourceId;
344 String itemResourceId;
345 // Clean up contact resources.
346 PlaceAuthorityClient client = new PlaceAuthorityClient();
347 parentResourceId = knownResourceId;
348 // Clean up item resources.
349 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
350 itemResourceId = entry.getKey();
351 parentResourceId = entry.getValue();
352 // Note: Any non-success responses from the delete operation
353 // below are ignored and not reported.
354 client.deleteItem(parentResourceId, itemResourceId).close();
356 // Clean up parent resources.
357 for (String resourceId : allResourceIdsCreated) {
358 // Note: Any non-success responses from the delete operation
359 // below are ignored and not reported.
360 client.delete(resourceId).close();
364 // ---------------------------------------------------------------
365 // Utility methods used by tests above
366 // ---------------------------------------------------------------
368 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
372 * Returns the root URL for the item service.
374 * This URL consists of a base URL for all services, followed by
375 * a path component for the owning parent, followed by the
376 * path component for the items.
378 * @param parentResourceIdentifier An identifier (such as a UUID) for the
379 * parent authority resource of the relevant item resource.
381 * @return The root URL for the item service.
383 protected String getItemServiceRootURL(String parentResourceIdentifier) {
384 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
388 * Returns the URL of a specific item resource managed by a service, and
389 * designated by an identifier (such as a universally unique ID, or UUID).
391 * @param parentResourceIdentifier An identifier (such as a UUID) for the
392 * parent authority resource of the relevant item resource.
394 * @param itemResourceIdentifier An identifier (such as a UUID) for an
397 * @return The URL of a specific item resource managed by a service.
399 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
400 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
404 public void authorityTests(String testName) {
405 // TODO Auto-generated method stub
410 // Place specific overrides
414 protected PoxPayloadOut createInstance(String commonPartName,
416 // Submit the request to the service and store the response.
417 String shortId = identifier;
418 String displayName = "displayName-" + shortId;
419 // String baseRefName = PlaceAuthorityClientUtils.createPlaceAuthRefName(shortId, null);
420 PoxPayloadOut result =
421 PlaceAuthorityClientUtils.createPlaceAuthorityInstance(
422 displayName, shortId, commonPartName);
427 protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
428 String displayName = "displayName-NON_EXISTENT_ID";
429 PoxPayloadOut result = PlaceAuthorityClientUtils.createPlaceAuthorityInstance(
430 displayName, "nonEx", commonPartName);
435 protected PlaceauthoritiesCommon updateInstance(PlaceauthoritiesCommon placeauthoritiesCommon) {
436 PlaceauthoritiesCommon result = new PlaceauthoritiesCommon();
438 result.setDisplayName("updated-" + placeauthoritiesCommon.getDisplayName());
439 result.setVocabType("updated-" + placeauthoritiesCommon.getVocabType());
445 protected void compareUpdatedInstances(PlaceauthoritiesCommon original,
446 PlaceauthoritiesCommon updated) throws Exception {
447 Assert.assertEquals(updated.getDisplayName(),
448 original.getDisplayName(),
449 "Display name in updated object did not match submitted data.");
452 protected void compareReadInstances(PlaceauthoritiesCommon original,
453 PlaceauthoritiesCommon fromRead) throws Exception {
454 Assert.assertNotNull(fromRead.getDisplayName());
455 Assert.assertNotNull(fromRead.getShortIdentifier());
456 Assert.assertNotNull(fromRead.getRefName());
460 // Authority item specific overrides
464 protected String createItemInAuthority(String authorityId) {
465 return createItemInAuthority(authorityId, null /*refname*/);
469 protected PlacesCommon updateItemInstance(PlacesCommon placesCommon) {
471 PlaceTermGroupList termList = placesCommon.getPlaceTermGroupList();
472 Assert.assertNotNull(termList);
473 List<PlaceTermGroup> terms = termList.getPlaceTermGroup();
474 Assert.assertNotNull(terms);
475 Assert.assertTrue(terms.size() > 0);
476 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
477 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
478 placesCommon.setPlaceTermGroupList(termList);
484 protected void compareUpdatedItemInstances(PlacesCommon original,
485 PlacesCommon updated) throws Exception {
487 PlaceTermGroupList originalTermList = original.getPlaceTermGroupList();
488 Assert.assertNotNull(originalTermList);
489 List<PlaceTermGroup> originalTerms = originalTermList.getPlaceTermGroup();
490 Assert.assertNotNull(originalTerms);
491 Assert.assertTrue(originalTerms.size() > 0);
493 PlaceTermGroupList updatedTermList = updated.getPlaceTermGroupList();
494 Assert.assertNotNull(updatedTermList);
495 List<PlaceTermGroup> updatedTerms = updatedTermList.getPlaceTermGroup();
496 Assert.assertNotNull(updatedTerms);
497 Assert.assertTrue(updatedTerms.size() > 0);
499 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
500 originalTerms.get(0).getTermDisplayName(),
501 "Value in updated record did not match submitted data.");
505 protected void verifyReadItemInstance(PlacesCommon item)
507 // TODO Auto-generated method stub
512 protected PoxPayloadOut createNonExistenceItemInstance(
513 String commonPartName, String identifier) {
514 Map<String, String> nonexMap = new HashMap<String,String>();
515 nonexMap.put(PlaceJAXBSchema.PLACE_NAME, TEST_NAME);
516 nonexMap.put(PlaceJAXBSchema.SHORT_IDENTIFIER, "nonEx");
517 nonexMap.put(PlaceJAXBSchema.PLACE_TYPE, TEST_PLACE_TYPE);
518 final String EMPTY_REFNAME = "";
519 PoxPayloadOut result =
520 PlaceAuthorityClientUtils.createPlaceInstance(EMPTY_REFNAME,
521 nonexMap, PlaceAuthorityClientUtils.getTermGroupInstance(TEST_NAME), commonPartName);