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