Merge remote-tracking branch 'origin/develop' into develop
[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, "R*", classification, MatchMode.BEGINNING, 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, "R*", null, MatchMode.BEGINNING, 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, "A*", null, MatchMode.BEGINNING, 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, "R*", classification, MatchMode.BEGINNING, 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, "R*", null, MatchMode.BEGINNING, 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, "*", classification, MatchMode.BEGINNING, 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, "R*", null, MatchMode.BEGINNING, 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, "*", null, MatchMode.BEGINNING, null, null, null, null);
251 Assert.assertEquals("There should be 3 Taxa", 3, results.size());
252
253 }
254
255 /**
256 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)}.
257 */
258 @Test
259 @DataSet
260 public void testGetTaxaByNameForEditor() {
261 Reference sec = referenceDao.findById(1);
262 assert sec != null : "sec must exist";
263
264 List<UuidAndTitleCache<IdentifiableEntity>> results = taxonDao.getTaxaByNameForEditor(true, true, false,false,"Acher", null, MatchMode.BEGINNING, null);
265 assertNotNull("getTaxaByName should return a List", results);
266 assertFalse("The list should not be empty", results.isEmpty());
267 assertEquals(4, results.size());
268
269
270 results = taxonDao.getTaxaByNameForEditor(true, true, false, false,"A",null, MatchMode.BEGINNING, null);
271 assertNotNull("getTaxaByName should return a List", results);
272 assertEquals(7, results.size());
273
274
275 results = taxonDao.getTaxaByNameForEditor(true, false,false, false,"A", null,MatchMode.BEGINNING, null);
276 assertNotNull("getTaxaByName should return a List", results);
277 assertEquals(5, results.size());
278 assertEquals(results.get(0).getType(), Taxon.class);
279
280 results = taxonDao.getTaxaByNameForEditor(false, true,false,false, "A", null,MatchMode.BEGINNING, null);
281 assertNotNull("getTaxaByName should return a List", results);
282 assertEquals(2, results.size());
283 assertEquals(results.get(0).getType(), Synonym.class);
284
285 results = taxonDao.getTaxaByNameForEditor(true, true,false,false,"Aus", null,MatchMode.EXACT, null);
286 assertNotNull("getTaxaByName should return a List", results);
287 assertEquals("Results list should contain one entity",1,results.size());
288
289 results = taxonDao.getTaxaByNameForEditor(true, true,true,false,"A", null,MatchMode.BEGINNING, null);
290 assertNotNull("getTaxaByName should return a List", results);
291 assertEquals("Results list should contain one entity", 8, results.size());
292
293 //TODO: test the search for misapplied names
294
295 }
296
297
298 /**
299 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)}
300 * restricting the search by a set of Areas.
301 */
302 @Test
303 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
304 public void testGetTaxaByNameAndArea() {
305
306 Set<NamedArea> namedAreas = new HashSet<NamedArea>();
307 namedAreas.add((NamedArea)definedTermDao.load(northernAmericaUuid));
308 //namedAreas.add((NamedArea)definedTermDao.load(southernAmericaUuid));
309 //namedAreas.add((NamedArea)definedTermDao.load(antarcticaUuid));
310
311 Classification taxonmicTree = classificationDao.findByUuid(classificationUuid);
312
313 // prepare some synonym relation ships for some tests
314 Synonym synAtroposAgassiz = (Synonym)taxonDao.findByUuid(atroposAgassiz);
315 Taxon taxonRethera = (Taxon)taxonDao.findByUuid(rethera);
316 taxonRethera.addSynonym(synAtroposAgassiz, SynonymType.SYNONYM_OF());
317 logger.warn("addSynonym(..)");
318
319 Synonym synAtroposLeach = (Synonym)taxonDao.findByUuid(atroposLeach);
320 Taxon taxonRetheraSecCdmtest = (Taxon)taxonDao.findByUuid(retheraSecCdmtest);
321 taxonRetheraSecCdmtest.addSynonym(synAtroposLeach, SynonymType.SYNONYM_OF());
322 this.taxonDao.save(taxonRetheraSecCdmtest);
323
324 Taxon test = (Taxon)this.taxonDao.findByUuid(retheraSecCdmtest);
325 // Set<Synonym> synonyms3 = test.getSynonyms();
326 // 1. searching for a taxon (Rethera)
327 //long numberOfTaxa = taxonDao.countTaxaByName(Taxon.class, "Rethera", null, MatchMode.BEGINNING, namedAreas);
328
329 List<TaxonBase> results = taxonDao.getTaxaByName(true,false, false, "Rethera", null, MatchMode.BEGINNING, namedAreas,
330 null, null, null);
331 assertNotNull("getTaxaByName should return a List", results);
332 assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
333
334 // 2. searching for a taxon (Rethera) contained in a specific classification
335 results = taxonDao.getTaxaByName(true, false, false, "Rethera", taxonmicTree, MatchMode.BEGINNING, namedAreas,
336 null, null, null);
337 assertNotNull("getTaxaByName should return a List", results);
338 assertTrue("expected to find one taxon but found "+results.size(), results.size() == 1);
339
340
341 // 3. searching for Synonyms
342 results = taxonDao.getTaxaByName(false, true, false, "Atropo", null, MatchMode.ANYWHERE, null,
343 null, null, null);
344 assertNotNull("getTaxaByName should return a List", results);
345 /*System.err.println(results.get(0).getTitleCache() + " - " +results.get(1).getTitleCache() + " - " +results.get(2).getTitleCache() );
346
347
348 System.err.println(((Synonym)results.get(0)).getAcceptedTaxa().contains(taxonRethera)+ " - " +((Synonym)results.get(1)).getAcceptedTaxa().contains(taxonRethera)+ " - " +((Synonym)results.get(2)).getAcceptedTaxa().contains(taxonRethera)+ " - " );
349 */
350 assertTrue("expected to find three taxa but found "+results.size(), results.size() == 3);
351
352 // 4. searching for Synonyms
353 results = taxonDao.getTaxaByName(false, true, false, "Atropo", null, MatchMode.BEGINNING, null,
354 null, null, null);
355 assertNotNull("getTaxaByName should return a List", results);
356 assertTrue("expected to find three taxa but found "+results.size(), results.size() == 3);
357
358
359 // 5. searching for a Synonyms and Taxa
360 results = taxonDao.getTaxaByName(true, true, false, "A", null, MatchMode.BEGINNING, namedAreas,
361 null, null, null);
362 //only five taxa have a distribution
363 assertNotNull("getTaxaByName should return a List", results);
364 assertTrue("expected to find 8 taxa but found "+results.size(), results.size() == 8);
365 }
366
367
368 /**
369 * 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)}
370 * restricting the search by a set of Areas.
371 */
372 @Test
373 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
374 public void testFindByNameTitleCache() {
375
376 Set<NamedArea> namedAreas = new HashSet<NamedArea>();
377 namedAreas.add((NamedArea)definedTermDao.load(northernAmericaUuid));
378 //namedAreas.add((NamedArea)definedTermDao.load(southernAmericaUuid));
379 //namedAreas.add((NamedArea)definedTermDao.load(antarcticaUuid));
380
381 Classification classification = classificationDao.findByUuid(classificationUuid);
382
383 // prepare some synonym relation ships for some tests
384 Synonym synAtroposAgassiz = (Synonym)taxonDao.findByUuid(atroposAgassiz);
385 Taxon taxonRethera = (Taxon)taxonDao.findByUuid(rethera);
386 taxonRethera.addSynonym(synAtroposAgassiz, SynonymType.SYNONYM_OF());
387 logger.warn("addSynonym(..)");
388 this.taxonDao.clear();
389 Synonym synAtroposLeach = (Synonym)taxonDao.findByUuid(atroposLeach);
390 Taxon taxonRetheraSecCdmtest = (Taxon)taxonDao.findByUuid(retheraSecCdmtest);
391 taxonRetheraSecCdmtest.addSynonym(synAtroposLeach, SynonymType.SYNONYM_OF());
392 this.taxonDao.clear();
393 // 1. searching for a taxon (Rethera)
394 //long numberOfTaxa = taxonDao.countTaxaByName(Taxon.class, "Rethera", null, MatchMode.BEGINNING, namedAreas);
395
396 List<TaxonBase> results = taxonDao.findByNameTitleCache(true, false, "Rethera Rothschild & Jordan, 1903", null, MatchMode.EXACT, namedAreas,
397 null, null, null);
398 assertNotNull("getTaxaByName should return a List", results);
399 assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
400
401 // 2. searching for a taxon (Rethera) contained in a specific classification
402 results = taxonDao.findByNameTitleCache(true, false, "Rethera Rothschild & Jordan, 1903", classification, MatchMode.EXACT, namedAreas,
403 null, null, null);
404 assertNotNull("getTaxaByName should return a List", results);
405 assertTrue("expected to find one taxon but found "+results.size(), results.size() == 1);
406
407
408 // 3. searching for Synonyms
409 results = taxonDao.findByNameTitleCache(false, true, "*Atropo", null, MatchMode.ANYWHERE, null,
410 null, null, null);
411 assertNotNull("getTaxaByName should return a List", results);
412
413 assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
414
415 // 4. searching for Synonyms
416 results = taxonDao.findByNameTitleCache(false, true, "Atropo", null, MatchMode.BEGINNING, null,
417 null, null, null);
418 assertNotNull("getTaxaByName should return a List", results);
419 assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
420
421
422 // 5. searching for a Synonyms and Taxa
423 // attache a synonym first
424 Synonym syn = (Synonym)taxonDao.findByUuid(this.atroposLeach);
425 Taxon tax = (Taxon) taxonDao.findByUuid(rethera);
426 tax.addSynonym(syn, SynonymType.HETEROTYPIC_SYNONYM_OF());
427
428 taxonDao.save(tax);
429 results = taxonDao.findByNameTitleCache(true, true, "A", null, MatchMode.BEGINNING, namedAreas,
430 null, null, null);
431 assertNotNull("getTaxaByName should return a List", results);
432 assertTrue("expected to find 8 taxa but found "+results.size(), results.size() == 8);
433 }
434
435 @Test
436 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
437 public void testTaxonNameInTwoClassifications(){
438 int numberOfClassifications = classificationDao.count();
439 List<String> propertyPaths = new ArrayList<String>();
440 propertyPaths.add("taxonNodes");
441 List<TaxonBase> taxa = taxonDao.getTaxaByName(true, true, false, "P", null, MatchMode.BEGINNING, null, null, null, null);
442 Taxon taxon = (Taxon)taxa.get(0);
443 Set<TaxonNode> nodes = taxon.getTaxonNodes();
444 assertTrue(nodes.size() == 1);
445 //assertNotNull(taxa);
446 //assertTrue(taxa.size() > 0);
447 }
448
449 @Test
450 @DataSet
451 public void testFindByUuid() {
452 Taxon taxon = (Taxon)taxonDao.findByUuid(uuid);
453 assertNotNull("findByUuid should return a taxon",taxon);
454 assertFalse("findByUuid should not return a taxon with it's name initialized",Hibernate.isInitialized(taxon.getName()));
455 }
456
457 @Test
458 @DataSet
459 public void testLoad() {
460 List<String> propertyPaths = new ArrayList<String>();
461 propertyPaths.add("name");
462 propertyPaths.add("sec");
463 Taxon taxon = (Taxon)taxonDao.load(uuid, propertyPaths);
464 assertNotNull("findByUuid should return a taxon",taxon);
465 assertTrue("load should return a taxon with it's name initialized, given that the property was specified in the method",Hibernate.isInitialized(taxon.getName()));
466 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()));
467 }
468
469 @Test
470 @DataSet
471 public void testCountTaxonRelationships() {
472 Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
473 assert taxon != null : "taxon must exist";
474
475 int numberOfRelatedTaxa = taxonDao.countTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), TaxonRelationship.Direction.relatedTo);
476 assertEquals("countTaxonRelationships should return 8", 8, numberOfRelatedTaxa);
477 }
478
479 @Test
480 @DataSet
481 public void testCountTaxaByName() {
482 long numberOfTaxa = taxonDao.countTaxaByName(true, false, false, "A", null, MatchMode.BEGINNING, null);
483 assertEquals(5, numberOfTaxa);
484 numberOfTaxa = taxonDao.countTaxaByName(true, false, false, "Smerinthus kindermannii", null, MatchMode.EXACT, null);
485 assertEquals(1, numberOfTaxa);
486 numberOfTaxa = taxonDao.countTaxaByName(false, true, false, "A", null, MatchMode.BEGINNING, null);
487 assertEquals(2, numberOfTaxa);
488 numberOfTaxa = taxonDao.countTaxaByName(true, true, false, "A", null, MatchMode.BEGINNING, null);
489 assertEquals(7, numberOfTaxa);
490 numberOfTaxa = taxonDao.countTaxaByName(true, true, false, "Aasfwerfwf fffe", null, MatchMode.BEGINNING, null);
491 assertEquals(0, numberOfTaxa);
492 // FIXME implement test for search in specific classification
493 // Reference reference = referenceDao.findByUuid(UUID.fromString("596b1325-be50-4b0a-9aa2-3ecd610215f2"));
494 // numberOfTaxa = taxonDao.countTaxaByName("A*", MatchMode.BEGINNING, SelectMode.ALL, null, null);
495 // assertEquals(numberOfTaxa, 2);
496 }
497
498 @Test
499 @DataSet
500 public void testRelatedTaxa() {
501 Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
502 assert taxon != null : "taxon must exist";
503
504 List<String> propertyPaths = new ArrayList<String>();
505 propertyPaths.add("fromTaxon");
506 propertyPaths.add("fromTaxon.name");
507 List<OrderHint> orderHints = new ArrayList<OrderHint>();
508 orderHints.add(new OrderHint("relatedFrom.name.genusOrUninomial", SortOrder.ASCENDING));
509 orderHints.add(new OrderHint("relatedFrom.name.specificEpithet", SortOrder.ASCENDING));
510 orderHints.add(new OrderHint("relatedFrom.name.infraSpecificEpithet", SortOrder.ASCENDING));
511
512 List<TaxonRelationship> relatedTaxa = taxonDao.getTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), null, null, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
513 assertNotNull("getRelatedTaxa should return a List",relatedTaxa);
514 assertEquals("getRelatedTaxa should return all 8 related taxa", 8, relatedTaxa.size());
515 assertTrue("getRelatedTaxa should return TaxonRelationship objects with the relatedFrom taxon initialized",Hibernate.isInitialized(relatedTaxa.get(0).getFromTaxon()));
516 assertTrue("getRelatedTaxa should return TaxonRelationship objects with the relatedFrom taxon initialized",Hibernate.isInitialized(relatedTaxa.get(0).getFromTaxon().getName()));
517
518 assertEquals("Acherontia should appear first in the list of related taxa", relatedTaxa.get(0).getFromTaxon().getTitleCache(), "Acherontia Laspeyres, 1809 sec. cate-sphingidae.org");
519 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());
520 }
521
522 @Test
523 @DataSet
524 public void testGetRelatedTaxaPaged() {
525 Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
526 assert taxon != null : "taxon must exist";
527
528 List<String> propertyPaths = new ArrayList<String>();
529 propertyPaths.add("fromTaxon");
530 propertyPaths.add("fromTaxon.name");
531
532 List<OrderHint> orderHints = new ArrayList<OrderHint>();
533 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
534
535 int pageSize = 3;
536 List<TaxonRelationship> firstPage = taxonDao.getTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), pageSize, 0, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
537 List<TaxonRelationship> secondPage = taxonDao.getTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),pageSize, 1, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
538 List<TaxonRelationship> thirdPage = taxonDao.getTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), pageSize, 2, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
539
540 assertNotNull("getRelatedTaxa: 3, 0 should return a List",firstPage);
541 assertEquals("getRelatedTaxa: 3, 0 should return a List with 3 elements", pageSize,firstPage.size());
542 assertNotNull("getRelatedTaxa: 3, 1 should return a List",secondPage);
543 assertEquals("getRelatedTaxa: 3, 1 should return a List with 3 elements", pageSize, secondPage.size());
544 assertNotNull("getRelatedTaxa: 3, 2 should return a List",thirdPage);
545 assertEquals("getRelatedTaxa: 3, 2 should return a List with 2 elements", 2, thirdPage.size());
546 }
547
548 @Test
549 @DataSet
550 public void testCountSynonyms() {
551 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
552 assert taxon != null : "taxon must exist";
553
554 int numberOfRelatedSynonym = taxonDao.countSynonyms(taxon,null);
555 assertEquals("countSynonyms should return 3", 3, numberOfRelatedSynonym);
556 }
557
558 @Test
559 @DataSet
560 public void testGetSynonyms() {
561 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
562 assert taxon != null : "taxon must exist";
563 List<String> propertyPaths = new ArrayList<String>();
564 propertyPaths.add("synonym");
565 propertyPaths.add("synonym.name");
566
567 List<OrderHint> orderHints = new ArrayList<OrderHint>();
568 orderHints.add(new OrderHint("titleCache", SortOrder.ASCENDING));
569
570 List<Synonym> synonyms = taxonDao.getSynonyms(taxon, null, null, null,orderHints,propertyPaths);
571
572 assertNotNull("getSynonyms should return a List", synonyms);
573 assertEquals("getSynonyms should return 3 synonyms", 3, synonyms.size());
574 assertTrue("getSynonyms should return synonym objects with the synonym initialized", Hibernate.isInitialized(synonyms.get(0)));
575 }
576
577 @Test
578 @DataSet
579 public void testCountSynonymsByType() {
580 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
581 assert taxon != null : "taxon must exist";
582
583 int numberOfTaxonomicSynonyms = taxonDao.countSynonyms(taxon, SynonymType.HETEROTYPIC_SYNONYM_OF());
584 assertEquals("countSynonyms should return 4", 3, numberOfTaxonomicSynonyms);
585 }
586
587 @Test
588 @DataSet
589 public void testSynonymsByType() {
590 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
591 assert taxon != null : "taxon must exist";
592
593 List<Synonym> synonyms = taxonDao.getSynonyms(taxon, SynonymType.HETEROTYPIC_SYNONYM_OF(), null, null,null,null);
594
595 assertNotNull("getSynonyms should return a List", synonyms);
596 assertEquals("getSynonyms should return 4 Synonyms", 3, synonyms.size());
597 }
598
599 @Test
600 @DataSet
601 public void testPageSynonyms(){
602 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
603 assert taxon != null : "taxon must exist";
604
605 int pageSize = 2;
606 List<Synonym> firstPage = taxonDao.getSynonyms(taxon, null, pageSize, 0,null,null);
607 List<Synonym> secondPage = taxonDao.getSynonyms(taxon, null, pageSize, 1,null,null);
608
609 assertNotNull("getSynonyms: 2, 0 should return a List",firstPage);
610 assertEquals("getSynonyms: 2, 0 should return 2 synonyms", pageSize,firstPage.size());
611 assertNotNull("getSynonyms: 2, 1 should return a List",secondPage);
612 assertEquals("getSynonyms: 2, 1 should return 1 synonym", 1, secondPage.size());
613 }
614
615 @Test
616 @DataSet("TaxonNodeDaoHibernateImplTest.xml")
617 public void testListAcceptedTaxaFor() {
618 UUID acheontitia_ciprosus = UUID.fromString("3ef145f7-bd92-4a64-8afd-2b8203e00e02");
619
620 Synonym synonym = (Synonym)taxonDao.findByUuid(acheontitia_ciprosus);
621 assertNotNull("synonym must exist", synonym);
622
623 Taxon taxon = taxonDao.acceptedTaxonFor(synonym, null, null);
624 assertNotNull("listAcceptedTaxaFor should return a taxon", taxon);
625
626 Classification classification = classificationDao.load(classificationUuid);
627 assertNotNull("classification must exist", classification);
628
629 taxon = taxonDao.acceptedTaxonFor(synonym, classification, null);
630 assertNull("listAcceptedTaxaFor should return not taxon due to classification filter", taxon);
631 }
632
633
634 @Test
635 @DataSet
636 public void testGetTaxonMatchingUninomial() {
637 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class, "Smerinthus", "*", "*", "*","*",null,null,null);
638
639 assertNotNull("findTaxaByName should return a List", result);
640 assertEquals("findTaxaByName should return two Taxa",2,result.size());
641 assertEquals("findTaxaByName should return a Taxon with id 5",5,result.get(0).getId());
642 }
643
644 @Test
645 @DataSet
646 public void testGetTaxonMatchingSpeciesBinomial() {
647 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class, "Smerinthus", null, "kindermannii", null,"*",null,null,null);
648
649 assertNotNull("findTaxaByName should return a List", result);
650 assertEquals("findTaxaByName should return one Taxon",1,result.size());
651 assertEquals("findTaxaByName should return a Taxon with id 8",8,result.get(0).getId());
652 }
653
654 @Test
655 @DataSet
656 public void testGetTaxonMatchingTrinomial() {
657 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class,"Cryptocoryne", null,"purpurea","borneoensis","*",null,null,null);
658
659 assertNotNull("findTaxaByName should return a List", result);
660 assertEquals("findTaxaByName should return one Taxon",1,result.size());
661 assertEquals("findTaxaByName should return a Taxon with id 38",38,result.get(0).getId());
662 }
663
664 @Test
665 @DataSet
666 public void testNegativeMatch() {
667 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class,"Acherontia", null,"atropos","dehli",null,null,null,null);
668
669 assertNotNull("findTaxaByName should return a List", result);
670 assertTrue("findTaxaByName should return an empty List",result.isEmpty());
671 }
672
673 @Test
674 @DataSet
675 public void testCountAllTaxa() {
676 int numberOfTaxa = taxonDao.count(Taxon.class);
677 assertEquals("count should return 14 taxa", 14, numberOfTaxa);
678 }
679
680 @Test
681 @DataSet
682 public void testListAllTaxa() {
683 List<Taxon> taxa = taxonDao.list(Taxon.class,100, 0);
684 assertNotNull("list should return a List", taxa);
685 assertEquals("list should return 14 taxa", 14, taxa.size());
686 }
687
688 @Test
689 @DataSet
690 // @ExpectedDataSet
691 public void testDelete() {
692 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
693 assert taxon != null : "taxon must exist";
694 taxonDao.delete(taxon);
695 taxon = (Taxon)taxonDao.findByUuid(acherontia);
696 assert taxon == null : "taxon must not exist";
697 setComplete();
698 endTransaction();
699 // try {
700 // printDataSet(new FileOutputStream("test.xml"), TABLE_NAMES);
701 // } catch (FileNotFoundException e) {
702 // e.printStackTrace();
703 // }
704 }
705
706 @Test
707 @DataSet
708 @ExpectedDataSet("TaxonDaoHibernateImplTest.testDelete-result.xml")
709 public void testDeleteWithMarker() {
710 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
711 taxon.addMarker(Marker.NewInstance(MarkerType.IS_DOUBTFUL(), true));
712 taxonDao.save(taxon);
713 assert taxon != null : "taxon must exist";
714
715 taxonDao.delete(taxon);
716 commitAndStartNewTransaction(null);
717 taxon = (Taxon)taxonDao.findByUuid(acherontia);
718 assert taxon == null : "taxon must not exist";
719 setComplete();
720 endTransaction();
721 // try {
722 // printDataSet(new FileOutputStream("test.xml"), TABLE_NAMES);
723 // } catch (FileNotFoundException e) {
724 // e.printStackTrace();
725 // }
726 }
727
728 @Test
729 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
730 public void testFindDeleted() {
731 TaxonBase<?> taxon = taxonDao.findByUuid(acherontia);
732 assertNull("findByUuid should return null in this view", taxon);
733 assertFalse("exist should return false in this view",taxonDao.exists(acherontia));
734 }
735
736 @Test
737 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
738 public void testFindDeletedInPreviousView() {
739 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
740 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
741 assertNotNull("findByUuid should return a taxon in this view",taxon);
742 assertTrue("exists should return true in this view", taxonDao.exists(acherontia));
743
744 try{
745 assertEquals("There should be 3 relations to this taxon in this view",3,taxon.getRelationsToThisTaxon().size());
746 } catch(Exception e) {
747 fail("We should not experience any problems initializing proxies with envers");
748 }
749 }
750
751 @Test
752 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
753 public void testGetAuditEvents() {
754 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
755 assert taxon != null : "taxon cannot be null";
756
757 List<String> propertyPaths = new ArrayList<String>();
758 propertyPaths.add("name");
759 propertyPaths.add("createdBy");
760 propertyPaths.add("updatedBy");
761
762 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(taxon, null,null,null,propertyPaths);
763 assertNotNull("getAuditEvents should return a list",auditEvents);
764 assertFalse("the list should not be empty",auditEvents.isEmpty());
765 assertEquals("There should be two AuditEventRecords in the list",2, auditEvents.size());
766 }
767
768 @Test
769 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
770 public void testGetAuditEventsFromNow() {
771 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
772 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
773 assert taxon != null : "taxon cannot be null";
774
775 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(taxon, null,null,AuditEventSort.FORWARDS,null);
776 assertNotNull("getAuditEvents should return a list",auditEvents);
777 assertFalse("the list should not be empty",auditEvents.isEmpty());
778 assertEquals("There should be one audit event in the list",1,auditEvents.size());
779 }
780
781 @Test
782 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
783 public void testCountAuditEvents() {
784 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
785 assert taxon != null : "taxon cannot be null";
786
787 int numberOfAuditEvents = taxonDao.countAuditEvents(taxon,null);
788 assertEquals("countAuditEvents should return 2",numberOfAuditEvents,2);
789 }
790
791 @Test
792 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
793 public void getPreviousAuditEvent() {
794 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
795 assert taxon != null : "taxon cannot be null";
796
797 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getPreviousAuditEvent(taxon);
798 assertNotNull("getPreviousAuditEvent should not return null as there is at least one audit event prior to the current one",auditEvent);
799 }
800
801 @Test
802 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
803 public void getPreviousAuditEventAtBeginning() {
804 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
805 TaxonBase taxon = taxonDao.findByUuid(sphingidae);
806 assert taxon != null : "taxon cannot be null";
807
808 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getPreviousAuditEvent(taxon);
809 assertNull("getPreviousAuditEvent should return null if we're at the first audit event anyway",auditEvent);
810 }
811
812 @Test
813 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
814 public void getNextAuditEvent() {
815 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
816 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
817 assert taxon != null : "taxon cannot be null";
818
819 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getNextAuditEvent(taxon);
820 assertNotNull("getNextAuditEvent should not return null as there is at least one audit event after the current one",auditEvent);
821 }
822
823 @Test
824 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
825 public void getNextAuditEventAtEnd() {
826 AuditEventContextHolder.getContext().setAuditEvent(mostRecentAuditEvent);
827 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
828 assert taxon != null : "taxon cannot be null";
829
830 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getNextAuditEvent(taxon);
831 assertNull("getNextAuditEvent should return null as there no more audit events after the current one",auditEvent);
832 }
833
834 @Test
835 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
836 public void testFind() {
837 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
838 assert taxon != null : "taxon cannot be null";
839
840 assertEquals("getRelationsToThisTaxon should contain 1 TaxonRelationship in this view",1,taxon.getRelationsToThisTaxon().size());
841 }
842
843 @Test
844 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
845 public void testFindInPreviousView() {
846 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
847 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
848 assert taxon != null : "taxon cannot be null";
849
850 assertTrue("getRelationsToThisTaxon should contain 0 TaxonRelationship in this view",taxon.getRelationsToThisTaxon().isEmpty());
851 }
852
853 @Test
854 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
855 public void testGetRelations() {
856 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
857 assert taxon != null : "taxon cannot be null";
858
859 List<String> propertyPaths = new ArrayList<String>();
860 propertyPaths.add("fromTaxon");
861 propertyPaths.add("fromTaxon.name");
862
863 List<OrderHint> orderHints = new ArrayList<OrderHint>();
864 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
865
866 List<TaxonRelationship> taxonRelations = taxonDao.getTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), null, null,orderHints,propertyPaths, TaxonRelationship.Direction.relatedFrom);
867 assertNotNull("getRelatedTaxa should return a list", taxonRelations);
868 assertEquals("there should be one TaxonRelationship in the list in the current view",1,taxonRelations.size());
869 assertTrue("TaxonRelationship.relatedFrom should be initialized",Hibernate.isInitialized(taxonRelations.get(0).getFromTaxon()));
870 assertTrue("TaxonRelationship.relatedFrom.name should be initialized",Hibernate.isInitialized(taxonRelations.get(0).getFromTaxon().getName()));
871 }
872
873 @Test
874 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
875 public void testCountRelations() {
876 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
877 assert taxon != null : "taxon cannot be null";
878 assertEquals("countRelatedTaxa should return 1 in the current view",1, taxonDao.countTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), TaxonRelationship.Direction.relatedTo));
879 }
880
881 @Test
882 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
883 public void testGetRelationsInPreviousView() {
884 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
885 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
886 assert taxon != null : "taxon cannot be null";
887
888 List<String> propertyPaths = new ArrayList<String>();
889 propertyPaths.add("relatedFrom");
890 propertyPaths.add("relatedFrom.name");
891
892 List<OrderHint> orderHints = new ArrayList<OrderHint>();
893 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
894
895 List<TaxonRelationship> taxonRelations = taxonDao.getTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), null, null,orderHints,propertyPaths, TaxonRelationship.Direction.relatedFrom);
896 assertNotNull("getRelatedTaxa should return a list",taxonRelations);
897 assertTrue("there should be no TaxonRelationships in the list in the prior view",taxonRelations.isEmpty());
898 }
899
900 @Test
901 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
902 public void testCountRelationsInPreviousView() {
903 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
904 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
905 assert taxon != null : "taxon cannot be null";
906 assertEquals("countRelatedTaxa should return 0 in the current view",0, taxonDao.countTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), TaxonRelationship.Direction.relatedTo));
907 }
908
909 @Test
910 @DataSet
911 public void testGroupTaxa() {
912 List<Grouping> groups = new ArrayList<Grouping>();
913 groups.add(new GroupByCount("count",SortOrder.DESCENDING));
914 groups.add(new Grouping("name.genusOrUninomial", "genus", "n", SortOrder.ASCENDING));
915 List<Object[]> results = taxonDao.group(null, null, null, groups,null);
916 System.out.println("count\tname.genuOrUninomial");
917 for(Object[] result : results) {
918 System.out.println(result[0] + "\t" + result[1]);
919 }
920 }
921
922 @Test
923 @DataSet
924 public void testGroupTaxaByClass() {
925 List<Grouping> groups = new ArrayList<Grouping>();
926 groups.add(new GroupByCount("count",SortOrder.DESCENDING));
927 groups.add(new Grouping("class", "class",null, SortOrder.ASCENDING));
928 List<Object[]> results = taxonDao.group(null, null, null, groups,null);
929 System.out.println("count\tclass");
930 for(Object[] result : results) {
931 System.out.println(result[0] + "\t" + result[1]);
932 }
933 }
934
935 @Test
936 @DataSet
937 public void testNativeSQLOrder() {
938 List<OrderHint> orderHints = new ArrayList<OrderHint>();
939 orderHints.add(new NativeSqlOrderHint("case when {alias}.titleCache like 'C%' then 0 else 1 end",SortOrder.ASCENDING));
940
941 List<TaxonBase> results = taxonDao.list(null, null, orderHints);
942 System.out.println("native SQL order");
943 for(TaxonBase<?> result : results) {
944 System.out.println(result.getTitleCache());
945 }
946 }
947
948 @Test
949 @DataSet
950 public void testGroupByDateTaxa() {
951 List<Grouping> groups = new ArrayList<Grouping>();
952 groups.add(new GroupByCount("count",null));
953 groups.add(new GroupByDate("created", "dateGroup", SortOrder.ASCENDING, GroupByDate.Resolution.MONTH));
954 List<Object[]> results = taxonDao.group(null, null, null, groups,null);
955 System.out.println("count\tyear\tmonth");
956 for(Object[] result : results) {
957 System.out.println(result[0] + "\t" + result[1] + "\t" + result[2]);
958 }
959 }
960
961 @Test
962 @DataSet ("TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
963 public final void testGetTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(){
964 Classification classification = classificationDao.findByUuid(classificationUuid);
965 List<UuidAndTitleCache<TaxonNode>> result = taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, null, null);
966 assertNotNull(result);
967 assertEquals(5, result.size());
968
969 //test exclude
970 UUID excludeUUID = UUID.fromString("a9f42927-e507-4fda-9629-62073a908aae");
971 List<UUID> excludeUUids = new ArrayList<UUID>();
972 excludeUUids.add(excludeUUID);
973 result = taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, null, null);
974 assertEquals(5, result.size());
975
976 //test limit
977 int limit = 2;
978 result = taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, limit, null);
979 assertEquals(2, result.size());
980
981 //test pattern
982 String pattern = "*Rothschi*";
983 result = taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, 2, pattern);
984 assertNotNull(result);
985 assertEquals(1, result.size());
986 assertEquals("0b5846e5-b8d2-4ca9-ac51-099286ea4adc", result.get(0).getUuid().toString());
987
988 }
989
990
991 @Test
992 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
993 //NOTE: There is a problem with loading AuditEvents if this test runs
994 //stand alone or as first (one of the first) in the test suite. For some reason
995 //the AuditEvent records from the @DataSet are not inserted into the database then,
996 //while those inserted by the terms dataset are inserted as well as a completely new one.
997 //This problem for some reason does not happen if not running at first place
998 public void testGetAuditEventsByTypeWithRestrictions() {
999 commitAndStartNewTransaction(new String[]{"AUDITEVENT", "TAXONBASE_AUD"});
1000
1001 List<String> propertyPaths = new ArrayList<String>();
1002 propertyPaths.add("name");
1003 propertyPaths.add("createdBy");
1004 propertyPaths.add("updatedBy");
1005
1006 List<AuditCriterion> criteria = new ArrayList<AuditCriterion>();
1007 criteria.add(AuditEntity.property("lsid_lsid").isNotNull());
1008
1009 int count = taxonDao.countAuditEvents(TaxonBase.class, null, null, null);
1010
1011 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(TaxonBase.class, previousAuditEvent, mostRecentAuditEvent, criteria,null, null, AuditEventSort.FORWARDS, propertyPaths);
1012 assertNotNull("getAuditEvents should return a list",auditEvents);
1013 assertFalse("the list should not be empty",auditEvents.isEmpty());
1014 assertEquals("There should be one AuditEventRecord in the list",1, auditEvents.size());
1015 }
1016
1017
1018 @Test
1019 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
1020 // @DataSets({ //for testing only
1021 // @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml"),
1022 // @DataSet("TaxonDaoHibernateImplTest.testFindDeletedAuditEvents.xml")
1023 // })
1024 //NOTE: There is a problem with loading AuditEvents if this test runs
1025 //stand alone or as first (one of the first) in the test suite. For some reason
1026 //the AuditEvent records from the @DataSet are not inserted into the database then,
1027 //while those inserted by the terms dataset are inserted as well as a completely new one.
1028 //This problem for some reason does not happen if not running at first place
1029 public void testGetAuditEventsByTypeWithNoRestrictions() {
1030 printDataSet(System.out, new String[]{"AUDITEVENT", "TAXONBASE_AUD"});
1031 commitAndStartNewTransaction(new String[]{"AUDITEVENT", "TAXONBASE_AUD"});
1032
1033 List<String> propertyPaths = new ArrayList<String>();
1034 propertyPaths.add("name");
1035 propertyPaths.add("createdBy");
1036 propertyPaths.add("updatedBy");
1037 int count = taxonDao.countAuditEvents(TaxonBase.class, null, null, null);
1038 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(TaxonBase.class, previousAuditEvent, mostRecentAuditEvent, null,null, null, AuditEventSort.FORWARDS, propertyPaths);
1039 assertNotNull("getAuditEvents should return a list", auditEvents);
1040 assertFalse("the list should not be empty", auditEvents.isEmpty());
1041 assertEquals("There should be thirty eight AuditEventRecords in the list", 2, auditEvents.size());
1042 }
1043
1044
1045 @Test
1046 @DataSet("TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
1047 public void testGetCommonName(){
1048 List<Taxon> commonNameResults = taxonDao.getTaxaByCommonName("common%", null,
1049 MatchMode.BEGINNING, null, null, null, null);
1050
1051 assertNotNull("getTaxaByCommonName should return a list", commonNameResults);
1052 assertFalse("the list should not be empty", commonNameResults.isEmpty());
1053 assertEquals("There should be one Taxon with common name", 1,commonNameResults.size());
1054 assertEquals(" sec. ???", ((TaxonBase)commonNameResults.get(0)).getTitleCache());
1055 }
1056
1057 @Test
1058 @DataSet
1059 public void testGetTitleCache(){
1060 UUID uuid = UUID.fromString("7b8b5cb3-37ba-4dba-91ac-4c6ffd6ac331");
1061 String titleCache = taxonDao.getTitleCache(uuid, false);
1062 Assert.assertEquals("Acherontia styx Westwood, 1847 sec. cate-sphingidae.org", titleCache);
1063 titleCache = taxonDao.getTitleCache(uuid, true);
1064 Assert.assertEquals("Acherontia styxx Westwood, 1847 sec. cate-sphingidae.org", titleCache);
1065 }
1066
1067
1068 @Test
1069 @DataSet("TaxonDaoHibernateImplTest.testPropertyPath.xml")
1070 public void testPropertyPath(){
1071 //Test that BeanInitializer also works on HiberanteProxys
1072 Classification c = classificationDao.load(UUID.fromString("4bceea53-893f-4685-8c63-6dcec6e85ab1"));
1073 TaxonNode singleNode = c.getRootNode().getChildNodes().iterator().next();
1074 Taxon taxonProxy = singleNode.getTaxon();
1075 Assert.assertTrue("Object to test should be a proxy ", taxonProxy instanceof HibernateProxy);
1076
1077 List<String> propertyPaths = new ArrayList<String>();
1078 propertyPaths.add("taxonNodes");
1079 Taxon taxon = (Taxon)this.taxonDao.load(
1080 UUID.fromString("4a5bc930-844f-45ec-aea8-dd155e1ab25f"),
1081 propertyPaths);
1082 Assert.assertSame("Returned object should be the same proxy to assure that we ran initialization on this proxy", taxonProxy, taxon);
1083 }
1084
1085 /**
1086 * {@inheritDoc}
1087 */
1088 @Override
1089 // @Test
1090 public void createTestDataSet() throws FileNotFoundException {
1091 // Classification classification = Classification.NewInstance("Test");
1092 // BotanicalName taxonNameBase = null;
1093 // Reference sec = null;
1094 // Taxon taxon = Taxon.NewInstance(taxonNameBase, sec);
1095 // classification.addChildTaxon(taxon, sec, null);
1096 //
1097 // classificationDao.save(classification);
1098 // this.commitAndStartNewTransaction(null);
1099 //
1100 // writeDbUnitDataSetFile(new String[] {
1101 // "CLASSIFICATION", "TAXONNAMEBASE",
1102 // "REFERENCE","TAXONNODE",
1103 // "TAXONBASE","LANGUAGESTRING",
1104 // "HIBERNATE_SEQUENCES" // IMPORTANT!!!
1105 // },
1106 // "testPropertyPath" );
1107 }
1108
1109 }