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