Merged latest trunk updates to branch
[cdmlib.git] / cdmlib-persistence / src / test / java / eu / etaxonomy / cdm / persistence / dao / hibernate / taxon / TaxonNodeDaoHibernateImplTest.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 junit.framework.Assert.assertNotNull;
13 import static org.junit.Assert.assertEquals;
14
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Set;
18 import java.util.UUID;
19
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.unitils.dbunit.annotation.DataSet;
24 import org.unitils.spring.annotation.SpringBeanByType;
25
26 import eu.etaxonomy.cdm.model.taxon.Taxon;
27 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
28 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
29 import eu.etaxonomy.cdm.model.taxon.Classification;
30 import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
31 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
32 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonNodeDao;
33 import eu.etaxonomy.cdm.persistence.dao.taxon.IClassificationDao;
34 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
35
36 public class TaxonNodeDaoHibernateImplTest extends CdmTransactionalIntegrationTest {
37
38 @SpringBeanByType
39 private ITaxonNodeDao taxonNodeDao;
40
41 @SpringBeanByType
42 private IClassificationDao classificationDao;
43
44 @SpringBeanByType
45 private ITaxonDao taxonDao;
46
47 private UUID uuid1;
48 private UUID uuid2;
49 private UUID uuid3;
50
51 @Before
52 public void setUp(){
53 uuid1 = UUID.fromString("20c8f083-5870-4cbd-bf56-c5b2b98ab6a7");
54 uuid2 = UUID.fromString("770239f6-4fa8-496b-8738-fe8f7b2ad519");
55 AuditEventContextHolder.clearContext();
56 }
57
58 @After
59 public void tearDown(){
60 AuditEventContextHolder.clearContext();
61 }
62
63
64 @Test
65 @DataSet
66 public void testInit() {
67 assertNotNull("Instance of ITaxonDao expected",taxonNodeDao);
68 assertNotNull("Instance of IReferenceDao expected",classificationDao);
69 }
70
71 @Test
72 @DataSet
73 public void testFindByUuid() {
74 TaxonNode taxonNode = (TaxonNode) taxonNodeDao.findByUuid(uuid1);
75 Classification.class.getDeclaredConstructors();
76 assertNotNull("findByUuid should return a taxon node", taxonNode);
77 }
78
79 @Test
80 @DataSet
81 public void testClassification() {
82 Classification classification = classificationDao.findByUuid(UUID.fromString("aeee7448-5298-4991-b724-8d5b75a0a7a9"));
83
84 assertNotNull("findByUuid should return a taxon tree", classification);
85 assertNotNull("classification should have a name",classification.getName());
86 assertEquals("classification should have a name which is 'Name'",classification.getName().getText(),"Name");
87 TaxonNode taxNode = (TaxonNode) taxonNodeDao.findByUuid(uuid1);
88 TaxonNode taxNode2 = (TaxonNode) taxonNodeDao.findByUuid(uuid2);
89 Set<TaxonNode> rootNodes = new HashSet<TaxonNode>();
90
91 rootNodes.add(taxNode);
92
93
94 for (TaxonNode rootNode : rootNodes){
95 classification.addChildNode(rootNode, rootNode.getReference(), rootNode.getMicroReference(), rootNode.getSynonymToBeUsed());
96 }
97
98 taxNode.addChildNode(taxNode2, null, null,null);
99
100 Taxon taxon2 = taxNode2.getTaxon();
101 Taxon taxon = taxNode.getTaxon();
102 UUID uuidTaxon = taxon.getUuid();
103 UUID uuidTaxon2 = taxon2.getUuid();
104
105 List<TaxonBase> taxa = taxonDao.getAllTaxonBases(10, 0);
106 assertEquals("there should be only two taxa", 5, taxa.size());
107
108 taxonNodeDao.delete(taxNode2);
109
110 taxa = taxonDao.getAllTaxonBases(10, 0);
111 assertEquals("there should be only one taxon left", 4, taxa.size());
112
113 classificationDao.delete(classification);
114 classification = classificationDao.findByUuid(UUID.fromString("aeee7448-5298-4991-b724-8d5b75a0a7a9"));
115 assertEquals("The tree should be null", null, classification);
116
117
118 }
119 }