Project

General

Profile

Download (17.8 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
package eu.etaxonomy.cdm.persistence.dao.hibernate.taxon;
10

    
11
import static org.junit.Assert.assertEquals;
12
import static org.junit.Assert.assertNotNull;
13
import static org.junit.Assert.assertNull;
14

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

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

    
32
import eu.etaxonomy.cdm.model.agent.Person;
33
import eu.etaxonomy.cdm.model.common.CdmBase;
34
import eu.etaxonomy.cdm.model.common.Language;
35
import eu.etaxonomy.cdm.model.common.LanguageString;
36
import eu.etaxonomy.cdm.model.name.Rank;
37
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
38
import eu.etaxonomy.cdm.model.taxon.Classification;
39
import eu.etaxonomy.cdm.model.taxon.Taxon;
40
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
41
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
42
import eu.etaxonomy.cdm.model.taxon.TaxonNodeAgentRelation;
43
import eu.etaxonomy.cdm.model.term.DefinedTerm;
44
import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
45
import eu.etaxonomy.cdm.persistence.dao.taxon.IClassificationDao;
46
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
47
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonNodeDao;
48
import eu.etaxonomy.cdm.persistence.dao.term.IDefinedTermDao;
49
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
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 final UUID uuid1 = UUID.fromString("0b5846e5-b8d2-4ca9-ac51-099286ea4adc");
71
    private final UUID uuid2 = UUID.fromString("770239f6-4fa8-496b-8738-fe8f7b2ad519");
72
    private final UUID uuid3  = UUID.fromString("20c8f083-5870-4cbd-bf56-c5b2b98ab6a7");
73
    private final UUID classificationUuid = UUID.fromString("aeee7448-5298-4991-b724-8d5b75a0a7a9");
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
        AuditEventContextHolder.clearContext();
92
        includeUnpublished = true;
93
    }
94

    
95
    @After
96
    public void tearDown(){
97
        AuditEventContextHolder.clearContext();
98
    }
99

    
100
    @Test
101
    @DataSet
102
    public void testInit() {
103
        assertNotNull("Instance of ITaxonDao expected",taxonNodeDao);
104
        assertNotNull("Instance of IReferenceDao expected",classificationDao);
105
    }
106

    
107
    @Test
108
    @DataSet
109
    public void testFindByUuid() {
110
        TaxonNode taxonNode = taxonNodeDao.findByUuid(uuid1);
111
        Classification.class.getDeclaredConstructors();
112
        assertNotNull("findByUuid should return a taxon node", taxonNode);
113
    }
114

    
115
    @Test
116
    @DataSet
117
    public void testClassification() {
118

    
119
        Classification classification =  classificationDao.load(ClassificationUuid, CLASSIFICATION_INIT_STRATEGY);
120

    
121
        assertNotNull("findByUuid should return a taxon tree", classification);
122
        assertNotNull("classification should have a name",classification.getName());
123
        assertEquals("classification should have a name which is 'Name'",classification.getName().getText(),"Name");
124
        TaxonNode taxNode = taxonNodeDao.load(uuid1,TAXONNODE_INIT_STRATEGY);
125
        TaxonNode taxNode2 = taxonNodeDao.load(uuid2,TAXONNODE_INIT_STRATEGY);
126

    
127
        TaxonNode taxNode3 = taxonNodeDao.load(uuid3, TAXONNODE_INIT_STRATEGY);
128

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

    
140
        assertNull(taxonNodeDao.findByUuid(taxNode3.getUuid()));
141
        classification = classificationDao.findByUuid(ClassificationUuid);
142

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

    
148
        classificationDao.delete(classification);
149
        classification = null;
150

    
151
       // classificationDao.flush();
152
        classification = classificationDao.findByUuid(ClassificationUuid);
153
        assertEquals("The tree should be null", null, classification);
154
    }
155

    
156
    @Test
157
    @DataSet
158
    public void testListChildren(){
159
        boolean includeUnpublished;
160
        Taxon t_acherontia = (Taxon) taxonDao.load(ACHERONTIA_UUID);
161
        TaxonNode subtree = null;
162
        includeUnpublished = true;
163

    
164
        Classification classification =  classificationDao.load(ClassificationUuid);
165
        List<TaxonNode> children = classificationDao.listChildrenOf(
166
                t_acherontia, classification, subtree, includeUnpublished, null, null, null);
167
        assertNotNull(children);
168
        assertEquals(2, children.size());
169

    
170
        includeUnpublished = false;
171
        children = classificationDao.listChildrenOf(
172
                t_acherontia, classification, subtree, includeUnpublished, null, null, null);
173
        assertNotNull(children);
174
        assertEquals(1, children.size()); //1 is unpublished
175

    
176
        includeUnpublished = true;
177
        TaxonNode t_acherontia_node = taxonNodeDao.load(NODE_ACHERONTIA_UUID);
178
        children =taxonNodeDao.listChildrenOf(t_acherontia_node, null, null, true, includeUnpublished, null);
179
        assertNotNull(children);
180
        assertEquals(3, children.size());
181

    
182
        includeUnpublished = false;
183
        children =taxonNodeDao.listChildrenOf(t_acherontia_node, null, null, true, includeUnpublished, null);
184
        assertNotNull(children);
185
        assertEquals(2, children.size()); //1 is unpublished
186
    }
187

    
188
    @Test
189
    @DataSet
190
    public void testListSiblings(){
191
        Taxon t_acherontia_lachesis = (Taxon) taxonDao.load(ACHERONTIA_LACHESIS);
192

    
193
        Classification classification =  classificationDao.load(ClassificationUuid);
194
        long count = classificationDao.countSiblingsOf(t_acherontia_lachesis, classification, includeUnpublished);
195
        assertEquals(2, count);
196
        List<TaxonNode> siblings = classificationDao.listSiblingsOf(
197
                t_acherontia_lachesis, classification, includeUnpublished, null, null, null);
198
        assertNotNull(siblings);
199
        assertEquals(2, siblings.size());
200
    }
201

    
202
    @Test
203
    @DataSet
204
    public void testGetAllTaxaByClassification(){
205
        Classification classification =  classificationDao.load(ClassificationUuid, CLASSIFICATION_INIT_STRATEGY);
206

    
207
        assertNotNull("findByUuid should return a taxon tree", classification);
208
        assertNotNull("classification should have a name",classification.getName());
209
        assertEquals("classification should have a name which is 'Name'",classification.getName().getText(),"Name");
210
        TaxonNode taxNode = taxonNodeDao.load(uuid1,TAXONNODE_INIT_STRATEGY);
211
        TaxonNode taxNode2 = taxonNodeDao.load(uuid2,TAXONNODE_INIT_STRATEGY);
212

    
213
        TaxonNode taxNode3 = taxonNodeDao.load(uuid3, TAXONNODE_INIT_STRATEGY);
214
        Taxon taxon = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()), null);
215
        Taxon taxon1 = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()), null);
216
        Taxon taxon2 = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()), null);
217
        TaxonNode child = taxNode.addChildTaxon(taxon, null, null);
218
        UUID childUuid = taxonNodeDao.saveOrUpdate(child);
219
        child = taxonNodeDao.load(childUuid);
220
        assertNotNull(child);
221
        child = taxNode2.addChildTaxon(taxon1, null, null);
222
        taxonNodeDao.saveOrUpdate(child);
223
        child = taxNode3.addChildTaxon(taxon2, null, null);
224
        taxonNodeDao.saveOrUpdate(child);
225

    
226
        List<TaxonNode> taxas = taxonNodeDao.getTaxonOfAcceptedTaxaByClassification(classification, null, null);
227
        assertEquals("there should be 7 taxa left", 7, taxas.size());
228
        commitAndStartNewTransaction(null);
229

    
230
        taxas = taxonNodeDao.getTaxonOfAcceptedTaxaByClassification(classification, 0, 10);
231
        logger.info(taxas.size());
232
        assertEquals("there should be 7 taxa left", 7, taxas.size());
233

    
234
        int countTaxa = taxonNodeDao.countTaxonOfAcceptedTaxaByClassification(classification);
235
        logger.info(countTaxa);
236
        assertEquals("there should be 7 taxa left", 7, countTaxa);
237
    }
238

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

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

    
270
    @Test
271
    public void testSaveAndLoadTaxonNodeAgentRelation(){
272
        Classification classification = Classification.NewInstance("Me");
273
        Taxon taxon = Taxon.NewInstance(null, null);
274
        Person person = Person.NewInstance();
275
        TaxonNode node = classification.addChildTaxon(taxon, null, null);
276
        DefinedTerm lastScrutiny = (DefinedTerm)termDao.findByUuid(DefinedTerm.uuidLastScrutiny);
277
        TaxonNodeAgentRelation rel = node.addAgentRelation(lastScrutiny, person);
278
        taxonNodeDao.save(node);
279
        commitAndStartNewTransaction(null);
280

    
281
        TaxonNode newNode = taxonNodeDao.load(node.getUuid());
282
        Assert.assertNotSame(node, newNode);
283
        Assert.assertEquals("Node should have agent relation", 1, newNode.getAgentRelations().size());
284
        TaxonNodeAgentRelation newRel = newNode.getAgentRelations().iterator().next();
285
        Assert.assertEquals(rel, newRel);
286
        Assert.assertEquals(rel.getId(), newRel.getId());
287
        Assert.assertNotSame(rel, newRel);
288
    }
289

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

    
302
        //read excluded node
303
        Map<Language, LanguageString> map = excludedNode.getStatusNote();
304
        Assert.assertEquals(2, map.size());
305
        Set<Integer> langIds = new HashSet<>();
306
        for (Language lang : map.keySet()){
307
            langIds.add(lang.getId());
308
        }
309
        Assert.assertTrue("Excluded note must contain text for language id = 1", langIds.contains(1));
310
        Assert.assertTrue("", langIds.contains(2));
311
    }
312

    
313
    @Test
314
    @DataSet ("TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
315
    public final void testGetTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(){
316
        Classification classification = classificationDao.findByUuid(classificationUuid);
317
        List<UuidAndTitleCache<TaxonNode>> result = taxonNodeDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification,  null, null, true);
318
        assertNotNull(result);
319
        assertEquals(6, result.size());
320

    
321
        //test exclude
322
        UUID excludeUUID = UUID.fromString("a9f42927-e507-4fda-9629-62073a908aae");
323
        List<UUID> excludeUUids = new ArrayList<>();
324
        excludeUUids.add(excludeUUID);
325
        result = taxonNodeDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification,  null, null, false);
326
        assertEquals(5, result.size());
327

    
328
        //test limit
329
        int limit = 2;
330
        result = taxonNodeDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification,  limit, null, false);
331
        assertEquals(2, result.size());
332

    
333
        //test pattern
334
        String pattern = "*Rothschi*";
335
        result = taxonNodeDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, 2, pattern, true);
336
        assertNotNull(result);
337
        assertEquals(1, result.size());
338
        assertEquals("0b5846e5-b8d2-4ca9-ac51-099286ea4adc", result.get(0).getUuid().toString());
339

    
340
        //test pattern
341
        pattern = "*TestBaum*";
342
        result = taxonNodeDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, null, pattern, true);
343
        assertNotNull(result);
344
        assertEquals(1, result.size());
345
        assertEquals("6d6b43aa-3a77-4be5-91d0-00b702fc5d6e", result.get(0).getUuid().toString());
346

    
347

    
348

    
349
    }
350
    @Test
351
    @DataSet ("TaxonNodeDaoHibernateImplTest.findWithoutRank.xml")
352
    public final void testGetTaxonNodeUuidAndTitleCacheOfacceptedTaxaByClassificationForNameWithoutRank(){
353
        //test name without rank
354
        Classification classification = classificationDao.findByUuid(ClassificationUuid);
355
        String pattern = "Acherontia kohlbeckeri*";
356
        List<UuidAndTitleCache<TaxonNode>> result = taxonNodeDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, null, pattern, true);
357
        assertNotNull(result);
358
        assertEquals(1, result.size());
359
        assertEquals("4f73adcc-a535-4fbe-a97a-c05ee8b12191", result.get(0).getUuid().toString()); // titleCache:Acherontia kohlbeckeri rank: Unknown Rank
360
    }
361

    
362
    @Test
363
    @DataSet ("TaxonNodeDaoHibernateImplTest.findWithoutRank.xml")
364
    public final void testGetTaxonNodeDtoWithoutRank(){
365

    
366
        List<TaxonNodeDto> result = taxonNodeDao.getTaxonNodeDto(null, "", null); // cant use "*" here since this is not supported by the method under test
367
        assertNotNull(result);
368
        assertEquals(4, result.size());
369
        assertEquals("20c8f083-5870-4cbd-bf56-c5b2b98ab6a7", result.get(0).getUuid().toString()); // Acherontia(Fabricius, 1798) rank: Genus
370
        assertEquals("0b5846e5-b8d2-4ca9-ac51-099286ea4adc", result.get(1).getUuid().toString()); // titleCache:Acherontia lachesis (Fabricius, 1798) rank: Species
371
        assertEquals("770239f6-4fa8-496b-8738-fe8f7b2ad519", result.get(2).getUuid().toString()); // titleCache:Acherontia styx Westwood, 1847 sec. cate-sphingidae.org rank: Species
372
        assertEquals("4f73adcc-a535-4fbe-a97a-c05ee8b12191", result.get(3).getUuid().toString()); // titleCache:Acherontia kohlbeckeri rank: Unknown Rank
373
    }
374

    
375
    @Override
376
    public void createTestDataSet() throws FileNotFoundException {}
377

    
378
}
(4-4/5)