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.api.service;
|
10
|
|
11
|
import static org.junit.Assert.assertEquals;
|
12
|
import static org.junit.Assert.assertTrue;
|
13
|
|
14
|
import java.io.FileNotFoundException;
|
15
|
import java.util.Arrays;
|
16
|
import java.util.List;
|
17
|
import java.util.Set;
|
18
|
import java.util.UUID;
|
19
|
|
20
|
import org.apache.log4j.Logger;
|
21
|
import org.junit.Assert;
|
22
|
import org.junit.Ignore;
|
23
|
import org.junit.Test;
|
24
|
import org.springframework.security.authentication.AbstractAuthenticationToken;
|
25
|
import org.springframework.security.authentication.AuthenticationManager;
|
26
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
27
|
import org.springframework.security.core.Authentication;
|
28
|
import org.springframework.security.core.context.SecurityContextHolder;
|
29
|
import org.springframework.security.core.context.SecurityContextImpl;
|
30
|
import org.unitils.dbunit.annotation.DataSet;
|
31
|
import org.unitils.spring.annotation.SpringBeanByName;
|
32
|
import org.unitils.spring.annotation.SpringBeanByType;
|
33
|
|
34
|
import eu.etaxonomy.cdm.api.service.config.IdentifiableServiceConfiguratorFactory;
|
35
|
import eu.etaxonomy.cdm.api.service.config.IdentifiableServiceConfiguratorImpl;
|
36
|
import eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator;
|
37
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
38
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
39
|
import eu.etaxonomy.cdm.model.common.Language;
|
40
|
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
|
41
|
import eu.etaxonomy.cdm.model.description.Feature;
|
42
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
43
|
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
|
44
|
import eu.etaxonomy.cdm.model.description.TextData;
|
45
|
import eu.etaxonomy.cdm.model.name.HybridRelationship;
|
46
|
import eu.etaxonomy.cdm.model.name.HybridRelationshipType;
|
47
|
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
|
48
|
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
|
49
|
import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
|
50
|
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
|
51
|
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
|
52
|
import eu.etaxonomy.cdm.model.name.Rank;
|
53
|
import eu.etaxonomy.cdm.model.name.Registration;
|
54
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
|
55
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
|
56
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
57
|
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
|
58
|
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
|
59
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
60
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
61
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
62
|
import eu.etaxonomy.cdm.persistence.dao.common.Restriction;
|
63
|
import eu.etaxonomy.cdm.persistence.dao.common.Restriction.Operator;
|
64
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
65
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
66
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
67
|
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
|
68
|
|
69
|
/**
|
70
|
* @author a.mueller
|
71
|
*
|
72
|
*/
|
73
|
public class NameServiceImplTest extends CdmTransactionalIntegrationTest {
|
74
|
private static final Logger logger = Logger.getLogger(NameServiceImplTest.class);
|
75
|
|
76
|
@SpringBeanByType
|
77
|
private INameService nameService;
|
78
|
|
79
|
@SpringBeanByType
|
80
|
private IOccurrenceService occurrenceService;
|
81
|
|
82
|
@SpringBeanByType
|
83
|
private IRegistrationService registrationService;
|
84
|
|
85
|
@SpringBeanByType
|
86
|
private ITaxonService taxonService;
|
87
|
|
88
|
@SpringBeanByType
|
89
|
private ITermService termService;
|
90
|
|
91
|
@SpringBeanByName
|
92
|
private AuthenticationManager authenticationManager;
|
93
|
|
94
|
private void setAuthentication(AbstractAuthenticationToken token) {
|
95
|
Authentication authentication = authenticationManager.authenticate(token);
|
96
|
|
97
|
SecurityContextImpl secureContext = new SecurityContextImpl();
|
98
|
SecurityContextHolder.setContext(secureContext);
|
99
|
secureContext.setAuthentication(authentication);
|
100
|
}
|
101
|
|
102
|
private void unsetAuthentication() {
|
103
|
|
104
|
SecurityContextImpl secureContext = new SecurityContextImpl();
|
105
|
SecurityContextHolder.setContext(secureContext);
|
106
|
secureContext.setAuthentication(null);
|
107
|
}
|
108
|
|
109
|
|
110
|
/* ******************** TESTS ********************************************/
|
111
|
|
112
|
/**
|
113
|
* Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#setDao(eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao)}.
|
114
|
*/
|
115
|
@Test
|
116
|
public void testSetDao() {
|
117
|
// Assert.assertNotNull(((NameServiceImpl)nameService).dao);
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#setVocabularyDao(eu.etaxonomy.cdm.persistence.dao.common.ITermVocabularyDao)}.
|
122
|
*/
|
123
|
@Test
|
124
|
public void testSetVocabularyDao() {
|
125
|
// Assert.assertNotNull(( (NameServiceImpl)nameService.vocabularyDao);
|
126
|
}
|
127
|
|
128
|
/**
|
129
|
* Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#getNamesByName(java.lang.String)}.
|
130
|
*/
|
131
|
@Test
|
132
|
public void testGetNamesByName() {
|
133
|
logger.warn("Not yet implemented");
|
134
|
}
|
135
|
|
136
|
|
137
|
@Test
|
138
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
|
139
|
public void testDeleteTaxonNameWithNameRelations() {
|
140
|
|
141
|
final String[] tableNames = new String[]{"USERACCOUNT", "TaxonName","NameRelationship","HybridRelationship","DescriptionBase","NomenclaturalStatus","TaxonBase","SpecimenOrObservationBase","OriginalSourceBase","DescriptionElementBase"};
|
142
|
// printDataSetWithNull(System.err, true, null);
|
143
|
|
144
|
TaxonName name1 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
145
|
name1.setTitleCache("Name1", true);
|
146
|
TaxonName nameWithBasionym = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
147
|
nameWithBasionym.setTitleCache("nameWithBasionym", true);
|
148
|
|
149
|
NameRelationshipType nameRelType = (NameRelationshipType)termService.find(NameRelationshipType.BASIONYM().getUuid());
|
150
|
name1.addRelationshipToName(nameWithBasionym,nameRelType , null, null, null);
|
151
|
// nameWithBasionym.addBasionym(name1);
|
152
|
nameService.save(name1);
|
153
|
nameService.save(nameWithBasionym);
|
154
|
commitAndStartNewTransaction(tableNames);
|
155
|
|
156
|
|
157
|
name1 = nameService.find(name1.getUuid());
|
158
|
DeleteResult result = nameService.delete(name1);
|
159
|
if (!result.isOk()){
|
160
|
Exception e = result.getExceptions().iterator().next();
|
161
|
Assert.assertEquals("The Ecxeption should be a ReferencedObjectException because it is a basionym", "Name can't be deleted as it is a basionym.", e.getMessage());
|
162
|
} else{
|
163
|
Assert.fail();
|
164
|
}
|
165
|
name1 = nameService.find(name1.getUuid());
|
166
|
Assert.assertNotNull("Name should still be in database",name1);
|
167
|
nameWithBasionym = name1.getNameRelations().iterator().next().getToName();
|
168
|
nameWithBasionym.removeBasionyms();
|
169
|
|
170
|
result = nameService.delete(name1); //should throw now exception
|
171
|
|
172
|
|
173
|
|
174
|
commitAndStartNewTransaction(tableNames);
|
175
|
name1 = nameService.find(name1.getUuid());
|
176
|
Assert.assertNull("Name should not be in database anymore",name1);
|
177
|
|
178
|
}
|
179
|
|
180
|
@Test
|
181
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
|
182
|
public void testDeleteTaxonNameConfiguratorWithNameRelations() {
|
183
|
final String[] tableNames = new String[]{"TaxonName","NameRelationship","HybridRelationship"};
|
184
|
|
185
|
// printDataSet(System.err, new String[]{"TaxonName","NameRelationship","HybridRelationship","DescriptionBase","NomenclaturalStatus","TaxonBase","SpecimenOrObservationBase","OriginalSourceBase",
|
186
|
// "DescriptionElementBase",
|
187
|
// "AGENTBASE", "USERACCOUNT", "PERMISSIONGROUP", "USERACCOUNT_PERMISSIONGROUP", "USERACCOUNT_GRANTEDAUTHORITYIMPL", "GRANTEDAUTHORITYIMPL"});
|
188
|
|
189
|
TaxonName name1 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
190
|
name1.setTitleCache("Name1", true);
|
191
|
TaxonName nameWithBasionym = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
192
|
nameWithBasionym.setTitleCache("nameWithBasionym", true);
|
193
|
|
194
|
NameRelationshipType nameRelType = (NameRelationshipType)termService.find(NameRelationshipType.BASIONYM().getUuid());
|
195
|
name1.addRelationshipToName(nameWithBasionym,nameRelType , null, null, null);
|
196
|
nameService.save(name1);
|
197
|
nameService.save(nameWithBasionym);
|
198
|
commitAndStartNewTransaction(tableNames);
|
199
|
NameDeletionConfigurator config = new NameDeletionConfigurator();
|
200
|
|
201
|
name1 = nameService.find(name1.getUuid());
|
202
|
DeleteResult result = nameService.delete(name1.getUuid(), config);
|
203
|
if (result.isOk()){
|
204
|
Assert.fail("This should throw an error as long as name relationships exist.");
|
205
|
}
|
206
|
|
207
|
|
208
|
name1 = nameService.find(name1.getUuid());
|
209
|
Assert.assertNotNull("Name should still be in database",name1);
|
210
|
|
211
|
//ignore is basionym for
|
212
|
config.setIgnoreIsBasionymFor(true);
|
213
|
|
214
|
name1 = nameService.find(name1.getUuid());
|
215
|
nameService.delete(name1.getUuid(), config);
|
216
|
commitAndStartNewTransaction(tableNames);
|
217
|
name1 = nameService.find(name1.getUuid());
|
218
|
Assert.assertNull("Name should not be in database anymore",name1);
|
219
|
|
220
|
}
|
221
|
|
222
|
@Test
|
223
|
public void testDeleteTaxonNameConfiguratorWithNameRelationsAll() {
|
224
|
final String[] tableNames = new String[]{"TaxonName","NameRelationship","HybridRelationship"};
|
225
|
|
226
|
TaxonName name1 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
227
|
name1.setTitleCache("Name1", true);
|
228
|
TaxonName nameWithBasionym = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
229
|
nameWithBasionym.setTitleCache("nameWithBasionym", true);
|
230
|
|
231
|
NameRelationshipType nameRelType = (NameRelationshipType)termService.find(NameRelationshipType.BASIONYM().getUuid());
|
232
|
name1.addRelationshipToName(nameWithBasionym,nameRelType , null, null, null);
|
233
|
nameService.save(name1);
|
234
|
nameService.save(nameWithBasionym);
|
235
|
commitAndStartNewTransaction(tableNames);
|
236
|
NameDeletionConfigurator config = new NameDeletionConfigurator();
|
237
|
|
238
|
name1 = nameService.find(name1.getUuid());
|
239
|
DeleteResult result = nameService.delete(name1.getUuid(), config);
|
240
|
if (result.isOk()){
|
241
|
Assert.fail("Delete should throw an error as long as name relationships exist.");
|
242
|
}
|
243
|
|
244
|
name1 = nameService.find(name1.getUuid());
|
245
|
Assert.assertNotNull("Name should still be in database",name1);
|
246
|
|
247
|
//ignore all name relationships
|
248
|
config.setRemoveAllNameRelationships(true);
|
249
|
|
250
|
name1 = nameService.find(name1.getUuid());
|
251
|
result = nameService.delete(name1.getUuid(), config);
|
252
|
logger.debug(result);
|
253
|
commitAndStartNewTransaction(tableNames);
|
254
|
name1 = nameService.find(name1.getUuid());
|
255
|
Assert.assertNull("Name should not be in database anymore",name1);
|
256
|
|
257
|
}
|
258
|
|
259
|
@Test
|
260
|
public void testDeleteTaxonNameConfiguratorWithHasBasionym() {
|
261
|
final String[] tableNames = new String[]{"TaxonName","NameRelationship","HybridRelationship"};
|
262
|
|
263
|
TaxonName name1 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
264
|
name1.setTitleCache("Name1", true);
|
265
|
TaxonName basionym = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
266
|
basionym.setTitleCache("basionym", true);
|
267
|
|
268
|
NameRelationshipType nameRelType = (NameRelationshipType)termService.find(NameRelationshipType.BASIONYM().getUuid());
|
269
|
basionym.addRelationshipToName(name1,nameRelType , null, null, null);
|
270
|
nameService.save(name1);
|
271
|
nameService.save(basionym);
|
272
|
commitAndStartNewTransaction(tableNames);
|
273
|
NameDeletionConfigurator config = new NameDeletionConfigurator();
|
274
|
config.setIgnoreHasBasionym(false);
|
275
|
|
276
|
name1 = nameService.find(name1.getUuid());
|
277
|
DeleteResult result = nameService.delete(name1.getUuid(), config);
|
278
|
if (result.isOk()){
|
279
|
Assert.fail("Delete should throw an error as long as name relationships exist.");
|
280
|
}
|
281
|
|
282
|
|
283
|
name1 = nameService.find(name1.getUuid());
|
284
|
Assert.assertNotNull("Name should still be in database",name1);
|
285
|
|
286
|
//ignore has basionym
|
287
|
config.setIgnoreHasBasionym(true);
|
288
|
try {
|
289
|
name1 = nameService.find(name1.getUuid());
|
290
|
result = nameService.delete(name1.getUuid(), config);
|
291
|
logger.debug(result);
|
292
|
commitAndStartNewTransaction(tableNames);
|
293
|
name1 = nameService.find(name1.getUuid());
|
294
|
Assert.assertNull("Name should not be in database anymore",name1);
|
295
|
} catch (Exception e) {
|
296
|
Assert.fail("Delete should not throw an error for .");
|
297
|
}
|
298
|
}
|
299
|
|
300
|
@Test
|
301
|
// @Ignore //currently does not run in suite
|
302
|
public void testDeleteTaxonNameWithHybridRelations() {
|
303
|
final String[] tableNames = new String[]{"TaxonName","NameRelationship","HybridRelationship"};
|
304
|
|
305
|
TaxonName name1 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
306
|
name1.setTitleCache("Name1", true);
|
307
|
TaxonName parent = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
308
|
parent.setTitleCache("parent", true);
|
309
|
TaxonName child = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
310
|
child.setTitleCache("child", true);
|
311
|
|
312
|
TaxonName hybrid = TaxonNameFactory.PARSED_BOTANICAL("Abies alba x Pinus beta");
|
313
|
|
314
|
Set<HybridRelationship> childRelations = hybrid.getHybridChildRelations();
|
315
|
for (HybridRelationship rel : childRelations){
|
316
|
TaxonName name = rel.getHybridName();
|
317
|
TaxonName parentName = rel.getParentName();
|
318
|
nameService.save(rel.getHybridName());
|
319
|
nameService.save(rel.getParentName());
|
320
|
}
|
321
|
|
322
|
|
323
|
|
324
|
commitAndStartNewTransaction(tableNames); //otherwise first save is rolled back with following failing delete
|
325
|
HybridRelationshipType relType = (HybridRelationshipType)termService.find(HybridRelationshipType.FIRST_PARENT().getUuid());
|
326
|
name1.addHybridParent(parent, relType, null);
|
327
|
nameService.save(name1);
|
328
|
nameService.save(parent);
|
329
|
commitAndStartNewTransaction(tableNames); //otherwise first save is rolled back with following failing delete
|
330
|
Assert.assertEquals("'Parent' should be a parent in a hybrid relation.", 1,parent.getHybridParentRelations().size());
|
331
|
|
332
|
//parent
|
333
|
|
334
|
name1 = nameService.find(name1.getUuid());
|
335
|
DeleteResult result = nameService.delete(name1);
|
336
|
if (result.isError()){
|
337
|
Assert.fail("Delete should throw NO exception when deleting a hybrid child: " + result.getExceptions().iterator().next().getMessage());
|
338
|
}
|
339
|
commitAndStartNewTransaction(tableNames);
|
340
|
name1 = nameService.find(name1.getUuid());
|
341
|
Assert.assertNull("Name should not be in database anymore",name1);
|
342
|
parent = nameService.find(parent.getUuid());
|
343
|
Assert.assertEquals("'Parent' should not be a parent anymore.", 0,parent.getHybridParentRelations().size());
|
344
|
|
345
|
//child
|
346
|
name1 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
347
|
name1.addHybridChild(child, relType, null);
|
348
|
nameService.save(name1);
|
349
|
nameService.save(child);
|
350
|
commitAndStartNewTransaction(tableNames);
|
351
|
|
352
|
|
353
|
result = nameService.delete(name1);
|
354
|
if (result.isOk()){
|
355
|
Assert.fail("Delete should throw an error as long as hybrid child exist.");
|
356
|
}
|
357
|
name1 = nameService.find(name1.getUuid());
|
358
|
Assert.assertNotNull("Name should still be in database",name1);
|
359
|
name1.removeHybridChild(child);
|
360
|
|
361
|
result = nameService.delete(name1); //should throw now exception
|
362
|
if (result.isError()){
|
363
|
Assert.fail("Delete should throw NO exception when deleting a hybrid child: " +result.getExceptions().iterator().next().getMessage());
|
364
|
}
|
365
|
|
366
|
commitAndStartNewTransaction(tableNames);
|
367
|
name1 = nameService.find(name1.getUuid());
|
368
|
Assert.assertNull("Name should not be in database anymore",name1);
|
369
|
}
|
370
|
|
371
|
@Test
|
372
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
|
373
|
public void testDeleteTaxonNameInConcept() {
|
374
|
final String[] tableNames = new String[]{"TaxonName","TaxonBase"};
|
375
|
|
376
|
TaxonName name1 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
377
|
name1.setTitleCache("Name1", true);
|
378
|
TaxonName basionym = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
379
|
basionym.setTitleCache("basionym", true);
|
380
|
|
381
|
Taxon taxon = Taxon.NewInstance(name1, null);
|
382
|
nameService.save(name1);
|
383
|
taxonService.save(taxon);
|
384
|
commitAndStartNewTransaction(tableNames);
|
385
|
|
386
|
DeleteResult result = nameService.delete(name1);
|
387
|
|
388
|
if (result.isOk()){
|
389
|
Assert.fail("Delete should throw an error as long as name is used in a concept.");
|
390
|
}
|
391
|
TaxonName nameBase =nameService.find(name1.getUuid());
|
392
|
Assert.assertNotNull("Name should still be in database",nameBase);
|
393
|
TaxonBase<?> taxonBase = taxonService.find(taxon.getUuid());
|
394
|
Assert.assertNotNull("Taxon should still be in database",taxonBase);
|
395
|
taxon = (Taxon)taxonBase;
|
396
|
taxon.setName(basionym);
|
397
|
taxonService.save(taxon);
|
398
|
nameBase =nameService.find(name1.getUuid());
|
399
|
|
400
|
|
401
|
result = nameService.delete(nameBase); //should throw no exception
|
402
|
if (result.isError()){
|
403
|
Assert.fail("Delete should throw NO error ");
|
404
|
}
|
405
|
commitAndStartNewTransaction(tableNames);
|
406
|
name1 = nameService.find(name1.getUuid());
|
407
|
Assert.assertNull("Name should still be in database",name1);
|
408
|
taxon = (Taxon)taxonService.find(taxon.getUuid());
|
409
|
Assert.assertNotNull("Taxon should still be in database",taxon);
|
410
|
}
|
411
|
|
412
|
@Test
|
413
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
|
414
|
public void testDeleteTaxonNameAsStoredUnder() {
|
415
|
final String[] tableNames = new String[]{"TaxonName","SpecimenOrObservationBase"};
|
416
|
|
417
|
TaxonName name1 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
418
|
name1.setTitleCache("Name1", true);
|
419
|
DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
|
420
|
specimen.setStoredUnder(name1);
|
421
|
|
422
|
occurrenceService.save(specimen);
|
423
|
UUID uuidName1 = nameService.save(name1).getUuid();
|
424
|
|
425
|
commitAndStartNewTransaction(tableNames);
|
426
|
DeleteResult result = nameService.delete(name1);
|
427
|
if (result.isOk()){
|
428
|
Assert.fail("This should throw an error because name is used for specimen#storedUnder.");
|
429
|
}
|
430
|
commitAndStartNewTransaction(tableNames);
|
431
|
|
432
|
name1 = nameService.find(uuidName1);
|
433
|
Assert.assertNotNull("Name should still be in database",name1);
|
434
|
specimen = (DerivedUnit)occurrenceService.find(specimen.getUuid());
|
435
|
Assert.assertNotNull("Specimen should still be in database",name1);
|
436
|
specimen.setStoredUnder(null);
|
437
|
occurrenceService.saveOrUpdate(specimen);
|
438
|
|
439
|
nameService.delete(name1); //should throw no exception
|
440
|
commitAndStartNewTransaction(tableNames);
|
441
|
|
442
|
name1 = nameService.find(uuidName1);
|
443
|
Assert.assertNull("Name should not be in database anymore",name1);
|
444
|
specimen = (DerivedUnit)occurrenceService.find(specimen.getUuid());
|
445
|
Assert.assertNotNull("Specimen should still be in database",specimen);
|
446
|
|
447
|
occurrenceService.delete(specimen); //this is to better run this test in the test suit
|
448
|
|
449
|
}
|
450
|
|
451
|
@Test
|
452
|
@Ignore //currently does not run in suite
|
453
|
public void testDeleteTaxonNameInSource() {
|
454
|
final String[] tableNames = new String[]{"TaxonName","DescriptionBase","TaxonBase","OriginalSourceBase","DescriptionElementBase"};
|
455
|
|
456
|
TaxonName name1 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
457
|
name1.setTitleCache("Name1", true);
|
458
|
TaxonName taxonName = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
459
|
taxonName.setTitleCache("taxonName", true);
|
460
|
Taxon taxon = Taxon.NewInstance(taxonName, null);
|
461
|
|
462
|
TaxonDescription taxonDescription = TaxonDescription.NewInstance(taxon);
|
463
|
Feature feature = (Feature)termService.find(Feature.DESCRIPTION().getUuid());
|
464
|
Language lang = (Language)termService.find(Language.DEFAULT().getUuid());
|
465
|
TextData textData = TextData.NewInstance("Any text", lang, null);
|
466
|
textData.setFeature(feature);
|
467
|
taxonDescription.addElement(textData);
|
468
|
DescriptionElementSource source = DescriptionElementSource.NewPrimarySourceInstance(null, null, name1, "");
|
469
|
textData.addSource(source);
|
470
|
taxonService.saveOrUpdate(taxon);
|
471
|
nameService.save(name1);
|
472
|
try {
|
473
|
commitAndStartNewTransaction(tableNames);
|
474
|
name1 = nameService.find(name1.getUuid());
|
475
|
nameService.delete(name1);
|
476
|
Assert.fail("Delete should throw an error as long as name is used in a source.");
|
477
|
} catch (Exception e) {
|
478
|
if (e.getMessage().contains("Name can't be deleted as it is used as descriptionElementSource#nameUsedInSource")){
|
479
|
//ok
|
480
|
endTransaction(); //exception rolls back transaction!
|
481
|
startNewTransaction();
|
482
|
}else{
|
483
|
Assert.fail("Unexpected error occurred when trying to delete taxon name: " + e.getMessage());
|
484
|
}
|
485
|
}
|
486
|
name1 = nameService.find(name1.getUuid());
|
487
|
Assert.assertNotNull("Name should still be in database",name1);
|
488
|
taxon = (Taxon)taxonService.find(taxon.getUuid());
|
489
|
Assert.assertNotNull("Taxon should still be in database",name1);
|
490
|
source = taxon.getDescriptions().iterator().next().getElements().iterator().next().getSources().iterator().next();
|
491
|
source.setNameUsedInSource(null);
|
492
|
taxonService.saveOrUpdate(taxon);
|
493
|
|
494
|
nameService.delete(name1); //should throw now exception
|
495
|
|
496
|
commitAndStartNewTransaction(tableNames);
|
497
|
name1 = nameService.find(name1.getUuid());
|
498
|
Assert.assertNull("Name should not be in database anymore",name1);
|
499
|
taxon = (Taxon)taxonService.find(taxon.getUuid());
|
500
|
Assert.assertNotNull("Taxon should still be in database",taxon);
|
501
|
source = taxon.getDescriptions().iterator().next().getElements().iterator().next().getSources().iterator().next();
|
502
|
Assert.assertNull("Source should not have a nameUsedInSource anymore",source.getNameUsedInSource());
|
503
|
}
|
504
|
|
505
|
|
506
|
@Test
|
507
|
public void testDeleteTaxonNameAsType() {
|
508
|
final String[] tableNames = new String[]{"TaxonName","TypeDesignationBase","TaxonName_TypeDesignationBase"};
|
509
|
|
510
|
TaxonName name1 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
511
|
name1.setTitleCache("Name used as type", true);
|
512
|
|
513
|
TaxonName higherName = TaxonNameFactory.NewBotanicalInstance(getGenusRank());
|
514
|
higherName.setTitleCache("genus name", true);
|
515
|
NameTypeDesignationStatus typeStatus = (NameTypeDesignationStatus)termService.find(NameTypeDesignationStatus.AUTOMATIC().getUuid());
|
516
|
boolean addToAllHomotypicNames = true;
|
517
|
higherName.addNameTypeDesignation(name1, null, null, null, typeStatus, addToAllHomotypicNames);
|
518
|
nameService.save(higherName);
|
519
|
|
520
|
commitAndStartNewTransaction(tableNames);
|
521
|
name1 = nameService.find(name1.getUuid());
|
522
|
DeleteResult result = nameService.delete(name1);
|
523
|
if (result.isOk()){
|
524
|
Assert.fail("This should throw an error because name is used in a type designation.");
|
525
|
}
|
526
|
|
527
|
commitAndStartNewTransaction(tableNames);
|
528
|
name1 = nameService.find(name1.getUuid());
|
529
|
Assert.assertNotNull("Name should still be in database",name1);
|
530
|
higherName = nameService.find(higherName.getUuid());
|
531
|
higherName.getNameTypeDesignations().iterator().next().removeType(); //keeps the designation but removes the name from it
|
532
|
// nameService.deleteTypeDesignation(higherName,commitAndStartNewTransaction(tableNames) ); //deletes the complete designation //both options can be used
|
533
|
|
534
|
nameService.delete(name1); //should throw now exception
|
535
|
|
536
|
commitAndStartNewTransaction(tableNames);
|
537
|
name1 = nameService.find(name1.getUuid());
|
538
|
Assert.assertNull("Name should not be in database anymore",name1);
|
539
|
higherName = nameService.find(higherName.getUuid());
|
540
|
Assert.assertNotNull("Higher name should still exist in database",higherName);
|
541
|
Assert.assertEquals("Higher name should not have type designations anymore",1, higherName.getTypeDesignations().size());
|
542
|
}
|
543
|
|
544
|
|
545
|
@Test
|
546
|
public void testDeleteTaxonName() {
|
547
|
final String[] tableNames = new String[]{"TaxonName","NameRelationship","HybridRelationship","DescriptionBase","NomenclaturalStatus","TaxonBase","SpecimenOrObservationBase","OriginalSourceBase","DescriptionElementBase","TypeDesignationBase","TaxonName_TypeDesignationBase"};
|
548
|
|
549
|
TaxonName name1 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
550
|
name1.setTitleCache("Name1", true);
|
551
|
|
552
|
//TaxonNameDescription
|
553
|
name1 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
554
|
TaxonNameDescription.NewInstance(name1);
|
555
|
nameService.saveOrUpdate(name1);
|
556
|
commitAndStartNewTransaction(tableNames);
|
557
|
|
558
|
name1 = nameService.find(name1.getUuid());
|
559
|
DeleteResult result = nameService.delete(name1);//should throw now exception
|
560
|
|
561
|
setComplete();
|
562
|
endTransaction();
|
563
|
name1 = nameService.find(name1.getUuid());
|
564
|
Assert.assertNull("Name should not be in database anymore",name1);
|
565
|
|
566
|
// printDataSet(System.out, tableNames);
|
567
|
|
568
|
|
569
|
//NomenclaturalStatus
|
570
|
name1 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
571
|
NomenclaturalStatusType nomStatusType = (NomenclaturalStatusType)termService.find(NomenclaturalStatusType.ILLEGITIMATE().getUuid());
|
572
|
NomenclaturalStatus status = NomenclaturalStatus.NewInstance(nomStatusType);
|
573
|
name1.addStatus(status);
|
574
|
nameService.saveOrUpdate(name1);
|
575
|
commitAndStartNewTransaction(tableNames);
|
576
|
|
577
|
name1 = nameService.find(name1.getUuid());
|
578
|
|
579
|
nameService.delete(name1); //should throw now exception
|
580
|
if (!result.isOk()){
|
581
|
Assert.fail();
|
582
|
}
|
583
|
setComplete();
|
584
|
endTransaction();
|
585
|
// printDataSet(System.out, tableNames);
|
586
|
|
587
|
|
588
|
//Type Designations
|
589
|
name1 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
590
|
name1.setTitleCache("Name with type designation", true);
|
591
|
SpecimenTypeDesignation typeDesignation = SpecimenTypeDesignation.NewInstance();
|
592
|
SpecimenTypeDesignationStatus typeStatus = (SpecimenTypeDesignationStatus)termService.find(SpecimenTypeDesignationStatus.HOLOTYPE().getUuid());
|
593
|
typeDesignation.setTypeStatus(typeStatus);
|
594
|
DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
|
595
|
specimen.setTitleCache("Type specimen", true);
|
596
|
occurrenceService.save(specimen);
|
597
|
typeDesignation.setTypeSpecimen(specimen);
|
598
|
|
599
|
name1.addTypeDesignation(typeDesignation, true);
|
600
|
nameService.save(name1);
|
601
|
commitAndStartNewTransaction(tableNames);
|
602
|
// printDataSet(System.out, tableNames);
|
603
|
|
604
|
name1 = nameService.find(name1.getUuid());
|
605
|
|
606
|
result = nameService.delete(name1); //should throw now exception
|
607
|
if (!result.isOk()){
|
608
|
Assert.fail();
|
609
|
}
|
610
|
setComplete();
|
611
|
endTransaction();
|
612
|
// printDataSet(System.out, tableNames);
|
613
|
|
614
|
}
|
615
|
|
616
|
@Test
|
617
|
public void testDeleteTaxonNameWithTypeInHomotypicalGroup() {
|
618
|
final String[] tableNames = new String[]{"TaxonName","NameRelationship","HybridRelationship","DescriptionBase","NomenclaturalStatus","TaxonBase","SpecimenOrObservationBase","OriginalSourceBase","DescriptionElementBase","TypeDesignationBase","TaxonName_TypeDesignationBase"};
|
619
|
|
620
|
//Type Designations for homotypical group with > 1 names
|
621
|
TaxonName name1 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
622
|
name1.setTitleCache("Name1 with type designation", true);
|
623
|
TaxonName name2 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
624
|
name2.setTitleCache("Name2 with type designation", true);
|
625
|
name2.setHomotypicalGroup(name1.getHomotypicalGroup());
|
626
|
|
627
|
DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
|
628
|
specimen.setTitleCache("Type specimen 2", true);
|
629
|
occurrenceService.save(specimen);
|
630
|
SpecimenTypeDesignationStatus typeStatus = (SpecimenTypeDesignationStatus)termService.find(SpecimenTypeDesignationStatus.HOLOTYPE().getUuid());
|
631
|
|
632
|
SpecimenTypeDesignation typeDesignation = SpecimenTypeDesignation.NewInstance();
|
633
|
typeDesignation.setTypeStatus(typeStatus);
|
634
|
typeDesignation.setTypeSpecimen(specimen);
|
635
|
|
636
|
boolean addToAllNames = true;
|
637
|
name1.addTypeDesignation(typeDesignation, addToAllNames);
|
638
|
nameService.saveOrUpdate(name1);
|
639
|
commitAndStartNewTransaction(tableNames);
|
640
|
|
641
|
name1 = nameService.find(name1.getUuid());
|
642
|
|
643
|
nameService.delete(name1); //should throw now exception
|
644
|
|
645
|
setComplete();
|
646
|
endTransaction();
|
647
|
// printDataSet(System.out, tableNames);
|
648
|
|
649
|
}
|
650
|
|
651
|
@Test
|
652
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
|
653
|
public void testDeleteTypeDesignation() {
|
654
|
final String[] tableNames = new String[]{
|
655
|
"TaxonName","TypeDesignationBase","TaxonName_TypeDesignationBase",
|
656
|
"SpecimenOrObservationBase"};
|
657
|
|
658
|
// IBotanicalName name1 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
659
|
// name1.setTitleCache("Name1");
|
660
|
// name1.setUuid(UUID.fromString("6dbd41d1-fe13-4d9c-bb58-31f051c2c384"));
|
661
|
//
|
662
|
// IBotanicalName name2 = TaxonNameFactory.NewBotanicalInstance(getSpeciesRank());
|
663
|
// name2.setTitleCache("Name2");
|
664
|
// name2.setUuid(UUID.fromString("f9e9c13f-5fa5-48d3-88cf-712c921a099e"));
|
665
|
//
|
666
|
// IBotanicalName name3 = TaxonNameFactory.NewBotanicalInstance(getGenusRank());
|
667
|
// name3.setTitleCache("Name3");
|
668
|
// name3.setUuid(UUID.fromString("e1e66264-f16a-4df9-80fd-6ab5028a3c28"));
|
669
|
//
|
670
|
//
|
671
|
// SpecimenTypeDesignation desig1 = SpecimenTypeDesignation.NewInstance();
|
672
|
// desig1.setUuid(UUID.fromString("1357c307-00c3-499c-8e20-0849d4706125"));
|
673
|
// name1.addTypeDesignation(desig1, true);
|
674
|
// name2.addTypeDesignation(desig1, true);
|
675
|
//
|
676
|
// SpecimenTypeDesignation desig2 = SpecimenTypeDesignation.NewInstance();
|
677
|
// desig2.setUuid(UUID.fromString("9bbda70b-7272-4e65-a807-852a3f2eba63"));
|
678
|
// name1.addTypeDesignation(desig2, true);
|
679
|
//
|
680
|
// Specimen specimen1 = Specimen.NewInstance();
|
681
|
// Fossil specimen2 = Fossil.NewInstance();
|
682
|
//
|
683
|
// desig1.setTypeSpecimen(specimen1);
|
684
|
// desig2.setTypeSpecimen(specimen2);
|
685
|
//
|
686
|
// NameTypeDesignation nameDesig = NameTypeDesignation.NewInstance();
|
687
|
// nameDesig.setTypeName(name1);
|
688
|
// name3.addTypeDesignation(nameDesig, true);
|
689
|
//
|
690
|
// nameService.save(name1);
|
691
|
// nameService.save(name2);
|
692
|
// nameService.save(name3);
|
693
|
//
|
694
|
// commitAndStartNewTransaction(tableNames);
|
695
|
//
|
696
|
// printDataSet(System.out, tableNames);
|
697
|
//
|
698
|
|
699
|
|
700
|
TaxonName name1 = this.nameService.load(UUID.fromString("6dbd41d1-fe13-4d9c-bb58-31f051c2c384"));
|
701
|
TaxonName name2 = this.nameService.load(UUID.fromString("f9e9c13f-5fa5-48d3-88cf-712c921a099e"));
|
702
|
TaxonName name3 = this.nameService.load(UUID.fromString("e1e66264-f16a-4df9-80fd-6ab5028a3c28"));
|
703
|
DerivedUnit specimen1 = CdmBase.deproxy(this.occurrenceService.load(UUID.fromString("0d19a9ca-21a7-4adb-8640-8d6719e15eea")), DerivedUnit.class);
|
704
|
DerivedUnit fossil = CdmBase.deproxy(this.occurrenceService.load(UUID.fromString("4c48b7c8-4c8d-4e48-b083-0837fe51a0a9")), DerivedUnit.class);
|
705
|
|
706
|
Set<TypeDesignationBase> desigs1 = name1.getTypeDesignations();
|
707
|
Set<TypeDesignationBase> desigs2 = name2.getTypeDesignations();
|
708
|
Set<TypeDesignationBase> desigs3 = name3.getTypeDesignations();
|
709
|
|
710
|
Assert.assertEquals("name1 should have 2 type designations", 2, desigs1.size());
|
711
|
Assert.assertEquals("name2 should have 1 type designations", 1, desigs2.size());
|
712
|
Assert.assertEquals("name3 should have 1 type designations", 1, desigs3.size());
|
713
|
Assert.assertEquals("Specimen1 should be used in 1 type designation", 1, specimen1.getSpecimenTypeDesignations().size());
|
714
|
Assert.assertEquals("Fossil should be used in 1 type designation", 1, fossil.getSpecimenTypeDesignations().size());
|
715
|
|
716
|
nameService.deleteTypeDesignation((TaxonName)null, null);
|
717
|
|
718
|
commitAndStartNewTransaction(tableNames);
|
719
|
|
720
|
name1 = this.nameService.load(UUID.fromString("6dbd41d1-fe13-4d9c-bb58-31f051c2c384"));
|
721
|
name2 = this.nameService.load(UUID.fromString("f9e9c13f-5fa5-48d3-88cf-712c921a099e"));
|
722
|
name3 = this.nameService.load(UUID.fromString("e1e66264-f16a-4df9-80fd-6ab5028a3c28"));
|
723
|
specimen1 = CdmBase.deproxy(this.occurrenceService.load(UUID.fromString("0d19a9ca-21a7-4adb-8640-8d6719e15eea")), DerivedUnit.class);
|
724
|
fossil = CdmBase.deproxy(this.occurrenceService.load(UUID.fromString("4c48b7c8-4c8d-4e48-b083-0837fe51a0a9")), DerivedUnit.class);
|
725
|
|
726
|
desigs1 = name1.getTypeDesignations();
|
727
|
desigs2 = name2.getTypeDesignations();
|
728
|
desigs3 = name3.getTypeDesignations();
|
729
|
//nothing should be deleted
|
730
|
Assert.assertEquals("name1 should have 2 type designations", 2, desigs1.size());
|
731
|
Assert.assertEquals("name2 should have 1 type designations", 1, desigs2.size());
|
732
|
Assert.assertEquals("name3 should have 1 type designations", 1, desigs3.size());
|
733
|
Assert.assertEquals("Specimen1 should be used in 1 type designation", 1, specimen1.getSpecimenTypeDesignations().size());
|
734
|
Assert.assertEquals("Fossil should be used in 1 type designation", 1, fossil.getSpecimenTypeDesignations().size());
|
735
|
|
736
|
nameService.deleteTypeDesignation(name1, null);
|
737
|
|
738
|
commitAndStartNewTransaction(tableNames);
|
739
|
|
740
|
name1 = this.nameService.load(UUID.fromString("6dbd41d1-fe13-4d9c-bb58-31f051c2c384"));
|
741
|
name2 = this.nameService.load(UUID.fromString("f9e9c13f-5fa5-48d3-88cf-712c921a099e"));
|
742
|
name3 = this.nameService.load(UUID.fromString("e1e66264-f16a-4df9-80fd-6ab5028a3c28"));
|
743
|
specimen1 = CdmBase.deproxy(this.occurrenceService.load(UUID.fromString("0d19a9ca-21a7-4adb-8640-8d6719e15eea")), DerivedUnit.class);
|
744
|
fossil = CdmBase.deproxy(this.occurrenceService.load(UUID.fromString("4c48b7c8-4c8d-4e48-b083-0837fe51a0a9")), DerivedUnit.class);
|
745
|
|
746
|
desigs1 = name1.getTypeDesignations();
|
747
|
desigs2 = name2.getTypeDesignations();
|
748
|
desigs3 = name3.getTypeDesignations();
|
749
|
//only the types of name1 should be deleted
|
750
|
Assert.assertEquals("name1 should have 0 type designations", 0, desigs1.size());
|
751
|
Assert.assertEquals("name2 should have 1 type designations", 1, desigs2.size());
|
752
|
Assert.assertEquals("name3 should have 1 type designations", 1, desigs3.size());
|
753
|
Assert.assertEquals("Specimen1 should be used in 1 type designation", 1, specimen1.getSpecimenTypeDesignations().size());
|
754
|
Assert.assertEquals("Fossil should be used in 0 type designation", 0, fossil.getSpecimenTypeDesignations().size());
|
755
|
|
756
|
SpecimenTypeDesignation desig2 = (SpecimenTypeDesignation)name2.getTypeDesignations().iterator().next();
|
757
|
nameService.deleteTypeDesignation(name2, desig2);
|
758
|
|
759
|
commitAndStartNewTransaction(tableNames);
|
760
|
|
761
|
name1 = this.nameService.load(UUID.fromString("6dbd41d1-fe13-4d9c-bb58-31f051c2c384"));
|
762
|
name2 = this.nameService.load(UUID.fromString("f9e9c13f-5fa5-48d3-88cf-712c921a099e"));
|
763
|
name3 = this.nameService.load(UUID.fromString("e1e66264-f16a-4df9-80fd-6ab5028a3c28"));
|
764
|
specimen1 = CdmBase.deproxy(this.occurrenceService.load(UUID.fromString("0d19a9ca-21a7-4adb-8640-8d6719e15eea")), DerivedUnit.class);
|
765
|
fossil = CdmBase.deproxy(this.occurrenceService.load(UUID.fromString("4c48b7c8-4c8d-4e48-b083-0837fe51a0a9")), DerivedUnit.class);
|
766
|
|
767
|
desigs1 = name1.getTypeDesignations();
|
768
|
desigs2 = name2.getTypeDesignations();
|
769
|
desigs3 = name3.getTypeDesignations();
|
770
|
|
771
|
Assert.assertEquals("name1 should have 0 type designations", 0, desigs1.size());
|
772
|
Assert.assertEquals("name2 should have 0 type designations", 0, desigs2.size());
|
773
|
Assert.assertEquals("name3 should have 1 type designations", 1, desigs3.size());
|
774
|
Assert.assertEquals("Specimen1 should be used in 0 type designation", 0, specimen1.getSpecimenTypeDesignations().size());
|
775
|
Assert.assertEquals("Fossil should be used in 0 type designation", 0, fossil.getSpecimenTypeDesignations().size());
|
776
|
|
777
|
NameTypeDesignation desig3 = (NameTypeDesignation)name3.getTypeDesignations().iterator().next();
|
778
|
name3.addTypeDesignation(SpecimenTypeDesignation.NewInstance(), false);
|
779
|
this.nameService.update(name3);
|
780
|
|
781
|
this.nameService.load(UUID.fromString("e1e66264-f16a-4df9-80fd-6ab5028a3c28"));
|
782
|
desigs3 = name3.getTypeDesignations();
|
783
|
|
784
|
NameTypeDesignation desigNew = NameTypeDesignation.NewInstance();
|
785
|
commitAndStartNewTransaction(tableNames);
|
786
|
name3 = nameService.load(name3.getUuid());
|
787
|
|
788
|
Assert.assertEquals("name3 should have 2 type designations", 2, desigs3.size());
|
789
|
|
790
|
nameService.deleteTypeDesignation(name3, desig3);
|
791
|
commitAndStartNewTransaction(tableNames);
|
792
|
|
793
|
name1 = this.nameService.load(UUID.fromString("6dbd41d1-fe13-4d9c-bb58-31f051c2c384"));
|
794
|
name2 = this.nameService.load(UUID.fromString("f9e9c13f-5fa5-48d3-88cf-712c921a099e"));
|
795
|
name3 = this.nameService.load(UUID.fromString("e1e66264-f16a-4df9-80fd-6ab5028a3c28"));
|
796
|
specimen1 = CdmBase.deproxy(this.occurrenceService.load(UUID.fromString("0d19a9ca-21a7-4adb-8640-8d6719e15eea")), DerivedUnit.class);
|
797
|
fossil = CdmBase.deproxy(this.occurrenceService.load(UUID.fromString("4c48b7c8-4c8d-4e48-b083-0837fe51a0a9")), DerivedUnit.class);
|
798
|
|
799
|
|
800
|
desigs1 = name1.getTypeDesignations();
|
801
|
desigs2 = name2.getTypeDesignations();
|
802
|
desigs3 = name3.getTypeDesignations();
|
803
|
|
804
|
Assert.assertEquals("name1 should have 0 type designations", 0, desigs1.size());
|
805
|
Assert.assertEquals("name2 should have 0 type designations", 0, desigs2.size());
|
806
|
Assert.assertEquals("name3 should have 0 type designations", 1, desigs3.size());
|
807
|
Assert.assertEquals("Specimen1 should be used in 0 type designation", 0, specimen1.getSpecimenTypeDesignations().size());
|
808
|
Assert.assertEquals("Fossil should be used in 0 type designation", 0, fossil.getSpecimenTypeDesignations().size());
|
809
|
|
810
|
}
|
811
|
|
812
|
@Test
|
813
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
|
814
|
public void testDeleteTypeDesignationWithRegistration() {
|
815
|
final String[] tableNames = new String[]{
|
816
|
"TaxonName","TypeDesignationBase","TaxonName_TypeDesignationBase",
|
817
|
"SpecimenOrObservationBase"};
|
818
|
|
819
|
TaxonName name3 = this.nameService.load(UUID.fromString("e1e66264-f16a-4df9-80fd-6ab5028a3c28"));
|
820
|
|
821
|
Set<TypeDesignationBase> desigs3 = name3.getTypeDesignations();
|
822
|
|
823
|
NameTypeDesignation desig3 = (NameTypeDesignation)name3.getTypeDesignations().iterator().next();
|
824
|
name3.addTypeDesignation(SpecimenTypeDesignation.NewInstance(), false);
|
825
|
this.nameService.update(name3);
|
826
|
|
827
|
name3 = this.nameService.load(UUID.fromString("e1e66264-f16a-4df9-80fd-6ab5028a3c28"));
|
828
|
desigs3 = name3.getTypeDesignations();
|
829
|
|
830
|
NameTypeDesignation desigNew = NameTypeDesignation.NewInstance();
|
831
|
|
832
|
UsernamePasswordAuthenticationToken submiterToken = new UsernamePasswordAuthenticationToken("admin","sPePhAz6");
|
833
|
setAuthentication(submiterToken);
|
834
|
Registration registration = Registration.NewInstance("abc", "abc", name3, null);
|
835
|
registration.addTypeDesignation(desigNew);
|
836
|
UUID uuidReg = registrationService.saveOrUpdate(registration);
|
837
|
unsetAuthentication();
|
838
|
|
839
|
commitAndStartNewTransaction(tableNames);
|
840
|
|
841
|
name3 = nameService.load(name3.getUuid());
|
842
|
|
843
|
Set<Registration> regs = name3.getRegistrations();
|
844
|
desigs3 = name3.getTypeDesignations();
|
845
|
Assert.assertEquals("name3 should have 2 type designations", 2, desigs3.size());
|
846
|
Assert.assertEquals("name should have 1 registrations", 1, regs.size());
|
847
|
Assert.assertEquals("registration should have 1 type designations",1, regs.iterator().next().getTypeDesignations().size());
|
848
|
|
849
|
nameService.deleteTypeDesignation(name3, desig3);
|
850
|
commitAndStartNewTransaction(tableNames);
|
851
|
|
852
|
name3 = this.nameService.load(UUID.fromString("e1e66264-f16a-4df9-80fd-6ab5028a3c28"));
|
853
|
|
854
|
regs = name3.getRegistrations();
|
855
|
Assert.assertEquals("name3 should have 1 registration", 1, regs.size());
|
856
|
Assert.assertEquals("registration should have 1 type designations",1, regs.iterator().next().getTypeDesignations().size());
|
857
|
|
858
|
desigs3 = name3.getTypeDesignations();
|
859
|
|
860
|
nameService.deleteTypeDesignation(name3, desigNew);
|
861
|
commitAndStartNewTransaction(tableNames);
|
862
|
name3 = this.nameService.load(UUID.fromString("e1e66264-f16a-4df9-80fd-6ab5028a3c28"));
|
863
|
regs = name3.getRegistrations();
|
864
|
desigs3 = name3.getTypeDesignations();
|
865
|
|
866
|
Assert.assertEquals("name3 should have 1 type designations", 1, desigs3.size());
|
867
|
Assert.assertEquals("registration should have 0 type designations",0, regs.iterator().next().getTypeDesignations().size());
|
868
|
}
|
869
|
|
870
|
|
871
|
@Test
|
872
|
@DataSet
|
873
|
public void testDeleteTypeDesignationAllNames() {
|
874
|
final String[] tableNames = new String[]{
|
875
|
"TaxonName","TypeDesignationBase",
|
876
|
"TaxonName_TypeDesignationBase","SpecimenOrObservationBase"};
|
877
|
|
878
|
|
879
|
TaxonName name1 = this.nameService.load(UUID.fromString("6dbd41d1-fe13-4d9c-bb58-31f051c2c384"));
|
880
|
TaxonName name2 = this.nameService.load(UUID.fromString("f9e9c13f-5fa5-48d3-88cf-712c921a099e"));
|
881
|
TaxonName name3 = this.nameService.load(UUID.fromString("e1e66264-f16a-4df9-80fd-6ab5028a3c28"));
|
882
|
DerivedUnit specimen1 = CdmBase.deproxy(this.occurrenceService.load(UUID.fromString("0d19a9ca-21a7-4adb-8640-8d6719e15eea")), DerivedUnit.class);
|
883
|
DerivedUnit fossil = CdmBase.deproxy(this.occurrenceService.load(UUID.fromString("4c48b7c8-4c8d-4e48-b083-0837fe51a0a9")), DerivedUnit.class);
|
884
|
|
885
|
Set<TypeDesignationBase> desigs1 = name1.getTypeDesignations();
|
886
|
Set<TypeDesignationBase> desigs2 = name2.getTypeDesignations();
|
887
|
Set<TypeDesignationBase> desigs3 = name3.getTypeDesignations();
|
888
|
|
889
|
Assert.assertEquals("name1 should have 2 type designations", 2, desigs1.size());
|
890
|
Assert.assertEquals("name2 should have 1 type designations", 1, desigs2.size());
|
891
|
Assert.assertEquals("name3 should have 1 type designations", 1, desigs3.size());
|
892
|
Assert.assertEquals("Specimen1 should be used in 1 type designation", 1, specimen1.getSpecimenTypeDesignations().size());
|
893
|
Assert.assertEquals("Fossil should be used in 1 type designation", 1, fossil.getSpecimenTypeDesignations().size());
|
894
|
|
895
|
SpecimenTypeDesignation desig2 = (SpecimenTypeDesignation)name2.getTypeDesignations().iterator().next();
|
896
|
|
897
|
nameService.deleteTypeDesignation(null, desig2);
|
898
|
|
899
|
commitAndStartNewTransaction(tableNames);
|
900
|
|
901
|
name1 = this.nameService.load(UUID.fromString("6dbd41d1-fe13-4d9c-bb58-31f051c2c384"));
|
902
|
name2 = this.nameService.load(UUID.fromString("f9e9c13f-5fa5-48d3-88cf-712c921a099e"));
|
903
|
name3 = this.nameService.load(UUID.fromString("e1e66264-f16a-4df9-80fd-6ab5028a3c28"));
|
904
|
specimen1 = CdmBase.deproxy(this.occurrenceService.load(UUID.fromString("0d19a9ca-21a7-4adb-8640-8d6719e15eea")), DerivedUnit.class);
|
905
|
fossil = CdmBase.deproxy(this.occurrenceService.load(UUID.fromString("4c48b7c8-4c8d-4e48-b083-0837fe51a0a9")), DerivedUnit.class);
|
906
|
|
907
|
desigs1 = name1.getTypeDesignations();
|
908
|
desigs2 = name2.getTypeDesignations();
|
909
|
desigs3 = name3.getTypeDesignations();
|
910
|
|
911
|
Assert.assertEquals("name1 should have 1 type designations", 1, desigs1.size());
|
912
|
Assert.assertEquals("name2 should have 0 type designations", 0, desigs2.size());
|
913
|
Assert.assertEquals("name3 should have 1 type designations", 1, desigs3.size());
|
914
|
Assert.assertEquals("Specimen1 should be used in 0 type designation", 0, specimen1.getSpecimenTypeDesignations().size());
|
915
|
Assert.assertEquals("Fossil should be used in 1 type designation", 1, fossil.getSpecimenTypeDesignations().size());
|
916
|
|
917
|
}
|
918
|
|
919
|
@Test
|
920
|
@DataSet
|
921
|
public void findByTitleWithRestrictions(){
|
922
|
|
923
|
// The following typeDesignations per name are assumed:
|
924
|
// Name1 -> SpecimenTypeDesignation -> Specimen1
|
925
|
// -> SpecimenTypeDesignation -> Specimen2
|
926
|
// Name2 -> SpecimenTypeDesignation -> Specimen2
|
927
|
|
928
|
// Logger.getLogger("org.hibernate.SQL").setLevel(Level.TRACE);
|
929
|
|
930
|
|
931
|
List<Restriction<?>> restrictions;
|
932
|
Pager<TaxonName> result;
|
933
|
|
934
|
result = nameService.findByTitleWithRestrictions(null, "Name1", MatchMode.EXACT, null, null, null, null, null);
|
935
|
assertEquals(1l, result.getCount().longValue());
|
936
|
assertEquals("Name1", result.getRecords().iterator().next().getTitleCache());
|
937
|
|
938
|
result = nameService.findByTitleWithRestrictions(null, "Name", MatchMode.BEGINNING, null, null, null, null, null);
|
939
|
assertEquals(3l, result.getCount().longValue());
|
940
|
|
941
|
restrictions = Arrays.asList(new Restriction<UUID>("typeDesignations.uuid", null, UUID.fromString("9bbda70b-7272-4e65-a807-852a3f2eba63")));
|
942
|
result = nameService.findByTitleWithRestrictions(null, "Name", MatchMode.BEGINNING, restrictions, null, null, null, null);
|
943
|
assertEquals(1l, result.getCount().longValue());
|
944
|
assertEquals("Name1", result.getRecords().iterator().next().getTitleCache());
|
945
|
|
946
|
result = nameService.findByTitleWithRestrictions(null, "me1", MatchMode.END, restrictions, null, null, null, null);
|
947
|
assertEquals(1l, result.getCount().longValue());
|
948
|
assertEquals("Name1", result.getRecords().iterator().next().getTitleCache());
|
949
|
|
950
|
result = nameService.findByTitleWithRestrictions(null, "Name2", MatchMode.EXACT, restrictions, null, null, null, null);
|
951
|
assertEquals(0l, result.getCount().longValue());
|
952
|
|
953
|
// Logger.getLogger("eu.etaxonomy.cdm.persistence.dao.hibernate.common").setLevel(Level.DEBUG);
|
954
|
|
955
|
restrictions = Arrays.asList(new Restriction<String>("typeDesignations.typeSpecimen.titleCache", MatchMode.EXACT, "Specimen2"));
|
956
|
result = nameService.findByTitleWithRestrictions(null, "Name2", MatchMode.EXACT, restrictions, null, null, null, null);
|
957
|
assertEquals(1l, result.getCount().longValue());
|
958
|
assertEquals("Name2", result.getRecords().iterator().next().getTitleCache());
|
959
|
|
960
|
restrictions = Arrays.asList(new Restriction<String>("typeDesignations.typeSpecimen.titleCache", MatchMode.EXACT, "Specimen1"));
|
961
|
result = nameService.findByTitleWithRestrictions(null, "Name2", MatchMode.EXACT, restrictions, null, null, null, null);
|
962
|
assertEquals(0l, result.getCount().longValue());
|
963
|
|
964
|
restrictions = Arrays.asList(new Restriction<String>("typeDesignations.typeSpecimen.titleCache", Restriction.Operator.OR, MatchMode.EXACT, "Specimen1"));
|
965
|
result = nameService.findByTitleWithRestrictions(null, "Name2", MatchMode.EXACT, restrictions, null, null, null, null);
|
966
|
assertEquals(2l, result.getCount().longValue());
|
967
|
|
968
|
restrictions = Arrays.asList(new Restriction<String>("typeDesignations.typeSpecimen.titleCache", MatchMode.BEGINNING, "Specimen"));
|
969
|
result = nameService.findByTitleWithRestrictions(null, "Name1", MatchMode.EXACT, restrictions, null, null, null, null);
|
970
|
assertEquals("names with multiple matching typeSpecimens must be deduplicated", 1l, result.getCount().longValue());
|
971
|
assertEquals("Name1", result.getRecords().iterator().next().getTitleCache());
|
972
|
|
973
|
restrictions = Arrays.asList(new Restriction<String>("typeDesignations.typeSpecimen.titleCache", MatchMode.BEGINNING, "Specimen"));
|
974
|
result = nameService.findByTitleWithRestrictions(null, "Name", MatchMode.BEGINNING, restrictions, null, null, null, null);
|
975
|
assertEquals(2l, result.getCount().longValue());
|
976
|
|
977
|
}
|
978
|
|
979
|
@Test
|
980
|
@DataSet
|
981
|
public void testFindByTitleTitleWithRestrictionsMultiValue(){
|
982
|
|
983
|
// The following typedesigbnations per name are assumed:
|
984
|
// Name1 -> SpecimenTypeDesignation -> Specimen1
|
985
|
// -> SpecimenTypeDesignation -> Specimen2
|
986
|
// Name2 -> SpecimenTypeDesignation -> Specimen2
|
987
|
// Name3 -> NameTypeDesignaton-> typeName = Name1
|
988
|
|
989
|
List<Restriction<?>> restrictions;
|
990
|
Pager<TaxonName> result;
|
991
|
|
992
|
restrictions = Arrays.asList(new Restriction<UUID>("typeDesignations.uuid", null, UUID.fromString("9bbda70b-7272-4e65-a807-852a3f2eba63"), UUID.fromString("1357c307-00c3-499c-8e20-0849d4706125")));
|
993
|
result = nameService.findByTitleWithRestrictions(null, "Name", MatchMode.BEGINNING, restrictions, null, null, null, null);
|
994
|
assertEquals(2l, result.getCount().longValue());
|
995
|
|
996
|
restrictions = Arrays.asList(new Restriction<String>("typeDesignations.typeSpecimen.titleCache", null, "Specimen1", "Specimen2"));
|
997
|
result = nameService.findByTitleWithRestrictions(null, "Name1", MatchMode.EXACT, restrictions, null, null, null, null);
|
998
|
assertEquals("names with multiple matching typeSpecimens must be distinct", 1l, result.getCount().longValue());
|
999
|
assertEquals("Name1", result.getRecords().iterator().next().getTitleCache());
|
1000
|
}
|
1001
|
|
1002
|
@Test
|
1003
|
@DataSet
|
1004
|
public void testFindByTitleTitleWithRestrictionsLogicalOperators(){
|
1005
|
|
1006
|
// The following typedesigbnations per name are assumed:
|
1007
|
// Name1 -> SpecimenTypeDesignation -> Specimen1
|
1008
|
// -> SpecimenTypeDesignation -> Specimen2
|
1009
|
// Name2 -> SpecimenTypeDesignation -> Specimen2
|
1010
|
// Name3 -> NameTypeDesignaton-> typeName = Name1
|
1011
|
|
1012
|
List<Restriction<?>> restrictions;
|
1013
|
Pager<TaxonName> result;
|
1014
|
|
1015
|
//Logger.getLogger("org.hibernate.SQL").setLevel(Level.TRACE);
|
1016
|
|
1017
|
restrictions = Arrays.asList(new Restriction<String>("typeDesignations.typeName.titleCache", Operator.AND, null, "Name1"));
|
1018
|
result = nameService.findByTitleWithRestrictions(null, "Name3", MatchMode.EXACT, restrictions, null, null, null, null);
|
1019
|
assertEquals(1l, result.getCount().longValue());
|
1020
|
|
1021
|
restrictions = Arrays.asList(new Restriction<String>("typeDesignations.typeName.titleCache", Operator.AND_NOT, null, "Name1"));
|
1022
|
result = nameService.findByTitleWithRestrictions(null, "Name", MatchMode.BEGINNING, restrictions, null, null, null, null);
|
1023
|
assertEquals(2l, result.getCount().longValue());
|
1024
|
|
1025
|
restrictions = Arrays.asList(new Restriction<String>("titleCache", Operator.AND_NOT, null, "Name1", "Name2"));
|
1026
|
result = nameService.findByTitleWithRestrictions(null, "Name", MatchMode.BEGINNING, restrictions, null, null, null, null);
|
1027
|
assertEquals(1l, result.getCount().longValue());
|
1028
|
assertEquals("Name3", result.getRecords().get(0).getTitleCache());
|
1029
|
|
1030
|
restrictions = Arrays.asList(new Restriction<String>("typeDesignations.typeName.titleCache", Operator.OR, null, "Name1"));
|
1031
|
result = nameService.findByTitleWithRestrictions(null, "Name1", MatchMode.EXACT, restrictions, null, null, null, null);
|
1032
|
assertEquals(2l, result.getCount().longValue());
|
1033
|
|
1034
|
restrictions = Arrays.asList(new Restriction<String>("typeDesignations.typeName.titleCache", Operator.OR_NOT, null, "Name1"));
|
1035
|
result = nameService.findByTitleWithRestrictions(null, "Name1", MatchMode.EXACT, restrictions, null, null, null, null);
|
1036
|
assertEquals(2l, result.getCount().longValue());
|
1037
|
|
1038
|
}
|
1039
|
|
1040
|
@Test
|
1041
|
@DataSet
|
1042
|
public void testFindByTitleTitle_using_Configurator(){
|
1043
|
|
1044
|
String searchString = "*";
|
1045
|
IdentifiableServiceConfiguratorImpl<TaxonName> searchConfigurator = new IdentifiableServiceConfiguratorImpl<TaxonName>();
|
1046
|
|
1047
|
searchConfigurator = IdentifiableServiceConfiguratorFactory.getConfigurator(TaxonName.class);
|
1048
|
searchConfigurator.setTitleSearchString(searchString);
|
1049
|
searchConfigurator.setMatchMode(null);
|
1050
|
searchConfigurator.setOrderHints(OrderHint.ORDER_BY_TITLE_CACHE.asList());
|
1051
|
|
1052
|
Pager<TaxonName> results = nameService.findByTitle(searchConfigurator);
|
1053
|
assertTrue(results.getRecords().size() > 0);
|
1054
|
|
1055
|
}
|
1056
|
|
1057
|
private Rank getSpeciesRank() {
|
1058
|
return (Rank)termService.find(Rank.uuidSpecies);
|
1059
|
}
|
1060
|
|
1061
|
private Rank getGenusRank() {
|
1062
|
return (Rank)termService.find(Rank.uuidGenus);
|
1063
|
}
|
1064
|
|
1065
|
/* (non-Javadoc)
|
1066
|
* @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
|
1067
|
*/
|
1068
|
@Override
|
1069
|
public void createTestDataSet() throws FileNotFoundException {}
|
1070
|
|
1071
|
|
1072
|
}
|