fix test for now stricter initialization strategy
[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
10 package eu.etaxonomy.cdm.api.service;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16
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.NameDeletionConfigurator;
30 import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
31 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
32 import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator.ChildHandling;
33 import eu.etaxonomy.cdm.api.service.exception.DataChangeNoRollbackException;
34 import eu.etaxonomy.cdm.api.service.exception.HomotypicalGroupChangeException;
35 import eu.etaxonomy.cdm.datagenerator.TaxonGenerator;
36 import eu.etaxonomy.cdm.model.common.CdmBase;
37 import eu.etaxonomy.cdm.model.common.Marker;
38 import eu.etaxonomy.cdm.model.common.MarkerType;
39 import eu.etaxonomy.cdm.model.common.RelationshipBase;
40 import eu.etaxonomy.cdm.model.description.TaxonDescription;
41 import eu.etaxonomy.cdm.model.name.BotanicalName;
42 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
43 import eu.etaxonomy.cdm.model.name.NameRelationship;
44 import eu.etaxonomy.cdm.model.name.NameRelationshipType;
45 import eu.etaxonomy.cdm.model.name.NonViralName;
46 import eu.etaxonomy.cdm.model.name.Rank;
47 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
48 import eu.etaxonomy.cdm.model.reference.Reference;
49 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
50 import eu.etaxonomy.cdm.model.taxon.Classification;
51 import eu.etaxonomy.cdm.model.taxon.Synonym;
52 import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
53 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
54 import eu.etaxonomy.cdm.model.taxon.Taxon;
55 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
56 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
57 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
58 import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
59
60 /**
61 * @author a.mueller
62 */
63
64
65 public class TaxonServiceImplTest extends CdmTransactionalIntegrationTest {
66 private static final Logger logger = Logger.getLogger(TaxonServiceImplTest.class);
67
68 @SpringBeanByType
69 private ITaxonService service;
70
71 @SpringBeanByType
72 private INameService nameService;
73
74 @SpringBeanByType
75 private IReferenceService referenceService;
76
77 @SpringBeanByType
78 private IClassificationService classificationService;
79
80 @SpringBeanByType
81 private ITaxonNodeService nodeService;
82
83 @SpringBeanByType
84 private IDescriptionService descriptionService;
85
86 @SpringBeanByType
87 private IMarkerService markerService;
88
89
90
91
92
93 /****************** TESTS *****************************/
94
95
96 /**
97 * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#getTaxonByUuid(java.util.UUID)}.
98 */
99 @Test
100 public final void testGetTaxonByUuid() {
101 Taxon expectedTaxon = Taxon.NewInstance(null, null);
102 UUID uuid = service.save(expectedTaxon);
103 TaxonBase<?> actualTaxon = service.find(uuid);
104 assertEquals(expectedTaxon, actualTaxon);
105 }
106
107 /**
108 * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#saveTaxon(eu.etaxonomy.cdm.model.taxon.TaxonBase)}.
109 */
110 @Test
111 public final void testSaveTaxon() {
112 Taxon expectedTaxon = Taxon.NewInstance(null, null);
113 UUID uuid = service.save(expectedTaxon);
114 TaxonBase<?> actualTaxon = service.find(uuid);
115 assertEquals(expectedTaxon, actualTaxon);
116 }
117
118 @Test
119 public final void testSaveOrUpdateTaxon() {
120 Taxon expectedTaxon = Taxon.NewInstance(null, null);
121 UUID uuid = service.save(expectedTaxon);
122 TaxonBase<?> actualTaxon = service.find(uuid);
123 assertEquals(expectedTaxon, actualTaxon);
124
125 actualTaxon.setName(BotanicalName.NewInstance(Rank.SPECIES()));
126 try{
127 service.saveOrUpdate(actualTaxon);
128 }catch(Exception e){
129 Assert.fail();
130 }
131 }
132 /**
133 * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#removeTaxon(eu.etaxonomy.cdm.model.taxon.TaxonBase)}.
134 */
135 @Test
136 public final void testRemoveTaxon() {
137 Taxon taxon = Taxon.NewInstance(BotanicalName.NewInstance(Rank.UNKNOWN_RANK()), null);
138 UUID uuid = service.save(taxon);
139 service.delete(taxon);
140 TaxonBase<?> actualTaxon = service.find(uuid);
141 assertNull(actualTaxon);
142 }
143
144
145 @Test
146 public final void testMakeTaxonSynonym() {
147 Rank rank = Rank.SPECIES();
148 Taxon tax1 = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test1", null, null, null, null, null, null, null), null);
149 Synonym synonym = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
150 tax1.addHomotypicSynonym(synonym, null, null);
151 UUID uuidTaxon = service.save(tax1);
152 UUID uuidSyn = service.save(synonym);
153
154 service.swapSynonymAndAcceptedTaxon(synonym, tax1);
155
156 // find forces flush
157 TaxonBase<?> tax = service.find(uuidTaxon);
158 TaxonBase<?> syn = service.find(uuidSyn);
159
160 assertTrue(tax.getName().getTitleCache().equals("Test2"));
161
162 HomotypicalGroup groupTest = tax.getHomotypicGroup();
163 HomotypicalGroup groupTest2 = syn.getHomotypicGroup();
164 assertEquals(groupTest, groupTest2);
165
166 }
167
168 @Test
169 public final void testChangeSynonymToAcceptedTaxon(){
170 Rank rank = Rank.SPECIES();
171 //HomotypicalGroup group = HomotypicalGroup.NewInstance();
172 Taxon taxWithoutSyn = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test1", null, null, null, null, null, null, null), null);
173 Taxon taxWithSyn = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test3", null, null, null, null, null, null, null), null);
174 Synonym synonym = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
175 Synonym synonym2 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test4", null, null, null, null, null, null, null), null);
176 synonym2.getName().setHomotypicalGroup(synonym.getHomotypicGroup());
177 //tax2.addHeterotypicSynonymName(synonym.getName());
178 taxWithSyn.addSynonym(synonym, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
179 taxWithSyn.addSynonym(synonym2, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
180
181 service.save(taxWithoutSyn);
182 UUID uuidSyn = service.save(synonym);
183 service.save(synonym2);
184 UUID uuidTaxWithSyn =service.save(taxWithSyn);
185
186 Taxon taxon = null;
187 try {
188 taxon = service.changeSynonymToAcceptedTaxon(synonym, taxWithSyn, true, true, null, null);
189 } catch (HomotypicalGroupChangeException e) {
190 Assert.fail("Invocation of change method should not throw an exception");
191 }
192 taxWithSyn = null;
193 //test flush (resave deleted object)
194 TaxonBase<?> syn = service.find(uuidSyn);
195 taxWithSyn = (Taxon)service.find(uuidTaxWithSyn);
196 Taxon taxNew = (Taxon)service.find(taxon.getUuid());
197 assertNull(syn);
198 assertNotNull(taxWithSyn);
199 assertNotNull(taxNew);
200
201 Assert.assertEquals("New taxon should have 1 synonym relationship (the old homotypic synonym)", 1, taxon.getSynonymRelations().size());
202 }
203
204
205
206 @Test
207 public final void testChangeSynonymToAcceptedTaxonSynonymForTwoTaxa(){
208 Rank rank = Rank.SPECIES();
209 //HomotypicalGroup group = HomotypicalGroup.NewInstance();
210 Taxon taxWithoutSyn = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test1", null, null, null, null, null, null, null), null);
211 Taxon tax2WithSyn = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test5", null, null, null, null, null, null, null), null);
212 Taxon taxWithSyn = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test3", null, null, null, null, null, null, null), null);
213 Synonym synonym = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
214 Synonym synonym2 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test4", null, null, null, null, null, null, null), null);
215 //synonym2.getName().setHomotypicalGroup(taxWithSyn.getHomotypicGroup());
216 //tax2.addHeterotypicSynonymName(synonym.getName());
217 taxWithSyn.addSynonym(synonym, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
218 taxWithSyn.addSynonym(synonym2, SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF());
219 tax2WithSyn.addSynonym(synonym, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
220
221 service.save(taxWithoutSyn);
222
223 UUID uuidSyn = service.save(synonym);
224 service.save(synonym2);
225 UUID uuidTaxWithSyn =service.save(taxWithSyn);
226 service.save(tax2WithSyn);
227
228
229 Taxon taxon = null;
230 try {
231 taxon = service.changeSynonymToAcceptedTaxon(synonym, taxWithSyn, true, true, null, null);
232 service.save(taxon);
233 } catch (HomotypicalGroupChangeException e) {
234 Assert.fail("Invocation of change method should not throw an exception");
235 }
236 taxWithSyn = null;
237 tax2WithSyn = null;
238
239 //test flush (resave deleted object)
240 TaxonBase<?> syn = service.find(uuidSyn);
241 taxWithSyn = (Taxon)service.find(uuidTaxWithSyn);
242 Taxon taxNew = (Taxon)service.find(taxon.getUuid());
243 assertNotNull(syn);
244 assertNotNull(taxWithSyn);
245 assertNotNull(taxNew);
246
247 // Assert.assertEquals("New taxon should have 1 synonym relationship (the old homotypic synonym)", 1, taxon.getSynonymRelations().size());
248 }
249
250 /**
251 * Old implementation taken from {@link TaxonServiceImplBusinessTest} for old version of method.
252 * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#moveSynonymToAnotherTaxon(eu.etaxonomy.cdm.model.taxon.SynonymRelationship, eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType, eu.etaxonomy.cdm.model.reference.Reference, java.lang.String)}.
253 */
254 @Test
255 public final void testMoveSynonymToAnotherTaxon_OLD() {
256 SynonymRelationshipType heteroTypicSynonymRelationshipType = SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF();
257 Reference<?> reference = ReferenceFactory.newGeneric();
258 String referenceDetail = "test";
259
260 NonViralName<?> t1n = NonViralName.NewInstance(null);
261 Taxon t1 = Taxon.NewInstance(t1n, reference);
262 NonViralName<?> t2n = NonViralName.NewInstance(null);
263 Taxon t2 = Taxon.NewInstance(t2n, reference);
264 NonViralName<?> s1n = NonViralName.NewInstance(null);
265 Synonym s1 = Synonym.NewInstance(s1n, reference);
266 t1.addSynonym(s1, heteroTypicSynonymRelationshipType);
267
268 SynonymRelationship synonymRelation = t1.getSynonymRelations().iterator().next();
269
270 boolean keepReference = false;
271 boolean moveHomotypicGroup = false;
272 try {
273 service.moveSynonymToAnotherTaxon(synonymRelation, t2, moveHomotypicGroup, heteroTypicSynonymRelationshipType, reference, referenceDetail, keepReference);
274 } catch (HomotypicalGroupChangeException e) {
275 Assert.fail("Method call should not throw exception");
276 }
277
278 Assert.assertTrue("t1 should have no synonym relationships", t1.getSynonymRelations().isEmpty());
279
280 Set<SynonymRelationship> synonymRelations = t2.getSynonymRelations();
281 Assert.assertTrue("t2 should have exactly one synonym relationship", synonymRelations.size() == 1);
282
283 synonymRelation = synonymRelations.iterator().next();
284
285 Assert.assertEquals(t2, synonymRelation.getAcceptedTaxon());
286 Assert.assertEquals(heteroTypicSynonymRelationshipType, synonymRelation.getType());
287 Assert.assertEquals(reference, synonymRelation.getCitation());
288 Assert.assertEquals(referenceDetail, synonymRelation.getCitationMicroReference());
289 }
290
291 @Test
292 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testMoveSynonymToAnotherTaxon.xml")
293 public final void testMoveSynonymToAnotherTaxon() throws Exception {
294 final String[] tableNames = new String[]{"SynonymRelationship"};
295
296 // printDataSet(System.err, new String[]{"AgentBase", "TaxonBase"});
297 // printDataSet(System.err, new String[]{"TaxonNode"});
298
299 UUID uuidNewTaxon = UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
300 UUID uuidOldTaxon = UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
301 UUID uuidSyn1 = UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
302 UUID uuidSyn3 = UUID.fromString("3fba2b22-22ae-4291-af67-faab748a5232");
303 UUID uuidSyn4 = UUID.fromString("f9b589c7-50cf-4df2-a52e-1b85eb7e4805");
304 UUID uuidSyn5 = UUID.fromString("fcc0bcf8-8bac-43bd-9508-1e97821587dd");
305 UUID uuidSyn6 = UUID.fromString("0ccd4e7c-6fbd-4b7c-bd47-29e45b92f34b");
306 UUID uuidRef1 = UUID.fromString("336f9b38-698c-45d7-be7b-993ed3355bdc");
307 UUID uuidRef2 = UUID.fromString("c8f49d1a-69e1-48a3-98bb-45d61f3da3e7");
308
309
310 boolean moveHomotypicGroup = true;
311 SynonymRelationshipType newSynonymRelationshipType = null;
312 boolean keepReference = true;
313 Reference<?> newReference = null;
314 String newReferenceDetail = null;
315
316 Taxon newTaxon = (Taxon)service.load(uuidNewTaxon);
317 Synonym homotypicSynonym = (Synonym)service.load(uuidSyn1);
318 Assert.assertNotNull("Synonym should exist", homotypicSynonym);
319 Assert.assertEquals("Synonym should have 1 relation", 1, homotypicSynonym.getSynonymRelations().size());
320 SynonymRelationship rel = homotypicSynonym.getSynonymRelations().iterator().next();
321 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, rel.getAcceptedTaxon().getUuid());
322 Taxon oldTaxon = rel.getAcceptedTaxon();
323
324 try {
325 service.moveSynonymToAnotherTaxon(rel, newTaxon, moveHomotypicGroup, newSynonymRelationshipType, newReference, newReferenceDetail, keepReference);
326 Assert.fail("Homotypic synonym move to other taxon should throw an exception");
327 } catch (HomotypicalGroupChangeException e) {
328 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")){
329 //OK
330 commitAndStartNewTransaction(tableNames);
331 }else{
332 Assert.fail("Unexpected exception occurred: " + e.getMessage());
333 }
334 }
335 //Asserts
336 homotypicSynonym = (Synonym)service.load(uuidSyn1);
337 Assert.assertNotNull("Synonym should still exist", homotypicSynonym);
338 Assert.assertEquals("Synonym should still have 1 relation", 1, homotypicSynonym.getSynonymRelations().size());
339 rel = homotypicSynonym.getSynonymRelations().iterator().next();
340 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", oldTaxon, rel.getAcceptedTaxon());
341
342 //test heterotypic synonym with other synonym in homotypic group
343 newTaxon = (Taxon)service.load(uuidNewTaxon);
344 Synonym heterotypicSynonym = (Synonym)service.load(uuidSyn3);
345 Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
346 Assert.assertEquals("Synonym should have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
347 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
348 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, rel.getAcceptedTaxon().getUuid());
349 oldTaxon = rel.getAcceptedTaxon();
350 moveHomotypicGroup = false;
351
352 try {
353 service.moveSynonymToAnotherTaxon(rel, newTaxon, moveHomotypicGroup, newSynonymRelationshipType, newReference, newReferenceDetail, keepReference);
354 Assert.fail("Heterotypic synonym move to other taxon should throw an exception");
355 } catch (HomotypicalGroupChangeException e) {
356 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")){
357 //OK
358 commitAndStartNewTransaction(tableNames);
359 }else{
360 Assert.fail("Unexpected exception occurred: " + e.getMessage());
361 }
362 }
363 //Asserts
364 heterotypicSynonym = (Synonym)service.load(uuidSyn3);
365 Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
366 Assert.assertEquals("Synonym should still have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
367 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
368 Assert.assertEquals("Accepted taxon of single relation should still be the old taxon", oldTaxon, rel.getAcceptedTaxon());
369
370
371 //test heterotypic synonym with no other synonym in homotypic group
372 //+ keep reference
373
374 // printDataSet(System.err, new String[]{"TaxonBase"});
375
376 newTaxon = (Taxon)service.load(uuidNewTaxon);
377 heterotypicSynonym = (Synonym)service.load(uuidSyn5);
378 Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
379 Assert.assertEquals("Synonym should have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
380 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
381 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, rel.getAcceptedTaxon().getUuid());
382 oldTaxon = rel.getAcceptedTaxon();
383 moveHomotypicGroup = false;
384
385
386 try {
387 service.moveSynonymToAnotherTaxon(rel, newTaxon, moveHomotypicGroup, newSynonymRelationshipType, newReference, newReferenceDetail, keepReference);
388 } catch (HomotypicalGroupChangeException e) {
389 Assert.fail("Move of single heterotypic synonym should not throw exception: " + e.getMessage());
390 }
391 //Asserts
392 //FIXME throws exception
393 commitAndStartNewTransaction(tableNames);
394
395 // printDataSet(System.err, new String[]{"AgentBase", "TaxonBase"});
396 //
397 // printDataSet(System.err, new String[]{"TaxonBase"});
398
399 heterotypicSynonym = (Synonym)service.load(uuidSyn5);
400
401 // printDataSet(System.err, new String[]{"TaxonBase"});
402 // System.exit(0);
403
404 Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
405 Assert.assertEquals("Synonym should still have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
406 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
407 Assert.assertEquals("Accepted taxon of single relation should be new taxon", newTaxon, rel.getAcceptedTaxon());
408 Assert.assertEquals("Old detail should be kept", "rel5", rel.getCitationMicroReference());
409
410
411 //test heterotypic synonym with other synonym in homotypic group and moveHomotypicGroup="true"
412 //+ new detail
413 newTaxon = (Taxon)service.load(uuidNewTaxon);
414 heterotypicSynonym = (Synonym)service.load(uuidSyn3);
415 Reference<?> ref1 = referenceService.load(uuidRef1);
416 Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
417 Assert.assertEquals("Synonym should have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
418 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
419 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, rel.getAcceptedTaxon().getUuid());
420 oldTaxon = rel.getAcceptedTaxon();
421 Assert.assertEquals("Detail should be ref1", ref1, rel.getCitation());
422 Assert.assertEquals("Detail should be 'rel3'", "rel3", rel.getCitationMicroReference());
423 TaxonNameBase<?,?> oldSynName3 = heterotypicSynonym.getName();
424
425 Synonym heterotypicSynonym4 = (Synonym)service.load(uuidSyn4);
426 Assert.assertNotNull("Synonym should exist", heterotypicSynonym4);
427 Assert.assertEquals("Synonym should have 1 relation", 1, heterotypicSynonym4.getSynonymRelations().size());
428 SynonymRelationship rel4 = heterotypicSynonym4.getSynonymRelations().iterator().next();
429 Assert.assertEquals("Accepted taxon of other synonym in group should be the old taxon", uuidOldTaxon, rel4.getAcceptedTaxon().getUuid());
430 Assert.assertSame("Homotypic group of both synonyms should be same", oldSynName3.getHomotypicalGroup() , heterotypicSynonym4.getName().getHomotypicalGroup() );
431
432 moveHomotypicGroup = true;
433 keepReference = false;
434
435 try {
436 service.moveSynonymToAnotherTaxon(rel, newTaxon, moveHomotypicGroup, newSynonymRelationshipType, newReference, newReferenceDetail, keepReference);
437 } catch (HomotypicalGroupChangeException e) {
438 Assert.fail("Move with 'moveHomotypicGroup = true' should not throw exception: " + e.getMessage());
439 }
440 //Asserts
441 commitAndStartNewTransaction(tableNames);
442 heterotypicSynonym = (Synonym)service.load(uuidSyn3);
443 Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
444 Assert.assertEquals("Synonym should still have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
445 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
446 Assert.assertEquals("Accepted taxon of relation should be new taxon now", newTaxon, rel.getAcceptedTaxon());
447 TaxonNameBase<?,?> synName3 = rel.getSynonym().getName();
448
449 heterotypicSynonym = (Synonym)service.load(uuidSyn4);
450 Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
451 Assert.assertEquals("Synonym should still have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
452 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
453 Assert.assertEquals("Accepted taxon of relation should be new taxon now", newTaxon, rel.getAcceptedTaxon());
454 Assert.assertNull("Old citation should be removed", rel.getCitation());
455 Assert.assertNull("Old detail should be removed", rel.getCitationMicroReference());
456 TaxonNameBase<?,?> synName4 = rel.getSynonym().getName();
457 Assert.assertEquals("Homotypic group of both synonyms should be equal", synName3.getHomotypicalGroup() , synName4.getHomotypicalGroup() );
458 Assert.assertSame("Homotypic group of both synonyms should be same", synName3.getHomotypicalGroup() , synName4.getHomotypicalGroup() );
459 Assert.assertEquals("Homotypic group of both synonyms should be equal to old homotypic group", oldSynName3.getHomotypicalGroup() , synName3.getHomotypicalGroup() );
460
461
462 //test single heterotypic synonym to homotypic synonym of new taxon
463 //+ new reference
464 newTaxon = (Taxon)service.load(uuidNewTaxon);
465 Reference<?> ref2 = referenceService.load(uuidRef2);
466 heterotypicSynonym = (Synonym)service.load(uuidSyn6);
467 Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
468 Assert.assertEquals("Synonym should have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
469 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
470 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, rel.getAcceptedTaxon().getUuid());
471 oldTaxon = rel.getAcceptedTaxon();
472 moveHomotypicGroup = false;
473 keepReference = false;
474 newReference = ref2;
475 newReferenceDetail = "newRefDetail";
476 newSynonymRelationshipType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
477
478 try {
479 service.moveSynonymToAnotherTaxon(rel, newTaxon, moveHomotypicGroup, newSynonymRelationshipType, newReference, newReferenceDetail, keepReference);
480 } catch (HomotypicalGroupChangeException e) {
481 Assert.fail("Move of single heterotypic synonym should not throw exception: " + e.getMessage());
482 }
483 //Asserts
484 commitAndStartNewTransaction(tableNames);
485 heterotypicSynonym = (Synonym)service.load(uuidSyn6);
486 Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
487 Assert.assertEquals("Synonym should still have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
488 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
489 Assert.assertEquals("Relationship type should be 'homotypic synonym'", newSynonymRelationshipType, rel.getType());
490 Assert.assertEquals("Accepted taxon of single relation should be new taxon", newTaxon, rel.getAcceptedTaxon());
491 Assert.assertEquals("New citation should be ref2", ref2 ,rel.getCitation());
492 Assert.assertEquals("New detail should be kept", "newRefDetail", rel.getCitationMicroReference());
493
494 Assert.assertEquals("New taxon and new synonym should have equal homotypical group", rel.getSynonym().getHomotypicGroup(), rel.getAcceptedTaxon().getHomotypicGroup());
495 Assert.assertSame("New taxon and new synonym should have same homotypical group", rel.getSynonym().getHomotypicGroup(), rel.getAcceptedTaxon().getHomotypicGroup());
496 }
497
498
499
500 @Test
501 public final void testGetHeterotypicSynonymyGroups(){
502 Rank rank = Rank.SPECIES();
503 Reference<?> ref1 = ReferenceFactory.newGeneric();
504 //HomotypicalGroup group = HomotypicalGroup.NewInstance();
505 Taxon taxon1 = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test3", null, null, null, null, null, null, null), null);
506 Synonym synonym0 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
507 Synonym synonym1 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
508 Synonym synonym2 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test4", null, null, null, null, null, null, null), null);
509 synonym0.getName().setHomotypicalGroup(taxon1.getHomotypicGroup());
510 synonym2.getName().setHomotypicalGroup(synonym1.getHomotypicGroup());
511 //tax2.addHeterotypicSynonymName(synonym.getName());
512 taxon1.addSynonym(synonym1, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
513 taxon1.addSynonym(synonym2, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
514
515 service.save(synonym1);
516 service.save(synonym2);
517 service.save(taxon1);
518
519 List<List<Synonym>> heteroSyns = service.getHeterotypicSynonymyGroups(taxon1, null);
520 Assert.assertEquals("There should be 1 heterotypic group", 1, heteroSyns.size());
521 List<Synonym> synList = heteroSyns.get(0);
522 Assert.assertEquals("There should be 2 heterotypic syns in group 1", 2, synList.size());
523
524 //test sec
525 synonym2.setSec(ref1);
526 heteroSyns = service.getHeterotypicSynonymyGroups(taxon1, null);
527 Assert.assertEquals("There should be 1 heterotypic group", 1, heteroSyns.size());
528 synList = heteroSyns.get(0);
529 Assert.assertEquals("getHeterotypicSynonymyGroups should be independent of sec reference", 2, synList.size());
530
531 }
532
533
534 @Test
535 public final void testGetHomotypicSynonymsByHomotypicGroup(){
536 Rank rank = Rank.SPECIES();
537 Reference<?> ref1 = ReferenceFactory.newGeneric();
538 //HomotypicalGroup group = HomotypicalGroup.NewInstance();
539 Taxon taxon1 = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test3", null, null, null, null, null, null, null), null);
540 Synonym synonym0 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
541 Synonym synonym1 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
542 Synonym synonym2 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test4", null, null, null, null, null, null, null), null);
543 synonym0.getName().setHomotypicalGroup(taxon1.getHomotypicGroup());
544 synonym2.getName().setHomotypicalGroup(synonym1.getHomotypicGroup());
545 //tax2.addHeterotypicSynonymName(synonym.getName());
546 taxon1.addSynonym(synonym0, SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF());
547 taxon1.addSynonym(synonym1, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
548 taxon1.addSynonym(synonym2, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
549
550 service.save(synonym1);
551 service.save(synonym2);
552 service.save(taxon1);
553
554 List<Synonym> homoSyns = service.getHomotypicSynonymsByHomotypicGroup(taxon1, null);
555 Assert.assertEquals("There should be 1 heterotypic group", 1, homoSyns.size());
556 Assert.assertSame("The homotypic synonym should be synonym0", synonym0, homoSyns.get(0));
557
558 //test sec
559 synonym0.setSec(ref1);
560 homoSyns = service.getHomotypicSynonymsByHomotypicGroup(taxon1, null);
561 Assert.assertEquals("getHeterotypicSynonymyGroups should be independent of sec reference", 1, homoSyns.size());
562
563 }
564
565 @Test
566 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
567 //test delete synonym, but the name will not be deleted
568 public final void testDeleteSynonymSynonymTaxonDontDeleteName(){
569 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
570 "SynonymRelationship","SynonymRelationship_AUD",
571 "HomotypicalGroup","HomotypicalGroup_AUD"};
572
573 int nSynonyms = service.count(Synonym.class);
574 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
575 int nNames = nameService.count(TaxonNameBase.class);
576 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
577
578
579 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
580
581
582 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
583 SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
584 config.setDeleteNameIfPossible(false);
585 config.setNewHomotypicGroupIfNeeded(true);
586 service.deleteSynonym(synonym1, config);
587
588 this.commitAndStartNewTransaction(tableNames);
589
590 nSynonyms = service.count(Synonym.class);
591 Assert.assertEquals("There should be 1 synonym left in the database", 1, nSynonyms);
592 nNames = nameService.count(TaxonNameBase.class);
593 Assert.assertEquals("There should be 4 names left in the database", 4, nNames);
594 int nRelations = service.countAllRelationships();
595 Assert.assertEquals("There should be no relationship left in the database", 0, nRelations);
596 }
597
598 @Test
599 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
600 //test delete synonym and his name
601 public final void testDeleteSynonymSynonymTaxonDeleteName(){
602 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
603 "SynonymRelationship","SynonymRelationship_AUD",
604 "HomotypicalGroup","HomotypicalGroup_AUD"};
605
606 int nSynonyms = service.count(Synonym.class);
607 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
608 int nNames = nameService.count(TaxonNameBase.class);
609 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
610 int nRelations = service.countAllRelationships();
611
612 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
613
614
615 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
616 service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
617
618 this.commitAndStartNewTransaction(tableNames);
619
620 nSynonyms = service.count(Synonym.class);
621 Assert.assertEquals("There should be 1 synonym left in the database", 1, nSynonyms);
622 nNames = nameService.count(TaxonNameBase.class);
623 Assert.assertEquals("There should be 3 names left in the database", 3, nNames);
624 nRelations = service.countAllRelationships();
625 Assert.assertEquals("There should be no relationship left in the database", 0, nRelations);
626
627 }
628
629 @Test
630 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
631 //test remove synonym from taxon -> synonym and name still in the db and the synonymrelationship to the other taxon
632 //test delete synonym -> all relationships are deleted, the name is deleted and the synonym itself
633 public final void testDeleteSynonymSynonymTaxonBooleanRelToOneTaxon(){
634 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
635 "SynonymRelationship","SynonymRelationship_AUD",
636 "HomotypicalGroup","HomotypicalGroup_AUD"};
637
638 int nSynonyms = service.count(Synonym.class);
639 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
640 int nNames = nameService.count(TaxonNameBase.class);
641 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
642
643 UUID uuidTaxon1=UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
644 UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
645 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
646
647
648 Taxon taxon2 = (Taxon)service.load(uuidTaxon2);
649
650 List<String> initStrat = new ArrayList<String>();
651 initStrat.add("markers");
652 Synonym synonym1 = (Synonym)service.load(uuidSynonym1, initStrat);
653
654 taxon2.removeSynonym(synonym1, false);
655 service.saveOrUpdate(taxon2);
656
657 commitAndStartNewTransaction(null);
658
659 nSynonyms = service.count(Synonym.class);
660 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
661 nNames = nameService.count(TaxonNameBase.class);
662 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
663 int nRelations = service.countAllRelationships();
664 Assert.assertEquals("There should be 1 relationship left in the database", 1, nRelations);
665
666 synonym1.addMarker(Marker.NewInstance(MarkerType.IMPORTED(), true));
667 synonym1.addMarker(Marker.NewInstance(MarkerType.COMPUTED(), true));
668 service.update(synonym1);
669 synonym1 =(Synonym) service.load(uuidSynonym1);
670
671
672 Set<Marker> markers = synonym1.getMarkers();
673 Marker marker = markers.iterator().next();
674 UUID markerUUID = marker.getUuid();
675 // taxon2 = (Taxon)service.load(uuidTaxon2);
676 synonym1 = (Synonym)service.load(uuidSynonym1);
677
678 service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
679
680 commitAndStartNewTransaction(tableNames);
681
682 nSynonyms = service.count(Synonym.class);
683 Assert.assertEquals("There should be 1 synonym left in the database", 1, nSynonyms);
684 nNames = nameService.count(TaxonNameBase.class);
685 Assert.assertEquals("There should be 3 names left in the database", 3, nNames);
686 nRelations = service.countAllRelationships();
687 Assert.assertEquals("There should be no relationship left in the database", 0, nRelations);
688 marker = markerService.load(markerUUID);
689 assertNull(marker);
690
691 }
692
693 @Test
694 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
695 //test delete synonym, only for a special taxon, but because of other relationships it will not be deleted at all
696 public final void testDeleteSynonymSynonymTaxonBooleanDeleteOneTaxon(){
697 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
698 "SynonymRelationship","SynonymRelationship_AUD",
699 "HomotypicalGroup","HomotypicalGroup_AUD"};
700
701
702 int nSynonyms = service.count(Synonym.class);
703 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
704 int nNames = nameService.count(TaxonNameBase.class);
705 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
706
707 UUID uuidTaxon1=UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
708 UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
709 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
710 UUID uuidSynonym2=UUID.fromString("f8d86dc9-5f18-4877-be46-fbb9412465e4");
711
712 Taxon taxon1 = (Taxon)service.load(uuidTaxon1);
713 Taxon taxon2 = (Taxon)service.load(uuidTaxon2);
714 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
715
716 service.deleteSynonym(synonym1, taxon1, new SynonymDeletionConfigurator());
717
718 this.commitAndStartNewTransaction(tableNames);
719
720 nSynonyms = service.count(Synonym.class);
721 Assert.assertEquals("There should still be 2 synonyms left in the database (synonym is related to taxon2)", 2, nSynonyms);
722 nNames = nameService.count(TaxonNameBase.class);
723 Assert.assertEquals("There should be 4 names left in the database (name not deleted as synonym was not deleted)", 4, nNames);
724 int nRelations = service.countAllRelationships();
725 Assert.assertEquals("There should be 1 relationship left in the database", 1, nRelations);
726
727
728 }
729
730 @Test
731 @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
732
733 public final void testDeleteSynonymSynonymTaxonBooleanWithRelatedName(){
734 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
735 "SynonymRelationship","SynonymRelationship_AUD",
736 "HomotypicalGroup","HomotypicalGroup_AUD"};
737
738 int nSynonyms = service.count(Synonym.class);
739 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
740 int nNames = nameService.count(TaxonNameBase.class);
741 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
742
743 UUID uuidTaxon1=UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
744 UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
745 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
746 UUID uuidSynonym2=UUID.fromString("f8d86dc9-5f18-4877-be46-fbb9412465e4");
747 UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
748
749 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
750 TaxonNameBase name2 = nameService.load(uuidSynonymName2);
751 UUID name3Uuid = synonym1.getName().getUuid();
752 TaxonNameBase name3 = nameService.load(name3Uuid);
753 name3.addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null);
754
755 service.saveOrUpdate(synonym1);
756
757 int nRelations = nameService.getAllRelationships(1000, 0).size();
758 logger.info("number of name relations: " + nRelations);
759 Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
760 SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
761
762 service.deleteSynonym(synonym1, config);
763
764 this.commitAndStartNewTransaction(tableNames);
765 //synonym is deleted, but the name can not be deleted because of a name relationship
766 nSynonyms = service.count(Synonym.class);
767 Assert.assertEquals("There should still be 1 synonyms left in the database", 1, nSynonyms);
768 nNames = nameService.count(TaxonNameBase.class);
769 Assert.assertEquals("There should be 4 names left in the database (name is related to synonymName2)", 4, nNames);
770 nRelations = service.countAllRelationships();
771 //may change with better implementation of countAllRelationships (see #2653)
772 nRelations = nameService.getAllRelationships(1000, 0).size();
773 logger.info("number of name relations: " + nRelations);
774 Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
775
776
777 //clean up database
778 name2 = nameService.load(uuidSynonymName2);
779 NameRelationship rel = CdmBase.deproxy(name2.getNameRelations().iterator().next(), NameRelationship.class);
780 name2.removeNameRelationship(rel);
781 nameService.save(name2);
782 this.setComplete();
783 this.endTransaction();
784
785 }
786 @Test
787 @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
788 public final void testDeleteSynonymSynonymTaxonBooleanWithRelatedNameDeleteAllNameRelations(){
789 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
790 "SynonymRelationship","SynonymRelationship_AUD",
791 "HomotypicalGroup","HomotypicalGroup_AUD"};
792
793 int nSynonyms = service.count(Synonym.class);
794 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
795 int nNames = nameService.count(TaxonNameBase.class);
796 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
797
798 UUID uuidTaxon1=UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
799 UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
800 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
801 UUID uuidSynonym2=UUID.fromString("f8d86dc9-5f18-4877-be46-fbb9412465e4");
802 UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
803
804 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
805 TaxonNameBase name2 = nameService.load(uuidSynonymName2);
806 UUID name3Uuid = synonym1.getName().getUuid();
807 TaxonNameBase name3 = nameService.load(name3Uuid);
808 name3.addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null);
809
810 service.saveOrUpdate(synonym1);
811
812 int nRelations = nameService.getAllRelationships(1000, 0).size();
813 logger.info("number of name relations: " + nRelations);
814 Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
815 SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
816 NameDeletionConfigurator nameDeletionConfig = new NameDeletionConfigurator();
817 nameDeletionConfig.setRemoveAllNameRelationships(true);
818 config.setNameDeletionConfig(nameDeletionConfig);
819
820 service.deleteSynonym(synonym1, config);
821
822 this.commitAndStartNewTransaction(tableNames);
823
824 nSynonyms = service.count(Synonym.class);
825 Assert.assertEquals("There should still be 1 synonyms left in the database", 1, nSynonyms);
826 nNames = nameService.count(TaxonNameBase.class);
827 Assert.assertEquals("There should be 3 names left in the database ", 3, nNames);
828 nRelations = service.countAllRelationships();
829 //may change with better implementation of countAllRelationships (see #2653)
830 nRelations = nameService.getAllRelationships(1000, 0).size();
831 logger.info("number of name relations: " + nRelations);
832 Assert.assertEquals("There should be no name relationship left in the database", 0, nRelations);
833 }
834
835 @Test
836 @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
837 public final void testDeleteSynonymSynonymTaxonBooleanWithRelatedNameIgnoreIsBasionym(){
838 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
839 "SynonymRelationship","SynonymRelationship_AUD",
840 "HomotypicalGroup","HomotypicalGroup_AUD"};
841
842 int nSynonyms = service.count(Synonym.class);
843 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
844 int nNames = nameService.count(TaxonNameBase.class);
845 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
846
847 UUID uuidTaxon1=UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
848 UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
849 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
850 UUID uuidSynonym2=UUID.fromString("f8d86dc9-5f18-4877-be46-fbb9412465e4");
851 UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
852
853 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
854 TaxonNameBase name2 = nameService.load(uuidSynonymName2);
855 UUID name3Uuid = synonym1.getName().getUuid();
856 TaxonNameBase name3 = nameService.load(name3Uuid);
857 name3.addRelationshipFromName(name2, NameRelationshipType.BASIONYM(), null);
858
859 service.saveOrUpdate(synonym1);
860
861 int nRelations = nameService.getAllRelationships(1000, 0).size();
862 logger.info("number of name relations: " + nRelations);
863 Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
864 SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
865 NameDeletionConfigurator nameDeletionConfig = new NameDeletionConfigurator();
866 nameDeletionConfig.setIgnoreIsBasionymFor(true);
867 config.setNameDeletionConfig(nameDeletionConfig);
868
869 service.deleteSynonym(synonym1, config);
870
871 this.commitAndStartNewTransaction(tableNames);
872
873 nSynonyms = service.count(Synonym.class);
874 Assert.assertEquals("There should still be 1 synonyms left in the database", 1, nSynonyms);
875 nNames = nameService.count(TaxonNameBase.class);
876 Assert.assertEquals("There should be 3 names left in the database ", 3, nNames);
877 nRelations = service.countAllRelationships();
878 //may change with better implementation of countAllRelationships (see #2653)
879 nRelations = nameService.getAllRelationships(1000, 0).size();
880 logger.info("number of name relations: " + nRelations);
881 Assert.assertEquals("There should be no name relationship left in the database", 0, nRelations);
882 }
883
884
885 @Test
886 @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
887 public final void testDeleteSynonymSynonymTaxonBooleanWithRollback(){
888 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
889 "SynonymRelationship","SynonymRelationship_AUD",
890 "HomotypicalGroup","HomotypicalGroup_AUD"};
891
892 int nSynonyms = service.count(Synonym.class);
893 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
894 int nNames = nameService.count(TaxonNameBase.class);
895 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
896 int nRelations = service.countAllRelationships();
897
898
899 //may change with better implementation of countAllRelationships (see #2653)
900
901 logger.debug("");
902 Assert.assertEquals("There should be 2 relationships in the database (the 2 synonym relationship) but no name relationship", 2, nRelations);
903
904 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
905 UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
906
907 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
908 TaxonNameBase name2 = nameService.load(uuidSynonymName2);
909 synonym1.getName().addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null);
910
911 service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
912
913 this.rollback();
914 // printDataSet(System.out, tableNames);
915 this.startNewTransaction();
916
917 nSynonyms = service.count(Synonym.class);
918 Assert.assertEquals("There should still be 2 synonyms left in the database", 2, nSynonyms);
919 nNames = nameService.count(TaxonNameBase.class);
920 Assert.assertEquals("There should be 4 names left in the database", 4, nNames);
921 nRelations = service.countAllRelationships();
922 //may change with better implementation of countAllRelationships (see #2653)
923 Assert.assertEquals("There should be 2 relationship in the database (the 2 synonym relationship) but no name relationship", 2, nRelations);
924
925 }
926
927 @Test
928 @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
929 public final void testDeleteSynonymSynonymTaxonBooleanWithoutTransaction(){
930 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
931 "SynonymRelationship","SynonymRelationship_AUD",
932 "HomotypicalGroup","HomotypicalGroup_AUD"};
933
934 int nSynonyms = service.count(Synonym.class);
935 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
936 int nNames = nameService.count(TaxonNameBase.class);
937 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
938 int nRelations = service.countAllRelationships();
939 //may change with better implementation of countAllRelationships (see #2653)
940 Assert.assertEquals("There should be 2 relationship in the database (the 2 synonym relationships) but no name relationship", 2, nRelations);
941
942 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
943 UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
944
945 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
946 TaxonNameBase name2 = nameService.load(uuidSynonymName2);
947 synonym1.getName().addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null);
948
949 service.saveOrUpdate(synonym1);
950
951 this.setComplete();
952 this.endTransaction();
953
954 // printDataSet(System.out, tableNames);
955
956 //out of wrapping transaction
957 service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
958
959 this.startNewTransaction();
960
961 nSynonyms = service.count(Synonym.class);
962 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);
963 nNames = nameService.count(TaxonNameBase.class);
964 Assert.assertEquals("There should be 4 names left in the database", 4, nNames);
965 nRelations = service.countAllRelationships();
966 Assert.assertEquals("There should be no taxon or synonym relationship in the database", 0, nRelations);
967 nRelations = nameService.getAllRelationships(1000,0).size();
968 Assert.assertEquals("There should be one name relationship in the database", 1, nRelations);
969
970 }
971
972 @Test
973 @DataSet("TaxonServiceImplTest.testInferredSynonyms.xml")
974 public void testCreateInferredSynonymy(){
975
976 UUID classificationUuid = UUID.fromString("aeee7448-5298-4991-b724-8d5b75a0a7a9");
977 Classification tree = classificationService.find(classificationUuid);
978 UUID taxonUuid = UUID.fromString("bc09aca6-06fd-4905-b1e7-cbf7cc65d783");
979 TaxonBase<?> taxonBase = service.find(taxonUuid);
980 List <TaxonBase> synonyms = service.list(Synonym.class, null, null, null, null);
981 assertEquals("Number of synonyms should be 2",2,synonyms.size());
982 Taxon taxon = (Taxon)taxonBase;
983
984 //synonyms = taxonDao.getAllSynonyms(null, null);
985 //assertEquals("Number of synonyms should be 2",2,synonyms.size());
986 List<Synonym> inferredSynonyms = service.createInferredSynonyms(taxon, tree, SynonymRelationshipType.INFERRED_EPITHET_OF(), true);
987 assertNotNull("there should be a new synonym ", inferredSynonyms);
988 assertEquals ("the name of inferred epithet should be SynGenus lachesis", "SynGenus lachesis sec. Sp. Pl.", inferredSynonyms.get(0).getTitleCache());
989
990 inferredSynonyms = service.createInferredSynonyms(taxon, tree, SynonymRelationshipType.INFERRED_GENUS_OF(), true);
991 assertNotNull("there should be a new synonym ", inferredSynonyms);
992 assertEquals ("the name of inferred epithet should be SynGenus lachesis", "Acherontia ciprosus sec. Sp. Pl.", inferredSynonyms.get(0).getTitleCache());
993
994 inferredSynonyms = service.createInferredSynonyms(taxon, tree, SynonymRelationshipType.POTENTIAL_COMBINATION_OF(), true);
995 assertNotNull("there should be a new synonym ", inferredSynonyms);
996 assertEquals ("the name of inferred epithet should be SynGenus lachesis", "SynGenus ciprosus sec. Sp. Pl.", inferredSynonyms.get(0).getTitleCache());
997 //assertTrue("set of synonyms should contain an inferred Synonym ", synonyms.contains(arg0))
998 }
999
1000 @Test
1001 @DataSet("BlankDataSet.xml")
1002 public final void testTaxonDeletionConfig(){
1003 final String[]tableNames = {
1004 "Classification", "Classification_AUD",
1005 "TaxonBase","TaxonBase_AUD",
1006 "TaxonNode","TaxonNode_AUD",
1007 "TaxonNameBase","TaxonNameBase_AUD",
1008 "SynonymRelationship","SynonymRelationship_AUD",
1009 "TaxonRelationship", "TaxonRelationship_AUD",
1010 "TaxonDescription", "TaxonDescription_AUD",
1011 "HomotypicalGroup","HomotypicalGroup_AUD",
1012 "PolytomousKey","PolytomousKey_AUD",
1013 "PolytomousKeyNode","PolytomousKeyNode_AUD",
1014 "Media","Media_AUD",
1015 "WorkingSet","WorkingSet_AUD",
1016 "DescriptionElementBase","DescriptionElementBase_AUD"};
1017
1018 UUID uuidParent=UUID.fromString("b5271d4f-e203-4577-941f-00d76fa9f4ca");
1019 UUID uuidChild1=UUID.fromString("326167f9-0b97-4e7d-b1bf-4ca47b82e21e");
1020 UUID uuidSameAs=UUID.fromString("c2bb0f01-f2dd-43fb-ba12-2a85727ccb8d");
1021 commitAndStartNewTransaction(tableNames);
1022 Taxon testTaxon = TaxonGenerator.getTestTaxon();
1023 service.save(testTaxon);
1024 commitAndStartNewTransaction(tableNames);
1025 int nTaxa = service.count(Taxon.class);
1026
1027 Assert.assertEquals("There should be 4 taxa in the database", 4, nTaxa);
1028 Taxon parent = (Taxon)service.find(TaxonGenerator.GENUS_UUID);
1029 Assert.assertNotNull("Parent taxon should exist", parent);
1030 Taxon child1 = (Taxon)service.find(TaxonGenerator.SPECIES1_UUID);
1031 Assert.assertNotNull("Child taxon should exist", child1);
1032 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1033 config.setDeleteTaxonNodes(false);
1034 config.setDeleteMisappliedNamesAndInvalidDesignations(false);
1035 try {
1036 //commitAndStartNewTransaction(tableNames);
1037
1038 service.deleteTaxon(child1, config, null);
1039 Assert.fail("Delete should throw an error as long as name is used in classification.");
1040 } catch (DataChangeNoRollbackException e) {
1041 if (e.getMessage().contains("Taxon can't be deleted as it is used in a classification node")){
1042 //ok
1043 commitAndStartNewTransaction(tableNames);
1044 }else{
1045 Assert.fail("Unexpected error occurred when trying to delete taxon: " + e.getMessage());
1046 }
1047 }
1048
1049 nTaxa = service.count(Taxon.class);
1050 Assert.assertEquals("There should be 4 taxa in the database", 4, nTaxa);
1051 child1 = (Taxon)service.find(TaxonGenerator.SPECIES1_UUID);
1052 Assert.assertNotNull("Child taxon should exist", child1);
1053 Assert.assertEquals("Child should belong to 1 node", 1, child1.getTaxonNodes().size());
1054
1055 TaxonNode node = child1.getTaxonNodes().iterator().next();
1056 TaxonNode parentNode = node.getParent();
1057 parentNode =CdmBase.deproxy(parentNode, TaxonNode.class);
1058 parentNode.deleteChildNode(node);
1059 nodeService.save(parentNode);
1060 commitAndStartNewTransaction(tableNames);
1061
1062 child1 = (Taxon)service.find(TaxonGenerator.SPECIES1_UUID);
1063
1064 assertEquals(0, child1.getTaxonNodes().size());
1065 try {
1066
1067 service.deleteTaxon(child1, config, null);
1068 } catch (DataChangeNoRollbackException e) {
1069 Assert.fail("Delete should not throw an exception anymore");
1070 }
1071 nTaxa = service.count(Taxon.class);
1072 Assert.assertEquals("There should be 3 taxa in the database", 3, nTaxa);
1073
1074 config.setDeleteTaxonNodes(true);
1075 Taxon child2 =(Taxon) service.find(TaxonGenerator.SPECIES2_UUID);
1076
1077 try {
1078 service.deleteTaxon(child2, config, null);
1079 } catch (DataChangeNoRollbackException e) {
1080 Assert.fail("Delete should not throw an exception");
1081 }
1082 //service.find(uuid);
1083
1084 nTaxa = service.count(Taxon.class);
1085 Assert.assertEquals("There should be 2 taxa in the database",2, nTaxa);
1086 // nNames = nameService.count(TaxonNameBase.class);
1087 // Assert.assertEquals("There should be 3 names left in the database", 3, nNames);
1088 // int nRelations = service.countAllRelationships();
1089 // Assert.assertEquals("There should be no relationship left in the database", 0, nRelations);
1090 }
1091
1092
1093 @Test
1094 @DataSet(value="BlankDataSet.xml")
1095 public final void testDeleteTaxon(){
1096
1097 //create a small classification
1098 Taxon testTaxon = TaxonGenerator.getTestTaxon();
1099
1100 UUID uuid = service.save(testTaxon);
1101
1102 Taxon speciesTaxon = (Taxon)service.find(TaxonGenerator.SPECIES1_UUID);
1103 Iterator<TaxonDescription> descriptionIterator = speciesTaxon.getDescriptions().iterator();
1104 UUID descrUUID = null;
1105 UUID descrElementUUID = null;
1106 if (descriptionIterator.hasNext()){
1107 TaxonDescription descr = descriptionIterator.next();
1108 descrUUID = descr.getUuid();
1109 descrElementUUID = descr.getElements().iterator().next().getUuid();
1110 }
1111 BotanicalName taxonName = (BotanicalName) nameService.find(TaxonGenerator.SPECIES1_NAME_UUID);
1112 assertNotNull(taxonName);
1113
1114 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1115 config.setDeleteNameIfPossible(false);
1116
1117
1118
1119 try {
1120 service.deleteTaxon(speciesTaxon, config, null);
1121 } catch (DataChangeNoRollbackException e) {
1122 e.printStackTrace();
1123 Assert.fail();
1124
1125 }
1126 commitAndStartNewTransaction(null);
1127
1128 taxonName = (BotanicalName) nameService.find(TaxonGenerator.SPECIES1_NAME_UUID);
1129 Taxon taxon = (Taxon)service.find(TaxonGenerator.SPECIES1_UUID);
1130
1131 //descriptionService.find(descrUUID);
1132 assertNull(descriptionService.find(descrUUID));
1133 assertNull(descriptionService.getDescriptionElementByUuid(descrElementUUID));
1134 //assertNull(synName);
1135 assertNotNull(taxonName);
1136 assertNull(taxon);
1137
1138 }
1139
1140 @Test
1141
1142 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="BlankDataSet.xml")
1143 public final void testDeleteTaxonDeleteSynonymRelations(){
1144
1145 final String[]tableNames = {
1146 "Classification", "Classification_AUD",
1147 "TaxonBase","TaxonBase_AUD",
1148 "TaxonNode","TaxonNode_AUD",
1149 "TaxonNameBase","TaxonNameBase_AUD"};
1150 commitAndStartNewTransaction(tableNames);
1151 //create a small classification
1152 Taxon testTaxon = TaxonGenerator.getTestTaxon();
1153
1154 UUID uuid = service.save(testTaxon);
1155
1156 Taxon speciesTaxon = (Taxon)service.find(TaxonGenerator.SPECIES2_UUID);
1157
1158 SynonymRelationship synRel = speciesTaxon.getSynonymRelations().iterator().next();
1159 UUID synonymRelationUuid = synRel.getUuid();
1160 UUID synonymUuid = synRel.getSynonym().getUuid();
1161 int i = service.getAllRelationships(1000, 0).size();
1162
1163 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1164 config.setDeleteSynonymsIfPossible(false);
1165
1166 try {
1167 service.deleteTaxon(speciesTaxon, config, null);
1168 } catch (DataChangeNoRollbackException e) {
1169 e.printStackTrace();
1170 Assert.fail();
1171
1172 }
1173 commitAndStartNewTransaction(null);
1174
1175 Taxon taxon = (Taxon)service.find(TaxonGenerator.SPECIES2_UUID);
1176 assertNull("The deleted taxon should no longer exist", taxon);
1177
1178 assertNotNull("The synonym should still exist since DeleteSynonymsIfPossible was false", service.find(synonymUuid));
1179
1180 for(RelationshipBase rel : service.getAllRelationships(1000, 0)){
1181 if(rel instanceof SynonymRelationship && rel.getUuid().equals(synonymRelationUuid)){
1182 Assert.fail("The SynonymRelationship should no longer exist");
1183 }
1184 }
1185 }
1186
1187
1188 @Test
1189 @DataSet(value="BlankDataSet.xml")
1190 public final void testDeleteTaxonNameUsedInOtherContext(){
1191
1192 //create a small classification
1193 Taxon testTaxon = TaxonGenerator.getTestTaxon();
1194
1195 UUID uuid = service.save(testTaxon);
1196
1197 Taxon speciesTaxon = (Taxon)service.find(TaxonGenerator.SPECIES1_UUID);
1198
1199 BotanicalName taxonName = (BotanicalName) nameService.find(TaxonGenerator.SPECIES1_NAME_UUID);
1200 assertNotNull(taxonName);
1201 BotanicalName fromName = BotanicalName.NewInstance(Rank.SPECIES());
1202 taxonName.addRelationshipFromName(fromName, NameRelationshipType.VALIDATED_BY_NAME(), null);
1203
1204 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1205 config.setDeleteNameIfPossible(true);
1206 try {
1207 service.deleteTaxon(speciesTaxon, config, null);
1208 } catch (DataChangeNoRollbackException e) {
1209
1210 Assert.fail();
1211 e.printStackTrace();
1212 }
1213 commitAndStartNewTransaction(null);
1214
1215 taxonName = (BotanicalName) nameService.find(TaxonGenerator.SPECIES1_NAME_UUID);
1216 Taxon taxon = (Taxon)service.find(TaxonGenerator.SPECIES1_UUID);
1217 //because of the namerelationship the name cannot be deleted
1218 assertNotNull(taxonName);
1219 assertNull(taxon);
1220
1221 }
1222
1223 @Test
1224 @DataSet(value="BlankDataSet.xml")
1225 public final void testDeleteTaxonNameUsedInTwoClassificationsDeleteAllNodes(){
1226 commitAndStartNewTransaction(null);
1227 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1228 //create a small classification
1229 Taxon testTaxon = TaxonGenerator.getTestTaxon();
1230
1231 UUID uuid = service.save(testTaxon);
1232 //BotanicalName name = nameService.find(uuid);
1233 Set<TaxonNode> nodes = testTaxon.getTaxonNodes();
1234 TaxonNode node = nodes.iterator().next();
1235 List<TaxonNode> childNodes = node.getChildNodes();
1236 TaxonNode childNode = childNodes.iterator().next();
1237 UUID childUUID = childNode.getTaxon().getUuid();
1238 Classification secondClassification = TaxonGenerator.getTestClassification("secondClassification");
1239
1240 secondClassification.addChildTaxon(testTaxon, null, null);
1241 //delete the taxon in all classifications
1242 try {
1243 service.deleteTaxon(testTaxon, config, null);
1244 } catch (DataChangeNoRollbackException e) {
1245 Assert.fail();
1246 }
1247 commitAndStartNewTransaction(null);
1248 Taxon tax = (Taxon)service.find(uuid);
1249 assertNull(tax);
1250 Taxon childTaxon = (Taxon)service.find(childUUID);
1251 assertNull(tax);
1252 commitAndStartNewTransaction(null);
1253
1254
1255
1256
1257
1258 }
1259
1260 @Test
1261 @DataSet(value="BlankDataSet.xml")
1262 public final void testDeleteTaxonNameUsedInTwoClassificationsDoNotDeleteAllNodes(){
1263 // delete the taxon only in second classification, this should delete only the nodes, not the taxa
1264 Taxon testTaxon = TaxonGenerator.getTestTaxon();
1265 UUID uuid = service.save(testTaxon);
1266 Classification secondClassification = TaxonGenerator.getTestClassification("secondClassification");
1267 Set<TaxonNode> nodes = testTaxon.getTaxonNodes();
1268 TaxonNode node = nodes.iterator().next();
1269 List<TaxonNode> childNodes = node.getChildNodes();
1270 TaxonNode childNode = childNodes.iterator().next();
1271 UUID childUUID = childNode.getTaxon().getUuid();
1272 childNode = secondClassification.addChildTaxon(testTaxon, null, null);
1273 UUID childNodeUUID = childNode.getUuid();
1274
1275 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1276 config.setDeleteInAllClassifications(false);
1277 try {
1278 service.deleteTaxon(testTaxon, config, secondClassification);
1279 Assert.fail("The taxon should not be deletable because it is used in a second classification and the configuration is set to deleteInAllClassifications = false");
1280 } catch (DataChangeNoRollbackException e) {
1281 logger.debug(e.getMessage());
1282 }
1283
1284 //commitAndStartNewTransaction(null);
1285 Taxon tax = (Taxon)service.find(uuid);
1286 assertNotNull(tax);
1287 Taxon childTaxon = (Taxon)service.find(childUUID);
1288 assertNotNull(tax);
1289 node = nodeService.find(childNodeUUID);
1290 assertNull(node);
1291 }
1292
1293 @Test
1294 @DataSet(value="BlankDataSet.xml")
1295 public final void testTaxonNodeDeletionConfiguratorMoveToParent(){
1296 //test childHandling MOVE_TO_PARENT:
1297 Taxon testTaxon = TaxonGenerator.getTestTaxon();
1298 UUID uuid = service.save(testTaxon);
1299
1300 Taxon topMost = Taxon.NewInstance(BotanicalName.NewInstance(Rank.FAMILY()), null);
1301
1302 Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1303 TaxonNode node =nodes.next();
1304 Classification classification = node.getClassification();
1305 classification.addParentChild(topMost, testTaxon, null, null);
1306 UUID topMostUUID = service.save(topMost);
1307
1308 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1309 config.getTaxonNodeConfig().setChildHandling(ChildHandling.MOVE_TO_PARENT);
1310
1311 try {
1312 service.deleteTaxon(testTaxon, config, null);
1313 } catch (DataChangeNoRollbackException e) {
1314 Assert.fail();
1315 }
1316
1317 commitAndStartNewTransaction(null);
1318 Taxon tax = (Taxon)service.find(uuid);
1319 assertNull(tax);
1320 tax = (Taxon)service.find(topMostUUID);
1321 Set<TaxonNode> topMostNodes = tax.getTaxonNodes();
1322 assertNotNull(topMostNodes);
1323 assertEquals("there should be one taxon node", 1, topMostNodes.size());
1324 nodes = topMostNodes.iterator();
1325 TaxonNode topMostNode = nodes.next();
1326 int size = topMostNode.getChildNodes().size();
1327
1328 assertEquals(2, size);
1329 }
1330
1331 @Test
1332 @DataSet(value="BlankDataSet.xml")
1333 public final void testTaxonNodeDeletionConfiguratorDeleteChildren(){
1334 //test childHandling DELETE:
1335 Taxon testTaxon = TaxonGenerator.getTestTaxon();
1336 UUID uuid = service.save(testTaxon);
1337
1338 Taxon topMost = Taxon.NewInstance(BotanicalName.NewInstance(Rank.FAMILY()), null);
1339
1340 Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1341 TaxonNode node =nodes.next();
1342 UUID taxonNodeUUID = node.getUuid();
1343 Classification classification = node.getClassification();
1344 classification.addParentChild(topMost, testTaxon, null, null);
1345 UUID topMostUUID = service.save(topMost);
1346
1347 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1348 config.getTaxonNodeConfig().setChildHandling(ChildHandling.DELETE);
1349
1350 try {
1351 service.deleteTaxon(testTaxon, config, null);
1352 } catch (DataChangeNoRollbackException e) {
1353 Assert.fail();
1354 }
1355
1356 commitAndStartNewTransaction(null);
1357 Taxon tax = (Taxon)service.find(uuid);
1358 assertNull(tax);
1359 tax = (Taxon)service.find(topMostUUID);
1360 Set<TaxonNode> topMostNodes = tax.getTaxonNodes();
1361 assertNotNull(topMostNodes);
1362 assertEquals("there should be one taxon node", 1, topMostNodes.size());
1363 nodes = topMostNodes.iterator();
1364 TaxonNode topMostNode = nodes.next();
1365 int size = topMostNode.getChildNodes().size();
1366 node = nodeService.find(taxonNodeUUID);
1367 assertNull(node);
1368 assertEquals(0, size);
1369 }
1370
1371
1372 @Test
1373 @DataSet(value="BlankDataSet.xml")
1374 public final void testTaxonDeletionConfiguratorDeleteMarker(){
1375 //test childHandling DELETE:
1376 Taxon testTaxon = TaxonGenerator.getTestTaxon();
1377 UUID uuid = service.save(testTaxon);
1378
1379 Taxon topMost = Taxon.NewInstance(BotanicalName.NewInstance(Rank.FAMILY()), null);
1380
1381 Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1382 TaxonNode node =nodes.next();
1383 Classification classification = node.getClassification();
1384 classification.addParentChild(topMost, testTaxon, null, null);
1385 UUID topMostUUID = service.save(topMost);
1386 Marker marker = Marker.NewInstance(testTaxon, true, MarkerType.IS_DOUBTFUL());
1387 testTaxon.addMarker(marker);
1388 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1389 config.getTaxonNodeConfig().setChildHandling(ChildHandling.DELETE);
1390
1391 try {
1392 service.deleteTaxon(testTaxon, config, null);
1393 } catch (DataChangeNoRollbackException e) {
1394 Assert.fail();
1395 }
1396
1397 commitAndStartNewTransaction(null);
1398 Taxon tax = (Taxon)service.find(uuid);
1399 assertNull(tax);
1400 tax = (Taxon)service.find(topMostUUID);
1401 Set<TaxonNode> topMostNodes = tax.getTaxonNodes();
1402 assertNotNull(topMostNodes);
1403 assertEquals("there should be one taxon node", 1, topMostNodes.size());
1404 nodes = topMostNodes.iterator();
1405 TaxonNode topMostNode = nodes.next();
1406 int size = topMostNode.getChildNodes().size();
1407
1408 assertEquals(0, size);
1409 }
1410
1411
1412 @Test
1413 @DataSet(value="BlankDataSet.xml")
1414 public final void testTaxonDeletionConfiguratorTaxonWithMisappliedName(){
1415
1416 Taxon testTaxon = TaxonGenerator.getTestTaxon();
1417 UUID uuid = service.save(testTaxon);
1418
1419 Taxon misappliedName = Taxon.NewInstance(BotanicalName.NewInstance(Rank.GENUS()), null);
1420
1421 Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1422 TaxonNode node =nodes.next();
1423 testTaxon.addMisappliedName(misappliedName, null, null);
1424 UUID misappliedNameUUID = service.save(misappliedName);
1425
1426 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1427 config.setDeleteMisappliedNamesAndInvalidDesignations(true);
1428
1429 try {
1430 service.deleteTaxon(testTaxon, config, null);
1431 } catch (DataChangeNoRollbackException e) {
1432 Assert.fail();
1433 }
1434
1435 commitAndStartNewTransaction(null);
1436 Taxon tax = (Taxon)service.find(uuid);
1437 assertNull(tax);
1438 tax = (Taxon)service.find(misappliedNameUUID);
1439 //TODO: is that correct or should it be deleted because there is no relation to anything
1440 assertNull(tax);
1441
1442 }
1443 @Test
1444 @DataSet(value="BlankDataSet.xml")
1445 public final void testTaxonDeletionConfiguratorTaxonWithMisappliedNameDoNotDelete(){
1446
1447 Taxon testTaxon = TaxonGenerator.getTestTaxon();
1448 UUID uuid = service.save(testTaxon);
1449
1450 Taxon misappliedName = Taxon.NewInstance(BotanicalName.NewInstance(Rank.GENUS()), null);
1451
1452 Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1453 TaxonNode node =nodes.next();
1454 testTaxon.addMisappliedName(misappliedName, null, null);
1455 UUID misappliedNameUUID = service.save(misappliedName);
1456
1457 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1458 config.setDeleteMisappliedNamesAndInvalidDesignations(false);
1459
1460 try {
1461 service.deleteTaxon(testTaxon, config, null);
1462 } catch (DataChangeNoRollbackException e) {
1463 Assert.fail();
1464 }
1465
1466 commitAndStartNewTransaction(null);
1467 Taxon tax = (Taxon)service.find(uuid);
1468 assertNull(tax);
1469 tax = (Taxon)service.find(misappliedNameUUID);
1470 //TODO: is that correct or should it be deleted because there is no relation to anything
1471 assertNotNull(tax);
1472
1473 }
1474
1475 @Test
1476 @DataSet(value="BlankDataSet.xml")
1477 public final void testTaxonDeletionConfiguratorTaxonMisappliedName(){
1478
1479 Taxon testTaxon = TaxonGenerator.getTestTaxon();
1480 UUID uuid = service.save(testTaxon);
1481
1482 Taxon misappliedNameTaxon = Taxon.NewInstance(BotanicalName.NewInstance(Rank.GENUS()), null);
1483
1484 Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1485 TaxonNode node =nodes.next();
1486 testTaxon.addMisappliedName(misappliedNameTaxon, null, null);
1487 UUID misappliedNameUUID = service.save(misappliedNameTaxon);
1488 misappliedNameTaxon = (Taxon)service.find(misappliedNameUUID);
1489 UUID misNameUUID = misappliedNameTaxon.getName().getUuid();
1490
1491 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1492
1493
1494 try {
1495 service.deleteTaxon(misappliedNameTaxon, config, null);
1496 } catch (DataChangeNoRollbackException e) {
1497 e.printStackTrace();
1498
1499 }
1500
1501 commitAndStartNewTransaction(null);
1502 Taxon tax = (Taxon)service.find(uuid);
1503 assertNotNull(tax);
1504 tax = (Taxon)service.find(misappliedNameUUID);
1505 BotanicalName name = (BotanicalName) nameService.find(misNameUUID);
1506
1507 assertNull(tax);
1508 assertNull(name);
1509
1510 }
1511
1512
1513 }
1514
1515
1516