Project

General

Profile

Download (11.4 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 ACHERONTIA_LACHESIS = UUID.fromString("bc09aca6-06fd-4905-b1e7-cbf7cc65d783");
69
    private static final UUID NODE_ACHERONTIA_UUID = UUID.fromString("20c8f083-5870-4cbd-bf56-c5b2b98ab6a7");
70

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

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

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

    
92

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

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

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

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

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

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

    
122

    
123

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

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

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

    
144
    }
145

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

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

    
161
    @Test
162
    @DataSet
163
    public void testListSiblings(){
164
        Taxon t_acherontia_lachesis = (Taxon) taxonDao.load(ACHERONTIA_LACHESIS);
165

    
166
        Classification classification =  classificationDao.load(ClassificationUuid);
167
        long count = classificationDao.countSiblingsOf(t_acherontia_lachesis, classification);
168
        assertEquals(2, count);
169
        List<TaxonNode> siblings = classificationDao.listSiblingsOf(t_acherontia_lachesis, classification, null, null, null);
170
        assertNotNull(siblings);
171
        assertEquals(2, siblings.size());
172
    }
173

    
174
    @Test
175
    @DataSet
176
    public void testGetAllTaxaByClassification(){
177
        Classification classification =  classificationDao.load(ClassificationUuid, CLASSIFICATION_INIT_STRATEGY);
178

    
179
        assertNotNull("findByUuid should return a taxon tree", classification);
180
        assertNotNull("classification should have a name",classification.getName());
181
        assertEquals("classification should have a name which is 'Name'",classification.getName().getText(),"Name");
182
        TaxonNode taxNode = taxonNodeDao.load(uuid1,TAXONNODE_INIT_STRATEGY);
183
        TaxonNode taxNode2 = taxonNodeDao.load(uuid2,TAXONNODE_INIT_STRATEGY);
184

    
185
        TaxonNode taxNode3 = taxonNodeDao.load(uuid3, TAXONNODE_INIT_STRATEGY);
186
        Taxon taxon = Taxon.NewInstance(BotanicalName.NewInstance(Rank.GENUS()), null);
187
        Taxon taxon1 = Taxon.NewInstance(BotanicalName.NewInstance(Rank.GENUS()), null);
188
        Taxon taxon2 = Taxon.NewInstance(BotanicalName.NewInstance(Rank.GENUS()), null);
189
        taxNode.addChildTaxon(taxon, null, null);
190
        taxNode2.addChildTaxon(taxon1, null, null);
191
        taxNode3.addChildTaxon(taxon2, null, null);
192

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

    
196

    
197
        taxas = taxonNodeDao.getTaxonOfAcceptedTaxaByClassification(classification, 0, 10);
198
        logger.info(taxas.size());
199
        assertEquals("there should be 7 taxa left", 7, taxas.size());
200

    
201
        int countTaxa = taxonNodeDao.countTaxonOfAcceptedTaxaByClassification(classification);
202
        logger.info(countTaxa);
203
        assertEquals("there should be 7 taxa left", 7, countTaxa);
204
    }
205

    
206
    @Test
207
    @DataSet(value="TaxonNodeDaoHibernateImplTest.testSortindexForJavassist.xml")
208
    @ExpectedDataSet("TaxonNodeDaoHibernateImplTest.testSortindexForJavassist-result.xml")
209

    
210
    //test if TaxonNode.remove(index) works correctly with proxies
211
    public void testSortindexForJavassist(){
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 firstTopLevelTaxon = (Taxon)taxonDao.findByUuid(UUID.fromString("7b8b5cb3-37ba-4dba-91ac-4c6ffd6ac331"));
216
    	Classification classification = classificationDao.findByUuid(ClassificationUuid);
217
    	classification.addParentChild(taxonWithLazyLoadedParentNodeOnTopLevel, firstTopLevelTaxon, null, null);
218
    	commitAndStartNewTransaction( new String[]{"TaxonNode"});
219
    }
220

    
221
    @Test
222
    @DataSet(value="TaxonNodeDaoHibernateImplTest.testSortindexForJavassist.xml")
223
    @ExpectedDataSet("TaxonNodeDaoHibernateImplTest.testSortindexForJavassist2-result.xml")
224
    //test if TaxonNode.addNode(node) works correctly with proxies
225
    public void testSortindexForJavassist2(){
226
    	Taxon taxonWithLazyLoadedParentNodeOnTopLevel = (Taxon)taxonDao.findByUuid(UUID.fromString("bc09aca6-06fd-4905-b1e7-cbf7cc65d783"));
227
    	TaxonNode parent = taxonWithLazyLoadedParentNodeOnTopLevel.getTaxonNodes().iterator().next().getParent();
228
    	Assert.assertTrue("Parent node must be proxy, otherwise test does not work", parent instanceof Proxy);
229
    	Taxon newTaxon = Taxon.NewInstance(null, null);
230
    	Classification classification = classificationDao.findByUuid(ClassificationUuid);
231
    	TaxonNode newNode = classification.addChildTaxon(newTaxon, 0, null, null);
232
    	newNode.setUuid(UUID.fromString("58728644-1155-4520-98f7-309fdb62abd7"));
233
    	commitAndStartNewTransaction( new String[]{"TaxonNode"});
234
    }
235

    
236
    @Test
237
    public void testSaveAndLoadTaxonNodeAgentRelation(){
238
        Classification classification = Classification.NewInstance("Me");
239
        Taxon taxon = Taxon.NewInstance(null, null);
240
        Person person = Person.NewInstance();
241
        TaxonNode node = classification.addChildTaxon(taxon, null, null);
242
        DefinedTerm lastScrutiny = (DefinedTerm)termDao.findByUuid(DefinedTerm.uuidLastScrutiny);
243
        TaxonNodeAgentRelation rel = node.addAgentRelation(lastScrutiny, person);
244
        taxonNodeDao.save(node);
245
        commitAndStartNewTransaction(null);
246

    
247
        TaxonNode newNode = taxonNodeDao.load(node.getUuid());
248
        Assert.assertNotSame(node, newNode);
249
        Assert.assertEquals("Node should have agent relation", 1, newNode.getAgentRelations().size());
250
        TaxonNodeAgentRelation newRel = newNode.getAgentRelations().iterator().next();
251
        Assert.assertEquals(rel, newRel);
252
        Assert.assertEquals(rel.getId(), newRel.getId());
253
        Assert.assertNotSame(rel, newRel);
254
    }
255

    
256

    
257
    @Override
258
    public void createTestDataSet() throws FileNotFoundException {
259
        // TODO Auto-generated method stub
260
    }
261

    
262
}
(4-4/4)