ref #7745 add subtree filter to taxon search (not yet tested for subtree != null)
[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.assertTrue;
17 import static org.junit.Assert.fail;
18
19 import java.io.FileNotFoundException;
20 import java.util.ArrayList;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24 import java.util.UUID;
25
26 import org.hibernate.Hibernate;
27 import org.hibernate.envers.query.AuditEntity;
28 import org.hibernate.envers.query.criteria.AuditCriterion;
29 import org.hibernate.proxy.HibernateProxy;
30 import org.junit.After;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.unitils.dbunit.annotation.DataSet;
35 import org.unitils.dbunit.annotation.ExpectedDataSet;
36 import org.unitils.spring.annotation.SpringBeanByType;
37
38 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
39 import eu.etaxonomy.cdm.model.common.Marker;
40 import eu.etaxonomy.cdm.model.common.MarkerType;
41 import eu.etaxonomy.cdm.model.location.NamedArea;
42 import eu.etaxonomy.cdm.model.name.TaxonName;
43 import eu.etaxonomy.cdm.model.reference.Reference;
44 import eu.etaxonomy.cdm.model.taxon.Classification;
45 import eu.etaxonomy.cdm.model.taxon.Synonym;
46 import eu.etaxonomy.cdm.model.taxon.SynonymType;
47 import eu.etaxonomy.cdm.model.taxon.Taxon;
48 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
49 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
50 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
51 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
52 import eu.etaxonomy.cdm.model.view.AuditEvent;
53 import eu.etaxonomy.cdm.model.view.AuditEventRecord;
54 import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
55 import eu.etaxonomy.cdm.persistence.dao.common.AuditEventSort;
56 import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
57 import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
58 import eu.etaxonomy.cdm.persistence.dao.taxon.IClassificationDao;
59 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
60 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
61 import eu.etaxonomy.cdm.persistence.query.GroupByCount;
62 import eu.etaxonomy.cdm.persistence.query.GroupByDate;
63 import eu.etaxonomy.cdm.persistence.query.Grouping;
64 import eu.etaxonomy.cdm.persistence.query.MatchMode;
65 import eu.etaxonomy.cdm.persistence.query.NativeSqlOrderHint;
66 import eu.etaxonomy.cdm.persistence.query.OrderHint;
67 import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
68 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
69 import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
70
71 /**
72 * @author a.mueller
73 * @author ben.clark
74 */
75 public class TaxonDaoHibernateImplTest extends CdmTransactionalIntegrationTest {
76
77 @SpringBeanByType
78 private ITaxonDao taxonDao;
79
80 @SpringBeanByType
81 private IClassificationDao classificationDao;
82
83 @SpringBeanByType
84 private IReferenceDao referenceDao;
85
86 @SpringBeanByType
87 private IDefinedTermDao definedTermDao;
88
89 private UUID uuid;
90 private UUID sphingidae;
91 private UUID acherontia;
92 private UUID rethera;
93 private UUID retheraSecCdmtest;
94 private UUID atroposAgassiz; // a Synonym
95 private UUID atroposOken; // a Synonym
96 private UUID atroposLeach; // a Synonym
97 private UUID acherontiaLachesis;
98 private UUID aus;
99
100 private AuditEvent previousAuditEvent;
101 private AuditEvent mostRecentAuditEvent;
102
103 private UUID northernAmericaUuid;
104 private UUID southernAmericaUuid;
105
106 private UUID classificationUuid;
107
108 private boolean includeUnpublished = true;
109 private boolean NO_UNPUBLISHED = false;
110
111 private static final boolean doTaxa = true;
112 private static final boolean noTaxa = false;
113 private static final boolean doSynonyms = true;
114 private static final boolean noSynonyms = false;
115 private static final boolean doMisapplied = true;
116 private static final boolean noMisapplied = false;
117 private static final boolean doCommonNames = true;
118 private static final boolean noCommonNames = false;
119
120
121
122 @SuppressWarnings("unused")
123 private static final String[] TABLE_NAMES = new String[] {
124 "HOMOTYPICALGROUP", "HOMOTYPICALGROUP_AUD", "REFERENCE", "REFERENCE_AUD", "TAXONBASE", "TAXONBASE_AUD"
125 , "TAXONNAME", "TAXONNAME_AUD", "TAXONRELATIONSHIP", "TAXONRELATIONSHIP_AUD" };
126
127
128 @Before
129 public void setUp() {
130
131 uuid = UUID.fromString("496b1325-be50-4b0a-9aa2-3ecd610215f2");
132 sphingidae = UUID.fromString("54e767ee-894e-4540-a758-f906ecb4e2d9");
133 acherontia = UUID.fromString("c5cc8674-4242-49a4-aada-72d63194f5fa");
134 acherontiaLachesis = UUID.fromString("b04cc9cb-2b4a-4cc4-a94a-3c93a2158b06");
135 atroposAgassiz = UUID.fromString("d75b2e3d-7394-4ada-b6a5-93175b8751c1");
136 atroposOken = UUID.fromString("6bfedf25-6dbc-4d5c-9d56-84f9052f3b2a");
137 atroposLeach = UUID.fromString("3da4ab34-6c50-4586-801e-732615899b07");
138 rethera = UUID.fromString("a9f42927-e507-4fda-9629-62073a908aae");
139 retheraSecCdmtest = UUID.fromString("a9f42927-e507-4fda-9629-62073a908aae");
140 aus = UUID.fromString("496b1325-be50-4b0a-9aa2-3ecd610215f2");
141
142 previousAuditEvent = new AuditEvent();
143 previousAuditEvent.setRevisionNumber(1025);
144 previousAuditEvent.setUuid(UUID.fromString("a680fab4-365e-4765-b49e-768f2ee30cda"));
145 mostRecentAuditEvent = new AuditEvent();
146 mostRecentAuditEvent.setRevisionNumber(1026);
147 mostRecentAuditEvent.setUuid(UUID.fromString("afe8e761-8545-497b-9134-6a6791fc0b0d"));
148 AuditEventContextHolder.clearContext(); // By default we're in the current view (i.e. view == null)
149
150 northernAmericaUuid = UUID.fromString("2757e726-d897-4546-93bd-7951d203bf6f");
151 southernAmericaUuid = UUID.fromString("6310b3ba-96f4-4855-bb5b-326e7af188ea");
152
153 classificationUuid = UUID.fromString("aeee7448-5298-4991-b724-8d5b75a0a7a9");
154 includeUnpublished = true;
155 }
156
157 @After
158 public void tearDown() {
159 AuditEventContextHolder.clearContext();
160 }
161
162 /**
163 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#TaxonDaoHibernateImpl()}.
164 */
165 @Test
166 @DataSet
167 public void testInit() {
168 logger.warn("testInit()");
169 assertNotNull("Instance of ITaxonDao expected",taxonDao);
170 assertNotNull("Instance of IReferenceDao expected",referenceDao);
171 }
172
173 @Test
174 @DataSet
175 public void testGetTaxaByName() {
176
177 includeUnpublished= true;
178 boolean accepted = true;
179
180 Reference sec = referenceDao.findById(1);
181 assert sec != null : "sec must exist";
182
183 @SuppressWarnings("rawtypes")
184 List<TaxonBase> results = taxonDao.getTaxaByName("Aus", includeUnpublished, sec);
185 assertNotNull("getTaxaByName should return a List", results);
186 //assertFalse("The list should not be empty", results.isEmpty());
187 assertEquals(1, results.size());
188
189 results = taxonDao.getTaxaByName("A*", MatchMode.BEGINNING, accepted, includeUnpublished, null, null);
190 assertNotNull("getTaxaByName should return a List", results);
191
192 int numberOfTaxaByName_A = 5;
193
194 //logger.setLevel(Level.DEBUG); //FIXME #######################
195 if (logger.isDebugEnabled()) {
196 for (int i = 0; i < results.size(); i++) {
197 String nameCache = "";
198 TaxonName taxonName= results.get(i).getName();
199 nameCache = taxonName.getNameCache();
200 logger.debug(results.get(i).getClass() + "(" + i +")" +
201 ": Name Cache = " + nameCache + ", Title Cache = " + results.get(i).getTitleCache());
202 }
203 }
204
205 assertEquals(numberOfTaxaByName_A, results.size());
206
207 includeUnpublished = false;
208 results = taxonDao.getTaxaByName("A*", MatchMode.BEGINNING, accepted, includeUnpublished, null, null);
209 assertEquals(numberOfTaxaByName_A, results.size()); // no unpublished yet
210
211
212 //System.err.println("Species group: " + Rank.SPECIESGROUP().getId() + "Species: " + Rank.SPECIES().getId() + "Section Botany: "+ Rank.SECTION_BOTANY());
213
214 // assertEquals(results.get(0).getTitleCache(), "Abies sec. ???");
215 // assertEquals(results.get(1).getTitleCache(), "Abies Mill.");
216 // assertEquals(results.get(2).getTitleCache(), "Abies mill. sec. ???");
217 // assertEquals(results.get(3).getTitleCache(), "Abies alba sec. ???");
218 // assertEquals(results.get(4).getTitleCache(), "Abies alba Michx. sec. ???");
219 // assertEquals(results.get(5).getTitleCache(), "Abies alba Mill. sec. ???");
220
221 includeUnpublished = true;
222 results = taxonDao.getTaxaByName("A*", MatchMode.BEGINNING, accepted, includeUnpublished, null, null);
223 assertNotNull("getTaxaByName should return a List", results);
224 assertEquals(numberOfTaxaByName_A, results.size());
225
226 results = taxonDao.getTaxaByName("Aus", MatchMode.EXACT, accepted, includeUnpublished, null, null);
227 assertNotNull("getTaxaByName should return a List", results);
228 assertEquals("Results list should contain one entity",1,results.size());
229 }
230
231
232 @Test
233 @DataSet (loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
234 public void testGetTaxaByNameWithMisappliedNames(){
235
236 Classification classification = classificationDao.load(classificationUuid);
237 TaxonNode subtree = null;
238 /* NOTE:
239 * The testdata contains 3 misapplied names (1. nameCache = Aus, 2. nameCache = Rethera, 3. nameCache = Daphnis),
240 * two contained in the classification used in this test,
241 * the other one is not contained in any classification. This later case is the more general situation.
242 * Misapplied names should be found regardless of whether they are contained in a classification or not.
243 */
244 //two accepted taxa starting with R in classification "TestBaum"
245 @SuppressWarnings("rawtypes")
246 List<TaxonBase> results = taxonDao.getTaxaByName(doTaxa, noSynonyms, noMisapplied, noCommonNames, false, "R*", classification, subtree, MatchMode.BEGINNING,
247 null, includeUnpublished, null, null, null, null);
248 Assert.assertEquals("There should be 2 Taxa", 2, results.size());
249
250 //three taxa, 2 accepted and 1 misapplied name starting with R
251 results = taxonDao.getTaxaByName(doTaxa, noSynonyms, doMisapplied, noCommonNames, false, "R*", null, subtree, MatchMode.BEGINNING,
252 null, includeUnpublished, null, null, null, null);
253 Assert.assertEquals("There should be 3 Taxa", 3, results.size());
254
255 //one synonym has no accepted taxon
256 results = taxonDao.getTaxaByName(doTaxa, doSynonyms, doMisapplied, noCommonNames, false, "A*", null, subtree, MatchMode.BEGINNING,
257 null, includeUnpublished, null, null, null, null);
258 Assert.assertEquals("There should be 11 Taxa",11, results.size());
259
260 //two accepted taxa in classification and 1 misapplied name with accepted name in classification
261 results = taxonDao.getTaxaByName(doTaxa, doSynonyms, doMisapplied, noCommonNames, false, "R*", classification, subtree, MatchMode.BEGINNING,
262 null, includeUnpublished, null, null, null, null);
263 Assert.assertEquals("There should be 3 Taxa", 3, results.size());
264 //same with unpublished
265 includeUnpublished = false;
266 results = taxonDao.getTaxaByName(doTaxa, doSynonyms, doMisapplied, noCommonNames, false, "R*", classification, subtree, MatchMode.BEGINNING,
267 null, includeUnpublished, null, null, null, null);
268 Assert.assertEquals("There should be 3 Taxa", 3, results.size());
269 includeUnpublished = true;
270
271
272 //same as above because all taxa, synonyms and misapplied names starting with R are in the classification
273 results = taxonDao.getTaxaByName(doTaxa, doSynonyms, doMisapplied, noCommonNames, false, "R*", null, subtree, MatchMode.BEGINNING,
274 null, includeUnpublished, null, null, null, null);
275 Assert.assertEquals("There should be 3 Taxa", 3, results.size());
276
277 //find misapplied names with accepted taxon in the classification, the accepted taxa of two misapplied names are in the classification
278 results = taxonDao.getTaxaByName(noTaxa, noSynonyms, doMisapplied, noCommonNames, false, "*", classification, subtree, MatchMode.BEGINNING,
279 null, includeUnpublished, null, null, null, null);
280 Assert.assertEquals("There should be 2 Taxa", 2, results.size());
281
282 //find misapplied names beginning with R
283 results = taxonDao.getTaxaByName(noTaxa, noSynonyms, doMisapplied, noCommonNames, false, "R*", null, subtree, MatchMode.BEGINNING,
284 null, includeUnpublished, null, null, null, null);
285 Assert.assertEquals("There should be 1 Taxa", 1, results.size());
286
287 //find all three misapplied names
288 results = taxonDao.getTaxaByName(noTaxa, noSynonyms, doMisapplied, noCommonNames, false, "*", null, subtree, MatchMode.BEGINNING,
289 null, includeUnpublished, null, null, null, null);
290 Assert.assertEquals("There should be 3 Taxa", 3, results.size());
291
292 }
293 /**
294 * Test for all not covered possibilities of searches
295 */
296 @Test
297 @DataSet (loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
298 public void testGetTaxaByNameVariants(){
299 TaxonNode subtree = null;
300 @SuppressWarnings("rawtypes")
301 List<TaxonBase> results = taxonDao.getTaxaByName(noTaxa, noSynonyms, noMisapplied, doCommonNames, false, "c*", null, subtree, MatchMode.BEGINNING,
302 null, includeUnpublished, null, null, null, null);
303 Assert.assertEquals("There should be 2 Taxa",2, results.size());
304
305 results = taxonDao.getTaxaByName(noTaxa, noSynonyms, doMisapplied, doCommonNames, false, "R*", null, subtree, MatchMode.BEGINNING,
306 null, includeUnpublished, null, null, null, null);
307 Assert.assertEquals("There should be 1 Taxa", 1, results.size());
308
309 results = taxonDao.getTaxaByName(noTaxa, doSynonyms, doMisapplied, doCommonNames, false, "R*", null, subtree, MatchMode.BEGINNING,
310 null, includeUnpublished, null, null, null, null);
311 Assert.assertEquals("There should be 1 Taxa", 1, results.size());
312
313 results = taxonDao.getTaxaByName(noTaxa, doSynonyms, noMisapplied, doCommonNames, false, "c*", null, subtree, MatchMode.BEGINNING,
314 null, includeUnpublished, null, null, null, null);
315 Assert.assertEquals("There should be 2 Taxa", 2, results.size());
316
317 results = taxonDao.getTaxaByName(doTaxa, noSynonyms, noMisapplied, doCommonNames, false, "c*", null, subtree, MatchMode.BEGINNING,
318 null, includeUnpublished, null, null, null, null);
319 Assert.assertEquals("There should be 2 Taxa", 2, results.size());
320
321 Classification classification = classificationDao.load(classificationUuid);
322 results = taxonDao.getTaxaByName(noTaxa, noSynonyms, noMisapplied, doCommonNames, false, "c*", classification, subtree, MatchMode.BEGINNING,
323 null, includeUnpublished, null, null, null, null);
324 Assert.assertEquals("There should be 1 Taxa", 1, results.size());
325
326 Set<NamedArea> namedAreas = new HashSet<>();
327 namedAreas.add((NamedArea)definedTermDao.load(southernAmericaUuid));
328 results = taxonDao.getTaxaByName(noTaxa, noSynonyms, noMisapplied, doCommonNames, false, "c*", null, subtree, MatchMode.BEGINNING,
329 namedAreas, includeUnpublished, null, null, null, null);
330 Assert.assertEquals("There should be 1 Taxa", 1, results.size());
331 }
332
333 /**
334 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)}.
335 */
336 @Test
337 @DataSet
338 public void testGetTaxaByNameForEditor() {
339 TaxonNode subtree = null;
340 Reference sec = referenceDao.findById(1);
341 assert sec != null : "sec must exist";
342
343 @SuppressWarnings("rawtypes")
344 List<UuidAndTitleCache<? extends IdentifiableEntity>> results = taxonDao.getTaxaByNameForEditor(
345 doTaxa, doSynonyms, noMisapplied, noCommonNames, false, includeUnpublished, "Acher", null, subtree, MatchMode.BEGINNING, null, null);
346 assertNotNull("getTaxaByName should return a List", results);
347 assertFalse("The list should not be empty", results.isEmpty());
348 assertEquals(4, results.size());
349
350
351 results = taxonDao.getTaxaByNameForEditor(doTaxa, doSynonyms, noMisapplied, noCommonNames, false,includeUnpublished, "A",null, subtree, MatchMode.BEGINNING, null, null);
352 assertNotNull("getTaxaByName should return a List", results);
353 assertEquals(7, results.size());
354
355
356 results = taxonDao.getTaxaByNameForEditor(doTaxa, noSynonyms, noMisapplied, noCommonNames, false, includeUnpublished, "A", null, subtree, MatchMode.BEGINNING, null, null);
357 assertNotNull("getTaxaByName should return a List", results);
358 assertEquals(5, results.size());
359 assertEquals(results.get(0).getType(), Taxon.class);
360
361 results = taxonDao.getTaxaByNameForEditor(noTaxa, doSynonyms, noMisapplied, noCommonNames, false, includeUnpublished, "A", null, subtree, MatchMode.BEGINNING, null, null);
362 assertNotNull("getTaxaByName should return a List", results);
363 assertEquals(2, results.size());
364 assertEquals(results.get(0).getType(), Synonym.class);
365
366 results = taxonDao.getTaxaByNameForEditor(doTaxa, doSynonyms, noMisapplied, noCommonNames, false,includeUnpublished, "Aus", null, subtree, MatchMode.EXACT, null, null);
367 assertNotNull("getTaxaByName should return a List", results);
368 assertEquals("Results list should contain one entity",1,results.size());
369
370 results = taxonDao.getTaxaByNameForEditor(doTaxa, doSynonyms, doMisapplied, noCommonNames, false, includeUnpublished, "A", null, subtree, MatchMode.BEGINNING, null, null);
371 assertNotNull("getTaxaByName should return a List", results);
372 assertEquals("Results list should contain one entity", 8, results.size());
373
374 //TODO: test the search for misapplied names
375
376 }
377
378
379 /**
380 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)}
381 * restricting the search by a set of Areas.
382 */
383 @Test
384 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
385 public void testGetTaxaByNameAndArea() {
386 TaxonNode subtree = null;
387 Set<NamedArea> namedAreas = new HashSet<>();
388 namedAreas.add((NamedArea)definedTermDao.load(northernAmericaUuid));
389 //namedAreas.add((NamedArea)definedTermDao.load(southernAmericaUuid));
390 //namedAreas.add((NamedArea)definedTermDao.load(antarcticaUuid));
391
392 Classification classification = classificationDao.findByUuid(classificationUuid);
393
394 // prepare some synonym relation ships for some tests
395 Synonym synAtroposAgassiz = (Synonym)taxonDao.findByUuid(atroposAgassiz);
396 Taxon taxonRethera = (Taxon)taxonDao.findByUuid(rethera);
397 taxonRethera.addSynonym(synAtroposAgassiz, SynonymType.SYNONYM_OF());
398 // logger.warn("addSynonym(..)");
399
400 Synonym synAtroposLeach = (Synonym)taxonDao.findByUuid(atroposLeach);
401 Taxon taxonRetheraSecCdmTest = (Taxon)taxonDao.findByUuid(retheraSecCdmtest);
402 taxonRetheraSecCdmTest.addSynonym(synAtroposLeach, SynonymType.SYNONYM_OF());
403 this.taxonDao.save(taxonRetheraSecCdmTest);
404
405 Taxon test = (Taxon)this.taxonDao.findByUuid(retheraSecCdmtest);
406 // Set<Synonym> synonyms3 = test.getSynonyms();
407 // 1. searching for a taxon (Rethera)
408 //long numberOfTaxa = taxonDao.countTaxaByName(Taxon.class, "Rethera", null, MatchMode.BEGINNING, namedAreas);
409
410 @SuppressWarnings("rawtypes")
411 List<TaxonBase> results = taxonDao.getTaxaByName(doTaxa, noSynonyms, noMisapplied, noCommonNames, false, "Rethera", null, subtree, MatchMode.BEGINNING, namedAreas,
412 includeUnpublished, null, null, null, null);
413 assertNotNull("getTaxaByName should return a List", results);
414 assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
415
416 results = taxonDao.getTaxaByName(noTaxa, noSynonyms, noMisapplied, doCommonNames, false, "com*", null, subtree, MatchMode.BEGINNING, namedAreas,
417 includeUnpublished, null, null, null, null);
418 assertNotNull("getTaxaByName should return a List", results);
419 assertTrue("expected to find one taxon but found "+results.size(), results.size() == 1);
420
421 // 2. searching for a taxon (Rethera) contained in a specific classification
422 results = taxonDao.getTaxaByName(doTaxa, noSynonyms, noMisapplied, noCommonNames, false, "Rethera", classification, subtree, MatchMode.BEGINNING, namedAreas,
423 includeUnpublished, null, null, null, null);
424 assertNotNull("getTaxaByName should return a List", results);
425 assertTrue("expected to find one taxon but found "+results.size(), results.size() == 1);
426
427
428 // 3. searching for Synonyms
429 results = taxonDao.getTaxaByName(noTaxa, doSynonyms, noMisapplied, noCommonNames, false, "Atropo", null, subtree, MatchMode.ANYWHERE, null,
430 includeUnpublished, null, null, null, null);
431 assertNotNull("getTaxaByName should return a List", results);
432 /*System.err.println(results.get(0).getTitleCache() + " - " +results.get(1).getTitleCache() + " - " +results.get(2).getTitleCache() );
433
434
435 System.err.println(((Synonym)results.get(0)).getAcceptedTaxa().contains(taxonRethera)+ " - " +((Synonym)results.get(1)).getAcceptedTaxa().contains(taxonRethera)+ " - " +((Synonym)results.get(2)).getAcceptedTaxa().contains(taxonRethera)+ " - " );
436 */
437 assertTrue("expected to find three taxa but found "+results.size(), results.size() == 3);
438
439 // 4. searching for Synonyms
440 results = taxonDao.getTaxaByName(noTaxa, doSynonyms, noMisapplied, noCommonNames, false,"Atropo", null, subtree, MatchMode.BEGINNING, null,
441 includeUnpublished, null, null, null, null);
442 assertNotNull("getTaxaByName should return a List", results);
443 assertTrue("expected to find three taxa but found "+results.size(), results.size() == 3);
444
445
446 // 5. searching for a Synonyms and Taxa
447 results = taxonDao.getTaxaByName(doTaxa, doSynonyms, noMisapplied, noCommonNames, false,"A", null, subtree, MatchMode.BEGINNING, namedAreas,
448 includeUnpublished, null, null, null, null);
449 //only five taxa have a distribution
450 assertNotNull("getTaxaByName should return a List", results);
451 assertTrue("expected to find 8 taxa but found "+results.size(), results.size() == 8);
452 }
453
454
455 /**
456 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#findByNameTitleCache(Class<? extends TaxonBase>clazz, String queryString, Classification classification, TaxonNode subtree, MatchMode matchMode, Set<NamedArea> namedAreas, Integer pageNumber, Integer pageSize, List<String> propertyPaths)}
457 * restricting the search by a set of Areas.
458 */
459 @Test
460 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
461 public void testFindByNameTitleCache() {
462 TaxonNode subtree = null;
463
464 Set<NamedArea> namedAreas = new HashSet<>();
465 namedAreas.add((NamedArea)definedTermDao.load(northernAmericaUuid));
466 //namedAreas.add((NamedArea)definedTermDao.load(southernAmericaUuid));
467 //namedAreas.add((NamedArea)definedTermDao.load(antarcticaUuid));
468
469 Classification classification = classificationDao.findByUuid(classificationUuid);
470
471 // prepare some synonym relation ships for some tests
472 Synonym synAtroposAgassiz = (Synonym)taxonDao.findByUuid(atroposAgassiz);
473 Taxon taxonRethera = (Taxon)taxonDao.findByUuid(rethera);
474 taxonRethera.addSynonym(synAtroposAgassiz, SynonymType.SYNONYM_OF());
475 logger.warn("addSynonym(..)");
476 this.taxonDao.clear();
477 Synonym synAtroposLeach = (Synonym)taxonDao.findByUuid(atroposLeach);
478 Taxon taxonRetheraSecCdmtest = (Taxon)taxonDao.findByUuid(retheraSecCdmtest);
479 taxonRetheraSecCdmtest.addSynonym(synAtroposLeach, SynonymType.SYNONYM_OF());
480 this.taxonDao.clear();
481 // 1. searching for a taxon (Rethera)
482 //long numberOfTaxa = taxonDao.countTaxaByName(Taxon.class, "Rethera", null, MatchMode.BEGINNING, namedAreas);
483
484 @SuppressWarnings("rawtypes")
485 List<TaxonBase> results = taxonDao.findByNameTitleCache(true, false, includeUnpublished, "Rethera Rothschild & Jordan, 1903", null, subtree, MatchMode.EXACT, namedAreas,
486 null, null, null, null);
487 assertNotNull("getTaxaByName should return a List", results);
488 assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
489
490 // 2. searching for a taxon (Rethera) contained in a specific classification
491 results = taxonDao.findByNameTitleCache(true, false, includeUnpublished, "Rethera Rothschild & Jordan, 1903", classification, subtree, MatchMode.EXACT, namedAreas,
492 null, null, null, null);
493 assertNotNull("getTaxaByName should return a List", results);
494 assertTrue("expected to find one taxon but found "+results.size(), results.size() == 1);
495
496
497 // 3. searching for Synonyms
498 results = taxonDao.findByNameTitleCache(false, true, includeUnpublished, "*Atropo", null, subtree, MatchMode.ANYWHERE, null,
499 null, null, null, null);
500 assertNotNull("getTaxaByName should return a List", results);
501 assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
502
503 // 4. searching for Synonyms
504 results = taxonDao.findByNameTitleCache(false, true, includeUnpublished, "Atropo", null, subtree, MatchMode.BEGINNING, null,
505 null, null, null, null);
506 assertNotNull("getTaxaByName should return a List", results);
507 assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
508
509
510 // 5. searching for a Synonyms and Taxa
511 // attache a synonym first
512 Synonym syn = (Synonym)taxonDao.findByUuid(this.atroposLeach);
513 Taxon tax = (Taxon) taxonDao.findByUuid(rethera);
514 tax.addSynonym(syn, SynonymType.HETEROTYPIC_SYNONYM_OF());
515
516 taxonDao.save(tax);
517 results = taxonDao.findByNameTitleCache(true, true, includeUnpublished, "A", null, subtree, MatchMode.BEGINNING, namedAreas,
518 null, null, null, null);
519 assertNotNull("getTaxaByName should return a List", results);
520 assertTrue("expected to find 8 taxa but found "+results.size(), results.size() == 8);
521 }
522
523 @Test
524 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
525 public void testTaxonNameInTwoClassifications(){
526 TaxonNode subtree = null;
527 List<String> propertyPaths = new ArrayList<>();
528 propertyPaths.add("taxonNodes");
529 @SuppressWarnings("rawtypes")
530 List<TaxonBase> taxa = taxonDao.getTaxaByName(doTaxa, doSynonyms, noMisapplied, noCommonNames, false,"P", null, subtree, MatchMode.BEGINNING,
531 null, includeUnpublished, null, null, null, null);
532 Taxon taxon = (Taxon)taxa.get(0);
533 Set<TaxonNode> nodes = taxon.getTaxonNodes();
534 assertTrue(nodes.size() == 1);
535 //assertNotNull(taxa);
536 //assertTrue(taxa.size() > 0);
537 }
538
539 @Test
540 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
541 public void testGetTaxaByNameProParteSynonym(){
542 TaxonNode subtree = null;
543 @SuppressWarnings("rawtypes")
544 List<TaxonBase> taxa = taxonDao.getTaxaByName(noTaxa, doSynonyms, noMisapplied, noCommonNames, false, "A", null,subtree,
545 MatchMode.BEGINNING, null, includeUnpublished, null, null, null, null);
546 Assert.assertEquals("2 synonyms and 1 pro parte synonym should be returned.", 3, taxa.size());
547 assertTrue("Pro parte should exist", existsInCollection(taxa, acherontiaLachesis));
548 assertTrue("Normal synonym should exist", existsInCollection(taxa, atroposAgassiz));
549 assertTrue("2. normal synonym should exist", existsInCollection(taxa, atroposOken));
550 //TODO shouldn't we also find orphaned synonyms (without accepted taxon) like Atropos Leach?
551
552 taxa = taxonDao.getTaxaByName(noTaxa, doSynonyms, noMisapplied, noCommonNames, false, "A", null,subtree,
553 MatchMode.BEGINNING, null, NO_UNPUBLISHED, null, null, null, null);
554 Assert.assertEquals("2 synonyms and no pro parte synonym should be returned.", 2, taxa.size());
555 assertTrue("Normal synonym should exist", existsInCollection(taxa, atroposAgassiz));
556 assertTrue("2. normal synonym should exist", existsInCollection(taxa, atroposOken));
557
558 taxa = taxonDao.getTaxaByName(noTaxa, noSynonyms, doMisapplied, noCommonNames, false, "A", null,subtree,
559 MatchMode.BEGINNING, null, includeUnpublished, null, null, null, null);
560 Assert.assertEquals("1 misapplied name, no pro parte synonym should be returned.", 1, taxa.size());
561 assertTrue("Pro parte should exist", existsInCollection(taxa, aus));
562
563 }
564
565 @Test
566 @DataSet
567 public void testFindByUuid() {
568 Taxon taxon = (Taxon)taxonDao.findByUuid(uuid);
569 assertNotNull("findByUuid should return a taxon", taxon);
570 assertFalse("findByUuid should not return a taxon with it's name initialized",Hibernate.isInitialized(taxon.getName()));
571 }
572
573 @Test
574 @DataSet
575 public void testLoad() {
576 List<String> propertyPaths = new ArrayList<>();
577 propertyPaths.add("name");
578 propertyPaths.add("sec");
579 Taxon taxon = (Taxon)taxonDao.load(uuid, propertyPaths);
580 assertNotNull("findByUuid should return a taxon",taxon);
581 assertTrue("load should return a taxon with it's name initialized, given that the property was specified in the method",Hibernate.isInitialized(taxon.getName()));
582 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()));
583 }
584
585 @Test
586 @DataSet
587 public void testCountTaxonRelationshipsByTaxon() {
588 Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
589 assert taxon != null : "taxon must exist";
590
591 long numberOfRelatedTaxa = taxonDao.countTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
592 includeUnpublished, TaxonRelationship.Direction.relatedTo);
593 assertEquals("countTaxonRelationships should return 8", 8, numberOfRelatedTaxa);
594 }
595
596 @Test
597 @DataSet
598 public void testCountTaxaByName() {
599 TaxonNode subtree = null;
600 long numberOfTaxa = taxonDao.countTaxaByName(true, false, false, false,false, "A", null, subtree, MatchMode.BEGINNING, null, includeUnpublished);
601 assertEquals(5, numberOfTaxa);
602 numberOfTaxa = taxonDao.countTaxaByName(true, false, false, false, false,"Smerinthus kindermannii", null, subtree, MatchMode.EXACT, null, includeUnpublished);
603 assertEquals(1, numberOfTaxa);
604 numberOfTaxa = taxonDao.countTaxaByName(false, true, false, false, false,"A", null, subtree, MatchMode.BEGINNING, null, includeUnpublished);
605 assertEquals(2, numberOfTaxa);
606 numberOfTaxa = taxonDao.countTaxaByName(true, true, false, false, false,"A", null, subtree, MatchMode.BEGINNING, null, includeUnpublished);
607 assertEquals(7, numberOfTaxa);
608 numberOfTaxa = taxonDao.countTaxaByName(true, true, false, false,false, "Aasfwerfwf fffe", null, subtree, MatchMode.BEGINNING, null, includeUnpublished);
609 assertEquals(0, numberOfTaxa);
610 // FIXME implement test for search in specific classification
611 // Reference reference = referenceDao.findByUuid(UUID.fromString("596b1325-be50-4b0a-9aa2-3ecd610215f2"));
612 // numberOfTaxa = taxonDao.countTaxaByName("A*", MatchMode.BEGINNING, SelectMode.ALL, null, null);
613 // assertEquals(numberOfTaxa, 2);
614 }
615
616 @Test
617 @DataSet
618 public void testRelatedTaxa() {
619 Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
620 assert taxon != null : "taxon must exist";
621
622 List<String> propertyPaths = new ArrayList<>();
623 propertyPaths.add("fromTaxon");
624 propertyPaths.add("fromTaxon.name");
625 List<OrderHint> orderHints = new ArrayList<>();
626 orderHints.add(new OrderHint("relatedFrom.name.genusOrUninomial", SortOrder.ASCENDING));
627 orderHints.add(new OrderHint("relatedFrom.name.specificEpithet", SortOrder.ASCENDING));
628 orderHints.add(new OrderHint("relatedFrom.name.infraSpecificEpithet", SortOrder.ASCENDING));
629
630 long count = taxonDao.countTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
631 includeUnpublished, TaxonRelationship.Direction.relatedTo);
632 assertEquals("Count should return 8 (related taxa)", 8, count);
633
634 List<TaxonRelationship> relatedTaxa = taxonDao.getTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
635 includeUnpublished, null, null, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
636 assertNotNull("getRelatedTaxa should return a List",relatedTaxa);
637 assertEquals("getRelatedTaxa should return all 8 related taxa", 8, relatedTaxa.size());
638 assertTrue("getRelatedTaxa should return TaxonRelationship objects with the relatedFrom taxon initialized",Hibernate.isInitialized(relatedTaxa.get(0).getFromTaxon()));
639 assertTrue("getRelatedTaxa should return TaxonRelationship objects with the relatedFrom taxon initialized",Hibernate.isInitialized(relatedTaxa.get(0).getFromTaxon().getName()));
640
641 assertEquals("Acherontia should appear first in the list of related taxa", relatedTaxa.get(0).getFromTaxon().getTitleCache(), "Acherontia Laspeyres, 1809 sec. cate-sphingidae.org");
642 assertEquals("Sphingonaepiopsis should appear last in the list of related taxa", "Sphingonaepiopsis Wallengren, 1858 sec. cate-sphingidae.org", relatedTaxa.get(relatedTaxa.size()-1).getFromTaxon().getTitleCache());
643 }
644
645 @Test
646 @DataSet
647 public void testGetRelatedTaxaPaged() {
648 Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
649 assert taxon != null : "taxon must exist";
650
651 List<String> propertyPaths = new ArrayList<>();
652 propertyPaths.add("fromTaxon");
653 propertyPaths.add("fromTaxon.name");
654
655 List<OrderHint> orderHints = new ArrayList<>();
656 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
657
658 int pageSize = 3;
659 List<TaxonRelationship> firstPage = taxonDao.getTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
660 includeUnpublished, pageSize, 0, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
661 List<TaxonRelationship> secondPage = taxonDao.getTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
662 includeUnpublished, pageSize, 1, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
663 List<TaxonRelationship> thirdPage = taxonDao.getTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
664 includeUnpublished, pageSize, 2, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
665
666 assertNotNull("getRelatedTaxa: 3, 0 should return a List",firstPage);
667 assertEquals("getRelatedTaxa: 3, 0 should return a List with 3 elements", pageSize,firstPage.size());
668 assertNotNull("getRelatedTaxa: 3, 1 should return a List",secondPage);
669 assertEquals("getRelatedTaxa: 3, 1 should return a List with 3 elements", pageSize, secondPage.size());
670 assertNotNull("getRelatedTaxa: 3, 2 should return a List",thirdPage);
671 assertEquals("getRelatedTaxa: 3, 2 should return a List with 2 elements", 2, thirdPage.size());
672 }
673
674 @Test
675 @DataSet
676 public void testCountSynonyms() {
677 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
678 assert taxon != null : "taxon must exist";
679
680 long numberOfRelatedSynonym = taxonDao.countSynonyms(taxon,null);
681 assertEquals("countSynonyms should return 3", 3, numberOfRelatedSynonym);
682 }
683
684 @Test
685 @DataSet
686 public void testGetSynonyms() {
687 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
688 assert taxon != null : "taxon must exist";
689 List<String> propertyPaths = new ArrayList<String>();
690 propertyPaths.add("synonym");
691 propertyPaths.add("synonym.name");
692
693 List<OrderHint> orderHints = new ArrayList<>();
694 orderHints.add(new OrderHint("titleCache", SortOrder.ASCENDING));
695
696 List<Synonym> synonyms = taxonDao.getSynonyms(taxon, null, null, null,orderHints,propertyPaths);
697
698 assertNotNull("getSynonyms should return a List", synonyms);
699 assertEquals("getSynonyms should return 3 synonyms", 3, synonyms.size());
700 assertTrue("getSynonyms should return synonym objects with the synonym initialized", Hibernate.isInitialized(synonyms.get(0)));
701 }
702
703 @Test
704 @DataSet
705 public void testCountSynonymsByType() {
706 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
707 assert taxon != null : "taxon must exist";
708
709 long numberOfTaxonomicSynonyms = taxonDao.countSynonyms(taxon, SynonymType.HETEROTYPIC_SYNONYM_OF());
710 assertEquals("countSynonyms should return 4", 3, numberOfTaxonomicSynonyms);
711 }
712
713 @Test
714 @DataSet
715 public void testSynonymsByType() {
716 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
717 assert taxon != null : "taxon must exist";
718
719 List<Synonym> synonyms = taxonDao.getSynonyms(taxon, SynonymType.HETEROTYPIC_SYNONYM_OF(), null, null,null,null);
720
721 assertNotNull("getSynonyms should return a List", synonyms);
722 assertEquals("getSynonyms should return 4 Synonyms", 3, synonyms.size());
723 }
724
725 @Test
726 @DataSet
727 public void testPageSynonyms(){
728 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
729 assert taxon != null : "taxon must exist";
730
731 int pageSize = 2;
732 List<Synonym> firstPage = taxonDao.getSynonyms(taxon, null, pageSize, 0,null,null);
733 List<Synonym> secondPage = taxonDao.getSynonyms(taxon, null, pageSize, 1,null,null);
734
735 assertNotNull("getSynonyms: 2, 0 should return a List",firstPage);
736 assertEquals("getSynonyms: 2, 0 should return 2 synonyms", pageSize,firstPage.size());
737 assertNotNull("getSynonyms: 2, 1 should return a List",secondPage);
738 assertEquals("getSynonyms: 2, 1 should return 1 synonym", 1, secondPage.size());
739 }
740
741 @Test
742 @DataSet
743 public void testCountTaxonRelationships() {
744 long count = taxonDao.countTaxonRelationships(null);
745 assertEquals("There should be 11 relationships", 11, count);
746
747 Set<TaxonRelationshipType> types = new HashSet<>();
748 count = taxonDao.countTaxonRelationships(types);
749 assertEquals("Empty filter should return empty result", 0, count);
750
751 types.add(TaxonRelationshipType.CONGRUENT_TO());
752 count = taxonDao.countTaxonRelationships(types);
753 assertEquals("There should be no congruent relationship", 0, count);
754
755 types.add(TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN());
756 count = taxonDao.countTaxonRelationships(types);
757 assertEquals("There should be 11 tax included relationships", 11, count);
758 }
759
760 @Test
761 @DataSet
762 public void testlistTaxonRelationships() {
763 List<TaxonRelationship> rels = taxonDao.getTaxonRelationships(null, null, null, null, null);
764 assertEquals("There should be 11 relationships", 11, rels.size());
765
766 rels = taxonDao.getTaxonRelationships(null, 2, 3, null, null);
767 assertEquals("There should be 11 relationships", 2, rels.size());
768
769 Set<TaxonRelationshipType> types = new HashSet<>();
770 rels = taxonDao.getTaxonRelationships(types, null, null, null, null);
771 assertEquals("Empty filter should return empty result", 0, rels.size());
772
773 types.add(TaxonRelationshipType.CONGRUENT_TO());
774 rels = taxonDao.getTaxonRelationships(types, null, null, null, null);
775 assertEquals("There should be no congruent relationship", 0, rels.size());
776
777 types.add(TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN());
778 rels = taxonDao.getTaxonRelationships(types, null, null, null, null);
779 assertEquals("There should be 11 tax included relationships", 11, rels.size());
780 }
781
782 @Test
783 @DataSet("TaxonNodeDaoHibernateImplTest.xml")
784 public void testListAcceptedTaxaFor() {
785 UUID acheontitia_ciprosus = UUID.fromString("3ef145f7-bd92-4a64-8afd-2b8203e00e02");
786
787 Synonym synonym = (Synonym)taxonDao.findByUuid(acheontitia_ciprosus);
788 assertNotNull("synonym must exist", synonym);
789
790 Taxon taxon = taxonDao.acceptedTaxonFor(synonym, null, null);
791 assertNotNull("listAcceptedTaxaFor should return a taxon", taxon);
792
793 Classification classification = classificationDao.load(classificationUuid);
794 assertNotNull("classification must exist", classification);
795
796 taxon = taxonDao.acceptedTaxonFor(synonym, classification, null);
797 assertNull("listAcceptedTaxaFor should return not taxon due to classification filter", taxon);
798 }
799
800
801 @Test
802 @DataSet
803 public void testGetTaxonMatchingUninomial() {
804 @SuppressWarnings("rawtypes")
805 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class, "Smerinthus", "*", "*", "*","*",null,null,null);
806
807 assertNotNull("findTaxaByName should return a List", result);
808 assertEquals("findTaxaByName should return two Taxa",2,result.size());
809 assertEquals("findTaxaByName should return a Taxon with id 5",5,result.get(0).getId());
810 }
811
812 @Test
813 @DataSet
814 public void testGetTaxonMatchingSpeciesBinomial() {
815 @SuppressWarnings("rawtypes")
816 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class, "Smerinthus", null, "kindermannii", null,"*",null,null,null);
817
818 assertNotNull("findTaxaByName should return a List", result);
819 assertEquals("findTaxaByName should return one Taxon",1,result.size());
820 assertEquals("findTaxaByName should return a Taxon with id 8",8,result.get(0).getId());
821 }
822
823 @Test
824 @DataSet
825 public void testGetTaxonMatchingTrinomial() {
826 @SuppressWarnings("rawtypes")
827 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class,"Cryptocoryne", null,"purpurea","borneoensis","*",null,null,null);
828
829 assertNotNull("findTaxaByName should return a List", result);
830 assertEquals("findTaxaByName should return one Taxon",1,result.size());
831 assertEquals("findTaxaByName should return a Taxon with id 38",38,result.get(0).getId());
832 }
833
834 @Test
835 @DataSet
836 public void testNegativeMatch() {
837 @SuppressWarnings("rawtypes")
838 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class,"Acherontia", null,"atropos","dehli",null,null,null,null);
839
840 assertNotNull("findTaxaByName should return a List", result);
841 assertTrue("findTaxaByName should return an empty List",result.isEmpty());
842 }
843
844 @Test
845 @DataSet
846 public void testCountAllTaxa() {
847 long numberOfTaxa = taxonDao.count(Taxon.class);
848 assertEquals("count should return 14 taxa", 14, numberOfTaxa);
849 }
850
851 @Test
852 @DataSet
853 public void testListAllTaxa() {
854 List<Taxon> taxa = taxonDao.list(Taxon.class,100, 0);
855 assertNotNull("list should return a List", taxa);
856 assertEquals("list should return 14 taxa", 14, taxa.size());
857 }
858
859 @Test
860 @DataSet
861 // @ExpectedDataSet
862 public void testDelete() {
863 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
864 assert taxon != null : "taxon must exist";
865 taxonDao.delete(taxon);
866 taxon = (Taxon)taxonDao.findByUuid(acherontia);
867 assert taxon == null : "taxon must not exist";
868 setComplete();
869 endTransaction();
870 // try {
871 // printDataSet(new FileOutputStream("test.xml"), TABLE_NAMES);
872 // } catch (FileNotFoundException e) {
873 // e.printStackTrace();
874 // }
875 }
876
877 @Test
878 @DataSet
879 @ExpectedDataSet("TaxonDaoHibernateImplTest.testDelete-result.xml")
880 public void testDeleteWithMarker() {
881 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
882 taxon.addMarker(Marker.NewInstance(MarkerType.IS_DOUBTFUL(), true));
883 taxonDao.save(taxon);
884 assert taxon != null : "taxon must exist";
885
886 taxonDao.delete(taxon);
887 commitAndStartNewTransaction(null);
888 taxon = (Taxon)taxonDao.findByUuid(acherontia);
889 assert taxon == null : "taxon must not exist";
890 setComplete();
891 endTransaction();
892 // try {
893 // printDataSet(new FileOutputStream("test.xml"), TABLE_NAMES);
894 // } catch (FileNotFoundException e) {
895 // e.printStackTrace();
896 // }
897 }
898
899 @Test
900 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
901 public void testFindDeleted() {
902 TaxonBase<?> taxon = taxonDao.findByUuid(acherontia);
903 assertNull("findByUuid should return null in this view", taxon);
904 assertFalse("exist should return false in this view",taxonDao.exists(acherontia));
905 }
906
907 @Test
908 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
909 public void testFindDeletedInPreviousView() {
910 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
911 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
912 assertNotNull("findByUuid should return a taxon in this view",taxon);
913 assertTrue("exists should return true in this view", taxonDao.exists(acherontia));
914
915 try{
916 assertEquals("There should be 3 relations to this taxon in this view",3,taxon.getRelationsToThisTaxon().size());
917 } catch(Exception e) {
918 fail("We should not experience any problems initializing proxies with envers");
919 }
920 }
921
922 @Test
923 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
924 public void testGetAuditEvents() {
925 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
926 assert taxon != null : "taxon cannot be null";
927
928 List<String> propertyPaths = new ArrayList<>();
929 propertyPaths.add("name");
930 propertyPaths.add("createdBy");
931 propertyPaths.add("updatedBy");
932
933 @SuppressWarnings("rawtypes")
934 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(taxon, null,null,null,propertyPaths);
935 assertNotNull("getAuditEvents should return a list",auditEvents);
936 assertFalse("the list should not be empty",auditEvents.isEmpty());
937 assertEquals("There should be two AuditEventRecords in the list",2, auditEvents.size());
938 }
939
940 @Test
941 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
942 public void testGetAuditEventsFromNow() {
943 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
944 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
945 assert taxon != null : "taxon cannot be null";
946
947 @SuppressWarnings("rawtypes")
948 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(taxon, null,null,AuditEventSort.FORWARDS,null);
949 assertNotNull("getAuditEvents should return a list",auditEvents);
950 assertFalse("the list should not be empty",auditEvents.isEmpty());
951 assertEquals("There should be one audit event in the list",1,auditEvents.size());
952 }
953
954 @Test
955 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
956 public void testCountAuditEvents() {
957 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
958 assert taxon != null : "taxon cannot be null";
959
960 long numberOfAuditEvents = taxonDao.countAuditEvents(taxon, null);
961 assertEquals("countAuditEvents should return 2",numberOfAuditEvents,2);
962 }
963
964 @Test
965 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
966 public void getPreviousAuditEvent() {
967 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
968 assert taxon != null : "taxon cannot be null";
969
970 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getPreviousAuditEvent(taxon);
971 assertNotNull("getPreviousAuditEvent should not return null as there is at least one audit event prior to the current one",auditEvent);
972 }
973
974 @Test
975 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
976 public void getPreviousAuditEventAtBeginning() {
977 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
978 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
979 assert taxon != null : "taxon cannot be null";
980
981 @SuppressWarnings("rawtypes")
982 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getPreviousAuditEvent(taxon);
983 assertNull("getPreviousAuditEvent should return null if we're at the first audit event anyway",auditEvent);
984 }
985
986 @Test
987 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
988 public void getNextAuditEvent() {
989 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
990 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
991 assert taxon != null : "taxon cannot be null";
992
993 @SuppressWarnings("rawtypes")
994 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getNextAuditEvent(taxon);
995 assertNotNull("getNextAuditEvent should not return null as there is at least one audit event after the current one",auditEvent);
996 }
997
998 @Test
999 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
1000 public void getNextAuditEventAtEnd() {
1001 AuditEventContextHolder.getContext().setAuditEvent(mostRecentAuditEvent);
1002 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
1003 assert taxon != null : "taxon cannot be null";
1004
1005 @SuppressWarnings("rawtypes")
1006 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getNextAuditEvent(taxon);
1007 assertNull("getNextAuditEvent should return null as there no more audit events after the current one",auditEvent);
1008 }
1009
1010 @Test
1011 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1012 public void testFind() {
1013 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
1014 assert taxon != null : "taxon cannot be null";
1015
1016 assertEquals("getRelationsToThisTaxon should contain 1 TaxonRelationship in this view",1,taxon.getRelationsToThisTaxon().size());
1017 }
1018
1019 @Test
1020 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1021 public void testFindInPreviousView() {
1022 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
1023 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
1024 assert taxon != null : "taxon cannot be null";
1025
1026 assertTrue("getRelationsToThisTaxon should contain 0 TaxonRelationship in this view",taxon.getRelationsToThisTaxon().isEmpty());
1027 }
1028
1029 @Test
1030 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1031 public void testGetRelations() {
1032 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
1033 assert taxon != null : "taxon cannot be null";
1034
1035 List<String> propertyPaths = new ArrayList<>();
1036 propertyPaths.add("fromTaxon");
1037 propertyPaths.add("fromTaxon.name");
1038
1039 List<OrderHint> orderHints = new ArrayList<>();
1040 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
1041
1042 List<TaxonRelationship> taxonRelations = taxonDao.getTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
1043 includeUnpublished, null, null,orderHints,propertyPaths, TaxonRelationship.Direction.relatedFrom);
1044 assertNotNull("getRelatedTaxa should return a list", taxonRelations);
1045 assertEquals("there should be one TaxonRelationship in the list in the current view",1,taxonRelations.size());
1046 assertTrue("TaxonRelationship.relatedFrom should be initialized",Hibernate.isInitialized(taxonRelations.get(0).getFromTaxon()));
1047 assertTrue("TaxonRelationship.relatedFrom.name should be initialized",Hibernate.isInitialized(taxonRelations.get(0).getFromTaxon().getName()));
1048 }
1049
1050 @Test
1051 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1052 public void testCountRelations() {
1053 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
1054 assert taxon != null : "taxon cannot be null";
1055 assertEquals("countRelatedTaxa should return 1 in the current view",1, taxonDao.countTaxonRelationships(
1056 taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
1057 includeUnpublished, TaxonRelationship.Direction.relatedTo));
1058 }
1059
1060 @Test
1061 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1062 public void testGetRelationsInPreviousView() {
1063 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
1064 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
1065 assert taxon != null : "taxon cannot be null";
1066
1067 List<String> propertyPaths = new ArrayList<String>();
1068 propertyPaths.add("relatedFrom");
1069 propertyPaths.add("relatedFrom.name");
1070
1071 List<OrderHint> orderHints = new ArrayList<>();
1072 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
1073
1074 List<TaxonRelationship> taxonRelations = taxonDao.getTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
1075 includeUnpublished, null, null, orderHints,propertyPaths, TaxonRelationship.Direction.relatedFrom);
1076 assertNotNull("getRelatedTaxa should return a list",taxonRelations);
1077 assertTrue("there should be no TaxonRelationships in the list in the prior view", taxonRelations.isEmpty());
1078 }
1079
1080 @Test
1081 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1082 public void testCountRelationsInPreviousView() {
1083 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
1084 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
1085 assert taxon != null : "taxon cannot be null";
1086 assertEquals("countRelatedTaxa should return 0 in the current view",0,
1087 taxonDao.countTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
1088 includeUnpublished, TaxonRelationship.Direction.relatedTo));
1089 }
1090
1091 @Test
1092 @DataSet
1093 public void testGroupTaxa() {
1094 List<Grouping> groups = new ArrayList<>();
1095 groups.add(new GroupByCount("count",SortOrder.DESCENDING));
1096 groups.add(new Grouping("name.genusOrUninomial", "genus", "n", SortOrder.ASCENDING));
1097 List<Object[]> results = taxonDao.group(null, null, null, groups,null);
1098 if(logger.isTraceEnabled()){
1099 System.out.println("count\tname.genuOrUninomial");
1100 for(Object[] result : results) {
1101 System.out.println(result[0] + "\t" + result[1]);
1102 }
1103 }
1104 }
1105
1106 @Test
1107 @DataSet
1108 public void testGroupTaxaByClass() {
1109 List<Grouping> groups = new ArrayList<>();
1110 groups.add(new GroupByCount("count",SortOrder.DESCENDING));
1111 groups.add(new Grouping("class", "class",null, SortOrder.ASCENDING));
1112 List<Object[]> results = taxonDao.group(null, null, null, groups,null);
1113 if(logger.isTraceEnabled()){
1114 System.out.println("count\tclass");
1115 for(Object[] result : results) {
1116 System.out.println(result[0] + "\t" + result[1]);
1117 }
1118 }
1119 }
1120
1121 @Test
1122 @DataSet
1123 public void testNativeSQLOrder() {
1124 List<OrderHint> orderHints = new ArrayList<>();
1125 orderHints.add(new NativeSqlOrderHint("case when {alias}.titleCache like 'C%' then 0 else 1 end",SortOrder.ASCENDING));
1126
1127 @SuppressWarnings("rawtypes")
1128 List<TaxonBase> results = taxonDao.list(null, null, orderHints);
1129 if(logger.isTraceEnabled()){
1130 System.out.println("native SQL order");
1131 for(TaxonBase<?> result : results) {
1132 System.out.println(result.getTitleCache());
1133 }
1134 }
1135 }
1136
1137 @Test
1138 @DataSet
1139 public void testGroupByDateTaxa() {
1140 List<Grouping> groups = new ArrayList<>();
1141 groups.add(new GroupByCount("count",null));
1142 groups.add(new GroupByDate("created", "dateGroup", SortOrder.ASCENDING, GroupByDate.Resolution.MONTH));
1143 List<Object[]> results = taxonDao.group(null, null, null, groups,null);
1144 if(logger.isTraceEnabled()){
1145 System.out.println("count\tyear\tmonth");
1146 for(Object[] result : results) {
1147 System.out.println(result[0] + "\t" + result[1] + "\t" + result[2]);
1148 }
1149 }
1150 }
1151
1152
1153
1154
1155 @Test
1156 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
1157 //NOTE: There is a problem with loading AuditEvents if this test runs
1158 //stand alone or as first (one of the first) in the test suite. For some reason
1159 //the AuditEvent records from the @DataSet are not inserted into the database then,
1160 //while those inserted by the terms dataset are inserted as well as a completely new one.
1161 //This problem for some reason does not happen if not running at first place
1162 public void testGetAuditEventsByTypeWithRestrictions() {
1163 commitAndStartNewTransaction(new String[]{"AUDITEVENT", "TAXONBASE_AUD"});
1164
1165 List<String> propertyPaths = new ArrayList<>();
1166 propertyPaths.add("name");
1167 propertyPaths.add("createdBy");
1168 propertyPaths.add("updatedBy");
1169
1170 List<AuditCriterion> criteria = new ArrayList<>();
1171 criteria.add(AuditEntity.property("lsid_lsid").isNotNull());
1172
1173 long count = taxonDao.countAuditEvents(TaxonBase.class, null, null, null);
1174
1175 @SuppressWarnings("rawtypes")
1176 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(TaxonBase.class, previousAuditEvent, mostRecentAuditEvent, criteria,null, null, AuditEventSort.FORWARDS, propertyPaths);
1177 assertNotNull("getAuditEvents should return a list",auditEvents);
1178 assertFalse("the list should not be empty",auditEvents.isEmpty());
1179 assertEquals("There should be one AuditEventRecord in the list",1, auditEvents.size());
1180 }
1181
1182
1183 @Test
1184 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
1185 // @DataSets({ //for testing only
1186 // @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml"),
1187 // @DataSet("TaxonDaoHibernateImplTest.testFindDeletedAuditEvents.xml")
1188 // })
1189 //NOTE: There is a problem with loading AuditEvents if this test runs
1190 //stand alone or as first (one of the first) in the test suite. For some reason
1191 //the AuditEvent records from the @DataSet are not inserted into the database then,
1192 //while those inserted by the terms dataset are inserted as well as a completely new one.
1193 //This problem for some reason does not happen if not running at first place
1194 public void testGetAuditEventsByTypeWithNoRestrictions() {
1195 // printDataSet(System.out, new String[]{"AUDITEVENT", "TAXONBASE_AUD"});
1196 commitAndStartNewTransaction(new String[]{"AUDITEVENT", "TAXONBASE_AUD"});
1197
1198 List<String> propertyPaths = new ArrayList<>();
1199 propertyPaths.add("name");
1200 propertyPaths.add("createdBy");
1201 propertyPaths.add("updatedBy");
1202 long count = taxonDao.countAuditEvents(TaxonBase.class, null, null, null);
1203 @SuppressWarnings("rawtypes")
1204 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(TaxonBase.class, previousAuditEvent, mostRecentAuditEvent, null,null, null, AuditEventSort.FORWARDS, propertyPaths);
1205 assertNotNull("getAuditEvents should return a list", auditEvents);
1206 assertFalse("the list should not be empty", auditEvents.isEmpty());
1207 assertEquals("There should be thirty eight AuditEventRecords in the list", 2, auditEvents.size());
1208 }
1209
1210
1211 @Test
1212 @DataSet("TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
1213 public void testGetCommonName(){
1214 List<Taxon> commonNameResults = taxonDao.getTaxaByCommonName("common%", null,
1215 MatchMode.BEGINNING, null, null, null, null);
1216
1217 assertNotNull("getTaxaByCommonName should return a list", commonNameResults);
1218 assertFalse("the list should not be empty", commonNameResults.isEmpty());
1219 assertEquals("There should be two taxa with common name", 2,commonNameResults.size());
1220
1221 List<UuidAndTitleCache<Taxon>> list = taxonDao.getTaxaByCommonNameForEditor(
1222 "common%", null, MatchMode.BEGINNING, null);
1223
1224 assertNotNull("getTaxaByCommonName should return a list", list);
1225 assertFalse("the list should not be empty", list.isEmpty());
1226 assertEquals("There should be two Taxon with common name", 2,list.size());
1227
1228 }
1229
1230 @Test
1231 @DataSet
1232 public void testGetTitleCache(){
1233 UUID uuid = UUID.fromString("7b8b5cb3-37ba-4dba-91ac-4c6ffd6ac331");
1234 String titleCache = taxonDao.getTitleCache(uuid, false);
1235 Assert.assertEquals("Acherontia styx Westwood, 1847 sec. cate-sphingidae.org", titleCache);
1236 titleCache = taxonDao.getTitleCache(uuid, true);
1237 Assert.assertEquals("Acherontia styxx Westwood, 1847 sec. cate-sphingidae.org", titleCache);
1238 }
1239
1240
1241 @Test
1242 @DataSet("TaxonDaoHibernateImplTest.testPropertyPath.xml")
1243 public void testPropertyPath(){
1244 //Test that BeanInitializer also works on HiberanteProxys
1245 Classification c = classificationDao.load(UUID.fromString("4bceea53-893f-4685-8c63-6dcec6e85ab1"));
1246 TaxonNode singleNode = c.getRootNode().getChildNodes().iterator().next();
1247 Taxon taxonProxy = singleNode.getTaxon();
1248 Assert.assertTrue("Object to test should be a proxy ", taxonProxy instanceof HibernateProxy);
1249
1250 List<String> propertyPaths = new ArrayList<>();
1251 propertyPaths.add("taxonNodes");
1252 Taxon taxon = (Taxon)this.taxonDao.load(
1253 UUID.fromString("4a5bc930-844f-45ec-aea8-dd155e1ab25f"),
1254 propertyPaths);
1255 Assert.assertSame("Returned object should be the same proxy to assure that we ran initialization on this proxy", taxonProxy, taxon);
1256 }
1257
1258
1259 /**
1260 * {@inheritDoc}
1261 */
1262 @Override
1263 // @Test
1264 public void createTestDataSet() throws FileNotFoundException {
1265 // Classification classification = Classification.NewInstance("Test");
1266 // BotanicalName TaxonName = null;
1267 // Reference sec = null;
1268 // Taxon taxon = Taxon.NewInstance(taxonName, sec);
1269 // classification.addChildTaxon(taxon, sec, null);
1270 //
1271 // classificationDao.save(classification);
1272 // this.commitAndStartNewTransaction(null);
1273 //
1274 // writeDbUnitDataSetFile(new String[] {
1275 // "CLASSIFICATION", "TAXONNAME",
1276 // "REFERENCE","TAXONNODE",
1277 // "TAXONBASE","LANGUAGESTRING",
1278 // "HIBERNATE_SEQUENCES" // IMPORTANT!!!
1279 // },
1280 // "testPropertyPath" );
1281 }
1282
1283 }