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 public ConceptAuthorityServiceTest() {
67 TEST_SHORTID = "concept1";
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_SCOPE_NOTE = "Covers quite a bit";
88 // TODO Make status type be a controlled vocab term.
89 final String TEST_STATUS = "Approved";
91 private String knownRecordTypeRefName = null;
94 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
97 protected CollectionSpaceClient getClientInstance() {
98 return new ConceptAuthorityClient();
102 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
103 return new ConceptAuthorityClient(clientPropertiesFilename);
107 * Creates the item in authority.
109 * @param vcsid the vcsid
110 * @param authRefName the auth ref name
114 protected String createItemInAuthority(AuthorityClient client, String authorityId, String shortId) {
116 final String testName = "createItemInAuthority("+authorityId+")";
117 if(logger.isDebugEnabled()){
118 logger.debug(testName);
121 String commonPartXML = createCommonPartXMLForItem(shortId, TEST_NAME);
125 newID = ConceptAuthorityClientUtils.createItemInAuthority(authorityId,
126 commonPartXML, (ConceptAuthorityClient) client);
127 } catch( Exception e ) {
128 logger.error("Problem creating item from XML: "+e.getLocalizedMessage());
129 logger.debug("commonPartXML: "+commonPartXML);
133 // Store the ID returned from the first item resource created
134 // for additional tests below.
135 if (knownItemResourceId == null){
136 setKnownItemResource(newID, shortId);
137 if (logger.isDebugEnabled()) {
138 logger.debug(testName + ": knownItemResourceId=" + newID);
142 // Store the IDs from any item resources created
143 // by tests, along with the IDs of their parents, so these items
144 // can be deleted after all tests have been run.
145 allResourceItemIdsCreated.put(newID, authorityId);
153 @Test(dataProvider = "testName", groups = {"readList"},
154 dependsOnMethods = {"readList"})
155 public void readItemList(String testName) {
156 readItemList(knownAuthorityWithItems, null);
160 * Read item list by authority name.
162 @Test(dataProvider = "testName", groups = {"readList"},
163 dependsOnMethods = {"readItemList"})
164 public void readItemListByAuthorityName(String testName) {
165 readItemList(null, READITEMS_SHORT_IDENTIFIER);
171 * @param vcsid the vcsid
172 * @param name the name
174 private void readItemList(String vcsid, String shortId) {
176 String testName = "readItemList";
181 // Submit the request to the service and store the response.
182 ConceptAuthorityClient client = new ConceptAuthorityClient();
185 res = client.readItemList(vcsid, null, null);
186 } else if(shortId!= null) {
187 res = client.readItemListForNamedAuthority(shortId, null, null);
189 Assert.fail("readItemList passed null csid and name!");
191 AbstractCommonList list = null;
193 assertStatusCode(res, testName);
194 list = res.readEntity(AbstractCommonList.class);
198 List<AbstractCommonList.ListItem> items =
200 int nItemsReturned = items.size();
201 // There will be 'nItemsToCreateInList'
202 // items created by the createItemList test,
203 // all associated with the same parent resource.
204 int nExpectedItems = nItemsToCreateInList;
205 if(logger.isDebugEnabled()){
206 logger.debug(testName + ": Expected "
207 + nExpectedItems +" items; got: "+nItemsReturned);
209 Assert.assertEquals(nItemsReturned, nExpectedItems);
211 for (AbstractCommonList.ListItem item : items) {
213 AbstractCommonListUtils.ListItemGetElementValue(item, ConceptJAXBSchema.REF_NAME);
214 Assert.assertTrue((null != value), "Item refName is null!");
216 AbstractCommonListUtils.ListItemGetElementValue(item, ConceptJAXBSchema.TERM_DISPLAY_NAME);
217 Assert.assertTrue((null != value), "Item termDisplayName is null!");
219 if(logger.isTraceEnabled()){
220 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
225 public void delete(String testName) throws Exception {
226 // Do nothing. See localDelete(). This ensure proper test order.
229 @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
230 public void localDelete(String testName) throws Exception {
231 super.delete(testName);
235 public void deleteItem(String testName) throws Exception {
236 // Do nothing. We need to wait until after the test "localDelete" gets run. When it does,
237 // its dependencies will get run first and then we can call the base class' delete method.
240 @Test(dataProvider = "testName", groups = {"delete"},
241 dependsOnMethods = {"readItem", "updateItem"})
242 public void localDeleteItem(String testName) throws Exception {
243 super.deleteItem(testName);
248 // ---------------------------------------------------------------
249 // Cleanup of resources created during testing
250 // ---------------------------------------------------------------
253 * Deletes all resources created by tests, after all tests have been run.
255 * This cleanup method will always be run, even if one or more tests fail.
256 * For this reason, it attempts to remove all resources created
257 * at any point during testing, even if some of those resources
258 * may be expected to be deleted by certain tests.
261 @AfterClass(alwaysRun=true)
262 public void cleanUp() {
263 String noTest = System.getProperty("noTestCleanup");
264 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
265 if (logger.isDebugEnabled()) {
266 logger.debug("Skipping Cleanup phase ...");
270 if (logger.isDebugEnabled()) {
271 logger.debug("Cleaning up temporary resources created for testing ...");
273 String parentResourceId;
274 String itemResourceId;
275 // Clean up contact resources.
276 ConceptAuthorityClient client = new ConceptAuthorityClient();
277 parentResourceId = knownResourceId;
278 // Clean up item resources.
279 for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
280 itemResourceId = entry.getKey();
281 parentResourceId = entry.getValue();
282 // Note: Any non-success responses from the delete operation
283 // below are ignored and not reported.
284 client.deleteItem(parentResourceId, itemResourceId).close();
286 // Clean up parent resources.
287 for (String resourceId : allResourceIdsCreated) {
288 // Note: Any non-success responses from the delete operation
289 // below are ignored and not reported.
290 client.delete(resourceId).close();
294 // ---------------------------------------------------------------
295 // Utility methods used by tests above
296 // ---------------------------------------------------------------
298 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
302 * Returns the root URL for the item service.
304 * This URL consists of a base URL for all services, followed by
305 * a path component for the owning parent, followed by the
306 * path component for the items.
308 * @param parentResourceIdentifier An identifier (such as a UUID) for the
309 * parent authority resource of the relevant item resource.
311 * @return The root URL for the item service.
313 protected String getItemServiceRootURL(String parentResourceIdentifier) {
314 return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
318 * Returns the URL of a specific item resource managed by a service, and
319 * designated by an identifier (such as a universally unique ID, or UUID).
321 * @param parentResourceIdentifier An identifier (such as a UUID) for the
322 * parent authority resource of the relevant item resource.
324 * @param itemResourceIdentifier An identifier (such as a UUID) for an
327 * @return The URL of a specific item resource managed by a service.
329 protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
330 return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
334 public void authorityTests(String testName) {
335 // TODO Auto-generated method stub
340 // Concept specific overrides
344 protected PoxPayloadOut createInstance(String commonPartName,
346 ConceptAuthorityClient client = new ConceptAuthorityClient();
347 String shortId = identifier;
348 String displayName = "displayName-" + shortId;
349 // String baseRefName = ConceptAuthorityClientUtils.createConceptAuthRefName(shortId, null);
350 PoxPayloadOut multipart =
351 ConceptAuthorityClientUtils.createConceptAuthorityInstance(
352 displayName, shortId, commonPartName);
357 private String createCommonPartXMLForItem(String shortId, String name ) {
359 StringBuilder commonPartXML = new StringBuilder("");
360 commonPartXML.append("<ns2:concepts_common xmlns:ns2=\"http://collectionspace.org/services/concept\">");
361 commonPartXML.append(" <shortIdentifier>"+shortId+"</shortIdentifier>");
362 commonPartXML.append(" <conceptTermGroupList>");
363 commonPartXML.append(" <conceptTermGroup>");
364 commonPartXML.append(" <termDisplayName>"+name+"</termDisplayName>");
365 commonPartXML.append(" <termName>"+name+"</termName>");
366 commonPartXML.append(" <termStatus>"+name+"</termStatus>");
367 commonPartXML.append(" </conceptTermGroup>");
368 commonPartXML.append(" </conceptTermGroupList>");
369 commonPartXML.append("</ns2:concepts_common>");
370 return commonPartXML.toString();
374 protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
375 String displayName = "displayName-NON_EXISTENT_ID";
376 PoxPayloadOut result = ConceptAuthorityClientUtils.createConceptAuthorityInstance(
377 displayName, "nonEx", commonPartName);
382 protected ConceptauthoritiesCommon updateInstance(ConceptauthoritiesCommon conceptauthoritiesCommon) {
383 ConceptauthoritiesCommon result = new ConceptauthoritiesCommon();
385 result.setDisplayName("updated-" + conceptauthoritiesCommon.getDisplayName());
386 result.setVocabType("updated-" + conceptauthoritiesCommon.getVocabType());
392 protected void compareUpdatedInstances(ConceptauthoritiesCommon original,
393 ConceptauthoritiesCommon updated) throws Exception {
394 Assert.assertEquals(updated.getDisplayName(),
395 original.getDisplayName(),
396 "Display name in updated object did not match submitted data.");
400 protected void compareReadInstances(ConceptauthoritiesCommon original,
401 ConceptauthoritiesCommon fromRead) throws Exception {
402 Assert.assertNotNull(fromRead.getDisplayName());
403 Assert.assertNotNull(fromRead.getShortIdentifier());
404 Assert.assertNotNull(fromRead.getRefName());
408 protected ConceptsCommon updateItemInstance(ConceptsCommon conceptsCommon) {
409 ConceptsCommon result = conceptsCommon;
410 ConceptTermGroupList termList = conceptsCommon.getConceptTermGroupList();
411 Assert.assertNotNull(termList);
412 List<ConceptTermGroup> terms = termList.getConceptTermGroup();
413 Assert.assertNotNull(terms);
414 Assert.assertTrue(terms.size() > 0);
415 terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
416 terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
417 terms.get(0).setTermStatus("updated-" + terms.get(0).getTermStatus());
418 result.setConceptTermGroupList(termList);
423 protected void compareUpdatedItemInstances(ConceptsCommon original,
424 ConceptsCommon updated) throws Exception {
425 ConceptTermGroupList originalTermList = original.getConceptTermGroupList();
426 Assert.assertNotNull(originalTermList);
427 List<ConceptTermGroup> originalTerms = originalTermList.getConceptTermGroup();
428 Assert.assertNotNull(originalTerms);
429 Assert.assertTrue(originalTerms.size() > 0);
431 ConceptTermGroupList updatedTermList = updated.getConceptTermGroupList();
432 Assert.assertNotNull(updatedTermList);
433 List<ConceptTermGroup> updatedTerms = updatedTermList.getConceptTermGroup();
434 Assert.assertNotNull(updatedTerms);
435 Assert.assertTrue(updatedTerms.size() > 0);
437 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
438 originalTerms.get(0).getTermDisplayName(),
439 "Value in updated record did not match submitted data.");
440 Assert.assertEquals(updatedTerms.get(0).getTermStatus(),
441 originalTerms.get(0).getTermDisplayName(),
442 "Value in updated record did not match submitted data.");
446 protected void verifyReadItemInstance(ConceptsCommon item)
448 // TODO Auto-generated method stub
454 protected PoxPayloadOut createNonExistenceItemInstance(
455 String commonPartName, String identifier) {
457 String commonPartXML = createCommonPartXMLForItem("nonExShortId", "nonExItem");
460 PoxPayloadOut result =
461 ConceptAuthorityClientUtils.createConceptInstance(
462 commonPartXML, commonPartName);
464 } catch( DocumentException de ) {
465 logger.error("Problem creating item from XML: "+de.getLocalizedMessage());
466 logger.debug("commonPartXML: "+commonPartXML);