implementing dao method listAcceptedTaxaFor, for #4112
[cdmlib.git] / cdmlib-persistence / src / test / java / eu / etaxonomy / cdm / persistence / dao / hibernate / taxon / TaxonDaoHibernateImplTest.java
1 /**
2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.persistence.dao.hibernate.taxon;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertNull;
16 import static org.junit.Assert.assertSame;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assert.fail;
19
20 import java.io.FileNotFoundException;
21 import java.io.FileOutputStream;
22 import java.util.ArrayList;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26 import java.util.UUID;
27
28 import org.apache.log4j.Level;
29 import org.hibernate.Hibernate;
30 import org.hibernate.envers.query.AuditEntity;
31 import org.hibernate.envers.query.criteria.AuditCriterion;
32 import org.junit.After;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Ignore;
36 import org.junit.Test;
37 import org.unitils.dbunit.annotation.DataSet;
38 import org.unitils.dbunit.annotation.ExpectedDataSet;
39 import org.unitils.spring.annotation.SpringBeanByType;
40
41 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
42 import eu.etaxonomy.cdm.model.common.Marker;
43 import eu.etaxonomy.cdm.model.common.MarkerType;
44 import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
45 import eu.etaxonomy.cdm.model.location.NamedArea;
46 import eu.etaxonomy.cdm.model.name.NonViralName;
47 import eu.etaxonomy.cdm.model.name.Rank;
48 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
49 import eu.etaxonomy.cdm.model.reference.Reference;
50 import eu.etaxonomy.cdm.model.taxon.Classification;
51 import eu.etaxonomy.cdm.model.taxon.Synonym;
52 import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
53 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
54 import eu.etaxonomy.cdm.model.taxon.Taxon;
55 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
56 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
57 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
58 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
59 import eu.etaxonomy.cdm.model.view.AuditEvent;
60 import eu.etaxonomy.cdm.model.view.AuditEventRecord;
61 import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
62 import eu.etaxonomy.cdm.persistence.dao.common.AuditEventSort;
63 import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
64 import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
65 import eu.etaxonomy.cdm.persistence.dao.taxon.IClassificationDao;
66 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
67 import eu.etaxonomy.cdm.persistence.fetch.CdmFetch;
68 import eu.etaxonomy.cdm.persistence.query.GroupByCount;
69 import eu.etaxonomy.cdm.persistence.query.GroupByDate;
70 import eu.etaxonomy.cdm.persistence.query.Grouping;
71 import eu.etaxonomy.cdm.persistence.query.MatchMode;
72 import eu.etaxonomy.cdm.persistence.query.NativeSqlOrderHint;
73 import eu.etaxonomy.cdm.persistence.query.OrderHint;
74 import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
75 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
76 import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
77
78 /**
79 * @author a.mueller
80 * @author ben.clark
81 *
82 */
83 public class TaxonDaoHibernateImplTest extends CdmTransactionalIntegrationTest {
84
85 @SpringBeanByType
86 private ITaxonDao taxonDao;
87
88 @SpringBeanByType
89 private IClassificationDao classificationDao;
90
91 @SpringBeanByType
92 private IReferenceDao referenceDao;
93
94 @SpringBeanByType
95 IDefinedTermDao definedTermDao;
96
97 private UUID uuid;
98 private UUID sphingidae;
99 private UUID acherontia;
100 private UUID mimas;
101 private UUID rethera;
102 private UUID retheraSecCdmtest;
103 private UUID atroposAgassiz; // a Synonym
104 private UUID atroposLeach; // a Synonym
105 private UUID acherontiaLachesis;
106
107 private AuditEvent previousAuditEvent;
108 private AuditEvent mostRecentAuditEvent;
109
110 private UUID northernAmericaUuid;
111 private UUID southernAmericaUuid;
112 private UUID antarcticaUuid;
113
114 private UUID classificationUuid;
115
116 private static final String[] TABLE_NAMES = new String[] {
117 "HOMOTYPICALGROUP", "HOMOTYPICALGROUP_AUD", "REFERENCE", "REFERENCE_AUD", "SYNONYMRELATIONSHIP", "SYNONYMRELATIONSHIP_AUD", "TAXONBASE", "TAXONBASE_AUD"
118 , "TAXONNAMEBASE", "TAXONNAMEBASE_AUD", "TAXONRELATIONSHIP", "TAXONRELATIONSHIP_AUD" };
119
120
121 @Before
122 public void setUp() {
123
124 uuid = UUID.fromString("496b1325-be50-4b0a-9aa2-3ecd610215f2");
125 sphingidae = UUID.fromString("54e767ee-894e-4540-a758-f906ecb4e2d9");
126 acherontia = UUID.fromString("c5cc8674-4242-49a4-aada-72d63194f5fa");
127 acherontiaLachesis = UUID.fromString("b04cc9cb-2b4a-4cc4-a94a-3c93a2158b06");
128 atroposAgassiz = UUID.fromString("d75b2e3d-7394-4ada-b6a5-93175b8751c1");
129 atroposLeach = UUID.fromString("3da4ab34-6c50-4586-801e-732615899b07");
130 rethera = UUID.fromString("a9f42927-e507-4fda-9629-62073a908aae");
131 retheraSecCdmtest = UUID.fromString("a9f42927-e507-4fda-9629-62073a908aae");
132
133
134 mimas = UUID.fromString("900052b7-b69c-4e26-a8f0-01c215214c40");
135 previousAuditEvent = new AuditEvent();
136 previousAuditEvent.setRevisionNumber(1025);
137 previousAuditEvent.setUuid(UUID.fromString("a680fab4-365e-4765-b49e-768f2ee30cda"));
138 mostRecentAuditEvent = new AuditEvent();
139 mostRecentAuditEvent.setRevisionNumber(1026);
140 mostRecentAuditEvent.setUuid(UUID.fromString("afe8e761-8545-497b-9134-6a6791fc0b0d"));
141 AuditEventContextHolder.clearContext(); // By default we're in the current view (i.e. view == null)
142
143 northernAmericaUuid = UUID.fromString("2757e726-d897-4546-93bd-7951d203bf6f");
144 southernAmericaUuid = UUID.fromString("6310b3ba-96f4-4855-bb5b-326e7af188ea");
145 antarcticaUuid = UUID.fromString("791b3aa0-54dd-4bed-9b68-56b4680aad0c");
146
147 classificationUuid = UUID.fromString("aeee7448-5298-4991-b724-8d5b75a0a7a9");
148 }
149
150 @After
151 public void tearDown() {
152 AuditEventContextHolder.clearContext();
153 }
154
155 /**
156 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#TaxonDaoHibernateImpl()}.
157 */
158 @Test
159 @DataSet
160 public void testInit() {
161 logger.warn("testInit()");
162 assertNotNull("Instance of ITaxonDao expected",taxonDao);
163 assertNotNull("Instance of IReferenceDao expected",referenceDao);
164 }
165
166 /**
167 * Test method for
168 * {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getRootTaxa(eu.etaxonomy.cdm.model.reference.Reference)}
169 * .
170 */
171 @Test
172 @DataSet
173 public void testGetRootTaxa() {
174 Reference sec1 = referenceDao.findById(1);
175 assert sec1 != null : "sec1 must exist";
176 Reference sec2 = referenceDao.findById(2);
177 assert sec2 != null : "sec2 must exist";
178
179 List<Taxon> rootTaxa = taxonDao.getRootTaxa(sec1);
180 assertNotNull("getRootTaxa should return a List", rootTaxa);
181 assertFalse("The list should not be empty", rootTaxa.isEmpty());
182 assertEquals("There should be one root taxon", 1, rootTaxa.size());
183
184 rootTaxa = taxonDao.getRootTaxa(sec1, CdmFetch.FETCH_CHILDTAXA(), true, false);
185 assertNotNull("getRootTaxa should return a List", rootTaxa);
186 assertFalse("The list should not be empty", rootTaxa.isEmpty());
187 assertEquals("There should be one root taxon", 1, rootTaxa.size());
188
189 rootTaxa = taxonDao.getRootTaxa(Rank.GENUS(), sec1, CdmFetch.FETCH_CHILDTAXA(), true, false, null);
190 assertNotNull("getRootTaxa should return a List", rootTaxa);
191 assertFalse("The list should not be empty", rootTaxa.isEmpty());
192 assertEquals("There should be one root taxon", 1, rootTaxa.size());
193
194 rootTaxa = taxonDao.getRootTaxa(Rank.FAMILY(), sec2, CdmFetch.FETCH_CHILDTAXA(), true, false, null);
195 if (logger.isDebugEnabled()) {
196 logger.debug("Root taxa rank Family (" + rootTaxa.size() + "):");
197 for (Taxon taxon : rootTaxa) {
198 logger.debug(taxon.getTitleCache());
199 }
200 }
201 assertEquals("There should be one root taxon rank Family", 1, rootTaxa.size());
202 rootTaxa = taxonDao.getRootTaxa(Rank.GENUS(), sec2, CdmFetch.FETCH_CHILDTAXA(), true, false, null);
203 assertNotNull("getRootTaxa should return a List", rootTaxa);
204 assertFalse("The list should not be empty", rootTaxa.isEmpty());
205 if (logger.isDebugEnabled()) {
206 logger.debug("Root taxa rank Genus (" + rootTaxa.size() + "):");
207 for (Taxon taxon : rootTaxa) {
208 logger.debug(taxon.getTitleCache());
209 }
210 }
211 assertEquals("There should be 22 root taxa rank Genus", 22, rootTaxa.size());
212
213 rootTaxa = taxonDao.getRootTaxa(Rank.SPECIES(), sec2, CdmFetch.FETCH_CHILDTAXA(), true, false, null);
214 if (logger.isDebugEnabled()) {
215 logger.debug("Root taxa rank Species (" + rootTaxa.size() + "):");
216 for (Taxon taxon : rootTaxa) {
217 logger.debug(taxon.getTitleCache());
218 }
219 }
220 assertEquals("There should be 4 root taxa rank Species", 3, rootTaxa.size());
221 }
222
223 /**
224 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)}.
225 */
226 @Test
227 @DataSet
228 public void testGetTaxaByName() {
229 Reference<?> sec = referenceDao.findById(1);
230 assert sec != null : "sec must exist";
231
232 List<TaxonBase> results = taxonDao.getTaxaByName("Aus", sec);
233 assertNotNull("getTaxaByName should return a List", results);
234 //assertFalse("The list should not be empty", results.isEmpty());
235 assertTrue(results.size() == 1);
236
237 results = taxonDao.getTaxaByName("A*", MatchMode.BEGINNING, true, null, null);
238 assertNotNull("getTaxaByName should return a List", results);
239
240 int numberOfTaxaByName_A = 9;
241
242 logger.setLevel(Level.DEBUG); //FIXME #######################
243 if (logger.isDebugEnabled()) {
244 for (int i = 0; i < results.size(); i++) {
245 String nameCache = "";
246 TaxonNameBase<?,?> taxonNameBase= results.get(i).getName();
247 nameCache = HibernateProxyHelper.deproxy(taxonNameBase, NonViralName.class).getNameCache();
248 logger.debug(results.get(i).getClass() + "(" + i +")" +
249 ": Name Cache = " + nameCache + ", Title Cache = " + results.get(i).getTitleCache());
250 }
251 }
252
253 assertEquals(numberOfTaxaByName_A, results.size());
254
255
256 //System.err.println("Species group: " + Rank.SPECIESGROUP().getId() + "Species: " + Rank.SPECIES().getId() + "Section Botany: "+ Rank.SECTION_BOTANY());
257
258 // assertEquals(results.get(0).getTitleCache(), "Abies sec. ???");
259 // assertEquals(results.get(1).getTitleCache(), "Abies Mill.");
260 // assertEquals(results.get(2).getTitleCache(), "Abies mill. sec. ???");
261 // assertEquals(results.get(3).getTitleCache(), "Abies alba sec. ???");
262 // assertEquals(results.get(4).getTitleCache(), "Abies alba Michx. sec. ???");
263 // assertEquals(results.get(5).getTitleCache(), "Abies alba Mill. sec. ???");
264
265 results = taxonDao.getTaxaByName("A", MatchMode.BEGINNING, true, null, null);
266 assertNotNull("getTaxaByName should return a List", results);
267 assertEquals(numberOfTaxaByName_A, results.size());
268
269 results = taxonDao.getTaxaByName("Aus", MatchMode.EXACT, true, null, null);
270 assertNotNull("getTaxaByName should return a List", results);
271 assertEquals("Results list should contain one entity",1,results.size());
272 }
273
274
275 @Test
276 //@Ignore
277 @DataSet (loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
278 public void testGetTaxaByNameWithMisappliedNames(){
279
280 Classification classification = classificationDao.load(classificationUuid);
281
282 /* NOTE:
283 * The testdata contains 3 misapplied names (1. nameCache = Aus, 2. nameCache = Rethera, 3. nameCache = Daphnis), two contained in the classification used in this test,
284 * the other one is not contained in any classification. This latter case is the more general situation.
285 * Misapplied names should be found regardless of whether they are contained in a classification or not.
286 */
287 //two accepted taxa starting with R in classification "TestBaum"
288 List<TaxonBase> results = taxonDao.getTaxaByName(true, false, false, "R*", classification, MatchMode.BEGINNING, null, null, null, null);
289 Assert.assertEquals("There should be 2 Taxa", 2, results.size());
290
291 //three taxa, 2 accepted and 1 misapplied name starting with R
292 results = taxonDao.getTaxaByName(true, false, true, "R*", null, MatchMode.BEGINNING, null, null, null, null);
293 Assert.assertEquals("There should be 3 Taxa", 3, results.size());
294
295 //one synonym is not in a synonymrelationship
296 results = taxonDao.getTaxaByName(true, true, true, "A*", null, MatchMode.BEGINNING, null, null, null, null);
297 Assert.assertEquals("There should be 11 Taxa",11, results.size());
298
299 //two accepted taxa in classification and 1 misapplied name with accepted name in classification
300 results = taxonDao.getTaxaByName(true, true, true, "R*", classification, MatchMode.BEGINNING, null, null, null, null);
301 Assert.assertEquals("There should be 3 Taxa", 3, results.size());
302
303 //same as above because all taxa, synonyms and misapplied names starting with R are in the classification
304 results = taxonDao.getTaxaByName(true, true, true, "R*", null, MatchMode.BEGINNING, null, null, null, null);
305 Assert.assertEquals("There should be 3 Taxa", 3, results.size());
306
307 //find misapplied names with accepted taxon in the classification, the accepted taxa of two misapplied names are in the classification
308 results = taxonDao.getTaxaByName(false, false, true, "*", classification, MatchMode.BEGINNING, null, null, null, null);
309 Assert.assertEquals("There should be 2 Taxa", 2, results.size());
310
311 //find misapplied names beginning with R
312 results = taxonDao.getTaxaByName(false, false, true, "R*", null, MatchMode.BEGINNING, null, null, null, null);
313 Assert.assertEquals("There should be 1 Taxa", 1, results.size());
314
315 //find all three misapplied names
316 results = taxonDao.getTaxaByName(false, false, true, "*", null, MatchMode.BEGINNING, null, null, null, null);
317 Assert.assertEquals("There should be 3 Taxa", 3, results.size());
318
319 }
320
321 /**
322 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)}.
323 */
324 @Test
325 @DataSet
326 public void testGetTaxaByNameForEditor() {
327 Reference<?> sec = referenceDao.findById(1);
328 assert sec != null : "sec must exist";
329
330 List<UuidAndTitleCache<TaxonBase>> results = taxonDao.getTaxaByNameForEditor(true, true, "Mand*", null, MatchMode.BEGINNING, null);
331 assertNotNull("getTaxaByName should return a List", results);
332 //assertFalse("The list should not be empty", results.isEmpty());
333 assertTrue(results.size() == 5);
334
335
336 results = taxonDao.getTaxaByNameForEditor(true, true,"A*",null, MatchMode.BEGINNING, null);
337 assertNotNull("getTaxaByName should return a List", results);
338 assertEquals(results.size(), 12);
339
340
341 results = taxonDao.getTaxaByNameForEditor(true, false,"A", null,MatchMode.BEGINNING, null);
342 assertNotNull("getTaxaByName should return a List", results);
343 assertTrue(results.size() == 9);
344 assertEquals(results.get(0).getType(), Taxon.class);
345
346 results = taxonDao.getTaxaByNameForEditor(false, true,"A", null,MatchMode.BEGINNING, null);
347 assertNotNull("getTaxaByName should return a List", results);
348 assertTrue(results.size() == 3);
349 assertEquals(results.get(0).getType(), Synonym.class);
350
351 results = taxonDao.getTaxaByNameForEditor(true, true,"Aus", null,MatchMode.EXACT, null);
352 assertNotNull("getTaxaByName should return a List", results);
353 assertEquals("Results list should contain one entity",1,results.size());
354 }
355
356
357 /**
358 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)}
359 * restricting the search by a set of Areas.
360 */
361 @Test
362 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
363 public void testGetTaxaByNameAndArea() {
364
365 Set<NamedArea> namedAreas = new HashSet<NamedArea>();
366 namedAreas.add((NamedArea)definedTermDao.load(northernAmericaUuid));
367 //namedAreas.add((NamedArea)definedTermDao.load(southernAmericaUuid));
368 //namedAreas.add((NamedArea)definedTermDao.load(antarcticaUuid));
369
370 Classification taxonmicTree = classificationDao.findByUuid(classificationUuid);
371
372 // prepare some synonym relation ships for some tests
373 Synonym synAtroposAgassiz = (Synonym)taxonDao.findByUuid(atroposAgassiz);
374 Taxon taxonRethera = (Taxon)taxonDao.findByUuid(rethera);
375 taxonRethera.addSynonym(synAtroposAgassiz, SynonymRelationshipType.SYNONYM_OF());
376 logger.warn("addSynonym(..)");
377 //this.taxonDao.clear();
378 Synonym synAtroposLeach = (Synonym)taxonDao.findByUuid(atroposLeach);
379 Taxon taxonRetheraSecCdmtest = (Taxon)taxonDao.findByUuid(retheraSecCdmtest);
380 taxonRetheraSecCdmtest.addSynonym(synAtroposLeach, SynonymRelationshipType.SYNONYM_OF());
381 this.taxonDao.save(taxonRetheraSecCdmtest);
382 //this.taxonDao.clear();
383 Taxon test = (Taxon)this.taxonDao.findByUuid(retheraSecCdmtest);
384 Set<Synonym> synonyms = test.getSynonyms();
385 // 1. searching for a taxon (Rethera)
386 //long numberOfTaxa = taxonDao.countTaxaByName(Taxon.class, "Rethera", null, MatchMode.BEGINNING, namedAreas);
387
388 List<TaxonBase> results = taxonDao.getTaxaByName(true,false, false, "Rethera", null, MatchMode.BEGINNING, namedAreas,
389 null, null, null);
390 assertNotNull("getTaxaByName should return a List", results);
391 assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
392
393 // 2. searching for a taxon (Rethera) contained in a specific classification
394 results = taxonDao.getTaxaByName(true, false, false, "Rethera", taxonmicTree, MatchMode.BEGINNING, namedAreas,
395 null, null, null);
396 assertNotNull("getTaxaByName should return a List", results);
397 assertTrue("expected to find one taxon but found "+results.size(), results.size() == 1);
398
399
400 // 3. searching for Synonyms
401 results = taxonDao.getTaxaByName(false, true, false, "Atropo", null, MatchMode.ANYWHERE, null,
402 null, null, null);
403 assertNotNull("getTaxaByName should return a List", results);
404 /*System.err.println(results.get(0).getTitleCache() + " - " +results.get(1).getTitleCache() + " - " +results.get(2).getTitleCache() );
405
406
407 System.err.println(((Synonym)results.get(0)).getAcceptedTaxa().contains(taxonRethera)+ " - " +((Synonym)results.get(1)).getAcceptedTaxa().contains(taxonRethera)+ " - " +((Synonym)results.get(2)).getAcceptedTaxa().contains(taxonRethera)+ " - " );
408 */
409 assertTrue("expected to find three taxa but found "+results.size(), results.size() == 3);
410
411 // 4. searching for Synonyms
412 results = taxonDao.getTaxaByName(false, true, false, "Atropo", null, MatchMode.BEGINNING, null,
413 null, null, null);
414 assertNotNull("getTaxaByName should return a List", results);
415 assertTrue("expected to find three taxa but found "+results.size(), results.size() == 3);
416
417
418 // 5. searching for a Synonyms and Taxa
419 // create a synonym relationship first
420 results = taxonDao.getTaxaByName(true, true, false, "A", null, MatchMode.BEGINNING, namedAreas,
421 null, null, null);
422 //only five taxa have a distribution
423 assertNotNull("getTaxaByName should return a List", results);
424 assertTrue("expected to find 7 taxa but found "+results.size(), results.size() == 8);
425 }
426
427
428 /**
429 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#findByNameTitleCache(Class<? extends TaxonBase>clazz, String queryString, Classification classification, MatchMode matchMode, Set<NamedArea> namedAreas, Integer pageNumber, Integer pageSize, List<String> propertyPaths)}
430 * restricting the search by a set of Areas.
431 */
432 @Test
433 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
434 public void testFindByNameTitleCache() {
435
436 Set<NamedArea> namedAreas = new HashSet<NamedArea>();
437 namedAreas.add((NamedArea)definedTermDao.load(northernAmericaUuid));
438 //namedAreas.add((NamedArea)definedTermDao.load(southernAmericaUuid));
439 //namedAreas.add((NamedArea)definedTermDao.load(antarcticaUuid));
440
441 Classification classification = classificationDao.findByUuid(classificationUuid);
442
443 // prepare some synonym relation ships for some tests
444 Synonym synAtroposAgassiz = (Synonym)taxonDao.findByUuid(atroposAgassiz);
445 Taxon taxonRethera = (Taxon)taxonDao.findByUuid(rethera);
446 taxonRethera.addSynonym(synAtroposAgassiz, SynonymRelationshipType.SYNONYM_OF());
447 logger.warn("addSynonym(..)");
448 this.taxonDao.clear();
449 Synonym synAtroposLeach = (Synonym)taxonDao.findByUuid(atroposLeach);
450 Taxon taxonRetheraSecCdmtest = (Taxon)taxonDao.findByUuid(retheraSecCdmtest);
451 taxonRetheraSecCdmtest.addSynonym(synAtroposLeach, SynonymRelationshipType.SYNONYM_OF());
452 this.taxonDao.clear();
453 // 1. searching for a taxon (Rethera)
454 //long numberOfTaxa = taxonDao.countTaxaByName(Taxon.class, "Rethera", null, MatchMode.BEGINNING, namedAreas);
455
456 List<TaxonBase> results = taxonDao.findByNameTitleCache(true, false, "Rethera Rothschild & Jordan, 1903", null, MatchMode.EXACT, namedAreas,
457 null, null, null);
458 assertNotNull("getTaxaByName should return a List", results);
459 assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
460
461 // 2. searching for a taxon (Rethera) contained in a specific classification
462 results = taxonDao.findByNameTitleCache(true, false, "Rethera Rothschild & Jordan, 1903", classification, MatchMode.EXACT, namedAreas,
463 null, null, null);
464 assertNotNull("getTaxaByName should return a List", results);
465 assertTrue("expected to find one taxon but found "+results.size(), results.size() == 1);
466
467
468 // 3. searching for Synonyms
469 results = taxonDao.findByNameTitleCache(false, true, "Atropo", null, MatchMode.ANYWHERE, null,
470 null, null, null);
471 assertNotNull("getTaxaByName should return a List", results);
472
473 assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
474
475 // 4. searching for Synonyms
476 results = taxonDao.findByNameTitleCache(false, true, "Atropo", null, MatchMode.BEGINNING, null,
477 null, null, null);
478 assertNotNull("getTaxaByName should return a List", results);
479 assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
480
481
482 // 5. searching for a Synonyms and Taxa
483 // create a synonym relationship first
484 Synonym syn = (Synonym)taxonDao.findByUuid(this.atroposLeach);
485 Taxon tax = (Taxon) taxonDao.findByUuid(rethera);
486 tax.addSynonym(syn, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
487
488 taxonDao.save(tax);
489 results = taxonDao.findByNameTitleCache(true, true, "A", null, MatchMode.BEGINNING, namedAreas,
490 null, null, null);
491 assertNotNull("getTaxaByName should return a List", results);
492 assertTrue("expected to find 8 taxa but found "+results.size(), results.size() == 8);
493 }
494
495 @Test
496 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
497 public void testTaxonNameInTwoClassifications(){
498 int numberOfClassifications = classificationDao.count();
499 List<String> propertyPaths = new ArrayList<String>();
500 propertyPaths.add("taxonNodes");
501 List<TaxonBase> taxa = taxonDao.getTaxaByName(true, true, false, "P*", null, MatchMode.BEGINNING, null, null, null, null);
502 Taxon taxon = (Taxon)taxa.get(0);
503 Set<TaxonNode> nodes = taxon.getTaxonNodes();
504 assertTrue(nodes.size() == 1);
505 //assertNotNull(taxa);
506 //assertTrue(taxa.size() > 0);
507 }
508
509 @Test
510 @DataSet
511 public void testFindByUuid() {
512 Taxon taxon = (Taxon)taxonDao.findByUuid(uuid);
513 assertNotNull("findByUuid should return a taxon",taxon);
514 assertFalse("findByUuid should not return a taxon with it's name initialized",Hibernate.isInitialized(taxon.getName()));
515 }
516
517 @Test
518 @DataSet
519 public void testLoad() {
520 List<String> propertyPaths = new ArrayList<String>();
521 propertyPaths.add("name");
522 propertyPaths.add("sec");
523 Taxon taxon = (Taxon)taxonDao.load(uuid, propertyPaths);
524 assertNotNull("findByUuid should return a taxon",taxon);
525 assertTrue("load should return a taxon with it's name initialized, given that the property was specified in the method",Hibernate.isInitialized(taxon.getName()));
526 assertTrue("load should return a taxon with it's secundum reference initialized, given that the property was specified in the method",Hibernate.isInitialized(taxon.getSec()));
527 }
528
529 @Test
530 @DataSet
531 public void testCountTaxonRelationships() {
532 Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
533 assert taxon != null : "taxon must exist";
534
535 int numberOfRelatedTaxa = taxonDao.countTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), TaxonRelationship.Direction.relatedTo);
536 assertEquals("countTaxonRelationships should return 23", 23, numberOfRelatedTaxa);
537 }
538
539 @Test
540 @DataSet
541 public void testCountTaxaByName() {
542 long numberOfTaxa = taxonDao.countTaxaByName(true, false, false, "A*", null, MatchMode.BEGINNING, null);
543 assertEquals(numberOfTaxa, 9);
544 numberOfTaxa = taxonDao.countTaxaByName(true, false, false, "Aus aus", null, MatchMode.EXACT, null);
545 assertEquals(numberOfTaxa, 1);
546 numberOfTaxa = taxonDao.countTaxaByName(false, true, false, "A*", null, MatchMode.BEGINNING, null);
547 assertEquals(numberOfTaxa, 3);
548 numberOfTaxa = taxonDao.countTaxaByName(true, true, false, "A*", null, MatchMode.BEGINNING, null);
549 assertEquals(numberOfTaxa,12);
550 numberOfTaxa = taxonDao.countTaxaByName(true, true, false, "Aasfwerfwf fffe", null, MatchMode.BEGINNING, null);
551 assertEquals(numberOfTaxa, 0);
552 // FIXME implement test for search in specific classification
553 // Reference reference = referenceDao.findByUuid(UUID.fromString("596b1325-be50-4b0a-9aa2-3ecd610215f2"));
554 // numberOfTaxa = taxonDao.countTaxaByName("A*", MatchMode.BEGINNING, SelectMode.ALL, null, null);
555 // assertEquals(numberOfTaxa, 2);
556 }
557
558 @Test
559 @DataSet
560 public void testRelatedTaxa() {
561 Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
562 assert taxon != null : "taxon must exist";
563
564 List<String> propertyPaths = new ArrayList<String>();
565 propertyPaths.add("fromTaxon");
566 propertyPaths.add("fromTaxon.name");
567 List<OrderHint> orderHints = new ArrayList<OrderHint>();
568 orderHints.add(new OrderHint("relatedFrom.name.genusOrUninomial", SortOrder.ASCENDING));
569 orderHints.add(new OrderHint("relatedFrom.name.specificEpithet", SortOrder.ASCENDING));
570 orderHints.add(new OrderHint("relatedFrom.name.infraSpecificEpithet", SortOrder.ASCENDING));
571
572 List<TaxonRelationship> relatedTaxa = taxonDao.getTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), null, null, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
573 assertNotNull("getRelatedTaxa should return a List",relatedTaxa);
574 assertEquals("getRelatedTaxa should return all 23 related taxa",relatedTaxa.size(),23);
575 assertTrue("getRelatedTaxa should return TaxonRelationship objects with the relatedFrom taxon initialized",Hibernate.isInitialized(relatedTaxa.get(0).getFromTaxon()));
576 assertTrue("getRelatedTaxa should return TaxonRelationship objects with the relatedFrom taxon initialized",Hibernate.isInitialized(relatedTaxa.get(0).getFromTaxon().getName()));
577
578 assertEquals("Acherontia should appear first in the list of related taxa", relatedTaxa.get(0).getFromTaxon().getTitleCache(), "Acherontia Laspeyres, 1809 sec. cate-sphingidae.org");
579 assertEquals("Sphingonaepiopsis should appear last in the list of related taxa", relatedTaxa.get(22).getFromTaxon().getTitleCache(), "Sphinx Linnaeus, 1758 sec. cate-sphingidae.org");
580 }
581
582 @Test
583 @DataSet
584 public void testGetRelatedTaxaPaged() {
585 Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
586 assert taxon != null : "taxon must exist";
587
588 List<String> propertyPaths = new ArrayList<String>();
589 propertyPaths.add("fromTaxon");
590 propertyPaths.add("fromTaxon.name");
591
592 List<OrderHint> orderHints = new ArrayList<OrderHint>();
593 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
594
595 List<TaxonRelationship> firstPage = taxonDao.getTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), 10, 0, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
596 List<TaxonRelationship> secondPage = taxonDao.getTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),10, 1, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
597 List<TaxonRelationship> thirdPage = taxonDao.getTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), 10, 2, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
598
599 assertNotNull("getRelatedTaxa: 10, 0 should return a List",firstPage);
600 assertEquals("getRelatedTaxa: 10, 0 should return a List with 10 elements",10,firstPage.size());
601 assertNotNull("getRelatedTaxa: 10, 1 should return a List",secondPage);
602 assertEquals("getRelatedTaxa: 10, 1 should return a List with 10 elements",secondPage.size(),10);
603 assertNotNull("getRelatedTaxa: 10, 2 should return a List",thirdPage);
604 assertEquals("getRelatedTaxa: 10, 2 should return a List with 3 elements",thirdPage.size(),3);
605 }
606
607 @Test
608 @DataSet
609 public void testCountSynonymRelationships() {
610 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
611 assert taxon != null : "taxon must exist";
612
613 int numberOfSynonymRelationships = taxonDao.countSynonyms(taxon,null);
614 assertEquals("countSynonymRelationships should return 5",5,numberOfSynonymRelationships);
615 }
616
617 @Test
618 @DataSet
619 public void testSynonymRelationships() {
620 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
621 assert taxon != null : "taxon must exist";
622 List<String> propertyPaths = new ArrayList<String>();
623 propertyPaths.add("synonym");
624 propertyPaths.add("synonym.name");
625
626 List<OrderHint> orderHints = new ArrayList<OrderHint>();
627 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
628
629 List<SynonymRelationship> synonyms = taxonDao.getSynonyms(taxon, null, null, null,orderHints,propertyPaths);
630
631 assertNotNull("getSynonyms should return a List",synonyms);
632 assertEquals("getSynonyms should return 5 SynonymRelationship entities",synonyms.size(),5);
633 assertTrue("getSynonyms should return SynonymRelationship objects with the synonym initialized",Hibernate.isInitialized(synonyms.get(0).getSynonym()));
634 }
635
636 @Test
637 @DataSet
638 public void testCountSynonymRelationshipsByType() {
639 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
640 assert taxon != null : "taxon must exist";
641
642 int numberOfTaxonomicSynonyms = taxonDao.countSynonyms(taxon, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
643 assertEquals("countSynonyms should return 4",numberOfTaxonomicSynonyms, 4);
644 }
645
646 @Test
647 @DataSet
648 public void testSynonymRelationshipsByType() {
649 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
650 assert taxon != null : "taxon must exist";
651
652 List<SynonymRelationship> synonyms = taxonDao.getSynonyms(taxon, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF(), null, null,null,null);
653
654 assertNotNull("getSynonyms should return a List",synonyms);
655 assertEquals("getSynonyms should return 4 SynonymRelationship entities",synonyms.size(),4);
656 }
657
658 @Test
659 @DataSet
660 public void testPageSynonymRelationships(){
661 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
662 assert taxon != null : "taxon must exist";
663
664 List<SynonymRelationship> firstPage = taxonDao.getSynonyms(taxon, null, 4, 0,null,null);
665 List<SynonymRelationship> secondPage = taxonDao.getSynonyms(taxon, null, 4, 1,null,null);
666
667 assertNotNull("getSynonyms: 4, 0 should return a List",firstPage);
668 assertEquals("getSynonyms: 4, 0 should return 4 SynonymRelationships", 4,firstPage.size());
669 assertNotNull("getSynonyms: 4, 1 should return a List",secondPage);
670 assertEquals("getSynonyms: 4, 1 should return 1 SynonymRelationship", 1, secondPage.size());
671 }
672
673 @Test
674 @DataSet("TaxonNodeDaoHibernateImplTest.xml")
675 public void testListAcceptedTaxaFor() {
676 UUID acheontitia_ciprosus = UUID.fromString("3ef145f7-bd92-4a64-8afd-2b8203e00e02");
677
678 Synonym synonym = (Synonym)taxonDao.findByUuid(acheontitia_ciprosus);
679 assertNotNull("synonym must exist", synonym);
680
681 List<Taxon> list = taxonDao.listAcceptedTaxaFor(synonym, null, 4, 0, null, null);
682 assertNotNull("listAcceptedTaxaFor should return a List");
683 assertEquals("listAcceptedTaxaFor should return 2 Taxa", 2, list.size());
684
685 Classification classification = classificationDao.load(classificationUuid);
686 assertNotNull("classification must exist", classification);
687
688 list = taxonDao.listAcceptedTaxaFor(synonym, classification, 4, 0, null, null);
689 assertNotNull("listAcceptedTaxaFor should return a List");
690 assertEquals("listAcceptedTaxaFor should return 1 Taxa", 1, list.size());
691 }
692
693
694 @Test
695 @DataSet
696 public void testGetTaxonMatchingUninomial() {
697 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class, "Smerinthus", "*", "*", "*",null,null,null);
698
699 assertNotNull("findTaxaByName should return a List", result);
700 assertEquals("findTaxaByName should return two Taxa",2,result.size());
701 assertEquals("findTaxaByName should return a Taxon with id 5",5,result.get(0).getId());
702 }
703
704 @Test
705 @DataSet
706 public void testGetTaxonMatchingSpeciesBinomial() {
707 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class,"Smerinthus", null, "kindermannii", null,null,null,null);
708
709 assertNotNull("findTaxaByName should return a List", result);
710 assertEquals("findTaxaByName should return one Taxon",1,result.size());
711 assertEquals("findTaxaByName should return a Taxon with id 8",8,result.get(0).getId());
712 }
713
714 @Test
715 @DataSet
716 public void testGetTaxonMatchingTrinomial() {
717 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class,"Cryptocoryne", null,"purpurea","borneoensis",null,null,null);
718
719 assertNotNull("findTaxaByName should return a List", result);
720 assertEquals("findTaxaByName should return one Taxon",1,result.size());
721 assertEquals("findTaxaByName should return a Taxon with id 38",38,result.get(0).getId());
722 }
723
724 @Test
725 @DataSet
726 public void testNegativeMatch() {
727 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class,"Acherontia", null,"atropos","dehli",null,null,null);
728
729 assertNotNull("findTaxaByName should return a List", result);
730 assertTrue("findTaxaByName should return an empty List",result.isEmpty());
731 }
732
733 @Test
734 @DataSet
735 public void testCountAllTaxa() {
736 int numberOfTaxa = taxonDao.count(Taxon.class);
737 assertEquals("count should return 33 taxa",33, numberOfTaxa);
738 }
739
740 @Test
741 @DataSet
742 public void testListAllTaxa() {
743 List<TaxonBase> taxa = taxonDao.list(Taxon.class,100, 0);
744 assertNotNull("list should return a List",taxa);
745 assertEquals("list should return 33 taxa",33, taxa.size());
746 }
747
748 @Test
749 @DataSet
750 // @ExpectedDataSet
751 public void testDelete() {
752 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
753 assert taxon != null : "taxon must exist";
754 taxonDao.delete(taxon);
755 taxon = (Taxon)taxonDao.findByUuid(acherontia);
756 assert taxon == null : "taxon must not exist";
757 setComplete();
758 endTransaction();
759 try {
760 printDataSet(new FileOutputStream("test.xml"), TABLE_NAMES);
761 } catch (FileNotFoundException e) {
762 e.printStackTrace();
763 }
764 }
765
766 @Test
767 @DataSet
768 // @ExpectedDataSet("TaxonDaoHibernateImplTest.testDelete-result.xml")
769 public void testDeleteWithMarker() {
770 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
771 taxon.addMarker(Marker.NewInstance(MarkerType.IS_DOUBTFUL(), true));
772 taxonDao.save(taxon);
773 assert taxon != null : "taxon must exist";
774
775 taxonDao.delete(taxon);
776 commitAndStartNewTransaction(null);
777 taxon = (Taxon)taxonDao.findByUuid(acherontia);
778 assert taxon == null : "taxon must not exist";
779 setComplete();
780 endTransaction();
781 try {
782 printDataSet(new FileOutputStream("test.xml"), TABLE_NAMES);
783 } catch (FileNotFoundException e) {
784 e.printStackTrace();
785 }
786 }
787
788 @Test
789 @DataSet
790 public void testDeleteWithChildren() {
791 Taxon taxonWithChildren = (Taxon)taxonDao.findByUuid(mimas);
792 assert taxonWithChildren != null : "taxon must exist";
793 assertEquals(taxonWithChildren.getTaxonomicChildrenCount(), 2);
794 Taxon parent = (Taxon)taxonDao.findByUuid(sphingidae);
795 assertSame(taxonWithChildren.getTaxonomicParent(), parent);
796 assertEquals(parent.getTaxonomicChildrenCount(), 204);
797 taxonDao.delete(taxonWithChildren);
798 assertEquals(parent.getTaxonomicChildrenCount(), 203);
799 }
800
801 @Test
802 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
803 public void testFindDeleted() {
804 TaxonBase<?> taxon = taxonDao.findByUuid(acherontia);
805 assertNull("findByUuid should return null in this view", taxon);
806 assertFalse("exist should return false in this view",taxonDao.exists(acherontia));
807 }
808
809 @Test
810 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
811 public void testFindDeletedInPreviousView() {
812 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
813 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
814 assertNotNull("findByUuid should return a taxon in this view",taxon);
815 assertTrue("exists should return true in this view", taxonDao.exists(acherontia));
816
817 try{
818 assertEquals("There should be 3 relations to this taxon in this view",3,taxon.getRelationsToThisTaxon().size());
819 } catch(Exception e) {
820 fail("We should not experience any problems initializing proxies with envers");
821 }
822 }
823
824 @Test
825 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
826 public void testGetAuditEvents() {
827 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
828 assert taxon != null : "taxon cannot be null";
829
830 List<String> propertyPaths = new ArrayList<String>();
831 propertyPaths.add("name");
832 propertyPaths.add("createdBy");
833 propertyPaths.add("updatedBy");
834
835 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(taxon, null,null,null,propertyPaths);
836 assertNotNull("getAuditEvents should return a list",auditEvents);
837 assertFalse("the list should not be empty",auditEvents.isEmpty());
838 assertEquals("There should be two AuditEventRecords in the list",2, auditEvents.size());
839 }
840
841 @Test
842 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
843 public void testGetAuditEventsFromNow() {
844 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
845 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
846 assert taxon != null : "taxon cannot be null";
847
848 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(taxon, null,null,AuditEventSort.FORWARDS,null);
849 assertNotNull("getAuditEvents should return a list",auditEvents);
850 assertFalse("the list should not be empty",auditEvents.isEmpty());
851 assertEquals("There should be one audit event in the list",1,auditEvents.size());
852 }
853
854 @Test
855 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
856 public void testCountAuditEvents() {
857 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
858 assert taxon != null : "taxon cannot be null";
859
860 int numberOfAuditEvents = taxonDao.countAuditEvents(taxon,null);
861 assertEquals("countAuditEvents should return 2",numberOfAuditEvents,2);
862 }
863
864 @Test
865 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
866 public void getPreviousAuditEvent() {
867 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
868 assert taxon != null : "taxon cannot be null";
869
870 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getPreviousAuditEvent(taxon);
871 assertNotNull("getPreviousAuditEvent should not return null as there is at least one audit event prior to the current one",auditEvent);
872 }
873
874 @Test
875 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
876 public void getPreviousAuditEventAtBeginning() {
877 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
878 TaxonBase taxon = taxonDao.findByUuid(sphingidae);
879 assert taxon != null : "taxon cannot be null";
880
881 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getPreviousAuditEvent(taxon);
882 assertNull("getPreviousAuditEvent should return null if we're at the first audit event anyway",auditEvent);
883 }
884
885 @Test
886 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
887 public void getNextAuditEvent() {
888 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
889 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
890 assert taxon != null : "taxon cannot be null";
891
892 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getNextAuditEvent(taxon);
893 assertNotNull("getNextAuditEvent should not return null as there is at least one audit event after the current one",auditEvent);
894 }
895
896 @Test
897 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
898 public void getNextAuditEventAtEnd() {
899 AuditEventContextHolder.getContext().setAuditEvent(mostRecentAuditEvent);
900 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
901 assert taxon != null : "taxon cannot be null";
902
903 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getNextAuditEvent(taxon);
904 assertNull("getNextAuditEvent should return null as there no more audit events after the current one",auditEvent);
905 }
906
907 @Test
908 @DataSet
909 @ExpectedDataSet
910 @Ignore
911 public void testAddChild() throws Exception {
912 Taxon parent = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
913 assert parent != null : "taxon cannot be null";
914 Taxon child = Taxon.NewInstance(null, null);
915 child.setTitleCache("Acherontia lachesis diehli Eitschberger, 2003", true);
916 child.addTaxonRelation(parent, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),null, null);
917 taxonDao.save(child);
918 setComplete();
919 endTransaction();
920 }
921
922 @Test
923 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
924 public void testFind() {
925 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
926 assert taxon != null : "taxon cannot be null";
927
928 assertEquals("getTaxonomicChildrenCount should return 1 in this view",1,taxon.getTaxonomicChildrenCount());
929 assertEquals("getRelationsToThisTaxon should contain 1 TaxonRelationship in this view",1,taxon.getRelationsToThisTaxon().size());
930 }
931
932 @Test
933 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
934 public void testFindInPreviousView() {
935 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
936 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
937 assert taxon != null : "taxon cannot be null";
938
939 assertEquals("getTaxonomicChildrenCount should return 0 in this view",0,taxon.getTaxonomicChildrenCount());
940 assertTrue("getRelationsToThisTaxon should contain 0 TaxonRelationship in this view",taxon.getRelationsToThisTaxon().isEmpty());
941 }
942
943 @Test
944 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
945 public void testGetRelations() {
946 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
947 assert taxon != null : "taxon cannot be null";
948
949 List<String> propertyPaths = new ArrayList<String>();
950 propertyPaths.add("fromTaxon");
951 propertyPaths.add("fromTaxon.name");
952
953 List<OrderHint> orderHints = new ArrayList<OrderHint>();
954 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
955
956 List<TaxonRelationship> taxonRelations = taxonDao.getTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), null, null,orderHints,propertyPaths, TaxonRelationship.Direction.relatedFrom);
957 assertNotNull("getRelatedTaxa should return a list", taxonRelations);
958 assertEquals("there should be one TaxonRelationship in the list in the current view",1,taxonRelations.size());
959 assertTrue("TaxonRelationship.relatedFrom should be initialized",Hibernate.isInitialized(taxonRelations.get(0).getFromTaxon()));
960 assertTrue("TaxonRelationship.relatedFrom.name should be initialized",Hibernate.isInitialized(taxonRelations.get(0).getFromTaxon().getName()));
961 }
962
963 @Test
964 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
965 public void testCountRelations() {
966 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
967 assert taxon != null : "taxon cannot be null";
968 assertEquals("countRelatedTaxa should return 1 in the current view",1, taxonDao.countTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), TaxonRelationship.Direction.relatedTo));
969 }
970
971 @Test
972 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
973 public void testGetRelationsInPreviousView() {
974 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
975 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
976 assert taxon != null : "taxon cannot be null";
977
978 List<String> propertyPaths = new ArrayList<String>();
979 propertyPaths.add("relatedFrom");
980 propertyPaths.add("relatedFrom.name");
981
982 List<OrderHint> orderHints = new ArrayList<OrderHint>();
983 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
984
985 List<TaxonRelationship> taxonRelations = taxonDao.getTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), null, null,orderHints,propertyPaths, TaxonRelationship.Direction.relatedFrom);
986 assertNotNull("getRelatedTaxa should return a list",taxonRelations);
987 assertTrue("there should be no TaxonRelationships in the list in the prior view",taxonRelations.isEmpty());
988 }
989
990 @Test
991 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
992 public void testCountRelationsInPreviousView() {
993 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
994 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
995 assert taxon != null : "taxon cannot be null";
996 assertEquals("countRelatedTaxa should return 0 in the current view",0, taxonDao.countTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), TaxonRelationship.Direction.relatedTo));
997 }
998
999 @Test
1000 @DataSet
1001 public void testGroupTaxa() {
1002 List<Grouping> groups = new ArrayList<Grouping>();
1003 groups.add(new GroupByCount("count",SortOrder.DESCENDING));
1004 groups.add(new Grouping("name.genusOrUninomial", "genus", "n", SortOrder.ASCENDING));
1005 List<Object[]> results = taxonDao.group(null, null, null, groups,null);
1006 System.out.println("count\tname.genuOrUninomial");
1007 for(Object[] result : results) {
1008 System.out.println(result[0] + "\t" + result[1]);
1009 }
1010 }
1011
1012 @Test
1013 @DataSet
1014 public void testGroupTaxaByClass() {
1015 List<Grouping> groups = new ArrayList<Grouping>();
1016 groups.add(new GroupByCount("count",SortOrder.DESCENDING));
1017 groups.add(new Grouping("class", "class",null, SortOrder.ASCENDING));
1018 List<Object[]> results = taxonDao.group(null, null, null, groups,null);
1019 System.out.println("count\tclass");
1020 for(Object[] result : results) {
1021 System.out.println(result[0] + "\t" + result[1]);
1022 }
1023 }
1024
1025 @Test
1026 @DataSet
1027 public void testNativeSQLOrder() {
1028 List<OrderHint> orderHints = new ArrayList<OrderHint>();
1029 orderHints.add(new NativeSqlOrderHint("case when {alias}.titleCache like 'C%' then 0 else 1 end",SortOrder.ASCENDING));
1030
1031 List<TaxonBase> results = taxonDao.list(null, null, orderHints);
1032 System.out.println("native SQL order");
1033 for(TaxonBase<?> result : results) {
1034 System.out.println(result.getTitleCache());
1035 }
1036 }
1037
1038 @Test
1039 @DataSet
1040 public void testGroupByDateTaxa() {
1041 List<Grouping> groups = new ArrayList<Grouping>();
1042 groups.add(new GroupByCount("count",null));
1043 groups.add(new GroupByDate("created", "dateGroup", SortOrder.ASCENDING, GroupByDate.Resolution.MONTH));
1044 List<Object[]> results = taxonDao.group(null, null, null, groups,null);
1045 System.out.println("count\tyear\tmonth");
1046 for(Object[] result : results) {
1047 System.out.println(result[0] + "\t" + result[1] + "\t" + result[2]);
1048 }
1049 }
1050
1051 @Test
1052 @DataSet ("TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
1053 public final void testGetTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(){
1054 Classification classification = classificationDao.findByUuid(classificationUuid);
1055 assertNotNull(taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification));
1056 }
1057
1058 @Test
1059 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
1060 @Ignore
1061 public void testGetAuditEventsByType() {
1062
1063 List<String> propertyPaths = new ArrayList<String>();
1064 propertyPaths.add("name");
1065 propertyPaths.add("createdBy");
1066 propertyPaths.add("updatedBy");
1067 int count = taxonDao.countAuditEvents(TaxonBase.class, null, null, null);
1068 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(TaxonBase.class, previousAuditEvent, mostRecentAuditEvent, null,null, null, AuditEventSort.FORWARDS, propertyPaths);
1069 assertNotNull("getAuditEvents should return a list",auditEvents);
1070 assertFalse("the list should not be empty",auditEvents.isEmpty());
1071 assertEquals("There should be thirty eight AuditEventRecords in the list",38, auditEvents.size());
1072 }
1073
1074 @Test
1075 @Ignore
1076 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
1077 public void testGetAuditEventsByTypeWithRestrictions() {
1078
1079 List<String> propertyPaths = new ArrayList<String>();
1080 propertyPaths.add("name");
1081 propertyPaths.add("createdBy");
1082 propertyPaths.add("updatedBy");
1083
1084 List<AuditCriterion> criteria = new ArrayList<AuditCriterion>();
1085 criteria.add(AuditEntity.property("lsid_lsid").isNotNull());
1086
1087 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(TaxonBase.class, previousAuditEvent, mostRecentAuditEvent, criteria,null, null, AuditEventSort.FORWARDS, propertyPaths);
1088 assertNotNull("getAuditEvents should return a list",auditEvents);
1089 assertFalse("the list should not be empty",auditEvents.isEmpty());
1090 assertEquals("There should be one AuditEventRecord in the list",1, auditEvents.size());
1091 }
1092 @Test
1093 @DataSet("TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
1094 public void testGetCommonName(){
1095
1096
1097 List<Object[]> commonNameResults = taxonDao.getTaxaByCommonName("common%", null,
1098 MatchMode.BEGINNING, null, null, null, null);
1099
1100 assertNotNull("getTaxaByCommonName should return a list", commonNameResults);
1101 assertFalse("the list should not be empty", commonNameResults.isEmpty());
1102 assertEquals("There should be one Taxon with common name", 1,commonNameResults.size());
1103 assertEquals(" sec. ???", ((TaxonBase)commonNameResults.get(0)[0]).getTitleCache());
1104
1105 }
1106
1107
1108 // @Test
1109 // public void testDeploy(){
1110 // TaxonBase taxonBase = taxonDao.findByUuid(UUID.fromString("6bfedf25-6dbc-4d5c-9d56-84f9052f3b2a"));
1111 //
1112 // Synonym synonym = taxonBase.deproxy(taxonBase, Synonym.class);
1113 // }
1114
1115
1116
1117 }