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 org.collectionspace.services.LocationJAXBSchema;
31 import org.collectionspace.services.client.AbstractCommonListUtils;
32 import org.collectionspace.services.client.AuthorityClient;
33 import org.collectionspace.services.client.CollectionSpaceClient;
34 import org.collectionspace.services.client.PayloadOutputPart;
35 import org.collectionspace.services.client.PoxPayloadIn;
36 import org.collectionspace.services.client.PoxPayloadOut;
37 import org.collectionspace.services.client.LocationAuthorityClient;
38 import org.collectionspace.services.client.LocationAuthorityClientUtils;
39 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
40 import org.collectionspace.services.jaxb.AbstractCommonList;
41 import org.collectionspace.services.location.LocTermGroup;
42 import org.collectionspace.services.location.LocTermGroupList;
43 import org.collectionspace.services.location.LocationauthoritiesCommon;
44 import org.collectionspace.services.location.LocationsCommon;
46 import org.jboss.resteasy.client.ClientResponse;
48 import org.slf4j.Logger;
49 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 AbstractAuthorityServiceTest<LocationauthoritiesCommon, LocationsCommon> {
65 private final String CLASS_NAME = LocationAuthorityServiceTest.class.getName();
66 private final Logger logger = LoggerFactory.getLogger(LocationAuthorityServiceTest.class);
67 private final static String CURRENT_DATE_UTC =
68 GregorianCalendarDateTimeUtils.currentDateUTC();
71 public String getServicePathComponent() {
72 return LocationAuthorityClient.SERVICE_PATH_COMPONENT;
76 protected String getServiceName() {
77 return LocationAuthorityClient.SERVICE_NAME;
80 public String getItemServicePathComponent() {
81 return AuthorityClient.ITEMS;
84 // Instance variables specific to this test.
86 final String TEST_NAME = "Shelf 1";
87 final String TEST_SHORTID = "shelf1";
88 final String TEST_CONDITION_NOTE = "Basically clean";
89 final String TEST_CONDITION_NOTE_DATE = CURRENT_DATE_UTC;
90 final String TEST_SECURITY_NOTE = "Kind of safe";
91 final String TEST_ACCESS_NOTE = "Only right-thinkers may see";
92 final String TEST_ADDRESS = "123 Main Street, Anytown USA";
93 // TODO Make loc type be a controlled vocab term.
94 final String TEST_LOCATION_TYPE = "Shelf";
95 // TODO Make status type be a controlled vocab term.
96 final String TEST_STATUS = "Approved";
98 /** The known resource id. */
99 private String knownLocationTypeRefName = null;
102 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
105 protected CollectionSpaceClient getClientInstance() {
106 return new LocationAuthorityClient();
110 * Creates the item in authority.
112 * @param vcsid the vcsid
113 * @param authRefName the auth ref name
116 private String createItemInAuthority(String vcsid, String authRefName) {
117 final String testName = "createItemInAuthority("+vcsid+","+authRefName+")";
119 // Submit the request to the service and store the response.
120 LocationAuthorityClient client = new LocationAuthorityClient();
121 Map<String, String> shelf1Map = new HashMap<String,String>();
122 // TODO Make loc type and status be controlled vocabs.
123 shelf1Map.put(LocationJAXBSchema.SHORT_IDENTIFIER, TEST_SHORTID);
124 shelf1Map.put(LocationJAXBSchema.CONDITION_NOTE, TEST_CONDITION_NOTE);
125 shelf1Map.put(LocationJAXBSchema.CONDITION_NOTE_DATE, TEST_CONDITION_NOTE_DATE);
126 shelf1Map.put(LocationJAXBSchema.SECURITY_NOTE, TEST_SECURITY_NOTE);
127 shelf1Map.put(LocationJAXBSchema.ACCESS_NOTE, TEST_ACCESS_NOTE);
128 shelf1Map.put(LocationJAXBSchema.ADDRESS, TEST_ADDRESS);
129 shelf1Map.put(LocationJAXBSchema.LOCATION_TYPE, TEST_LOCATION_TYPE);
131 List<LocTermGroup> shelf1Terms = new ArrayList<LocTermGroup>();
132 LocTermGroup term = new LocTermGroup();
133 term.setTermDisplayName(TEST_NAME);
134 term.setTermName(TEST_NAME);
135 term.setTermStatus(TEST_STATUS);
136 shelf1Terms.add(term);
137 shelf1Map.put(LocationJAXBSchema.TERM_STATUS, TEST_STATUS);
139 String newID = LocationAuthorityClientUtils.createItemInAuthority(vcsid,
140 authRefName, shelf1Map, shelf1Terms, client );
142 // Store the ID returned from the first item resource created
143 // for additional tests below.
144 if (knownItemResourceId == null){
145 setKnownItemResource(newID, TEST_SHORTID);
146 if (logger.isDebugEnabled()) {
147 logger.debug(testName + ": knownItemResourceId=" + newID);
151 // Store the IDs from any item resources created
152 // by tests, along with the IDs of their parents, so these items
153 // can be deleted after all tests have been run.
154 allResourceItemIdsCreated.put(newID, vcsid);
160 * Verify illegal item display name.
162 * @param testName the test name
163 * @throws Exception the exception
165 @Test(dataProvider="testName")
166 public void verifyIllegalItemDisplayName(String testName) throws Exception {
167 // Perform setup for read.
170 // Submit the request to the service and store the response.
171 LocationAuthorityClient client = new LocationAuthorityClient();
172 ClientResponse<String> res = client.readItem(knownResourceId, knownItemResourceId);
173 LocationsCommon location = null;
175 assertStatusCode(res, testName);
176 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
177 location = (LocationsCommon) extractPart(input,
178 client.getItemCommonPartName(), LocationsCommon.class);
179 Assert.assertNotNull(location);
182 res.releaseConnection();
187 // Make an invalid UPDATE request, without a display name
189 LocTermGroupList termList = location.getLocTermGroupList();
190 Assert.assertNotNull(termList);
191 List<LocTermGroup> terms = termList.getLocTermGroup();
192 Assert.assertNotNull(terms);
193 Assert.assertTrue(terms.size() > 0);
194 terms.get(0).setTermDisplayName(null);
195 terms.get(0).setTermName(null);
197 setupUpdateWithInvalidBody(); // we expect a failure
199 // Submit the updated resource to the service and store the response.
200 PoxPayloadOut output = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
201 PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), location);
202 setupUpdateWithInvalidBody(); // we expected a failure here.
203 res = client.updateItem(knownResourceId, knownItemResourceId, output);
205 assertStatusCode(res, testName);
208 res.releaseConnection();
216 @Test(dataProvider = "testName", groups = {"readList"},
217 dependsOnMethods = {"readList"})
218 public void readItemList(String testName) {
219 readItemList(knownAuthorityWithItems, null);
223 * Read item list by authority name.
225 @Test(dataProvider = "testName", groups = {"readList"},
226 dependsOnMethods = {"readItemList"})
227 public void readItemListByAuthorityName(String testName) {
228 readItemList(null, READITEMS_SHORT_IDENTIFIER);
239 private void readItemList(String vcsid, String shortId) {
240 String testName = "readItemList";
245 // Submit the request to the service and store the response.
246 LocationAuthorityClient client = new LocationAuthorityClient();
247 ClientResponse<AbstractCommonList> res = null;
249 res = client.readItemList(vcsid, null, null);
250 } else if (shortId != null) {
251 res = client.readItemListForNamedAuthority(shortId, null, null);
253 Assert.fail("readItemList passed null csid and name!");
256 AbstractCommonList list = null;
258 assertStatusCode(res, testName);
259 list = res.getEntity();
262 res.releaseConnection();
266 List<AbstractCommonList.ListItem> items = list.getListItem();
267 int nItemsReturned = items.size();
268 // There will be 'nItemsToCreateInList'
269 // items created by the createItemList test,
270 // all associated with the same parent resource.
271 int nExpectedItems = nItemsToCreateInList;
272 if (logger.isDebugEnabled()) {
273 logger.debug(testName + ": Expected " + nExpectedItems
274 + " items; got: " + nItemsReturned);
276 Assert.assertEquals(nItemsReturned, nExpectedItems);
278 for (AbstractCommonList.ListItem item : items) {
279 String value = AbstractCommonListUtils.ListItemGetElementValue(
280 item, LocationJAXBSchema.REF_NAME);
281 Assert.assertTrue((null != value), "Item refName is null!");
282 value = AbstractCommonListUtils.ListItemGetElementValue(item,
283 LocationJAXBSchema.TERM_DISPLAY_NAME);
284 Assert.assertTrue((null != value), "Item termDisplayName is null!");
286 if (logger.isTraceEnabled()) {
287 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger,
293 public void delete(String testName) throws Exception {
294 // Do nothing. See localDelete(). This ensure proper test order.
297 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
298 public void localDelete(String testName) throws Exception {
299 super.delete(testName);
303 public void deleteItem(String testName) throws Exception {
304 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
305 // its dependencies will get run first and then we can call the base class' delete method.
308 @Test(dataProvider = "testName", groups = {"delete"},
309 dependsOnMethods = {"verifyIllegalItemDisplayName"})
310 public void localDeleteItem(String testName) throws Exception {
311 super.deleteItem(testName);
314 // ---------------------------------------------------------------
315 // Cleanup of resources created during testing
316 // ---------------------------------------------------------------
319 * Deletes all resources created by tests, after all tests have been run.
321 * This cleanup method will always be run, even if one or more tests fail.
322 * For this reason, it attempts to remove all resources created
323 * at any point during testing, even if some of those resources
324 * may be expected to be deleted by certain tests.
327 @AfterClass(alwaysRun=true)
328 public void cleanUp() {
329 String noTest = System.getProperty("noTestCleanup");
330 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
331 if (logger.isDebugEnabled()) {
332 logger.debug("Skipping Cleanup phase ...");
336 if (logger.isDebugEnabled()) {
337 logger.debug("Cleaning up temporary resources created for testing ...");
339 String parentResourceId;
340 String itemResourceId;
341 // Clean up contact resources.
342 LocationAuthorityClient client = new LocationAuthorityClient();
343 parentResourceId = knownResourceId;
344 // Clean up item resources.
345 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
346 itemResourceId = entry.getKey();
347 parentResourceId = entry.getValue();
348 // Note: Any non-success responses from the delete operation
349 // below are ignored and not reported.
350 client.deleteItem(parentResourceId, itemResourceId).close();
352 // Clean up parent resources.
353 for (String resourceId : allResourceIdsCreated) {
354 // Note: Any non-success responses from the delete operation
355 // below are ignored and not reported.
356 client.delete(resourceId).close();
360 // ---------------------------------------------------------------
361 // Utility methods used by tests above
362 // ---------------------------------------------------------------
364 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
368 * Returns the root URL for the item service.
370 * This URL consists of a base URL for all services, followed by
371 * a path component for the owning parent, followed by the
372 * path component for the items.
374 * @param parentResourceIdentifier An identifier (such as a UUID) for the
375 * parent authority resource of the relevant item resource.
377 * @return The root URL for the item service.
379 protected String getItemServiceRootURL(String parentResourceIdentifier) {
380 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
384 * Returns the URL of a specific item resource managed by a service, and
385 * designated by an identifier (such as a universally unique ID, or UUID).
387 * @param parentResourceIdentifier An identifier (such as a UUID) for the
388 * parent authority resource of the relevant item resource.
390 * @param itemResourceIdentifier An identifier (such as a UUID) for an
393 * @return The URL of a specific item resource managed by a service.
395 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
396 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
400 public void authorityTests(String testName) {
401 // TODO Auto-generated method stub
406 // Location specific overrides
410 protected PoxPayloadOut createInstance(String commonPartName,
412 // Submit the request to the service and store the response.
413 String shortId = identifier;
414 String displayName = "displayName-" + shortId;
415 // String baseRefName = LocationAuthorityClientUtils.createLocationAuthRefName(shortId, null);
416 PoxPayloadOut result =
417 LocationAuthorityClientUtils.createLocationAuthorityInstance(
418 displayName, shortId, commonPartName);
423 protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
424 String displayName = "displayName-NON_EXISTENT_ID";
425 PoxPayloadOut result = LocationAuthorityClientUtils.createLocationAuthorityInstance(
426 displayName, "nonEx", commonPartName);
431 protected LocationauthoritiesCommon updateInstance(LocationauthoritiesCommon locationauthoritiesCommon) {
432 LocationauthoritiesCommon result = new LocationauthoritiesCommon();
434 result.setDisplayName("updated-" + locationauthoritiesCommon.getDisplayName());
435 result.setVocabType("updated-" + locationauthoritiesCommon.getVocabType());
441 protected void compareUpdatedInstances(LocationauthoritiesCommon original,
442 LocationauthoritiesCommon updated) throws Exception {
443 Assert.assertEquals(updated.getDisplayName(),
444 original.getDisplayName(),
445 "Display name in updated object did not match submitted data.");
448 protected void compareReadInstances(LocationauthoritiesCommon original,
449 LocationauthoritiesCommon fromRead) throws Exception {
450 Assert.assertNotNull(fromRead.getDisplayName());
451 Assert.assertNotNull(fromRead.getShortIdentifier());
452 Assert.assertNotNull(fromRead.getRefName());
456 // Authority item specific overrides
460 protected String createItemInAuthority(String authorityId) {
461 return createItemInAuthority(authorityId, null /*refname*/);
465 protected LocationsCommon updateItemInstance(LocationsCommon locationsCommon) {
467 LocTermGroupList termList = locationsCommon.getLocTermGroupList();
468 Assert.assertNotNull(termList);
469 List<LocTermGroup> terms = termList.getLocTermGroup();
470 Assert.assertNotNull(terms);
471 Assert.assertTrue(terms.size() > 0);
472 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
473 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
474 locationsCommon.setLocTermGroupList(termList);
476 return locationsCommon;
481 protected void compareUpdatedItemInstances(LocationsCommon original,
482 LocationsCommon updated) throws Exception {
483 LocTermGroupList originalTermList = original.getLocTermGroupList();
484 Assert.assertNotNull(originalTermList);
485 List<LocTermGroup> originalTerms = originalTermList.getLocTermGroup();
486 Assert.assertNotNull(originalTerms);
487 Assert.assertTrue(originalTerms.size() > 0);
489 LocTermGroupList updatedTermList = updated.getLocTermGroupList();
490 Assert.assertNotNull(updatedTermList);
491 List<LocTermGroup> updatedTerms = updatedTermList.getLocTermGroup();
492 Assert.assertNotNull(updatedTerms);
493 Assert.assertTrue(updatedTerms.size() > 0);
495 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
496 originalTerms.get(0).getTermDisplayName(),
497 "Value in updated record did not match submitted data.");
501 protected void verifyReadItemInstance(LocationsCommon item)
503 // TODO Auto-generated method stub
508 protected PoxPayloadOut createNonExistenceItemInstance(
509 String commonPartName, String identifier) {
510 Map<String, String> nonexMap = new HashMap<String, String>();
511 nonexMap.put(LocationJAXBSchema.SHORT_IDENTIFIER, "nonExistent");
512 final String EMPTY_REFNAME = "";
513 PoxPayloadOut result =
514 LocationAuthorityClientUtils.createLocationInstance(EMPTY_REFNAME, nonexMap,
515 LocationAuthorityClientUtils.getTermGroupInstance(""), commonPartName);