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