Project

General

Profile

Download (10.7 KB) Statistics
| Branch: | Tag: | Revision:
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.assertNotNull;
14

    
15
import java.io.FileNotFoundException;
16
import java.util.Arrays;
17
import java.util.List;
18
import java.util.UUID;
19

    
20
import javassist.util.proxy.Proxy;
21

    
22
import org.junit.After;
23
import org.junit.Assert;
24
import org.junit.Before;
25
import org.junit.Test;
26
import org.unitils.dbunit.annotation.DataSet;
27
import org.unitils.dbunit.annotation.ExpectedDataSet;
28
import org.unitils.spring.annotation.SpringBeanByType;
29

    
30
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
31
import eu.etaxonomy.cdm.model.agent.Person;
32
import eu.etaxonomy.cdm.model.common.DefinedTerm;
33
import eu.etaxonomy.cdm.model.name.BotanicalName;
34
import eu.etaxonomy.cdm.model.name.Rank;
35
import eu.etaxonomy.cdm.model.taxon.Classification;
36
import eu.etaxonomy.cdm.model.taxon.Taxon;
37
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
38
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
39
import eu.etaxonomy.cdm.model.taxon.TaxonNodeAgentRelation;
40
import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
41
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
42
import eu.etaxonomy.cdm.persistence.dao.taxon.IClassificationDao;
43
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
44
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonNodeDao;
45
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
46

    
47
public class TaxonNodeDaoHibernateImplTest extends CdmTransactionalIntegrationTest {
48

    
49
    private static final UUID ClassificationUuid = UUID.fromString("aeee7448-5298-4991-b724-8d5b75a0a7a9");
50

    
51
	@SpringBeanByType
52
    private ITaxonNodeDao taxonNodeDao;
53

    
54
    @SpringBeanByType
55
    private IClassificationDao classificationDao;
56

    
57
    @SpringBeanByType
58
    private ITaxonDao taxonDao;
59

    
60
    @SpringBeanByType
61
    private IDefinedTermDao termDao;
62

    
63
    private UUID uuid1;
64
    private UUID uuid2;
65
    private UUID uuid3;
66

    
67
    private static final UUID ACHERONTIA_UUID = UUID.fromString("3b2b3e17-5c4a-4d1b-aa39-349f63100d6b");
68
    private static final UUID NODE_ACHERONTIA_UUID = UUID.fromString("20c8f083-5870-4cbd-bf56-c5b2b98ab6a7");
69

    
70
    private static final List<String> CLASSIFICATION_INIT_STRATEGY = Arrays.asList(new String[]{
71
            "rootNode"
72
    });
73
    private static final List<String> TAXONNODE_INIT_STRATEGY = Arrays.asList(new String[]{
74
            "taxon",
75
            "childNodes"
76
    });
77

    
78
    @Before
79
    public void setUp(){
80
        uuid1 = UUID.fromString("0b5846e5-b8d2-4ca9-ac51-099286ea4adc");
81
        uuid3 = UUID.fromString("20c8f083-5870-4cbd-bf56-c5b2b98ab6a7");
82
        uuid2 = UUID.fromString("770239f6-4fa8-496b-8738-fe8f7b2ad519");
83
        AuditEventContextHolder.clearContext();
84
    }
85

    
86
    @After
87
    public void tearDown(){
88
        AuditEventContextHolder.clearContext();
89
    }
90

    
91

    
92
    @Test
93
    @DataSet
94
    public void testInit() {
95
        assertNotNull("Instance of ITaxonDao expected",taxonNodeDao);
96
        assertNotNull("Instance of IReferenceDao expected",classificationDao);
97
    }
98

    
99
    @Test
100
    @DataSet
101
    public void testFindByUuid() {
102
        TaxonNode taxonNode = taxonNodeDao.findByUuid(uuid1);
103
        Classification.class.getDeclaredConstructors();
104
        assertNotNull("findByUuid should return a taxon node", taxonNode);
105
    }
106

    
107
    @Test
108
    @DataSet
109
    public void testClassification() {
110

    
111
        Classification classification =  classificationDao.load(ClassificationUuid, CLASSIFICATION_INIT_STRATEGY);
112

    
113
        assertNotNull("findByUuid should return a taxon tree", classification);
114
        assertNotNull("classification should have a name",classification.getName());
115
        assertEquals("classification should have a name which is 'Name'",classification.getName().getText(),"Name");
116
        TaxonNode taxNode = taxonNodeDao.load(uuid1,TAXONNODE_INIT_STRATEGY);
117
        TaxonNode taxNode2 = taxonNodeDao.load(uuid2,TAXONNODE_INIT_STRATEGY);
118

    
119
        TaxonNode taxNode3 = taxonNodeDao.load(uuid3, TAXONNODE_INIT_STRATEGY);
120

    
121

    
122

    
123
        List<TaxonBase> taxa = taxonDao.getAllTaxonBases(10, 0);
124
        assertEquals("there should be 7 taxa", 7, taxa.size());
125
        taxNode3 = HibernateProxyHelper.deproxy(taxNode3, TaxonNode.class);
126
        taxNode = HibernateProxyHelper.deproxy(taxNode, TaxonNode.class);
127
        taxNode2 = HibernateProxyHelper.deproxy(taxNode2, TaxonNode.class);
128
        TaxonNode rootNode = HibernateProxyHelper.deproxy(classification.getRootNode(), TaxonNode.class);
129
        rootNode.addChildTaxon(Taxon.NewInstance(BotanicalName.NewInstance(Rank.GENUS()), null), null, null);
130
        taxonNodeDao.delete(taxNode3, true);
131
        classification = classificationDao.findByUuid(ClassificationUuid);
132

    
133
        taxa = taxonDao.getAllTaxonBases(10, 0);
134
        assertEquals("there should be 7 taxa left", 7, taxa.size());
135
        taxonNodeDao.flush();
136
        classificationDao.delete(classification);
137
        classification = null;
138

    
139
        classificationDao.flush();
140
        classification = classificationDao.findByUuid(ClassificationUuid);
141
        assertEquals("The tree should be null", null, classification);
142

    
143
    }
144

    
145
    @Test
146
    @DataSet
147
    public void testListChildren(){
148
        Taxon t_acherontia = (Taxon) taxonDao.load(ACHERONTIA_UUID);
149

    
150
        Classification classification =  classificationDao.load(ClassificationUuid);
151
        List<TaxonNode> children = classificationDao.listChildrenOf(t_acherontia, classification, null, null, null);
152
        assertNotNull(children);
153
        assertEquals(2, children.size());
154
        TaxonNode t_acherontia_node = taxonNodeDao.load(NODE_ACHERONTIA_UUID);
155
        children =taxonNodeDao.listChildrenOf(t_acherontia_node, null, null, null, true);
156
        assertNotNull(children);
157
        assertEquals(3, children.size());
158
    }
159

    
160
    @Test
161
    @DataSet
162
    public void testGetAllTaxaByClassification(){
163
        Classification classification =  classificationDao.load(ClassificationUuid, CLASSIFICATION_INIT_STRATEGY);
164

    
165
        assertNotNull("findByUuid should return a taxon tree", classification);
166
        assertNotNull("classification should have a name",classification.getName());
167
        assertEquals("classification should have a name which is 'Name'",classification.getName().getText(),"Name");
168
        TaxonNode taxNode = taxonNodeDao.load(uuid1,TAXONNODE_INIT_STRATEGY);
169
        TaxonNode taxNode2 = taxonNodeDao.load(uuid2,TAXONNODE_INIT_STRATEGY);
170

    
171
        TaxonNode taxNode3 = taxonNodeDao.load(uuid3, TAXONNODE_INIT_STRATEGY);
172
        Taxon taxon = Taxon.NewInstance(BotanicalName.NewInstance(Rank.GENUS()), null);
173
        Taxon taxon1 = Taxon.NewInstance(BotanicalName.NewInstance(Rank.GENUS()), null);
174
        Taxon taxon2 = Taxon.NewInstance(BotanicalName.NewInstance(Rank.GENUS()), null);
175
        taxNode.addChildTaxon(taxon, null, null);
176
        taxNode2.addChildTaxon(taxon1, null, null);
177
        taxNode3.addChildTaxon(taxon2, null, null);
178

    
179
        List<TaxonNode> taxas = taxonNodeDao.getTaxonOfAcceptedTaxaByClassification(classification, null, null);
180
        assertEquals("there should be 7 taxa left", 7, taxas.size());
181

    
182

    
183
        taxas = taxonNodeDao.getTaxonOfAcceptedTaxaByClassification(classification, 0, 10);
184
        logger.info(taxas.size());
185
        assertEquals("there should be 7 taxa left", 7, taxas.size());
186

    
187
        int countTaxa = taxonNodeDao.countTaxonOfAcceptedTaxaByClassification(classification);
188
        logger.info(countTaxa);
189
        assertEquals("there should be 7 taxa left", 7, countTaxa);
190
    }
191

    
192
    @Test
193
    @DataSet(value="TaxonNodeDaoHibernateImplTest.testSortindexForJavassist.xml")
194
    @ExpectedDataSet("TaxonNodeDaoHibernateImplTest.testSortindexForJavassist-result.xml")
195

    
196
    //test if TaxonNode.remove(index) works correctly with proxies
197
    public void testSortindexForJavassist(){
198
    	Taxon taxonWithLazyLoadedParentNodeOnTopLevel = (Taxon)taxonDao.findByUuid(UUID.fromString("bc09aca6-06fd-4905-b1e7-cbf7cc65d783"));
199
    	TaxonNode parent = taxonWithLazyLoadedParentNodeOnTopLevel.getTaxonNodes().iterator().next().getParent();
200
    	Assert.assertTrue("Parent node must be proxy, otherwise test does not work", parent instanceof Proxy);
201
    	Taxon firstTopLevelTaxon = (Taxon)taxonDao.findByUuid(UUID.fromString("7b8b5cb3-37ba-4dba-91ac-4c6ffd6ac331"));
202
    	Classification classification = classificationDao.findByUuid(ClassificationUuid);
203
    	classification.addParentChild(taxonWithLazyLoadedParentNodeOnTopLevel, firstTopLevelTaxon, null, null);
204
    	commitAndStartNewTransaction( new String[]{"TaxonNode"});
205
    }
206

    
207
    @Test
208
    @DataSet(value="TaxonNodeDaoHibernateImplTest.testSortindexForJavassist.xml")
209
    @ExpectedDataSet("TaxonNodeDaoHibernateImplTest.testSortindexForJavassist2-result.xml")
210
    //test if TaxonNode.addNode(node) works correctly with proxies
211
    public void testSortindexForJavassist2(){
212
    	Taxon taxonWithLazyLoadedParentNodeOnTopLevel = (Taxon)taxonDao.findByUuid(UUID.fromString("bc09aca6-06fd-4905-b1e7-cbf7cc65d783"));
213
    	TaxonNode parent = taxonWithLazyLoadedParentNodeOnTopLevel.getTaxonNodes().iterator().next().getParent();
214
    	Assert.assertTrue("Parent node must be proxy, otherwise test does not work", parent instanceof Proxy);
215
    	Taxon newTaxon = Taxon.NewInstance(null, null);
216
    	Classification classification = classificationDao.findByUuid(ClassificationUuid);
217
    	TaxonNode newNode = classification.addChildTaxon(newTaxon, 0, null, null);
218
    	newNode.setUuid(UUID.fromString("58728644-1155-4520-98f7-309fdb62abd7"));
219
    	commitAndStartNewTransaction( new String[]{"TaxonNode"});
220
    }
221

    
222
    @Test
223
    public void testSaveAndLoadTaxonNodeAgentRelation(){
224
        Classification classification = Classification.NewInstance("Me");
225
        Taxon taxon = Taxon.NewInstance(null, null);
226
        Person person = Person.NewInstance();
227
        TaxonNode node = classification.addChildTaxon(taxon, null, null);
228
        DefinedTerm lastScrutiny = (DefinedTerm)termDao.findByUuid(DefinedTerm.uuidLastScrutiny);
229
        TaxonNodeAgentRelation rel = node.addAgentRelation(lastScrutiny, person);
230
        taxonNodeDao.save(node);
231
        commitAndStartNewTransaction(null);
232

    
233
        TaxonNode newNode = taxonNodeDao.load(node.getUuid());
234
        Assert.assertNotSame(node, newNode);
235
        Assert.assertEquals("Node should have agent relation", 1, newNode.getAgentRelations().size());
236
        TaxonNodeAgentRelation newRel = newNode.getAgentRelations().iterator().next();
237
        Assert.assertEquals(rel, newRel);
238
        Assert.assertEquals(rel.getId(), newRel.getId());
239
        Assert.assertNotSame(rel, newRel);
240
    }
241

    
242

    
243
    @Override
244
    public void createTestDataSet() throws FileNotFoundException {
245
        // TODO Auto-generated method stub
246
    }
247

    
248
}
(4-4/4)