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.ConceptJAXBSchema;
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.common.datetime.GregorianCalendarDateTimeUtils;
37 import org.collectionspace.services.client.ConceptAuthorityClient;
38 import org.collectionspace.services.client.ConceptAuthorityClientUtils;
39 import org.collectionspace.services.jaxb.AbstractCommonList;
40 import org.collectionspace.services.concept.ConceptauthoritiesCommon;
41 import org.collectionspace.services.concept.ConceptsCommon;
42 import org.dom4j.DocumentException;
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 * ConceptAuthorityServiceTest, carries out tests against a
55 * deployed and running ConceptAuthority Service.
57 * $LastChangedRevision: 753 $
58 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
60 public class ConceptAuthorityServiceTest extends AbstractAuthorityServiceTest<ConceptauthoritiesCommon, ConceptsCommon> {
63 private final String CLASS_NAME = ConceptAuthorityServiceTest.class.getName();
64 private final Logger logger = LoggerFactory.getLogger(ConceptAuthorityServiceTest.class);
65 private final String REFNAME = "refName";
66 private final String DISPLAYNAME = "displayName";
67 private final static String CURRENT_DATE_UTC =
68 GregorianCalendarDateTimeUtils.currentDateUTC();
71 public String getServicePathComponent() {
72 return ConceptAuthorityClient.SERVICE_PATH_COMPONENT;
76 protected String getServiceName() {
77 return ConceptAuthorityClient.SERVICE_NAME;
80 public String getItemServicePathComponent() {
81 return AuthorityClient.ITEMS;
84 // Instance variables specific to this test.
86 final String TEST_NAME = "Concept 1";
87 final String TEST_SHORTID = "concept1";
88 final String TEST_SCOPE_NOTE = "Covers quite a bit";
89 // TODO Make status type be a controlled vocab term.
90 final String TEST_STATUS = "Approved";
92 private String knownConceptTypeRefName = null;
95 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
98 protected CollectionSpaceClient getClientInstance() {
99 return new ConceptAuthorityClient();
103 * Creates the item in authority.
105 * @param vcsid the vcsid
106 * @param authRefName the auth ref name
110 protected String createItemInAuthority(String authorityId) {
112 final String testName = "createItemInAuthority("+authorityId+")";
113 if(logger.isDebugEnabled()){
114 logger.debug(testName);
117 // Submit the request to the service and store the response.
118 ConceptAuthorityClient client = new ConceptAuthorityClient();
120 String commonPartXML = createCommonPartXMLForItem(TEST_SHORTID, TEST_NAME);
124 newID = ConceptAuthorityClientUtils.createItemInAuthority(authorityId,
125 commonPartXML, client );
126 } catch( DocumentException de ) {
127 logger.error("Problem creating item from XML: "+de.getLocalizedMessage());
128 logger.debug("commonPartXML: "+commonPartXML);
132 // Store the ID returned from the first item resource created
133 // for additional tests below.
134 if (knownItemResourceId == null){
135 setKnownItemResource(newID, TEST_SHORTID);
136 if (logger.isDebugEnabled()) {
137 logger.debug(testName + ": knownItemResourceId=" + newID);
141 // Store the IDs from any item resources created
142 // by tests, along with the IDs of their parents, so these items
143 // can be deleted after all tests have been run.
144 allResourceItemIdsCreated.put(newID, authorityId);
152 @Test(dataProvider = "testName", groups = {"readList"},
153 dependsOnMethods = {"readList"})
154 public void readItemList(String testName) {
155 readItemList(knownAuthorityWithItems, null);
159 * Read item list by authority name.
161 @Test(dataProvider = "testName", groups = {"readList"},
162 dependsOnMethods = {"readItemList"})
163 public void readItemListByAuthorityName(String testName) {
164 readItemList(null, READITEMS_SHORT_IDENTIFIER);
170 * @param vcsid the vcsid
171 * @param name the name
173 private void readItemList(String vcsid, String shortId) {
175 String testName = "readItemList";
180 // Submit the request to the service and store the response.
181 ConceptAuthorityClient client = new ConceptAuthorityClient();
182 ClientResponse<AbstractCommonList> res = null;
184 res = client.readItemList(vcsid, null, null);
185 } else if(shortId!= null) {
186 res = client.readItemListForNamedAuthority(shortId, null, null);
188 Assert.fail("readItemList passed null csid and name!");
190 AbstractCommonList list = null;
192 assertStatusCode(res, testName);
193 list = res.getEntity();
195 res.releaseConnection();
197 List<AbstractCommonList.ListItem> items =
199 int nItemsReturned = items.size();
200 // There will be 'nItemsToCreateInList'
201 // items created by the createItemList test,
202 // all associated with the same parent resource.
203 int nExpectedItems = nItemsToCreateInList;
204 if(logger.isDebugEnabled()){
205 logger.debug(testName + ": Expected "
206 + nExpectedItems +" items; got: "+nItemsReturned);
208 Assert.assertEquals(nItemsReturned, nExpectedItems);
210 for (AbstractCommonList.ListItem item : items) {
212 AbstractCommonListUtils.ListItemGetElementValue(item, REFNAME);
213 Assert.assertTrue((null != value), "Item refName is null!");
215 AbstractCommonListUtils.ListItemGetElementValue(item, DISPLAYNAME);
216 Assert.assertTrue((null != value), "Item displayName is null!");
218 if(logger.isTraceEnabled()){
219 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
224 public void delete(String testName) throws Exception {
225 // Do nothing. See localDelete(). This ensure proper test order.
228 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
229 public void localDelete(String testName) throws Exception {
230 super.delete(testName);
234 public void deleteItem(String testName) throws Exception {
235 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
236 // its dependencies will get run first and then we can call the base class' delete method.
239 @Test(dataProvider = "testName", groups = {"delete"},
240 dependsOnMethods = {"readItem", "updateItem"})
241 public void localDeleteItem(String testName) throws Exception {
242 super.deleteItem(testName);
247 // ---------------------------------------------------------------
248 // Cleanup of resources created during testing
249 // ---------------------------------------------------------------
252 * Deletes all resources created by tests, after all tests have been run.
254 * This cleanup method will always be run, even if one or more tests fail.
255 * For this reason, it attempts to remove all resources created
256 * at any point during testing, even if some of those resources
257 * may be expected to be deleted by certain tests.
260 @AfterClass(alwaysRun=true)
261 public void cleanUp() {
262 String noTest = System.getProperty("noTestCleanup");
263 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
264 if (logger.isDebugEnabled()) {
265 logger.debug("Skipping Cleanup phase ...");
269 if (logger.isDebugEnabled()) {
270 logger.debug("Cleaning up temporary resources created for testing ...");
272 String parentResourceId;
273 String itemResourceId;
274 // Clean up contact resources.
275 ConceptAuthorityClient client = new ConceptAuthorityClient();
276 parentResourceId = knownResourceId;
277 // Clean up item resources.
278 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
279 itemResourceId = entry.getKey();
280 parentResourceId = entry.getValue();
281 // Note: Any non-success responses from the delete operation
282 // below are ignored and not reported.
283 client.deleteItem(parentResourceId, itemResourceId).releaseConnection();
285 // Clean up parent resources.
286 for (String resourceId : allResourceIdsCreated) {
287 // Note: Any non-success responses from the delete operation
288 // below are ignored and not reported.
289 client.delete(resourceId).releaseConnection();
293 // ---------------------------------------------------------------
294 // Utility methods used by tests above
295 // ---------------------------------------------------------------
297 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
301 * Returns the root URL for the item service.
303 * This URL consists of a base URL for all services, followed by
304 * a path component for the owning parent, followed by the
305 * path component for the items.
307 * @param parentResourceIdentifier An identifier (such as a UUID) for the
308 * parent authority resource of the relevant item resource.
310 * @return The root URL for the item service.
312 protected String getItemServiceRootURL(String parentResourceIdentifier) {
313 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
317 * Returns the URL of a specific item resource managed by a service, and
318 * designated by an identifier (such as a universally unique ID, or UUID).
320 * @param parentResourceIdentifier An identifier (such as a UUID) for the
321 * parent authority resource of the relevant item resource.
323 * @param itemResourceIdentifier An identifier (such as a UUID) for an
326 * @return The URL of a specific item resource managed by a service.
328 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
329 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
333 public void authorityTests(String testName) {
334 // TODO Auto-generated method stub
339 // Concept specific overrides
343 protected PoxPayloadOut createInstance(String commonPartName,
345 ConceptAuthorityClient client = new ConceptAuthorityClient();
346 String shortId = identifier;
347 String displayName = "displayName-" + shortId;
348 // String baseRefName = ConceptAuthorityClientUtils.createConceptAuthRefName(shortId, null);
349 PoxPayloadOut multipart =
350 ConceptAuthorityClientUtils.createConceptAuthorityInstance(
351 displayName, shortId, commonPartName);
356 private String createCommonPartXMLForItem(String shortId, String name ) {
358 String commonPartXML =
359 "<ns2:concepts_common xmlns:ns2=\"http://collectionspace.org/services/concept\">" +
360 " <shortIdentifier>"+shortId+"</shortIdentifier>"+
361 " <displayName>"+name+"</displayName>"+
362 " <displayNameComputed>false</displayNameComputed>"+
363 " <termStatus>Imagined</termStatus>"+
365 " <conceptTermGroupList>"+
366 " <conceptTermGroup>"+
367 " <term>Another term</term>"+
368 " <termType>alternate</termType>"+
369 " <source>My Imagination</source>"+
370 " </conceptTermGroup>"+
371 " </conceptTermGroupList>"+
373 "</ns2:concepts_common>";
374 return commonPartXML;
378 protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
379 String displayName = "displayName-NON_EXISTENT_ID";
380 PoxPayloadOut result = ConceptAuthorityClientUtils.createConceptAuthorityInstance(
381 displayName, "nonEx", commonPartName);
386 protected ConceptauthoritiesCommon updateInstance(ConceptauthoritiesCommon conceptauthoritiesCommon) {
387 ConceptauthoritiesCommon result = new ConceptauthoritiesCommon();
389 result.setDisplayName("updated-" + conceptauthoritiesCommon.getDisplayName());
390 result.setVocabType("updated-" + conceptauthoritiesCommon.getVocabType());
396 protected void compareUpdatedInstances(ConceptauthoritiesCommon original,
397 ConceptauthoritiesCommon updated) throws Exception {
398 Assert.assertEquals(updated.getDisplayName(),
399 original.getDisplayName(),
400 "Display name in updated object did not match submitted data.");
403 protected void compareReadInstances(ConceptauthoritiesCommon original,
404 ConceptauthoritiesCommon fromRead) throws Exception {
405 Assert.assertNotNull(fromRead.getDisplayName());
406 Assert.assertNotNull(fromRead.getShortIdentifier());
407 Assert.assertNotNull(fromRead.getRefName());
411 protected ConceptsCommon updateItemInstance(ConceptsCommon conceptsCommon) {
412 ConceptsCommon result = new ConceptsCommon();
414 result.setTermStatus("updated-" + conceptsCommon.getTermStatus());
415 result.setDisplayName("updated-" + conceptsCommon.getDisplayName());
421 protected void compareUpdatedItemInstances(ConceptsCommon original,
422 ConceptsCommon updated) throws Exception {
423 Assert.assertEquals(updated.getTermStatus(), original.getTermStatus(),
424 "Data in updated Concept did not match submitted data.");
425 Assert.assertEquals(updated.getDisplayName(), original.getDisplayName(),
426 "Data in updated Concept did not match submitted data.");
430 protected void verifyReadItemInstance(ConceptsCommon item)
432 // TODO Auto-generated method stub
438 protected PoxPayloadOut createNonExistenceItemInstance(
439 String commonPartName, String identifier) {
441 String commonPartXML = createCommonPartXMLForItem("nonExShortId", "nonExItem");
444 PoxPayloadOut result =
445 ConceptAuthorityClientUtils.createConceptInstance(
446 commonPartXML, commonPartName);
448 } catch( DocumentException de ) {
449 logger.error("Problem creating item from XML: "+de.getLocalizedMessage());
450 logger.debug("commonPartXML: "+commonPartXML);