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.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.hibernate.HibernateProxyHelper;
|
33
|
import eu.etaxonomy.cdm.model.agent.Person;
|
34
|
import eu.etaxonomy.cdm.model.common.DefinedTerm;
|
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.view.context.AuditEventContextHolder;
|
45
|
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
|
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.test.integration.CdmTransactionalIntegrationTest;
|
50
|
import javassist.util.proxy.Proxy;
|
51
|
|
52
|
public class TaxonNodeDaoHibernateImplTest extends CdmTransactionalIntegrationTest {
|
53
|
|
54
|
private static final UUID ClassificationUuid = UUID.fromString("aeee7448-5298-4991-b724-8d5b75a0a7a9");
|
55
|
|
56
|
@SpringBeanByType
|
57
|
private ITaxonNodeDao taxonNodeDao;
|
58
|
|
59
|
@SpringBeanByType
|
60
|
private IClassificationDao classificationDao;
|
61
|
|
62
|
@SpringBeanByType
|
63
|
private ITaxonDao taxonDao;
|
64
|
|
65
|
@SpringBeanByType
|
66
|
private IDefinedTermDao termDao;
|
67
|
|
68
|
private UUID uuid1;
|
69
|
private UUID uuid2;
|
70
|
private UUID uuid3;
|
71
|
|
72
|
private static final UUID ACHERONTIA_UUID = UUID.fromString("3b2b3e17-5c4a-4d1b-aa39-349f63100d6b");
|
73
|
private static final UUID ACHERONTIA_LACHESIS = UUID.fromString("bc09aca6-06fd-4905-b1e7-cbf7cc65d783");
|
74
|
private static final UUID NODE_ACHERONTIA_UUID = UUID.fromString("20c8f083-5870-4cbd-bf56-c5b2b98ab6a7");
|
75
|
|
76
|
private static final List<String> CLASSIFICATION_INIT_STRATEGY = Arrays.asList(new String[]{
|
77
|
"rootNode"
|
78
|
});
|
79
|
private static final List<String> TAXONNODE_INIT_STRATEGY = Arrays.asList(new String[]{
|
80
|
"taxon",
|
81
|
"childNodes"
|
82
|
});
|
83
|
|
84
|
@Before
|
85
|
public void setUp(){
|
86
|
uuid1 = UUID.fromString("0b5846e5-b8d2-4ca9-ac51-099286ea4adc");
|
87
|
uuid3 = UUID.fromString("20c8f083-5870-4cbd-bf56-c5b2b98ab6a7");
|
88
|
uuid2 = UUID.fromString("770239f6-4fa8-496b-8738-fe8f7b2ad519");
|
89
|
AuditEventContextHolder.clearContext();
|
90
|
}
|
91
|
|
92
|
@After
|
93
|
public void tearDown(){
|
94
|
AuditEventContextHolder.clearContext();
|
95
|
}
|
96
|
|
97
|
|
98
|
@Test
|
99
|
@DataSet
|
100
|
public void testInit() {
|
101
|
assertNotNull("Instance of ITaxonDao expected",taxonNodeDao);
|
102
|
assertNotNull("Instance of IReferenceDao expected",classificationDao);
|
103
|
}
|
104
|
|
105
|
@Test
|
106
|
@DataSet
|
107
|
public void testFindByUuid() {
|
108
|
TaxonNode taxonNode = taxonNodeDao.findByUuid(uuid1);
|
109
|
Classification.class.getDeclaredConstructors();
|
110
|
assertNotNull("findByUuid should return a taxon node", taxonNode);
|
111
|
}
|
112
|
|
113
|
@Test
|
114
|
@DataSet
|
115
|
public void testClassification() {
|
116
|
|
117
|
Classification classification = classificationDao.load(ClassificationUuid, CLASSIFICATION_INIT_STRATEGY);
|
118
|
|
119
|
assertNotNull("findByUuid should return a taxon tree", classification);
|
120
|
assertNotNull("classification should have a name",classification.getName());
|
121
|
assertEquals("classification should have a name which is 'Name'",classification.getName().getText(),"Name");
|
122
|
TaxonNode taxNode = taxonNodeDao.load(uuid1,TAXONNODE_INIT_STRATEGY);
|
123
|
TaxonNode taxNode2 = taxonNodeDao.load(uuid2,TAXONNODE_INIT_STRATEGY);
|
124
|
|
125
|
TaxonNode taxNode3 = taxonNodeDao.load(uuid3, TAXONNODE_INIT_STRATEGY);
|
126
|
|
127
|
|
128
|
|
129
|
List<TaxonBase> taxa = taxonDao.getAllTaxonBases(10, 0);
|
130
|
assertEquals("there should be 7 taxa", 7, taxa.size());
|
131
|
taxNode3 = HibernateProxyHelper.deproxy(taxNode3, TaxonNode.class);
|
132
|
taxNode = HibernateProxyHelper.deproxy(taxNode, TaxonNode.class);
|
133
|
taxNode2 = HibernateProxyHelper.deproxy(taxNode2, TaxonNode.class);
|
134
|
TaxonNode rootNode = HibernateProxyHelper.deproxy(classification.getRootNode(), TaxonNode.class);
|
135
|
TaxonNode newNode = rootNode.addChildTaxon(Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()), null), null, null);
|
136
|
taxonNodeDao.saveOrUpdate(newNode);
|
137
|
taxonNodeDao.delete(taxNode3, true);
|
138
|
|
139
|
assertNull(taxonNodeDao.findByUuid(taxNode3.getUuid()));
|
140
|
classification = classificationDao.findByUuid(ClassificationUuid);
|
141
|
|
142
|
taxa = taxonDao.getAllTaxonBases(10, 0);
|
143
|
// There should be 4 taxonBases: at the beginning 6 in the classification + 1 orphan taxon; 1 new created taxon -> 8: delete node3 deleted 4 taxa -> 4 taxa left.
|
144
|
assertEquals("there should be 4 taxa left", 4, taxa.size());
|
145
|
|
146
|
classificationDao.delete(classification);
|
147
|
classification = null;
|
148
|
|
149
|
// classificationDao.flush();
|
150
|
classification = classificationDao.findByUuid(ClassificationUuid);
|
151
|
assertEquals("The tree should be null", null, classification);
|
152
|
|
153
|
}
|
154
|
|
155
|
@Test
|
156
|
@DataSet
|
157
|
public void testListChildren(){
|
158
|
Taxon t_acherontia = (Taxon) taxonDao.load(ACHERONTIA_UUID);
|
159
|
|
160
|
Classification classification = classificationDao.load(ClassificationUuid);
|
161
|
List<TaxonNode> children = classificationDao.listChildrenOf(t_acherontia, classification, null, null, null);
|
162
|
assertNotNull(children);
|
163
|
assertEquals(2, children.size());
|
164
|
TaxonNode t_acherontia_node = taxonNodeDao.load(NODE_ACHERONTIA_UUID);
|
165
|
children =taxonNodeDao.listChildrenOf(t_acherontia_node, null, null, null, true);
|
166
|
assertNotNull(children);
|
167
|
assertEquals(3, children.size());
|
168
|
}
|
169
|
|
170
|
@Test
|
171
|
@DataSet
|
172
|
public void testListSiblings(){
|
173
|
Taxon t_acherontia_lachesis = (Taxon) taxonDao.load(ACHERONTIA_LACHESIS);
|
174
|
|
175
|
Classification classification = classificationDao.load(ClassificationUuid);
|
176
|
long count = classificationDao.countSiblingsOf(t_acherontia_lachesis, classification);
|
177
|
assertEquals(2, count);
|
178
|
List<TaxonNode> siblings = classificationDao.listSiblingsOf(t_acherontia_lachesis, classification, null, null, null);
|
179
|
assertNotNull(siblings);
|
180
|
assertEquals(2, siblings.size());
|
181
|
}
|
182
|
|
183
|
@Test
|
184
|
@DataSet
|
185
|
public void testGetAllTaxaByClassification(){
|
186
|
Classification classification = classificationDao.load(ClassificationUuid, CLASSIFICATION_INIT_STRATEGY);
|
187
|
|
188
|
assertNotNull("findByUuid should return a taxon tree", classification);
|
189
|
assertNotNull("classification should have a name",classification.getName());
|
190
|
assertEquals("classification should have a name which is 'Name'",classification.getName().getText(),"Name");
|
191
|
TaxonNode taxNode = taxonNodeDao.load(uuid1,TAXONNODE_INIT_STRATEGY);
|
192
|
TaxonNode taxNode2 = taxonNodeDao.load(uuid2,TAXONNODE_INIT_STRATEGY);
|
193
|
|
194
|
TaxonNode taxNode3 = taxonNodeDao.load(uuid3, TAXONNODE_INIT_STRATEGY);
|
195
|
Taxon taxon = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()), null);
|
196
|
Taxon taxon1 = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()), null);
|
197
|
Taxon taxon2 = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()), null);
|
198
|
TaxonNode child = taxNode.addChildTaxon(taxon, null, null);
|
199
|
UUID childUuid = taxonNodeDao.saveOrUpdate(child);
|
200
|
child = taxonNodeDao.load(childUuid);
|
201
|
assertNotNull(child);
|
202
|
child = taxNode2.addChildTaxon(taxon1, null, null);
|
203
|
taxonNodeDao.saveOrUpdate(child);
|
204
|
child = taxNode3.addChildTaxon(taxon2, null, null);
|
205
|
taxonNodeDao.saveOrUpdate(child);
|
206
|
|
207
|
List<TaxonNode> taxas = taxonNodeDao.getTaxonOfAcceptedTaxaByClassification(classification, null, null);
|
208
|
assertEquals("there should be 7 taxa left", 7, taxas.size());
|
209
|
commitAndStartNewTransaction(null);
|
210
|
|
211
|
taxas = taxonNodeDao.getTaxonOfAcceptedTaxaByClassification(classification, 0, 10);
|
212
|
logger.info(taxas.size());
|
213
|
assertEquals("there should be 7 taxa left", 7, taxas.size());
|
214
|
|
215
|
int countTaxa = taxonNodeDao.countTaxonOfAcceptedTaxaByClassification(classification);
|
216
|
logger.info(countTaxa);
|
217
|
assertEquals("there should be 7 taxa left", 7, countTaxa);
|
218
|
}
|
219
|
|
220
|
@Test
|
221
|
@DataSet(value="TaxonNodeDaoHibernateImplTest.testSortindexForJavassist.xml")
|
222
|
@ExpectedDataSet("TaxonNodeDaoHibernateImplTest.testSortindexForJavassist-result.xml")
|
223
|
|
224
|
//test if TaxonNode.remove(index) works correctly with proxies
|
225
|
public void testSortindexForJavassist(){
|
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 firstTopLevelTaxon = (Taxon)taxonDao.findByUuid(UUID.fromString("7b8b5cb3-37ba-4dba-91ac-4c6ffd6ac331"));
|
230
|
Classification classification = classificationDao.findByUuid(ClassificationUuid);
|
231
|
TaxonNode childNode = classification.addParentChild(taxonWithLazyLoadedParentNodeOnTopLevel, firstTopLevelTaxon, null, null);
|
232
|
this.taxonNodeDao.saveOrUpdate(childNode);
|
233
|
commitAndStartNewTransaction( new String[]{"TaxonNode"});
|
234
|
}
|
235
|
|
236
|
@Test
|
237
|
@DataSet(value="TaxonNodeDaoHibernateImplTest.testSortindexForJavassist.xml")
|
238
|
@ExpectedDataSet("TaxonNodeDaoHibernateImplTest.testSortindexForJavassist2-result.xml")
|
239
|
//test if TaxonNode.addNode(node) works correctly with proxies
|
240
|
public void testSortindexForJavassist2(){
|
241
|
Taxon taxonWithLazyLoadedParentNodeOnTopLevel = (Taxon)taxonDao.findByUuid(UUID.fromString("bc09aca6-06fd-4905-b1e7-cbf7cc65d783"));
|
242
|
TaxonNode parent = taxonWithLazyLoadedParentNodeOnTopLevel.getTaxonNodes().iterator().next().getParent();
|
243
|
Assert.assertTrue("Parent node must be proxy, otherwise test does not work", parent instanceof Proxy);
|
244
|
Taxon newTaxon = Taxon.NewInstance(null, null);
|
245
|
Classification classification = classificationDao.findByUuid(ClassificationUuid);
|
246
|
TaxonNode newNode = classification.addChildTaxon(newTaxon, 0, null, null);
|
247
|
newNode.setUuid(UUID.fromString("58728644-1155-4520-98f7-309fdb62abd7"));
|
248
|
this.taxonNodeDao.saveOrUpdate(newNode);
|
249
|
commitAndStartNewTransaction( new String[]{"TaxonNode"});
|
250
|
}
|
251
|
|
252
|
@Test
|
253
|
public void testSaveAndLoadTaxonNodeAgentRelation(){
|
254
|
Classification classification = Classification.NewInstance("Me");
|
255
|
Taxon taxon = Taxon.NewInstance(null, null);
|
256
|
Person person = Person.NewInstance();
|
257
|
TaxonNode node = classification.addChildTaxon(taxon, null, null);
|
258
|
DefinedTerm lastScrutiny = (DefinedTerm)termDao.findByUuid(DefinedTerm.uuidLastScrutiny);
|
259
|
TaxonNodeAgentRelation rel = node.addAgentRelation(lastScrutiny, person);
|
260
|
taxonNodeDao.save(node);
|
261
|
commitAndStartNewTransaction(null);
|
262
|
|
263
|
TaxonNode newNode = taxonNodeDao.load(node.getUuid());
|
264
|
Assert.assertNotSame(node, newNode);
|
265
|
Assert.assertEquals("Node should have agent relation", 1, newNode.getAgentRelations().size());
|
266
|
TaxonNodeAgentRelation newRel = newNode.getAgentRelations().iterator().next();
|
267
|
Assert.assertEquals(rel, newRel);
|
268
|
Assert.assertEquals(rel.getId(), newRel.getId());
|
269
|
Assert.assertNotSame(rel, newRel);
|
270
|
}
|
271
|
|
272
|
//see comment 7 in #6199
|
273
|
@Test
|
274
|
@DataSet
|
275
|
public void testPersistExcludedInfos(){
|
276
|
//test read
|
277
|
TaxonNode excludedNode = taxonNodeDao.load(UUID.fromString("4f73adcc-a535-4fbe-a97a-c05ee8b12191"));
|
278
|
Assert.assertTrue("Node should be excluded", excludedNode.isExcluded());
|
279
|
TaxonNode unplacedNode = taxonNodeDao.load(UUID.fromString("20c8f083-5870-4cbd-bf56-c5b2b98ab6a7"));
|
280
|
Assert.assertTrue("Node should be unplaced", unplacedNode.isUnplaced());
|
281
|
TaxonNode notSpecialNode = taxonNodeDao.load(UUID.fromString("770239f6-4fa8-496b-8738-fe8f7b2ad519"));
|
282
|
Assert.assertFalse("Node should be neither excluded nor unplaced", notSpecialNode.isUnplaced() || notSpecialNode.isExcluded());
|
283
|
|
284
|
//read excluded node
|
285
|
Map<Language, LanguageString> map = excludedNode.getExcludedNote();
|
286
|
Assert.assertEquals(2, map.size());
|
287
|
Set<Integer> langIds = new HashSet<>();
|
288
|
for (Language lang : map.keySet()){
|
289
|
langIds.add(lang.getId());
|
290
|
}
|
291
|
Assert.assertTrue("Excluded note must contain text for language id = 1", langIds.contains(1));
|
292
|
Assert.assertTrue("", langIds.contains(2));
|
293
|
}
|
294
|
|
295
|
|
296
|
@Override
|
297
|
public void createTestDataSet() throws FileNotFoundException {}
|
298
|
|
299
|
}
|