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