Updated a couple of methods to be more in line with generic dao method pattern
[cdmlib.git] / cdmlib-persistence / src / test / java / eu / etaxonomy / cdm / persistence / dao / hibernate / taxon / TaxonDaoHibernateImplTest.java
1 package eu.etaxonomy.cdm.persistence.dao.hibernate.taxon;
2
3 import static junit.framework.Assert.assertEquals;
4 import static junit.framework.Assert.assertFalse;
5 import static junit.framework.Assert.assertNotNull;
6 import static junit.framework.Assert.assertSame;
7 import static junit.framework.Assert.assertTrue;
8 import static org.junit.Assert.assertNull;
9 import static org.junit.Assert.fail;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.UUID;
14
15 import org.hibernate.Hibernate;
16 import org.junit.After;
17 import org.junit.Before;
18 import org.junit.Ignore;
19 import org.junit.Test;
20 import org.unitils.dbunit.annotation.DataSet;
21 import org.unitils.dbunit.annotation.ExpectedDataSet;
22 import org.unitils.spring.annotation.SpringBeanByType;
23
24 import eu.etaxonomy.cdm.model.name.Rank;
25 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
26 import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
27 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
28 import eu.etaxonomy.cdm.model.taxon.Taxon;
29 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
30 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
31 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
32 import eu.etaxonomy.cdm.model.view.AuditEvent;
33 import eu.etaxonomy.cdm.model.view.AuditEventRecord;
34 import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
35 import eu.etaxonomy.cdm.persistence.dao.common.AuditEventSort;
36 import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
37 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
38 import eu.etaxonomy.cdm.persistence.fetch.CdmFetch;
39 import eu.etaxonomy.cdm.persistence.query.OrderHint;
40 import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
41 import eu.etaxonomy.cdm.persistence.query.MatchMode;
42 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
43
44 /**
45 * @author a.mueller
46 * @author ben.clark
47 *
48 */
49 public class TaxonDaoHibernateImplTest extends CdmTransactionalIntegrationTest {
50
51 @SpringBeanByType
52 private ITaxonDao taxonDao;
53
54 @SpringBeanByType
55 private IReferenceDao referenceDao;
56
57 private UUID uuid;
58 private UUID sphingidae;
59 private UUID acherontia;
60 private UUID mimas;
61 private UUID acherontiaLachesis;
62 private AuditEvent previousAuditEvent;
63 private AuditEvent mostRecentAuditEvent;
64
65 @Before
66 public void setUp() {
67 uuid = UUID.fromString("496b1325-be50-4b0a-9aa2-3ecd610215f2");
68 sphingidae = UUID.fromString("54e767ee-894e-4540-a758-f906ecb4e2d9");
69 acherontia = UUID.fromString("c5cc8674-4242-49a4-aada-72d63194f5fa");
70 acherontiaLachesis = UUID.fromString("b04cc9cb-2b4a-4cc4-a94a-3c93a2158b06");
71 mimas = UUID.fromString("900052b7-b69c-4e26-a8f0-01c215214c40");
72 previousAuditEvent = new AuditEvent();
73 previousAuditEvent.setRevisionNumber(1025);
74 previousAuditEvent.setUuid(UUID.fromString("a680fab4-365e-4765-b49e-768f2ee30cda"));
75 mostRecentAuditEvent = new AuditEvent();
76 mostRecentAuditEvent.setRevisionNumber(1026);
77 mostRecentAuditEvent.setUuid(UUID.fromString("afe8e761-8545-497b-9134-6a6791fc0b0d"));
78 AuditEventContextHolder.clearContext(); // By default we're in the current view (i.e. view == null)
79 }
80
81 @After
82 public void tearDown() {
83 AuditEventContextHolder.clearContext();
84 }
85
86 /**
87 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#TaxonDaoHibernateImpl()}.
88 */
89 @Test
90 @DataSet
91 public void testInit() {
92 assertNotNull("Instance of ITaxonDao expected",taxonDao);
93 assertNotNull("Instance of IReferenceDao expected",referenceDao);
94 }
95
96 /**
97 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getRootTaxa(eu.etaxonomy.cdm.model.reference.ReferenceBase)}.
98 */
99 @Test
100 @DataSet
101 public void testGetRootTaxa() {
102 ReferenceBase sec1 = referenceDao.findById(1);
103 assert sec1 != null : "sec1 must exist";
104 ReferenceBase sec2 = referenceDao.findById(2);
105 assert sec2 != null : "sec2 must exist";
106
107 List<Taxon> rootTaxa = taxonDao.getRootTaxa(sec1);
108 assertNotNull("getRootTaxa should return a List",rootTaxa);
109 assertFalse("The list should not be empty",rootTaxa.isEmpty());
110 assertEquals("There should be one root taxon",1, rootTaxa.size());
111
112 rootTaxa = taxonDao.getRootTaxa(sec1, CdmFetch.FETCH_CHILDTAXA(), true, false);
113 assertNotNull("getRootTaxa should return a List",rootTaxa);
114 assertFalse("The list should not be empty",rootTaxa.isEmpty());
115 assertEquals("There should be one root taxon",1, rootTaxa.size());
116
117 rootTaxa = taxonDao.getRootTaxa(Rank.GENUS(), sec1, CdmFetch.FETCH_CHILDTAXA(), true, false, null);
118 assertNotNull("getRootTaxa should return a List",rootTaxa);
119 assertFalse("The list should not be empty",rootTaxa.isEmpty());
120 assertEquals("There should be one root taxon",1, rootTaxa.size());
121
122 rootTaxa = taxonDao.getRootTaxa(Rank.FAMILY(), sec2, CdmFetch.FETCH_CHILDTAXA(), true, false, null);
123 if (logger.isDebugEnabled()) {
124 logger.debug("Root taxa rank Family (" + rootTaxa.size() + "):");
125 for (Taxon taxon: rootTaxa) {
126 logger.debug(taxon.getTitleCache());
127 }
128 }
129 assertEquals("There should be one root taxon rank Family",1, rootTaxa.size());
130 rootTaxa = taxonDao.getRootTaxa(Rank.GENUS(), sec2, CdmFetch.FETCH_CHILDTAXA(), true, false, null);
131 assertNotNull("getRootTaxa should return a List",rootTaxa);
132 assertFalse("The list should not be empty",rootTaxa.isEmpty());
133 if (logger.isDebugEnabled()) {
134 logger.debug("Root taxa rank Genus (" + rootTaxa.size() + "):");
135 for (Taxon taxon: rootTaxa) {
136 logger.debug(taxon.getTitleCache());
137 }
138 }
139 assertEquals("There should be 22 root taxa rank Genus",22, rootTaxa.size());
140
141 rootTaxa = taxonDao.getRootTaxa(Rank.SPECIES(), sec2, CdmFetch.FETCH_CHILDTAXA(), true, false, null);
142 if (logger.isDebugEnabled()) {
143 logger.debug("Root taxa rank Species (" + rootTaxa.size() + "):");
144 for (Taxon taxon: rootTaxa) {
145 logger.debug(taxon.getTitleCache());
146 }
147 }
148 assertEquals("There should be 4 root taxa rank Species",3, rootTaxa.size());
149 }
150
151 /**
152 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.ReferenceBase)}.
153 */
154 @Test
155 @DataSet
156 public void testGetTaxaByName() {
157 ReferenceBase sec = referenceDao.findById(1);
158 assert sec != null : "sec must exist";
159
160 List<TaxonBase> results = taxonDao.getTaxaByName("Aus", sec);
161 assertNotNull("getTaxaByName should return a List", results);
162 //assertFalse("The list should not be empty", results.isEmpty());
163 assertTrue(results.size() == 1);
164
165 results = taxonDao.getTaxaByName("A*", MatchMode.BEGINNING,
166 true, null, null);
167 assertNotNull("getTaxaByName should return a List", results);
168 assertTrue(results.size() == 9);
169
170 results = taxonDao.getTaxaByName("A", MatchMode.BEGINNING,
171 true, null, null);
172 assertNotNull("getTaxaByName should return a List", results);
173 assertTrue(results.size() == 9);
174
175 results = taxonDao.getTaxaByName("Aus", MatchMode.EXACT,
176 true, null, null);
177 assertNotNull("getTaxaByName should return a List", results);
178 assertTrue(results.size() == 1);
179 }
180
181 @Test
182 @DataSet
183 public void testFindByUuid() {
184 Taxon taxon = (Taxon)taxonDao.findByUuid(uuid);
185 assertNotNull("findByUuid should return a taxon",taxon);
186 assertFalse("findByUuid should not return a taxon with it's name initialized",Hibernate.isInitialized(taxon.getName()));
187 }
188
189 @Test
190 @DataSet
191 public void testLoad() {
192 List<String> propertyPaths = new ArrayList<String>();
193 propertyPaths.add("name");
194 propertyPaths.add("sec");
195 Taxon taxon = (Taxon)taxonDao.load(uuid, propertyPaths);
196 assertNotNull("findByUuid should return a taxon",taxon);
197 assertTrue("load should return a taxon with it's name initialized, given that the property was specified in the method",Hibernate.isInitialized(taxon.getName()));
198 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()));
199 }
200
201 @Test
202 @DataSet
203 public void testCountRelatedTaxa() {
204 Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
205 assert taxon != null : "taxon must exist";
206
207 int numberOfRelatedTaxa = taxonDao.countRelatedTaxa(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN());
208 assertEquals("countRelatedTaxa should return 23", 23, numberOfRelatedTaxa);
209 }
210
211 @Test
212 @DataSet
213 public void testRelatedTaxa() {
214 Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
215 assert taxon != null : "taxon must exist";
216
217 List<String> propertyPaths = new ArrayList<String>();
218 propertyPaths.add("fromTaxon");
219 propertyPaths.add("fromTaxon.name");
220 List<OrderHint> orderHints = new ArrayList<OrderHint>();
221 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
222
223 List<TaxonRelationship> relatedTaxa = taxonDao.getRelatedTaxa(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), null, null, orderHints,propertyPaths);
224 assertNotNull("getRelatedTaxa should return a List",relatedTaxa);
225 assertEquals("getRelatedTaxa should return all 23 related taxa",relatedTaxa.size(),23);
226 assertTrue("getRelatedTaxa should return TaxonRelationship objects with the relatedFrom taxon initialized",Hibernate.isInitialized(relatedTaxa.get(0).getFromTaxon()));
227 assertTrue("getRelatedTaxa should return TaxonRelationship objects with the relatedFrom taxon initialized",Hibernate.isInitialized(relatedTaxa.get(0).getFromTaxon().getName()));
228
229 assertEquals("Acherontia should appear first in the list of related taxa", relatedTaxa.get(0).getFromTaxon().getTitleCache(), "Acherontia Laspeyres, 1809 sec. cate-sphingidae.org");
230 assertEquals("Sphingonaepiopsis should appear last in the list of related taxa", relatedTaxa.get(22).getFromTaxon().getTitleCache(), "Sphinx Linnaeus, 1758 sec. cate-sphingidae.org");
231 }
232
233 @Test
234 @DataSet
235 public void testGetRelatedTaxaPaged() {
236 Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
237 assert taxon != null : "taxon must exist";
238
239 List<String> propertyPaths = new ArrayList<String>();
240 propertyPaths.add("fromTaxon");
241 propertyPaths.add("fromTaxon.name");
242
243 List<OrderHint> orderHints = new ArrayList<OrderHint>();
244 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
245
246 List<TaxonRelationship> firstPage = taxonDao.getRelatedTaxa(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), 10, 0, orderHints,propertyPaths);
247 List<TaxonRelationship> secondPage = taxonDao.getRelatedTaxa(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),10, 1, orderHints,propertyPaths);
248 List<TaxonRelationship> thirdPage = taxonDao.getRelatedTaxa(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), 10, 2, orderHints,propertyPaths);
249
250 assertNotNull("getRelatedTaxa: 10, 0 should return a List",firstPage);
251 assertEquals("getRelatedTaxa: 10, 0 should return a List with 10 elements",10,firstPage.size());
252 assertNotNull("getRelatedTaxa: 10, 1 should return a List",secondPage);
253 assertEquals("getRelatedTaxa: 10, 1 should return a List with 10 elements",secondPage.size(),10);
254 assertNotNull("getRelatedTaxa: 10, 2 should return a List",thirdPage);
255 assertEquals("getRelatedTaxa: 10, 2 should return a List with 3 elements",thirdPage.size(),3);
256 }
257
258 @Test
259 @DataSet
260 public void testCountSynonymRelationships() {
261 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
262 assert taxon != null : "taxon must exist";
263
264 int numberOfSynonymRelationships = taxonDao.countSynonyms(taxon,null);
265 assertEquals("countSynonymRelationships should return 5",5,numberOfSynonymRelationships);
266 }
267
268 @Test
269 @DataSet
270 public void testSynonymRelationships() {
271 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
272 assert taxon != null : "taxon must exist";
273 List<String> propertyPaths = new ArrayList<String>();
274 propertyPaths.add("synonym");
275 propertyPaths.add("synonym.name");
276
277 List<OrderHint> orderHints = new ArrayList<OrderHint>();
278 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
279
280 List<SynonymRelationship> synonyms = taxonDao.getSynonyms(taxon, null, null, null,orderHints,propertyPaths);
281
282 assertNotNull("getSynonyms should return a List",synonyms);
283 assertEquals("getSynonyms should return 5 SynonymRelationship entities",synonyms.size(),5);
284 assertTrue("getSynonyms should return SynonymRelationship objects with the synonym initialized",Hibernate.isInitialized(synonyms.get(0).getSynonym()));
285 }
286
287 @Test
288 @DataSet
289 public void testCountSynonymRelationshipsByType() {
290 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
291 assert taxon != null : "taxon must exist";
292
293 int numberOfTaxonomicSynonyms = taxonDao.countSynonyms(taxon, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
294 assertEquals("countSynonyms should return 4",numberOfTaxonomicSynonyms, 4);
295 }
296
297 @Test
298 @DataSet
299 public void testSynonymRelationshipsByType() {
300 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
301 assert taxon != null : "taxon must exist";
302
303 List<SynonymRelationship> synonyms = taxonDao.getSynonyms(taxon, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF(), null, null,null,null);
304
305 assertNotNull("getSynonyms should return a List",synonyms);
306 assertEquals("getSynonyms should return 4 SynonymRelationship entities",synonyms.size(),4);
307 }
308
309 @Test
310 @DataSet
311 public void testPageSynonymRelationships(){
312 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
313 assert taxon != null : "taxon must exist";
314
315 List<SynonymRelationship> firstPage = taxonDao.getSynonyms(taxon, null, 4, 0,null,null);
316 List<SynonymRelationship> secondPage = taxonDao.getSynonyms(taxon, null, 4, 1,null,null);
317
318 assertNotNull("getSynonyms: 4, 0 should return a List",firstPage);
319 assertEquals("getSynonyms: 4, 0 should return 4 SynonymRelationships", firstPage.size(),4);
320 assertNotNull("getSynonyms: 4, 1 should return a List",secondPage);
321 assertEquals("getSynonyms: 4, 1 should return 1 SynonymRelationship",secondPage.size(),1);
322 }
323
324 @Test
325 @DataSet
326 public void testGetTaxonMatchingUninomial() {
327 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class, "Smerinthus", "*", "*", "*",null,null,null);
328
329 assertNotNull("findTaxaByName should return a List", result);
330 assertEquals("findTaxaByName should return two Taxa",2,result.size());
331 assertEquals("findTaxaByName should return a Taxon with id 5",5,result.get(0).getId());
332 }
333
334 @Test
335 @DataSet
336 public void testGetTaxonMatchingSpeciesBinomial() {
337 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class,"Smerinthus", null, "kindermannii", null,null,null,null);
338
339 assertNotNull("findTaxaByName should return a List", result);
340 assertEquals("findTaxaByName should return one Taxon",1,result.size());
341 assertEquals("findTaxaByName should return a Taxon with id 8",8,result.get(0).getId());
342 }
343
344 @Test
345 @DataSet
346 public void testGetTaxonMatchingTrinomial() {
347 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class,"Cryptocoryne", null,"purpurea","borneoensis",null,null,null);
348
349 assertNotNull("findTaxaByName should return a List", result);
350 assertEquals("findTaxaByName should return one Taxon",1,result.size());
351 assertEquals("findTaxaByName should return a Taxon with id 38",38,result.get(0).getId());
352 }
353
354 @Test
355 @DataSet
356 public void testNegativeMatch() {
357 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class,"Acherontia", null,"atropos","dehli",null,null,null);
358
359 assertNotNull("findTaxaByName should return a List", result);
360 assertTrue("findTaxaByName should return an empty List",result.isEmpty());
361 }
362
363 @Test
364 @DataSet
365 public void testCountAllTaxa() {
366 int numberOfTaxa = taxonDao.count(Taxon.class);
367 assertEquals("count should return 33 taxa",33, numberOfTaxa);
368 }
369
370 @Test
371 @DataSet
372 public void testListAllTaxa() {
373 List<Taxon> taxa = taxonDao.list(Taxon.class,100, 0);
374 assertNotNull("list should return a List",taxa);
375 assertEquals("list should return 33 taxa",33, taxa.size());
376 }
377
378 @Test
379 @DataSet
380 @ExpectedDataSet
381 public void testDelete() {
382 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
383 assert taxon != null : "taxon must exist";
384 taxonDao.delete(taxon);
385 Taxon taxonWithChildren = (Taxon)taxonDao.findByUuid(mimas);
386 assert taxonWithChildren != null : "taxon must exist";
387 assertEquals(taxonWithChildren.getTaxonomicChildrenCount(), 2);
388 Taxon parent = (Taxon)taxonDao.findByUuid(sphingidae);
389 assertSame(taxonWithChildren.getTaxonomicParent(), parent);
390 assertEquals(parent.getTaxonomicChildrenCount(), 203);
391 taxonDao.delete(taxonWithChildren);
392 assertEquals(parent.getTaxonomicChildrenCount(), 202);
393 setComplete();
394 endTransaction();
395 }
396
397 @Test
398 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
399 public void testFindDeleted() {
400 TaxonBase taxon = taxonDao.findByUuid(acherontia);
401 assertNull("findByUuid should return null in this view", taxon);
402 assertFalse("exist should return false in this view",taxonDao.exists(acherontia));
403 }
404
405 @Test
406 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
407 public void testFindDeletedInPreviousView() {
408 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
409 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
410 assertNotNull("findByUuid should return a taxon in this view",taxon);
411 assertTrue("exists should return true in this view", taxonDao.exists(acherontia));
412
413 try{
414 assertEquals("There should be 3 relations to this taxon in this view",3,taxon.getRelationsToThisTaxon().size());
415 } catch(Exception e) {
416 fail("We should not experience any problems initializing proxies with envers");
417 }
418 }
419
420 @Test
421 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
422 public void testGetAuditEvents() {
423 TaxonBase taxon = taxonDao.findByUuid(sphingidae);
424 assert taxon != null : "taxon cannot be null";
425
426 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(taxon, null,null,null);
427 assertNotNull("getAuditEvents should return a list",auditEvents);
428 assertFalse("the list should not be empty",auditEvents.isEmpty());
429 assertEquals("There should be two AuditEventRecords in the list",2, auditEvents.size());
430 }
431
432 @Test
433 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
434 public void testGetAuditEventsFromNow() {
435 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
436 TaxonBase taxon = taxonDao.findByUuid(sphingidae);
437 assert taxon != null : "taxon cannot be null";
438
439 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(taxon, null,null,AuditEventSort.FORWARDS);
440 assertNotNull("getAuditEvents should return a list",auditEvents);
441 assertFalse("the list should not be empty",auditEvents.isEmpty());
442 assertEquals("There should be one audit event in the list",1,auditEvents.size());
443 }
444
445 @Test
446 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
447 public void testCountAuditEvents() {
448 TaxonBase taxon = taxonDao.findByUuid(sphingidae);
449 assert taxon != null : "taxon cannot be null";
450
451 int numberOfAuditEvents = taxonDao.countAuditEvents(taxon,null);
452 assertEquals("countAuditEvents should return 2",numberOfAuditEvents,2);
453 }
454
455 @Test
456 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
457 public void getPreviousAuditEvent() {
458 TaxonBase taxon = taxonDao.findByUuid(sphingidae);
459 assert taxon != null : "taxon cannot be null";
460
461 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getPreviousAuditEvent(taxon);
462 assertNotNull("getPreviousAuditEvent should not return null as there is at least one audit event prior to the current one",auditEvent);
463 }
464
465 @Test
466 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
467 public void getPreviousAuditEventAtBeginning() {
468 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
469 TaxonBase taxon = taxonDao.findByUuid(sphingidae);
470 assert taxon != null : "taxon cannot be null";
471
472 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getPreviousAuditEvent(taxon);
473 assertNull("getPreviousAuditEvent should return null if we're at the first audit event anyway",auditEvent);
474 }
475
476 @Test
477 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
478 public void getNextAuditEvent() {
479 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
480 TaxonBase taxon = taxonDao.findByUuid(sphingidae);
481 assert taxon != null : "taxon cannot be null";
482
483 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getNextAuditEvent(taxon);
484 assertNotNull("getNextAuditEvent should not return null as there is at least one audit event after the current one",auditEvent);
485 }
486
487 @Test
488 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
489 public void getNextAuditEventAtEnd() {
490 AuditEventContextHolder.getContext().setAuditEvent(mostRecentAuditEvent);
491 TaxonBase taxon = taxonDao.findByUuid(sphingidae);
492 assert taxon != null : "taxon cannot be null";
493
494 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getNextAuditEvent(taxon);
495 assertNull("getNextAuditEvent should return null as there no more audit events after the current one",auditEvent);
496 }
497
498 @Test
499 @DataSet
500 @ExpectedDataSet
501 @Ignore
502 public void testAddChild() throws Exception {
503 Taxon parent = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
504 assert parent != null : "taxon cannot be null";
505 Taxon child = Taxon.NewInstance(null, null);
506 child.setTitleCache("Acherontia lachesis diehli Eitschberger, 2003", true);
507 child.addTaxonRelation(parent, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),null, null);
508 taxonDao.save(child);
509 setComplete();
510 endTransaction();
511 }
512
513 @Test
514 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
515 public void testFind() {
516 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
517 assert taxon != null : "taxon cannot be null";
518
519 assertEquals("getTaxonomicChildrenCount should return 1 in this view",1,taxon.getTaxonomicChildrenCount());
520 assertEquals("getRelationsToThisTaxon should contain 1 TaxonRelationship in this view",1,taxon.getRelationsToThisTaxon().size());
521 }
522
523 @Test
524 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
525 public void testFindInPreviousView() {
526 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
527 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
528 assert taxon != null : "taxon cannot be null";
529
530 assertEquals("getTaxonomicChildrenCount should return 0 in this view",0,taxon.getTaxonomicChildrenCount());
531 assertTrue("getRelationsToThisTaxon should contain 0 TaxonRelationship in this view",taxon.getRelationsToThisTaxon().isEmpty());
532 }
533
534 @Test
535 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
536 public void testGetRelations() {
537 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
538 assert taxon != null : "taxon cannot be null";
539
540 List<String> propertyPaths = new ArrayList<String>();
541 propertyPaths.add("fromTaxon");
542 propertyPaths.add("fromTaxon.name");
543
544 List<OrderHint> orderHints = new ArrayList<OrderHint>();
545 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
546
547 List<TaxonRelationship> taxonRelations = taxonDao.getRelatedTaxa(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), null, null,orderHints,propertyPaths);
548 assertNotNull("getRelatedTaxa should return a list", taxonRelations);
549 assertEquals("there should be one TaxonRelationship in the list in the current view",1,taxonRelations.size());
550 assertTrue("TaxonRelationship.relatedFrom should be initialized",Hibernate.isInitialized(taxonRelations.get(0).getFromTaxon()));
551 assertTrue("TaxonRelationship.relatedFrom.name should be initialized",Hibernate.isInitialized(taxonRelations.get(0).getFromTaxon().getName()));
552 }
553
554 @Test
555 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
556 public void testCountRelations() {
557 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
558 assert taxon != null : "taxon cannot be null";
559 assertEquals("countRelatedTaxa should return 1 in the current view",1, taxonDao.countRelatedTaxa(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN()));
560 }
561
562 @Test
563 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
564 public void testGetRelationsInPreviousView() {
565 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
566 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
567 assert taxon != null : "taxon cannot be null";
568
569 List<String> propertyPaths = new ArrayList<String>();
570 propertyPaths.add("relatedFrom");
571 propertyPaths.add("relatedFrom.name");
572
573 List<OrderHint> orderHints = new ArrayList<OrderHint>();
574 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
575
576 List<TaxonRelationship> taxonRelations = taxonDao.getRelatedTaxa(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), null, null,orderHints,propertyPaths);
577 assertNotNull("getRelatedTaxa should return a list",taxonRelations);
578 assertTrue("there should be no TaxonRelationships in the list in the prior view",taxonRelations.isEmpty());
579 }
580
581 @Test
582 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
583 public void testCountRelationsInPreviousView() {
584 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
585 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
586 assert taxon != null : "taxon cannot be null";
587 assertEquals("countRelatedTaxa should return 0 in the current view",0, taxonDao.countRelatedTaxa(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN()));
588 }
589 }