Project

General

Profile

Download (84.3 KB) Statistics
| Branch: | Tag: | Revision:
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.io.FileNotFoundException;
18
import java.util.ArrayList;
19
import java.util.Iterator;
20
import java.util.List;
21
import java.util.Set;
22
import java.util.UUID;
23

    
24
import org.apache.log4j.Logger;
25
import org.junit.Assert;
26
import org.junit.Test;
27
import org.unitils.dbunit.annotation.DataSet;
28
import org.unitils.spring.annotation.SpringBeanByType;
29

    
30
import eu.etaxonomy.cdm.api.service.config.IncludedTaxonConfiguration;
31
import eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator;
32
import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
33
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
34
import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator.ChildHandling;
35
import eu.etaxonomy.cdm.api.service.dto.IncludedTaxaDTO;
36
import eu.etaxonomy.cdm.api.service.exception.HomotypicalGroupChangeException;
37
import eu.etaxonomy.cdm.datagenerator.TaxonGenerator;
38
import eu.etaxonomy.cdm.model.common.CdmBase;
39
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
40
import eu.etaxonomy.cdm.model.common.Marker;
41
import eu.etaxonomy.cdm.model.common.MarkerType;
42
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
43
import eu.etaxonomy.cdm.model.common.RelationshipBase;
44
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
45
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
46
import eu.etaxonomy.cdm.model.description.TaxonDescription;
47
import eu.etaxonomy.cdm.model.name.BotanicalName;
48
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
49
import eu.etaxonomy.cdm.model.name.NameRelationship;
50
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
51
import eu.etaxonomy.cdm.model.name.NonViralName;
52
import eu.etaxonomy.cdm.model.name.Rank;
53
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
54
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
55
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
56
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
57
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
58
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
59
import eu.etaxonomy.cdm.model.reference.Reference;
60
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
61
import eu.etaxonomy.cdm.model.taxon.Classification;
62
import eu.etaxonomy.cdm.model.taxon.Synonym;
63
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
64
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
65
import eu.etaxonomy.cdm.model.taxon.Taxon;
66
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
67
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
68
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
69
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
70
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
71
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
72
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
73

    
74
/**
75
 * @author a.mueller
76
 */
77

    
78

    
79
public class TaxonServiceImplTest extends CdmTransactionalIntegrationTest {
80
    private static final Logger logger = Logger.getLogger(TaxonServiceImplTest.class);
81

    
82
    @SpringBeanByType
83
    private ITaxonService service;
84

    
85
    @SpringBeanByType
86
    private INameService nameService;
87

    
88
    @SpringBeanByType
89
    private IReferenceService referenceService;
90

    
91
    @SpringBeanByType
92
    private IClassificationService classificationService;
93

    
94
    @SpringBeanByType
95
    private ITaxonNodeService nodeService;
96

    
97
    @SpringBeanByType
98
    private IDescriptionService descriptionService;
99

    
100
    @SpringBeanByType
101
    private IMarkerService markerService;
102

    
103
    @SpringBeanByType
104
    private IEventBaseService eventService;
105

    
106
    @SpringBeanByType
107
    private IOccurrenceService occurenceService;
108

    
109
    private Synonym synonym;
110
    private Synonym synonym2;
111

    
112
    private Taxon taxWithSyn;
113
    private Taxon tax2WithSyn;
114
    private Taxon taxWithoutSyn;
115
    private UUID uuidSyn;
116
    private UUID uuidTaxWithoutSyn;
117
    private UUID uuidSyn2;
118
    private UUID uuidTaxWithSyn;
119

    
120
/****************** TESTS *****************************/
121

    
122

    
123
    /**
124
     * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#getTaxonByUuid(java.util.UUID)}.
125
     */
126
    @Test
127
    public final void testGetTaxonByUuid() {
128
        Taxon expectedTaxon = Taxon.NewInstance(null, null);
129
        UUID uuid = service.save(expectedTaxon).getUuid();
130
        TaxonBase<?> actualTaxon = service.find(uuid);
131
        assertEquals(expectedTaxon, actualTaxon);
132
    }
133

    
134
    /**
135
     * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#saveTaxon(eu.etaxonomy.cdm.model.taxon.TaxonBase)}.
136
     */
137
    @Test
138
    public final void testSaveTaxon() {
139
        Taxon expectedTaxon = Taxon.NewInstance(null, null);
140
        UUID uuid = service.save(expectedTaxon).getUuid();
141
        TaxonBase<?> actualTaxon = service.find(uuid);
142
        assertEquals(expectedTaxon, actualTaxon);
143
    }
144

    
145
    @Test
146
    public final void testSaveOrUpdateTaxon() {
147
        Taxon expectedTaxon = Taxon.NewInstance(null, null);
148
        UUID uuid = service.save(expectedTaxon).getUuid();
149
        TaxonBase<?> actualTaxon = service.find(uuid);
150
        assertEquals(expectedTaxon, actualTaxon);
151

    
152
        actualTaxon.setName(BotanicalName.NewInstance(Rank.SPECIES()));
153
        try{
154
            service.saveOrUpdate(actualTaxon);
155
        }catch(Exception e){
156
            Assert.fail();
157
        }
158
    }
159
    /**
160
     * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#removeTaxon(eu.etaxonomy.cdm.model.taxon.TaxonBase)}.
161
     */
162
    @Test
163
    public final void testRemoveTaxon() {
164
        Taxon taxon = Taxon.NewInstance(BotanicalName.NewInstance(Rank.UNKNOWN_RANK()), null);
165
        UUID uuid = service.save(taxon).getUuid();
166
       // try {
167
			service.deleteTaxon(taxon.getUuid(), null, null);
168
		/*} catch (DataChangeNoRollbackException e) {
169
			// TODO Auto-generated catch block
170
			e.printStackTrace();
171
		}*/
172
        TaxonBase<?> actualTaxon = service.find(uuid);
173
        assertNull(actualTaxon);
174
    }
175

    
176

    
177
    @Test
178
    public final void testMakeTaxonSynonym() {
179
        try {
180
			createTestDataSet();
181
		} catch (FileNotFoundException e) {
182
			// TODO Auto-generated catch block
183
			e.printStackTrace();
184
		}
185

    
186
        service.swapSynonymAndAcceptedTaxon(synonym, taxWithSyn);
187

    
188
        // find forces flush
189
        Taxon tax = (Taxon)service.find(uuidTaxWithSyn);
190
        tax.removeSynonym(synonym);
191
        tax.addHomotypicSynonym(synonym, null, null);
192
        service.saveOrUpdate(tax);
193
        TaxonBase<?> syn = service.find(uuidSyn);
194

    
195
        assertTrue(tax.getName().getTitleCache().equals("Test2"));
196

    
197
        HomotypicalGroup groupTest = tax.getHomotypicGroup();
198
        HomotypicalGroup groupTest2 = syn.getHomotypicGroup();
199
        assertEquals(groupTest, groupTest2);
200

    
201
    }
202

    
203
    @Test
204
    public final void testChangeSynonymToAcceptedTaxon(){
205
    	try {
206
			createTestDataSet();
207
		} catch (FileNotFoundException e1) {
208
			// TODO Auto-generated catch block
209
			e1.printStackTrace();
210
		}
211

    
212

    
213
        Taxon taxon = null;
214
        try {
215
            taxon = service.changeSynonymToAcceptedTaxon(synonym, taxWithSyn, true, true, null, null);
216
        } catch (HomotypicalGroupChangeException e) {
217
            Assert.fail("Invocation of change method should not throw an exception");
218
        }
219
        taxWithSyn = null;
220
        //test flush (resave deleted object)
221
        TaxonBase<?> syn = service.find(uuidSyn);
222
        taxWithSyn = (Taxon)service.find(uuidTaxWithSyn);
223
        Taxon taxNew = (Taxon)service.find(taxon.getUuid());
224
        assertNull(syn);
225
        assertNotNull(taxWithSyn);
226
        assertNotNull(taxNew);
227

    
228
        Assert.assertEquals("New taxon should have 1 synonym relationship (the old homotypic synonym)", 1, taxon.getSynonymRelations().size());
229
    }
230

    
231

    
232

    
233
    @Test
234
    public final void testChangeSynonymToAcceptedTaxonSynonymForTwoTaxa(){
235
        try {
236
			createTestDataSet();
237
		} catch (FileNotFoundException e1) {
238
			// TODO Auto-generated catch block
239
			e1.printStackTrace();
240
		}
241

    
242

    
243
        Taxon taxon = null;
244
        try {
245
            taxon = service.changeSynonymToAcceptedTaxon(synonym, taxWithSyn, true, true, null, null);
246
            service.save(taxon);
247
        } catch (HomotypicalGroupChangeException e) {
248
            Assert.fail("Invocation of change method should not throw an exception");
249
        }
250
        taxWithSyn = null;
251
        tax2WithSyn = null;
252

    
253
        //test flush (resave deleted object)
254
        TaxonBase<?> syn = service.find(uuidSyn);
255
        taxWithSyn = (Taxon)service.find(uuidTaxWithSyn);
256
        Taxon taxNew = (Taxon)service.find(taxon.getUuid());
257
        assertNull(syn);
258
        assertNotNull(taxWithSyn);
259
        assertNotNull(taxNew);
260

    
261
       // Assert.assertEquals("New taxon should have 1 synonym relationship (the old homotypic synonym)", 1, taxon.getSynonymRelations().size());
262
    }
263

    
264
    /**
265
     * Old implementation taken from {@link TaxonServiceImplBusinessTest} for old version of method.
266
     * 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)}.
267
     */
268
    @Test
269
    public final void testMoveSynonymToAnotherTaxon_OLD() {
270
        SynonymRelationshipType heteroTypicSynonymRelationshipType = SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF();
271
        Reference<?> reference = ReferenceFactory.newGeneric();
272
        String referenceDetail = "test";
273

    
274
        NonViralName<?> t1n = NonViralName.NewInstance(null);
275
        Taxon t1 = Taxon.NewInstance(t1n, reference);
276
        NonViralName<?> t2n = NonViralName.NewInstance(null);
277
        Taxon t2 = Taxon.NewInstance(t2n, reference);
278
        NonViralName<?> s1n = NonViralName.NewInstance(null);
279
        Synonym s1 = Synonym.NewInstance(s1n, reference);
280
        t1.addSynonym(s1, heteroTypicSynonymRelationshipType);
281

    
282
        SynonymRelationship synonymRelation = t1.getSynonymRelations().iterator().next();
283

    
284
        boolean keepReference = false;
285
        boolean moveHomotypicGroup = false;
286
        try {
287
            service.moveSynonymToAnotherTaxon(synonymRelation, t2, moveHomotypicGroup, heteroTypicSynonymRelationshipType, reference, referenceDetail, keepReference);
288
        } catch (HomotypicalGroupChangeException e) {
289
            Assert.fail("Method call should not throw exception");
290
        }
291

    
292
        Assert.assertTrue("t1 should have no synonym relationships", t1.getSynonymRelations().isEmpty());
293

    
294
        Set<SynonymRelationship> synonymRelations = t2.getSynonymRelations();
295
        Assert.assertTrue("t2 should have exactly one synonym relationship", synonymRelations.size() == 1);
296

    
297
        synonymRelation = synonymRelations.iterator().next();
298

    
299
        Assert.assertEquals(t2, synonymRelation.getAcceptedTaxon());
300
        Assert.assertEquals(heteroTypicSynonymRelationshipType, synonymRelation.getType());
301
        Assert.assertEquals(reference, synonymRelation.getCitation());
302
        Assert.assertEquals(referenceDetail, synonymRelation.getCitationMicroReference());
303
    }
304

    
305
    @Test
306
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testMoveSynonymToAnotherTaxon.xml")
307
    public final void testMoveSynonymToAnotherTaxon() throws Exception {
308
        final String[] tableNames = new String[]{"SynonymRelationship"};
309

    
310
//        printDataSet(System.err, new String[]{"AgentBase", "TaxonBase"});
311
//        printDataSet(System.err, new String[]{"TaxonNode"});
312

    
313
        UUID uuidNewTaxon = UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
314
        UUID uuidOldTaxon = UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
315
        UUID uuidSyn1 = UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
316
        UUID uuidSyn3 = UUID.fromString("3fba2b22-22ae-4291-af67-faab748a5232");
317
        UUID uuidSyn4 = UUID.fromString("f9b589c7-50cf-4df2-a52e-1b85eb7e4805");
318
        UUID uuidSyn5 = UUID.fromString("fcc0bcf8-8bac-43bd-9508-1e97821587dd");
319
        UUID uuidSyn6 = UUID.fromString("0ccd4e7c-6fbd-4b7c-bd47-29e45b92f34b");
320
        UUID uuidRef1 = UUID.fromString("336f9b38-698c-45d7-be7b-993ed3355bdc");
321
        UUID uuidRef2 = UUID.fromString("c8f49d1a-69e1-48a3-98bb-45d61f3da3e7");
322

    
323

    
324
        boolean moveHomotypicGroup = true;
325
        SynonymRelationshipType newSynonymRelationshipType = null;
326
        boolean keepReference = true;
327
        Reference<?> newReference = null;
328
        String newReferenceDetail = null;
329

    
330
        Taxon newTaxon = (Taxon)service.load(uuidNewTaxon);
331
        Synonym homotypicSynonym = (Synonym)service.load(uuidSyn1);
332
        Assert.assertNotNull("Synonym should exist", homotypicSynonym);
333
        Assert.assertEquals("Synonym should have 1 relation", 1, homotypicSynonym.getSynonymRelations().size());
334
        SynonymRelationship rel = homotypicSynonym.getSynonymRelations().iterator().next();
335
        Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, rel.getAcceptedTaxon().getUuid());
336
        Taxon oldTaxon = rel.getAcceptedTaxon();
337

    
338
        try {
339
            service.moveSynonymToAnotherTaxon(rel, newTaxon, moveHomotypicGroup, newSynonymRelationshipType, newReference, newReferenceDetail, keepReference);
340
            Assert.fail("Homotypic synonym move to other taxon should throw an exception");
341
        } catch (HomotypicalGroupChangeException e) {
342
            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")){
343
                //OK
344
                commitAndStartNewTransaction(tableNames);
345
            }else{
346
                Assert.fail("Unexpected exception occurred: " + e.getMessage());
347
            }
348
        }
349
        //Asserts
350
        homotypicSynonym = (Synonym)service.load(uuidSyn1);
351
        Assert.assertNotNull("Synonym should still exist", homotypicSynonym);
352
        Assert.assertEquals("Synonym should still have 1 relation", 1, homotypicSynonym.getSynonymRelations().size());
353
        rel = homotypicSynonym.getSynonymRelations().iterator().next();
354
        Assert.assertEquals("Accepted taxon of single relation should be the old taxon", oldTaxon, rel.getAcceptedTaxon());
355

    
356
        //test heterotypic synonym with other synonym in homotypic group
357
        newTaxon = (Taxon)service.load(uuidNewTaxon);
358
        Synonym heterotypicSynonym = (Synonym)service.load(uuidSyn3);
359
        Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
360
        Assert.assertEquals("Synonym should have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
361
        rel = heterotypicSynonym.getSynonymRelations().iterator().next();
362
        Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, rel.getAcceptedTaxon().getUuid());
363
        oldTaxon = rel.getAcceptedTaxon();
364
        moveHomotypicGroup = false;
365

    
366
        try {
367
            service.moveSynonymToAnotherTaxon(rel, newTaxon, moveHomotypicGroup, newSynonymRelationshipType, newReference, newReferenceDetail, keepReference);
368
            Assert.fail("Heterotypic synonym move to other taxon should throw an exception");
369
        } catch (HomotypicalGroupChangeException e) {
370
            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")){
371
                //OK
372
                commitAndStartNewTransaction(tableNames);
373
            }else{
374
                Assert.fail("Unexpected exception occurred: " + e.getMessage());
375
            }
376
        }
377
        //Asserts
378
        heterotypicSynonym = (Synonym)service.load(uuidSyn3);
379
        Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
380
        Assert.assertEquals("Synonym should still have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
381
        rel = heterotypicSynonym.getSynonymRelations().iterator().next();
382
        Assert.assertEquals("Accepted taxon of single relation should still be the old taxon", oldTaxon, rel.getAcceptedTaxon());
383

    
384

    
385
        //test heterotypic synonym with no other synonym in homotypic group
386
        //+ keep reference
387

    
388
//        printDataSet(System.err, new String[]{"TaxonBase"});
389

    
390
        newTaxon = (Taxon)service.load(uuidNewTaxon);
391
        heterotypicSynonym = (Synonym)service.load(uuidSyn5);
392
        Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
393
        Assert.assertEquals("Synonym should have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
394
        rel = heterotypicSynonym.getSynonymRelations().iterator().next();
395
        Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, rel.getAcceptedTaxon().getUuid());
396
        oldTaxon = rel.getAcceptedTaxon();
397
        moveHomotypicGroup = false;
398

    
399

    
400
        try {
401
            service.moveSynonymToAnotherTaxon(rel, newTaxon, moveHomotypicGroup, newSynonymRelationshipType, newReference, newReferenceDetail, keepReference);
402
        } catch (HomotypicalGroupChangeException e) {
403
            Assert.fail("Move of single heterotypic synonym should not throw exception: " + e.getMessage());
404
        }
405
        //Asserts
406
        //FIXME throws exception
407
        commitAndStartNewTransaction(tableNames);
408

    
409
//        printDataSet(System.err, new String[]{"AgentBase", "TaxonBase"});
410
//
411
//      printDataSet(System.err, new String[]{"TaxonBase"});
412

    
413
        heterotypicSynonym = (Synonym)service.load(uuidSyn5);
414

    
415
//      printDataSet(System.err, new String[]{"TaxonBase"});
416
//      System.exit(0);
417

    
418
        Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
419
        Assert.assertEquals("Synonym should still have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
420
        rel = heterotypicSynonym.getSynonymRelations().iterator().next();
421
        Assert.assertEquals("Accepted taxon of single relation should be new taxon", newTaxon, rel.getAcceptedTaxon());
422
        Assert.assertEquals("Old detail should be kept", "rel5", rel.getCitationMicroReference());
423

    
424

    
425
        //test heterotypic synonym with other synonym in homotypic group and moveHomotypicGroup="true"
426
        //+ new detail
427
        newTaxon = (Taxon)service.load(uuidNewTaxon);
428
        heterotypicSynonym = (Synonym)service.load(uuidSyn3);
429
        Reference<?> ref1 = referenceService.load(uuidRef1);
430
        Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
431
        Assert.assertEquals("Synonym should have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
432
        rel = heterotypicSynonym.getSynonymRelations().iterator().next();
433
        Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, rel.getAcceptedTaxon().getUuid());
434
        oldTaxon = rel.getAcceptedTaxon();
435
        Assert.assertEquals("Detail should be ref1", ref1, rel.getCitation());
436
        Assert.assertEquals("Detail should be 'rel3'", "rel3", rel.getCitationMicroReference());
437
        TaxonNameBase<?,?> oldSynName3 = heterotypicSynonym.getName();
438

    
439
        Synonym heterotypicSynonym4 = (Synonym)service.load(uuidSyn4);
440
        Assert.assertNotNull("Synonym should exist", heterotypicSynonym4);
441
        Assert.assertEquals("Synonym should have 1 relation", 1, heterotypicSynonym4.getSynonymRelations().size());
442
        SynonymRelationship rel4 = heterotypicSynonym4.getSynonymRelations().iterator().next();
443
        Assert.assertEquals("Accepted taxon of other synonym in group should be the old taxon", uuidOldTaxon, rel4.getAcceptedTaxon().getUuid());
444
        Assert.assertSame("Homotypic group of both synonyms should be same", oldSynName3.getHomotypicalGroup() , heterotypicSynonym4.getName().getHomotypicalGroup() );
445

    
446
        moveHomotypicGroup = true;
447
        keepReference = false;
448

    
449
        try {
450
            service.moveSynonymToAnotherTaxon(rel, newTaxon, moveHomotypicGroup, newSynonymRelationshipType, newReference, newReferenceDetail, keepReference);
451
        } catch (HomotypicalGroupChangeException e) {
452
            Assert.fail("Move with 'moveHomotypicGroup = true' should not throw exception: " + e.getMessage());
453
        }
454
        //Asserts
455
        commitAndStartNewTransaction(tableNames);
456
        heterotypicSynonym = (Synonym)service.load(uuidSyn3);
457
        Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
458
        Assert.assertEquals("Synonym should still have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
459
        rel = heterotypicSynonym.getSynonymRelations().iterator().next();
460
        Assert.assertEquals("Accepted taxon of relation should be new taxon now", newTaxon, rel.getAcceptedTaxon());
461
        TaxonNameBase<?,?> synName3 = rel.getSynonym().getName();
462

    
463
        heterotypicSynonym = (Synonym)service.load(uuidSyn4);
464
        Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
465
        Assert.assertEquals("Synonym should still have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
466
        rel = heterotypicSynonym.getSynonymRelations().iterator().next();
467
        Assert.assertEquals("Accepted taxon of relation should be new taxon now", newTaxon, rel.getAcceptedTaxon());
468
        Assert.assertNull("Old citation should be removed", rel.getCitation());
469
        Assert.assertNull("Old detail should be removed", rel.getCitationMicroReference());
470
        TaxonNameBase<?,?> synName4 = rel.getSynonym().getName();
471
        Assert.assertEquals("Homotypic group of both synonyms should be equal", synName3.getHomotypicalGroup() , synName4.getHomotypicalGroup() );
472
        Assert.assertSame("Homotypic group of both synonyms should be same", synName3.getHomotypicalGroup() , synName4.getHomotypicalGroup() );
473
        Assert.assertEquals("Homotypic group of both synonyms should be equal to old homotypic group", oldSynName3.getHomotypicalGroup() , synName3.getHomotypicalGroup() );
474

    
475

    
476
        //test single heterotypic synonym to homotypic synonym of new taxon
477
        //+ new reference
478
        newTaxon = (Taxon)service.load(uuidNewTaxon);
479
        Reference<?> ref2 = referenceService.load(uuidRef2);
480
        heterotypicSynonym = (Synonym)service.load(uuidSyn6);
481
        Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
482
        Assert.assertEquals("Synonym should have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
483
        rel = heterotypicSynonym.getSynonymRelations().iterator().next();
484
        Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, rel.getAcceptedTaxon().getUuid());
485
        oldTaxon = rel.getAcceptedTaxon();
486
        moveHomotypicGroup = false;
487
        keepReference = false;
488
        newReference = ref2;
489
        newReferenceDetail = "newRefDetail";
490
        newSynonymRelationshipType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
491

    
492
        try {
493
            service.moveSynonymToAnotherTaxon(rel, newTaxon, moveHomotypicGroup, newSynonymRelationshipType, newReference, newReferenceDetail, keepReference);
494
        } catch (HomotypicalGroupChangeException e) {
495
            Assert.fail("Move of single heterotypic synonym should not throw exception: " + e.getMessage());
496
        }
497
        //Asserts
498
        commitAndStartNewTransaction(tableNames);
499
        heterotypicSynonym = (Synonym)service.load(uuidSyn6);
500
        Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
501
        Assert.assertEquals("Synonym should still have 1 relation", 1, heterotypicSynonym.getSynonymRelations().size());
502
        rel = heterotypicSynonym.getSynonymRelations().iterator().next();
503
        Assert.assertEquals("Relationship type should be 'homotypic synonym'", newSynonymRelationshipType, rel.getType());
504
        Assert.assertEquals("Accepted taxon of single relation should be new taxon", newTaxon, rel.getAcceptedTaxon());
505
        Assert.assertEquals("New citation should be ref2", ref2 ,rel.getCitation());
506
        Assert.assertEquals("New detail should be kept", "newRefDetail", rel.getCitationMicroReference());
507

    
508
        Assert.assertEquals("New taxon and new synonym should have equal homotypical group", rel.getSynonym().getHomotypicGroup(), rel.getAcceptedTaxon().getHomotypicGroup());
509
        Assert.assertSame("New taxon and new synonym should have same homotypical group", rel.getSynonym().getHomotypicGroup(), rel.getAcceptedTaxon().getHomotypicGroup());
510
    }
511

    
512

    
513

    
514
    @Test
515
    public final void testGetHeterotypicSynonymyGroups(){
516
        Rank rank = Rank.SPECIES();
517
        Reference<?> ref1 = ReferenceFactory.newGeneric();
518
        //HomotypicalGroup group = HomotypicalGroup.NewInstance();
519
        Taxon taxon1 = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test3", null, null, null, null, null, null, null), null);
520
        Synonym synonym0 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
521
        Synonym synonym1 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
522
        Synonym synonym2 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test4", null, null, null, null, null, null, null), null);
523
        synonym0.getName().setHomotypicalGroup(taxon1.getHomotypicGroup());
524
        synonym2.getName().setHomotypicalGroup(synonym1.getHomotypicGroup());
525
        //tax2.addHeterotypicSynonymName(synonym.getName());
526
        taxon1.addSynonym(synonym1, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
527
        taxon1.addSynonym(synonym2, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
528

    
529
        service.save(synonym1);
530
        service.save(synonym2);
531
        service.save(taxon1);
532

    
533
        List<List<Synonym>> heteroSyns = service.getHeterotypicSynonymyGroups(taxon1, null);
534
        Assert.assertEquals("There should be 1 heterotypic group", 1, heteroSyns.size());
535
        List<Synonym> synList = heteroSyns.get(0);
536
        Assert.assertEquals("There should be 2 heterotypic syns in group 1", 2, synList.size());
537

    
538
        //test sec
539
        synonym2.setSec(ref1);
540
        heteroSyns = service.getHeterotypicSynonymyGroups(taxon1, null);
541
        Assert.assertEquals("There should be 1 heterotypic group", 1, heteroSyns.size());
542
        synList = heteroSyns.get(0);
543
        Assert.assertEquals("getHeterotypicSynonymyGroups should be independent of sec reference", 2, synList.size());
544

    
545
    }
546

    
547

    
548
    @Test
549
    public final void testGetHomotypicSynonymsByHomotypicGroup(){
550
        Rank rank = Rank.SPECIES();
551
        Reference<?> ref1 = ReferenceFactory.newGeneric();
552
        //HomotypicalGroup group = HomotypicalGroup.NewInstance();
553
        Taxon taxon1 = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test3", null, null, null, null, null, null, null), null);
554
        Synonym synonym0 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
555
        Synonym synonym1 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
556
        Synonym synonym2 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test4", null, null, null, null, null, null, null), null);
557
        synonym0.getName().setHomotypicalGroup(taxon1.getHomotypicGroup());
558
        synonym2.getName().setHomotypicalGroup(synonym1.getHomotypicGroup());
559
        //tax2.addHeterotypicSynonymName(synonym.getName());
560
        taxon1.addSynonym(synonym0, SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF());
561
        taxon1.addSynonym(synonym1, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
562
        taxon1.addSynonym(synonym2, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
563

    
564
        service.save(synonym1);
565
        service.save(synonym2);
566
        service.save(taxon1);
567

    
568
        List<Synonym> homoSyns = service.getHomotypicSynonymsByHomotypicGroup(taxon1, null);
569
        Assert.assertEquals("There should be 1 heterotypic group", 1, homoSyns.size());
570
        Assert.assertSame("The homotypic synonym should be synonym0", synonym0, homoSyns.get(0));
571

    
572
        //test sec
573
        synonym0.setSec(ref1);
574
        homoSyns = service.getHomotypicSynonymsByHomotypicGroup(taxon1, null);
575
        Assert.assertEquals("getHeterotypicSynonymyGroups should be independent of sec reference", 1, homoSyns.size());
576

    
577
    }
578

    
579
    @Test
580
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
581
    //test delete synonym, but the name will not be deleted
582
    public final void testDeleteSynonymSynonymTaxonDontDeleteName(){
583
        final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
584
                "SynonymRelationship","SynonymRelationship_AUD",
585
                "HomotypicalGroup","HomotypicalGroup_AUD"};
586

    
587
        int nSynonyms = service.count(Synonym.class);
588
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
589
        int nNames = nameService.count(TaxonNameBase.class);
590
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
591
        int nRelations = service.countAllRelationships();
592
        Assert.assertEquals("There should be two relationship left in the database", 2, nRelations);
593

    
594
        UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
595

    
596

    
597
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
598
        SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
599
        config.setDeleteNameIfPossible(false);
600
        config.setNewHomotypicGroupIfNeeded(true);
601
        service.deleteSynonym(synonym1, config);
602

    
603
        this.commitAndStartNewTransaction(tableNames);
604

    
605
        nSynonyms = service.count(Synonym.class);
606
        Assert.assertEquals("There should be 1 synonym left in the database", 1, nSynonyms);
607
        nNames = nameService.count(TaxonNameBase.class);
608
        Assert.assertEquals("There should be 4 names left in the database", 4, nNames);
609
        nRelations = service.countAllRelationships();
610
        Assert.assertEquals("There should be no relationship left in the database", 1, nRelations);
611
    }
612

    
613
    @Test
614
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
615
    //test delete synonym and his name
616
    public final void testDeleteSynonymSynonymTaxonDeleteName(){
617
        final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
618
                "SynonymRelationship","SynonymRelationship_AUD",
619
                "HomotypicalGroup","HomotypicalGroup_AUD"};
620

    
621
        int nSynonyms = service.count(Synonym.class);
622
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
623
        int nNames = nameService.count(TaxonNameBase.class);
624
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
625
        int nRelations = service.countAllRelationships();
626
        Assert.assertEquals("There should be 2 relationship left in the database", 2, nRelations);
627

    
628
        UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
629

    
630

    
631
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
632
        service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
633

    
634
        this.commitAndStartNewTransaction(tableNames);
635

    
636
        nSynonyms = service.count(Synonym.class);
637
        Assert.assertEquals("There should be 1 synonym left in the database", 1, nSynonyms);
638
        nNames = nameService.count(TaxonNameBase.class);
639
        Assert.assertEquals("There should be 3 names left in the database", 3, nNames);
640
        nRelations = service.countAllRelationships();
641
        Assert.assertEquals("There should be 1 relationship left in the database", 1, nRelations);
642

    
643
    }
644

    
645
    @Test
646
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
647
    //test remove synonym from taxon -> synonym and name still in the db and the synonymrelationship to the other taxon
648
    //test delete synonym -> all relationships are deleted, the name is deleted and the synonym itself
649
    public final void testDeleteSynonymSynonymTaxonBooleanRelToOneTaxon(){
650
        final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
651
                "SynonymRelationship","SynonymRelationship_AUD",
652
                "HomotypicalGroup","HomotypicalGroup_AUD"};
653

    
654
        int nSynonyms = service.count(Synonym.class);
655
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
656
        int nNames = nameService.count(TaxonNameBase.class);
657
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
658

    
659
        UUID uuidTaxon1=UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
660
        UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
661
        UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
662

    
663

    
664
        Taxon taxon2 = (Taxon)service.load(uuidTaxon1);
665

    
666
        List<String> initStrat = new ArrayList<String>();
667
        initStrat.add("markers");
668
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1, initStrat);
669
        int nRelations = service.countAllRelationships();
670
        Assert.assertEquals("There should be 2 relationship left in the database", 2, nRelations);
671

    
672
        taxon2.removeSynonym(synonym1, false);
673
        service.saveOrUpdate(taxon2);
674

    
675
        commitAndStartNewTransaction(null);
676

    
677
        nSynonyms = service.count(Synonym.class);
678
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
679
        nNames = nameService.count(TaxonNameBase.class);
680
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
681
        nRelations = service.countAllRelationships();
682
        Assert.assertEquals("There should be 1 relationship left in the database", 1, nRelations);
683
        Marker marker1 = Marker.NewInstance(MarkerType.IMPORTED(), true);
684
        Marker marker2 = Marker.NewInstance(MarkerType.COMPUTED(), true);
685
        synonym1.addMarker(marker1);
686
        synonym1.addMarker(marker2);
687
        service.update(synonym1);
688
        synonym1 =(Synonym) service.load(uuidSynonym1);
689

    
690

    
691
        Set<Marker> markers = synonym1.getMarkers();
692
        Marker marker = markers.iterator().next();
693
        UUID markerUUID = marker.getUuid();
694
       // taxon2 = (Taxon)service.load(uuidTaxon2);
695
        synonym1 = (Synonym)service.load(uuidSynonym1);
696
        //the marker should not prevent the deletion
697
        DeleteResult result = service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
698
        if (!result.isOk()){
699
        	Assert.fail();
700
        }
701

    
702

    
703

    
704

    
705
        commitAndStartNewTransaction(tableNames);
706
        nSynonyms = service.count(Synonym.class);
707
        Assert.assertEquals("There should be 1 synonym left in the database", 1, nSynonyms);
708
        nNames = nameService.count(TaxonNameBase.class);
709
        Assert.assertEquals("There should be 3 names left in the database", 3, nNames);
710
        nRelations = service.countAllRelationships();
711
        Assert.assertEquals("There should be no relationship left in the database", 1, nRelations);
712
        marker = markerService.load(markerUUID);
713
        assertNull(marker);
714

    
715
    }
716

    
717
    @Test
718
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
719
    //test delete synonym, only for a special taxon, but because of other relationships it will not be deleted at all
720
    public final void testDeleteSynonymSynonymTaxonBooleanDeleteOneTaxon(){
721
        final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
722
                "SynonymRelationship","SynonymRelationship_AUD",
723
                "HomotypicalGroup","HomotypicalGroup_AUD"};
724

    
725

    
726
        int nSynonyms = service.count(Synonym.class);
727
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
728
        int nNames = nameService.count(TaxonNameBase.class);
729
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
730

    
731
        UUID uuidTaxon1=UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
732
        UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
733
        UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
734
        UUID uuidSynonym2=UUID.fromString("f8d86dc9-5f18-4877-be46-fbb9412465e4");
735

    
736
        Taxon taxon1 = (Taxon)service.load(uuidTaxon1);
737
        Taxon taxon2 = (Taxon)service.load(uuidTaxon2);
738
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
739
        taxon2.addSynonym(synonym1, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
740
        service.saveOrUpdate(synonym1);
741
        int nRelations = service.countAllRelationships();
742
        Assert.assertEquals("There should be 3 relationship left in the database", 3, nRelations);
743
        service.deleteSynonym(synonym1, taxon1, new SynonymDeletionConfigurator());
744

    
745
        this.commitAndStartNewTransaction(tableNames);
746

    
747
        nSynonyms = service.count(Synonym.class);
748
        Assert.assertEquals("There should still be 2 synonyms left in the database (synonym is related to taxon2)", 2, nSynonyms);
749
        nNames = nameService.count(TaxonNameBase.class);
750
        Assert.assertEquals("There should be 4 names left in the database (name not deleted as synonym was not deleted)", 4, nNames);
751
        nRelations = service.countAllRelationships();
752
        Assert.assertEquals("There should be 2 relationship left in the database", 2, nRelations);
753

    
754

    
755
    }
756

    
757
    @Test
758
    @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
759

    
760
    public final void testDeleteSynonymSynonymTaxonBooleanWithRelatedName(){
761
        final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
762
                "SynonymRelationship","SynonymRelationship_AUD",
763
                "HomotypicalGroup","HomotypicalGroup_AUD"};
764

    
765
        int nSynonyms = service.count(Synonym.class);
766
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
767
        int nNames = nameService.count(TaxonNameBase.class);
768
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
769

    
770
        UUID uuidTaxon1=UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
771
        UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
772
        UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
773
        UUID uuidSynonym2=UUID.fromString("f8d86dc9-5f18-4877-be46-fbb9412465e4");
774
        UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
775

    
776
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
777
        TaxonNameBase name2 = nameService.load(uuidSynonymName2);
778
        UUID name3Uuid = synonym1.getName().getUuid();
779
        TaxonNameBase name3 = nameService.load(name3Uuid);
780
        name3.addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null);
781

    
782
        service.saveOrUpdate(synonym1);
783

    
784
        int nRelations = nameService.getAllRelationships(1000, 0).size();
785
        logger.info("number of name relations: " + nRelations);
786
        Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
787
        SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
788

    
789
        service.deleteSynonym(synonym1, config);
790

    
791
        this.commitAndStartNewTransaction(tableNames);
792
        //synonym is deleted, but the name can not be deleted because of a name relationship
793
        nSynonyms = service.count(Synonym.class);
794
        Assert.assertEquals("There should still be 1 synonyms left in the database", 1, nSynonyms);
795
        nNames = nameService.count(TaxonNameBase.class);
796
        Assert.assertEquals("There should be 4 names left in the database (name is related to synonymName2)", 4, nNames);
797
        nRelations = service.countAllRelationships();
798
        //may change with better implementation of countAllRelationships (see #2653)
799
        nRelations = nameService.getAllRelationships(1000, 0).size();
800
        logger.info("number of name relations: " + nRelations);
801
        Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
802

    
803

    
804
        //clean up database
805
        name2 = nameService.load(uuidSynonymName2);
806
        NameRelationship rel = CdmBase.deproxy(name2.getNameRelations().iterator().next(), NameRelationship.class);
807
        name2.removeNameRelationship(rel);
808
        nameService.save(name2);
809
        this.setComplete();
810
        this.endTransaction();
811

    
812
    }
813
    @Test
814
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
815
    public final void testDeleteSynonymSynonymTaxonBooleanWithRelatedNameDeleteAllNameRelations(){
816
        final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
817
                "SynonymRelationship","SynonymRelationship_AUD",
818
                "HomotypicalGroup","HomotypicalGroup_AUD"};
819

    
820
        int nSynonyms = service.count(Synonym.class);
821
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
822
        int nNames = nameService.count(TaxonNameBase.class);
823
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
824

    
825
        UUID uuidTaxon1=UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
826
        UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
827
        UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
828
        UUID uuidSynonym2=UUID.fromString("f8d86dc9-5f18-4877-be46-fbb9412465e4");
829
        UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
830

    
831
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
832
        TaxonNameBase name2 = nameService.load(uuidSynonymName2);
833
        UUID name3Uuid = synonym1.getName().getUuid();
834
        TaxonNameBase name3 = nameService.load(name3Uuid);
835
        name3.addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null);
836

    
837
        service.saveOrUpdate(synonym1);
838

    
839
        int nRelations = nameService.getAllRelationships(1000, 0).size();
840
        logger.info("number of name relations: " + nRelations);
841
        Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
842
        SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
843
        NameDeletionConfigurator nameDeletionConfig = new NameDeletionConfigurator();
844
        nameDeletionConfig.setRemoveAllNameRelationships(true);
845
        config.setNameDeletionConfig(nameDeletionConfig);
846

    
847
        service.deleteSynonym(synonym1, config);
848

    
849
        this.commitAndStartNewTransaction(tableNames);
850

    
851
        nSynonyms = service.count(Synonym.class);
852
        Assert.assertEquals("There should still be 1 synonyms left in the database", 1, nSynonyms);
853
        nNames = nameService.count(TaxonNameBase.class);
854
        Assert.assertEquals("There should be 3 names left in the database ", 3, nNames);
855
        nRelations = service.countAllRelationships();
856
        //may change with better implementation of countAllRelationships (see #2653)
857
        nRelations = nameService.getAllRelationships(1000, 0).size();
858
        logger.info("number of name relations: " + nRelations);
859
        Assert.assertEquals("There should be no name relationship left in the database", 0, nRelations);
860
    }
861

    
862
    @Test
863
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
864
    public final void testDeleteSynonymSynonymTaxonBooleanWithRelatedNameIgnoreIsBasionym(){
865
        final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
866
                "SynonymRelationship","SynonymRelationship_AUD",
867
                "HomotypicalGroup","HomotypicalGroup_AUD"};
868

    
869
        int nSynonyms = service.count(Synonym.class);
870
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
871
        int nNames = nameService.count(TaxonNameBase.class);
872
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
873

    
874
        UUID uuidTaxon1=UUID.fromString("c47fdb72-f32c-452e-8305-4b44f01179d0");
875
        UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
876
        UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
877
        UUID uuidSynonym2=UUID.fromString("f8d86dc9-5f18-4877-be46-fbb9412465e4");
878
        UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
879

    
880
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
881
        TaxonNameBase synName2 = nameService.load(uuidSynonymName2);
882
        UUID name3Uuid = synonym1.getName().getUuid();
883
        TaxonNameBase synName1 = nameService.load(name3Uuid);
884
        synName1.addRelationshipFromName(synName2, NameRelationshipType.BASIONYM(), null);
885

    
886
        service.saveOrUpdate(synonym1);
887

    
888
        int nRelations = nameService.getAllRelationships(1000, 0).size();
889
        logger.info("number of name relations: " + nRelations);
890
        Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
891
        SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
892
        NameDeletionConfigurator nameDeletionConfig = new NameDeletionConfigurator();
893
        nameDeletionConfig.setIgnoreIsBasionymFor(true);
894
        config.setNameDeletionConfig(nameDeletionConfig);
895

    
896
        DeleteResult result =service.deleteSynonym(synonym1, config);
897
        if (!result.isOk()){
898
        	Assert.fail();
899
        }
900

    
901

    
902
        logger.debug(result);
903
        this.commitAndStartNewTransaction(tableNames);
904

    
905
        nSynonyms = service.count(Synonym.class);
906
        Assert.assertEquals("There should still be 1 synonyms left in the database", 1, nSynonyms);
907
        nNames = nameService.count(TaxonNameBase.class);
908
        Assert.assertEquals("There should be 3 names left in the database ", 3, nNames);
909
        nRelations = service.countAllRelationships();
910
        //may change with better implementation of countAllRelationships (see #2653)
911
        nRelations = nameService.getAllRelationships(1000, 0).size();
912
        logger.info("number of name relations: " + nRelations);
913
        Assert.assertEquals("There should be no name relationship left in the database", 0, nRelations);
914
    }
915

    
916

    
917
    @Test
918
    @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
919
    public final void testDeleteSynonymSynonymTaxonBooleanWithRollback(){
920
        final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
921
                "SynonymRelationship","SynonymRelationship_AUD",
922
                "HomotypicalGroup","HomotypicalGroup_AUD"};
923

    
924
        int nSynonyms = service.count(Synonym.class);
925
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
926
        int nNames = nameService.count(TaxonNameBase.class);
927
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
928
        int nRelations = service.countAllRelationships();
929

    
930

    
931
        //may change with better implementation of countAllRelationships (see #2653)
932

    
933
        logger.debug("");
934
        Assert.assertEquals("There should be 2 relationships in the database (the 2 synonym relationship) but no name relationship", 2, nRelations);
935

    
936
        UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
937
        UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
938

    
939
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
940
        TaxonNameBase name2 = nameService.load(uuidSynonymName2);
941
        synonym1.getName().addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null);
942

    
943
        service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
944

    
945
        this.rollback();
946
//		printDataSet(System.out, tableNames);
947
        this.startNewTransaction();
948

    
949
        nSynonyms = service.count(Synonym.class);
950
        Assert.assertEquals("There should still be 2 synonyms left in the database", 2, nSynonyms);
951
        nNames = nameService.count(TaxonNameBase.class);
952
        Assert.assertEquals("There should be 4 names left in the database", 4, nNames);
953
        nRelations = service.countAllRelationships();
954
        //may change with better implementation of countAllRelationships (see #2653)
955
        Assert.assertEquals("There should be 2 relationship in the database (the 2 synonym relationship) but no name relationship", 2, nRelations);
956

    
957
    }
958

    
959
    @Test
960
    @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
961
    public final void testDeleteSynonymSynonymTaxonBooleanWithoutTransaction(){
962
        final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonNameBase","TaxonNameBase_AUD",
963
                "SynonymRelationship","SynonymRelationship_AUD",
964
                "HomotypicalGroup","HomotypicalGroup_AUD"};
965

    
966
        int nSynonyms = service.count(Synonym.class);
967
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
968
        int nNames = nameService.count(TaxonNameBase.class);
969
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
970
        int nRelations = service.countAllRelationships();
971
        //may change with better implementation of countAllRelationships (see #2653)
972
        Assert.assertEquals("There should be 2 relationship in the database (the 2 synonym relationships) but no name relationship", 2, nRelations);
973

    
974
        UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
975
        UUID uuidSynonymName2=UUID.fromString("613f3c93-013e-4ffc-aadc-1c98d71c335e");
976

    
977
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
978
        TaxonNameBase name2 = nameService.load(uuidSynonymName2);
979
        synonym1.getName().addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null);
980

    
981
        service.saveOrUpdate(synonym1);
982
        nRelations = service.countAllRelationships();
983
        Assert.assertEquals("There should be two relationships in the database", 2, nRelations);
984
        this.setComplete();
985
        this.endTransaction();
986

    
987
//        printDataSet(System.out, tableNames);
988

    
989
        //out of wrapping transaction
990
        service.deleteSynonym(synonym1,  new SynonymDeletionConfigurator());
991

    
992
        this.startNewTransaction();
993

    
994
        nSynonyms = service.count(Synonym.class);
995
        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);
996
        nNames = nameService.count(TaxonNameBase.class);
997
        Assert.assertEquals("There should be 4 names left in the database", 4, nNames);
998
        nRelations = service.countAllRelationships();
999
        Assert.assertEquals("There should be no taxon or synonym relationship in the database", 1, nRelations);
1000
        nRelations = nameService.getAllRelationships(1000,0).size();
1001
        Assert.assertEquals("There should be one name relationship in the database", 1, nRelations);
1002

    
1003
    }
1004

    
1005
    @Test
1006
    @DataSet("TaxonServiceImplTest.testInferredSynonyms.xml")
1007
    public void testCreateInferredSynonymy(){
1008

    
1009
        UUID classificationUuid = UUID.fromString("aeee7448-5298-4991-b724-8d5b75a0a7a9");
1010
        Classification tree = classificationService.find(classificationUuid);
1011
        UUID taxonUuid = UUID.fromString("bc09aca6-06fd-4905-b1e7-cbf7cc65d783");
1012
        TaxonBase<?> taxonBase =  service.find(taxonUuid);
1013
        List <Synonym> synonyms = service.list(Synonym.class, null, null, null, null);
1014
        assertEquals("Number of synonyms should be 2",2,synonyms.size());
1015
        Taxon taxon = (Taxon)taxonBase;
1016

    
1017
        //synonyms = taxonDao.getAllSynonyms(null, null);
1018
        //assertEquals("Number of synonyms should be 2",2,synonyms.size());
1019
        List<Synonym> inferredSynonyms = service.createInferredSynonyms(taxon, tree, SynonymRelationshipType.INFERRED_EPITHET_OF(), true);
1020
        assertNotNull("there should be a new synonym ", inferredSynonyms);
1021
        assertEquals ("the name of inferred epithet should be SynGenus lachesis", "SynGenus lachesis sec. Sp. Pl.", inferredSynonyms.get(0).getTitleCache());
1022

    
1023
        inferredSynonyms = service.createInferredSynonyms(taxon, tree, SynonymRelationshipType.INFERRED_GENUS_OF(), true);
1024
        assertNotNull("there should be a new synonym ", inferredSynonyms);
1025
        assertEquals ("the name of inferred epithet should be SynGenus lachesis", "Acherontia ciprosus sec. Sp. Pl.", inferredSynonyms.get(0).getTitleCache());
1026

    
1027
        inferredSynonyms = service.createInferredSynonyms(taxon, tree, SynonymRelationshipType.POTENTIAL_COMBINATION_OF(), true);
1028
        assertNotNull("there should be a new synonym ", inferredSynonyms);
1029
        assertEquals ("the name of inferred epithet should be SynGenus lachesis", "SynGenus ciprosus sec. Sp. Pl.", inferredSynonyms.get(0).getTitleCache());
1030
        //assertTrue("set of synonyms should contain an inferred Synonym ", synonyms.contains(arg0))
1031
    }
1032

    
1033
    @Test
1034
    @DataSet("BlankDataSet.xml")
1035
    public final void testTaxonDeletionConfig(){
1036
        final String[]tableNames = {
1037
                "Classification", "Classification_AUD",
1038
                "TaxonBase","TaxonBase_AUD",
1039
                "TaxonNode","TaxonNode_AUD",
1040
                "TaxonNameBase","TaxonNameBase_AUD",
1041
                "SynonymRelationship","SynonymRelationship_AUD",
1042
                "TaxonRelationship", "TaxonRelationship_AUD",
1043
                "TaxonDescription", "TaxonDescription_AUD",
1044
                "HomotypicalGroup","HomotypicalGroup_AUD",
1045
                "PolytomousKey","PolytomousKey_AUD",
1046
                "PolytomousKeyNode","PolytomousKeyNode_AUD",
1047
                "Media","Media_AUD",
1048
                "WorkingSet","WorkingSet_AUD",
1049
                "DescriptionElementBase","DescriptionElementBase_AUD",
1050
        		"DeterminationEvent","DeterminationEvent_AUD",
1051
        		"SpecimenOrObservationBase","SpecimenOrObservationBase_AUD"};
1052

    
1053
        UUID uuidParent=UUID.fromString("b5271d4f-e203-4577-941f-00d76fa9f4ca");
1054
        UUID uuidChild1=UUID.fromString("326167f9-0b97-4e7d-b1bf-4ca47b82e21e");
1055
        UUID uuidSameAs=UUID.fromString("c2bb0f01-f2dd-43fb-ba12-2a85727ccb8d");
1056
        commitAndStartNewTransaction(tableNames);
1057
        Taxon testTaxon = TaxonGenerator.getTestTaxon();
1058
        service.save(testTaxon);
1059
        commitAndStartNewTransaction(tableNames);
1060
        int nTaxa = service.count(Taxon.class);
1061

    
1062
        Assert.assertEquals("There should be 4 taxa in the database", 4, nTaxa);
1063
        Taxon parent = (Taxon)service.find(TaxonGenerator.GENUS_UUID);
1064
        Assert.assertNotNull("Parent taxon should exist", parent);
1065
        Taxon child1 = (Taxon)service.find(TaxonGenerator.SPECIES1_UUID);
1066
        Assert.assertNotNull("Child taxon should exist", child1);
1067
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1068
        config.setDeleteTaxonNodes(false);
1069
        config.setDeleteMisappliedNamesAndInvalidDesignations(false);
1070
        //try {
1071
            //commitAndStartNewTransaction(tableNames);
1072

    
1073
        DeleteResult result = service.deleteTaxon(child1.getUuid(), config, null);
1074
        if (result.isOk()){
1075
            Assert.fail("Delete should throw an error as long as name is used in classification.");
1076
        }
1077

    
1078
        nTaxa = service.count(Taxon.class);
1079
        Assert.assertEquals("There should be 4 taxa in the database", 4, nTaxa);
1080
        child1 = (Taxon)service.find(TaxonGenerator.SPECIES1_UUID);
1081
        Assert.assertNotNull("Child taxon should exist", child1);
1082
        Assert.assertEquals("Child should belong to 1 node", 1, child1.getTaxonNodes().size());
1083

    
1084
        TaxonNode node = child1.getTaxonNodes().iterator().next();
1085
        child1.addSource(IdentifiableSource.NewInstance(OriginalSourceType.Import));
1086

    
1087
        SpecimenOrObservationBase<IIdentifiableEntityCacheStrategy> identifiedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.DerivedUnit);
1088
        DeterminationEvent determinationEvent = DeterminationEvent.NewInstance(child1, identifiedUnit);
1089
        //UUID eventUUID = eventService.save(determinationEvent);
1090
        UUID identifiedUnitUUID = occurenceService.save(identifiedUnit).getUuid();
1091

    
1092

    
1093
        TaxonNode parentNode = node.getParent();
1094
        parentNode =CdmBase.deproxy(parentNode, TaxonNode.class);
1095
        parentNode.deleteChildNode(node);
1096
        nodeService.save(parentNode);
1097
        //commitAndStartNewTransaction(tableNames);
1098

    
1099
       // try {
1100

    
1101
       result = service.deleteTaxon(child1
1102
    		   .getUuid(), config, null);
1103
       if (result.isOk()){
1104
           	Assert.fail("Delete should throw an exception because of the determination event");
1105
       }
1106

    
1107

    
1108

    
1109
        //determinationEvent = (DeterminationEvent)eventService.load(eventUUID);
1110
        commitAndStartNewTransaction(tableNames);
1111
        identifiedUnit = occurenceService.load(identifiedUnitUUID);
1112

    
1113
        occurenceService.delete(identifiedUnit);
1114

    
1115
        commitAndStartNewTransaction(tableNames);
1116
        child1 = (Taxon)service.find(TaxonGenerator.SPECIES1_UUID);
1117

    
1118
        assertEquals(0, child1.getTaxonNodes().size());
1119
       // try {
1120

    
1121
         result = service.deleteTaxon(child1.getUuid(), config, null);
1122

    
1123
         if (!result.isOk()){
1124
            Assert.fail("Delete should not throw an exception anymore");
1125
         }
1126

    
1127
        nTaxa = service.count(Taxon.class);
1128
        Assert.assertEquals("There should be 3 taxa in the database", 3, nTaxa);
1129

    
1130
        config.setDeleteTaxonNodes(true);
1131
        Taxon child2 =(Taxon) service.find(TaxonGenerator.SPECIES2_UUID);
1132

    
1133
       // try {
1134
        result = service.deleteTaxon(child2.getUuid(), config, null);
1135
        if (!result.isOk()){
1136
            Assert.fail("Delete should not throw an exception");
1137
        }
1138

    
1139

    
1140
        //service.find(uuid);
1141

    
1142
        nTaxa = service.count(Taxon.class);
1143
        Assert.assertEquals("There should be 2 taxa in the database",2, nTaxa);
1144
//		nNames = nameService.count(TaxonNameBase.class);
1145
//		Assert.assertEquals("There should be 3 names left in the database", 3, nNames);
1146
//		int nRelations = service.countAllRelationships();
1147
//		Assert.assertEquals("There should be no relationship left in the database", 0, nRelations);
1148
    }
1149

    
1150

    
1151
    @Test
1152
    @DataSet(value="BlankDataSet.xml")
1153
    public final void testDeleteTaxon(){
1154

    
1155
        //create a small classification
1156
        Taxon testTaxon = TaxonGenerator.getTestTaxon();
1157

    
1158
        UUID uuid = service.save(testTaxon).getUuid();
1159

    
1160
        Taxon speciesTaxon = (Taxon)service.find(TaxonGenerator.SPECIES1_UUID);
1161
        Iterator<TaxonDescription> descriptionIterator = speciesTaxon.getDescriptions().iterator();
1162
        UUID descrUUID = null;
1163
        UUID descrElementUUID = null;
1164
        if (descriptionIterator.hasNext()){
1165
            TaxonDescription descr = descriptionIterator.next();
1166
            descrUUID = descr.getUuid();
1167
            descrElementUUID = descr.getElements().iterator().next().getUuid();
1168
        }
1169
        BotanicalName taxonName = (BotanicalName) nameService.find(TaxonGenerator.SPECIES1_NAME_UUID);
1170
        assertNotNull(taxonName);
1171

    
1172
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1173
        config.setDeleteNameIfPossible(false);
1174

    
1175

    
1176

    
1177
       // try {
1178
        DeleteResult result = service.deleteTaxon(speciesTaxon.getUuid(), config, null);
1179
        if (!result.isOk()){
1180
        	Assert.fail();
1181
        }
1182
        commitAndStartNewTransaction(null);
1183

    
1184
        taxonName = (BotanicalName) nameService.find(TaxonGenerator.SPECIES1_NAME_UUID);
1185
        Taxon taxon = (Taxon)service.find(TaxonGenerator.SPECIES1_UUID);
1186

    
1187
        //descriptionService.find(descrUUID);
1188
        assertNull(descriptionService.find(descrUUID));
1189
        assertNull(descriptionService.getDescriptionElementByUuid(descrElementUUID));
1190
        //assertNull(synName);
1191
        assertNotNull(taxonName);
1192
        assertNull(taxon);
1193
        config.setDeleteNameIfPossible(true);
1194
        Taxon newTaxon = Taxon.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null);
1195
        service.save(newTaxon);
1196
        result = service.deleteTaxon(newTaxon.getUuid()
1197
        		, config, null);
1198
        if (!result.isOk()){
1199
        	Assert.fail();
1200
        }
1201

    
1202

    
1203
    }
1204

    
1205
    @Test
1206
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="BlankDataSet.xml")
1207
    public final void testDeleteTaxonDeleteSynonymRelations(){
1208

    
1209
    	 final String[]tableNames = {
1210
                 "Classification", "Classification_AUD",
1211
                 "TaxonBase","TaxonBase_AUD",
1212
                 "TaxonNode","TaxonNode_AUD",
1213
                 "TaxonNameBase","TaxonNameBase_AUD"};
1214
    	 commitAndStartNewTransaction(tableNames);
1215
        //create a small classification
1216
        Taxon testTaxon = TaxonGenerator.getTestTaxon();
1217

    
1218
        UUID uuid = service.save(testTaxon).getUuid();
1219

    
1220
        Taxon speciesTaxon = (Taxon)service.find(TaxonGenerator.SPECIES2_UUID);
1221

    
1222
        SynonymRelationship synRel = speciesTaxon.getSynonymRelations().iterator().next();
1223
        UUID synonymRelationUuid = synRel.getUuid();
1224
        UUID synonymUuid = synRel.getSynonym().getUuid();
1225
        int i = service.getAllRelationships(1000, 0).size();
1226

    
1227
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1228
        config.setDeleteSynonymsIfPossible(false);
1229

    
1230

    
1231
       DeleteResult result = service.deleteTaxon(speciesTaxon.getUuid(), config, null);
1232
        if (!result.isOk()){
1233
        	Assert.fail();
1234
        }
1235
        commitAndStartNewTransaction(null);
1236

    
1237
        Taxon taxon = (Taxon)service.find(TaxonGenerator.SPECIES2_UUID);
1238
        assertNull("The deleted taxon should no longer exist", taxon);
1239

    
1240
        assertNotNull("The synonym should still exist since DeleteSynonymsIfPossible was false", service.find(synonymUuid));
1241

    
1242
        for(RelationshipBase rel : service.getAllRelationships(1000, 0)){
1243
            if(rel instanceof SynonymRelationship && rel.getUuid().equals(synonymRelationUuid)){
1244
                Assert.fail("The SynonymRelationship should no longer exist");
1245
            }
1246
        }
1247
    }
1248

    
1249

    
1250
    @Test
1251
    @DataSet(value="BlankDataSet.xml")
1252
    public final void testDeleteTaxonNameUsedInOtherContext(){
1253

    
1254
        //create a small classification
1255
        Taxon testTaxon = TaxonGenerator.getTestTaxon();
1256

    
1257
        UUID uuid = service.save(testTaxon).getUuid();
1258

    
1259
        Taxon speciesTaxon = (Taxon)service.find(TaxonGenerator.SPECIES1_UUID);
1260

    
1261
        BotanicalName taxonName = (BotanicalName) nameService.find(TaxonGenerator.SPECIES1_NAME_UUID);
1262
        assertNotNull(taxonName);
1263
        BotanicalName fromName = BotanicalName.NewInstance(Rank.SPECIES());
1264
        taxonName.addRelationshipFromName(fromName, NameRelationshipType.VALIDATED_BY_NAME(), null);
1265

    
1266
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1267
        config.setDeleteNameIfPossible(true);
1268
        DeleteResult result = service.deleteTaxon(speciesTaxon.getUuid(), config, null);
1269
        if (!result.isOk()){
1270
        	Assert.fail();
1271
        }
1272
        commitAndStartNewTransaction(null);
1273

    
1274
        taxonName = (BotanicalName) nameService.find(TaxonGenerator.SPECIES1_NAME_UUID);
1275
        Taxon taxon = (Taxon)service.find(TaxonGenerator.SPECIES1_UUID);
1276
        //because of the namerelationship the name cannot be deleted
1277
        assertNotNull(taxonName);
1278
        assertNull(taxon);
1279

    
1280
    }
1281

    
1282
    @Test
1283
    @DataSet(value="BlankDataSet.xml")
1284
    public final void testDeleteTaxonNameUsedInTwoClassificationsDeleteAllNodes(){
1285
        commitAndStartNewTransaction(null);
1286
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1287
        //create a small classification
1288
        Taxon testTaxon = TaxonGenerator.getTestTaxon();
1289

    
1290
        UUID uuid = service.save(testTaxon).getUuid();
1291
        //BotanicalName name = nameService.find(uuid);
1292
        Set<TaxonNode> nodes = testTaxon.getTaxonNodes();
1293
        TaxonNode node = nodes.iterator().next();
1294
        List<TaxonNode> childNodes = node.getChildNodes();
1295
        TaxonNode childNode = childNodes.iterator().next();
1296
        UUID childUUID = childNode.getTaxon().getUuid();
1297
        Classification secondClassification = TaxonGenerator.getTestClassification("secondClassification");
1298

    
1299
        secondClassification.addChildTaxon(testTaxon, null, null);
1300
        //delete the taxon in all classifications
1301
        //try {
1302
       DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, null);
1303
       if (!result.isOk()){
1304
        	Assert.fail();
1305
        }
1306
        commitAndStartNewTransaction(null);
1307
        Taxon tax = (Taxon)service.find(uuid);
1308
        assertNull(tax);
1309
        Taxon childTaxon = (Taxon)service.find(childUUID);
1310
        assertNull(tax);
1311
        commitAndStartNewTransaction(null);
1312

    
1313

    
1314

    
1315

    
1316

    
1317
    }
1318

    
1319
    @Test
1320
    @DataSet(value="BlankDataSet.xml")
1321
    public final void testDeleteTaxonNameUsedInTwoClassificationsDoNotDeleteAllNodes(){
1322
        // delete the taxon only in second classification, this should delete only the nodes, not the taxa
1323
        Taxon testTaxon = TaxonGenerator.getTestTaxon();
1324
        UUID uuid = service.save(testTaxon).getUuid();
1325
        Classification secondClassification = TaxonGenerator.getTestClassification("secondClassification");
1326
        Set<TaxonNode> nodes = testTaxon.getTaxonNodes();
1327
        TaxonNode node = nodes.iterator().next();
1328
        List<TaxonNode> childNodes = node.getChildNodes();
1329
        TaxonNode childNode = childNodes.iterator().next();
1330
        UUID childUUID = childNode.getTaxon().getUuid();
1331
        childNode = secondClassification.addChildTaxon(testTaxon, null, null);
1332
        UUID childNodeUUID = childNode.getUuid();
1333

    
1334
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1335
        config.setDeleteInAllClassifications(false);
1336
       //     try {
1337
       DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, secondClassification.getUuid());
1338
/*                Assert.fail("The taxon should not be deletable because it is used in a second classification and the configuration is set to deleteInAllClassifications = false");
1339
            } catch (DataChangeNoRollbackException e) {
1340
                logger.debug(e.getMessage());
1341
            }
1342
  */
1343

    
1344
       if (result.isOk()){
1345
           	Assert.fail("The taxon should not be deletable because it is used in a second classification and the configuration is set to deleteInAllClassifications = false");
1346
        }
1347

    
1348
        //commitAndStartNewTransaction(null);
1349
        Taxon tax = (Taxon)service.find(uuid);
1350
        assertNotNull(tax);
1351
        Taxon childTaxon = (Taxon)service.find(childUUID);
1352
        assertNotNull(tax);
1353
        node = nodeService.find(childNodeUUID);
1354
        assertNull(node);
1355
    }
1356

    
1357
    @Test
1358
    @DataSet(value="BlankDataSet.xml")
1359
    public final void testTaxonNodeDeletionConfiguratorMoveToParent(){
1360
        //test childHandling MOVE_TO_PARENT:
1361
        Taxon testTaxon = TaxonGenerator.getTestTaxon();
1362
        UUID uuid = service.save(testTaxon).getUuid();
1363

    
1364
        Taxon topMost = Taxon.NewInstance(BotanicalName.NewInstance(Rank.FAMILY()), null);
1365

    
1366
        Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1367
        TaxonNode node =nodes.next();
1368
        Classification classification = node.getClassification();
1369
        classification.addParentChild(topMost, testTaxon, null, null);
1370
        UUID topMostUUID = service.save(topMost).getUuid();
1371

    
1372
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1373
        config.getTaxonNodeConfig().setChildHandling(ChildHandling.MOVE_TO_PARENT);
1374

    
1375

    
1376
        DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, null);
1377
        if(!result.isOk()){
1378
         	Assert.fail();
1379
       	}
1380

    
1381
        commitAndStartNewTransaction(null);
1382
        Taxon tax = (Taxon)service.find(uuid);
1383
        assertNull(tax);
1384
        tax = (Taxon)service.find(topMostUUID);
1385
        Set<TaxonNode> topMostNodes = tax.getTaxonNodes();
1386
        assertNotNull(topMostNodes);
1387
        assertEquals("there should be one taxon node", 1, topMostNodes.size());
1388
        nodes = topMostNodes.iterator();
1389
        TaxonNode topMostNode = nodes.next();
1390
        int size = topMostNode.getChildNodes().size();
1391

    
1392
        assertEquals(2, size);
1393
    }
1394

    
1395
    @Test
1396
    @DataSet(value="BlankDataSet.xml")
1397
    public final void testTaxonNodeDeletionConfiguratorDeleteChildren(){
1398
        //test childHandling DELETE:
1399
        Taxon testTaxon = TaxonGenerator.getTestTaxon();
1400
        UUID uuid = service.save(testTaxon).getUuid();
1401

    
1402
        Taxon topMost = Taxon.NewInstance(BotanicalName.NewInstance(Rank.FAMILY()), null);
1403

    
1404
        Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1405
        TaxonNode node =nodes.next();
1406
        UUID taxonNodeUUID = node.getUuid();
1407
        Classification classification = node.getClassification();
1408
        classification.addParentChild(topMost, testTaxon, null, null);
1409
        UUID topMostUUID = service.save(topMost).getUuid();
1410

    
1411
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1412
        config.getTaxonNodeConfig().setChildHandling(ChildHandling.DELETE);
1413

    
1414
       // try {
1415
        DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, null);
1416
        if(!result.isOk()){
1417
         	Assert.fail();
1418
       	}
1419
        commitAndStartNewTransaction(null);
1420
        Taxon tax = (Taxon)service.find(uuid);
1421
        assertNull(tax);
1422
        tax = (Taxon)service.find(topMostUUID);
1423
        Set<TaxonNode> topMostNodes = tax.getTaxonNodes();
1424
        assertNotNull(topMostNodes);
1425
        assertEquals("there should be one taxon node", 1, topMostNodes.size());
1426
        nodes = topMostNodes.iterator();
1427
        TaxonNode topMostNode = nodes.next();
1428
        int size = topMostNode.getChildNodes().size();
1429
        node = nodeService.find(taxonNodeUUID);
1430
        assertNull(node);
1431
        assertEquals(0, size);
1432
    }
1433

    
1434

    
1435
    @Test
1436
    @DataSet(value="BlankDataSet.xml")
1437
    public final void testTaxonDeletionConfiguratorDeleteMarker(){
1438
        //test childHandling DELETE:
1439
        Taxon testTaxon = TaxonGenerator.getTestTaxon();
1440
        UUID uuid = service.save(testTaxon).getUuid();
1441

    
1442
        Taxon topMost = Taxon.NewInstance(BotanicalName.NewInstance(Rank.FAMILY()), null);
1443

    
1444
        Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1445
        TaxonNode node =nodes.next();
1446
        Classification classification = node.getClassification();
1447
        classification.addParentChild(topMost, testTaxon, null, null);
1448
        UUID topMostUUID = service.save(topMost).getUuid();
1449
        Marker marker = Marker.NewInstance(testTaxon, true, MarkerType.IS_DOUBTFUL());
1450
        testTaxon.addMarker(marker);
1451
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1452
        config.getTaxonNodeConfig().setChildHandling(ChildHandling.DELETE);
1453

    
1454
        DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, null);
1455

    
1456
        if(!result.isOk()){
1457
         	Assert.fail();
1458
       	}
1459
        commitAndStartNewTransaction(null);
1460
        Taxon tax = (Taxon)service.find(uuid);
1461
        assertNull(tax);
1462
        tax = (Taxon)service.find(topMostUUID);
1463
        Set<TaxonNode> topMostNodes = tax.getTaxonNodes();
1464
        assertNotNull(topMostNodes);
1465
        assertEquals("there should be one taxon node", 1, topMostNodes.size());
1466
        nodes = topMostNodes.iterator();
1467
        TaxonNode topMostNode = nodes.next();
1468
        int size = topMostNode.getChildNodes().size();
1469

    
1470
        assertEquals(0, size);
1471
    }
1472

    
1473

    
1474
    @Test
1475
    @DataSet(value="BlankDataSet.xml")
1476
    public final void testTaxonDeletionConfiguratorTaxonWithMisappliedName(){
1477

    
1478
        Taxon testTaxon = TaxonGenerator.getTestTaxon();
1479
        UUID uuid = service.save(testTaxon).getUuid();
1480

    
1481
        Taxon misappliedName = Taxon.NewInstance(BotanicalName.NewInstance(Rank.GENUS()), null);
1482

    
1483
        Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1484
        TaxonNode node =nodes.next();
1485
        testTaxon.addMisappliedName(misappliedName, null, null);
1486
        UUID misappliedNameUUID = service.save(misappliedName).getUuid();
1487

    
1488
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1489
        config.setDeleteMisappliedNamesAndInvalidDesignations(true);
1490

    
1491
        DeleteResult result  = service.deleteTaxon(testTaxon.getUuid(), config, null);
1492
        if(!result.isOk()){
1493
         	Assert.fail();
1494
       	}
1495
        commitAndStartNewTransaction(null);
1496
        Taxon tax = (Taxon)service.find(uuid);
1497
        assertNull(tax);
1498
        tax = (Taxon)service.find(misappliedNameUUID);
1499
        //TODO: is that correct or should it be deleted because there is no relation to anything
1500
        assertNull(tax);
1501

    
1502
    }
1503
    @Test
1504
    @DataSet(value="BlankDataSet.xml")
1505
    public final void testTaxonDeletionConfiguratorTaxonWithMisappliedNameDoNotDelete(){
1506

    
1507
        Taxon testTaxon = TaxonGenerator.getTestTaxon();
1508
        UUID uuid = service.save(testTaxon).getUuid();
1509

    
1510
        Taxon misappliedName = Taxon.NewInstance(BotanicalName.NewInstance(Rank.GENUS()), null);
1511

    
1512
        Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1513
        TaxonNode node =nodes.next();
1514
        testTaxon.addMisappliedName(misappliedName, null, null);
1515
        UUID misappliedNameUUID = service.save(misappliedName).getUuid();
1516

    
1517
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1518
        config.setDeleteMisappliedNamesAndInvalidDesignations(false);
1519

    
1520
        DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, null);
1521
        if(!result.isOk()){
1522
         	Assert.fail();
1523
       	}
1524
        commitAndStartNewTransaction(null);
1525
        Taxon tax = (Taxon)service.find(uuid);
1526
        assertNull(tax);
1527
        tax = (Taxon)service.find(misappliedNameUUID);
1528
        //TODO: is that correct or should it be deleted because there is no relation to anything
1529
        assertNotNull(tax);
1530

    
1531
    }
1532

    
1533
    @Test
1534
    @DataSet(value="BlankDataSet.xml")
1535
    public final void testTaxonDeletionConfiguratorTaxonMisappliedName(){
1536

    
1537
        Taxon testTaxon = TaxonGenerator.getTestTaxon();
1538
        UUID uuid = service.save(testTaxon).getUuid();
1539

    
1540
        Taxon misappliedNameTaxon = Taxon.NewInstance(BotanicalName.NewInstance(Rank.GENUS()), null);
1541

    
1542
        Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1543
        TaxonNode node =nodes.next();
1544
        testTaxon.addMisappliedName(misappliedNameTaxon, null, null);
1545
        UUID misappliedNameUUID = service.save(misappliedNameTaxon).getUuid();
1546
        misappliedNameTaxon = (Taxon)service.find(misappliedNameUUID);
1547
        UUID misNameUUID = misappliedNameTaxon.getName().getUuid();
1548

    
1549
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1550

    
1551

    
1552
       // try {
1553
            service.deleteTaxon(misappliedNameTaxon.getUuid(), config, null);
1554
       // } catch (DataChangeNoRollbackException e) {
1555
         //   e.printStackTrace();
1556

    
1557
        //}
1558

    
1559
        commitAndStartNewTransaction(null);
1560
        Taxon tax = (Taxon)service.find(uuid);
1561
        assertNotNull(tax);
1562
        tax = (Taxon)service.find(misappliedNameUUID);
1563
        BotanicalName name = (BotanicalName) nameService.find(misNameUUID);
1564

    
1565
        assertNull(tax);
1566
        assertNull(name);
1567

    
1568
    }
1569

    
1570
    @Test
1571
    @DataSet(value="BlankDataSet.xml")
1572
    public final void testLlistIncludedTaxa(){
1573
    	Reference<?> citation = null;
1574
    	String microcitation = null;
1575

    
1576
    	//Data
1577
    	Classification cl1 = Classification.NewInstance("testClassification1");
1578
    	Classification cl2 = Classification.NewInstance("testClassification2");
1579
    	Classification cl3 = Classification.NewInstance("testClassification3");
1580

    
1581
    	Taxon c1Genus = Taxon.NewInstance(null, null);c1Genus.setUuid(UUID.fromString("daa24f6f-7e38-4668-b385-10c789212e4e"));
1582
    	Taxon c1Species = Taxon.NewInstance(null, null);c1Species.setUuid(UUID.fromString("1c1d0566-67d0-4806-bf23-ecf55f4b9118"));
1583
    	Taxon c1SubSpecies1 = Taxon.NewInstance(null, null);c1SubSpecies1.setUuid(UUID.fromString("96ae2fad-76df-429f-b179-42e00838fea4"));
1584
    	Taxon c1SubSpecies2 = Taxon.NewInstance(null, null);c1SubSpecies2.setUuid(UUID.fromString("5d3f6147-ca72-40e0-be8a-6c835a09a579"));
1585
    	cl1.addParentChild(c1Genus, c1Species, null, null);
1586
    	cl1.addParentChild(c1Species, c1SubSpecies1, null, null);
1587
    	cl1.addParentChild(c1Species, c1SubSpecies2, null, null);
1588

    
1589
    	Taxon c2Genus = Taxon.NewInstance(null, null);c2Genus.setUuid(UUID.fromString("ed0ec006-3ac8-4a12-ae13-fdf2a13dedbe"));
1590
    	Taxon c2Species = Taxon.NewInstance(null, null);c2Species.setUuid(UUID.fromString("1027eb18-1c26-450e-a299-981b775ebc3c"));
1591
    	Taxon c2SubSpecies1 = Taxon.NewInstance(null, null);c2SubSpecies1.setUuid(UUID.fromString("61f039c8-01f3-4f5d-8e16-1602139774e7"));
1592
    	Taxon c2SubSpecies2 = Taxon.NewInstance(null, null);c2SubSpecies2.setUuid(UUID.fromString("2ed6b6f8-05f9-459a-a075-2bca57e3013e"));
1593
    	cl2.addParentChild(c2Genus, c2Species, null, null);
1594
    	cl2.addParentChild(c2Species, c2SubSpecies1, null, null);
1595
    	cl2.addParentChild(c2Species, c2SubSpecies2, null, null);
1596

    
1597
    	Taxon c3Genus = Taxon.NewInstance(null, null);c3Genus.setUuid(UUID.fromString("407dfc8d-7a4f-4370-ada4-76c1a8279d1f"));
1598
    	Taxon c3Species = Taxon.NewInstance(null, null);c3Species.setUuid(UUID.fromString("b6d34fc7-4aa7-41e5-b633-86f474edbbd5"));
1599
    	Taxon c3SubSpecies1 = Taxon.NewInstance(null, null);c3SubSpecies1.setUuid(UUID.fromString("01c07585-a422-40cd-9339-a74c56901d9f"));
1600
    	Taxon c3SubSpecies2 = Taxon.NewInstance(null, null);c3SubSpecies2.setUuid(UUID.fromString("390c8e23-e05f-4f89-b417-50cf080f4c91"));
1601
    	cl3.addParentChild(c3Genus, c3Species, null, null);
1602
    	cl3.addParentChild(c3Species, c3SubSpecies1, null, null);
1603
    	cl3.addParentChild(c3Species, c3SubSpecies2, null, null);
1604

    
1605
    	classificationService.save(cl1);
1606
    	classificationService.save(cl2);
1607
    	classificationService.save(cl3);
1608

    
1609
      	Taxon c4Genus = Taxon.NewInstance(null, null);c4Genus.setUuid(UUID.fromString("bfd6bbdd-0116-4ab2-a781-9316224aad78"));
1610
    	Taxon c4Species = Taxon.NewInstance(null, null);c4Species.setUuid(UUID.fromString("9347a3d9-5ece-4d64-9035-e8aaf5d3ee02"));
1611
    	Taxon c4SubSpecies = Taxon.NewInstance(null, null);c4SubSpecies.setUuid(UUID.fromString("777aabbe-4c3a-449c-ab99-a91f2fec9f07"));
1612

    
1613
    	TaxonRelationship rel = c1Species.addTaxonRelation(c2Species, TaxonRelationshipType.CONGRUENT_TO(), citation, microcitation);
1614
    	rel.setDoubtful(true);
1615
    	c1Species.addTaxonRelation(c4Species, TaxonRelationshipType.INCLUDES(), citation, microcitation);
1616
    	c2Species.addTaxonRelation(c1SubSpecies2, TaxonRelationshipType.INCLUDES(), citation, microcitation);
1617

    
1618
    	service.saveOrUpdate(c1Species);
1619
       	service.saveOrUpdate(c2Species);
1620

    
1621
    	//Tests
1622
       	//default starting at species 1
1623
       	IncludedTaxaDTO dto = service.listIncludedTaxa(c1Species.getUuid(), new IncludedTaxonConfiguration(null, true, false));
1624
    	Assert.assertNotNull("IncludedTaxaDTO", dto);
1625
    	Assert.assertEquals("Result should contain 7 taxa: c1Species", 7, dto.getIncludedTaxa().size());
1626
    	Assert.assertNotNull("date should not be null", dto.getDate());
1627
//    	Assert.assertTrue(dto.contains(taxonUuid));
1628
        //same without doubtful
1629
    	dto = service.listIncludedTaxa(c1Species.getUuid(), new IncludedTaxonConfiguration(null, false, false));
1630
    	Assert.assertEquals(4, dto.getIncludedTaxa().size());
1631

    
1632
    	//other example starting at Genus2
1633
    	dto = service.listIncludedTaxa(c2Genus.getUuid(), new IncludedTaxonConfiguration(null, true, false));
1634
    	Assert.assertEquals(8, dto.getIncludedTaxa().size());
1635
    	//same without doubtful
1636
    	dto = service.listIncludedTaxa(c2Genus.getUuid(), new IncludedTaxonConfiguration(null, false, false));
1637
    	Assert.assertEquals(5, dto.getIncludedTaxa().size());
1638

    
1639
    	//only congruent
1640
    	dto = service.listIncludedTaxa(c1Species.getUuid(), new IncludedTaxonConfiguration(null, true, true));
1641
    	Assert.assertEquals(2, dto.getIncludedTaxa().size());
1642
    	//same without doubtful
1643
    	dto = service.listIncludedTaxa(c1Species.getUuid(), new IncludedTaxonConfiguration(null, false, true));
1644
    	Assert.assertEquals(1, dto.getIncludedTaxa().size());
1645

    
1646

    
1647

    
1648

    
1649
    }
1650

    
1651
    @Test
1652
    public void testDeleteDescriptions(){
1653
    	try {
1654
			createTestDataSet();
1655
		} catch (FileNotFoundException e) {
1656
			// TODO Auto-generated catch block
1657
			e.printStackTrace();
1658
		}
1659
    	TaxonDescription description = TaxonDescription.NewInstance(taxWithoutSyn);
1660
    	SpecimenOrObservationBase<IIdentifiableEntityCacheStrategy<FieldUnit>> specimen = FieldUnit.NewInstance();
1661
    	UUID uuid = occurenceService.saveOrUpdate(specimen);
1662
    	DescriptionElementBase element = IndividualsAssociation.NewInstance(specimen);
1663
    	description.addElement(element);
1664
    	service.saveOrUpdate(taxWithoutSyn);
1665

    
1666

    
1667
    	Taxon tax = (Taxon)service.find(uuidTaxWithoutSyn);
1668
    	Set<TaxonDescription> descr =  tax.getDescriptions();
1669
    	assertEquals(1, descr.size());
1670
    	description = descr.iterator().next();
1671
    	UUID uuidDescr = description.getUuid();
1672
    	UUID uuidDescEl = description.getElements().iterator().next().getUuid();
1673

    
1674
    	descriptionService.deleteDescription(description);
1675
    	service.saveOrUpdate(tax);
1676

    
1677
    	description = (TaxonDescription) descriptionService.find(uuidDescr);
1678
    	specimen = occurenceService.find(uuid);
1679
    	assertNull(description);
1680
    	DeleteResult result = occurenceService.delete(specimen);
1681
    	assertTrue(result.isOk());
1682

    
1683
    }
1684

    
1685
    @Test
1686
    public void testRemoveDescriptionsFromTaxa(){
1687
        try {
1688
            createTestDataSet();
1689
        } catch (FileNotFoundException e) {
1690
            // TODO Auto-generated catch block
1691
            e.printStackTrace();
1692
        }
1693
        TaxonDescription description = TaxonDescription.NewInstance(taxWithoutSyn);
1694
        SpecimenOrObservationBase<IIdentifiableEntityCacheStrategy<FieldUnit>> specimen = FieldUnit.NewInstance();
1695
        UUID uuid = occurenceService.saveOrUpdate(specimen);
1696
        DescriptionElementBase element = IndividualsAssociation.NewInstance(specimen);
1697
        description.addElement(element);
1698
        service.saveOrUpdate(taxWithoutSyn);
1699

    
1700

    
1701
        Taxon tax = (Taxon)service.find(uuidTaxWithoutSyn);
1702
        Set<TaxonDescription> descr =  tax.getDescriptions();
1703
        assertEquals(1, descr.size());
1704
        description = descr.iterator().next();
1705
        UUID uuidDescr = description.getUuid();
1706

    
1707

    
1708
        tax.removeDescription(description, true);
1709
        service.saveOrUpdate(tax);
1710

    
1711
        description = (TaxonDescription) descriptionService.find(uuidDescr);
1712
        specimen = occurenceService.find(uuid);
1713
        assertNotNull(description);
1714
        DeleteResult result = occurenceService.delete(specimen);
1715
        assertTrue(result.isOk());
1716

    
1717
    }
1718

    
1719

    
1720
    /* (non-Javadoc)
1721
     * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
1722
     */
1723
    @Override
1724
    public void createTestDataSet() throws FileNotFoundException {
1725
    	Rank rank = Rank.SPECIES();
1726

    
1727
        taxWithoutSyn = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test1", null, null, null, null, null, null, null), null);
1728
        taxWithSyn = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test3", null, null, null, null, null, null, null), null);
1729
        tax2WithSyn = Taxon.NewInstance(BotanicalName.NewInstance(rank, "Test5", null, null, null, null, null, null, null), null);
1730
        synonym = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test2", null, null, null, null, null, null, null), null);
1731
        synonym2 = Synonym.NewInstance(BotanicalName.NewInstance(rank, "Test4", null, null, null, null, null, null, null), null);
1732
        synonym2.getName().setHomotypicalGroup(synonym.getHomotypicGroup());
1733

    
1734
        taxWithSyn.addSynonym(synonym, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
1735
        taxWithSyn.addSynonym(synonym2, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
1736

    
1737
        uuidTaxWithoutSyn = service.save(taxWithoutSyn).getUuid();
1738
        uuidSyn = service.save(synonym).getUuid();
1739
        uuidSyn2 = service.save(synonym2).getUuid();
1740
        uuidTaxWithSyn =service.save(taxWithSyn).getUuid();
1741

    
1742
    }
1743

    
1744

    
1745
}
1746

    
1747

    
1748

    
(25-25/31)