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;
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 Logger logger = LoggerFactory.getLogger(PlaceAuthorityServiceTest.class);
66 * Default constructor. Used to set the short ID for all tests authority items
68 public PlaceAuthorityServiceTest() {
70 TEST_SHORTID = "sanjose";
74 public String getServicePathComponent() {
75 return PlaceAuthorityClient.SERVICE_PATH_COMPONENT;
79 protected String getServiceName() {
80 return PlaceAuthorityClient.SERVICE_NAME;
83 public String getItemServicePathComponent() {
84 return AuthorityClient.ITEMS;
87 // Instance variables specific to this test.
89 final String TEST_DNAME = "San Jose, CA";
90 final String TEST_NAME = "San Jose";
91 // TODO Make place type be a controlled vocab term.
92 final String TEST_PLACE_TYPE = "City";
93 // TODO Make status type be a controlled vocab term.
94 final String TEST_STATUS = "Approved";
95 final String TEST_NOTE = "My hometown";
96 final String TEST_SOURCE = "Peralta's Places of California";
97 final String TEST_SOURCE_PAGE = "p.21";
98 final String TEST_DISPLAY_DATE = "This year";
99 final String TEST_EARLIEST_SINGLE_YEAR = "2012";
102 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
105 protected CollectionSpaceClient getClientInstance() {
106 return new PlaceAuthorityClient();
110 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
111 return new PlaceAuthorityClient(clientPropertiesFilename);
115 protected String createItemInAuthority(AuthorityClient client, String authorityId, String shortId) {
116 return createItemInAuthority(client, authorityId, shortId, null /*refname*/);
121 * Creates the item in authority.
123 * @param vcsid the vcsid
124 * @param authRefName the auth ref name
127 private String createItemInAuthority(AuthorityClient client, String vcsid, String shortId, String authRefName) {
128 final String testName = "createItemInAuthority("+vcsid+","+authRefName+")";
130 // Submit the request to the service and store the response.
131 Map<String, String> sanjoseMap = new HashMap<String,String>();
132 // TODO Make place type and status be controlled vocabs.
133 sanjoseMap.put(PlaceJAXBSchema.SHORT_IDENTIFIER, shortId);
134 sanjoseMap.put(PlaceJAXBSchema.PLACE_TYPE, TEST_PLACE_TYPE);
135 sanjoseMap.put(PlaceJAXBSchema.NOTE, TEST_NOTE);
137 List<PlaceTermGroup> terms = new ArrayList<PlaceTermGroup>();
138 PlaceTermGroup term = new PlaceTermGroup();
139 term.setTermDisplayName(TEST_DNAME);
140 term.setTermName(TEST_NAME);
141 term.setTermSource(TEST_SOURCE);
142 term.setTermSourceDetail(TEST_SOURCE_PAGE);
143 term.setTermStatus(TEST_STATUS);
146 String newID = PlaceAuthorityClientUtils.createItemInAuthority(vcsid,
147 authRefName, sanjoseMap, terms, (PlaceAuthorityClient) client);
149 // Store the ID returned from the first item resource created
150 // for additional tests below.
151 if (knownItemResourceId == null){
152 setKnownItemResource(newID, shortId);
153 if (logger.isDebugEnabled()) {
154 logger.debug(testName + ": knownItemResourceId=" + newID);
158 // Store the IDs from any item resources created
159 // by tests, along with the IDs of their parents, so these items
160 // can be deleted after all tests have been run.
161 allResourceItemIdsCreated.put(newID, vcsid);
167 * Verify illegal item display name.
169 * @param testName the test name
170 * @throws Exception the exception
172 @Test(dataProvider="testName")
173 public void verifyIllegalItemDisplayName(String testName) throws Exception {
174 // Perform setup for read.
177 // Submit the request to the service and store the response.
178 PlaceAuthorityClient client = new PlaceAuthorityClient();
179 Response res = client.readItem(knownResourceId, knownItemResourceId);
180 PlacesCommon place= null;
182 assertStatusCode(res, testName);
183 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
184 place = (PlacesCommon) extractPart(input,
185 client.getItemCommonPartName(), PlacesCommon.class);
186 Assert.assertNotNull(place);
194 // Make an invalid UPDATE request, without a display name
196 PlaceTermGroupList termList = place.getPlaceTermGroupList();
197 Assert.assertNotNull(termList);
198 List<PlaceTermGroup> terms = termList.getPlaceTermGroup();
199 Assert.assertNotNull(terms);
200 Assert.assertTrue(terms.size() > 0);
201 terms.get(0).setTermDisplayName(null);
202 terms.get(0).setTermName(null);
204 setupUpdateWithInvalidBody(); // we expect a failure
206 // Submit the updated resource to the service and store the response.
207 PoxPayloadOut output = new PoxPayloadOut(PlaceAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
208 setupUpdateWithInvalidBody(); // we expected a failure here.
209 res = client.updateItem(knownResourceId, knownItemResourceId, output);
211 assertStatusCode(res, testName);
220 public void delete(String testName) throws Exception {
221 // Do nothing. See localDelete(). This ensure proper test order.
224 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
225 public void localDelete(String testName) throws Exception {
226 super.delete(testName);
230 public void deleteItem(String testName) throws Exception {
231 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
232 // its dependencies will get run first and then we can call the base class' delete method.
235 @Test(dataProvider = "testName", groups = {"delete"},
236 dependsOnMethods = {"verifyIllegalItemDisplayName"})
237 public void localDeleteItem(String testName) throws Exception {
238 super.deleteItem(testName);
241 // ---------------------------------------------------------------
242 // Cleanup of resources created during testing
243 // ---------------------------------------------------------------
246 * Deletes all resources created by tests, after all tests have been run.
248 * This cleanup method will always be run, even if one or more tests fail.
249 * For this reason, it attempts to remove all resources created
250 * at any point during testing, even if some of those resources
251 * may be expected to be deleted by certain tests.
254 @AfterClass(alwaysRun=true)
255 public void cleanUp() {
256 String noTest = System.getProperty("noTestCleanup");
257 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
258 if (logger.isDebugEnabled()) {
259 logger.debug("Skipping Cleanup phase ...");
263 if (logger.isDebugEnabled()) {
264 logger.debug("Cleaning up temporary resources created for testing ...");
266 String parentResourceId;
267 String itemResourceId;
268 // Clean up contact resources.
269 PlaceAuthorityClient client = new PlaceAuthorityClient();
270 parentResourceId = knownResourceId;
271 // Clean up item resources.
272 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
273 itemResourceId = entry.getKey();
274 parentResourceId = entry.getValue();
275 // Note: Any non-success responses from the delete operation
276 // below are ignored and not reported.
277 client.deleteItem(parentResourceId, itemResourceId).close();
279 // Clean up parent resources.
280 for (String resourceId : allResourceIdsCreated) {
281 // Note: Any non-success responses from the delete operation
282 // below are ignored and not reported.
283 client.delete(resourceId).close();
287 // ---------------------------------------------------------------
288 // Utility methods used by tests above
289 // ---------------------------------------------------------------
291 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
295 * Returns the root URL for the item service.
297 * This URL consists of a base URL for all services, followed by
298 * a path component for the owning parent, followed by the
299 * path component for the items.
301 * @param parentResourceIdentifier An identifier (such as a UUID) for the
302 * parent authority resource of the relevant item resource.
304 * @return The root URL for the item service.
306 protected String getItemServiceRootURL(String parentResourceIdentifier) {
307 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
311 * Returns the URL of a specific item resource managed by a service, and
312 * designated by an identifier (such as a universally unique ID, or UUID).
314 * @param parentResourceIdentifier An identifier (such as a UUID) for the
315 * parent authority resource of the relevant item resource.
317 * @param itemResourceIdentifier An identifier (such as a UUID) for an
320 * @return The URL of a specific item resource managed by a service.
322 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
323 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
327 public void authorityTests(String testName) {
328 // TODO Auto-generated method stub
333 // Place specific overrides
337 protected PoxPayloadOut createInstance(String commonPartName,
339 // Submit the request to the service and store the response.
340 String shortId = identifier;
341 String displayName = "displayName-" + shortId;
342 // String baseRefName = PlaceAuthorityClientUtils.createPlaceAuthRefName(shortId, null);
343 PoxPayloadOut result =
344 PlaceAuthorityClientUtils.createPlaceAuthorityInstance(
345 displayName, shortId, commonPartName);
350 protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
351 String displayName = "displayName-NON_EXISTENT_ID";
352 PoxPayloadOut result = PlaceAuthorityClientUtils.createPlaceAuthorityInstance(
353 displayName, "nonEx", commonPartName);
358 protected PlaceauthoritiesCommon updateInstance(PlaceauthoritiesCommon placeauthoritiesCommon) {
359 PlaceauthoritiesCommon result = new PlaceauthoritiesCommon();
361 result.setDisplayName("updated-" + placeauthoritiesCommon.getDisplayName());
362 result.setVocabType("updated-" + placeauthoritiesCommon.getVocabType());
368 protected void compareUpdatedInstances(PlaceauthoritiesCommon original,
369 PlaceauthoritiesCommon updated) throws Exception {
370 Assert.assertEquals(updated.getDisplayName(),
371 original.getDisplayName(),
372 "Display name in updated object did not match submitted data.");
375 protected void compareReadInstances(PlaceauthoritiesCommon original,
376 PlaceauthoritiesCommon fromRead) throws Exception {
377 Assert.assertNotNull(fromRead.getDisplayName());
378 Assert.assertNotNull(fromRead.getShortIdentifier());
379 Assert.assertNotNull(fromRead.getRefName());
383 // Authority item specific overrides
387 protected PlacesCommon updateItemInstance(PlacesCommon placesCommon) {
389 PlaceTermGroupList termList = placesCommon.getPlaceTermGroupList();
390 Assert.assertNotNull(termList);
391 List<PlaceTermGroup> terms = termList.getPlaceTermGroup();
392 Assert.assertNotNull(terms);
393 Assert.assertTrue(terms.size() > 0);
394 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
395 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
396 placesCommon.setPlaceTermGroupList(termList);
402 protected void compareUpdatedItemInstances(PlacesCommon original,
403 PlacesCommon updated,
404 boolean compareRevNumbers) throws Exception {
406 PlaceTermGroupList originalTermList = original.getPlaceTermGroupList();
407 Assert.assertNotNull(originalTermList);
408 List<PlaceTermGroup> originalTerms = originalTermList.getPlaceTermGroup();
409 Assert.assertNotNull(originalTerms);
410 Assert.assertTrue(originalTerms.size() > 0);
412 PlaceTermGroupList updatedTermList = updated.getPlaceTermGroupList();
413 Assert.assertNotNull(updatedTermList);
414 List<PlaceTermGroup> updatedTerms = updatedTermList.getPlaceTermGroup();
415 Assert.assertNotNull(updatedTerms);
416 Assert.assertTrue(updatedTerms.size() > 0);
418 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
419 originalTerms.get(0).getTermDisplayName(),
420 "Value in updated record did not match submitted data.");
422 if (compareRevNumbers == true) {
423 Assert.assertEquals(original.getRev(), updated.getRev(), "Revision numbers should match.");
428 protected void verifyReadItemInstance(PlacesCommon item)
430 // TODO Auto-generated method stub
435 protected PoxPayloadOut createNonExistenceItemInstance(
436 String commonPartName, String identifier) {
437 Map<String, String> nonexMap = new HashMap<String,String>();
438 nonexMap.put(PlaceJAXBSchema.PLACE_NAME, TEST_NAME);
439 nonexMap.put(PlaceJAXBSchema.SHORT_IDENTIFIER, "nonEx");
440 nonexMap.put(PlaceJAXBSchema.PLACE_TYPE, TEST_PLACE_TYPE);
441 final String EMPTY_REFNAME = "";
442 PoxPayloadOut result =
443 PlaceAuthorityClientUtils.createPlaceInstance(EMPTY_REFNAME,
444 nonexMap, PlaceAuthorityClientUtils.getTermGroupInstance(TEST_NAME), commonPartName);