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