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.List;
28 import javax.ws.rs.core.Response;
30 import org.collectionspace.services.ConceptJAXBSchema;
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.ConceptAuthorityClient;
35 import org.collectionspace.services.client.ConceptAuthorityClientUtils;
36 import org.collectionspace.services.client.PoxPayloadOut;
37 import org.collectionspace.services.concept.ConceptTermGroup;
38 import org.collectionspace.services.concept.ConceptTermGroupList;
39 import org.collectionspace.services.concept.ConceptauthoritiesCommon;
40 import org.collectionspace.services.concept.ConceptsCommon;
41 import org.collectionspace.services.jaxb.AbstractCommonList;
42 import org.dom4j.DocumentException;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.testng.Assert;
47 import org.testng.annotations.AfterClass;
48 import org.testng.annotations.Test;
51 * ConceptAuthorityServiceTest, carries out tests against a
52 * deployed and running ConceptAuthority Service.
54 * $LastChangedRevision: 753 $
55 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
57 public class ConceptAuthorityServiceTest extends AbstractAuthorityServiceTest<ConceptauthoritiesCommon, ConceptsCommon> {
60 private final Logger logger = LoggerFactory.getLogger(ConceptAuthorityServiceTest.class);
63 * Default constructor. Used to set the short ID for all tests authority items
65 ConceptAuthorityServiceTest() {
66 TEST_SHORTID = "concept1";
70 public String getServicePathComponent() {
71 return ConceptAuthorityClient.SERVICE_PATH_COMPONENT;
75 protected String getServiceName() {
76 return ConceptAuthorityClient.SERVICE_NAME;
79 public String getItemServicePathComponent() {
80 return AuthorityClient.ITEMS;
83 // Instance variables specific to this test.
85 final String TEST_NAME = "Concept 1";
86 final String TEST_SCOPE_NOTE = "Covers quite a bit";
87 // TODO Make status type be a controlled vocab term.
88 final String TEST_STATUS = "Approved";
90 private String knownRecordTypeRefName = null;
93 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
96 protected CollectionSpaceClient getClientInstance() {
97 return new ConceptAuthorityClient();
101 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
102 return new ConceptAuthorityClient(clientPropertiesFilename);
106 * Creates the item in authority.
108 * @param vcsid the vcsid
109 * @param authRefName the auth ref name
113 protected String createItemInAuthority(AuthorityClient client, String authorityId, String shortId) {
115 final String testName = "createItemInAuthority("+authorityId+")";
116 if(logger.isDebugEnabled()){
117 logger.debug(testName);
120 String commonPartXML = createCommonPartXMLForItem(shortId, TEST_NAME);
124 newID = ConceptAuthorityClientUtils.createItemInAuthority(authorityId,
125 commonPartXML, (ConceptAuthorityClient) client);
126 } catch( Exception e ) {
127 logger.error("Problem creating item from XML: "+e.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, 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();
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.readEntity(AbstractCommonList.class);
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, ConceptJAXBSchema.REF_NAME);
213 Assert.assertTrue((null != value), "Item refName is null!");
215 AbstractCommonListUtils.ListItemGetElementValue(item, ConceptJAXBSchema.TERM_DISPLAY_NAME);
216 Assert.assertTrue((null != value), "Item termDisplayName 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).close();
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).close();
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 StringBuilder commonPartXML = new StringBuilder("");
359 commonPartXML.append("<ns2:concepts_common xmlns:ns2=\"http://collectionspace.org/services/concept\">");
360 commonPartXML.append(" <shortIdentifier>"+shortId+"</shortIdentifier>");
361 commonPartXML.append(" <conceptTermGroupList>");
362 commonPartXML.append(" <conceptTermGroup>");
363 commonPartXML.append(" <termDisplayName>"+name+"</termDisplayName>");
364 commonPartXML.append(" <termName>"+name+"</termName>");
365 commonPartXML.append(" <termStatus>"+name+"</termStatus>");
366 commonPartXML.append(" </conceptTermGroup>");
367 commonPartXML.append(" </conceptTermGroupList>");
368 commonPartXML.append("</ns2:concepts_common>");
369 return commonPartXML.toString();
373 protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
374 String displayName = "displayName-NON_EXISTENT_ID";
375 PoxPayloadOut result = ConceptAuthorityClientUtils.createConceptAuthorityInstance(
376 displayName, "nonEx", commonPartName);
381 protected ConceptauthoritiesCommon updateInstance(ConceptauthoritiesCommon conceptauthoritiesCommon) {
382 ConceptauthoritiesCommon result = new ConceptauthoritiesCommon();
384 result.setDisplayName("updated-" + conceptauthoritiesCommon.getDisplayName());
385 result.setVocabType("updated-" + conceptauthoritiesCommon.getVocabType());
391 protected void compareUpdatedInstances(ConceptauthoritiesCommon original,
392 ConceptauthoritiesCommon updated) throws Exception {
393 Assert.assertEquals(updated.getDisplayName(),
394 original.getDisplayName(),
395 "Display name in updated object did not match submitted data.");
399 protected void compareReadInstances(ConceptauthoritiesCommon original,
400 ConceptauthoritiesCommon fromRead) throws Exception {
401 Assert.assertNotNull(fromRead.getDisplayName());
402 Assert.assertNotNull(fromRead.getShortIdentifier());
403 Assert.assertNotNull(fromRead.getRefName());
407 protected ConceptsCommon updateItemInstance(ConceptsCommon conceptsCommon) {
408 ConceptsCommon result = conceptsCommon;
409 ConceptTermGroupList termList = conceptsCommon.getConceptTermGroupList();
410 Assert.assertNotNull(termList);
411 List<ConceptTermGroup> terms = termList.getConceptTermGroup();
412 Assert.assertNotNull(terms);
413 Assert.assertTrue(terms.size() > 0);
414 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
415 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
416 terms.get(0).setTermStatus("updated-" + terms.get(0).getTermStatus());
417 result.setConceptTermGroupList(termList);
422 protected void compareUpdatedItemInstances(ConceptsCommon original,
423 ConceptsCommon updated) throws Exception {
424 ConceptTermGroupList originalTermList = original.getConceptTermGroupList();
425 Assert.assertNotNull(originalTermList);
426 List<ConceptTermGroup> originalTerms = originalTermList.getConceptTermGroup();
427 Assert.assertNotNull(originalTerms);
428 Assert.assertTrue(originalTerms.size() > 0);
430 ConceptTermGroupList updatedTermList = updated.getConceptTermGroupList();
431 Assert.assertNotNull(updatedTermList);
432 List<ConceptTermGroup> updatedTerms = updatedTermList.getConceptTermGroup();
433 Assert.assertNotNull(updatedTerms);
434 Assert.assertTrue(updatedTerms.size() > 0);
436 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
437 originalTerms.get(0).getTermDisplayName(),
438 "Value in updated record did not match submitted data.");
439 Assert.assertEquals(updatedTerms.get(0).getTermStatus(),
440 originalTerms.get(0).getTermDisplayName(),
441 "Value in updated record did not match submitted data.");
445 protected void verifyReadItemInstance(ConceptsCommon item)
447 // TODO Auto-generated method stub
453 protected PoxPayloadOut createNonExistenceItemInstance(
454 String commonPartName, String identifier) {
456 String commonPartXML = createCommonPartXMLForItem("nonExShortId", "nonExItem");
459 PoxPayloadOut result =
460 ConceptAuthorityClientUtils.createConceptInstance(
461 commonPartXML, commonPartName);
463 } catch( DocumentException de ) {
464 logger.error("Problem creating item from XML: "+de.getLocalizedMessage());
465 logger.debug("commonPartXML: "+commonPartXML);