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
|
//test if TaxonNode.remove(index) works correctly with proxies
|
196
|
public void testSortindexForJavassist(){
|
197
|
Taxon taxonWithLazyLoadedParentNodeOnTopLevel = (Taxon)taxonDao.findByUuid(UUID.fromString("bc09aca6-06fd-4905-b1e7-cbf7cc65d783"));
|
198
|
TaxonNode parent = taxonWithLazyLoadedParentNodeOnTopLevel.getTaxonNodes().iterator().next().getParent();
|
199
|
Assert.assertTrue("Parent node must be proxy, otherwise test does not work", parent instanceof Proxy);
|
200
|
Taxon firstTopLevelTaxon = (Taxon)taxonDao.findByUuid(UUID.fromString("7b8b5cb3-37ba-4dba-91ac-4c6ffd6ac331"));
|
201
|
Classification classification = classificationDao.findByUuid(ClassificationUuid);
|
202
|
classification.addParentChild(taxonWithLazyLoadedParentNodeOnTopLevel, firstTopLevelTaxon, null, null);
|
203
|
commitAndStartNewTransaction( new String[]{"TaxonNode"});
|
204
|
}
|
205
|
|
206
|
@Test
|
207
|
@DataSet(value="TaxonNodeDaoHibernateImplTest.testSortindexForJavassist.xml")
|
208
|
@ExpectedDataSet("TaxonNodeDaoHibernateImplTest.testSortindexForJavassist2-result.xml")
|
209
|
//test if TaxonNode.addNode(node) works correctly with proxies
|
210
|
public void testSortindexForJavassist2(){
|
211
|
Taxon taxonWithLazyLoadedParentNodeOnTopLevel = (Taxon)taxonDao.findByUuid(UUID.fromString("bc09aca6-06fd-4905-b1e7-cbf7cc65d783"));
|
212
|
TaxonNode parent = taxonWithLazyLoadedParentNodeOnTopLevel.getTaxonNodes().iterator().next().getParent();
|
213
|
Assert.assertTrue("Parent node must be proxy, otherwise test does not work", parent instanceof Proxy);
|
214
|
Taxon newTaxon = Taxon.NewInstance(null, null);
|
215
|
Classification classification = classificationDao.findByUuid(ClassificationUuid);
|
216
|
TaxonNode newNode = classification.addChildTaxon(newTaxon, 0, null, null);
|
217
|
newNode.setUuid(UUID.fromString("58728644-1155-4520-98f7-309fdb62abd7"));
|
218
|
commitAndStartNewTransaction( new String[]{"TaxonNode"});
|
219
|
}
|
220
|
|
221
|
@Test
|
222
|
public void testSaveAndLoadTaxonNodeAgentRelation(){
|
223
|
Classification classification = Classification.NewInstance("Me");
|
224
|
Taxon taxon = Taxon.NewInstance(null, null);
|
225
|
Person person = Person.NewInstance();
|
226
|
TaxonNode node = classification.addChildTaxon(taxon, null, null);
|
227
|
DefinedTerm lastScrutiny = (DefinedTerm)termDao.findByUuid(DefinedTerm.uuidLastScrutiny);
|
228
|
TaxonNodeAgentRelation rel = node.addAgentRelation(lastScrutiny, person);
|
229
|
taxonNodeDao.save(node);
|
230
|
commitAndStartNewTransaction(null);
|
231
|
|
232
|
TaxonNode newNode = taxonNodeDao.load(node.getUuid());
|
233
|
Assert.assertNotSame(node, newNode);
|
234
|
Assert.assertEquals("Node should have agent relation", 1, newNode.getAgentRelations().size());
|
235
|
TaxonNodeAgentRelation newRel = newNode.getAgentRelations().iterator().next();
|
236
|
Assert.assertEquals(rel, newRel);
|
237
|
Assert.assertEquals(rel.getId(), newRel.getId());
|
238
|
Assert.assertNotSame(rel, newRel);
|
239
|
}
|
240
|
|
241
|
|
242
|
@Override
|
243
|
public void createTestDataSet() throws FileNotFoundException {
|
244
|
// TODO Auto-generated method stub
|
245
|
}
|
246
|
|
247
|
}
|