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