1
|
/**
|
2
|
* Copyright (C) 2018 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.api.service.taxonGraph;
|
10
|
|
11
|
import java.io.FileNotFoundException;
|
12
|
import java.util.List;
|
13
|
import java.util.UUID;
|
14
|
|
15
|
import org.hibernate.SessionFactory;
|
16
|
import org.hibernate.event.service.spi.EventListenerRegistry;
|
17
|
import org.hibernate.event.spi.EventType;
|
18
|
import org.hibernate.internal.SessionFactoryImpl;
|
19
|
import org.junit.After;
|
20
|
import org.junit.Assert;
|
21
|
import org.junit.Before;
|
22
|
import org.junit.Test;
|
23
|
import org.unitils.dbunit.annotation.DataSet;
|
24
|
import org.unitils.spring.annotation.SpringBeanByType;
|
25
|
|
26
|
import eu.etaxonomy.cdm.api.application.IRunAs;
|
27
|
import eu.etaxonomy.cdm.api.application.RunAsAdmin;
|
28
|
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
|
29
|
import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
|
30
|
import eu.etaxonomy.cdm.model.name.Rank;
|
31
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
32
|
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
|
33
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
34
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
35
|
import eu.etaxonomy.cdm.persistence.dao.common.IPreferenceDao;
|
36
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.taxonGraph.TaxonGraphDaoHibernateImpl;
|
37
|
import eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao;
|
38
|
import eu.etaxonomy.cdm.persistence.dao.taxonGraph.ITaxonGraphDao;
|
39
|
import eu.etaxonomy.cdm.persistence.dao.taxonGraph.TaxonGraphException;
|
40
|
import eu.etaxonomy.cdm.persistence.dto.TaxonGraphEdgeDTO;
|
41
|
import eu.etaxonomy.cdm.persistence.hibernate.TaxonGraphHibernateListener;
|
42
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
43
|
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
|
44
|
|
45
|
/**
|
46
|
* @author a.kohlbecker
|
47
|
* @since Oct 1, 2018
|
48
|
*
|
49
|
*/
|
50
|
public class TaxonGraphHibernateListenerTest extends CdmTransactionalIntegrationTest {
|
51
|
|
52
|
@SpringBeanByType
|
53
|
protected ITaxonGraphDao taxonGraphDao;
|
54
|
|
55
|
@SpringBeanByType
|
56
|
private ITaxonNameDao nameDao;
|
57
|
|
58
|
@SpringBeanByType
|
59
|
private IPreferenceDao prefDao;
|
60
|
|
61
|
@SpringBeanByType
|
62
|
private SessionFactory sessionFactory;
|
63
|
|
64
|
private static boolean isRegistered;
|
65
|
|
66
|
protected static UUID uuid_secRef = UUID.fromString("34e1ff99-63c4-4296-81b6-b20afb98902e");
|
67
|
|
68
|
protected static UUID uuid_n_euglenophyceae = UUID.fromString("9928147d-4499-4ce9-bcf3-e4eaa13e509e");
|
69
|
protected static UUID uuid_n_euglena = UUID.fromString("ab59d853-dd4f-4f80-bd7b-cf53bfd42d39");
|
70
|
protected static UUID uuid_n_trachelomonas = UUID.fromString("5e3d015c-0a5c-4975-a3b0-334b4b47ff79");
|
71
|
protected static UUID uuid_n_trachelomonas_a = UUID.fromString("a798721a-e305-420d-aec1-e915ad1971e4");
|
72
|
protected static UUID uuid_n_trachelomonas_o = UUID.fromString("a2e7eeff-b844-4b3d-ab75-2a113b44573e");
|
73
|
protected static UUID uuid_n_trachelomonas_o_var_d = UUID.fromString("d8a0e3ad-2a4d-45ed-b874-f96616015f91");
|
74
|
protected static UUID uuid_n_trachelomonas_s = UUID.fromString("5b90bd58-7f76-45c4-9966-7f65e7bf0bb0");
|
75
|
protected static UUID uuid_n_trachelomonas_s_var_a = UUID.fromString("192ad8a1-55ca-4379-87a1-3bbd04e8b880");
|
76
|
protected static UUID uuid_n_trachelomonas_r_s = UUID.fromString("2d6e68bf-aba7-433d-8325-ea15f3e567f4");
|
77
|
protected static UUID uuid_n_phacus_s = UUID.fromString("d59b8715-1b98-4da4-a42d-efcbe85b323c");
|
78
|
|
79
|
protected static UUID uuid_t_euglenophyceae = UUID.fromString("4ea17d7a-17a3-41f0-8de6-e924494ecbae");
|
80
|
protected static UUID uuid_t_euglena = UUID.fromString("1c69afd4-ae58-4913-8706-5c89729d38f4");
|
81
|
protected static UUID uuid_t_trachelomonas = UUID.fromString("52b9a8e0-9133-4ee0-ba9f-84ca6e28d033");
|
82
|
protected static UUID uuid_t_trachelomonas_a = UUID.fromString("04443b64-f2e5-48c5-9069-9354f43ded9f");
|
83
|
protected static UUID uuid_t_trachelomonas_o = UUID.fromString("bdf75350-8361-4e33-a614-a4214cc3e90a");
|
84
|
protected static UUID uuid_t_trachelomonas_o_var_d = UUID.fromString("f54ad8cf-fe87-499d-826a-2c5a71551fcf");
|
85
|
protected static UUID uuid_t_trachelomonas_s = UUID.fromString("5dce8a09-c809-4027-a9ce-b70901e7b820");
|
86
|
protected static UUID uuid_t_trachelomonas_s_var_a = UUID.fromString("3f14c528-e191-4a6f-b2a9-36c9a3fc7eee");
|
87
|
|
88
|
private static TaxonGraphHibernateListener taxonGraphHibernateListener = new TaxonGraphHibernateListener();
|
89
|
|
90
|
@Before
|
91
|
public void registerListener() throws NoSuchMethodException, SecurityException {
|
92
|
|
93
|
if(!TaxonGraphHibernateListenerTest.isRegistered){
|
94
|
EventListenerRegistry listenerRegistry = ((SessionFactoryImpl) sessionFactory).getServiceRegistry().getService(EventListenerRegistry.class);
|
95
|
listenerRegistry.appendListeners(EventType.POST_UPDATE, taxonGraphHibernateListener);
|
96
|
listenerRegistry.appendListeners(EventType.POST_INSERT, taxonGraphHibernateListener);
|
97
|
TaxonGraphHibernateListenerTest.isRegistered = true;
|
98
|
}
|
99
|
//
|
100
|
taxonGraphHibernateListener.registerProcessClass(TaxonGraphBeforeTransactionCompleteProcess.class, new Object[]{new RunAsAdmin()}, new Class[]{IRunAs.class});
|
101
|
}
|
102
|
|
103
|
|
104
|
@After
|
105
|
public void inactivateListener() {
|
106
|
taxonGraphHibernateListener.unRegisterProcessClass(TaxonGraphBeforeTransactionCompleteProcess.class);
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
*
|
111
|
*/
|
112
|
protected void setUuidPref() {
|
113
|
PrefKey key = TaxonGraphDaoHibernateImpl.CDM_PREF_KEY_SEC_REF_UUID;
|
114
|
prefDao.set(new CdmPreference(key.getSubject(), key.getPredicate(), uuid_secRef.toString()));
|
115
|
commitAndStartNewTransaction();
|
116
|
}
|
117
|
|
118
|
@Test
|
119
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonGraphTest.xml")
|
120
|
public void testNewTaxonName() throws TaxonGraphException{
|
121
|
|
122
|
try{
|
123
|
setUuidPref();
|
124
|
|
125
|
Reference refX = ReferenceFactory.newBook();
|
126
|
refX.setTitleCache("Ref-X", true);
|
127
|
|
128
|
TaxonName n_t_argentinensis = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES(), "Trachelomonas", null, "argentinensis", null, null, refX, null, null);
|
129
|
n_t_argentinensis = nameDao.save(n_t_argentinensis);
|
130
|
commitAndStartNewTransaction();
|
131
|
|
132
|
n_t_argentinensis = nameDao.load(n_t_argentinensis.getUuid());
|
133
|
Assert.assertTrue("a taxon should have been created", n_t_argentinensis.getTaxa().size() > 0);
|
134
|
|
135
|
List<TaxonGraphEdgeDTO> edges = taxonGraphDao.edges(n_t_argentinensis, nameDao.load(uuid_n_trachelomonas), true);
|
136
|
Assert.assertEquals(1, edges.size());
|
137
|
Assert.assertEquals(refX.getUuid(), edges.get(0).getCitationUuid());
|
138
|
|
139
|
} finally {
|
140
|
rollback();
|
141
|
}
|
142
|
}
|
143
|
|
144
|
@Test
|
145
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonGraphTest.xml")
|
146
|
public void testNewGenusName() throws TaxonGraphException{
|
147
|
|
148
|
try{
|
149
|
setUuidPref();
|
150
|
|
151
|
Reference refX = ReferenceFactory.newBook();
|
152
|
refX.setTitleCache("Ref-X", true);
|
153
|
|
154
|
TaxonName n_phacus = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS(), "Phacus", null, null, null, null, refX, null, null);
|
155
|
nameDao.save(n_phacus);
|
156
|
commitAndStartNewTransaction();
|
157
|
|
158
|
n_phacus = nameDao.load(n_phacus.getUuid());
|
159
|
Assert.assertTrue("a taxon should have been created", n_phacus.getTaxa().size() > 0);
|
160
|
|
161
|
List<TaxonGraphEdgeDTO> edges = taxonGraphDao.edges(nameDao.load(uuid_n_phacus_s), n_phacus, true);
|
162
|
Assert.assertEquals(1, edges.size());
|
163
|
Assert.assertEquals(refX.getUuid(), edges.get(0).getCitationUuid());
|
164
|
|
165
|
} finally {
|
166
|
rollback();
|
167
|
}
|
168
|
}
|
169
|
|
170
|
@Test
|
171
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonGraphTest.xml")
|
172
|
public void testChangeNomRef() throws TaxonGraphException{
|
173
|
try {
|
174
|
setUuidPref();
|
175
|
|
176
|
Reference refX = ReferenceFactory.newBook();
|
177
|
refX.setTitleCache("Ref-X", true);
|
178
|
|
179
|
// printDataSet(System.err,"TaxonRelationship");
|
180
|
TaxonName n_trachelomonas_a = nameDao.load(uuid_n_trachelomonas_a);
|
181
|
n_trachelomonas_a.setNomenclaturalReference(refX);
|
182
|
nameDao.saveOrUpdate(n_trachelomonas_a);
|
183
|
commitAndStartNewTransaction();
|
184
|
|
185
|
// printDataSet(System.err,"TaxonRelationship");
|
186
|
|
187
|
List<TaxonGraphEdgeDTO> edges = taxonGraphDao.edges(nameDao.load(uuid_n_trachelomonas_a), nameDao.load(uuid_n_trachelomonas), true);
|
188
|
Assert.assertEquals(1, edges.size());
|
189
|
Assert.assertEquals(refX.getUuid(), edges.get(0).getCitationUuid());
|
190
|
|
191
|
} finally {
|
192
|
rollback();
|
193
|
}
|
194
|
}
|
195
|
|
196
|
@Test
|
197
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonGraphTest.xml")
|
198
|
public void testChangeRank() throws TaxonGraphException{
|
199
|
|
200
|
try {
|
201
|
setUuidPref();
|
202
|
|
203
|
TaxonName n_trachelomonas_o_var_d = nameDao.load(uuid_n_trachelomonas_o_var_d);
|
204
|
|
205
|
List<TaxonGraphEdgeDTO> edges = taxonGraphDao.edges(n_trachelomonas_o_var_d, nameDao.load(uuid_n_trachelomonas), true);
|
206
|
Assert.assertEquals("One edge from 'Trachelomonas oviformis var. duplex' to 'Trachelomonas' expected", 1, edges.size());
|
207
|
|
208
|
n_trachelomonas_o_var_d.setRank(Rank.SPECIES());
|
209
|
nameDao.saveOrUpdate(n_trachelomonas_o_var_d);
|
210
|
commitAndStartNewTransaction();
|
211
|
|
212
|
// printDataSet(System.err,"TaxonRelationship");
|
213
|
edges = taxonGraphDao.edges(n_trachelomonas_o_var_d, nameDao.load(uuid_n_trachelomonas), true);
|
214
|
Assert.assertEquals("The edge to Trachelomonas should still exist", 1, edges.size());
|
215
|
edges = taxonGraphDao.edges(n_trachelomonas_o_var_d, nameDao.load(uuid_n_trachelomonas_o), true);
|
216
|
Assert.assertEquals("The edge to Trachelomonas oviformis should have been deleted", 0, edges.size());
|
217
|
|
218
|
} finally {
|
219
|
rollback();
|
220
|
}
|
221
|
}
|
222
|
|
223
|
@Test
|
224
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonGraphTest.xml")
|
225
|
public void testChangeGenus() throws TaxonGraphException{
|
226
|
|
227
|
try {
|
228
|
setUuidPref();
|
229
|
|
230
|
TaxonName n_trachelomonas_o_var_d = nameDao.load(uuid_n_trachelomonas_o_var_d);
|
231
|
|
232
|
List<TaxonGraphEdgeDTO> edges = taxonGraphDao.edges(n_trachelomonas_o_var_d, nameDao.load(uuid_n_trachelomonas), true);
|
233
|
Assert.assertEquals("One edge from 'Trachelomonas oviformis var. duplex' to 'Trachelomonas' expected", 1, edges.size());
|
234
|
edges = taxonGraphDao.edges(n_trachelomonas_o_var_d, nameDao.load(uuid_n_trachelomonas_o), true);
|
235
|
Assert.assertEquals("One edge from 'Trachelomonas oviformis var. duplex' to 'Trachelomonas oviformis' expected", 1, edges.size());
|
236
|
edges = taxonGraphDao.edges(null, nameDao.load(uuid_n_euglena), true);
|
237
|
Assert.assertEquals("No edges to 'Euglena' expected", 0, edges.size());
|
238
|
|
239
|
n_trachelomonas_o_var_d.setGenusOrUninomial("Euglena");
|
240
|
nameDao.saveOrUpdate(n_trachelomonas_o_var_d);
|
241
|
commitAndStartNewTransaction();
|
242
|
|
243
|
// printDataSet(System.err,"TaxonRelationship");
|
244
|
edges = taxonGraphDao.edges(n_trachelomonas_o_var_d, nameDao.load(uuid_n_trachelomonas), true);
|
245
|
Assert.assertEquals("The edge to Trachelomonas should have been deleted", 0, edges.size());
|
246
|
edges = taxonGraphDao.edges(n_trachelomonas_o_var_d, nameDao.load(uuid_n_trachelomonas_o), true);
|
247
|
Assert.assertEquals("The edge to 'Trachelomonas oviformis' should have been deleted", 0, edges.size());
|
248
|
edges = taxonGraphDao.edges(n_trachelomonas_o_var_d, nameDao.load(uuid_n_euglena), true);
|
249
|
Assert.assertEquals("The edge to 'Euglena' should have been created", 1, edges.size());
|
250
|
|
251
|
} finally {
|
252
|
rollback();
|
253
|
}
|
254
|
}
|
255
|
|
256
|
@Test
|
257
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonGraphTest.xml")
|
258
|
public void testChangeSpecificEpithet_of_InfraSpecific() throws TaxonGraphException{
|
259
|
|
260
|
try {
|
261
|
setUuidPref();
|
262
|
|
263
|
TaxonName n_trachelomonas_o_var_d = nameDao.load(uuid_n_trachelomonas_o_var_d);
|
264
|
|
265
|
List<TaxonGraphEdgeDTO> edges = taxonGraphDao.edges(n_trachelomonas_o_var_d, nameDao.load(uuid_n_trachelomonas), true);
|
266
|
Assert.assertEquals("One edge from 'Trachelomonas oviformis var. duplex' to 'Trachelomonas' expected", 1, edges.size());
|
267
|
edges = taxonGraphDao.edges(n_trachelomonas_o_var_d, nameDao.load(uuid_n_trachelomonas_o), true);
|
268
|
Assert.assertEquals("One edge from 'Trachelomonas oviformis var. duplex' to 'Trachelomonas oviformis' expected", 1, edges.size());
|
269
|
|
270
|
n_trachelomonas_o_var_d.setSpecificEpithet("alabamensis");
|
271
|
nameDao.saveOrUpdate(n_trachelomonas_o_var_d);
|
272
|
commitAndStartNewTransaction();
|
273
|
|
274
|
// printDataSet(System.err,"TaxonRelationship");
|
275
|
edges = taxonGraphDao.edges(n_trachelomonas_o_var_d, nameDao.load(uuid_n_trachelomonas), true);
|
276
|
Assert.assertEquals("The edge to Trachelomonas should still exist", 1, edges.size());
|
277
|
edges = taxonGraphDao.edges(n_trachelomonas_o_var_d, nameDao.load(uuid_n_trachelomonas_o), true);
|
278
|
Assert.assertEquals("The edge to Trachelomonas oviformis should have been deleted", 0, edges.size());
|
279
|
edges = taxonGraphDao.edges(n_trachelomonas_o_var_d, nameDao.load(uuid_n_trachelomonas_a), true);
|
280
|
Assert.assertEquals("The edge to Trachelomonas alabamensis should have been created", 1, edges.size());
|
281
|
|
282
|
} finally {
|
283
|
rollback();
|
284
|
}
|
285
|
}
|
286
|
|
287
|
@Test
|
288
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonGraphTest.xml")
|
289
|
public void testChangeSpecificEpithet_of_Species() throws TaxonGraphException{
|
290
|
|
291
|
try {
|
292
|
setUuidPref();
|
293
|
|
294
|
TaxonName n_trachelomonas_o = nameDao.load(uuid_n_trachelomonas_o);
|
295
|
|
296
|
List<TaxonGraphEdgeDTO> edges = taxonGraphDao.edges(nameDao.load(uuid_n_trachelomonas_o_var_d), n_trachelomonas_o, true);
|
297
|
Assert.assertEquals("One edge from 'Trachelomonas oviformis var. duplex' to 'Trachelomonas oviformis' expected", 1, edges.size());
|
298
|
edges = taxonGraphDao.edges(n_trachelomonas_o, nameDao.load(uuid_n_trachelomonas) , true);
|
299
|
Assert.assertEquals("One edge from 'Trachelomonas oviformis' to 'Trachelomonas' expected", 1, edges.size());
|
300
|
|
301
|
n_trachelomonas_o.setSpecificEpithet("robusta");
|
302
|
nameDao.saveOrUpdate(n_trachelomonas_o);
|
303
|
commitAndStartNewTransaction();
|
304
|
n_trachelomonas_o = nameDao.load(n_trachelomonas_o.getUuid());
|
305
|
|
306
|
// printDataSet(System.err,"TaxonRelationship");
|
307
|
edges = taxonGraphDao.edges(n_trachelomonas_o, nameDao.load(uuid_n_trachelomonas) , true);
|
308
|
Assert.assertEquals("The edge to Trachelomonas should still exist", 1, edges.size());
|
309
|
edges = taxonGraphDao.edges(nameDao.load(uuid_n_trachelomonas_o_var_d), n_trachelomonas_o, true);
|
310
|
Assert.assertEquals("The edge from 'Trachelomonas oviformis var. duplex' should have been deleted", 0, edges.size());
|
311
|
edges = taxonGraphDao.edges(nameDao.load(uuid_n_trachelomonas_r_s), n_trachelomonas_o, true);
|
312
|
Assert.assertEquals("The edge to 'Trachelomonas robusta var. sparsiornata' should have been created", 1, edges.size());
|
313
|
|
314
|
} finally {
|
315
|
rollback();
|
316
|
}
|
317
|
}
|
318
|
|
319
|
|
320
|
/**
|
321
|
* {@inheritDoc}
|
322
|
*/
|
323
|
@Override
|
324
|
public void createTestDataSet() throws FileNotFoundException {
|
325
|
// DataSet create by createTestDataSet()
|
326
|
}
|
327
|
}
|