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