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