Project

General

Profile

Download (16.1 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
import static org.junit.Assert.assertNull;
15

    
16
import java.io.FileNotFoundException;
17
import java.util.ArrayList;
18
import java.util.Arrays;
19
import java.util.HashSet;
20
import java.util.List;
21
import java.util.Map;
22
import java.util.Set;
23
import java.util.UUID;
24

    
25
import org.junit.After;
26
import org.junit.Assert;
27
import org.junit.Before;
28
import org.junit.Test;
29
import org.unitils.dbunit.annotation.DataSet;
30
import org.unitils.dbunit.annotation.ExpectedDataSet;
31
import org.unitils.spring.annotation.SpringBeanByType;
32

    
33
import eu.etaxonomy.cdm.model.agent.Person;
34
import eu.etaxonomy.cdm.model.common.CdmBase;
35
import eu.etaxonomy.cdm.model.common.Language;
36
import eu.etaxonomy.cdm.model.common.LanguageString;
37
import eu.etaxonomy.cdm.model.name.Rank;
38
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
39
import eu.etaxonomy.cdm.model.taxon.Classification;
40
import eu.etaxonomy.cdm.model.taxon.Taxon;
41
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
42
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
43
import eu.etaxonomy.cdm.model.taxon.TaxonNodeAgentRelation;
44
import eu.etaxonomy.cdm.model.term.DefinedTerm;
45
import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
46
import eu.etaxonomy.cdm.persistence.dao.taxon.IClassificationDao;
47
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
48
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonNodeDao;
49
import eu.etaxonomy.cdm.persistence.dao.term.IDefinedTermDao;
50
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
51
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
52
import javassist.util.proxy.Proxy;
53

    
54
public class TaxonNodeDaoHibernateImplTest extends CdmTransactionalIntegrationTest {
55

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

    
58
	@SpringBeanByType
59
    private ITaxonNodeDao taxonNodeDao;
60

    
61
    @SpringBeanByType
62
    private IClassificationDao classificationDao;
63

    
64
    @SpringBeanByType
65
    private ITaxonDao taxonDao;
66

    
67
    @SpringBeanByType
68
    private IDefinedTermDao termDao;
69

    
70
    private UUID uuid1;
71
    private UUID uuid2;
72
    private UUID uuid3;
73
    private UUID classificationUuid;
74

    
75
    boolean includeUnpublished;
76

    
77
    private static final UUID ACHERONTIA_UUID = UUID.fromString("3b2b3e17-5c4a-4d1b-aa39-349f63100d6b");
78
    private static final UUID ACHERONTIA_LACHESIS = UUID.fromString("bc09aca6-06fd-4905-b1e7-cbf7cc65d783");
79
    private static final UUID NODE_ACHERONTIA_UUID = UUID.fromString("20c8f083-5870-4cbd-bf56-c5b2b98ab6a7");
80

    
81
    private static final List<String> CLASSIFICATION_INIT_STRATEGY = Arrays.asList(new String[]{
82
            "rootNode"
83
    });
84
    private static final List<String> TAXONNODE_INIT_STRATEGY = Arrays.asList(new String[]{
85
            "taxon",
86
            "childNodes"
87
    });
88

    
89
    @Before
90
    public void setUp(){
91
        uuid1 = UUID.fromString("0b5846e5-b8d2-4ca9-ac51-099286ea4adc");
92
        uuid3 = UUID.fromString("20c8f083-5870-4cbd-bf56-c5b2b98ab6a7");
93
        uuid2 = UUID.fromString("770239f6-4fa8-496b-8738-fe8f7b2ad519");
94
        classificationUuid = UUID.fromString("aeee7448-5298-4991-b724-8d5b75a0a7a9");
95
        AuditEventContextHolder.clearContext();
96
        includeUnpublished = true;
97
    }
98

    
99
    @After
100
    public void tearDown(){
101
        AuditEventContextHolder.clearContext();
102
    }
103

    
104

    
105
    @Test
106
    @DataSet
107
    public void testInit() {
108
        assertNotNull("Instance of ITaxonDao expected",taxonNodeDao);
109
        assertNotNull("Instance of IReferenceDao expected",classificationDao);
110
    }
111

    
112
    @Test
113
    @DataSet
114
    public void testFindByUuid() {
115
        TaxonNode taxonNode = taxonNodeDao.findByUuid(uuid1);
116
        Classification.class.getDeclaredConstructors();
117
        assertNotNull("findByUuid should return a taxon node", taxonNode);
118
    }
119

    
120
    @Test
121
    @DataSet
122
    public void testClassification() {
123

    
124
        Classification classification =  classificationDao.load(ClassificationUuid, CLASSIFICATION_INIT_STRATEGY);
125

    
126
        assertNotNull("findByUuid should return a taxon tree", classification);
127
        assertNotNull("classification should have a name",classification.getName());
128
        assertEquals("classification should have a name which is 'Name'",classification.getName().getText(),"Name");
129
        TaxonNode taxNode = taxonNodeDao.load(uuid1,TAXONNODE_INIT_STRATEGY);
130
        TaxonNode taxNode2 = taxonNodeDao.load(uuid2,TAXONNODE_INIT_STRATEGY);
131

    
132
        TaxonNode taxNode3 = taxonNodeDao.load(uuid3, TAXONNODE_INIT_STRATEGY);
133

    
134

    
135
        @SuppressWarnings("rawtypes")
136
        List<TaxonBase> taxa = taxonDao.list(10, 0);
137
        assertEquals("there should be 7 taxa", 7, taxa.size());
138
        taxNode3 = CdmBase.deproxy(taxNode3);
139
        taxNode = CdmBase.deproxy(taxNode);
140
        taxNode2 = CdmBase.deproxy(taxNode2);
141
        TaxonNode rootNode = CdmBase.deproxy(classification.getRootNode());
142
        TaxonNode newNode = rootNode.addChildTaxon(Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()), null), null, null);
143
        taxonNodeDao.saveOrUpdate(newNode);
144
        taxonNodeDao.delete(taxNode3, true);
145

    
146
        assertNull(taxonNodeDao.findByUuid(taxNode3.getUuid()));
147
        classification = classificationDao.findByUuid(ClassificationUuid);
148

    
149
        taxa = taxonDao.list(10, 0);
150
        // There should be 4 taxonBases: at the beginning 6 in the classification + 1 orphan taxon;
151
        //1 new created taxon -> 8: delete node3 deleted 4 taxa -> 4 taxa left.
152
        assertEquals("there should be 4 taxa left", 4, taxa.size());
153

    
154
        classificationDao.delete(classification);
155
        classification = null;
156

    
157
       // classificationDao.flush();
158
        classification = classificationDao.findByUuid(ClassificationUuid);
159
        assertEquals("The tree should be null", null, classification);
160

    
161
    }
162

    
163
    @Test
164
    @DataSet
165
    public void testListChildren(){
166
        boolean includeUnpublished;
167
        Taxon t_acherontia = (Taxon) taxonDao.load(ACHERONTIA_UUID);
168
        TaxonNode subtree = null;
169
        includeUnpublished = true;
170

    
171
        Classification classification =  classificationDao.load(ClassificationUuid);
172
        List<TaxonNode> children = classificationDao.listChildrenOf(
173
                t_acherontia, classification, subtree, includeUnpublished, null, null, null);
174
        assertNotNull(children);
175
        assertEquals(2, children.size());
176

    
177
        includeUnpublished = false;
178
        children = classificationDao.listChildrenOf(
179
                t_acherontia, classification, subtree, includeUnpublished, null, null, null);
180
        assertNotNull(children);
181
        assertEquals(1, children.size()); //1 is unpublished
182

    
183
        includeUnpublished = true;
184
        TaxonNode t_acherontia_node = taxonNodeDao.load(NODE_ACHERONTIA_UUID);
185
        children =taxonNodeDao.listChildrenOf(t_acherontia_node, null, null, true, includeUnpublished, null);
186
        assertNotNull(children);
187
        assertEquals(3, children.size());
188

    
189
        includeUnpublished = false;
190
        children =taxonNodeDao.listChildrenOf(t_acherontia_node, null, null, true, includeUnpublished, null);
191
        assertNotNull(children);
192
        assertEquals(2, children.size()); //1 is unpublished
193
    }
194

    
195
    @Test
196
    @DataSet
197
    public void testListSiblings(){
198
        Taxon t_acherontia_lachesis = (Taxon) taxonDao.load(ACHERONTIA_LACHESIS);
199

    
200
        Classification classification =  classificationDao.load(ClassificationUuid);
201
        long count = classificationDao.countSiblingsOf(t_acherontia_lachesis, classification, includeUnpublished);
202
        assertEquals(2, count);
203
        List<TaxonNode> siblings = classificationDao.listSiblingsOf(
204
                t_acherontia_lachesis, classification, includeUnpublished, null, null, null);
205
        assertNotNull(siblings);
206
        assertEquals(2, siblings.size());
207
    }
208

    
209
    @Test
210
    @DataSet
211
    public void testGetAllTaxaByClassification(){
212
        Classification classification =  classificationDao.load(ClassificationUuid, CLASSIFICATION_INIT_STRATEGY);
213

    
214
        assertNotNull("findByUuid should return a taxon tree", classification);
215
        assertNotNull("classification should have a name",classification.getName());
216
        assertEquals("classification should have a name which is 'Name'",classification.getName().getText(),"Name");
217
        TaxonNode taxNode = taxonNodeDao.load(uuid1,TAXONNODE_INIT_STRATEGY);
218
        TaxonNode taxNode2 = taxonNodeDao.load(uuid2,TAXONNODE_INIT_STRATEGY);
219

    
220
        TaxonNode taxNode3 = taxonNodeDao.load(uuid3, TAXONNODE_INIT_STRATEGY);
221
        Taxon taxon = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()), null);
222
        Taxon taxon1 = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()), null);
223
        Taxon taxon2 = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()), null);
224
        TaxonNode child = taxNode.addChildTaxon(taxon, null, null);
225
        UUID childUuid = taxonNodeDao.saveOrUpdate(child);
226
        child = taxonNodeDao.load(childUuid);
227
        assertNotNull(child);
228
        child = taxNode2.addChildTaxon(taxon1, null, null);
229
        taxonNodeDao.saveOrUpdate(child);
230
        child = taxNode3.addChildTaxon(taxon2, null, null);
231
        taxonNodeDao.saveOrUpdate(child);
232

    
233
        List<TaxonNode> taxas = taxonNodeDao.getTaxonOfAcceptedTaxaByClassification(classification, null, null);
234
        assertEquals("there should be 7 taxa left", 7, taxas.size());
235
        commitAndStartNewTransaction(null);
236

    
237
        taxas = taxonNodeDao.getTaxonOfAcceptedTaxaByClassification(classification, 0, 10);
238
        logger.info(taxas.size());
239
        assertEquals("there should be 7 taxa left", 7, taxas.size());
240

    
241
        int countTaxa = taxonNodeDao.countTaxonOfAcceptedTaxaByClassification(classification);
242
        logger.info(countTaxa);
243
        assertEquals("there should be 7 taxa left", 7, countTaxa);
244
    }
245

    
246
    @Test
247
    @DataSet(value="TaxonNodeDaoHibernateImplTest.testSortindexForJavassist.xml")
248
    @ExpectedDataSet("TaxonNodeDaoHibernateImplTest.testSortindexForJavassist-result.xml")
249
    //test if TaxonNode.remove(index) works correctly with proxies
250
    public void testSortindexForJavassist(){
251
    	Taxon taxonWithLazyLoadedParentNodeOnTopLevel = (Taxon)taxonDao.findByUuid(UUID.fromString("bc09aca6-06fd-4905-b1e7-cbf7cc65d783"));
252
    	TaxonNode parent = taxonWithLazyLoadedParentNodeOnTopLevel.getTaxonNodes().iterator().next().getParent();
253
    	Assert.assertTrue("Parent node must be proxy, otherwise test does not work", parent instanceof Proxy);
254
    	Taxon firstTopLevelTaxon = (Taxon)taxonDao.findByUuid(UUID.fromString("7b8b5cb3-37ba-4dba-91ac-4c6ffd6ac331"));
255
    	Classification classification = classificationDao.findByUuid(ClassificationUuid);
256
    	TaxonNode childNode = classification.addParentChild(taxonWithLazyLoadedParentNodeOnTopLevel, firstTopLevelTaxon, null, null);
257
    	this.taxonNodeDao.saveOrUpdate(childNode);
258
    	commitAndStartNewTransaction( new String[]{"TaxonNode"});
259
    }
260

    
261
    @Test
262
    @DataSet(value="TaxonNodeDaoHibernateImplTest.testSortindexForJavassist.xml")
263
    @ExpectedDataSet("TaxonNodeDaoHibernateImplTest.testSortindexForJavassist2-result.xml")
264
    //test if TaxonNode.addNode(node) works correctly with proxies
265
    public void testSortindexForJavassist2(){
266
    	Taxon taxonWithLazyLoadedParentNodeOnTopLevel = (Taxon)taxonDao.findByUuid(UUID.fromString("bc09aca6-06fd-4905-b1e7-cbf7cc65d783"));
267
    	TaxonNode parent = taxonWithLazyLoadedParentNodeOnTopLevel.getTaxonNodes().iterator().next().getParent();
268
    	Assert.assertTrue("Parent node must be proxy, otherwise test does not work", parent instanceof Proxy);
269
    	Taxon newTaxon = Taxon.NewInstance(null, null);
270
    	Classification classification = classificationDao.findByUuid(ClassificationUuid);
271
    	TaxonNode newNode = classification.addChildTaxon(newTaxon, 0, null, null);
272
    	newNode.setUuid(UUID.fromString("58728644-1155-4520-98f7-309fdb62abd7"));
273
    	this.taxonNodeDao.saveOrUpdate(newNode);
274
    	commitAndStartNewTransaction( new String[]{"TaxonNode"});
275
    }
276

    
277
    @Test
278
    public void testSaveAndLoadTaxonNodeAgentRelation(){
279
        Classification classification = Classification.NewInstance("Me");
280
        Taxon taxon = Taxon.NewInstance(null, null);
281
        Person person = Person.NewInstance();
282
        TaxonNode node = classification.addChildTaxon(taxon, null, null);
283
        DefinedTerm lastScrutiny = (DefinedTerm)termDao.findByUuid(DefinedTerm.uuidLastScrutiny);
284
        TaxonNodeAgentRelation rel = node.addAgentRelation(lastScrutiny, person);
285
        taxonNodeDao.save(node);
286
        commitAndStartNewTransaction(null);
287

    
288
        TaxonNode newNode = taxonNodeDao.load(node.getUuid());
289
        Assert.assertNotSame(node, newNode);
290
        Assert.assertEquals("Node should have agent relation", 1, newNode.getAgentRelations().size());
291
        TaxonNodeAgentRelation newRel = newNode.getAgentRelations().iterator().next();
292
        Assert.assertEquals(rel, newRel);
293
        Assert.assertEquals(rel.getId(), newRel.getId());
294
        Assert.assertNotSame(rel, newRel);
295
    }
296

    
297
    //see comment 7 in #6199
298
    @Test
299
    @DataSet
300
    public void testPersistExcludedInfos(){
301
        //test read
302
        TaxonNode excludedNode = taxonNodeDao.load(UUID.fromString("4f73adcc-a535-4fbe-a97a-c05ee8b12191"));
303
        Assert.assertTrue("Node should be excluded", excludedNode.isExcluded());
304
        TaxonNode unplacedNode = taxonNodeDao.load(UUID.fromString("20c8f083-5870-4cbd-bf56-c5b2b98ab6a7"));
305
        Assert.assertTrue("Node should be unplaced", unplacedNode.isUnplaced());
306
        TaxonNode notSpecialNode = taxonNodeDao.load(UUID.fromString("770239f6-4fa8-496b-8738-fe8f7b2ad519"));
307
        Assert.assertFalse("Node should be neither excluded nor unplaced", notSpecialNode.isUnplaced() || notSpecialNode.isExcluded());
308

    
309
        //read excluded node
310
        Map<Language, LanguageString> map = excludedNode.getExcludedNote();
311
        Assert.assertEquals(2, map.size());
312
        Set<Integer> langIds = new HashSet<>();
313
        for (Language lang : map.keySet()){
314
            langIds.add(lang.getId());
315
        }
316
        Assert.assertTrue("Excluded note must contain text for language id = 1", langIds.contains(1));
317
        Assert.assertTrue("", langIds.contains(2));
318
    }
319

    
320
    @Test
321
    @DataSet ("TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
322
    public final void testGetTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(){
323
        Classification classification = classificationDao.findByUuid(classificationUuid);
324
        List<UuidAndTitleCache<TaxonNode>> result = taxonNodeDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification,  null, null, true);
325
        assertNotNull(result);
326
        assertEquals(6, result.size());
327

    
328
        //test exclude
329
        UUID excludeUUID = UUID.fromString("a9f42927-e507-4fda-9629-62073a908aae");
330
        List<UUID> excludeUUids = new ArrayList<>();
331
        excludeUUids.add(excludeUUID);
332
        result = taxonNodeDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification,  null, null, false);
333
        assertEquals(5, result.size());
334

    
335
        //test limit
336
        int limit = 2;
337
        result = taxonNodeDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification,  limit, null, false);
338
        assertEquals(2, result.size());
339

    
340
        //test pattern
341
        String pattern = "*Rothschi*";
342
        result = taxonNodeDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, 2, pattern, true);
343
        assertNotNull(result);
344
        assertEquals(1, result.size());
345
        assertEquals("0b5846e5-b8d2-4ca9-ac51-099286ea4adc", result.get(0).getUuid().toString());
346

    
347
        //test pattern
348
        pattern = "*TestBaum*";
349
        result = taxonNodeDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, null, pattern, true);
350
        assertNotNull(result);
351
        assertEquals(1, result.size());
352
        assertEquals("6d6b43aa-3a77-4be5-91d0-00b702fc5d6e", result.get(0).getUuid().toString());
353

    
354
    }
355

    
356
    @Override
357
    public void createTestDataSet() throws FileNotFoundException {}
358

    
359
}
(4-4/5)