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