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