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