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