]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e9ec75758192c5896d07c034f8adbb5def0c382e
[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     @Override
252     public void delete(String testName) throws Exception {
253         // Do nothing.  See localDelete().  This ensure proper test order.
254     }
255
256     @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
257     public void localDelete(String testName) throws Exception {
258         super.delete(testName);
259     }
260
261     @Override
262     public void deleteItem(String testName) throws Exception {
263         // Do nothing.  We need to wait until after the test "localDelete" gets run.  When it does,
264         // its dependencies will get run first and then we can call the base class' delete method.
265     }
266
267     @Test(dataProvider = "testName", groups = {"delete"},
268     dependsOnMethods = {"verifyIllegalItemDisplayName"})
269     public void localDeleteItem(String testName) throws Exception {
270         super.deleteItem(testName);
271     }
272
273     // ---------------------------------------------------------------
274     // Cleanup of resources created during testing
275     // ---------------------------------------------------------------
276     /**
277      * Deletes all resources created by tests, after all tests have been run.
278      *
279      * This cleanup method will always be run, even if one or more tests fail.
280      * For this reason, it attempts to remove all resources created at any point
281      * during testing, even if some of those resources may be expected to be
282      * deleted by certain tests.
283      */
284     @AfterClass(alwaysRun = true)
285     public void cleanUp() {
286         String noTest = System.getProperty("noTestCleanup");
287         if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
288             if (logger.isDebugEnabled()) {
289                 logger.debug("Skipping Cleanup phase ...");
290             }
291             return;
292         }
293         if (logger.isDebugEnabled()) {
294             logger.debug("Cleaning up temporary resources created for testing ...");
295         }
296         String parentResourceId;
297         String itemResourceId;
298         // Clean up contact resources.
299         TaxonomyAuthorityClient client = new TaxonomyAuthorityClient();
300         parentResourceId = knownResourceId;
301         // Clean up item resources.
302         for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
303             itemResourceId = entry.getKey();
304             parentResourceId = entry.getValue();
305             // Note: Any non-success responses from the delete operation
306             // below are ignored and not reported.
307             client.deleteItem(parentResourceId, itemResourceId).close();
308         }
309         
310         // Clean up parent resources.
311         for (String resourceId : allResourceIdsCreated) {
312             // Note: Any non-success responses from the delete operation
313             // below are ignored and not reported.
314             client.delete(resourceId).close();
315         }
316     }
317
318     // ---------------------------------------------------------------
319     // Utility methods used by tests above
320     // ---------------------------------------------------------------
321     /*
322      * (non-Javadoc) @see
323      * org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
324      */
325     /**
326      * Returns the root URL for the item service.
327      *
328      * This URL consists of a base URL for all services, followed by a path
329      * component for the owning parent, followed by the path component for the
330      * items.
331      *
332      * @param parentResourceIdentifier An identifier (such as a UUID) for the
333      * parent authority resource of the relevant item resource.
334      *
335      * @return The root URL for the item service.
336      */
337     protected String getItemServiceRootURL(String parentResourceIdentifier) {
338         return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
339     }
340
341     /**
342      * Returns the URL of a specific item resource managed by a service, and
343      * designated by an identifier (such as a universally unique ID, or UUID).
344      *
345      * @param parentResourceIdentifier An identifier (such as a UUID) for the
346      * parent authority resource of the relevant item resource.
347      *
348      * @param itemResourceIdentifier An identifier (such as a UUID) for an item
349      * resource.
350      *
351      * @return The URL of a specific item resource managed by a service.
352      */
353     protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
354         return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
355     }
356
357     @Override
358     public void authorityTests(String testName) {
359         // TODO Auto-generated method stub
360     }
361
362     //
363     // Taxonomy authority specific instances
364     //
365     @Override
366     protected PoxPayloadOut createInstance(String commonPartName,
367             String identifier) {
368         String shortId = identifier;
369         String displayName = "displayName-" + shortId;
370         PoxPayloadOut multipart = TaxonomyAuthorityClientUtils.createTaxonomyAuthorityInstance(
371                 displayName, shortId, commonPartName);
372         return multipart;
373     }
374
375     @Override
376     protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
377         String displayName = "displayName-NON_EXISTENT_ID";
378         PoxPayloadOut result = TaxonomyAuthorityClientUtils.createTaxonomyAuthorityInstance(
379                 displayName, "nonEx", commonPartName);
380         return result;
381     }
382
383     @Override
384     protected TaxonomyauthorityCommon updateInstance(
385             TaxonomyauthorityCommon taxonomyAuthority) {
386         TaxonomyauthorityCommon result = new TaxonomyauthorityCommon();
387
388         result.setDisplayName("updated-" + taxonomyAuthority.getDisplayName());
389         result.setVocabType("updated-" + taxonomyAuthority.getVocabType());
390
391         return result;
392     }
393
394     @Override
395     protected void compareUpdatedInstances(TaxonomyauthorityCommon original,
396             TaxonomyauthorityCommon updated) throws Exception {
397         // Verify that the updated resource received the correct data.
398         Assert.assertEquals(updated.getDisplayName(),
399                 original.getDisplayName(),
400                 "Display name in updated object did not match submitted data.");
401     }
402
403     //
404     // Authority item specific overrides
405     //
406     @Override
407     protected TaxonCommon updateItemInstance(TaxonCommon taxonCommon) {
408         TaxonCommon result = taxonCommon;
409         TaxonTermGroupList termList = taxonCommon.getTaxonTermGroupList();
410         Assert.assertNotNull(termList);
411         List<TaxonTermGroup> terms = termList.getTaxonTermGroup();
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         result.setTaxonTermGroupList(termList);
417         return result;
418     }
419
420     @Override
421     protected void compareUpdatedItemInstances(TaxonCommon original,
422             TaxonCommon updated) throws Exception {
423
424         TaxonTermGroupList originalTermList = original.getTaxonTermGroupList();
425         Assert.assertNotNull(originalTermList);
426         List<TaxonTermGroup> originalTerms = originalTermList.getTaxonTermGroup();
427         Assert.assertNotNull(originalTerms);
428         Assert.assertTrue(originalTerms.size() > 0);
429
430         TaxonTermGroupList updatedTermList = updated.getTaxonTermGroupList();
431         Assert.assertNotNull(updatedTermList);
432         List<TaxonTermGroup> updatedTerms = updatedTermList.getTaxonTermGroup();
433         Assert.assertNotNull(updatedTerms);
434         Assert.assertTrue(updatedTerms.size() > 0);
435
436         Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
437                 originalTerms.get(0).getTermDisplayName(),
438                 "Value in updated record did not match submitted data.");
439     }
440
441     @Override
442     protected void verifyReadItemInstance(TaxonCommon item) throws Exception {
443         
444         TaxonTermGroupList termList = item.getTaxonTermGroupList();
445         Assert.assertNotNull(termList);
446         List<TaxonTermGroup> terms = termList.getTaxonTermGroup();
447         Assert.assertNotNull(terms);
448         Assert.assertTrue(terms.size() > 0);
449         
450         String preferredTermName = terms.get(0).getTermName();
451         Assert.assertNotNull(preferredTermName, "Field value is unexpectedly null.");
452         Assert.assertEquals(preferredTermName, TEST_TAXON_FULL_NAME,
453                 "Field value " + preferredTermName
454                 + "does not match expected value " + TEST_TAXON_FULL_NAME);
455     }
456
457     @Override
458     protected PoxPayloadOut createNonExistenceItemInstance(
459             String commonPartName, String identifier) {
460         Map<String, String> nonexMap = new HashMap<String, String>();
461         nonexMap.put(TaxonJAXBSchema.NAME, TEST_TAXON_FULL_NAME);
462         nonexMap.put(TaxonJAXBSchema.SHORT_IDENTIFIER, "nonEx");
463         nonexMap.put(TaxonJAXBSchema.TERM_STATUS, TEST_TERM_STATUS);
464         final String EMPTY_REFNAME = "";
465         PoxPayloadOut result =
466                 TaxonomyAuthorityClientUtils.createTaxonInstance(EMPTY_REFNAME,
467                 nonexMap, NULL_TAXON_TERMS_LIST, NULL_TAXON_AUTHOR_GROUP_LIST, NULL_TAXON_CITATION_LIST,
468                 NULL_COMMON_NAME_GROUP_LIST, commonPartName);
469         return result;
470     }
471 }