]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ce5dd3616f4cd45cf492fd304f6073b44ce60411
[tmp/jakarta-migration.git] /
1 /**
2  * This document is a part of the source code and related artifacts for
3  * CollectionSpace, an open source collections management system for museums and
4  * related institutions:
5  *
6  * http://www.collectionspace.org http://wiki.collectionspace.org
7  *
8  * Copyright (c)) 2009 Regents of the University of California
9  *
10  * Licensed under the Educational Community License (ECL), Version 2.0. You may
11  * not use this file except in compliance with this License.
12  *
13  * You may obtain a copy of the ECL 2.0 License at
14  * https://source.collectionspace.org/collection-space/LICENSE.txt
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
19  * License for the specific language governing permissions and limitations under
20  * the License.
21  */
22 package org.collectionspace.services.client.test;
23
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.collectionspace.services.TaxonJAXBSchema;
29 import org.collectionspace.services.client.AbstractCommonListUtils;
30 import org.collectionspace.services.client.AuthorityClient;
31 import org.collectionspace.services.client.CollectionSpaceClient;
32 import org.collectionspace.services.client.PayloadOutputPart;
33 import org.collectionspace.services.client.PoxPayloadIn;
34 import org.collectionspace.services.client.PoxPayloadOut;
35 import org.collectionspace.services.client.TaxonomyAuthorityClient;
36 import org.collectionspace.services.client.TaxonomyAuthorityClientUtils;
37 import org.collectionspace.services.client.TaxonomyAuthorityProxy;
38 import org.collectionspace.services.jaxb.AbstractCommonList;
39
40 import org.collectionspace.services.taxonomy.CommonNameGroup;
41 import org.collectionspace.services.taxonomy.CommonNameGroupList;
42 import org.collectionspace.services.taxonomy.TaxonAuthorGroup;
43 import org.collectionspace.services.taxonomy.TaxonAuthorGroupList;
44 import org.collectionspace.services.taxonomy.TaxonCitationList;
45 import org.collectionspace.services.taxonomy.TaxonCommon;
46 import org.collectionspace.services.taxonomy.TaxonTermGroup;
47 import org.collectionspace.services.taxonomy.TaxonTermGroupList;
48 import org.collectionspace.services.taxonomy.TaxonomyauthorityCommon;
49
50 import javax.ws.rs.core.Response;
51
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.testng.Assert;
55 import org.testng.annotations.AfterClass;
56 import org.testng.annotations.Test;
57
58 /**
59  * TaxonomyAuthorityServiceTest, carries out tests against a deployed and
60  * running TaxonomyAuthority Service.
61  *
62  * $LastChangedRevision$ $LastChangedDate$
63  */
64 public class TaxonomyAuthorityServiceTest extends AbstractAuthorityServiceTest<TaxonomyauthorityCommon, TaxonCommon> {
65
66     /**
67      * Default constructor.  Used to set the short ID for all tests authority items
68      */
69         public TaxonomyAuthorityServiceTest() {
70                 super();
71             TEST_SHORTID = "CentauruspleurexanthemusGreen1832";         
72         }
73         
74     /**
75      * The logger.
76      */
77     private final String CLASS_NAME = TaxonomyAuthorityServiceTest.class.getName();
78     private final Logger logger = LoggerFactory.getLogger(TaxonomyAuthorityServiceTest.class);
79     private final String TEST_TERM_STATUS = "accepted";
80     private final String TEST_TAXON_FULL_NAME = "Centaurus pleurexanthemus Green 1832";
81     // TODO Re-implement the Taxon Rank field so as to provide an orderable
82     // ranking value, as well as a display name.
83     private final String TEST_TAXON_RANK = "species";
84     private final String TEST_TAXON_AUTHOR = "J. Green";
85     private final String TEST_TAXON_AUTHOR_TYPE = "ascribed";
86     private final String TEST_TAXON_CITATION = "A Monograph of the Trilobites of North America";
87     private final String TEST_TAXON_CURRENCY = "current";
88     private final String TEST_TAXON_YEAR = "1832";
89     private final String TEST_TAXONOMIC_STATUS = "valid";
90     private final String TEST_TAXON_IS_NAMED_HYBRID = "false";
91     private final List<TaxonTermGroup> NULL_TAXON_TERMS_LIST = null;
92     private final TaxonAuthorGroupList NULL_TAXON_AUTHOR_GROUP_LIST = null;
93     private final TaxonCitationList NULL_TAXON_CITATION_LIST = null;
94     private final CommonNameGroupList NULL_COMMON_NAME_GROUP_LIST = null;
95
96     @Override
97     public String getServicePathComponent() {
98         return TaxonomyAuthorityClient.SERVICE_PATH_COMPONENT;
99     }
100
101     @Override
102     protected String getServiceName() {
103         return TaxonomyAuthorityClient.SERVICE_NAME;
104     }
105
106     public String getItemServicePathComponent() {
107         return AuthorityClient.ITEMS;
108     }
109
110     /*
111      * (non-Javadoc) @see
112      * org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
113      */
114     @Override
115     protected CollectionSpaceClient<AbstractCommonList, PoxPayloadOut, String, TaxonomyAuthorityProxy> getClientInstance() {
116         return new TaxonomyAuthorityClient();
117     }
118
119         @Override
120         protected CollectionSpaceClient<AbstractCommonList, PoxPayloadOut, String, TaxonomyAuthorityProxy> getClientInstance(String clientPropertiesFilename) {
121         return new TaxonomyAuthorityClient(clientPropertiesFilename);
122         }
123         
124     @Override
125     protected String createItemInAuthority(AuthorityClient client, String authorityId, String shortId) {
126         return createItemInAuthority(client, authorityId, shortId, null /** refname */);
127     }
128
129     /**
130      * Creates the item in authority.
131      *
132      * @param vcsid the vcsid
133      * @param authRefName the auth ref name
134      * @return the string
135      */
136     private String createItemInAuthority(AuthorityClient client, String vcsid, String shortId, String authRefName) {
137
138         final String testName = "createItemInAuthority(" + vcsid + "," + authRefName + ")";
139         if (logger.isDebugEnabled()) {
140             logger.debug(getTestBanner(testName, CLASS_NAME));
141         }
142
143         // Submit the request to the service and store the response.
144         Map<String, String> taxonMap = new HashMap<String, String>();
145
146         // Fields present in all authority records.
147         taxonMap.put(TaxonJAXBSchema.SHORT_IDENTIFIER, shortId);
148         // TODO Make term status be controlled vocab.
149         taxonMap.put(TaxonJAXBSchema.TERM_STATUS, TEST_TERM_STATUS);
150
151         // Fields specific to this specific authority record type.
152         taxonMap.put(TaxonJAXBSchema.NAME, TEST_TAXON_FULL_NAME);
153         taxonMap.put(TaxonJAXBSchema.TAXON_RANK, TEST_TAXON_RANK);
154         taxonMap.put(TaxonJAXBSchema.TAXON_CURRENCY, TEST_TAXON_CURRENCY);
155         taxonMap.put(TaxonJAXBSchema.TAXON_YEAR, TEST_TAXON_YEAR);
156         taxonMap.put(TaxonJAXBSchema.TAXONOMIC_STATUS, TEST_TAXONOMIC_STATUS);
157         taxonMap.put(TaxonJAXBSchema.TAXON_IS_NAMED_HYBRID, TEST_TAXON_IS_NAMED_HYBRID);
158
159         TaxonCitationList taxonCitationList = new TaxonCitationList();
160         List<String> taxonCitations = taxonCitationList.getTaxonCitation();
161         taxonCitations.add(TEST_TAXON_CITATION);
162
163         TaxonAuthorGroupList taxonAuthorGroupList = new TaxonAuthorGroupList();
164         List<TaxonAuthorGroup> taxonAuthorGroups = taxonAuthorGroupList.getTaxonAuthorGroup();
165         TaxonAuthorGroup taxonAuthorGroup = new TaxonAuthorGroup();
166         taxonAuthorGroup.setTaxonAuthor(TEST_TAXON_AUTHOR);
167         taxonAuthorGroup.setTaxonAuthorType(TEST_TAXON_AUTHOR_TYPE);
168         taxonAuthorGroups.add(taxonAuthorGroup);
169                 
170         CommonNameGroupList commonNameGroupList = new CommonNameGroupList();
171         List<CommonNameGroup> commonNameGroups = commonNameGroupList.getCommonNameGroup();
172         CommonNameGroup commonNameGroup = new CommonNameGroup();
173         commonNameGroup.setCommonName(TEST_TAXON_FULL_NAME);
174         commonNameGroups.add(commonNameGroup);
175
176         // FIXME: Add additional fields in the Taxon record here,
177         // including at least one each of:
178         // * a Boolean field (when implemented)
179         // * an authref field (when implemented)
180
181         String newID = TaxonomyAuthorityClientUtils.createItemInAuthority(vcsid,
182                 authRefName, taxonMap, NULL_TAXON_TERMS_LIST, taxonAuthorGroupList,
183                 taxonCitationList, commonNameGroupList, (TaxonomyAuthorityClient)client);
184
185         // Store the ID returned from the first item resource created
186         // for additional tests below.
187         if (knownItemResourceId == null) {
188             setKnownItemResource(newID, shortId);
189             if (logger.isDebugEnabled()) {
190                 logger.debug(testName + ": knownItemResourceId=" + newID + " inAuthority=" + vcsid);
191             }
192         }
193
194         // Store the IDs from any item resources created
195         // by tests, along with the IDs of their parents, so these items
196         // can be deleted after all tests have been run.
197         allResourceItemIdsCreated.put(newID, vcsid);
198
199         return newID;
200     }
201
202     /**
203      * Verify illegal item display name.
204      *
205      * @param testName the test name
206      * @throws Exception the exception
207      */
208     @Test(dataProvider = "testName")
209     public void verifyIllegalItemDisplayName(String testName) throws Exception {
210         //
211         // First read in our known resource.
212         //
213         setupRead();
214         TaxonomyAuthorityClient client = new TaxonomyAuthorityClient();
215         Response res = client.readItem(knownResourceId, knownItemResourceId);
216         TaxonCommon taxon = null;
217         try {
218             assertStatusCode(res, testName);
219             // Check whether Taxonomy has expected displayName.
220             PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
221             taxon = (TaxonCommon) extractPart(input,
222                     client.getItemCommonPartName(), TaxonCommon.class);
223             Assert.assertNotNull(taxon);
224         } finally {
225             if (res != null) {
226                 res.close();
227             }
228         }
229         //
230         // Make an invalid UPDATE request, without a display name
231         //
232         TaxonTermGroupList termList = taxon.getTaxonTermGroupList();
233         Assert.assertNotNull(termList);
234         List<TaxonTermGroup> terms = termList.getTaxonTermGroup();
235         Assert.assertNotNull(terms);
236         Assert.assertTrue(terms.size() > 0);
237         terms.get(0).setTermDisplayName(null);
238         terms.get(0).setTermName(null);
239
240         PoxPayloadOut output = new PoxPayloadOut(TaxonomyAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
241         PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), taxon);
242         setupUpdateWithInvalidBody(); // we expect a failure here
243         res = client.updateItem(knownResourceId, knownItemResourceId, output);
244         try {
245             assertStatusCode(res, testName);
246         } finally {
247             res.close();
248         }
249     }
250
251     /**
252      * Read item list.
253      */
254     @Test(dataProvider = "testName", groups = {"readList"},
255     dependsOnMethods = {"readList"})
256     public void readItemList(String testName) {
257         readItemList(knownAuthorityWithItems, null);
258     }
259
260     /**
261      * Read item list by authority name.
262      */
263     @Test(dataProvider = "testName", groups = {"readList"},
264     dependsOnMethods = {"readItemList"})
265     public void readItemListByAuthorityName(String testName) {
266         readItemList(null, READITEMS_SHORT_IDENTIFIER);
267     }
268
269     /**
270      * Read item list.
271      *
272      * @param vcsid the vcsid
273      * @param name the name
274      */
275     private void readItemList(String vcsid, String shortId) {
276         String testName = "readItemList";
277
278         // Perform setup.
279         setupReadList();
280
281         // Submit the request to the service and store the response.
282         TaxonomyAuthorityClient client = new TaxonomyAuthorityClient();
283         Response res = null;
284         if (vcsid != null) {
285             res = client.readItemList(vcsid, null, null);
286         } else if (shortId != null) {
287             res = client.readItemListForNamedAuthority(shortId, null, null);
288         } else {
289             Assert.fail("readItemList passed null csid and name!");
290         }
291         try {
292             assertStatusCode(res, testName);
293             AbstractCommonList list = res.readEntity(AbstractCommonList.class);
294             int statusCode = res.getStatus();
295
296             // Check the status code of the response: does it match
297             // the expected response(s)?
298             if (logger.isDebugEnabled()) {
299                 logger.debug(testName + ": status = " + statusCode);
300             }
301             Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
302                     invalidStatusCodeMessage(testRequestType, statusCode));
303             Assert.assertEquals(statusCode, testExpectedStatusCode);
304
305             List<AbstractCommonList.ListItem> items = list.getListItem();
306             int nItemsReturned = items.size();
307             // There will be 'nItemsToCreateInList'
308             // items created by the createItemList test,
309             // all associated with the same parent resource.
310             int nExpectedItems = nItemsToCreateInList;
311             if (logger.isDebugEnabled()) {
312                 logger.debug(testName + ": Expected "
313                         + nExpectedItems + " items; got: " + nItemsReturned);
314             }
315             Assert.assertEquals(nItemsReturned, nExpectedItems);
316
317             for (AbstractCommonList.ListItem item : items) {
318                 String value =
319                         AbstractCommonListUtils.ListItemGetElementValue(item, TaxonJAXBSchema.REF_NAME);
320                 Assert.assertTrue((null != value), "Item refName is null!");
321                 value =
322                         AbstractCommonListUtils.ListItemGetElementValue(item, TaxonJAXBSchema.TERM_DISPLAY_NAME);
323                 Assert.assertTrue((null != value), "Item termDisplayName is null!");
324             }
325             if (logger.isTraceEnabled()) {
326                 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
327             }
328         } finally {
329             res.close();
330         }
331     }
332
333     @Override
334     public void delete(String testName) throws Exception {
335         // Do nothing.  See localDelete().  This ensure proper test order.
336     }
337
338     @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
339     public void localDelete(String testName) throws Exception {
340         super.delete(testName);
341     }
342
343     @Override
344     public void deleteItem(String testName) throws Exception {
345         // Do nothing.  We need to wait until after the test "localDelete" gets run.  When it does,
346         // its dependencies will get run first and then we can call the base class' delete method.
347     }
348
349     @Test(dataProvider = "testName", groups = {"delete"},
350     dependsOnMethods = {"verifyIllegalItemDisplayName"})
351     public void localDeleteItem(String testName) throws Exception {
352         super.deleteItem(testName);
353     }
354
355     // ---------------------------------------------------------------
356     // Cleanup of resources created during testing
357     // ---------------------------------------------------------------
358     /**
359      * Deletes all resources created by tests, after all tests have been run.
360      *
361      * This cleanup method will always be run, even if one or more tests fail.
362      * For this reason, it attempts to remove all resources created at any point
363      * during testing, even if some of those resources may be expected to be
364      * deleted by certain tests.
365      */
366     @AfterClass(alwaysRun = true)
367     public void cleanUp() {
368         String noTest = System.getProperty("noTestCleanup");
369         if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
370             if (logger.isDebugEnabled()) {
371                 logger.debug("Skipping Cleanup phase ...");
372             }
373             return;
374         }
375         if (logger.isDebugEnabled()) {
376             logger.debug("Cleaning up temporary resources created for testing ...");
377         }
378         String parentResourceId;
379         String itemResourceId;
380         // Clean up contact resources.
381         TaxonomyAuthorityClient client = new TaxonomyAuthorityClient();
382         parentResourceId = knownResourceId;
383         // Clean up item resources.
384         for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
385             itemResourceId = entry.getKey();
386             parentResourceId = entry.getValue();
387             // Note: Any non-success responses from the delete operation
388             // below are ignored and not reported.
389             client.deleteItem(parentResourceId, itemResourceId).close();
390         }
391         
392         // Clean up parent resources.
393         for (String resourceId : allResourceIdsCreated) {
394             // Note: Any non-success responses from the delete operation
395             // below are ignored and not reported.
396             client.delete(resourceId).close();
397         }
398     }
399
400     // ---------------------------------------------------------------
401     // Utility methods used by tests above
402     // ---------------------------------------------------------------
403     /*
404      * (non-Javadoc) @see
405      * org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
406      */
407     /**
408      * Returns the root URL for the item service.
409      *
410      * This URL consists of a base URL for all services, followed by a path
411      * component for the owning parent, followed by the path component for the
412      * items.
413      *
414      * @param parentResourceIdentifier An identifier (such as a UUID) for the
415      * parent authority resource of the relevant item resource.
416      *
417      * @return The root URL for the item service.
418      */
419     protected String getItemServiceRootURL(String parentResourceIdentifier) {
420         return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
421     }
422
423     /**
424      * Returns the URL of a specific item resource managed by a service, and
425      * designated by an identifier (such as a universally unique ID, or UUID).
426      *
427      * @param parentResourceIdentifier An identifier (such as a UUID) for the
428      * parent authority resource of the relevant item resource.
429      *
430      * @param itemResourceIdentifier An identifier (such as a UUID) for an item
431      * resource.
432      *
433      * @return The URL of a specific item resource managed by a service.
434      */
435     protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
436         return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
437     }
438
439     @Override
440     public void authorityTests(String testName) {
441         // TODO Auto-generated method stub
442     }
443
444     //
445     // Taxonomy authority specific instances
446     //
447     @Override
448     protected PoxPayloadOut createInstance(String commonPartName,
449             String identifier) {
450         String shortId = identifier;
451         String displayName = "displayName-" + shortId;
452         PoxPayloadOut multipart = TaxonomyAuthorityClientUtils.createTaxonomyAuthorityInstance(
453                 displayName, shortId, commonPartName);
454         return multipart;
455     }
456
457     @Override
458     protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
459         String displayName = "displayName-NON_EXISTENT_ID";
460         PoxPayloadOut result = TaxonomyAuthorityClientUtils.createTaxonomyAuthorityInstance(
461                 displayName, "nonEx", commonPartName);
462         return result;
463     }
464
465     @Override
466     protected TaxonomyauthorityCommon updateInstance(
467             TaxonomyauthorityCommon taxonomyAuthority) {
468         TaxonomyauthorityCommon result = new TaxonomyauthorityCommon();
469
470         result.setDisplayName("updated-" + taxonomyAuthority.getDisplayName());
471         result.setVocabType("updated-" + taxonomyAuthority.getVocabType());
472
473         return result;
474     }
475
476     @Override
477     protected void compareUpdatedInstances(TaxonomyauthorityCommon original,
478             TaxonomyauthorityCommon updated) throws Exception {
479         // Verify that the updated resource received the correct data.
480         Assert.assertEquals(updated.getDisplayName(),
481                 original.getDisplayName(),
482                 "Display name in updated object did not match submitted data.");
483     }
484
485     //
486     // Authority item specific overrides
487     //
488     @Override
489     protected TaxonCommon updateItemInstance(TaxonCommon taxonCommon) {
490         TaxonCommon result = taxonCommon;
491         TaxonTermGroupList termList = taxonCommon.getTaxonTermGroupList();
492         Assert.assertNotNull(termList);
493         List<TaxonTermGroup> terms = termList.getTaxonTermGroup();
494         Assert.assertNotNull(terms);
495         Assert.assertTrue(terms.size() > 0);
496         terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
497         terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
498         result.setTaxonTermGroupList(termList);
499         return result;
500     }
501
502     @Override
503     protected void compareUpdatedItemInstances(TaxonCommon original,
504             TaxonCommon updated) throws Exception {
505
506         TaxonTermGroupList originalTermList = original.getTaxonTermGroupList();
507         Assert.assertNotNull(originalTermList);
508         List<TaxonTermGroup> originalTerms = originalTermList.getTaxonTermGroup();
509         Assert.assertNotNull(originalTerms);
510         Assert.assertTrue(originalTerms.size() > 0);
511
512         TaxonTermGroupList updatedTermList = updated.getTaxonTermGroupList();
513         Assert.assertNotNull(updatedTermList);
514         List<TaxonTermGroup> updatedTerms = updatedTermList.getTaxonTermGroup();
515         Assert.assertNotNull(updatedTerms);
516         Assert.assertTrue(updatedTerms.size() > 0);
517
518         Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
519                 originalTerms.get(0).getTermDisplayName(),
520                 "Value in updated record did not match submitted data.");
521     }
522
523     @Override
524     protected void verifyReadItemInstance(TaxonCommon item) throws Exception {
525         
526         TaxonTermGroupList termList = item.getTaxonTermGroupList();
527         Assert.assertNotNull(termList);
528         List<TaxonTermGroup> terms = termList.getTaxonTermGroup();
529         Assert.assertNotNull(terms);
530         Assert.assertTrue(terms.size() > 0);
531         
532         String preferredTermName = terms.get(0).getTermName();
533         Assert.assertNotNull(preferredTermName, "Field value is unexpectedly null.");
534         Assert.assertEquals(preferredTermName, TEST_TAXON_FULL_NAME,
535                 "Field value " + preferredTermName
536                 + "does not match expected value " + TEST_TAXON_FULL_NAME);
537     }
538
539     @Override
540     protected PoxPayloadOut createNonExistenceItemInstance(
541             String commonPartName, String identifier) {
542         Map<String, String> nonexMap = new HashMap<String, String>();
543         nonexMap.put(TaxonJAXBSchema.NAME, TEST_TAXON_FULL_NAME);
544         nonexMap.put(TaxonJAXBSchema.SHORT_IDENTIFIER, "nonEx");
545         nonexMap.put(TaxonJAXBSchema.TERM_STATUS, TEST_TERM_STATUS);
546         final String EMPTY_REFNAME = "";
547         PoxPayloadOut result =
548                 TaxonomyAuthorityClientUtils.createTaxonInstance(EMPTY_REFNAME,
549                 nonexMap, NULL_TAXON_TERMS_LIST, NULL_TAXON_AUTHOR_GROUP_LIST, NULL_TAXON_CITATION_LIST,
550                 NULL_COMMON_NAME_GROUP_LIST, commonPartName);
551         return result;
552     }
553 }