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