Merge branch 'release/5.45.0'
[cdmlib.git] / cdmlib-services / src / test / java / eu / etaxonomy / cdm / api / service / TaxonServiceImplTest.java
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.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15
16 import java.io.FileNotFoundException;
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Set;
21 import java.util.UUID;
22
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.unitils.dbunit.annotation.DataSet;
28 import org.unitils.spring.annotation.SpringBeanByType;
29
30 import eu.etaxonomy.cdm.api.service.config.IdentifiableServiceConfiguratorImpl;
31 import eu.etaxonomy.cdm.api.service.config.IncludedTaxonConfiguration;
32 import eu.etaxonomy.cdm.api.service.config.MatchingTaxonConfigurator;
33 import eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator;
34 import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
35 import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
36 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
37 import eu.etaxonomy.cdm.api.service.dto.IncludedTaxaDTO;
38 import eu.etaxonomy.cdm.api.service.exception.HomotypicalGroupChangeException;
39 import eu.etaxonomy.cdm.api.service.pager.Pager;
40 import eu.etaxonomy.cdm.model.agent.Person;
41 import eu.etaxonomy.cdm.model.common.Annotation;
42 import eu.etaxonomy.cdm.model.common.CdmBase;
43 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
44 import eu.etaxonomy.cdm.model.common.Language;
45 import eu.etaxonomy.cdm.model.common.LanguageString;
46 import eu.etaxonomy.cdm.model.common.Marker;
47 import eu.etaxonomy.cdm.model.common.MarkerType;
48 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
49 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
50 import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
51 import eu.etaxonomy.cdm.model.description.TaxonDescription;
52 import eu.etaxonomy.cdm.model.description.TextData;
53 import eu.etaxonomy.cdm.model.metadata.SecReferenceHandlingEnum;
54 import eu.etaxonomy.cdm.model.metadata.SecReferenceHandlingSwapEnum;
55 import eu.etaxonomy.cdm.model.name.IBotanicalName;
56 import eu.etaxonomy.cdm.model.name.INonViralName;
57 import eu.etaxonomy.cdm.model.name.NameRelationship;
58 import eu.etaxonomy.cdm.model.name.NameRelationshipType;
59 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
60 import eu.etaxonomy.cdm.model.name.Rank;
61 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
62 import eu.etaxonomy.cdm.model.name.TaxonName;
63 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
64 import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
65 import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
66 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
67 import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
68 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
69 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
70 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
71 import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
72 import eu.etaxonomy.cdm.model.reference.Reference;
73 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
74 import eu.etaxonomy.cdm.model.taxon.Classification;
75 import eu.etaxonomy.cdm.model.taxon.Synonym;
76 import eu.etaxonomy.cdm.model.taxon.SynonymType;
77 import eu.etaxonomy.cdm.model.taxon.Taxon;
78 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
79 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
80 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
81 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
82 import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
83 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
84 import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
85
86 /**
87 * @author a.mueller
88 */
89 public class TaxonServiceImplTest extends CdmTransactionalIntegrationTest {
90
91 private static final Logger logger = LogManager.getLogger();
92
93 @SpringBeanByType
94 private ITaxonService service;
95
96 @SpringBeanByType
97 private INameService nameService;
98
99 @SpringBeanByType
100 private IReferenceService referenceService;
101
102 @SpringBeanByType
103 private IClassificationService classificationService;
104
105 @SpringBeanByType
106 private ITaxonNodeService nodeService;
107
108 @SpringBeanByType
109 private IDescriptionService descriptionService;
110
111 @SpringBeanByType
112 private IDescriptionElementService descriptionElementService;
113
114 @SpringBeanByType
115 private IMarkerService markerService;
116
117 @SpringBeanByType
118 private IEventBaseService eventService;
119
120 @SpringBeanByType
121 private IOccurrenceService occurenceService;
122
123 private Synonym synonym;
124 private Synonym synonym2;
125
126 private Taxon taxWithSyn;
127 private Taxon tax2WithSyn;
128 private Taxon taxWithoutSyn;
129 private UUID uuidSyn;
130 private UUID uuidTaxWithoutSyn;
131 private UUID uuidSyn2;
132 private UUID uuidTaxWithSyn;
133
134 private static String[] genera = {"Carex", "Abies", "Belladonna", "Dracula", "Maria", "Calendula", "Polygala", "Vincia"};
135 private static String[] epitheta = {"vulgaris", "magdalena", "officinalis", "alba", "negra", "communa", "alpina", "rotundifolia", "greutheriana", "helventica", "allemania", "franca"};
136 private static String[] ranks = {"subsp", "var", "f"};
137
138 public static UUID GENUS_NAME_UUID = UUID.fromString("8d761fc4-b509-42f4-9568-244161934336");
139 public static UUID GENUS_UUID = UUID.fromString("bf4298a8-1735-4353-a210-244442e1bd62");
140 public static UUID BASIONYM_UUID = UUID.fromString("7911c51d-ccb7-4708-8992-639eae58a0e3");
141 public static UUID SPECIES1_UUID = UUID.fromString("f0eb77d9-76e0-47f4-813f-9b5605b78685");
142 public static UUID SPECIES1_NAME_UUID = UUID.fromString("efd78713-126f-42e1-9070-a1ff83f12abf");
143 public static UUID SYNONYM_NAME_UUID = UUID.fromString("b9cbaa74-dbe0-4930-8050-b7754ce85dc0");
144 public static UUID SPECIES2_NAME_UUID = UUID.fromString("0267ab67-483e-4da5-b654-11013b242c22");
145 public static UUID SPECIES2_UUID = UUID.fromString("e20eb549-ced6-4e79-9d74-44f0792a4929");
146 public static UUID SYNONYM2_NAME_UUID = UUID.fromString("7c17c811-4201-454b-8108-7be7c91c0938");
147 public static UUID SYNONYM2_UUID = UUID.fromString("2520b103-bd89-4ac1-99e4-e3bfcedfd4eb");
148 public static UUID SPECIES5_NAME_UUID = UUID.fromString("0c6ecaac-804d-49e5-a33f-1b7ee77439e3");
149
150 /****************** TESTS *****************************/
151
152
153 /**
154 * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#getTaxonByUuid(java.util.UUID)}.
155 */
156 @Test
157 public final void testGetTaxonByUuid() {
158 Taxon expectedTaxon = Taxon.NewInstance(null, null);
159 UUID uuid = service.save(expectedTaxon).getUuid();
160 TaxonBase<?> actualTaxon = service.find(uuid);
161 assertEquals(expectedTaxon, actualTaxon);
162 }
163
164 /**
165 * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#getTaxonByUuid(java.util.UUID)}.
166 */
167 @Test
168 public final void testGetTaxonByTitle() {
169 TaxonName name = TaxonName.NewInstance(NomenclaturalCode.ICNAFP, Rank.SPECIES(), "Abies", null, "alba", null, null, null, null, null);
170 Taxon expectedTaxon = Taxon.NewInstance(name, null);
171 expectedTaxon.setDoubtful(true);
172 service.save(expectedTaxon);
173 IdentifiableServiceConfiguratorImpl<TaxonBase> config = new IdentifiableServiceConfiguratorImpl<TaxonBase>();
174 config.setTitleSearchString("Abies alba*");
175 //doubtful taxa should be found
176 Pager<TaxonBase> actualTaxa = service.findByTitle(config);
177 assertEquals(expectedTaxon, actualTaxa.getRecords().get(0));
178
179 //and other taxa as well
180 expectedTaxon.setDoubtful(false);
181 service.saveOrUpdate(expectedTaxon);
182 actualTaxa = service.findByTitle(config);
183 assertEquals(expectedTaxon, actualTaxa.getRecords().get(0));
184 }
185
186
187 /**
188 * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#saveTaxon(eu.etaxonomy.cdm.model.taxon.TaxonBase)}.
189 */
190 @Test
191 public final void testSaveTaxon() {
192 Taxon expectedTaxon = Taxon.NewInstance(null, null);
193 UUID uuid = service.save(expectedTaxon).getUuid();
194 TaxonBase<?> actualTaxon = service.find(uuid);
195 assertEquals(expectedTaxon, actualTaxon);
196 }
197
198 @Test
199 public final void testSaveOrUpdateTaxon() {
200 Taxon expectedTaxon = Taxon.NewInstance(null, null);
201 UUID uuid = service.save(expectedTaxon).getUuid();
202 TaxonBase<?> actualTaxon = service.find(uuid);
203 assertEquals(expectedTaxon, actualTaxon);
204
205 actualTaxon.setName(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()));
206 try{
207 service.saveOrUpdate(actualTaxon);
208 }catch(Exception e){
209 Assert.fail();
210 }
211 }
212
213 @Test
214 public final void testSaveOrUpdateTaxonWithMisappliedName() {
215 Taxon expectedTaxon = Taxon.NewInstance(null, null);
216 TaxonName misappliedNameName = TaxonName.NewInstance(NomenclaturalCode.ICNAFP, Rank.SPECIES(), "Abies", null, "alba", null, null, null, null, null);
217
218 UUID misappliedNameNameUuid = nameService.save(misappliedNameName).getUuid();
219 misappliedNameName = nameService.find(misappliedNameNameUuid);
220 SpecimenTypeDesignation typedes = SpecimenTypeDesignation.NewInstance();
221 DerivedUnit derivedUnit = DerivedUnit.NewPreservedSpecimenInstance();
222 FieldUnit fieldUnit = FieldUnit.NewInstance();
223 DerivationEvent derivationEvent = DerivationEvent.NewSimpleInstance(fieldUnit, derivedUnit, DerivationEventType.ACCESSIONING());
224 // derivedUnit.addDerivationEvent(derivationEvent);
225 typedes.setTypeSpecimen(derivedUnit);
226 misappliedNameName.addTypeDesignation(typedes, false);
227 Taxon misappliedName = Taxon.NewInstance(misappliedNameName, null);
228 UUID misappliedNameUuid = service.save(misappliedName).getUuid();
229 misappliedName = (Taxon) service.find(misappliedNameUuid);
230 expectedTaxon.addMisappliedName(misappliedName, null, null);
231 UUID uuid = service.save(expectedTaxon).getUuid();
232 TaxonBase<?> actualTaxon = service.find(uuid);
233 assertEquals(expectedTaxon, actualTaxon);
234 misappliedName.setSec(ReferenceFactory.newArticle());
235
236 try{
237 service.saveOrUpdate(actualTaxon);
238 misappliedName = (Taxon)service.find(misappliedNameUuid);
239 Assert.assertNotNull(misappliedName.getSec());
240 }catch(Exception e){
241 Assert.fail();
242 }
243 commitAndStartNewTransaction(null);
244 actualTaxon = service.find(uuid);
245 ((Taxon)actualTaxon).getTaxonRelations(misappliedName).iterator().next().getFromTaxon().setSec(null);
246 try{
247 service.saveOrUpdate(actualTaxon);
248 misappliedName = (Taxon)service.find(misappliedNameUuid);
249 Assert.assertNull(misappliedName.getSec());
250 }catch(Exception e){
251 Assert.fail();
252 }
253 }
254
255
256 @Test
257 public final void testSwapSynonymAndAcceptedTaxon() throws FileNotFoundException{
258
259 createTestDataSet();
260 synonym.setSec(ReferenceFactory.newArticle());
261 service.saveOrUpdate(synonym);
262 UpdateResult result = service.swapSynonymAndAcceptedTaxon(synonym, taxWithSyn, true, false, SecReferenceHandlingSwapEnum.AlwaysDelete, null, null);
263
264 // find forces flush
265 Taxon tax = (Taxon)service.find(result.getCdmEntity().getUuid());
266 MatchingTaxonConfigurator configurator = MatchingTaxonConfigurator.NewInstance();
267 configurator.setTaxonNameTitle("Test3");
268 List<TaxonBase> synList = service.findTaxaByName(configurator);
269
270 if (synList.size() > 0){
271 TaxonBase<?> syn = synList.get(0);
272 assertTrue(tax.getSynonyms().contains(syn));
273 }else{
274 Assert.fail("There should be a synonym with name Test3");
275 }
276 assertTrue(tax.getName().getTitleCache().equals("Test2"));
277 }
278
279 @Test
280 public final void testSwapSynonymAndAcceptedTaxonNewUuid() throws FileNotFoundException{
281
282 createTestDataSet();
283 synonym.setSec(ReferenceFactory.newArticle());
284 service.saveOrUpdate(synonym);
285 UpdateResult result = service.swapSynonymAndAcceptedTaxon(synonym, taxWithSyn, true, true, SecReferenceHandlingSwapEnum.AlwaysDelete, null, null);
286
287 // find forces flush
288 Taxon tax = (Taxon)service.find(result.getCdmEntity().getUuid());
289 MatchingTaxonConfigurator configurator = MatchingTaxonConfigurator.NewInstance();
290 configurator.setTaxonNameTitle("Test3");
291 @SuppressWarnings("rawtypes")
292 List<TaxonBase> synList = service.findTaxaByName(configurator);
293
294 if (synList.size() > 0){
295 TaxonBase<?> syn = synList.get(0);
296 assertTrue(tax.getSynonyms().contains(syn));
297 }else{
298 Assert.fail("There should be a synonym with name Test3");
299 }
300 assertTrue(tax.getName().getTitleCache().equals("Test2"));
301 }
302
303 @Test
304 public final void testChangeSynonymToAcceptedTaxon() throws FileNotFoundException{
305
306 createTestDataSet();
307
308 UpdateResult result = new UpdateResult();
309 try {
310 result = service.changeSynonymToAcceptedTaxon(synonym, taxWithSyn, null, null, null, true);
311 } catch (HomotypicalGroupChangeException e) {
312 Assert.fail("Invocation of change method should not throw an exception");
313 }
314 taxWithSyn = null;
315 //test flush (resave deleted object)
316 TaxonBase<?> syn = service.find(uuidSyn);
317 taxWithSyn = (Taxon)service.find(uuidTaxWithSyn);
318 Taxon taxNew = (Taxon)service.find(result.getCdmEntity().getUuid());
319 assertNull(syn);
320 assertNotNull(taxWithSyn);
321 assertNotNull(taxNew);
322
323 Assert.assertEquals("New taxon should have 1 synonym relationship (the old homotypic synonym)", 1, ((Taxon)result.getCdmEntity()).getSynonyms().size());
324 }
325
326 @Test
327 public final void testChangeSynonymToAcceptedTaxonWithSecHandlingAlwaysDelete(){
328 Taxon genus = getTestTaxon();
329 TaxonNode node = genus.getTaxonNodes().iterator().next();
330
331 UpdateResult result = new UpdateResult();
332 try {
333 result = service.changeSynonymToAcceptedTaxon(SYNONYM2_UUID, SPECIES2_UUID, node.getUuid(), null, null, SecReferenceHandlingEnum.AlwaysDelete, true);
334 } catch (HomotypicalGroupChangeException e) {
335 Assert.fail("Invocation of change method should not throw an exception");
336 }
337 taxWithSyn = null;
338 //test flush (resave deleted object)
339 TaxonBase<?> syn = service.find(SYNONYM2_UUID);
340 taxWithSyn = (Taxon)service.find(SPECIES2_UUID);
341 TaxonNode taxNodeNew = nodeService.find(result.getCdmEntity().getUuid());
342 Taxon taxNew = taxNodeNew.getTaxon();
343 assertNull(syn);
344 assertNotNull(taxWithSyn);
345 assertNotNull(taxNew);
346 assertNull(taxNew.getSec());
347
348
349 }
350
351
352
353 @Test
354 public final void testChangeSynonymToAcceptedTaxonWithSecHandlingUseNewParentSec(){
355 Taxon genus = getTestTaxon();
356 TaxonNode node = genus.getTaxonNodes().iterator().next();
357 UpdateResult result = new UpdateResult();
358 TaxonBase<?> syn = service.find(SYNONYM2_UUID);
359 Reference sec = ReferenceFactory.newBook();
360 sec.setTitleCache("Flora Cuba", true);
361 syn.setSec(sec);
362 service.saveOrUpdate(syn);
363 try {
364 result = service.changeSynonymToAcceptedTaxon(SYNONYM2_UUID, SPECIES2_UUID, node.getUuid(), null, null, SecReferenceHandlingEnum.UseNewParentSec, true);
365 } catch (HomotypicalGroupChangeException e) {
366 Assert.fail("Invocation of change method should not throw an exception");
367 }
368 taxWithSyn = null;
369 //test flush (resave deleted object)
370 syn = service.find(SYNONYM2_UUID);
371 taxWithSyn = (Taxon)service.find(SPECIES2_UUID);
372 TaxonNode taxNodeNew = nodeService.find(result.getCdmEntity().getUuid());
373 Taxon taxNew = taxNodeNew.getTaxon();
374 assertNull(syn);
375 assertNotNull(taxWithSyn);
376 assertNotNull(taxNew);
377 assertNotNull(taxNew.getSec());
378 assertEquals(taxWithSyn.getSec(), taxNew.getSec());
379
380 }
381
382 @Test
383 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testMoveSynonymToAnotherTaxon.xml")
384 public final void testChangeSynonymToAcceptedTaxonWithSecHandlingWarningSelect(){
385 Taxon genus = getTestTaxon();
386 TaxonNode node = genus.getTaxonNodes().iterator().next();
387
388 UpdateResult result = new UpdateResult();
389 TaxonBase<?> syn = service.find(SYNONYM2_UUID);
390 Reference sec = ReferenceFactory.newBook();
391 sec.setTitleCache("Flora Cuba", true);
392 Reference newSec = ReferenceFactory.newBook();
393 newSec.setTitleCache("Flora Hawaii", true);
394 UUID newSecUuid = referenceService.saveOrUpdate(newSec);
395 syn.setSec(sec);
396 service.saveOrUpdate(syn);
397 try {
398 result = service.changeSynonymToAcceptedTaxon(SYNONYM2_UUID, SPECIES2_UUID, node.getUuid(), newSecUuid, "23", SecReferenceHandlingEnum.KeepOrSelect, true);
399 } catch (HomotypicalGroupChangeException e) {
400 Assert.fail("Invocation of change method should not throw an exception");
401 }
402 taxWithSyn = null;
403 //test flush (resave deleted object)
404 syn = service.find(SYNONYM2_UUID);
405 taxWithSyn = (Taxon)service.find(SPECIES2_UUID);
406 TaxonNode taxNodeNew = nodeService.find(result.getCdmEntity().getUuid());
407 Taxon taxNew = taxNodeNew.getTaxon();
408 assertNull(syn);
409 assertNotNull(taxWithSyn);
410 assertNotNull(taxNew);
411 assertNotNull(taxNew.getSec());
412 assertEquals(newSec, taxNew.getSec());
413
414 }
415
416
417 @Test
418 public final void testChangeSynonymToAcceptedTaxonSynonymForTwoTaxa(){
419 try {
420 createTestDataSet();
421 } catch (FileNotFoundException e1) {
422 // TODO Auto-generated catch block
423 e1.printStackTrace();
424 }
425
426
427 Taxon taxon = null;
428 UpdateResult result = new UpdateResult();
429 try {
430 result = service.changeSynonymToAcceptedTaxon(synonym, taxWithSyn, null, null, null, true);
431 service.save(taxon);
432 } catch (HomotypicalGroupChangeException e) {
433 Assert.fail("Invocation of change method should not throw an exception");
434 }
435 taxWithSyn = null;
436 tax2WithSyn = null;
437
438 //test flush (resave deleted object)
439 TaxonBase<?> syn = service.find(uuidSyn);
440 taxWithSyn = (Taxon)service.find(uuidTaxWithSyn);
441 Taxon taxNew = (Taxon)service.find(((Taxon)result.getCdmEntity()).getUuid());
442 assertNull(syn);
443 assertNotNull(taxWithSyn);
444 assertNotNull(taxNew);
445
446 // Assert.assertEquals("New taxon should have 1 synonym relationship (the old homotypic synonym)", 1, taxon.getSynonymRelations().size());
447 }
448
449 /**
450 * Old implementation taken from {@link TaxonServiceImplBusinessTest} for old version of method.
451 */
452 @Test
453 public final void testMoveSynonymToAnotherTaxon_OLD() {
454 SynonymType heteroTypicSynonymType = SynonymType.HETEROTYPIC_SYNONYM_OF;
455 Reference reference = ReferenceFactory.newGeneric();
456 String referenceDetail = "test";
457
458 INonViralName t1n = TaxonNameFactory.NewNonViralInstance(null);
459 Taxon t1 = Taxon.NewInstance(t1n, reference);
460 INonViralName t2n = TaxonNameFactory.NewNonViralInstance(null);
461 Taxon t2 = Taxon.NewInstance(t2n, reference);
462 INonViralName s1n = TaxonNameFactory.NewNonViralInstance(null);
463 Synonym s1 = Synonym.NewInstance(s1n, reference);
464 t1.addSynonym(s1, heteroTypicSynonymType);
465 service.saveOrUpdate(t1);
466
467 Synonym synonym = t1.getSynonyms().iterator().next();
468
469 boolean keepReference = false;
470 boolean moveHomotypicGroup = false;
471 try {
472 service.moveSynonymToAnotherTaxon(synonym, t2, moveHomotypicGroup, heteroTypicSynonymType, reference.getUuid(), referenceDetail, keepReference);
473 } catch (HomotypicalGroupChangeException e) {
474 Assert.fail("Method call should not throw exception");
475 }
476
477 Assert.assertTrue("t1 should have no synonyms", t1.getSynonyms().isEmpty());
478
479 Set<Synonym> synonyms = t2.getSynonyms();
480 Assert.assertTrue("t2 should have exactly one synonym", synonyms.size() == 1);
481
482 synonym = synonyms.iterator().next();
483
484 Assert.assertEquals(t2, synonym.getAcceptedTaxon());
485 Assert.assertEquals(heteroTypicSynonymType, synonym.getType());
486 Assert.assertEquals(reference, synonym.getSec());
487 Assert.assertEquals(referenceDetail, synonym.getSecMicroReference());
488 }
489
490 @Test
491 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testMoveSynonymToAnotherTaxon.xml")
492 public final void testMoveSynonymToAnotherTaxon() throws Exception {
493 final String[] tableNames = new String[]{};
494
495 // printDataSet(System.err, new String[]{"AgentBase", "TaxonBase"});
496 // printDataSet(System.err, new String[]{"TaxonNode"});
497
498 UUID uuidNewTaxon = UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
499 UUID uuidOldTaxon = UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
500 UUID uuidSyn1 = UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
501 UUID uuidSyn3 = UUID.fromString("3fba2b22-22ae-4291-af67-faab748a5232");
502 UUID uuidSyn4 = UUID.fromString("f9b589c7-50cf-4df2-a52e-1b85eb7e4805");
503 UUID uuidSyn5 = UUID.fromString("fcc0bcf8-8bac-43bd-9508-1e97821587dd");
504 UUID uuidSyn6 = UUID.fromString("0ccd4e7c-6fbd-4b7c-bd47-29e45b92f34b");
505 UUID uuidRef1 = UUID.fromString("336f9b38-698c-45d7-be7b-993ed3355bdc");
506 UUID uuidRef2 = UUID.fromString("c8f49d1a-69e1-48a3-98bb-45d61f3da3e7");
507
508
509 boolean moveHomotypicGroup = true;
510 SynonymType newSynonymType = null;
511 boolean keepReference = true;
512 Reference newReference = null;
513 String newReferenceDetail = null;
514
515 Taxon newTaxon = (Taxon)service.load(uuidNewTaxon);
516 Synonym homotypicSynonym = (Synonym)service.load(uuidSyn1);
517 Assert.assertNotNull("Synonym should exist", homotypicSynonym);
518 Assert.assertNotNull("Synonym should have 1 relation", homotypicSynonym.getAcceptedTaxon());
519 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, homotypicSynonym.getAcceptedTaxon().getUuid());
520 Taxon oldTaxon = homotypicSynonym.getAcceptedTaxon();
521
522 try {
523 service.moveSynonymToAnotherTaxon(homotypicSynonym, newTaxon, moveHomotypicGroup, newSynonymType, null, newReferenceDetail, keepReference);
524 Assert.fail("Homotypic synonym move to other taxon should throw an exception");
525 } catch (HomotypicalGroupChangeException e) {
526 if (e.getMessage().contains("Synonym is in homotypic group with accepted taxon and other synonym(s). First remove synonym from homotypic group of accepted taxon before moving to other taxon")){
527 //OK
528 commitAndStartNewTransaction(tableNames);
529 }else{
530 Assert.fail("Unexpected exception occurred: " + e.getMessage());
531 }
532 }
533 //Asserts
534 homotypicSynonym = (Synonym)service.load(uuidSyn1);
535 Assert.assertNotNull("Synonym should still exist", homotypicSynonym);
536 Assert.assertNotNull("Synonym should still have 1 relation", homotypicSynonym.getAcceptedTaxon());
537 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", oldTaxon, homotypicSynonym.getAcceptedTaxon());
538
539 //test heterotypic synonym with other synonym in homotypic group
540 newTaxon = (Taxon)service.load(uuidNewTaxon);
541 Synonym heterotypicSynonym = (Synonym)service.load(uuidSyn3);
542 Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
543 Assert.assertNotNull("Synonym should have 1 relation", heterotypicSynonym.getAcceptedTaxon());
544 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, heterotypicSynonym.getAcceptedTaxon().getUuid());
545 oldTaxon = heterotypicSynonym.getAcceptedTaxon();
546 moveHomotypicGroup = false;
547
548 try {
549 service.moveSynonymToAnotherTaxon(heterotypicSynonym, newTaxon, moveHomotypicGroup, newSynonymType, null, newReferenceDetail, keepReference);
550 Assert.fail("Heterotypic synonym move to other taxon should throw an exception");
551 } catch (HomotypicalGroupChangeException e) {
552 if (e.getMessage().contains("Synonym is in homotypic group with other synonym(s). Either move complete homotypic group or remove synonym from homotypic group prior to moving to other taxon")){
553 //OK
554 commitAndStartNewTransaction(tableNames);
555 }else{
556 Assert.fail("Unexpected exception occurred: " + e.getMessage());
557 }
558 }
559 //Asserts
560 heterotypicSynonym = (Synonym)service.load(uuidSyn3);
561 Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
562 Assert.assertNotNull("Synonym should have accepted taxon", heterotypicSynonym.getAcceptedTaxon());
563 Assert.assertEquals("Accepted taxon of single relation should still be the old taxon", oldTaxon, heterotypicSynonym.getAcceptedTaxon());
564
565
566 //test heterotypic synonym with no other synonym in homotypic group
567 //+ keep reference
568
569 newTaxon = (Taxon)service.load(uuidNewTaxon);
570 heterotypicSynonym = (Synonym)service.load(uuidSyn5);
571 Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
572 Assert.assertNotNull("Synonym should have accepted taxon", heterotypicSynonym.getAcceptedTaxon());
573 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, heterotypicSynonym.getAcceptedTaxon().getUuid());
574 oldTaxon = heterotypicSynonym.getAcceptedTaxon();
575 moveHomotypicGroup = false;
576
577 try {
578 service.moveSynonymToAnotherTaxon(heterotypicSynonym, newTaxon, moveHomotypicGroup, newSynonymType, null, newReferenceDetail, keepReference);
579 } catch (HomotypicalGroupChangeException e) {
580 Assert.fail("Move of single heterotypic synonym should not throw exception: " + e.getMessage());
581 }
582 //Asserts
583 commitAndStartNewTransaction(tableNames);
584
585 heterotypicSynonym = (Synonym)service.load(uuidSyn5);
586 Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
587 Assert.assertNotNull("Synonym should have accepted taxon", heterotypicSynonym.getAcceptedTaxon());
588 Assert.assertEquals("Accepted taxon of single relation should be new taxon", newTaxon, heterotypicSynonym.getAcceptedTaxon());
589 Assert.assertEquals("Old detail should be kept", "rel5", heterotypicSynonym.getSecMicroReference());
590
591 //test heterotypic synonym with other synonym in homotypic group and moveHomotypicGroup="true"
592 //+ new detail
593 newTaxon = (Taxon)service.load(uuidNewTaxon);
594 heterotypicSynonym = (Synonym)service.load(uuidSyn3);
595 Reference ref1 = referenceService.load(uuidRef1);
596 Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
597 Assert.assertNotNull("Synonym should have 1 relation", heterotypicSynonym.getAcceptedTaxon());
598 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, heterotypicSynonym.getAcceptedTaxon().getUuid());
599 oldTaxon = heterotypicSynonym.getAcceptedTaxon();
600 Assert.assertEquals("Detail should be ref1", ref1, heterotypicSynonym.getSec());
601 Assert.assertEquals("Detail should be 'rel3'", "rel3", heterotypicSynonym.getSecMicroReference());
602 TaxonName oldSynName3 = heterotypicSynonym.getName();
603
604 Synonym heterotypicSynonym4 = (Synonym)service.load(uuidSyn4);
605 Assert.assertNotNull("Synonym should exist", heterotypicSynonym4);
606 Assert.assertNotNull("Synonym should have accepted taxon", heterotypicSynonym4.getAcceptedTaxon());
607 Assert.assertEquals("Accepted taxon of other synonym in group should be the old taxon", uuidOldTaxon, heterotypicSynonym4.getAcceptedTaxon().getUuid());
608 Assert.assertSame("Homotypic group of both synonyms should be same", oldSynName3.getHomotypicalGroup() , heterotypicSynonym4.getName().getHomotypicalGroup() );
609
610 moveHomotypicGroup = true;
611 keepReference = false;
612
613 try {
614 service.moveSynonymToAnotherTaxon(heterotypicSynonym4, newTaxon, moveHomotypicGroup, newSynonymType, null, newReferenceDetail, keepReference);
615 } catch (HomotypicalGroupChangeException e) {
616 Assert.fail("Move with 'moveHomotypicGroup = true' should not throw exception: " + e.getMessage());
617 }
618 //Asserts
619 commitAndStartNewTransaction(tableNames);
620 heterotypicSynonym = (Synonym)service.load(uuidSyn3);
621 Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
622 Assert.assertNotNull("Synonym should still have accepted taxon", heterotypicSynonym.getAcceptedTaxon());
623 Assert.assertEquals("Accepted taxon of relation should be new taxon now", newTaxon, heterotypicSynonym.getAcceptedTaxon());
624 TaxonName synName3 = heterotypicSynonym.getName();
625
626 heterotypicSynonym = (Synonym)service.load(uuidSyn4);
627 Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
628 Assert.assertNotNull("Synonym should still have accepted taxon", heterotypicSynonym.getAcceptedTaxon());
629 Assert.assertEquals("Accepted taxon of relation should be new taxon now", newTaxon, heterotypicSynonym.getAcceptedTaxon());
630 Assert.assertNull("Old citation should be removed", heterotypicSynonym.getSec());
631 Assert.assertNull("Old detail should be removed", heterotypicSynonym.getSecMicroReference());
632 TaxonName synName4 = heterotypicSynonym.getName();
633 Assert.assertEquals("Homotypic group of both synonyms should be equal", synName3.getHomotypicalGroup() , synName4.getHomotypicalGroup() );
634 Assert.assertSame("Homotypic group of both synonyms should be same", synName3.getHomotypicalGroup() , synName4.getHomotypicalGroup() );
635 Assert.assertEquals("Homotypic group of both synonyms should be equal to old homotypic group", oldSynName3.getHomotypicalGroup() , synName3.getHomotypicalGroup() );
636
637 //test single heterotypic synonym to homotypic synonym of new taxon
638 //+ new reference
639 newTaxon = (Taxon)service.load(uuidNewTaxon);
640 Reference ref2 = referenceService.load(uuidRef2);
641 heterotypicSynonym = (Synonym)service.load(uuidSyn6);
642 Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
643 Assert.assertNotNull("Synonym should have accepted taxon", heterotypicSynonym.getAcceptedTaxon());
644 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, heterotypicSynonym.getAcceptedTaxon().getUuid());
645 oldTaxon = heterotypicSynonym.getAcceptedTaxon();
646 moveHomotypicGroup = false;
647 keepReference = false;
648 newReference = ref2;
649 newReferenceDetail = "newRefDetail";
650 newSynonymType = SynonymType.HOMOTYPIC_SYNONYM_OF;
651
652 try {
653 service.moveSynonymToAnotherTaxon(heterotypicSynonym, newTaxon, moveHomotypicGroup, newSynonymType, newReference.getUuid(), newReferenceDetail, keepReference);
654 } catch (HomotypicalGroupChangeException e) {
655 Assert.fail("Move of single heterotypic synonym should not throw exception: " + e.getMessage());
656 }
657 //Asserts
658 commitAndStartNewTransaction(tableNames);
659 heterotypicSynonym = (Synonym)service.load(uuidSyn6);
660 Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
661 Assert.assertNotNull("Synonym should still have accepted taxon", heterotypicSynonym.getAcceptedTaxon());
662 Assert.assertEquals("Relationship type should be 'homotypic synonym'", newSynonymType, heterotypicSynonym.getType());
663 Assert.assertEquals("Accepted taxon of single relation should be new taxon", newTaxon, heterotypicSynonym.getAcceptedTaxon());
664 Assert.assertEquals("New citation should be ref2", ref2 ,heterotypicSynonym.getSec());
665 Assert.assertEquals("New detail should be kept", "newRefDetail", heterotypicSynonym.getSecMicroReference());
666
667 Assert.assertEquals("New taxon and new synonym should have equal homotypical group", heterotypicSynonym.getHomotypicGroup(), heterotypicSynonym.getAcceptedTaxon().getHomotypicGroup());
668 Assert.assertSame("New taxon and new synonym should have same homotypical group", heterotypicSynonym.getHomotypicGroup(), heterotypicSynonym.getAcceptedTaxon().getHomotypicGroup());
669 }
670
671 @Test
672 public final void testGetHeterotypicSynonymyGroups(){
673 Rank rank = Rank.SPECIES();
674 Reference ref1 = ReferenceFactory.newGeneric();
675 //HomotypicalGroup group = HomotypicalGroup.NewInstance();
676 Taxon taxon1 = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test3", null, null, null, null, null, null, null), null);
677 Synonym synonym0 = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test2", null, null, null, null, null, null, null), null);
678 Synonym synonym1 = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test2", null, null, null, null, null, null, null), null);
679 Synonym synonym2 = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test4", null, null, null, null, null, null, null), null);
680 synonym0.getName().setHomotypicalGroup(taxon1.getHomotypicGroup());
681 synonym2.getName().setHomotypicalGroup(synonym1.getHomotypicGroup());
682 //tax2.addHeterotypicSynonymName(synonym.getName());
683 taxon1.addSynonym(synonym1, SynonymType.HETEROTYPIC_SYNONYM_OF);
684 taxon1.addSynonym(synonym2, SynonymType.HETEROTYPIC_SYNONYM_OF);
685
686 service.save(synonym1);
687 service.save(synonym2);
688 service.save(taxon1);
689
690 List<List<Synonym>> heteroSyns = service.getHeterotypicSynonymyGroups(taxon1, null);
691 Assert.assertEquals("There should be 1 heterotypic group", 1, heteroSyns.size());
692 List<Synonym> synList = heteroSyns.get(0);
693 Assert.assertEquals("There should be 2 heterotypic syns in group 1", 2, synList.size());
694
695 //test sec
696 synonym2.setSec(ref1);
697 heteroSyns = service.getHeterotypicSynonymyGroups(taxon1, null);
698 Assert.assertEquals("There should be 1 heterotypic group", 1, heteroSyns.size());
699 synList = heteroSyns.get(0);
700 Assert.assertEquals("getHeterotypicSynonymyGroups should be independent of sec reference", 2, synList.size());
701 }
702
703 @Test
704 public final void testGetHomotypicSynonymsByHomotypicGroup(){
705 Rank rank = Rank.SPECIES();
706 Reference ref1 = ReferenceFactory.newGeneric();
707 //HomotypicalGroup group = HomotypicalGroup.NewInstance();
708 Taxon taxon1 = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test3", null, null, null, null, null, null, null), null);
709 Synonym synonym0 = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test2", null, null, null, null, null, null, null), null);
710 Synonym synonym1 = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test2", null, null, null, null, null, null, null), null);
711 Synonym synonym2 = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test4", null, null, null, null, null, null, null), null);
712 synonym0.getName().setHomotypicalGroup(taxon1.getHomotypicGroup());
713 synonym2.getName().setHomotypicalGroup(synonym1.getHomotypicGroup());
714 //tax2.addHeterotypicSynonymName(synonym.getName());
715 taxon1.addSynonym(synonym0, SynonymType.HOMOTYPIC_SYNONYM_OF);
716 taxon1.addSynonym(synonym1, SynonymType.HETEROTYPIC_SYNONYM_OF);
717 taxon1.addSynonym(synonym2, SynonymType.HETEROTYPIC_SYNONYM_OF);
718
719 service.save(synonym1);
720 service.save(synonym2);
721 service.save(taxon1);
722
723 List<Synonym> homoSyns = service.getHomotypicSynonymsByHomotypicGroup(taxon1, null);
724 Assert.assertEquals("There should be 1 heterotypic group", 1, homoSyns.size());
725 Assert.assertSame("The homotypic synonym should be synonym0", synonym0, homoSyns.get(0));
726
727 //test sec
728 synonym0.setSec(ref1);
729 homoSyns = service.getHomotypicSynonymsByHomotypicGroup(taxon1, null);
730 Assert.assertEquals("getHeterotypicSynonymyGroups should be independent of sec reference", 1, homoSyns.size());
731 }
732
733 @Test
734 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
735 //test delete synonym, but the name will not be deleted
736 public final void testDeleteSynonymSynonymTaxonDontDeleteName(){
737 final String[]tableNames = {
738 // "TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
739 // "HomotypicalGroup","HomotypicalGroup_AUD"
740 };
741
742 int nSynonyms = service.count(Synonym.class);
743 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
744 int nNames = nameService.count(TaxonName.class);
745 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
746 long nRelations = service.countSynonyms(true);
747 Assert.assertEquals("There should be two relationship left in the database", 2, nRelations);
748
749 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
750
751 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
752 SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
753 config.setDeleteNameIfPossible(false);
754 config.setNewHomotypicGroupIfNeeded(true);
755 service.deleteSynonym(synonym1, config);
756
757 this.commitAndStartNewTransaction(tableNames);
758
759 nSynonyms = service.count(Synonym.class);
760 Assert.assertEquals("There should be 1 synonym left in the database", 1, nSynonyms);
761 nNames = nameService.count(TaxonName.class);
762 Assert.assertEquals("There should be 4 names left in the database", 4, nNames);
763 nRelations = service.countSynonyms(true);
764 Assert.assertEquals("There should be no relationship left in the database", 1, nRelations);
765 }
766
767 @Test
768 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
769 //test delete synonym and his name
770 public final void testDeleteSynonymSynonymTaxonDeleteName(){
771 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
772 "HomotypicalGroup","HomotypicalGroup_AUD"};
773
774 int nSynonyms = service.count(Synonym.class);
775 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
776 int nNames = nameService.count(TaxonName.class);
777 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
778 long nRelations = service.countSynonyms(true);
779 Assert.assertEquals("There should be 2 relationship left in the database", 2, nRelations);
780
781 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
782 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
783 service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
784
785 this.commitAndStartNewTransaction(tableNames);
786
787 nSynonyms = service.count(Synonym.class);
788 Assert.assertEquals("There should be 1 synonym left in the database", 1, nSynonyms);
789 nNames = nameService.count(TaxonName.class);
790 Assert.assertEquals("There should be 3 names left in the database", 3, nNames);
791 nRelations = service.countSynonyms(true);
792 Assert.assertEquals("There should be 1 relationship left in the database", 1, nRelations);
793 }
794
795 @Test
796 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
797 //test remove synonym from taxon -> synonym and name still in the db and the synonymrelationship to the other taxon
798 //test delete synonym -> all relationships are deleted, the name is deleted and the synonym itself
799 public final void testDeleteSynonymSynonymTaxonBooleanRelToOneTaxon(){
800 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
801 "HomotypicalGroup","HomotypicalGroup_AUD"};
802
803 int nSynonyms = service.count(Synonym.class);
804 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
805 int nNames = nameService.count(TaxonName.class);
806 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
807
808 UUID uuidTaxon1=UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
809 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
810
811 Taxon taxon2 = (Taxon)service.load(uuidTaxon1);
812
813 List<String> initStrat = new ArrayList<>();
814 initStrat.add("markers");
815 Synonym synonym1 = (Synonym)service.load(uuidSynonym1, initStrat);
816 long nRelations = service.countSynonyms(true);
817 Assert.assertEquals("There should be 2 relationship left in the database", 2, nRelations);
818
819 taxon2.removeSynonym(synonym1, false);
820 service.saveOrUpdate(taxon2);
821
822 commitAndStartNewTransaction(null);
823
824 nSynonyms = service.count(Synonym.class);
825 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
826 nNames = nameService.count(TaxonName.class);
827 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
828 nRelations = service.countSynonyms(true);
829 Assert.assertEquals("There should be 1 relationship left in the database", 1, nRelations);
830 Marker marker1 = Marker.NewInstance(MarkerType.IMPORTED(), true);
831 Marker marker2 = Marker.NewInstance(MarkerType.COMPUTED(), true);
832 synonym1.addMarker(marker1);
833 synonym1.addMarker(marker2);
834 service.update(synonym1);
835 synonym1 =(Synonym) service.load(uuidSynonym1);
836
837 Set<Marker> markers = synonym1.getMarkers();
838 Marker marker = markers.iterator().next();
839 UUID markerUUID = marker.getUuid();
840 // taxon2 = (Taxon)service.load(uuidTaxon2);
841 synonym1 = (Synonym)service.load(uuidSynonym1);
842 //the marker should not prevent the deletion
843 DeleteResult result = service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
844 if (!result.isOk()){
845 Assert.fail();
846 }
847
848 commitAndStartNewTransaction(tableNames);
849 nSynonyms = service.count(Synonym.class);
850 Assert.assertEquals("There should be 1 synonym left in the database", 1, nSynonyms);
851 nNames = nameService.count(TaxonName.class);
852 Assert.assertEquals("There should be 3 names left in the database", 3, nNames);
853 nRelations = service.countSynonyms(true);
854 Assert.assertEquals("There should be no relationship left in the database", 1, nRelations);
855 marker = markerService.load(markerUUID);
856 assertNull(marker);
857 }
858
859 @Test
860 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
861 //this test is more or less obsolete since we have no synonym relationships anymore
862 //test delete synonym, only for a special taxon, but because of other relationships it will not be deleted at all
863 public final void testDeleteSynonymSynonymTaxonBooleanDeleteOneTaxon(){
864 final String[]tableNames = {
865 // "TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
866 // "HomotypicalGroup","HomotypicalGroup_AUD"
867 };
868 int nSynonyms = service.count(Synonym.class);
869 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
870 int nNames = nameService.count(TaxonName.class);
871 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
872
873 UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
874 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
875
876 Taxon taxon2 = (Taxon)service.load(uuidTaxon2);
877 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
878 synonym1.setSec(ReferenceFactory.newArticle());
879
880 taxon2.addSynonym(synonym1, SynonymType.HETEROTYPIC_SYNONYM_OF);
881 service.saveOrUpdate(synonym1);
882 long nRelations = service.countSynonyms(true);
883 //this was "3" when we still had synonym relationships
884 Assert.assertEquals("There should be 2 relationship left in the database", 2, nRelations);
885 service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
886
887 this.commitAndStartNewTransaction(tableNames);
888
889 nSynonyms = service.count(Synonym.class);
890 //this was "2" when we still had synonym relationships
891 Assert.assertEquals("There should still be 1 synonym left in the database", 1, nSynonyms);
892 nNames = nameService.count(TaxonName.class);
893 //was 3
894 Assert.assertEquals("There should be 3 names left in the database", 3, nNames);
895 nRelations = service.countSynonyms(true);
896 Assert.assertEquals("There should be 1 related synonym left in the database", 1, nRelations);
897 }
898
899 @Test
900 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
901
902 public final void testDeleteSynonymWithAnnotations(){
903 final String[]tableNames = {
904 // "TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
905 // "HomotypicalGroup","HomotypicalGroup_AUD"
906 };
907
908 UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
909 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
910
911 Taxon taxon2 = (Taxon)service.load(uuidTaxon2);
912 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
913 taxon2.addSynonym(synonym1, SynonymType.HETEROTYPIC_SYNONYM_OF);
914
915 Annotation annotation = Annotation.NewDefaultLanguageInstance("test");
916 synonym1.addAnnotation(annotation);
917 service.saveOrUpdate(synonym1);
918
919 DeleteResult result = service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
920 if (result.isError()){
921 Assert.fail();
922 }
923 this.commitAndStartNewTransaction(tableNames);
924 }
925
926 @Test
927 @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
928
929 public final void testDeleteSynonymSynonymTaxonBooleanWithRelatedName(){
930 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
931 "HomotypicalGroup","HomotypicalGroup_AUD"};
932
933 int nSynonyms = service.count(Synonym.class);
934 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
935 int nNames = nameService.count(TaxonName.class);
936 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
937
938 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
939 UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
940
941 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
942 TaxonName name2 = nameService.load(uuidSynonymName2);
943 UUID name3Uuid = synonym1.getName().getUuid();
944 TaxonName name3 = nameService.load(name3Uuid);
945 name3.addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null, null);
946
947 service.saveOrUpdate(synonym1);
948
949 long nRelations = nameService.listNameRelationships(null, 1000, 0, null, null).size();
950 logger.info("number of name relations: " + nRelations);
951 Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
952 SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
953
954 service.deleteSynonym(synonym1, config);
955
956 this.commitAndStartNewTransaction(tableNames);
957 //synonym is deleted, but the name can not be deleted because of a name relationship
958 nSynonyms = service.count(Synonym.class);
959 Assert.assertEquals("There should still be 1 synonyms left in the database", 1, nSynonyms);
960 nNames = nameService.count(TaxonName.class);
961 Assert.assertEquals("There should be 4 names left in the database (name is related to synonymName2)", 4, nNames);
962 nRelations = service.countSynonyms(true);
963 //may change with better implementation of countAllRelationships (see #2653)
964 nRelations = nameService.listNameRelationships(null, 1000, 0, null, null).size();
965 logger.info("number of name relations: " + nRelations);
966 Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
967
968 //clean up database
969 name2 = nameService.load(uuidSynonymName2);
970 NameRelationship rel = CdmBase.deproxy(name2.getNameRelations().iterator().next(), NameRelationship.class);
971 name2.removeNameRelationship(rel);
972 nameService.save(name2);
973 this.setComplete();
974 this.endTransaction();
975 }
976
977 @Test
978 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
979 public final void testDeleteSynonymSynonymTaxonBooleanWithRelatedNameDeleteAllNameRelations(){
980 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
981 "HomotypicalGroup","HomotypicalGroup_AUD"};
982
983 int nSynonyms = service.count(Synonym.class);
984 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
985 int nNames = nameService.count(TaxonName.class);
986 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
987
988 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
989 UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
990
991 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
992 TaxonName name2 = nameService.load(uuidSynonymName2);
993 UUID name3Uuid = synonym1.getName().getUuid();
994 TaxonName name3 = nameService.load(name3Uuid);
995 name3.addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null, null);
996
997 service.saveOrUpdate(synonym1);
998
999 long nRelations = nameService.listNameRelationships(null, 1000, 0, null, null).size();
1000 logger.info("number of name relations: " + nRelations);
1001 Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
1002 SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
1003 NameDeletionConfigurator nameDeletionConfig = new NameDeletionConfigurator();
1004 nameDeletionConfig.setRemoveAllNameRelationships(true);
1005 config.setNameDeletionConfig(nameDeletionConfig);
1006
1007 service.deleteSynonym(synonym1, config);
1008
1009 this.commitAndStartNewTransaction(tableNames);
1010
1011 nSynonyms = service.count(Synonym.class);
1012 Assert.assertEquals("There should still be 1 synonyms left in the database", 1, nSynonyms);
1013 nNames = nameService.count(TaxonName.class);
1014 Assert.assertEquals("There should be 3 names left in the database ", 3, nNames);
1015 nRelations = service.countSynonyms(true);
1016 //may change with better implementation of countAllRelationships (see #2653)
1017 nRelations = nameService.listNameRelationships(null, 1000, 0, null, null).size();
1018 logger.info("number of name relations: " + nRelations);
1019 Assert.assertEquals("There should be no name relationship left in the database", 0, nRelations);
1020 }
1021
1022 @Test
1023 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
1024 public final void testDeleteSynonymSynonymTaxonBooleanWithRelatedNameIgnoreIsBasionym(){
1025 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
1026 "HomotypicalGroup","HomotypicalGroup_AUD"};
1027
1028 int nSynonyms = service.count(Synonym.class);
1029 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
1030 int nNames = nameService.count(TaxonName.class);
1031 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
1032
1033 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
1034 UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
1035
1036 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
1037 TaxonName synName2 = nameService.load(uuidSynonymName2);
1038 UUID name3Uuid = synonym1.getName().getUuid();
1039 TaxonName synName1 = nameService.load(name3Uuid);
1040 synName1.addRelationshipFromName(synName2, NameRelationshipType.BASIONYM(), null, null);
1041
1042 service.saveOrUpdate(synonym1);
1043
1044 long nRelations = nameService.listNameRelationships(null, 1000, 0, null, null).size();
1045 logger.info("number of name relations: " + nRelations);
1046 Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
1047 SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
1048 NameDeletionConfigurator nameDeletionConfig = new NameDeletionConfigurator();
1049 nameDeletionConfig.setIgnoreIsBasionymFor(true);
1050 config.setNameDeletionConfig(nameDeletionConfig);
1051
1052 DeleteResult result =service.deleteSynonym(synonym1, config);
1053 if (!result.isOk()){
1054 Assert.fail();
1055 }
1056
1057 logger.debug(result);
1058 this.commitAndStartNewTransaction(tableNames);
1059
1060 nSynonyms = service.count(Synonym.class);
1061 Assert.assertEquals("There should still be 1 synonyms left in the database", 1, nSynonyms);
1062 nNames = nameService.count(TaxonName.class);
1063 Assert.assertEquals("There should be 3 names left in the database ", 3, nNames);
1064 nRelations = service.countSynonyms(true);
1065 //may change with better implementation of countAllRelationships (see #2653)
1066 nRelations = nameService.listNameRelationships(null, 1000, 0, null, null).size();
1067 logger.info("number of name relations: " + nRelations);
1068 Assert.assertEquals("There should be no name relationship left in the database", 0, nRelations);
1069 }
1070
1071 @Test
1072 @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
1073 public final void testDeleteSynonymSynonymTaxonBooleanWithRollback(){
1074 // final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
1075 // "HomotypicalGroup","HomotypicalGroup_AUD"};
1076
1077 int nSynonyms = service.count(Synonym.class);
1078 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
1079 int nNames = nameService.count(TaxonName.class);
1080 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
1081 long nRelations = service.countSynonyms(true);
1082
1083 //may change with better implementation of countAllRelationships (see #2653)
1084
1085 logger.debug("");
1086 Assert.assertEquals("There should be 2 relationships in the database (the 2 synonym relationship) but no name relationship", 2, nRelations);
1087
1088 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
1089 UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
1090
1091 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
1092 TaxonName name2 = nameService.load(uuidSynonymName2);
1093 synonym1.getName().addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null, null);
1094
1095 service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
1096
1097 this.rollback();
1098 // printDataSet(System.out, tableNames);
1099 this.startNewTransaction();
1100
1101 nSynonyms = service.count(Synonym.class);
1102 Assert.assertEquals("There should still be 2 synonyms left in the database", 2, nSynonyms);
1103 nNames = nameService.count(TaxonName.class);
1104 Assert.assertEquals("There should be 4 names left in the database", 4, nNames);
1105 nRelations = service.countSynonyms(true);
1106 //may change with better implementation of countAllRelationships (see #2653)
1107 Assert.assertEquals("There should be 2 relationship in the database (the 2 synonym relationship) but no name relationship", 2, nRelations);
1108 }
1109
1110 @Test
1111 @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
1112 public final void testDeleteSynonymSynonymTaxonBooleanWithoutTransaction(){
1113 @SuppressWarnings("unused")
1114 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
1115 "HomotypicalGroup","HomotypicalGroup_AUD"};
1116
1117 int nSynonyms = service.count(Synonym.class);
1118 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
1119 int nNames = nameService.count(TaxonName.class);
1120 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
1121 long nRelations = service.countSynonyms(true);
1122 //may change with better implementation of countAllRelationships (see #2653)
1123 Assert.assertEquals("There should be 2 relationship in the database (the 2 synonym relationships) but no name relationship", 2, nRelations);
1124
1125 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
1126 UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
1127
1128 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
1129 TaxonName name2 = nameService.load(uuidSynonymName2);
1130 synonym1.getName().addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null, null);
1131
1132 service.saveOrUpdate(synonym1);
1133 nRelations = service.countSynonyms(true);
1134 Assert.assertEquals("There should be two relationships in the database", 2, nRelations);
1135 this.setComplete();
1136 this.endTransaction();
1137
1138 // printDataSet(System.out, tableNames);
1139
1140 //out of wrapping transaction
1141 service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
1142
1143 this.startNewTransaction();
1144
1145 nSynonyms = service.count(Synonym.class);
1146 Assert.assertEquals("There should still be 1 synonyms left in the database. The rollback on name delete should not lead to rollback in synonym delete.", 1, nSynonyms);
1147 nNames = nameService.count(TaxonName.class);
1148 Assert.assertEquals("There should be 4 names left in the database", 4, nNames);
1149 nRelations = service.countSynonyms(true);
1150 Assert.assertEquals("There should be no taxon or synonym relationship in the database", 1, nRelations);
1151 nRelations = nameService.listNameRelationships(null, 1000, 0, null, null).size();
1152 Assert.assertEquals("There should be one name relationship in the database", 1, nRelations);
1153 }
1154
1155 @Test
1156 @DataSet("TaxonServiceImplTest.testInferredSynonyms.xml")
1157 public void testCreateInferredSynonymy(){
1158
1159 UUID classificationUuid = UUID.fromString("aeee7448-5298-4991-b724-8d5b75a0a7a9");
1160 Classification tree = classificationService.find(classificationUuid);
1161 UUID taxonUuid = UUID.fromString("bc09aca6-06fd-4905-b1e7-cbf7cc65d783");
1162 TaxonBase<?> taxonBase = service.find(taxonUuid);
1163 List <Synonym> synonyms = service.list(Synonym.class, null, null, null, null);
1164 assertEquals("Number of synonyms should be 2",2,synonyms.size());
1165 Taxon taxon = (Taxon)taxonBase;
1166
1167 //synonyms = taxonDao.getAllSynonyms(null, null);
1168 //assertEquals("Number of synonyms should be 2",2,synonyms.size());
1169 List<Synonym> inferredSynonyms = service.createInferredSynonyms(taxon, tree, SynonymType.INFERRED_EPITHET_OF, true);
1170 assertNotNull("there should be a new synonym ", inferredSynonyms);
1171 assertEquals ("the name of inferred epithet should be SynGenus lachesis", "SynGenus lachesis syn. sec. Sp. Pl.", inferredSynonyms.get(0).getTitleCache());
1172
1173 inferredSynonyms = service.createInferredSynonyms(taxon, tree, SynonymType.INFERRED_GENUS_OF, true);
1174 assertNotNull("there should be a new synonym ", inferredSynonyms);
1175 assertEquals ("the name of inferred epithet should be SynGenus lachesis", "Acherontia ciprosus syn. sec. Sp. Pl.", inferredSynonyms.get(0).getTitleCache());
1176
1177 inferredSynonyms = service.createInferredSynonyms(taxon, tree, SynonymType.POTENTIAL_COMBINATION_OF, true);
1178 assertNotNull("there should be a new synonym ", inferredSynonyms);
1179 assertEquals ("the name of inferred epithet should be SynGenus lachesis", "SynGenus ciprosus syn. sec. Sp. Pl.", inferredSynonyms.get(0).getTitleCache());
1180 //assertTrue("set of synonyms should contain an inferred Synonym ", synonyms.contains(arg0))
1181 }
1182
1183 @Test
1184 @DataSet("../../database/ClearDBDataSet.xml")
1185 public final void testTaxonDeletionConfig(){
1186 final String[]tableNames = {}
1187 // "Classification", "Classification_AUD",
1188 // "TaxonBase","TaxonBase_AUD",
1189 // "TaxonNode","TaxonNode_AUD",
1190 // "TaxonName","TaxonName_AUD",
1191 // "TaxonRelationship", "TaxonRelationship_AUD",
1192 // "TaxonDescription", "TaxonDescription_AUD",
1193 // "HomotypicalGroup","HomotypicalGroup_AUD",
1194 // "PolytomousKey","PolytomousKey_AUD",
1195 // "PolytomousKeyNode","PolytomousKeyNode_AUD",
1196 // "Media","Media_AUD",
1197 // "DescriptiveDataSet","DescriptiveDataSet_AUD",
1198 // "DescriptionElementBase","DescriptionElementBase_AUD",
1199 // "DeterminationEvent","DeterminationEvent_AUD",
1200 // "SpecimenOrObservationBase","SpecimenOrObservationBase_AUD"}
1201 ;
1202
1203 commitAndStartNewTransaction(tableNames);
1204 getTestTaxon();
1205 commitAndStartNewTransaction(tableNames);
1206 int nTaxa = service.count(Taxon.class);
1207
1208 Assert.assertEquals("There should be 4 taxa in the database", 4, nTaxa);
1209 Taxon parent = (Taxon)service.find(GENUS_UUID);
1210 Assert.assertNotNull("Parent taxon should exist", parent);
1211 Taxon child1 = (Taxon)service.find(SPECIES1_UUID);
1212 Assert.assertNotNull("Child taxon should exist", child1);
1213 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1214 config.setDeleteTaxonNodes(false);
1215 config.setDeleteMisappliedNames(false);
1216 //try {
1217 //commitAndStartNewTransaction(tableNames);
1218
1219 DeleteResult result = service.deleteTaxon(child1.getUuid(), config, null);
1220 if (result.isOk()){
1221 Assert.fail("Delete should throw an error as long as name is used in classification.");
1222 }
1223
1224 nTaxa = service.count(Taxon.class);
1225 Assert.assertEquals("There should be 4 taxa in the database", 4, nTaxa);
1226 child1 = (Taxon)service.find(SPECIES1_UUID);
1227 Assert.assertNotNull("Child taxon should exist", child1);
1228 Assert.assertEquals("Child should belong to 1 node", 1, child1.getTaxonNodes().size());
1229
1230 TaxonNode node = child1.getTaxonNodes().iterator().next();
1231 child1.addSource(IdentifiableSource.NewInstance(OriginalSourceType.Import));
1232
1233 SpecimenOrObservationBase<?> identifiedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.DerivedUnit);
1234 DeterminationEvent.NewInstance(child1, identifiedUnit);
1235 //UUID eventUUID = eventService.save(determinationEvent);
1236 UUID identifiedUnitUUID = occurenceService.save(identifiedUnit).getUuid();
1237
1238 TaxonNode parentNode = node.getParent();
1239 parentNode =CdmBase.deproxy(parentNode, TaxonNode.class);
1240 parentNode.deleteChildNode(node);
1241 nodeService.save(parentNode);
1242 //commitAndStartNewTransaction(tableNames);
1243
1244 // try {
1245
1246 result = service.deleteTaxon(child1
1247 .getUuid(), config, null);
1248 if (result.isOk()){
1249 Assert.fail("Delete should throw an exception because of the determination event");
1250 }
1251
1252 //determinationEvent = (DeterminationEvent)eventService.load(eventUUID);
1253 commitAndStartNewTransaction(tableNames);
1254 identifiedUnit = occurenceService.load(identifiedUnitUUID);
1255
1256 occurenceService.delete(identifiedUnit);
1257
1258 commitAndStartNewTransaction(tableNames);
1259 child1 = (Taxon)service.find(SPECIES1_UUID);
1260
1261 assertEquals(0, child1.getTaxonNodes().size());
1262 // try {
1263
1264 result = service.deleteTaxon(child1.getUuid(), config, null);
1265
1266 if (!result.isOk()){
1267 Assert.fail("Delete should not throw an exception anymore");
1268 }
1269
1270 nTaxa = service.count(Taxon.class);
1271 Assert.assertEquals("There should be 3 taxa in the database", 3, nTaxa);
1272
1273 config.setDeleteTaxonNodes(true);
1274 Taxon child2 =(Taxon) service.find(SPECIES2_UUID);
1275
1276 // try {
1277 result = service.deleteTaxon(child2.getUuid(), config, child2.getTaxonNodes().iterator().next().getClassification().getUuid());
1278 if (!result.isOk()){
1279 Assert.fail("Delete should not throw an exception");
1280 }
1281
1282 //service.find(uuid);
1283
1284 nTaxa = service.count(Taxon.class);
1285 Assert.assertEquals("There should be 2 taxa in the database",2, nTaxa);
1286 // nNames = nameService.count(TaxonName.class);
1287 // Assert.assertEquals("There should be 3 names left in the database", 3, nNames);
1288 // int nRelations = service.countAllRelationships();
1289 // Assert.assertEquals("There should be no relationship left in the database", 0, nRelations);
1290 }
1291
1292 @Test
1293 @DataSet(value="../../database/ClearDBDataSet.xml")
1294 public final void testDeleteTaxon(){
1295
1296 //create a small classification
1297 Taxon testTaxon = getTestTaxon();
1298 service.save(testTaxon).getUuid();
1299
1300 Taxon speciesTaxon = (Taxon)service.find(SPECIES1_UUID);
1301 Iterator<TaxonDescription> descriptionIterator = speciesTaxon.getDescriptions().iterator();
1302 UUID descrUUID = null;
1303 UUID descrElementUUID = null;
1304 if (descriptionIterator.hasNext()){
1305 TaxonDescription descr = descriptionIterator.next();
1306 descrUUID = descr.getUuid();
1307 descrElementUUID = descr.getElements().iterator().next().getUuid();
1308 }
1309 IBotanicalName taxonName = nameService.find(SPECIES1_NAME_UUID);
1310 assertNotNull(taxonName);
1311
1312 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1313 config.setDeleteNameIfPossible(false);
1314
1315 DeleteResult result = service.deleteTaxon(speciesTaxon.getUuid(), config, speciesTaxon.getTaxonNodes().iterator().next().getClassification().getUuid());
1316 if (!result.isOk()){
1317 Assert.fail();
1318 }
1319 commitAndStartNewTransaction(null);
1320
1321 taxonName = nameService.find(SPECIES1_NAME_UUID);
1322 Taxon taxon = (Taxon)service.find(SPECIES1_UUID);
1323
1324 //descriptionService.find(descrUUID);
1325 assertNull(descriptionService.find(descrUUID));
1326 assertNull(descriptionElementService.find(descrElementUUID));
1327 //assertNull(synName);
1328 assertNotNull(taxonName);
1329 assertNull(taxon);
1330 config.setDeleteNameIfPossible(true);
1331 Taxon newTaxon = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), null);
1332 service.save(newTaxon);
1333 result = service.deleteTaxon(newTaxon.getUuid()
1334 , config, null);
1335 if (!result.isOk()){
1336 Assert.fail();
1337 }
1338 }
1339
1340 @Test
1341 @DataSet(value="../../database/ClearDBDataSet.xml")
1342 public final void testDeleteTaxonWithAnnotations(){
1343
1344 //create a small classification
1345 Taxon testTaxon = getTestTaxon();
1346 service.save(testTaxon).getUuid();
1347
1348 Taxon speciesTaxon = (Taxon)service.find(SPECIES1_UUID);
1349 Iterator<TaxonDescription> descriptionIterator = speciesTaxon.getDescriptions().iterator();
1350 UUID descrUUID = null;
1351 UUID descrElementUUID = null;
1352 if (descriptionIterator.hasNext()){
1353 TaxonDescription descr = descriptionIterator.next();
1354 descrUUID = descr.getUuid();
1355 descrElementUUID = descr.getElements().iterator().next().getUuid();
1356 }
1357 IBotanicalName taxonName = nameService.find(SPECIES1_NAME_UUID);
1358 assertNotNull(taxonName);
1359
1360 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1361 config.setDeleteNameIfPossible(false);
1362 Annotation annotation = Annotation.NewDefaultLanguageInstance("test");
1363 speciesTaxon.addAnnotation(annotation);
1364
1365
1366 DeleteResult result = service.deleteTaxon(speciesTaxon.getUuid(), config, speciesTaxon.getTaxonNodes().iterator().next().getClassification().getUuid());
1367 if (!result.isOk()){
1368 Assert.fail();
1369 }
1370 commitAndStartNewTransaction(null);
1371
1372 taxonName = nameService.find(SPECIES1_NAME_UUID);
1373 Taxon taxon = (Taxon)service.find(SPECIES1_UUID);
1374
1375 //descriptionService.find(descrUUID);
1376 assertNull(descriptionService.find(descrUUID));
1377 assertNull(descriptionElementService.find(descrElementUUID));
1378 //assertNull(synName);
1379 assertNotNull(taxonName);
1380 assertNull(taxon);
1381 config.setDeleteNameIfPossible(true);
1382 Taxon newTaxon = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), null);
1383 service.save(newTaxon);
1384 result = service.deleteTaxon(newTaxon.getUuid()
1385 , config, null);
1386 if (!result.isOk()){
1387 Assert.fail();
1388 }
1389 }
1390
1391 @Test
1392 @DataSet(value="../../database/ClearDBDataSet.xml")
1393 public final void testDeleteTaxonUsedInTaxonRelation(){
1394
1395 //create a small classification
1396 Taxon testTaxon = getTestTaxon();
1397 service.save(testTaxon).getUuid();
1398
1399 Taxon speciesTaxon = (Taxon)service.find(SPECIES1_UUID);
1400 Taxon speciesTaxon2 = (Taxon)service.find(SPECIES2_UUID);
1401 speciesTaxon.addTaxonRelation(speciesTaxon2, TaxonRelationshipType.MISAPPLIED_NAME_FOR(), null, null);
1402
1403 IBotanicalName taxonName = nameService.find(SPECIES1_NAME_UUID);
1404 assertNotNull(taxonName);
1405
1406 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1407 config.setDeleteNameIfPossible(false);
1408 config.setDeleteTaxonRelationships(false);
1409
1410
1411 DeleteResult result = service.deleteTaxon(speciesTaxon.getUuid(), config, speciesTaxon.getTaxonNodes().iterator().next().getClassification().getUuid());
1412 if (result.isOk()){
1413 Assert.fail();
1414 }
1415 commitAndStartNewTransaction(null);
1416
1417 taxonName = nameService.find(SPECIES1_NAME_UUID);
1418 Taxon taxon = (Taxon)service.find(SPECIES1_UUID);
1419
1420 assertNotNull(taxonName);
1421 assertNotNull(taxon);
1422
1423 config.setDeleteNameIfPossible(false);
1424 config.setDeleteTaxonRelationships(true);
1425
1426
1427 result = service.deleteTaxon(speciesTaxon.getUuid(), config, speciesTaxon.getTaxonNodes().iterator().next().getClassification().getUuid());
1428 if (!result.isOk()){
1429 Assert.fail();
1430 }
1431 commitAndStartNewTransaction(null);
1432
1433 config.setDeleteNameIfPossible(true);
1434 Taxon newTaxon = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), null);
1435 service.save(newTaxon);
1436 result = service.deleteTaxon(newTaxon.getUuid()
1437 , config, null);
1438 if (!result.isOk()){
1439 Assert.fail();
1440 }
1441 }
1442
1443 @Test
1444 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="../../database/ClearDBDataSet.xml")
1445 public final void testDeleteTaxonDeleteSynonymRelations(){
1446
1447 final String[]tableNames = {
1448 "Classification", "Classification_AUD",
1449 "TaxonBase","TaxonBase_AUD",
1450 "TaxonNode","TaxonNode_AUD",
1451 "TaxonName","TaxonName_AUD"};
1452 commitAndStartNewTransaction(tableNames);
1453 //create a small classification
1454 Taxon testTaxon = getTestTaxon();
1455 service.save(testTaxon).getUuid();
1456 Taxon speciesTaxon = (Taxon)service.find(SPECIES2_UUID);
1457
1458 Synonym synonym = speciesTaxon.getSynonyms().iterator().next();
1459 UUID synonymUuid = synonym.getUuid();
1460 service.countSynonyms(true);
1461
1462 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1463 config.setDeleteSynonymsIfPossible(false);
1464
1465
1466 DeleteResult result = service.deleteTaxon(speciesTaxon.getUuid(), config, speciesTaxon.getTaxonNodes().iterator().next().getClassification().getUuid());
1467 if (!result.isOk()){
1468 Assert.fail();
1469 }
1470 commitAndStartNewTransaction(null);
1471
1472 Taxon taxon = (Taxon)service.find(SPECIES2_UUID);
1473 assertNull("The deleted taxon should no longer exist", taxon);
1474
1475 Synonym syn = (Synonym)service.find(synonymUuid);
1476 assertNotNull("The synonym should still exist since DeleteSynonymsIfPossible was false", service.find(synonymUuid));
1477 assertNull("The synonym should not be attached to an accepted taxon anymore", syn.getAcceptedTaxon());
1478 }
1479
1480 @Test
1481 @DataSet(value="../../database/ClearDBDataSet.xml")
1482 public final void testDeleteTaxonNameUsedInOtherContext(){
1483
1484 //create a small classification
1485 Taxon testTaxon = getTestTaxon();
1486 service.save(testTaxon).getUuid();
1487 Taxon speciesTaxon = (Taxon)service.find(SPECIES1_UUID);
1488
1489 IBotanicalName taxonName = nameService.find(SPECIES1_NAME_UUID);
1490 assertNotNull(taxonName);
1491 TaxonName fromName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
1492 taxonName.addRelationshipFromName(fromName, NameRelationshipType.VALIDATED_BY_NAME(), null, null);
1493 nameService.save(fromName);
1494
1495 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1496 config.setDeleteNameIfPossible(true);
1497 DeleteResult result = service.deleteTaxon(speciesTaxon.getUuid(), config, speciesTaxon.getTaxonNodes().iterator().next().getClassification().getUuid());
1498 if (!result.isOk()){
1499 Assert.fail();
1500 }
1501 commitAndStartNewTransaction(null);
1502
1503 taxonName = nameService.find(SPECIES1_NAME_UUID);
1504 Taxon taxon = (Taxon)service.find(SPECIES1_UUID);
1505 //because of the namerelationship the name cannot be deleted
1506 assertNotNull(taxonName);
1507 assertNull(taxon);
1508 }
1509
1510 @Test
1511 @DataSet(value="../../database/ClearDBDataSet.xml")
1512 public final void testDeleteTaxonNameUsedInTwoClassificationsDeleteAllNodes(){
1513 commitAndStartNewTransaction(null);
1514 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1515 //create a small classification
1516 Taxon testTaxon = getTestTaxon();
1517
1518 UUID uuid = service.save(testTaxon).getUuid();
1519 //BotanicalName name = nameService.find(uuid);
1520 Set<TaxonNode> nodes = testTaxon.getTaxonNodes();
1521 TaxonNode node = nodes.iterator().next();
1522 List<TaxonNode> childNodes = node.getChildNodes();
1523 TaxonNode childNode = childNodes.iterator().next();
1524 UUID childUUID = childNode.getTaxon().getUuid();
1525 Classification secondClassification = getTestClassification("secondClassification");
1526
1527 secondClassification.addChildTaxon(testTaxon, null, null);
1528 //delete the taxon in all classifications
1529 config.setDeleteInAllClassifications(true);
1530 DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, null);
1531 if (!result.isOk()){
1532 Assert.fail();
1533 }
1534 commitAndStartNewTransaction(null);
1535 Taxon tax = (Taxon)service.find(uuid);
1536 assertNull(tax);
1537 Taxon childTaxon = (Taxon)service.find(childUUID);
1538 assertNull(tax);
1539 commitAndStartNewTransaction(null);
1540 }
1541
1542 @Test
1543 @DataSet(value="../../database/ClearDBDataSet.xml")
1544 public final void testDeleteTaxonNameUsedInTwoClassificationsDoNotDeleteAllNodes(){
1545 // delete the taxon only in second classification, this should delete only the nodes, not the taxa
1546 Taxon testTaxon = getTestTaxon();
1547 UUID uuid = service.save(testTaxon).getUuid();
1548 Classification secondClassification = getTestClassification("secondClassification");
1549 Set<TaxonNode> nodes = testTaxon.getTaxonNodes();
1550 TaxonNode node = nodes.iterator().next();
1551 List<TaxonNode> childNodes = node.getChildNodes();
1552 TaxonNode childNode = childNodes.iterator().next();
1553 UUID childUUID = childNode.getTaxon().getUuid();
1554 childNode = secondClassification.addChildTaxon(testTaxon, null, null);
1555 UUID childNodeUUID = childNode.getUuid();
1556
1557 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1558 config.setDeleteInAllClassifications(false);
1559 // try {
1560 DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, secondClassification.getUuid());
1561 /* Assert.fail("The taxon should not be deletable because it is used in a second classification and the configuration is set to deleteInAllClassifications = false");
1562 } catch (DataChangeNoRollbackException e) {
1563 logger.debug(e.getMessage());
1564 }
1565 */
1566
1567 if (result.isOk()){
1568 Assert.fail("The taxon should not be deletable because it is used in a second classification and the configuration is set to deleteInAllClassifications = false");
1569 }
1570
1571 //commitAndStartNewTransaction(null);
1572 Taxon tax = (Taxon)service.find(uuid);
1573 assertNotNull(tax);
1574 Taxon childTaxon = (Taxon)service.find(childUUID);
1575 assertNotNull(tax);
1576 //when calling delete taxon and the taxon can not be deleted the children should not be deleted as well. If children should be deleted call delete taxonnode
1577 node = nodeService.find(childNodeUUID);
1578 assertNotNull(node);
1579 }
1580
1581 @Test
1582 @DataSet(value="../../database/ClearDBDataSet.xml")
1583 public final void testTaxonNodeDeletionConfiguratorMoveToParent(){
1584 //test childHandling MOVE_TO_PARENT:
1585 Taxon testTaxon = getTestTaxon();
1586 UUID uuid = service.save(testTaxon).getUuid();
1587
1588 Taxon topMost = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.FAMILY()), null);
1589
1590 Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1591 TaxonNode node =nodes.next();
1592 Classification classification = node.getClassification();
1593 classification.addParentChild(topMost, testTaxon, null, null);
1594 UUID topMostUUID = service.save(topMost).getUuid();
1595
1596 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1597 config.getTaxonNodeConfig().setChildHandling(ChildHandling.MOVE_TO_PARENT);
1598
1599
1600 DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, classification.getUuid());
1601 if(!result.isOk()){
1602 Assert.fail();
1603 }
1604
1605 commitAndStartNewTransaction(null);
1606 Taxon tax = (Taxon)service.find(uuid);
1607 assertNull(tax);
1608 tax = (Taxon)service.find(topMostUUID);
1609 Set<TaxonNode> topMostNodes = tax.getTaxonNodes();
1610 assertNotNull(topMostNodes);
1611 assertEquals("there should be one taxon node", 1, topMostNodes.size());
1612 nodes = topMostNodes.iterator();
1613 TaxonNode topMostNode = nodes.next();
1614 int size = topMostNode.getChildNodes().size();
1615
1616 assertEquals(2, size);
1617 }
1618
1619 @Test
1620 @DataSet(value="../../database/ClearDBDataSet.xml")
1621 public final void testTaxonNodeDeletionConfiguratorDeleteChildren(){
1622 //test childHandling DELETE:
1623 Taxon testTaxon = getTestTaxon();
1624 UUID uuid = service.save(testTaxon).getUuid();
1625
1626 Taxon topMost = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.FAMILY()), null);
1627
1628 Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1629 TaxonNode node =nodes.next();
1630 UUID taxonNodeUUID = node.getUuid();
1631 Classification classification = node.getClassification();
1632 classification.addParentChild(topMost, testTaxon, null, null);
1633 UUID topMostUUID = service.save(topMost).getUuid();
1634
1635 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1636 config.getTaxonNodeConfig().setChildHandling(ChildHandling.DELETE);
1637
1638 // try {
1639 DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, testTaxon.getTaxonNodes().iterator().next().getClassification().getUuid());
1640 if(!result.isOk()){
1641 Assert.fail();
1642 }
1643 commitAndStartNewTransaction(null);
1644 Taxon tax = (Taxon)service.find(uuid);
1645 assertNull(tax);
1646 tax = (Taxon)service.find(topMostUUID);
1647 Set<TaxonNode> topMostNodes = tax.getTaxonNodes();
1648 assertNotNull(topMostNodes);
1649 assertEquals("there should be one taxon node", 1, topMostNodes.size());
1650 nodes = topMostNodes.iterator();
1651 TaxonNode topMostNode = nodes.next();
1652 int size = topMostNode.getChildNodes().size();
1653 node = nodeService.find(taxonNodeUUID);
1654 assertNull(node);
1655 assertEquals(0, size);
1656 }
1657
1658
1659 @Test
1660 @DataSet(value="../../database/ClearDBDataSet.xml")
1661 public final void testTaxonDeletionConfiguratorDeleteMarker(){
1662 //test childHandling DELETE:
1663 Taxon testTaxon = getTestTaxon();
1664 UUID uuid = service.save(testTaxon).getUuid();
1665
1666 Taxon topMost = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.FAMILY()), null);
1667
1668 Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1669 TaxonNode node =nodes.next();
1670 Classification classification = node.getClassification();
1671 classification.addParentChild(topMost, testTaxon, null, null);
1672 UUID topMostUUID = service.save(topMost).getUuid();
1673 Marker marker = Marker.NewInstance(testTaxon, true, MarkerType.IS_DOUBTFUL());
1674 testTaxon.addMarker(marker);
1675 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1676 config.getTaxonNodeConfig().setChildHandling(ChildHandling.DELETE);
1677
1678 DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, node.getClassification().getUuid());
1679
1680 if(!result.isOk()){
1681 Assert.fail();
1682 }
1683 commitAndStartNewTransaction(null);
1684 Taxon tax = (Taxon)service.find(uuid);
1685 assertNull(tax);
1686 tax = (Taxon)service.find(topMostUUID);
1687 Set<TaxonNode> topMostNodes = tax.getTaxonNodes();
1688 assertNotNull(topMostNodes);
1689 assertEquals("there should be one taxon node", 1, topMostNodes.size());
1690 nodes = topMostNodes.iterator();
1691 TaxonNode topMostNode = nodes.next();
1692 int size = topMostNode.getChildNodes().size();
1693
1694 assertEquals(0, size);
1695 }
1696
1697
1698 @Test
1699 @DataSet(value="../../database/ClearDBDataSet.xml")
1700 public final void testTaxonDeletionConfiguratorTaxonWithMisappliedName(){
1701
1702 Taxon testTaxon = getTestTaxon();
1703 UUID uuid = service.save(testTaxon).getUuid();
1704
1705 Taxon misappliedName = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()), null);
1706
1707 Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1708 TaxonNode node =nodes.next();
1709 testTaxon.addMisappliedName(misappliedName, null, null);
1710 UUID misappliedNameUUID = service.save(misappliedName).getUuid();
1711
1712 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1713 config.setDeleteMisappliedNames(true);
1714
1715 DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, node.getClassification().getUuid());
1716 if(!result.isOk()){
1717 Assert.fail();
1718 }
1719 commitAndStartNewTransaction(null);
1720 Taxon tax = (Taxon)service.find(uuid);
1721 assertNull(tax);
1722 tax = (Taxon)service.find(misappliedNameUUID);
1723 //TODO: is that correct or should it be deleted because there is no relation to anything
1724 assertNull(tax);
1725
1726 }
1727 @Test
1728 @DataSet(value="../../database/ClearDBDataSet.xml")
1729 public final void testTaxonDeletionConfiguratorTaxonWithMisappliedNameDoNotDelete(){
1730
1731 Taxon testTaxon = getTestTaxon();
1732 UUID uuid = service.save(testTaxon).getUuid();
1733
1734 Taxon misappliedName = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()), null);
1735
1736 Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1737 TaxonNode node =nodes.next();
1738 testTaxon.addMisappliedName(misappliedName, null, null);
1739 UUID misappliedNameUUID = service.save(misappliedName).getUuid();
1740
1741 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1742 config.setDeleteMisappliedNames(false);
1743
1744 DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, node.getClassification().getUuid());
1745 if(!result.isOk()){
1746 Assert.fail();
1747 }
1748 commitAndStartNewTransaction(null);
1749 Taxon tax = (Taxon)service.find(uuid);
1750 assertNull(tax);
1751 tax = (Taxon)service.find(misappliedNameUUID);
1752 //TODO: is that correct or should it be deleted because there is no relation to anything
1753 assertNotNull(tax);
1754 }
1755
1756 @Test
1757 @DataSet(value="../../database/ClearDBDataSet.xml")
1758 public final void testTaxonDeletionConfiguratorTaxonMisappliedName(){
1759
1760 Taxon testTaxon = getTestTaxon();
1761 UUID uuid = service.save(testTaxon).getUuid();
1762
1763 Taxon misappliedNameTaxon = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()), null);
1764
1765 Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1766 TaxonNode node =nodes.next();
1767 testTaxon.addMisappliedName(misappliedNameTaxon, null, null);
1768 UUID misappliedNameUUID = service.save(misappliedNameTaxon).getUuid();
1769 misappliedNameTaxon = (Taxon)service.find(misappliedNameUUID);
1770 UUID misNameUUID = misappliedNameTaxon.getName().getUuid();
1771
1772 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1773
1774 service.deleteTaxon(misappliedNameTaxon.getUuid(), config,null);
1775
1776 commitAndStartNewTransaction(null);
1777 Taxon tax = (Taxon)service.find(uuid);
1778 assertNotNull(tax);
1779 tax = (Taxon)service.find(misappliedNameUUID);
1780 IBotanicalName name = nameService.find(misNameUUID);
1781
1782 assertNull(tax);
1783 assertNull(name);
1784 }
1785
1786 @Test
1787 @DataSet(value="../../database/ClearDBDataSet.xml")
1788 public final void testListIncludedTaxa(){
1789 Reference citation = null;
1790 String microcitation = null;
1791
1792 //Data
1793 Classification cl1 = Classification.NewInstance("testClassification1");
1794 Classification cl2 = Classification.NewInstance("testClassification2");
1795 Classification cl3 = Classification.NewInstance("testClassification3");
1796
1797 classificationService.save(cl1);
1798 classificationService.save(cl2);
1799 classificationService.save(cl3);
1800
1801 Taxon c1Genus = Taxon.NewInstance(null, null);c1Genus.setUuid(UUID.fromString("daa24f6f-7e38-4668-b385-10c789212e4e"));
1802 Taxon c1Species = Taxon.NewInstance(null, null);c1Species.setUuid(UUID.fromString("1c1d0566-67d0-4806-bf23-ecf55f4b9118"));
1803 Taxon c1SubSpecies1 = Taxon.NewInstance(null, null);c1SubSpecies1.setUuid(UUID.fromString("96ae2fad-76df-429f-b179-42e00838fea4"));
1804 Taxon c1SubSpecies2 = Taxon.NewInstance(null, null);c1SubSpecies2.setUuid(UUID.fromString("5d3f6147-ca72-40e0-be8a-6c835a09a579"));
1805 TaxonNode c1childNodeSpecies1 = cl1.addParentChild(c1Genus, c1Species, null, null);
1806 nodeService.saveOrUpdate(c1childNodeSpecies1.getParent());
1807 nodeService.saveOrUpdate(c1childNodeSpecies1);
1808 TaxonNode c1childNodeSubSpecies1 =cl1.addParentChild(c1Species, c1SubSpecies1, null, null);
1809 nodeService.saveOrUpdate(c1childNodeSubSpecies1);
1810 TaxonNode c1childNodeSubSpecies2 =cl1.addParentChild(c1Species, c1SubSpecies2, null, null);
1811 nodeService.saveOrUpdate(c1childNodeSubSpecies2);
1812
1813 Taxon c2Genus = Taxon.NewInstance(null, null);c2Genus.setUuid(UUID.fromString("ed0ec006-3ac8-4a12-ae13-fdf2a13dedbe"));
1814 Taxon c2Species = Taxon.NewInstance(null, null);c2Species.setUuid(UUID.fromString("1027eb18-1c26-450e-a299-981b775ebc3c"));
1815 Taxon c2SubSpecies1 = Taxon.NewInstance(null, null);c2SubSpecies1.setUuid(UUID.fromString("61f039c8-01f3-4f5d-8e16-1602139774e7"));
1816 Taxon c2SubSpecies2 = Taxon.NewInstance(null, null);c2SubSpecies2.setUuid(UUID.fromString("2ed6b6f8-05f9-459a-a075-2bca57e3013e"));
1817 TaxonNode c2childNodeSpecies1 = cl2.addParentChild(c2Genus, c2Species, null, null);
1818 nodeService.saveOrUpdate(c2childNodeSpecies1.getParent());
1819 nodeService.saveOrUpdate(c2childNodeSpecies1);
1820 TaxonNode c2childNodeSubSpecies1 = cl2.addParentChild(c2Species, c2SubSpecies1, null, null);
1821 nodeService.saveOrUpdate(c2childNodeSubSpecies1);
1822 TaxonNode c2childNodeSubSpecies2 = cl2.addParentChild(c2Species, c2SubSpecies2, null, null);
1823 nodeService.saveOrUpdate(c2childNodeSubSpecies2);
1824
1825 Taxon c3Genus = Taxon.NewInstance(null, null);c3Genus.setUuid(UUID.fromString("407dfc8d-7a4f-4370-ada4-76c1a8279d1f"));
1826 Taxon c3Species = Taxon.NewInstance(null, null);c3Species.setUuid(UUID.fromString("b6d34fc7-4aa7-41e5-b633-86f474edbbd5"));
1827 Taxon c3SubSpecies1 = Taxon.NewInstance(null, null);c3SubSpecies1.setUuid(UUID.fromString("01c07585-a422-40cd-9339-a74c56901d9f"));
1828 Taxon c3SubSpecies2 = Taxon.NewInstance(null, null);c3SubSpecies2.setUuid(UUID.fromString("390c8e23-e05f-4f89-b417-50cf080f4c91"));
1829 TaxonNode c3childNodeSpecies1 = cl3.addParentChild(c3Genus, c3Species, null, null);
1830 nodeService.saveOrUpdate(c3childNodeSpecies1.getParent());
1831 nodeService.saveOrUpdate(c3childNodeSpecies1);
1832 TaxonNode c3childNodeSubSpecies1 = cl3.addParentChild(c3Species, c3SubSpecies1, null, null);
1833 nodeService.saveOrUpdate(c3childNodeSubSpecies1);
1834 TaxonNode c3childNodeSubSpecies2 = cl3.addParentChild(c3Species, c3SubSpecies2, null, null);
1835 nodeService.saveOrUpdate(c3childNodeSubSpecies2);
1836
1837 Taxon c4Genus = Taxon.NewInstance(null, null);c4Genus.setUuid(UUID.fromString("bfd6bbdd-0116-4ab2-a781-9316224aad78"));
1838 Taxon c4Species = Taxon.NewInstance(null, null);c4Species.setUuid(UUID.fromString("9347a3d9-5ece-4d64-9035-e8aaf5d3ee02"));
1839 Taxon c4SubSpecies = Taxon.NewInstance(null, null);c4SubSpecies.setUuid(UUID.fromString("777aabbe-4c3a-449c-ab99-a91f2fec9f07"));
1840
1841 TaxonRelationship rel = c1Species.addTaxonRelation(c2Species, TaxonRelationshipType.CONGRUENT_TO(), citation, microcitation);
1842 rel.setDoubtful(true);
1843 c1Species.addTaxonRelation(c4Species, TaxonRelationshipType.INCLUDES(), citation, microcitation);
1844 c2Species.addTaxonRelation(c1SubSpecies2, TaxonRelationshipType.INCLUDES(), citation, microcitation);
1845
1846 service.saveOrUpdate(c1Species);
1847 service.saveOrUpdate(c2Species);
1848 service.save(c4Species);
1849 commitAndStartNewTransaction();
1850
1851 //Tests
1852 //default starting at species 1
1853 IncludedTaxaDTO dto = service.listIncludedTaxa(c1Species.getUuid(), new IncludedTaxonConfiguration(null, true, false));
1854 Assert.assertNotNull("IncludedTaxaDTO", dto);
1855 Assert.assertEquals("Result should contain 7 taxa: c1Species", 7, dto.getIncludedTaxa().size());
1856 Assert.assertNotNull("date should not be null", dto.getDate());
1857 // Assert.assertTrue(dto.contains(taxonUuid));
1858 //same without doubtful
1859 dto = service.listIncludedTaxa(c1Species.getUuid(), new IncludedTaxonConfiguration(null, false, false));
1860 Assert.assertEquals(4, dto.getIncludedTaxa().size());
1861
1862 //other example starting at Genus2
1863 dto = service.listIncludedTaxa(c2Genus.getUuid(), new IncludedTaxonConfiguration(null, true, false));
1864 Assert.assertEquals(8, dto.getIncludedTaxa().size());
1865 //same without doubtful
1866 dto = service.listIncludedTaxa(c2Genus.getUuid(), new IncludedTaxonConfiguration(null, false, false));
1867 Assert.assertEquals(5, dto.getIncludedTaxa().size());
1868
1869 //only congruent
1870 dto = service.listIncludedTaxa(c1Species.getUuid(), new IncludedTaxonConfiguration(null, true, true));
1871 Assert.assertEquals(2, dto.getIncludedTaxa().size());
1872 //same without doubtful
1873 dto = service.listIncludedTaxa(c1Species.getUuid(), new IncludedTaxonConfiguration(null, false, true));
1874 Assert.assertEquals(1, dto.getIncludedTaxa().size());
1875 }
1876
1877 @Test
1878 public void testDeleteDescriptions(){
1879 try {
1880 createTestDataSet();
1881 } catch (FileNotFoundException e) {
1882 // TODO Auto-generated catch block
1883 e.printStackTrace();
1884 }
1885 TaxonDescription description = TaxonDescription.NewInstance(taxWithoutSyn);
1886 SpecimenOrObservationBase<IIdentifiableEntityCacheStrategy<FieldUnit>> specimen = FieldUnit.NewInstance();
1887 UUID uuid = occurenceService.saveOrUpdate(specimen);
1888 DescriptionElementBase element = IndividualsAssociation.NewInstance(specimen);
1889 description.addElement(element);
1890 service.saveOrUpdate(taxWithoutSyn);
1891
1892
1893 Taxon tax = (Taxon)service.find(uuidTaxWithoutSyn);
1894 Set<TaxonDescription> descr = tax.getDescriptions();
1895 assertEquals(1, descr.size());
1896 description = descr.iterator().next();
1897 UUID uuidDescr = description.getUuid();
1898 UUID uuidDescEl = description.getElements().iterator().next().getUuid();
1899
1900 descriptionService.deleteDescription(description);
1901 service.saveOrUpdate(tax);
1902
1903 description = (TaxonDescription) descriptionService.find(uuidDescr);
1904 specimen = occurenceService.find(uuid);
1905 assertNull(description);
1906 DeleteResult result = occurenceService.delete(specimen);
1907 assertTrue(result.isOk());
1908
1909 }
1910
1911 @Test
1912 public void testRemoveDescriptionsFromTaxa(){
1913 try {
1914 createTestDataSet();
1915 } catch (FileNotFoundException e) {
1916 // TODO Auto-generated catch block
1917 e.printStackTrace();
1918 }
1919 TaxonDescription description = TaxonDescription.NewInstance(taxWithoutSyn);
1920 SpecimenOrObservationBase<IIdentifiableEntityCacheStrategy<FieldUnit>> specimen = FieldUnit.NewInstance();
1921 UUID uuid = occurenceService.saveOrUpdate(specimen);
1922 DescriptionElementBase element = IndividualsAssociation.NewInstance(specimen);
1923 description.addElement(element);
1924 service.saveOrUpdate(taxWithoutSyn);
1925
1926
1927 Taxon tax = (Taxon)service.find(uuidTaxWithoutSyn);
1928 Set<TaxonDescription> descr = tax.getDescriptions();
1929 assertEquals(1, descr.size());
1930 description = descr.iterator().next();
1931 UUID uuidDescr = description.getUuid();
1932
1933
1934 tax.removeDescription(description, true);
1935 service.saveOrUpdate(tax);
1936
1937 description = (TaxonDescription) descriptionService.find(uuidDescr);
1938 specimen = occurenceService.find(uuid);
1939 assertNotNull(description);
1940 DeleteResult result = occurenceService.delete(specimen);
1941 assertTrue(result.isOk());
1942 }
1943
1944 @Override
1945 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="ClearDBDataSet.xml")
1946 public void createTestDataSet() throws FileNotFoundException {
1947 Rank rank = Rank.SPECIES();
1948 taxWithoutSyn = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test1", null, null, null, null, null, null, null), null);
1949 taxWithSyn = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test3", null, null, null, null, null, null, null), null);
1950 tax2WithSyn = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test5", null, null, null, null, null, null, null), null);
1951 synonym = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test2", null, null, null, null, null, null, null), null);
1952 synonym2 = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test4", null, null, null, null, null, null, null), null);
1953 synonym2.getName().setHomotypicalGroup(synonym.getHomotypicGroup());
1954
1955 taxWithSyn.addSynonym(synonym, SynonymType.HETEROTYPIC_SYNONYM_OF);
1956 taxWithSyn.addSynonym(synonym2, SynonymType.HETEROTYPIC_SYNONYM_OF);
1957
1958 uuidTaxWithoutSyn = service.save(taxWithoutSyn).getUuid();
1959 uuidSyn = service.save(synonym).getUuid();
1960 uuidSyn2 = service.save(synonym2).getUuid();
1961 uuidTaxWithSyn =service.save(taxWithSyn).getUuid();
1962
1963 }
1964
1965 //public static UUID DESCRIPTION1_UUID = UUID.fromString("f3e061f6-c5df-465c-a253-1e18ab4c7e50");
1966 //public static UUID DESCRIPTION2_UUID = UUID.fromString("1b009a40-ebff-4f7e-9f7f-75a850ba995d");
1967
1968 public Taxon getTestTaxon(){
1969 int descrIndex = 6000;
1970 Person deCandolle = Person.NewInstance();
1971 deCandolle.setTitleCache("DC.", true);
1972
1973 Reference sec = ReferenceFactory.newDatabase();
1974 sec.setTitleCache("Flora lunaea", true);
1975 Reference citationRef = ReferenceFactory.newBook();
1976 citationRef.setTitleCache("Sp. lunarum", true);
1977
1978 //genus taxon with Name, combinationAuthor,
1979 IBotanicalName botName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
1980 botName.setTitleCache("Hieracium L.", true);
1981 botName.setGenusOrUninomial("Hieracium");
1982 botName.setCombinationAuthorship(Person.NewInstance());
1983 botName.getCombinationAuthorship().setNomenclaturalTitleCache("L.", true);
1984 botName.setUuid(GENUS_NAME_UUID);
1985 Taxon genusTaxon = Taxon.NewInstance(botName, sec);
1986 genusTaxon.setUuid(GENUS_UUID);
1987 service.save(genusTaxon);
1988 //a name that is the basionym of genusTaxon's name
1989 TaxonName basionym = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
1990 basionym.setTitleCache("Hieracilla DC.", true);
1991 basionym.setGenusOrUninomial("Hieracilla");
1992 basionym.setCombinationAuthorship(deCandolle);
1993 basionym.setUuid(BASIONYM_UUID);
1994 botName.addBasionym(basionym, null, null,"216", null);
1995 nameService.saveOrUpdate(basionym);
1996 //species taxon that is the child of genus taxon
1997 IBotanicalName botSpecies = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
1998 botSpecies.setTitleCache("Hieracium asturianum Pau", true);
1999 botSpecies.setGenusOrUninomial("Hieracium");
2000 botSpecies.setSpecificEpithet("asturianum");
2001 botSpecies.setCombinationAuthorship(Person.NewInstance());
2002 botSpecies.getCombinationAuthorship().setNomenclaturalTitleCache("Pau", true);
2003 botSpecies.setUuid(SPECIES1_NAME_UUID);
2004 Taxon childTaxon = Taxon.NewInstance(botSpecies, sec);
2005 childTaxon.setUuid(SPECIES1_UUID);
2006 TaxonDescription taxDesc = getTestDescription(descrIndex++);
2007 //taxDesc.setUuid(DESCRIPTION1_UUID);
2008 childTaxon.addDescription(taxDesc);
2009 service.saveOrUpdate(childTaxon);
2010 Classification classification = getTestClassification("TestClassification");
2011 classification.addParentChild(genusTaxon, childTaxon, citationRef, "456");
2012 // childTaxon.setTaxonomicParent(genusTaxon, citationRef, "456");
2013 classificationService.save(classification);
2014 //homotypic synonym of childTaxon1
2015 IBotanicalName botSpecies4= TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
2016 botSpecies4.setTitleCache("Hieracium gueri DC.", true);
2017 botSpecies4.setGenusOrUninomial("Hieracium");
2018 botSpecies4.setSpecificEpithet("gueri");
2019 botSpecies4.setCombinationAuthorship(deCandolle);
2020 botSpecies4.setUuid(SYNONYM_NAME_UUID);
2021 Synonym homoSynonym = Synonym.NewInstance(botSpecies4, sec);
2022
2023 childTaxon.addSynonym(homoSynonym, SynonymType.HOMOTYPIC_SYNONYM_OF);
2024 service.saveOrUpdate(childTaxon);
2025
2026 //2nd child species taxon that is the child of genus taxon
2027 IBotanicalName botSpecies2= TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
2028 botSpecies2.setTitleCache("Hieracium wolffii Zahn", true);
2029 botSpecies2.setGenusOrUninomial("Hieracium");
2030 botSpecies2.setSpecificEpithet("wolffii");
2031 botSpecies2.setCombinationAuthorship(Person.NewInstance());
2032 botSpecies2.getCombinationAuthorship().setNomenclaturalTitleCache("Zahn", true);
2033 botSpecies2.setUuid(SPECIES2_NAME_UUID);
2034 Taxon childTaxon2 = Taxon.NewInstance(botSpecies2, sec);
2035 childTaxon2.setUuid(SPECIES2_UUID);
2036 classification.addParentChild(genusTaxon, childTaxon2, citationRef, "499");
2037 //childTaxon2.setTaxonomicParent(genusTaxon, citationRef, "499");
2038 service.saveOrUpdate(childTaxon2);
2039 //heterotypic synonym of childTaxon2
2040 IBotanicalName botSpecies3= TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
2041 botSpecies3.setTitleCache("Hieracium lupium DC.", true);
2042 botSpecies3.setGenusOrUninomial("Hieracium");
2043 botSpecies3.setSpecificEpithet("lupium");
2044 botSpecies3.setCombinationAuthorship(deCandolle);
2045 botSpecies3.setUuid(SYNONYM2_NAME_UUID);
2046 Synonym heteroSynonym = Synonym.NewInstance(botSpecies3, sec);
2047 heteroSynonym.setUuid(SYNONYM2_UUID);
2048 childTaxon2.addSynonym(heteroSynonym, SynonymType.HETEROTYPIC_SYNONYM_OF);
2049 service.saveOrUpdate(childTaxon2);
2050 //missaplied Name for childTaxon2
2051 IBotanicalName missName= TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
2052 missName.setTitleCache("Hieracium lupium DC.", true);
2053 missName.setGenusOrUninomial("Hieracium");
2054 missName.setSpecificEpithet("lupium");
2055 missName.setCombinationAuthorship(deCandolle);
2056 missName.setUuid(SPECIES5_NAME_UUID);
2057 Taxon misappliedNameTaxon = Taxon.NewInstance(missName, sec);
2058 childTaxon2.addMisappliedName(misappliedNameTaxon, citationRef, "125");
2059 taxDesc = getTestDescription(descrIndex++);
2060 // taxDesc.setUuid(DESCRIPTION2_UUID);
2061 genusTaxon.addDescription(taxDesc);
2062 service.saveOrUpdate(genusTaxon);
2063 service.save(misappliedNameTaxon);
2064
2065 return genusTaxon;
2066 }
2067
2068 public TaxonDescription getTestDescription(int index){
2069 TaxonDescription taxonDescription = TaxonDescription.NewInstance();
2070 Language language = Language.DEFAULT();
2071 //taxonDescription.setId(index);
2072
2073 //textData
2074 TextData textData = TextData.NewInstance();
2075 String descriptionText = "this is a desciption for a taxon";
2076 LanguageString languageString = LanguageString.NewInstance(descriptionText, language);
2077 textData.putText(languageString);
2078 taxonDescription.addElement(textData);
2079
2080 //commonName
2081
2082 String commonNameString = "Schönveilchen";
2083 CommonTaxonName commonName = CommonTaxonName.NewInstance(commonNameString, language);
2084 taxonDescription.addElement(commonName);
2085
2086 return taxonDescription;
2087 }
2088
2089 public Classification getTestClassification(String name){
2090 return Classification.NewInstance(name);
2091 }
2092 }