fixing delete functionality and adding tests
[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.Iterator;
18 import java.util.List;
19 import java.util.Set;
20 import java.util.UUID;
21
22 import org.apache.log4j.Logger;
23 import org.junit.Assert;
24 import org.junit.Ignore;
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.exception.HomotypicalGroupChangeException;
33 import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
34 import eu.etaxonomy.cdm.datagenerator.TaxonGenerator;
35 import eu.etaxonomy.cdm.model.common.CdmBase;
36 import eu.etaxonomy.cdm.model.name.BotanicalName;
37 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
38 import eu.etaxonomy.cdm.model.name.NameRelationship;
39 import eu.etaxonomy.cdm.model.name.NameRelationshipType;
40 import eu.etaxonomy.cdm.model.name.NonViralName;
41 import eu.etaxonomy.cdm.model.name.Rank;
42 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
43 import eu.etaxonomy.cdm.model.reference.Reference;
44 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
45 import eu.etaxonomy.cdm.model.taxon.Classification;
46 import eu.etaxonomy.cdm.model.taxon.Synonym;
47 import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
48 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
49 import eu.etaxonomy.cdm.model.taxon.Taxon;
50 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
51 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
52 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
53 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
54 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
55 import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
56
57 /**
58 * @author a.mueller
59 *
60 */
61
62
63 public class TaxonServiceImplTest extends CdmTransactionalIntegrationTest {
64 @SuppressWarnings("unused")
65 private static final Logger logger = Logger.getLogger(TaxonServiceImplTest.class);
66
67 @SpringBeanByType
68 private ITaxonService service;
69
70 @SpringBeanByType
71 private INameService nameService;
72
73 @SpringBeanByType
74 private IReferenceService referenceService;
75
76 @SpringBeanByType
77 private IClassificationService classificationService;
78
79
80 /****************** TESTS *****************************/
81
82
83 /**
84 * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#getTaxonByUuid(java.util.UUID)}.
85 */
86 @Test
87 public final void testGetTaxonByUuid() {
88 Taxon expectedTaxon = Taxon.NewInstance(null, null);
89 UUID uuid = service.save(expectedTaxon);
90 TaxonBase<?> actualTaxon = service.find(uuid);
91 assertEquals(expectedTaxon, actualTaxon);
92 }
93
94 /**
95 * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#saveTaxon(eu.etaxonomy.cdm.model.taxon.TaxonBase)}.
96 */
97 @Test
98 public final void testSaveTaxon() {
99 Taxon expectedTaxon = Taxon.NewInstance(null, null);
100 UUID uuid = service.save(expectedTaxon);
101 TaxonBase<?> actualTaxon = service.find(uuid);
102 assertEquals(expectedTaxon, actualTaxon);
103 }
104
105 @Test
106 public final void testSaveOrUpdateTaxon() {
107 Taxon expectedTaxon = Taxon.NewInstance(null, null);
108 UUID uuid = service.save(expectedTaxon);
109 TaxonBase<?> actualTaxon = service.find(uuid);
110 assertEquals(expectedTaxon, actualTaxon);
111
112 actualTaxon.setName(BotanicalName.NewInstance(Rank.SPECIES()));
113 try{
114 service.saveOrUpdate(actualTaxon);
115 }catch(Exception e){
116 Assert.fail();
117 }
118 }
119 /**
120 * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#removeTaxon(eu.etaxonomy.cdm.model.taxon.TaxonBase)}.
121 */
122 @Test
123 public final void testRemoveTaxon() {
124 Taxon taxon = Taxon.NewInstance(BotanicalName.NewInstance(Rank.UNKNOWN_RANK()), null);
125 UUID uuid = service.save(taxon);
126 service.delete(taxon);
127 TaxonBase<?> actualTaxon = service.find(uuid);
128 assertNull(actualTaxon);
129 }
130
131
132 @Test
133 public final void testMakeTaxonSynonym() {
134 Rank rank = Rank.SPECIES();
135 Taxon tax1 = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test1", null, null, null, null, null, null, null), null);
136 Synonym synonym = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
137 tax1.addHomotypicSynonym(synonym, null, null);
138 UUID uuidTaxon = service.save(tax1);
139 UUID uuidSyn = service.save(synonym);
140
141 service.swapSynonymAndAcceptedTaxon(synonym, tax1);
142
143 // find forces flush
144 TaxonBase<?> tax = service.find(uuidTaxon);
145 TaxonBase<?> syn = service.find(uuidSyn);
146
147 assertTrue(tax.getName().getTitleCache().equals("Test2"));
148
149 HomotypicalGroup groupTest = tax.getHomotypicGroup();
150 HomotypicalGroup groupTest2 = syn.getHomotypicGroup();
151 assertEquals(groupTest, groupTest2);
152 }
153
154 @Test
155 public final void testChangeSynonymToAcceptedTaxon(){
156 Rank rank = Rank.SPECIES();
157 //HomotypicalGroup group = HomotypicalGroup.NewInstance();
158 Taxon taxWithoutSyn = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test1", null, null, null, null, null, null, null), null);
159 Taxon taxWithSyn = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test3", null, null, null, null, null, null, null), null);
160 Synonym synonym = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
161 Synonym synonym2 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test4", null, null, null, null, null, null, null), null);
162 synonym2.getName().setHomotypicalGroup(synonym.getHomotypicGroup());
163 //tax2.addHeterotypicSynonymName(synonym.getName());
164 taxWithSyn.addSynonym(synonym, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
165 taxWithSyn.addSynonym(synonym2, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
166
167 service.save(taxWithoutSyn);
168 UUID uuidSyn = service.save(synonym);
169 service.save(synonym2);
170 UUID uuidTaxWithSyn =service.save(taxWithSyn);
171
172 Taxon taxon = null;
173 try {
174 taxon = service.changeSynonymToAcceptedTaxon(synonym, taxWithSyn, true, true, null, null);
175 } catch (HomotypicalGroupChangeException e) {
176 Assert.fail("Invocation of change method should not throw an exception");
177 }
178 taxWithSyn = null;
179 //test flush (resave deleted object)
180 TaxonBase<?> syn = service.find(uuidSyn);
181 taxWithSyn = (Taxon)service.find(uuidTaxWithSyn);
182 Taxon taxNew = (Taxon)service.find(taxon.getUuid());
183 assertNull(syn);
184 assertNotNull(taxWithSyn);
185 assertNotNull(taxNew);
186
187 Assert.assertEquals("New taxon should have 1 synonym relationship (the old homotypic synonym)", 1, taxon.getSynonymRelations().size());
188 }
189
190
191 /**
192 * Old implementation taken from {@link TaxonServiceImplBusinessTest} for old version of method.
193 * 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)}.
194 */
195 @Test
196 public final void testMoveSynonymToAnotherTaxon_OLD() {
197 SynonymRelationshipType heteroTypicSynonymRelationshipType = SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF();
198 Reference<?> reference = ReferenceFactory.newGeneric();
199 String referenceDetail = "test";
200
201 NonViralName<?> t1n = NonViralName.NewInstance(null);
202 Taxon t1 = Taxon.NewInstance(t1n, reference);
203 NonViralName<?> t2n = NonViralName.NewInstance(null);
204 Taxon t2 = Taxon.NewInstance(t2n, reference);
205 NonViralName<?> s1n = NonViralName.NewInstance(null);
206 Synonym s1 = Synonym.NewInstance(s1n, reference);
207 t1.addSynonym(s1, heteroTypicSynonymRelationshipType);
208
209 SynonymRelationship synonymRelation = t1.getSynonymRelations().iterator().next();
210
211 boolean keepReference = false;
212 boolean moveHomotypicGroup = false;
213 try {
214 service.moveSynonymToAnotherTaxon(synonymRelation, t2, moveHomotypicGroup, heteroTypicSynonymRelationshipType, reference, referenceDetail, keepReference);
215 } catch (HomotypicalGroupChangeException e) {
216 Assert.fail("Method call should not throw exception");
217 }
218
219 Assert.assertTrue("t1 should have no synonym relationships", t1.getSynonymRelations().isEmpty());
220
221 Set<SynonymRelationship> synonymRelations = t2.getSynonymRelations();
222 Assert.assertTrue("t2 should have exactly one synonym relationship", synonymRelations.size() == 1);
223
224 synonymRelation = synonymRelations.iterator().next();
225
226 Assert.assertEquals(t2, synonymRelation.getAcceptedTaxon());
227 Assert.assertEquals(heteroTypicSynonymRelationshipType, synonymRelation.getType());
228 Assert.assertEquals(reference, synonymRelation.getCitation());
229 Assert.assertEquals(referenceDetail, synonymRelation.getCitationMicroReference());
230 }
231
232 @Test
233 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testMoveSynonymToAnotherTaxon.xml")
234 public final void testMoveSynonymToAnotherTaxon() throws Exception {
235 final String[] tableNames = new String[]{"SynonymRelationship"};
236
237 // printDataSet(System.err, new String[]{"AgentBase", "TaxonBase"});
238 // printDataSet(System.err, new String[]{"TaxonNode"});
239
240 UUID uuidNewTaxon = UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
241 UUID uuidOldTaxon = UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
242 UUID uuidSyn1 = UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
243 UUID uuidSyn3 = UUID.fromString("3fba2b22-22ae-4291-af67-faab748a5232");
244 UUID uuidSyn4 = UUID.fromString("f9b589c7-50cf-4df2-a52e-1b85eb7e4805");
245 UUID uuidSyn5 = UUID.fromString("fcc0bcf8-8bac-43bd-9508-1e97821587dd");
246 UUID uuidSyn6 = UUID.fromString("0ccd4e7c-6fbd-4b7c-bd47-29e45b92f34b");
247 UUID uuidRef1 = UUID.fromString("336f9b38-698c-45d7-be7b-993ed3355bdc");
248 UUID uuidRef2 = UUID.fromString("c8f49d1a-69e1-48a3-98bb-45d61f3da3e7");
249
250
251 boolean moveHomotypicGroup = true;
252 SynonymRelationshipType newSynonymRelationshipType = null;
253 boolean keepReference = true;
254 Reference<?> newReference = null;
255 String newReferenceDetail = null;
256
257 Taxon newTaxon = (Taxon)service.load(uuidNewTaxon);
258 Synonym homotypicSynonym = (Synonym)service.load(uuidSyn1);
259 Assert.assertNotNull("Synonym should exist", homotypicSynonym);
260 Assert.assertEquals("Synonym should have 1 relation", 1, homotypicSynonym.getSynonymRelations().size());
261 SynonymRelationship rel = homotypicSynonym.getSynonymRelations().iterator().next();
262 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, rel.getAcceptedTaxon().getUuid());
263 Taxon oldTaxon = rel.getAcceptedTaxon();
264
265 try {
266 service.moveSynonymToAnotherTaxon(rel, newTaxon, moveHomotypicGroup, newSynonymRelationshipType, newReference, newReferenceDetail, keepReference);
267 Assert.fail("Homotypic synonym move to other taxon should throw an exception");
268 } catch (HomotypicalGroupChangeException e) {
269 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")){
270 //OK
271 commitAndStartNewTransaction(tableNames);
272 }else{
273 Assert.fail("Unexpected exception occurred: " + e.getMessage());
274 }
275 }
276 //Asserts
277 homotypicSynonym = (Synonym)service.load(uuidSyn1);
278 Assert.assertNotNull("Synonym should still exist", homotypicSynonym);
279 Assert.assertEquals("Synonym should still have 1 relation", 1, homotypicSynonym.getSynonymRelations().size());
280 rel = homotypicSynonym.getSynonymRelations().iterator().next();
281 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", oldTaxon, rel.getAcceptedTaxon());
282
283 //test heterotypic synonym with other synonym in homotypic group
284 newTaxon = (Taxon)service.load(uuidNewTaxon);
285 Synonym heterotypicSynonym = (Synonym)service.load(uuidSyn3);
286 Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
287 Assert.assertEquals("Synonym should have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
288 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
289 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, rel.getAcceptedTaxon().getUuid());
290 oldTaxon = rel.getAcceptedTaxon();
291 moveHomotypicGroup = false;
292
293 try {
294 service.moveSynonymToAnotherTaxon(rel, newTaxon, moveHomotypicGroup, newSynonymRelationshipType, newReference, newReferenceDetail, keepReference);
295 Assert.fail("Heterotypic synonym move to other taxon should throw an exception");
296 } catch (HomotypicalGroupChangeException e) {
297 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")){
298 //OK
299 commitAndStartNewTransaction(tableNames);
300 }else{
301 Assert.fail("Unexpected exception occurred: " + e.getMessage());
302 }
303 }
304 //Asserts
305 heterotypicSynonym = (Synonym)service.load(uuidSyn3);
306 Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
307 Assert.assertEquals("Synonym should still have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
308 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
309 Assert.assertEquals("Accepted taxon of single relation should still be the old taxon", oldTaxon, rel.getAcceptedTaxon());
310
311
312 //test heterotypic synonym with no other synonym in homotypic group
313 //+ keep reference
314
315 // printDataSet(System.err, new String[]{"TaxonBase"});
316
317 newTaxon = (Taxon)service.load(uuidNewTaxon);
318 heterotypicSynonym = (Synonym)service.load(uuidSyn5);
319 Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
320 Assert.assertEquals("Synonym should have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
321 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
322 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, rel.getAcceptedTaxon().getUuid());
323 oldTaxon = rel.getAcceptedTaxon();
324 moveHomotypicGroup = false;
325
326
327 try {
328 service.moveSynonymToAnotherTaxon(rel, newTaxon, moveHomotypicGroup, newSynonymRelationshipType, newReference, newReferenceDetail, keepReference);
329 } catch (HomotypicalGroupChangeException e) {
330 Assert.fail("Move of single heterotypic synonym should not throw exception: " + e.getMessage());
331 }
332 //Asserts
333 //FIXME throws exception
334 commitAndStartNewTransaction(tableNames);
335
336 // printDataSet(System.err, new String[]{"AgentBase", "TaxonBase"});
337 //
338 // printDataSet(System.err, new String[]{"TaxonBase"});
339
340 heterotypicSynonym = (Synonym)service.load(uuidSyn5);
341
342 // printDataSet(System.err, new String[]{"TaxonBase"});
343 // System.exit(0);
344
345 Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
346 Assert.assertEquals("Synonym should still have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
347 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
348 Assert.assertEquals("Accepted taxon of single relation should be new taxon", newTaxon, rel.getAcceptedTaxon());
349 Assert.assertEquals("Old detail should be kept", "rel5", rel.getCitationMicroReference());
350
351
352 //test heterotypic synonym with other synonym in homotypic group and moveHomotypicGroup="true"
353 //+ new detail
354 newTaxon = (Taxon)service.load(uuidNewTaxon);
355 heterotypicSynonym = (Synonym)service.load(uuidSyn3);
356 Reference<?> ref1 = referenceService.load(uuidRef1);
357 Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
358 Assert.assertEquals("Synonym should have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
359 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
360 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, rel.getAcceptedTaxon().getUuid());
361 oldTaxon = rel.getAcceptedTaxon();
362 Assert.assertEquals("Detail should be ref1", ref1, rel.getCitation());
363 Assert.assertEquals("Detail should be 'rel3'", "rel3", rel.getCitationMicroReference());
364 TaxonNameBase<?,?> oldSynName3 = heterotypicSynonym.getName();
365
366 Synonym heterotypicSynonym4 = (Synonym)service.load(uuidSyn4);
367 Assert.assertNotNull("Synonym should exist", heterotypicSynonym4);
368 Assert.assertEquals("Synonym should have 1 relation", 1, heterotypicSynonym4.getSynonymRelations().size());
369 SynonymRelationship rel4 = heterotypicSynonym4.getSynonymRelations().iterator().next();
370 Assert.assertEquals("Accepted taxon of other synonym in group should be the old taxon", uuidOldTaxon, rel4.getAcceptedTaxon().getUuid());
371 Assert.assertSame("Homotypic group of both synonyms should be same", oldSynName3.getHomotypicalGroup() , heterotypicSynonym4.getName().getHomotypicalGroup() );
372
373 moveHomotypicGroup = true;
374 keepReference = false;
375
376 try {
377 service.moveSynonymToAnotherTaxon(rel, newTaxon, moveHomotypicGroup, newSynonymRelationshipType, newReference, newReferenceDetail, keepReference);
378 } catch (HomotypicalGroupChangeException e) {
379 Assert.fail("Move with 'moveHomotypicGroup = true' should not throw exception: " + e.getMessage());
380 }
381 //Asserts
382 commitAndStartNewTransaction(tableNames);
383 heterotypicSynonym = (Synonym)service.load(uuidSyn3);
384 Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
385 Assert.assertEquals("Synonym should still have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
386 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
387 Assert.assertEquals("Accepted taxon of relation should be new taxon now", newTaxon, rel.getAcceptedTaxon());
388 TaxonNameBase<?,?> synName3 = rel.getSynonym().getName();
389
390 heterotypicSynonym = (Synonym)service.load(uuidSyn4);
391 Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
392 Assert.assertEquals("Synonym should still have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
393 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
394 Assert.assertEquals("Accepted taxon of relation should be new taxon now", newTaxon, rel.getAcceptedTaxon());
395 Assert.assertNull("Old citation should be removed", rel.getCitation());
396 Assert.assertNull("Old detail should be removed", rel.getCitationMicroReference());
397 TaxonNameBase<?,?> synName4 = rel.getSynonym().getName();
398 Assert.assertEquals("Homotypic group of both synonyms should be equal", synName3.getHomotypicalGroup() , synName4.getHomotypicalGroup() );
399 Assert.assertSame("Homotypic group of both synonyms should be same", synName3.getHomotypicalGroup() , synName4.getHomotypicalGroup() );
400 Assert.assertEquals("Homotypic group of both synonyms should be equal to old homotypic group", oldSynName3.getHomotypicalGroup() , synName3.getHomotypicalGroup() );
401
402
403 //test single heterotypic synonym to homotypic synonym of new taxon
404 //+ new reference
405 newTaxon = (Taxon)service.load(uuidNewTaxon);
406 Reference<?> ref2 = (Reference<?>)referenceService.load(uuidRef2);
407 heterotypicSynonym = (Synonym)service.load(uuidSyn6);
408 Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
409 Assert.assertEquals("Synonym should have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
410 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
411 Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, rel.getAcceptedTaxon().getUuid());
412 oldTaxon = rel.getAcceptedTaxon();
413 moveHomotypicGroup = false;
414 keepReference = false;
415 newReference = ref2;
416 newReferenceDetail = "newRefDetail";
417 newSynonymRelationshipType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
418
419 try {
420 service.moveSynonymToAnotherTaxon(rel, newTaxon, moveHomotypicGroup, newSynonymRelationshipType, newReference, newReferenceDetail, keepReference);
421 } catch (HomotypicalGroupChangeException e) {
422 Assert.fail("Move of single heterotypic synonym should not throw exception: " + e.getMessage());
423 }
424 //Asserts
425 commitAndStartNewTransaction(tableNames);
426 heterotypicSynonym = (Synonym)service.load(uuidSyn6);
427 Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
428 Assert.assertEquals("Synonym should still have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
429 rel = heterotypicSynonym.getSynonymRelations().iterator().next();
430 Assert.assertEquals("Relationship type should be 'homotypic synonym'", newSynonymRelationshipType, rel.getType());
431 Assert.assertEquals("Accepted taxon of single relation should be new taxon", newTaxon, rel.getAcceptedTaxon());
432 Assert.assertEquals("New citation should be ref2", ref2 ,rel.getCitation());
433 Assert.assertEquals("New detail should be kept", "newRefDetail", rel.getCitationMicroReference());
434
435 Assert.assertEquals("New taxon and new synonym should have equal homotypical group", rel.getSynonym().getHomotypicGroup(), rel.getAcceptedTaxon().getHomotypicGroup());
436 Assert.assertSame("New taxon and new synonym should have same homotypical group", rel.getSynonym().getHomotypicGroup(), rel.getAcceptedTaxon().getHomotypicGroup());
437 }
438
439
440
441 @Test
442 public final void testGetHeterotypicSynonymyGroups(){
443 Rank rank = Rank.SPECIES();
444 Reference<?> ref1 = ReferenceFactory.newGeneric();
445 //HomotypicalGroup group = HomotypicalGroup.NewInstance();
446 Taxon taxon1 = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test3", null, null, null, null, null, null, null), null);
447 Synonym synonym0 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
448 Synonym synonym1 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
449 Synonym synonym2 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test4", null, null, null, null, null, null, null), null);
450 synonym0.getName().setHomotypicalGroup(taxon1.getHomotypicGroup());
451 synonym2.getName().setHomotypicalGroup(synonym1.getHomotypicGroup());
452 //tax2.addHeterotypicSynonymName(synonym.getName());
453 taxon1.addSynonym(synonym1, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
454 taxon1.addSynonym(synonym2, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
455
456 service.save(synonym1);
457 service.save(synonym2);
458 service.save(taxon1);
459
460 List<List<Synonym>> heteroSyns = service.getHeterotypicSynonymyGroups(taxon1, null);
461 Assert.assertEquals("There should be 1 heterotypic group", 1, heteroSyns.size());
462 List<Synonym> synList = heteroSyns.get(0);
463 Assert.assertEquals("There should be 2 heterotypic syns in group 1", 2, synList.size());
464
465 //test sec
466 synonym2.setSec(ref1);
467 heteroSyns = service.getHeterotypicSynonymyGroups(taxon1, null);
468 Assert.assertEquals("There should be 1 heterotypic group", 1, heteroSyns.size());
469 synList = heteroSyns.get(0);
470 Assert.assertEquals("getHeterotypicSynonymyGroups should be independent of sec reference", 2, synList.size());
471
472 }
473
474
475 @Test
476 public final void testGetHomotypicSynonymsByHomotypicGroup(){
477 Rank rank = Rank.SPECIES();
478 Reference<?> ref1 = ReferenceFactory.newGeneric();
479 //HomotypicalGroup group = HomotypicalGroup.NewInstance();
480 Taxon taxon1 = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test3", null, null, null, null, null, null, null), null);
481 Synonym synonym0 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
482 Synonym synonym1 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
483 Synonym synonym2 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test4", null, null, null, null, null, null, null), null);
484 synonym0.getName().setHomotypicalGroup(taxon1.getHomotypicGroup());
485 synonym2.getName().setHomotypicalGroup(synonym1.getHomotypicGroup());
486 //tax2.addHeterotypicSynonymName(synonym.getName());
487 taxon1.addSynonym(synonym0, SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF());
488 taxon1.addSynonym(synonym1, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
489 taxon1.addSynonym(synonym2, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
490
491 service.save(synonym1);
492 service.save(synonym2);
493 service.save(taxon1);
494
495 List<Synonym> homoSyns = service.getHomotypicSynonymsByHomotypicGroup(taxon1, null);
496 Assert.assertEquals("There should be 1 heterotypic group", 1, homoSyns.size());
497 Assert.assertSame("The homotypic synonym should be synonym0", synonym0, homoSyns.get(0));
498
499 //test sec
500 synonym0.setSec(ref1);
501 homoSyns = service.getHomotypicSynonymsByHomotypicGroup(taxon1, null);
502 Assert.assertEquals("getHeterotypicSynonymyGroups should be independent of sec reference", 1, homoSyns.size());
503
504 }
505
506 @Test
507 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
508 //test delete synonym, but the name will not be deleted
509 public final void testDeleteSynonymSynonymTaxonDontDeleteName(){
510 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
511 "SynonymRelationship","SynonymRelationship_AUD",
512 "HomotypicalGroup","HomotypicalGroup_AUD"};
513
514 int nSynonyms = service.count(Synonym.class);
515 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
516 int nNames = nameService.count(TaxonNameBase.class);
517 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
518
519
520 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
521
522
523 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
524 SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
525 config.setDeleteNameIfPossible(false);
526 config.setNewHomotypicGroupIfNeeded(true);
527 service.deleteSynonym(synonym1, config);
528
529 this.commitAndStartNewTransaction(tableNames);
530
531 nSynonyms = service.count(Synonym.class);
532 Assert.assertEquals("There should be 1 synonym left in the database", 1, nSynonyms);
533 nNames = nameService.count(TaxonNameBase.class);
534 Assert.assertEquals("There should be 4 names left in the database", 4, nNames);
535 int nRelations = service.countAllRelationships();
536 Assert.assertEquals("There should be no relationship left in the database", 0, nRelations);
537 }
538
539 @Test
540 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
541 //test delete synonym and his name
542 public final void testDeleteSynonymSynonymTaxonDeleteName(){
543 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
544 "SynonymRelationship","SynonymRelationship_AUD",
545 "HomotypicalGroup","HomotypicalGroup_AUD"};
546
547 int nSynonyms = service.count(Synonym.class);
548 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
549 int nNames = nameService.count(TaxonNameBase.class);
550 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
551 int nRelations = service.countAllRelationships();
552
553 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
554
555
556 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
557 service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
558
559 this.commitAndStartNewTransaction(tableNames);
560
561 nSynonyms = service.count(Synonym.class);
562 Assert.assertEquals("There should be 1 synonym left in the database", 1, nSynonyms);
563 nNames = nameService.count(TaxonNameBase.class);
564 Assert.assertEquals("There should be 3 names left in the database", 3, nNames);
565 nRelations = service.countAllRelationships();
566 Assert.assertEquals("There should be no relationship left in the database", 0, nRelations);
567 }
568
569 @Test
570 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
571 //test remove synonym from taxon -> synonym and name still in the db and the synonymrelationship to the other taxon
572 //test delete synonym -> all relationships are deleted, the name is deleted and the synonym itself
573 public final void testDeleteSynonymSynonymTaxonBooleanRelToOneTaxon(){
574 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
575 "SynonymRelationship","SynonymRelationship_AUD",
576 "HomotypicalGroup","HomotypicalGroup_AUD"};
577
578 int nSynonyms = service.count(Synonym.class);
579 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
580 int nNames = nameService.count(TaxonNameBase.class);
581 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
582
583 UUID uuidTaxon1=UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
584 UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
585 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
586
587
588 Taxon taxon2 = (Taxon)service.load(uuidTaxon2);
589 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
590
591 taxon2.removeSynonym(synonym1, false);
592 service.saveOrUpdate(taxon2);
593
594 commitAndStartNewTransaction(null);
595
596 nSynonyms = service.count(Synonym.class);
597 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
598 nNames = nameService.count(TaxonNameBase.class);
599 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
600 int nRelations = service.countAllRelationships();
601 Assert.assertEquals("There should be 1 relationship left in the database", 1, nRelations);
602
603 // taxon2 = (Taxon)service.load(uuidTaxon2);
604 synonym1 = (Synonym)service.load(uuidSynonym1);
605
606 service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
607
608 commitAndStartNewTransaction(tableNames);
609
610 nSynonyms = service.count(Synonym.class);
611 Assert.assertEquals("There should be 1 synonym left in the database", 1, nSynonyms);
612 nNames = nameService.count(TaxonNameBase.class);
613 Assert.assertEquals("There should be 3 names left in the database", 3, nNames);
614 nRelations = service.countAllRelationships();
615 Assert.assertEquals("There should be no relationship left in the database", 0, nRelations);
616
617 }
618
619 @Test
620 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
621 //test delete synonym, only for a special taxon, but because of other relationships it will not be deleted at all
622 public final void testDeleteSynonymSynonymTaxonBooleanDeleteOneTaxon(){
623 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
624 "SynonymRelationship","SynonymRelationship_AUD",
625 "HomotypicalGroup","HomotypicalGroup_AUD"};
626
627
628 int nSynonyms = service.count(Synonym.class);
629 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
630 int nNames = nameService.count(TaxonNameBase.class);
631 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
632
633 UUID uuidTaxon1=UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
634 UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
635 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
636 UUID uuidSynonym2=UUID.fromString("f8d86dc9-5f18-4877-be46-fbb9412465e4");
637
638 Taxon taxon1 = (Taxon)service.load(uuidTaxon1);
639 Taxon taxon2 = (Taxon)service.load(uuidTaxon2);
640 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
641
642 service.deleteSynonym(synonym1, taxon1, new SynonymDeletionConfigurator());
643
644 this.commitAndStartNewTransaction(tableNames);
645
646 nSynonyms = service.count(Synonym.class);
647 Assert.assertEquals("There should still be 2 synonyms left in the database (synonym is related to taxon2)", 2, nSynonyms);
648 nNames = nameService.count(TaxonNameBase.class);
649 Assert.assertEquals("There should be 4 names left in the database (name not deleted as synonym was not deleted)", 4, nNames);
650 int nRelations = service.countAllRelationships();
651 Assert.assertEquals("There should be 1 relationship left in the database", 1, nRelations);
652
653 }
654
655 @Test
656 @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
657
658 public final void testDeleteSynonymSynonymTaxonBooleanWithRelatedName(){
659 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
660 "SynonymRelationship","SynonymRelationship_AUD",
661 "HomotypicalGroup","HomotypicalGroup_AUD"};
662
663 int nSynonyms = service.count(Synonym.class);
664 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
665 int nNames = nameService.count(TaxonNameBase.class);
666 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
667
668 UUID uuidTaxon1=UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
669 UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
670 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
671 UUID uuidSynonym2=UUID.fromString("f8d86dc9-5f18-4877-be46-fbb9412465e4");
672 UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
673
674 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
675 TaxonNameBase name2 = (TaxonNameBase)nameService.load(uuidSynonymName2);
676 UUID name3Uuid = synonym1.getName().getUuid();
677 TaxonNameBase name3 = (TaxonNameBase)nameService.load(name3Uuid);
678 name3.addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null);
679
680 service.saveOrUpdate(synonym1);
681
682 int nRelations = nameService.getAllRelationships(1000, 0).size();
683 logger.info("number of name relations: " + nRelations);
684 Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
685 SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
686
687 service.deleteSynonym(synonym1, config);
688
689 this.commitAndStartNewTransaction(tableNames);
690 //synonym is deleted, but the name can not be deleted because of a name relationship
691 nSynonyms = service.count(Synonym.class);
692 Assert.assertEquals("There should still be 1 synonyms left in the database", 1, nSynonyms);
693 nNames = nameService.count(TaxonNameBase.class);
694 Assert.assertEquals("There should be 4 names left in the database (name is related to synonymName2)", 4, nNames);
695 nRelations = service.countAllRelationships();
696 //may change with better implementation of countAllRelationships (see #2653)
697 nRelations = nameService.getAllRelationships(1000, 0).size();
698 logger.info("number of name relations: " + nRelations);
699 Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
700
701
702 //clean up database
703 name2 = (TaxonNameBase)nameService.load(uuidSynonymName2);
704 NameRelationship rel = CdmBase.deproxy(name2.getNameRelations().iterator().next(), NameRelationship.class);
705 name2.removeNameRelationship(rel);
706 nameService.save(name2);
707 this.setComplete();
708 this.endTransaction();
709
710 }
711 @Test
712 @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
713 public final void testDeleteSynonymSynonymTaxonBooleanWithRelatedNameDeleteAllNameRelations(){
714 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
715 "SynonymRelationship","SynonymRelationship_AUD",
716 "HomotypicalGroup","HomotypicalGroup_AUD"};
717
718 int nSynonyms = service.count(Synonym.class);
719 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
720 int nNames = nameService.count(TaxonNameBase.class);
721 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
722
723 UUID uuidTaxon1=UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
724 UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
725 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
726 UUID uuidSynonym2=UUID.fromString("f8d86dc9-5f18-4877-be46-fbb9412465e4");
727 UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
728
729 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
730 TaxonNameBase name2 = (TaxonNameBase)nameService.load(uuidSynonymName2);
731 UUID name3Uuid = synonym1.getName().getUuid();
732 TaxonNameBase name3 = (TaxonNameBase)nameService.load(name3Uuid);
733 name3.addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null);
734
735 service.saveOrUpdate(synonym1);
736
737 int nRelations = nameService.getAllRelationships(1000, 0).size();
738 logger.info("number of name relations: " + nRelations);
739 Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
740 SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
741 NameDeletionConfigurator nameDeletionConfig = new NameDeletionConfigurator();
742 nameDeletionConfig.setRemoveAllNameRelationships(true);
743 config.setNameDeletionConfig(nameDeletionConfig);
744
745 service.deleteSynonym(synonym1, config);
746
747 this.commitAndStartNewTransaction(tableNames);
748
749 nSynonyms = service.count(Synonym.class);
750 Assert.assertEquals("There should still be 1 synonyms left in the database", 1, nSynonyms);
751 nNames = nameService.count(TaxonNameBase.class);
752 Assert.assertEquals("There should be 3 names left in the database ", 3, nNames);
753 nRelations = service.countAllRelationships();
754 //may change with better implementation of countAllRelationships (see #2653)
755 nRelations = nameService.getAllRelationships(1000, 0).size();
756 logger.info("number of name relations: " + nRelations);
757 Assert.assertEquals("There should be no name relationship left in the database", 0, nRelations);
758 }
759
760 @Test
761 @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
762 public final void testDeleteSynonymSynonymTaxonBooleanWithRelatedNameIgnoreIsBasionym(){
763 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
764 "SynonymRelationship","SynonymRelationship_AUD",
765 "HomotypicalGroup","HomotypicalGroup_AUD"};
766
767 int nSynonyms = service.count(Synonym.class);
768 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
769 int nNames = nameService.count(TaxonNameBase.class);
770 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
771
772 UUID uuidTaxon1=UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
773 UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
774 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
775 UUID uuidSynonym2=UUID.fromString("f8d86dc9-5f18-4877-be46-fbb9412465e4");
776 UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
777
778 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
779 TaxonNameBase name2 = (TaxonNameBase)nameService.load(uuidSynonymName2);
780 UUID name3Uuid = synonym1.getName().getUuid();
781 TaxonNameBase name3 = (TaxonNameBase)nameService.load(name3Uuid);
782 name3.addRelationshipFromName(name2, NameRelationshipType.BASIONYM(), null);
783
784 service.saveOrUpdate(synonym1);
785
786 int nRelations = nameService.getAllRelationships(1000, 0).size();
787 logger.info("number of name relations: " + nRelations);
788 Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
789 SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
790 NameDeletionConfigurator nameDeletionConfig = new NameDeletionConfigurator();
791 nameDeletionConfig.setIgnoreIsBasionymFor(true);
792 config.setNameDeletionConfig(nameDeletionConfig);
793
794 service.deleteSynonym(synonym1, config);
795
796 this.commitAndStartNewTransaction(tableNames);
797
798 nSynonyms = service.count(Synonym.class);
799 Assert.assertEquals("There should still be 1 synonyms left in the database", 1, nSynonyms);
800 nNames = nameService.count(TaxonNameBase.class);
801 Assert.assertEquals("There should be 3 names left in the database ", 3, nNames);
802 nRelations = service.countAllRelationships();
803 //may change with better implementation of countAllRelationships (see #2653)
804 nRelations = nameService.getAllRelationships(1000, 0).size();
805 logger.info("number of name relations: " + nRelations);
806 Assert.assertEquals("There should be no name relationship left in the database", 0, nRelations);
807 }
808
809
810 @Test
811 @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
812 public final void testDeleteSynonymSynonymTaxonBooleanWithRollback(){
813 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
814 "SynonymRelationship","SynonymRelationship_AUD",
815 "HomotypicalGroup","HomotypicalGroup_AUD"};
816
817 int nSynonyms = service.count(Synonym.class);
818 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
819 int nNames = nameService.count(TaxonNameBase.class);
820 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
821 int nRelations = service.countAllRelationships();
822
823
824 //may change with better implementation of countAllRelationships (see #2653)
825
826 logger.debug("");
827 Assert.assertEquals("There should be 2 relationships in the database (the 2 synonym relationship) but no name relationship", 2, nRelations);
828
829 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
830 UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
831
832 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
833 TaxonNameBase name2 = (TaxonNameBase)nameService.load(uuidSynonymName2);
834 synonym1.getName().addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null);
835
836 service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
837
838 this.rollback();
839 // printDataSet(System.out, tableNames);
840 this.startNewTransaction();
841
842 nSynonyms = service.count(Synonym.class);
843 Assert.assertEquals("There should still be 2 synonyms left in the database", 2, nSynonyms);
844 nNames = nameService.count(TaxonNameBase.class);
845 Assert.assertEquals("There should be 4 names left in the database", 4, nNames);
846 nRelations = service.countAllRelationships();
847 //may change with better implementation of countAllRelationships (see #2653)
848 Assert.assertEquals("There should be 2 relationship in the database (the 2 synonym relationship) but no name relationship", 2, nRelations);
849
850 }
851
852 @Test
853 @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
854 public final void testDeleteSynonymSynonymTaxonBooleanWithoutTransaction(){
855 final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
856 "SynonymRelationship","SynonymRelationship_AUD",
857 "HomotypicalGroup","HomotypicalGroup_AUD"};
858
859 int nSynonyms = service.count(Synonym.class);
860 Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
861 int nNames = nameService.count(TaxonNameBase.class);
862 Assert.assertEquals("There should be 4 names in the database", 4, nNames);
863 int nRelations = service.countAllRelationships();
864 //may change with better implementation of countAllRelationships (see #2653)
865 Assert.assertEquals("There should be 2 relationship in the database (the 2 synonym relationships) but no name relationship", 2, nRelations);
866
867 UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
868 UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
869
870 Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
871 TaxonNameBase name2 = (TaxonNameBase)nameService.load(uuidSynonymName2);
872 synonym1.getName().addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null);
873
874 service.saveOrUpdate(synonym1);
875
876 this.setComplete();
877 this.endTransaction();
878
879 // printDataSet(System.out, tableNames);
880
881 //out of wrapping transaction
882 service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
883
884 this.startNewTransaction();
885
886 nSynonyms = service.count(Synonym.class);
887 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);
888 nNames = nameService.count(TaxonNameBase.class);
889 Assert.assertEquals("There should be 4 names left in the database", 4, nNames);
890 nRelations = service.countAllRelationships();
891 Assert.assertEquals("There should be no taxon or synonym relationship in the database", 0, nRelations);
892 nRelations = nameService.getAllRelationships(1000,0).size();
893 Assert.assertEquals("There should be one name relationship in the database", 1, nRelations);
894
895 }
896
897 @Test
898 @DataSet("TaxonServiceImplTest.testInferredSynonyms.xml")
899 public void testCreateInferredSynonymy(){
900
901 UUID classificationUuid = UUID.fromString("aeee7448-5298-4991-b724-8d5b75a0a7a9");
902 Classification tree = classificationService.find(classificationUuid);
903 UUID taxonUuid = UUID.fromString("bc09aca6-06fd-4905-b1e7-cbf7cc65d783");
904 TaxonBase<?> taxonBase = service.find(taxonUuid);
905 List <TaxonBase> synonyms = service.list(Synonym.class, null, null, null, null);
906 assertEquals("Number of synonyms should be 2",2,synonyms.size());
907 Taxon taxon = (Taxon)taxonBase;
908
909 //synonyms = taxonDao.getAllSynonyms(null, null);
910 //assertEquals("Number of synonyms should be 2",2,synonyms.size());
911 List<Synonym> inferredSynonyms = service.createInferredSynonyms(taxon, tree, SynonymRelationshipType.INFERRED_EPITHET_OF(), true);
912 assertNotNull("there should be a new synonym ", inferredSynonyms);
913 // System.err.println(inferredSynonyms.size());
914 assertEquals ("the name of inferred epithet should be SynGenus lachesis", inferredSynonyms.get(0).getTitleCache(), "SynGenus lachesis sec. ");
915
916 inferredSynonyms = service.createInferredSynonyms(taxon, tree, SynonymRelationshipType.INFERRED_GENUS_OF(), true);
917 assertNotNull("there should be a new synonym ", inferredSynonyms);
918 // System.err.println(inferredSynonyms.get(0).getTitleCache());
919 assertEquals ("the name of inferred epithet should be SynGenus lachesis", inferredSynonyms.get(0).getTitleCache(), "Acherontia ciprosus sec. ");
920
921 inferredSynonyms = service.createInferredSynonyms(taxon, tree, SynonymRelationshipType.POTENTIAL_COMBINATION_OF(), true);
922 assertNotNull("there should be a new synonym ", inferredSynonyms);
923 assertEquals ("the name of inferred epithet should be SynGenus lachesis", inferredSynonyms.get(0).getTitleCache(), "SynGenus ciprosus sec. ");
924 //assertTrue("set of synonyms should contain an inferred Synonym ", synonyms.contains(arg0))
925 }
926
927 @Test
928 @DataSet("TaxonServiceImplTest.testDeleteTaxonConfig.xml")
929 @Ignore //not fully working yet
930 public final void testDeleteTaxonConfig(){
931 final String[]tableNames = {
932 "Classification", "Classification_AUD",
933 "TaxonBase","TaxonBase_AUD",
934 "TaxonNode","TaxonNode_AUD",
935 "TaxonNameBase","TaxonNameBase_AUD",
936 "SynonymRelationship","SynonymRelationship_AUD",
937 "TaxonRelationship", "TaxonRelationship_AUD",
938 "TaxonDescription", "TaxonDescription_AUD",
939 "HomotypicalGroup","HomotypicalGroup_AUD",
940 "PolytomousKey","PolytomousKey_AUD",
941 "PolytomousKeyNode","PolytomousKeyNode_AUD",
942 "Media","Media_AUD",
943 "WorkingSet","WorkingSet_AUD",
944 "DescriptionElementBase","DescriptionElementBase_AUD"};
945
946 UUID uuidParent=UUID.fromString("b5271d4f-e203-4577-941f-00d76fa9f4ca");
947 UUID uuidChild1=UUID.fromString("326167f9-0b97-4e7d-b1bf-4ca47b82e21e");
948 UUID uuidSameAs=UUID.fromString("c2bb0f01-f2dd-43fb-ba12-2a85727ccb8d");
949
950 int nTaxa = service.count(Taxon.class);
951 Assert.assertEquals("There should be 3 taxa in the database", 3, nTaxa);
952 Taxon parent = (Taxon)service.find(uuidParent);
953 Assert.assertNotNull("Parent taxon should exist", parent);
954 Taxon child1 = (Taxon)service.find(uuidChild1);
955 Assert.assertNotNull("Child taxon should exist", child1);
956 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
957 config.setDeleteTaxonNodes(false);
958
959 try {
960 // commitAndStartNewTransaction(tableNames);
961
962 service.deleteTaxon(child1, new TaxonDeletionConfigurator(), null);
963 Assert.fail("Delete should throw an error as long as name is used in classification.");
964 } catch (ReferencedObjectUndeletableException e) {
965 if (e.getMessage().contains("Taxon can't be deleted as it is used in a classification node")){
966 //ok
967 commitAndStartNewTransaction(tableNames);
968 }else{
969 Assert.fail("Unexpected error occurred when trying to delete taxon: " + e.getMessage());
970 }
971 }
972
973 nTaxa = service.count(Taxon.class);
974 Assert.assertEquals("There should be 3 taxa in the database", 3, nTaxa);
975 child1 = (Taxon)service.find(uuidChild1);
976 Assert.assertNotNull("Child taxon should exist", child1);
977 Assert.assertEquals("Child should belong to 1 node", 1, child1.getTaxonNodes().size());
978
979 TaxonNode node = child1.getTaxonNodes().iterator().next();
980 node.getParent().deleteChildNode(node);
981 service.save(node.getTaxon());
982 commitAndStartNewTransaction(tableNames);
983
984 child1 = (Taxon)service.find(uuidChild1);
985 try {
986
987 service.deleteTaxon(child1, config, null);
988 } catch (ReferencedObjectUndeletableException e) {
989 Assert.fail("Delete should not throw an exception anymore");
990 }
991
992
993 // nNames = nameService.count(TaxonNameBase.class);
994 // Assert.assertEquals("There should be 3 names left in the database", 3, nNames);
995 // int nRelations = service.countAllRelationships();
996 // Assert.assertEquals("There should be no relationship left in the database", 0, nRelations);
997 }
998
999
1000 @Test
1001 @DataSet(value="BlankDataSet.xml")
1002 public final void testDeleteTaxon(){
1003
1004 //create a small classification
1005 Taxon testTaxon = TaxonGenerator.getTestTaxon();
1006
1007 UUID uuid = service.save(testTaxon);
1008
1009 Taxon speciesTaxon = (Taxon)service.find(uuid);
1010 BotanicalName name = (BotanicalName)speciesTaxon.getName();
1011
1012 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1013 try {
1014 service.deleteTaxon(speciesTaxon, config, null);
1015 } catch (ReferencedObjectUndeletableException e) {
1016
1017 Assert.fail();
1018 e.printStackTrace();
1019 }
1020 commitAndStartNewTransaction(null);
1021
1022 BotanicalName taxonName = (BotanicalName) nameService.find(TaxonGenerator.GENUS_NAME_UUID);
1023 Taxon taxon = (Taxon)service.find(uuid);
1024
1025 //assertNull(synName);
1026 //assertNull(taxonName);
1027 assertNull(taxon);
1028
1029 }
1030
1031 @Test
1032 @DataSet(value="BlankDataSet.xml")
1033 public final void testDeleteTaxonNameUsedInTwoClassificationsDeleteAllNodes(){
1034 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1035 //create a small classification
1036 Taxon testTaxon = TaxonGenerator.getTestTaxon();
1037
1038 UUID uuid = service.save(testTaxon);
1039 //BotanicalName name = nameService.find(uuid);
1040 Set<TaxonNode> nodes = testTaxon.getTaxonNodes();
1041 TaxonNode node = nodes.iterator().next();
1042 Set<TaxonNode> childNodes = node.getChildNodes();
1043 TaxonNode childNode = childNodes.iterator().next();
1044 UUID childUUID = childNode.getTaxon().getUuid();
1045 Classification secondClassification = TaxonGenerator.getTestClassification("secondClassification");
1046
1047 secondClassification.addChildTaxon(testTaxon, null, null, null);
1048 //delete the taxon in all classifications
1049 try {
1050 service.deleteTaxon(testTaxon, config, null);
1051 } catch (ReferencedObjectUndeletableException e) {
1052 // TODO Auto-generated catch block
1053 e.printStackTrace();
1054 }
1055 commitAndStartNewTransaction(null);
1056 Taxon tax = (Taxon)service.find(uuid);
1057 assertNull(tax);
1058 Taxon childTaxon = (Taxon)service.find(childUUID);
1059 assertNull(tax);
1060
1061
1062
1063
1064
1065
1066 }
1067
1068 @Test
1069 @DataSet(value="BlankDataSet.xml")
1070 public final void testDeleteTaxonNameUsedInTwoClassificationsDoNotDeleteAllNodes(){
1071 // delete the taxon only in second classification, this should delete only the nodes, not the taxa
1072 Taxon testTaxon = TaxonGenerator.getTestTaxon();
1073 UUID uuid = service.save(testTaxon);
1074 Classification secondClassification = TaxonGenerator.getTestClassification("secondClassification");
1075 Set<TaxonNode> nodes = testTaxon.getTaxonNodes();
1076 TaxonNode node = nodes.iterator().next();
1077 Set<TaxonNode> childNodes = node.getChildNodes();
1078 TaxonNode childNode = childNodes.iterator().next();
1079 UUID childUUID = childNode.getTaxon().getUuid();
1080 secondClassification.addChildTaxon(testTaxon, null, null, null);
1081
1082 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1083 config.getTaxonNodeConfig().setDeleteInAllClassifications(false);
1084 try {
1085 service.deleteTaxon(testTaxon, config, secondClassification);
1086 } catch (ReferencedObjectUndeletableException e) {
1087 // TODO Auto-generated catch block
1088 e.printStackTrace();
1089 }
1090
1091 commitAndStartNewTransaction(null);
1092 Taxon tax = (Taxon)service.find(uuid);
1093 assertNotNull(tax);
1094 Taxon childTaxon = (Taxon)service.find(childUUID);
1095 assertNotNull(tax);
1096 }
1097
1098
1099 }