Project

General

Profile

Download (99 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.Random;
22
import java.util.Set;
23
import java.util.UUID;
24

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

    
31
import eu.etaxonomy.cdm.api.service.config.IdentifiableServiceConfiguratorImpl;
32
import eu.etaxonomy.cdm.api.service.config.IncludedTaxonConfiguration;
33
import eu.etaxonomy.cdm.api.service.config.MatchingTaxonConfigurator;
34
import eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator;
35
import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
36
import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
37
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
38
import eu.etaxonomy.cdm.api.service.dto.IncludedTaxaDTO;
39
import eu.etaxonomy.cdm.api.service.exception.HomotypicalGroupChangeException;
40
import eu.etaxonomy.cdm.api.service.pager.Pager;
41
import eu.etaxonomy.cdm.model.agent.Person;
42
import eu.etaxonomy.cdm.model.common.Annotation;
43
import eu.etaxonomy.cdm.model.common.CdmBase;
44
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
45
import eu.etaxonomy.cdm.model.common.Language;
46
import eu.etaxonomy.cdm.model.common.LanguageString;
47
import eu.etaxonomy.cdm.model.common.Marker;
48
import eu.etaxonomy.cdm.model.common.MarkerType;
49
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
50
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
51
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
52
import eu.etaxonomy.cdm.model.description.TaxonDescription;
53
import eu.etaxonomy.cdm.model.description.TextData;
54
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
55
import eu.etaxonomy.cdm.model.name.IBotanicalName;
56
import eu.etaxonomy.cdm.model.name.INonViralName;
57
import eu.etaxonomy.cdm.model.name.NameRelationship;
58
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
59
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
60
import eu.etaxonomy.cdm.model.name.Rank;
61
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
62
import eu.etaxonomy.cdm.model.name.TaxonName;
63
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
64
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
65
import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
66
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
67
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
68
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
69
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
70
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
71
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
72
import eu.etaxonomy.cdm.model.reference.Reference;
73
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
74
import eu.etaxonomy.cdm.model.taxon.Classification;
75
import eu.etaxonomy.cdm.model.taxon.Synonym;
76
import eu.etaxonomy.cdm.model.taxon.SynonymType;
77
import eu.etaxonomy.cdm.model.taxon.Taxon;
78
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
79
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
80
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
81
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
82
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
83
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
84
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
85

    
86
/**
87
 * @author a.mueller
88
 */
89
public class TaxonServiceImplTest extends CdmTransactionalIntegrationTest {
90
    private static final Logger logger = Logger.getLogger(TaxonServiceImplTest.class);
91

    
92
    @SpringBeanByType
93
    private ITaxonService service;
94

    
95
    @SpringBeanByType
96
    private INameService nameService;
97

    
98
    @SpringBeanByType
99
    private IReferenceService referenceService;
100

    
101
    @SpringBeanByType
102
    private IClassificationService classificationService;
103

    
104
    @SpringBeanByType
105
    private ITaxonNodeService nodeService;
106

    
107
    @SpringBeanByType
108
    private IDescriptionService descriptionService;
109

    
110
    @SpringBeanByType
111
    private IMarkerService markerService;
112

    
113
    @SpringBeanByType
114
    private IEventBaseService eventService;
115

    
116
    @SpringBeanByType
117
    private IOccurrenceService occurenceService;
118

    
119
    private Synonym synonym;
120
    private Synonym synonym2;
121

    
122
    private Taxon taxWithSyn;
123
    private Taxon tax2WithSyn;
124
    private Taxon taxWithoutSyn;
125
    private UUID uuidSyn;
126
    private UUID uuidTaxWithoutSyn;
127
    private UUID uuidSyn2;
128
    private UUID uuidTaxWithSyn;
129

    
130
    private static String[] genera = {"Carex", "Abies", "Belladonna", "Dracula", "Maria", "Calendula", "Polygala", "Vincia"};
131
    private static String[] epitheta = {"vulgaris", "magdalena", "officinalis", "alba", "negra", "communa", "alpina", "rotundifolia", "greutheriana", "helventica", "allemania", "franca"};
132
    private static String[] ranks = {"subsp", "var", "f"};
133

    
134
    public static UUID GENUS_NAME_UUID = UUID.fromString("8d761fc4-b509-42f4-9568-244161934336");
135
    public static UUID GENUS_UUID = UUID.fromString("bf4298a8-1735-4353-a210-244442e1bd62");
136
    public static UUID BASIONYM_UUID = UUID.fromString("7911c51d-ccb7-4708-8992-639eae58a0e3");
137
    public static UUID SPECIES1_UUID = UUID.fromString("f0eb77d9-76e0-47f4-813f-9b5605b78685");
138
    public static UUID SPECIES1_NAME_UUID = UUID.fromString("efd78713-126f-42e1-9070-a1ff83f12abf");
139
    public static UUID SYNONYM_NAME_UUID = UUID.fromString("b9cbaa74-dbe0-4930-8050-b7754ce85dc0");
140
    public static UUID SPECIES2_NAME_UUID = UUID.fromString("0267ab67-483e-4da5-b654-11013b242c22");
141
    public static UUID SPECIES2_UUID = UUID.fromString("e20eb549-ced6-4e79-9d74-44f0792a4929");
142
    public static UUID SYNONYM2_NAME_UUID = UUID.fromString("7c17c811-4201-454b-8108-7be7c91c0938");
143
    public static UUID SPECIES5_NAME_UUID = UUID.fromString("0c6ecaac-804d-49e5-a33f-1b7ee77439e3");
144

    
145
/****************** TESTS *****************************/
146

    
147

    
148
    /**
149
     * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#getTaxonByUuid(java.util.UUID)}.
150
     */
151
    @Test
152
    public final void testGetTaxonByUuid() {
153
        Taxon expectedTaxon = Taxon.NewInstance(null, null);
154
        UUID uuid = service.save(expectedTaxon).getUuid();
155
        TaxonBase<?> actualTaxon = service.find(uuid);
156
        assertEquals(expectedTaxon, actualTaxon);
157
    }
158

    
159
    /**
160
     * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#getTaxonByUuid(java.util.UUID)}.
161
     */
162
    @Test
163
    public final void testGetTaxonByTitle() {
164
        TaxonName name = TaxonName.NewInstance(NomenclaturalCode.ICNAFP, Rank.SPECIES(), "Abies", null, "alba", null, null, null, null, null);
165
        Taxon expectedTaxon = Taxon.NewInstance(name, null);
166
        expectedTaxon.setDoubtful(true);
167
        TaxonBase taxon = service.save(expectedTaxon);
168
        IdentifiableServiceConfiguratorImpl<TaxonBase> config = new IdentifiableServiceConfiguratorImpl<TaxonBase>();
169
        config.setTitleSearchString("Abies alba*");
170
        //doubtful taxa should be found
171
        Pager<TaxonBase> actualTaxa = service.findByTitle(config);
172
        assertEquals(expectedTaxon, actualTaxa.getRecords().get(0));
173

    
174
        //and other taxa as well
175
        expectedTaxon.setDoubtful(false);
176
        service.saveOrUpdate(expectedTaxon);
177
        actualTaxa = service.findByTitle(config);
178
        assertEquals(expectedTaxon, actualTaxa.getRecords().get(0));
179
    }
180

    
181

    
182
    /**
183
     * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#saveTaxon(eu.etaxonomy.cdm.model.taxon.TaxonBase)}.
184
     */
185
    @Test
186
    public final void testSaveTaxon() {
187
        Taxon expectedTaxon = Taxon.NewInstance(null, null);
188
        UUID uuid = service.save(expectedTaxon).getUuid();
189
        TaxonBase<?> actualTaxon = service.find(uuid);
190
        assertEquals(expectedTaxon, actualTaxon);
191
    }
192

    
193
    @Test
194
    public final void testSaveOrUpdateTaxon() {
195
        Taxon expectedTaxon = Taxon.NewInstance(null, null);
196
        UUID uuid = service.save(expectedTaxon).getUuid();
197
        TaxonBase<?> actualTaxon = service.find(uuid);
198
        assertEquals(expectedTaxon, actualTaxon);
199

    
200
        actualTaxon.setName(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()));
201
        try{
202
            service.saveOrUpdate(actualTaxon);
203
        }catch(Exception e){
204
            Assert.fail();
205
        }
206
    }
207

    
208
    @Test
209
    public final void testSaveOrUpdateTaxonWithMisappliedName() {
210
        Taxon expectedTaxon = Taxon.NewInstance(null, null);
211
        TaxonName misappliedNameName = TaxonName.NewInstance(NomenclaturalCode.ICNAFP, Rank.SPECIES(), "Abies", null, "alba", null, null, null, null, null);
212

    
213
        UUID misappliedNameNameUuid = nameService.save(misappliedNameName).getUuid();
214
        misappliedNameName = nameService.find(misappliedNameNameUuid);
215
        SpecimenTypeDesignation typedes = SpecimenTypeDesignation.NewInstance();
216
        DerivedUnit derivedUnit = DerivedUnit.NewPreservedSpecimenInstance();
217
        FieldUnit fieldUnit = FieldUnit.NewInstance();
218
        DerivationEvent derivationEvent = DerivationEvent.NewSimpleInstance(fieldUnit, derivedUnit, DerivationEventType.ACCESSIONING());
219
//        derivedUnit.addDerivationEvent(derivationEvent);
220
        typedes.setTypeSpecimen(derivedUnit);
221
        misappliedNameName.addTypeDesignation(typedes, false);
222
        Taxon misappliedName = Taxon.NewInstance(misappliedNameName, null);
223
        UUID misappliedNameUuid = service.save(misappliedName).getUuid();
224
        misappliedName = (Taxon) service.find(misappliedNameUuid);
225
        expectedTaxon.addMisappliedName(misappliedName, null, null);
226
        UUID uuid = service.save(expectedTaxon).getUuid();
227
        TaxonBase<?> actualTaxon = service.find(uuid);
228
        assertEquals(expectedTaxon, actualTaxon);
229
        misappliedName.setSec(ReferenceFactory.newArticle());
230

    
231
        try{
232
            service.saveOrUpdate(actualTaxon);
233
            misappliedName = (Taxon)service.find(misappliedNameUuid);
234
            Assert.assertNotNull(misappliedName.getSec());
235
        }catch(Exception e){
236
            Assert.fail();
237
        }
238
        commitAndStartNewTransaction(null);
239
        actualTaxon = service.find(uuid);
240
        ((Taxon)actualTaxon).getTaxonRelations(misappliedName).iterator().next().getFromTaxon().setSec(null);
241
        try{
242
            service.saveOrUpdate(actualTaxon);
243
            misappliedName = (Taxon)service.find(misappliedNameUuid);
244
            Assert.assertNull(misappliedName.getSec());
245
        }catch(Exception e){
246
            Assert.fail();
247
        }
248
    }
249

    
250
    /**
251
     * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#removeTaxon(eu.etaxonomy.cdm.model.taxon.TaxonBase)}.
252
     */
253
    @Test
254
    public final void testRemoveTaxon() {
255
        Taxon taxon = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.UNKNOWN_RANK()), null);
256
        UUID uuid = service.save(taxon).getUuid();
257
       // try {
258
			service.deleteTaxon(taxon.getUuid(), null, null);
259
		/*} catch (DataChangeNoRollbackException e) {
260
			// TODO Auto-generated catch block
261
			e.printStackTrace();
262
		}*/
263
        TaxonBase<?> actualTaxon = service.find(uuid);
264
        assertNull(actualTaxon);
265
    }
266

    
267

    
268
    @Test
269
    public final void testMakeTaxonSynonym() {
270
        try {
271
			createTestDataSet();
272
		} catch (FileNotFoundException e) {
273
			// TODO Auto-generated catch block
274
			e.printStackTrace();
275
		}
276

    
277
        UpdateResult result = service.swapSynonymAndAcceptedTaxon(synonym, taxWithSyn, true);
278

    
279
        // find forces flush
280
        Taxon tax = (Taxon)service.find(result.getCdmEntity().getUuid());
281
        MatchingTaxonConfigurator configurator = MatchingTaxonConfigurator.NewInstance();
282
        configurator.setTaxonNameTitle("Test3");
283
        List<TaxonBase> synList = service.findTaxaByName(configurator);
284
        HomotypicalGroup groupTest2 = null;
285
        if (synList.size() > 0){
286
            TaxonBase syn = synList.get(0);
287
            groupTest2 = syn.getHomotypicGroup();
288
            assertTrue(tax.getSynonyms().contains(syn));
289
        }else{
290
            Assert.fail("There should be a synonym with name Test3");
291
        }
292

    
293
        assertTrue(tax.getName().getTitleCache().equals("Test2"));
294

    
295

    
296

    
297
    }
298

    
299
    @Test
300
    public final void testChangeSynonymToAcceptedTaxon(){
301
    	try {
302
			createTestDataSet();
303
		} catch (FileNotFoundException e1) {
304
			// TODO Auto-generated catch block
305
			e1.printStackTrace();
306
		}
307

    
308

    
309
        Taxon taxon = null;
310
        UpdateResult result = new UpdateResult();
311
        try {
312
            result = service.changeSynonymToAcceptedTaxon(synonym, taxWithSyn, true);
313
        } catch (HomotypicalGroupChangeException e) {
314
            Assert.fail("Invocation of change method should not throw an exception");
315
        }
316
        taxWithSyn = null;
317
        //test flush (resave deleted object)
318
        TaxonBase<?> syn = service.find(uuidSyn);
319
        taxWithSyn = (Taxon)service.find(uuidTaxWithSyn);
320
        Taxon taxNew = (Taxon)service.find(result.getCdmEntity().getUuid());
321
        assertNull(syn);
322
        assertNotNull(taxWithSyn);
323
        assertNotNull(taxNew);
324

    
325
        Assert.assertEquals("New taxon should have 1 synonym relationship (the old homotypic synonym)", 1, ((Taxon)result.getCdmEntity()).getSynonyms().size());
326
    }
327

    
328

    
329

    
330
    @Test
331
    public final void testChangeSynonymToAcceptedTaxonSynonymForTwoTaxa(){
332
        try {
333
			createTestDataSet();
334
		} catch (FileNotFoundException e1) {
335
			// TODO Auto-generated catch block
336
			e1.printStackTrace();
337
		}
338

    
339

    
340
        Taxon taxon = null;
341
        UpdateResult result = new UpdateResult();
342
        try {
343
            result = service.changeSynonymToAcceptedTaxon(synonym, taxWithSyn, true);
344
            service.save(taxon);
345
        } catch (HomotypicalGroupChangeException e) {
346
            Assert.fail("Invocation of change method should not throw an exception");
347
        }
348
        taxWithSyn = null;
349
        tax2WithSyn = null;
350

    
351
        //test flush (resave deleted object)
352
        TaxonBase<?> syn = service.find(uuidSyn);
353
        taxWithSyn = (Taxon)service.find(uuidTaxWithSyn);
354
        Taxon taxNew = (Taxon)service.find(((Taxon)result.getCdmEntity()).getUuid());
355
        assertNull(syn);
356
        assertNotNull(taxWithSyn);
357
        assertNotNull(taxNew);
358

    
359
       // Assert.assertEquals("New taxon should have 1 synonym relationship (the old homotypic synonym)", 1, taxon.getSynonymRelations().size());
360
    }
361

    
362
    /**
363
     * Old implementation taken from {@link TaxonServiceImplBusinessTest} for old version of method.
364
     */
365
    @Test
366
    public final void testMoveSynonymToAnotherTaxon_OLD() {
367
        SynonymType heteroTypicSynonymType = SynonymType.HETEROTYPIC_SYNONYM_OF();
368
        Reference reference = ReferenceFactory.newGeneric();
369
        String referenceDetail = "test";
370

    
371
        INonViralName t1n = TaxonNameFactory.NewNonViralInstance(null);
372
        Taxon t1 = Taxon.NewInstance(t1n, reference);
373
        INonViralName t2n = TaxonNameFactory.NewNonViralInstance(null);
374
        Taxon t2 = Taxon.NewInstance(t2n, reference);
375
        INonViralName s1n = TaxonNameFactory.NewNonViralInstance(null);
376
        Synonym s1 = Synonym.NewInstance(s1n, reference);
377
        t1.addSynonym(s1, heteroTypicSynonymType);
378
        service.saveOrUpdate(t1);
379

    
380
        Synonym synonym = t1.getSynonyms().iterator().next();
381

    
382
        boolean keepReference = false;
383
        boolean moveHomotypicGroup = false;
384
        try {
385
            service.moveSynonymToAnotherTaxon(synonym, t2, moveHomotypicGroup, heteroTypicSynonymType, reference, referenceDetail, keepReference);
386
        } catch (HomotypicalGroupChangeException e) {
387
            Assert.fail("Method call should not throw exception");
388
        }
389

    
390
        Assert.assertTrue("t1 should have no synonyms", t1.getSynonyms().isEmpty());
391

    
392
        Set<Synonym> synonyms = t2.getSynonyms();
393
        Assert.assertTrue("t2 should have exactly one synonym", synonyms.size() == 1);
394

    
395
        synonym = synonyms.iterator().next();
396

    
397
        Assert.assertEquals(t2, synonym.getAcceptedTaxon());
398
        Assert.assertEquals(heteroTypicSynonymType, synonym.getType());
399
        Assert.assertEquals(reference, synonym.getSec());
400
        Assert.assertEquals(referenceDetail, synonym.getSecMicroReference());
401
    }
402

    
403
    @Test
404
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testMoveSynonymToAnotherTaxon.xml")
405
    public final void testMoveSynonymToAnotherTaxon() throws Exception {
406
        final String[] tableNames = new String[]{};
407

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

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

    
421

    
422
        boolean moveHomotypicGroup = true;
423
        SynonymType newSynonymType = null;
424
        boolean keepReference = true;
425
        Reference newReference = null;
426
        String newReferenceDetail = null;
427

    
428
        Taxon newTaxon = (Taxon)service.load(uuidNewTaxon);
429
        Synonym homotypicSynonym = (Synonym)service.load(uuidSyn1);
430
        Assert.assertNotNull("Synonym should exist", homotypicSynonym);
431
        Assert.assertNotNull("Synonym should have 1 relation", homotypicSynonym.getAcceptedTaxon());
432
        Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, homotypicSynonym.getAcceptedTaxon().getUuid());
433
        Taxon oldTaxon = homotypicSynonym.getAcceptedTaxon();
434

    
435
        try {
436
            service.moveSynonymToAnotherTaxon(homotypicSynonym, newTaxon, moveHomotypicGroup, newSynonymType, newReference, newReferenceDetail, keepReference);
437
            Assert.fail("Homotypic synonym move to other taxon should throw an exception");
438
        } catch (HomotypicalGroupChangeException e) {
439
            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")){
440
                //OK
441
                commitAndStartNewTransaction(tableNames);
442
            }else{
443
                Assert.fail("Unexpected exception occurred: " + e.getMessage());
444
            }
445
        }
446
        //Asserts
447
        homotypicSynonym = (Synonym)service.load(uuidSyn1);
448
        Assert.assertNotNull("Synonym should still exist", homotypicSynonym);
449
        Assert.assertNotNull("Synonym should still have 1 relation", homotypicSynonym.getAcceptedTaxon());
450
        Assert.assertEquals("Accepted taxon of single relation should be the old taxon", oldTaxon, homotypicSynonym.getAcceptedTaxon());
451

    
452
        //test heterotypic synonym with other synonym in homotypic group
453
        newTaxon = (Taxon)service.load(uuidNewTaxon);
454
        Synonym heterotypicSynonym = (Synonym)service.load(uuidSyn3);
455
        Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
456
        Assert.assertNotNull("Synonym should have 1 relation", heterotypicSynonym.getAcceptedTaxon());
457
        Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, heterotypicSynonym.getAcceptedTaxon().getUuid());
458
        oldTaxon = heterotypicSynonym.getAcceptedTaxon();
459
        moveHomotypicGroup = false;
460

    
461
        try {
462
            service.moveSynonymToAnotherTaxon(heterotypicSynonym, newTaxon, moveHomotypicGroup, newSynonymType, newReference, newReferenceDetail, keepReference);
463
            Assert.fail("Heterotypic synonym move to other taxon should throw an exception");
464
        } catch (HomotypicalGroupChangeException e) {
465
            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")){
466
                //OK
467
                commitAndStartNewTransaction(tableNames);
468
            }else{
469
                Assert.fail("Unexpected exception occurred: " + e.getMessage());
470
            }
471
        }
472
        //Asserts
473
        heterotypicSynonym = (Synonym)service.load(uuidSyn3);
474
        Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
475
        Assert.assertNotNull("Synonym should have accepted taxon", heterotypicSynonym.getAcceptedTaxon());
476
        Assert.assertEquals("Accepted taxon of single relation should still be the old taxon", oldTaxon, heterotypicSynonym.getAcceptedTaxon());
477

    
478

    
479
        //test heterotypic synonym with no other synonym in homotypic group
480
        //+ keep reference
481

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

    
484
        newTaxon = (Taxon)service.load(uuidNewTaxon);
485
        heterotypicSynonym = (Synonym)service.load(uuidSyn5);
486
        Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
487
        Assert.assertNotNull("Synonym should have accepted taxon", heterotypicSynonym.getAcceptedTaxon());
488
        Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, heterotypicSynonym.getAcceptedTaxon().getUuid());
489
        oldTaxon = heterotypicSynonym.getAcceptedTaxon();
490
        moveHomotypicGroup = false;
491

    
492

    
493
        try {
494
            service.moveSynonymToAnotherTaxon(heterotypicSynonym, newTaxon, moveHomotypicGroup, newSynonymType, newReference, newReferenceDetail, keepReference);
495
        } catch (HomotypicalGroupChangeException e) {
496
            Assert.fail("Move of single heterotypic synonym should not throw exception: " + e.getMessage());
497
        }
498
        //Asserts
499
        //FIXME throws exception
500
        commitAndStartNewTransaction(tableNames);
501

    
502
//        printDataSet(System.err, new String[]{"AgentBase", "TaxonBase"});
503
//
504
//      printDataSet(System.err, new String[]{"TaxonBase"});
505

    
506
        heterotypicSynonym = (Synonym)service.load(uuidSyn5);
507

    
508
//      printDataSet(System.err, new String[]{"TaxonBase"});
509
//      System.exit(0);
510

    
511
        Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
512
        Assert.assertNotNull("Synonym should have accepted taxon", heterotypicSynonym.getAcceptedTaxon());
513
        Assert.assertEquals("Accepted taxon of single relation should be new taxon", newTaxon, heterotypicSynonym.getAcceptedTaxon());
514
        Assert.assertEquals("Old detail should be kept", "rel5", heterotypicSynonym.getSecMicroReference());
515

    
516

    
517
        //test heterotypic synonym with other synonym in homotypic group and moveHomotypicGroup="true"
518
        //+ new detail
519
        newTaxon = (Taxon)service.load(uuidNewTaxon);
520
        heterotypicSynonym = (Synonym)service.load(uuidSyn3);
521
        Reference ref1 = referenceService.load(uuidRef1);
522
        Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
523
        Assert.assertNotNull("Synonym should have 1 relation", heterotypicSynonym.getAcceptedTaxon());
524
        Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, heterotypicSynonym.getAcceptedTaxon().getUuid());
525
        oldTaxon = heterotypicSynonym.getAcceptedTaxon();
526
        Assert.assertEquals("Detail should be ref1", ref1, heterotypicSynonym.getSec());
527
        Assert.assertEquals("Detail should be 'rel3'", "rel3", heterotypicSynonym.getSecMicroReference());
528
        TaxonName oldSynName3 = heterotypicSynonym.getName();
529

    
530
        Synonym heterotypicSynonym4 = (Synonym)service.load(uuidSyn4);
531
        Assert.assertNotNull("Synonym should exist", heterotypicSynonym4);
532
        Assert.assertNotNull("Synonym should have accepted taxon", heterotypicSynonym4.getAcceptedTaxon());
533
        Assert.assertEquals("Accepted taxon of other synonym in group should be the old taxon", uuidOldTaxon, heterotypicSynonym4.getAcceptedTaxon().getUuid());
534
        Assert.assertSame("Homotypic group of both synonyms should be same", oldSynName3.getHomotypicalGroup() , heterotypicSynonym4.getName().getHomotypicalGroup() );
535

    
536
        moveHomotypicGroup = true;
537
        keepReference = false;
538

    
539
        try {
540
            service.moveSynonymToAnotherTaxon(heterotypicSynonym4, newTaxon, moveHomotypicGroup, newSynonymType, newReference, newReferenceDetail, keepReference);
541
        } catch (HomotypicalGroupChangeException e) {
542
            Assert.fail("Move with 'moveHomotypicGroup = true' should not throw exception: " + e.getMessage());
543
        }
544
        //Asserts
545
        commitAndStartNewTransaction(tableNames);
546
        heterotypicSynonym = (Synonym)service.load(uuidSyn3);
547
        Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
548
        Assert.assertNotNull("Synonym should still have accepted taxon", heterotypicSynonym.getAcceptedTaxon());
549
        Assert.assertEquals("Accepted taxon of relation should be new taxon now", newTaxon, heterotypicSynonym.getAcceptedTaxon());
550
        TaxonName synName3 = heterotypicSynonym.getName();
551

    
552
        heterotypicSynonym = (Synonym)service.load(uuidSyn4);
553
        Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
554
        Assert.assertNotNull("Synonym should still have accepted taxon", heterotypicSynonym.getAcceptedTaxon());
555
        Assert.assertEquals("Accepted taxon of relation should be new taxon now", newTaxon, heterotypicSynonym.getAcceptedTaxon());
556
        Assert.assertNull("Old citation should be removed", heterotypicSynonym.getSec());
557
        Assert.assertNull("Old detail should be removed", heterotypicSynonym.getSecMicroReference());
558
        TaxonName synName4 = heterotypicSynonym.getName();
559
        Assert.assertEquals("Homotypic group of both synonyms should be equal", synName3.getHomotypicalGroup() , synName4.getHomotypicalGroup() );
560
        Assert.assertSame("Homotypic group of both synonyms should be same", synName3.getHomotypicalGroup() , synName4.getHomotypicalGroup() );
561
        Assert.assertEquals("Homotypic group of both synonyms should be equal to old homotypic group", oldSynName3.getHomotypicalGroup() , synName3.getHomotypicalGroup() );
562

    
563

    
564
        //test single heterotypic synonym to homotypic synonym of new taxon
565
        //+ new reference
566
        newTaxon = (Taxon)service.load(uuidNewTaxon);
567
        Reference ref2 = referenceService.load(uuidRef2);
568
        heterotypicSynonym = (Synonym)service.load(uuidSyn6);
569
        Assert.assertNotNull("Synonym should exist", heterotypicSynonym);
570
        Assert.assertNotNull("Synonym should have accepted taxon", heterotypicSynonym.getAcceptedTaxon());
571
        Assert.assertEquals("Accepted taxon of single relation should be the old taxon", uuidOldTaxon, heterotypicSynonym.getAcceptedTaxon().getUuid());
572
        oldTaxon = heterotypicSynonym.getAcceptedTaxon();
573
        moveHomotypicGroup = false;
574
        keepReference = false;
575
        newReference = ref2;
576
        newReferenceDetail = "newRefDetail";
577
        newSynonymType = SynonymType.HOMOTYPIC_SYNONYM_OF();
578

    
579
        try {
580
            service.moveSynonymToAnotherTaxon(heterotypicSynonym, newTaxon, moveHomotypicGroup, newSynonymType, newReference, newReferenceDetail, keepReference);
581
        } catch (HomotypicalGroupChangeException e) {
582
            Assert.fail("Move of single heterotypic synonym should not throw exception: " + e.getMessage());
583
        }
584
        //Asserts
585
        commitAndStartNewTransaction(tableNames);
586
        heterotypicSynonym = (Synonym)service.load(uuidSyn6);
587
        Assert.assertNotNull("Synonym should still exist", heterotypicSynonym);
588
        Assert.assertNotNull("Synonym should still have accepted taxon", heterotypicSynonym.getAcceptedTaxon());
589
        Assert.assertEquals("Relationship type should be 'homotypic synonym'", newSynonymType, heterotypicSynonym.getType());
590
        Assert.assertEquals("Accepted taxon of single relation should be new taxon", newTaxon, heterotypicSynonym.getAcceptedTaxon());
591
        Assert.assertEquals("New citation should be ref2", ref2 ,heterotypicSynonym.getSec());
592
        Assert.assertEquals("New detail should be kept", "newRefDetail", heterotypicSynonym.getSecMicroReference());
593

    
594
        Assert.assertEquals("New taxon and new synonym should have equal homotypical group", heterotypicSynonym.getHomotypicGroup(), heterotypicSynonym.getAcceptedTaxon().getHomotypicGroup());
595
        Assert.assertSame("New taxon and new synonym should have same homotypical group", heterotypicSynonym.getHomotypicGroup(), heterotypicSynonym.getAcceptedTaxon().getHomotypicGroup());
596
    }
597

    
598

    
599

    
600
    @Test
601
    public final void testGetHeterotypicSynonymyGroups(){
602
        Rank rank = Rank.SPECIES();
603
        Reference ref1 = ReferenceFactory.newGeneric();
604
        //HomotypicalGroup group = HomotypicalGroup.NewInstance();
605
        Taxon taxon1 = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test3", null, null, null, null, null, null, null), null);
606
        Synonym synonym0 = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test2", null, null, null, null, null, null, null), null);
607
        Synonym synonym1 = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test2", null, null, null, null, null, null, null), null);
608
        Synonym synonym2 = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test4", null, null, null, null, null, null, null), null);
609
        synonym0.getName().setHomotypicalGroup(taxon1.getHomotypicGroup());
610
        synonym2.getName().setHomotypicalGroup(synonym1.getHomotypicGroup());
611
        //tax2.addHeterotypicSynonymName(synonym.getName());
612
        taxon1.addSynonym(synonym1, SynonymType.HETEROTYPIC_SYNONYM_OF());
613
        taxon1.addSynonym(synonym2, SynonymType.HETEROTYPIC_SYNONYM_OF());
614

    
615
        service.save(synonym1);
616
        service.save(synonym2);
617
        service.save(taxon1);
618

    
619
        List<List<Synonym>> heteroSyns = service.getHeterotypicSynonymyGroups(taxon1, null);
620
        Assert.assertEquals("There should be 1 heterotypic group", 1, heteroSyns.size());
621
        List<Synonym> synList = heteroSyns.get(0);
622
        Assert.assertEquals("There should be 2 heterotypic syns in group 1", 2, synList.size());
623

    
624
        //test sec
625
        synonym2.setSec(ref1);
626
        heteroSyns = service.getHeterotypicSynonymyGroups(taxon1, null);
627
        Assert.assertEquals("There should be 1 heterotypic group", 1, heteroSyns.size());
628
        synList = heteroSyns.get(0);
629
        Assert.assertEquals("getHeterotypicSynonymyGroups should be independent of sec reference", 2, synList.size());
630

    
631
    }
632

    
633

    
634
    @Test
635
    public final void testGetHomotypicSynonymsByHomotypicGroup(){
636
        Rank rank = Rank.SPECIES();
637
        Reference ref1 = ReferenceFactory.newGeneric();
638
        //HomotypicalGroup group = HomotypicalGroup.NewInstance();
639
        Taxon taxon1 = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test3", null, null, null, null, null, null, null), null);
640
        Synonym synonym0 = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test2", null, null, null, null, null, null, null), null);
641
        Synonym synonym1 = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test2", null, null, null, null, null, null, null), null);
642
        Synonym synonym2 = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test4", null, null, null, null, null, null, null), null);
643
        synonym0.getName().setHomotypicalGroup(taxon1.getHomotypicGroup());
644
        synonym2.getName().setHomotypicalGroup(synonym1.getHomotypicGroup());
645
        //tax2.addHeterotypicSynonymName(synonym.getName());
646
        taxon1.addSynonym(synonym0, SynonymType.HOMOTYPIC_SYNONYM_OF());
647
        taxon1.addSynonym(synonym1, SynonymType.HETEROTYPIC_SYNONYM_OF());
648
        taxon1.addSynonym(synonym2, SynonymType.HETEROTYPIC_SYNONYM_OF());
649

    
650
        service.save(synonym1);
651
        service.save(synonym2);
652
        service.save(taxon1);
653

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

    
658
        //test sec
659
        synonym0.setSec(ref1);
660
        homoSyns = service.getHomotypicSynonymsByHomotypicGroup(taxon1, null);
661
        Assert.assertEquals("getHeterotypicSynonymyGroups should be independent of sec reference", 1, homoSyns.size());
662

    
663
    }
664

    
665
    @Test
666
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
667
    //test delete synonym, but the name will not be deleted
668
    public final void testDeleteSynonymSynonymTaxonDontDeleteName(){
669
        final String[]tableNames = {
670
//                "TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
671
//                "HomotypicalGroup","HomotypicalGroup_AUD"
672
        };
673

    
674
        int nSynonyms = service.count(Synonym.class);
675
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
676
        int nNames = nameService.count(TaxonName.class);
677
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
678
        long nRelations = service.countSynonyms(true);
679
        Assert.assertEquals("There should be two relationship left in the database", 2, nRelations);
680

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

    
683

    
684
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
685
        SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
686
        config.setDeleteNameIfPossible(false);
687
        config.setNewHomotypicGroupIfNeeded(true);
688
        service.deleteSynonym(synonym1, config);
689

    
690
        this.commitAndStartNewTransaction(tableNames);
691

    
692
        nSynonyms = service.count(Synonym.class);
693
        Assert.assertEquals("There should be 1 synonym left in the database", 1, nSynonyms);
694
        nNames = nameService.count(TaxonName.class);
695
        Assert.assertEquals("There should be 4 names left in the database", 4, nNames);
696
        nRelations = service.countSynonyms(true);
697
        Assert.assertEquals("There should be no relationship left in the database", 1, nRelations);
698
    }
699

    
700
    @Test
701
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
702
    //test delete synonym and his name
703
    public final void testDeleteSynonymSynonymTaxonDeleteName(){
704
        final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
705
                "HomotypicalGroup","HomotypicalGroup_AUD"};
706

    
707
        int nSynonyms = service.count(Synonym.class);
708
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
709
        int nNames = nameService.count(TaxonName.class);
710
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
711
        long nRelations = service.countSynonyms(true);
712
        Assert.assertEquals("There should be 2 relationship left in the database", 2, nRelations);
713

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

    
716

    
717
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
718
        service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
719

    
720
        this.commitAndStartNewTransaction(tableNames);
721

    
722
        nSynonyms = service.count(Synonym.class);
723
        Assert.assertEquals("There should be 1 synonym left in the database", 1, nSynonyms);
724
        nNames = nameService.count(TaxonName.class);
725
        Assert.assertEquals("There should be 3 names left in the database", 3, nNames);
726
        nRelations = service.countSynonyms(true);
727
        Assert.assertEquals("There should be 1 relationship left in the database", 1, nRelations);
728

    
729
    }
730

    
731
    @Test
732
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
733
    //test remove synonym from taxon -> synonym and name still in the db and the synonymrelationship to the other taxon
734
    //test delete synonym -> all relationships are deleted, the name is deleted and the synonym itself
735
    public final void testDeleteSynonymSynonymTaxonBooleanRelToOneTaxon(){
736
        final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
737
                "HomotypicalGroup","HomotypicalGroup_AUD"};
738

    
739
        int nSynonyms = service.count(Synonym.class);
740
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
741
        int nNames = nameService.count(TaxonName.class);
742
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
743

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

    
748

    
749
        Taxon taxon2 = (Taxon)service.load(uuidTaxon1);
750

    
751
        List<String> initStrat = new ArrayList<String>();
752
        initStrat.add("markers");
753
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1, initStrat);
754
        long nRelations = service.countSynonyms(true);
755
        Assert.assertEquals("There should be 2 relationship left in the database", 2, nRelations);
756

    
757
        taxon2.removeSynonym(synonym1, false);
758
        service.saveOrUpdate(taxon2);
759

    
760
        commitAndStartNewTransaction(null);
761

    
762
        nSynonyms = service.count(Synonym.class);
763
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
764
        nNames = nameService.count(TaxonName.class);
765
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
766
        nRelations = service.countSynonyms(true);
767
        Assert.assertEquals("There should be 1 relationship left in the database", 1, nRelations);
768
        Marker marker1 = Marker.NewInstance(MarkerType.IMPORTED(), true);
769
        Marker marker2 = Marker.NewInstance(MarkerType.COMPUTED(), true);
770
        synonym1.addMarker(marker1);
771
        synonym1.addMarker(marker2);
772
        service.update(synonym1);
773
        synonym1 =(Synonym) service.load(uuidSynonym1);
774

    
775

    
776
        Set<Marker> markers = synonym1.getMarkers();
777
        Marker marker = markers.iterator().next();
778
        UUID markerUUID = marker.getUuid();
779
       // taxon2 = (Taxon)service.load(uuidTaxon2);
780
        synonym1 = (Synonym)service.load(uuidSynonym1);
781
        //the marker should not prevent the deletion
782
        DeleteResult result = service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
783
        if (!result.isOk()){
784
        	Assert.fail();
785
        }
786

    
787

    
788
        commitAndStartNewTransaction(tableNames);
789
        nSynonyms = service.count(Synonym.class);
790
        Assert.assertEquals("There should be 1 synonym left in the database", 1, nSynonyms);
791
        nNames = nameService.count(TaxonName.class);
792
        Assert.assertEquals("There should be 3 names left in the database", 3, nNames);
793
        nRelations = service.countSynonyms(true);
794
        Assert.assertEquals("There should be no relationship left in the database", 1, nRelations);
795
        marker = markerService.load(markerUUID);
796
        assertNull(marker);
797

    
798
    }
799

    
800
    @Test
801
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
802
    //this test is more or less obsolete since we have no synonym relationships anymore
803
    //test delete synonym, only for a special taxon, but because of other relationships it will not be deleted at all
804
    public final void testDeleteSynonymSynonymTaxonBooleanDeleteOneTaxon(){
805
        final String[]tableNames = {
806
//                "TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
807
//                "HomotypicalGroup","HomotypicalGroup_AUD"
808
        };
809
        int nSynonyms = service.count(Synonym.class);
810
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
811
        int nNames = nameService.count(TaxonName.class);
812
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
813

    
814
        UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
815
        UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
816

    
817
        Taxon taxon2 = (Taxon)service.load(uuidTaxon2);
818
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
819
        taxon2.addSynonym(synonym1, SynonymType.HETEROTYPIC_SYNONYM_OF());
820
        service.saveOrUpdate(synonym1);
821
        long nRelations = service.countSynonyms(true);
822
        //this was "3" when we still had synonym relationships
823
        Assert.assertEquals("There should be 2 relationship left in the database", 2, nRelations);
824
        service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
825

    
826
        this.commitAndStartNewTransaction(tableNames);
827

    
828
        nSynonyms = service.count(Synonym.class);
829
        //this was "2" when we still had synonym relationships
830
        Assert.assertEquals("There should still be 1 synonym left in the database", 1, nSynonyms);
831
        nNames = nameService.count(TaxonName.class);
832
        //was 3
833
        Assert.assertEquals("There should be 3 names left in the database", 3, nNames);
834
        nRelations = service.countSynonyms(true);
835
        Assert.assertEquals("There should be 1 related synonym left in the database", 1, nRelations);
836
    }
837

    
838
    @Test
839
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
840

    
841
    public final void testDeleteSynonymWithAnnotations(){
842
        final String[]tableNames = {
843
//                "TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
844
//                "HomotypicalGroup","HomotypicalGroup_AUD"
845
        };
846

    
847

    
848
        UUID uuidTaxon2=UUID.fromString("2d9a642d-5a82-442d-8fec-95efa978e8f8");
849
        UUID uuidSynonym1=UUID.fromString("7da85381-ad9d-4886-9d4d-0eeef40e3d88");
850

    
851
        Taxon taxon2 = (Taxon)service.load(uuidTaxon2);
852
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
853
        taxon2.addSynonym(synonym1, SynonymType.HETEROTYPIC_SYNONYM_OF());
854

    
855
        Annotation annotation = Annotation.NewDefaultLanguageInstance("test");
856
        synonym1.addAnnotation(annotation);
857
        service.saveOrUpdate(synonym1);
858

    
859
        DeleteResult result = service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
860
        if (result.isError()){
861
            Assert.fail();
862
        }
863
        this.commitAndStartNewTransaction(tableNames);
864

    
865

    
866
    }
867

    
868

    
869
    @Test
870
    @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
871

    
872
    public final void testDeleteSynonymSynonymTaxonBooleanWithRelatedName(){
873
        final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
874
                "HomotypicalGroup","HomotypicalGroup_AUD"};
875

    
876
        int nSynonyms = service.count(Synonym.class);
877
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
878
        int nNames = nameService.count(TaxonName.class);
879
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
880

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

    
887
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
888
        TaxonName name2 = nameService.load(uuidSynonymName2);
889
        UUID name3Uuid = synonym1.getName().getUuid();
890
        TaxonName name3 = nameService.load(name3Uuid);
891
        name3.addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null, null);
892

    
893
        service.saveOrUpdate(synonym1);
894

    
895
        long nRelations = nameService.listNameRelationships(null, 1000, 0, null, null).size();
896
        logger.info("number of name relations: " + nRelations);
897
        Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
898
        SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
899

    
900
        service.deleteSynonym(synonym1, config);
901

    
902
        this.commitAndStartNewTransaction(tableNames);
903
        //synonym is deleted, but the name can not be deleted because of a name relationship
904
        nSynonyms = service.count(Synonym.class);
905
        Assert.assertEquals("There should still be 1 synonyms left in the database", 1, nSynonyms);
906
        nNames = nameService.count(TaxonName.class);
907
        Assert.assertEquals("There should be 4 names left in the database (name is related to synonymName2)", 4, nNames);
908
        nRelations = service.countSynonyms(true);
909
        //may change with better implementation of countAllRelationships (see #2653)
910
        nRelations = nameService.listNameRelationships(null, 1000, 0, null, null).size();
911
        logger.info("number of name relations: " + nRelations);
912
        Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
913

    
914

    
915
        //clean up database
916
        name2 = nameService.load(uuidSynonymName2);
917
        NameRelationship rel = CdmBase.deproxy(name2.getNameRelations().iterator().next(), NameRelationship.class);
918
        name2.removeNameRelationship(rel);
919
        nameService.save(name2);
920
        this.setComplete();
921
        this.endTransaction();
922

    
923
    }
924

    
925
    @Test
926
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
927
    public final void testDeleteSynonymSynonymTaxonBooleanWithRelatedNameDeleteAllNameRelations(){
928
        final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
929
                "HomotypicalGroup","HomotypicalGroup_AUD"};
930

    
931
        int nSynonyms = service.count(Synonym.class);
932
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
933
        int nNames = nameService.count(TaxonName.class);
934
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
935

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

    
942
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
943
        TaxonName name2 = nameService.load(uuidSynonymName2);
944
        UUID name3Uuid = synonym1.getName().getUuid();
945
        TaxonName name3 = nameService.load(name3Uuid);
946
        name3.addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null, null);
947

    
948
        service.saveOrUpdate(synonym1);
949

    
950
        long nRelations = nameService.listNameRelationships(null, 1000, 0, null, null).size();
951
        logger.info("number of name relations: " + nRelations);
952
        Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
953
        SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
954
        NameDeletionConfigurator nameDeletionConfig = new NameDeletionConfigurator();
955
        nameDeletionConfig.setRemoveAllNameRelationships(true);
956
        config.setNameDeletionConfig(nameDeletionConfig);
957

    
958
        service.deleteSynonym(synonym1, config);
959

    
960
        this.commitAndStartNewTransaction(tableNames);
961

    
962
        nSynonyms = service.count(Synonym.class);
963
        Assert.assertEquals("There should still be 1 synonyms left in the database", 1, nSynonyms);
964
        nNames = nameService.count(TaxonName.class);
965
        Assert.assertEquals("There should be 3 names left in the database ", 3, nNames);
966
        nRelations = service.countSynonyms(true);
967
        //may change with better implementation of countAllRelationships (see #2653)
968
        nRelations = nameService.listNameRelationships(null, 1000, 0, null, null).size();
969
        logger.info("number of name relations: " + nRelations);
970
        Assert.assertEquals("There should be no name relationship left in the database", 0, nRelations);
971
    }
972

    
973
    @Test
974
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonServiceImplTest.testDeleteSynonym.xml")
975
    public final void testDeleteSynonymSynonymTaxonBooleanWithRelatedNameIgnoreIsBasionym(){
976
        final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
977
                "HomotypicalGroup","HomotypicalGroup_AUD"};
978

    
979
        int nSynonyms = service.count(Synonym.class);
980
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
981
        int nNames = nameService.count(TaxonName.class);
982
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
983

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

    
990
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
991
        TaxonName synName2 = nameService.load(uuidSynonymName2);
992
        UUID name3Uuid = synonym1.getName().getUuid();
993
        TaxonName synName1 = nameService.load(name3Uuid);
994
        synName1.addRelationshipFromName(synName2, NameRelationshipType.BASIONYM(), null, null);
995

    
996
        service.saveOrUpdate(synonym1);
997

    
998
        long nRelations = nameService.listNameRelationships(null, 1000, 0, null, null).size();
999
        logger.info("number of name relations: " + nRelations);
1000
        Assert.assertEquals("There should be 1 name relationship left in the database", 1, nRelations);
1001
        SynonymDeletionConfigurator config = new SynonymDeletionConfigurator();
1002
        NameDeletionConfigurator nameDeletionConfig = new NameDeletionConfigurator();
1003
        nameDeletionConfig.setIgnoreIsBasionymFor(true);
1004
        config.setNameDeletionConfig(nameDeletionConfig);
1005

    
1006
        DeleteResult result =service.deleteSynonym(synonym1, config);
1007
        if (!result.isOk()){
1008
        	Assert.fail();
1009
        }
1010

    
1011

    
1012
        logger.debug(result);
1013
        this.commitAndStartNewTransaction(tableNames);
1014

    
1015
        nSynonyms = service.count(Synonym.class);
1016
        Assert.assertEquals("There should still be 1 synonyms left in the database", 1, nSynonyms);
1017
        nNames = nameService.count(TaxonName.class);
1018
        Assert.assertEquals("There should be 3 names left in the database ", 3, nNames);
1019
        nRelations = service.countSynonyms(true);
1020
        //may change with better implementation of countAllRelationships (see #2653)
1021
        nRelations = nameService.listNameRelationships(null, 1000, 0, null, null).size();
1022
        logger.info("number of name relations: " + nRelations);
1023
        Assert.assertEquals("There should be no name relationship left in the database", 0, nRelations);
1024
    }
1025

    
1026

    
1027
    @Test
1028
    @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
1029
    public final void testDeleteSynonymSynonymTaxonBooleanWithRollback(){
1030
        final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
1031
                "HomotypicalGroup","HomotypicalGroup_AUD"};
1032

    
1033
        int nSynonyms = service.count(Synonym.class);
1034
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
1035
        int nNames = nameService.count(TaxonName.class);
1036
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
1037
        long nRelations = service.countSynonyms(true);
1038

    
1039

    
1040
        //may change with better implementation of countAllRelationships (see #2653)
1041

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

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

    
1048
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
1049
        TaxonName name2 = nameService.load(uuidSynonymName2);
1050
        synonym1.getName().addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null, null);
1051

    
1052
        service.deleteSynonym(synonym1, new SynonymDeletionConfigurator());
1053

    
1054
        this.rollback();
1055
//		printDataSet(System.out, tableNames);
1056
        this.startNewTransaction();
1057

    
1058
        nSynonyms = service.count(Synonym.class);
1059
        Assert.assertEquals("There should still be 2 synonyms left in the database", 2, nSynonyms);
1060
        nNames = nameService.count(TaxonName.class);
1061
        Assert.assertEquals("There should be 4 names left in the database", 4, nNames);
1062
        nRelations = service.countSynonyms(true);
1063
        //may change with better implementation of countAllRelationships (see #2653)
1064
        Assert.assertEquals("There should be 2 relationship in the database (the 2 synonym relationship) but no name relationship", 2, nRelations);
1065

    
1066
    }
1067

    
1068
    @Test
1069
    @DataSet("TaxonServiceImplTest.testDeleteSynonym.xml")
1070
    public final void testDeleteSynonymSynonymTaxonBooleanWithoutTransaction(){
1071
        @SuppressWarnings("unused")
1072
        final String[]tableNames = {"TaxonBase","TaxonBase_AUD", "TaxonName","TaxonName_AUD",
1073
                "HomotypicalGroup","HomotypicalGroup_AUD"};
1074

    
1075
        int nSynonyms = service.count(Synonym.class);
1076
        Assert.assertEquals("There should be 2 synonyms in the database", 2, nSynonyms);
1077
        int nNames = nameService.count(TaxonName.class);
1078
        Assert.assertEquals("There should  be 4 names in the database", 4, nNames);
1079
        long nRelations = service.countSynonyms(true);
1080
        //may change with better implementation of countAllRelationships (see #2653)
1081
        Assert.assertEquals("There should be 2 relationship in the database (the 2 synonym relationships) but no name relationship", 2, nRelations);
1082

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

    
1086
        Synonym synonym1 = (Synonym)service.load(uuidSynonym1);
1087
        TaxonName name2 = nameService.load(uuidSynonymName2);
1088
        synonym1.getName().addRelationshipFromName(name2, NameRelationshipType.LATER_HOMONYM(), null, null);
1089

    
1090
        service.saveOrUpdate(synonym1);
1091
        nRelations = service.countSynonyms(true);
1092
        Assert.assertEquals("There should be two relationships in the database", 2, nRelations);
1093
        this.setComplete();
1094
        this.endTransaction();
1095

    
1096
//        printDataSet(System.out, tableNames);
1097

    
1098
        //out of wrapping transaction
1099
        service.deleteSynonym(synonym1,  new SynonymDeletionConfigurator());
1100

    
1101
        this.startNewTransaction();
1102

    
1103
        nSynonyms = service.count(Synonym.class);
1104
        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);
1105
        nNames = nameService.count(TaxonName.class);
1106
        Assert.assertEquals("There should be 4 names left in the database", 4, nNames);
1107
        nRelations = service.countSynonyms(true);
1108
        Assert.assertEquals("There should be no taxon or synonym relationship in the database", 1, nRelations);
1109
        nRelations = nameService.listNameRelationships(null, 1000, 0, null, null).size();
1110
        Assert.assertEquals("There should be one name relationship in the database", 1, nRelations);
1111

    
1112
    }
1113

    
1114
    @Test
1115
    @DataSet("TaxonServiceImplTest.testInferredSynonyms.xml")
1116
    public void testCreateInferredSynonymy(){
1117

    
1118
        UUID classificationUuid = UUID.fromString("aeee7448-5298-4991-b724-8d5b75a0a7a9");
1119
        Classification tree = classificationService.find(classificationUuid);
1120
        UUID taxonUuid = UUID.fromString("bc09aca6-06fd-4905-b1e7-cbf7cc65d783");
1121
        TaxonBase<?> taxonBase =  service.find(taxonUuid);
1122
        List <Synonym> synonyms = service.list(Synonym.class, null, null, null, null);
1123
        assertEquals("Number of synonyms should be 2",2,synonyms.size());
1124
        Taxon taxon = (Taxon)taxonBase;
1125

    
1126
        //synonyms = taxonDao.getAllSynonyms(null, null);
1127
        //assertEquals("Number of synonyms should be 2",2,synonyms.size());
1128
        List<Synonym> inferredSynonyms = service.createInferredSynonyms(taxon, tree, SynonymType.INFERRED_EPITHET_OF(), true);
1129
        assertNotNull("there should be a new synonym ", inferredSynonyms);
1130
        assertEquals ("the name of inferred epithet should be SynGenus lachesis", "SynGenus lachesis syn. sec. Sp. Pl.", inferredSynonyms.get(0).getTitleCache());
1131

    
1132
        inferredSynonyms = service.createInferredSynonyms(taxon, tree, SynonymType.INFERRED_GENUS_OF(), true);
1133
        assertNotNull("there should be a new synonym ", inferredSynonyms);
1134
        assertEquals ("the name of inferred epithet should be SynGenus lachesis", "Acherontia ciprosus syn. sec. Sp. Pl.", inferredSynonyms.get(0).getTitleCache());
1135

    
1136
        inferredSynonyms = service.createInferredSynonyms(taxon, tree, SynonymType.POTENTIAL_COMBINATION_OF(), true);
1137
        assertNotNull("there should be a new synonym ", inferredSynonyms);
1138
        assertEquals ("the name of inferred epithet should be SynGenus lachesis", "SynGenus ciprosus syn. sec. Sp. Pl.", inferredSynonyms.get(0).getTitleCache());
1139
        //assertTrue("set of synonyms should contain an inferred Synonym ", synonyms.contains(arg0))
1140
    }
1141

    
1142
    @Test
1143
    @DataSet("../../database/ClearDBDataSet.xml")
1144
    public final void testTaxonDeletionConfig(){
1145
        final String[]tableNames = {}
1146
//                "Classification", "Classification_AUD",
1147
//                "TaxonBase","TaxonBase_AUD",
1148
//                "TaxonNode","TaxonNode_AUD",
1149
//                "TaxonName","TaxonName_AUD",
1150
//                "TaxonRelationship", "TaxonRelationship_AUD",
1151
//                "TaxonDescription", "TaxonDescription_AUD",
1152
//                "HomotypicalGroup","HomotypicalGroup_AUD",
1153
//                "PolytomousKey","PolytomousKey_AUD",
1154
//                "PolytomousKeyNode","PolytomousKeyNode_AUD",
1155
//                "Media","Media_AUD",
1156
//                "DescriptiveDataSet","DescriptiveDataSet_AUD",
1157
//                "DescriptionElementBase","DescriptionElementBase_AUD",
1158
//        		"DeterminationEvent","DeterminationEvent_AUD",
1159
//        		"SpecimenOrObservationBase","SpecimenOrObservationBase_AUD"}
1160
        ;
1161

    
1162
        UUID uuidParent=UUID.fromString("b5271d4f-e203-4577-941f-00d76fa9f4ca");
1163
        UUID uuidChild1=UUID.fromString("326167f9-0b97-4e7d-b1bf-4ca47b82e21e");
1164
        UUID uuidSameAs=UUID.fromString("c2bb0f01-f2dd-43fb-ba12-2a85727ccb8d");
1165
        commitAndStartNewTransaction(tableNames);
1166
        Taxon testTaxon =getTestTaxon();
1167
       // service.save(testTaxon);
1168
        commitAndStartNewTransaction(tableNames);
1169
        int nTaxa = service.count(Taxon.class);
1170

    
1171
        Assert.assertEquals("There should be 4 taxa in the database", 4, nTaxa);
1172
        Taxon parent = (Taxon)service.find(GENUS_UUID);
1173
        Assert.assertNotNull("Parent taxon should exist", parent);
1174
        Taxon child1 = (Taxon)service.find(SPECIES1_UUID);
1175
        Assert.assertNotNull("Child taxon should exist", child1);
1176
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1177
        config.setDeleteTaxonNodes(false);
1178
        config.setDeleteMisappliedNames(false);
1179
        //try {
1180
            //commitAndStartNewTransaction(tableNames);
1181

    
1182
        DeleteResult result = service.deleteTaxon(child1.getUuid(), config, null);
1183
        if (result.isOk()){
1184
            Assert.fail("Delete should throw an error as long as name is used in classification.");
1185
        }
1186

    
1187
        nTaxa = service.count(Taxon.class);
1188
        Assert.assertEquals("There should be 4 taxa in the database", 4, nTaxa);
1189
        child1 = (Taxon)service.find(SPECIES1_UUID);
1190
        Assert.assertNotNull("Child taxon should exist", child1);
1191
        Assert.assertEquals("Child should belong to 1 node", 1, child1.getTaxonNodes().size());
1192

    
1193
        TaxonNode node = child1.getTaxonNodes().iterator().next();
1194
        child1.addSource(IdentifiableSource.NewInstance(OriginalSourceType.Import));
1195

    
1196
        SpecimenOrObservationBase<?> identifiedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.DerivedUnit);
1197
        DeterminationEvent.NewInstance(child1, identifiedUnit);
1198
        //UUID eventUUID = eventService.save(determinationEvent);
1199
        UUID identifiedUnitUUID = occurenceService.save(identifiedUnit).getUuid();
1200

    
1201

    
1202
        TaxonNode parentNode = node.getParent();
1203
        parentNode =CdmBase.deproxy(parentNode, TaxonNode.class);
1204
        parentNode.deleteChildNode(node);
1205
        nodeService.save(parentNode);
1206
        //commitAndStartNewTransaction(tableNames);
1207

    
1208
       // try {
1209

    
1210
       result = service.deleteTaxon(child1
1211
    		   .getUuid(), config, null);
1212
       if (result.isOk()){
1213
           	Assert.fail("Delete should throw an exception because of the determination event");
1214
       }
1215

    
1216

    
1217

    
1218
        //determinationEvent = (DeterminationEvent)eventService.load(eventUUID);
1219
        commitAndStartNewTransaction(tableNames);
1220
        identifiedUnit = occurenceService.load(identifiedUnitUUID);
1221

    
1222
        occurenceService.delete(identifiedUnit);
1223

    
1224
        commitAndStartNewTransaction(tableNames);
1225
        child1 = (Taxon)service.find(SPECIES1_UUID);
1226

    
1227
        assertEquals(0, child1.getTaxonNodes().size());
1228
       // try {
1229

    
1230
         result = service.deleteTaxon(child1.getUuid(), config, null);
1231

    
1232
         if (!result.isOk()){
1233
            Assert.fail("Delete should not throw an exception anymore");
1234
         }
1235

    
1236
        nTaxa = service.count(Taxon.class);
1237
        Assert.assertEquals("There should be 3 taxa in the database", 3, nTaxa);
1238

    
1239
        config.setDeleteTaxonNodes(true);
1240
        Taxon child2 =(Taxon) service.find(SPECIES2_UUID);
1241

    
1242
       // try {
1243
        result = service.deleteTaxon(child2.getUuid(), config, child2.getTaxonNodes().iterator().next().getClassification().getUuid());
1244
        if (!result.isOk()){
1245
            Assert.fail("Delete should not throw an exception");
1246
        }
1247

    
1248

    
1249
        //service.find(uuid);
1250

    
1251
        nTaxa = service.count(Taxon.class);
1252
        Assert.assertEquals("There should be 2 taxa in the database",2, nTaxa);
1253
//		nNames = nameService.count(TaxonName.class);
1254
//		Assert.assertEquals("There should be 3 names left in the database", 3, nNames);
1255
//		int nRelations = service.countAllRelationships();
1256
//		Assert.assertEquals("There should be no relationship left in the database", 0, nRelations);
1257
    }
1258

    
1259

    
1260
    @Test
1261
    @DataSet(value="../../database/ClearDBDataSet.xml")
1262
    public final void testDeleteTaxon(){
1263

    
1264
        //create a small classification
1265

    
1266
        Taxon testTaxon = getTestTaxon();
1267

    
1268
        service.save(testTaxon).getUuid();
1269

    
1270
        Taxon speciesTaxon = (Taxon)service.find(SPECIES1_UUID);
1271
        Iterator<TaxonDescription> descriptionIterator = speciesTaxon.getDescriptions().iterator();
1272
        UUID descrUUID = null;
1273
        UUID descrElementUUID = null;
1274
        if (descriptionIterator.hasNext()){
1275
            TaxonDescription descr = descriptionIterator.next();
1276
            descrUUID = descr.getUuid();
1277
            descrElementUUID = descr.getElements().iterator().next().getUuid();
1278
        }
1279
        IBotanicalName taxonName = nameService.find(SPECIES1_NAME_UUID);
1280
        assertNotNull(taxonName);
1281

    
1282
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1283
        config.setDeleteNameIfPossible(false);
1284

    
1285

    
1286

    
1287
       // try {
1288

    
1289
        DeleteResult result = service.deleteTaxon(speciesTaxon.getUuid(), config, speciesTaxon.getTaxonNodes().iterator().next().getClassification().getUuid());
1290
        if (!result.isOk()){
1291
        	Assert.fail();
1292
        }
1293
        commitAndStartNewTransaction(null);
1294

    
1295
        taxonName = nameService.find(SPECIES1_NAME_UUID);
1296
        Taxon taxon = (Taxon)service.find(SPECIES1_UUID);
1297

    
1298
        //descriptionService.find(descrUUID);
1299
        assertNull(descriptionService.find(descrUUID));
1300
        assertNull(descriptionService.getDescriptionElementByUuid(descrElementUUID));
1301
        //assertNull(synName);
1302
        assertNotNull(taxonName);
1303
        assertNull(taxon);
1304
        config.setDeleteNameIfPossible(true);
1305
        Taxon newTaxon = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), null);
1306
        service.save(newTaxon);
1307
        result = service.deleteTaxon(newTaxon.getUuid()
1308
        		, config, null);
1309
        if (!result.isOk()){
1310
        	Assert.fail();
1311
        }
1312

    
1313

    
1314
    }
1315

    
1316
    @Test
1317
    @DataSet(value="../../database/ClearDBDataSet.xml")
1318
    public final void testDeleteTaxonWithAnnotations(){
1319

    
1320
        //create a small classification
1321

    
1322
        Taxon testTaxon = getTestTaxon();
1323

    
1324
        service.save(testTaxon).getUuid();
1325

    
1326
        Taxon speciesTaxon = (Taxon)service.find(SPECIES1_UUID);
1327
        Iterator<TaxonDescription> descriptionIterator = speciesTaxon.getDescriptions().iterator();
1328
        UUID descrUUID = null;
1329
        UUID descrElementUUID = null;
1330
        if (descriptionIterator.hasNext()){
1331
            TaxonDescription descr = descriptionIterator.next();
1332
            descrUUID = descr.getUuid();
1333
            descrElementUUID = descr.getElements().iterator().next().getUuid();
1334
        }
1335
        IBotanicalName taxonName = nameService.find(SPECIES1_NAME_UUID);
1336
        assertNotNull(taxonName);
1337

    
1338
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1339
        config.setDeleteNameIfPossible(false);
1340
        Annotation annotation = Annotation.NewDefaultLanguageInstance("test");
1341
        speciesTaxon.addAnnotation(annotation);
1342

    
1343

    
1344
       // try {
1345

    
1346
        DeleteResult result = service.deleteTaxon(speciesTaxon.getUuid(), config, speciesTaxon.getTaxonNodes().iterator().next().getClassification().getUuid());
1347
        if (!result.isOk()){
1348
            Assert.fail();
1349
        }
1350
        commitAndStartNewTransaction(null);
1351

    
1352
        taxonName = nameService.find(SPECIES1_NAME_UUID);
1353
        Taxon taxon = (Taxon)service.find(SPECIES1_UUID);
1354

    
1355
        //descriptionService.find(descrUUID);
1356
        assertNull(descriptionService.find(descrUUID));
1357
        assertNull(descriptionService.getDescriptionElementByUuid(descrElementUUID));
1358
        //assertNull(synName);
1359
        assertNotNull(taxonName);
1360
        assertNull(taxon);
1361
        config.setDeleteNameIfPossible(true);
1362
        Taxon newTaxon = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), null);
1363
        service.save(newTaxon);
1364
        result = service.deleteTaxon(newTaxon.getUuid()
1365
                , config, null);
1366
        if (!result.isOk()){
1367
            Assert.fail();
1368
        }
1369

    
1370

    
1371
    }
1372

    
1373
    @Test
1374
    @DataSet(value="../../database/ClearDBDataSet.xml")
1375
    public final void testDeleteTaxonUsedInTaxonRelation(){
1376

    
1377
        //create a small classification
1378
        Taxon testTaxon = getTestTaxon();
1379

    
1380
        service.save(testTaxon).getUuid();
1381

    
1382
        Taxon speciesTaxon = (Taxon)service.find(SPECIES1_UUID);
1383
        Taxon speciesTaxon2 = (Taxon)service.find(SPECIES2_UUID);
1384
        speciesTaxon.addTaxonRelation(speciesTaxon2, TaxonRelationshipType.MISAPPLIED_NAME_FOR(), null, null);
1385

    
1386
        IBotanicalName taxonName = nameService.find(SPECIES1_NAME_UUID);
1387
        assertNotNull(taxonName);
1388

    
1389
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1390
        config.setDeleteNameIfPossible(false);
1391
        config.setDeleteTaxonRelationships(false);
1392

    
1393

    
1394
       // try {
1395

    
1396
        DeleteResult result = service.deleteTaxon(speciesTaxon.getUuid(), config, speciesTaxon.getTaxonNodes().iterator().next().getClassification().getUuid());
1397
        if (result.isOk()){
1398
            Assert.fail();
1399
        }
1400
        commitAndStartNewTransaction(null);
1401

    
1402
        taxonName = nameService.find(SPECIES1_NAME_UUID);
1403
        Taxon taxon = (Taxon)service.find(SPECIES1_UUID);
1404

    
1405

    
1406
        assertNotNull(taxonName);
1407
        assertNotNull(taxon);
1408

    
1409

    
1410
        config.setDeleteNameIfPossible(false);
1411
        config.setDeleteTaxonRelationships(true);
1412

    
1413

    
1414
       // try {
1415

    
1416
       result = service.deleteTaxon(speciesTaxon.getUuid(), config, speciesTaxon.getTaxonNodes().iterator().next().getClassification().getUuid());
1417
        if (!result.isOk()){
1418
            Assert.fail();
1419
        }
1420
        commitAndStartNewTransaction(null);
1421

    
1422

    
1423
        config.setDeleteNameIfPossible(true);
1424
        Taxon newTaxon = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), null);
1425
        service.save(newTaxon);
1426
        result = service.deleteTaxon(newTaxon.getUuid()
1427
                , config, null);
1428
        if (!result.isOk()){
1429
            Assert.fail();
1430
        }
1431

    
1432

    
1433
    }
1434

    
1435
    @Test
1436
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="../../database/ClearDBDataSet.xml")
1437
    public final void testDeleteTaxonDeleteSynonymRelations(){
1438

    
1439
    	 final String[]tableNames = {
1440
                 "Classification", "Classification_AUD",
1441
                 "TaxonBase","TaxonBase_AUD",
1442
                 "TaxonNode","TaxonNode_AUD",
1443
                 "TaxonName","TaxonName_AUD"};
1444
    	 commitAndStartNewTransaction(tableNames);
1445
        //create a small classification
1446
        Taxon testTaxon = getTestTaxon();
1447

    
1448
        service.save(testTaxon).getUuid();
1449

    
1450
        Taxon speciesTaxon = (Taxon)service.find(SPECIES2_UUID);
1451

    
1452
        Synonym synonym = speciesTaxon.getSynonyms().iterator().next();
1453
        UUID synonymUuid = synonym.getUuid();
1454
        service.countSynonyms(true);
1455

    
1456
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1457
        config.setDeleteSynonymsIfPossible(false);
1458

    
1459

    
1460
        DeleteResult result = service.deleteTaxon(speciesTaxon.getUuid(), config, speciesTaxon.getTaxonNodes().iterator().next().getClassification().getUuid());
1461
        if (!result.isOk()){
1462
        	Assert.fail();
1463
        }
1464
        commitAndStartNewTransaction(null);
1465

    
1466
        Taxon taxon = (Taxon)service.find(SPECIES2_UUID);
1467
        assertNull("The deleted taxon should no longer exist", taxon);
1468

    
1469
        Synonym syn = (Synonym)service.find(synonymUuid);
1470
        assertNotNull("The synonym should still exist since DeleteSynonymsIfPossible was false", service.find(synonymUuid));
1471
        assertNull("The synonym should not be attached to an accepted taxon anymore", syn.getAcceptedTaxon());
1472
    }
1473

    
1474

    
1475
    @Test
1476
    @DataSet(value="../../database/ClearDBDataSet.xml")
1477
    public final void testDeleteTaxonNameUsedInOtherContext(){
1478

    
1479
        //create a small classification
1480
        Taxon testTaxon = getTestTaxon();
1481

    
1482
        service.save(testTaxon).getUuid();
1483

    
1484
        Taxon speciesTaxon = (Taxon)service.find(SPECIES1_UUID);
1485

    
1486
        IBotanicalName taxonName = nameService.find(SPECIES1_NAME_UUID);
1487
        assertNotNull(taxonName);
1488
        TaxonName fromName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
1489
        taxonName.addRelationshipFromName(fromName, NameRelationshipType.VALIDATED_BY_NAME(), null, null);
1490
        nameService.save(fromName);
1491

    
1492
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1493
        config.setDeleteNameIfPossible(true);
1494
        DeleteResult result = service.deleteTaxon(speciesTaxon.getUuid(), config, speciesTaxon.getTaxonNodes().iterator().next().getClassification().getUuid());
1495
        if (!result.isOk()){
1496
        	Assert.fail();
1497
        }
1498
        commitAndStartNewTransaction(null);
1499

    
1500
        taxonName = nameService.find(SPECIES1_NAME_UUID);
1501
        Taxon taxon = (Taxon)service.find(SPECIES1_UUID);
1502
        //because of the namerelationship the name cannot be deleted
1503
        assertNotNull(taxonName);
1504
        assertNull(taxon);
1505

    
1506
    }
1507

    
1508
    @Test
1509
    @DataSet(value="../../database/ClearDBDataSet.xml")
1510
    public final void testDeleteTaxonNameUsedInTwoClassificationsDeleteAllNodes(){
1511
        commitAndStartNewTransaction(null);
1512
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
1513
        //create a small classification
1514
        Taxon testTaxon = getTestTaxon();
1515

    
1516
        UUID uuid = service.save(testTaxon).getUuid();
1517
        //BotanicalName name = nameService.find(uuid);
1518
        Set<TaxonNode> nodes = testTaxon.getTaxonNodes();
1519
        TaxonNode node = nodes.iterator().next();
1520
        List<TaxonNode> childNodes = node.getChildNodes();
1521
        TaxonNode childNode = childNodes.iterator().next();
1522
        UUID childUUID = childNode.getTaxon().getUuid();
1523
        Classification secondClassification = getTestClassification("secondClassification");
1524

    
1525
        secondClassification.addChildTaxon(testTaxon, null, null);
1526
        //delete the taxon in all classifications
1527
        config.setDeleteInAllClassifications(true);
1528
       DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, null);
1529
       if (!result.isOk()){
1530
        	Assert.fail();
1531
        }
1532
        commitAndStartNewTransaction(null);
1533
        Taxon tax = (Taxon)service.find(uuid);
1534
        assertNull(tax);
1535
        Taxon childTaxon = (Taxon)service.find(childUUID);
1536
        assertNull(tax);
1537
        commitAndStartNewTransaction(null);
1538

    
1539

    
1540

    
1541

    
1542

    
1543
    }
1544

    
1545
    @Test
1546
    @DataSet(value="../../database/ClearDBDataSet.xml")
1547
    public final void testDeleteTaxonNameUsedInTwoClassificationsDoNotDeleteAllNodes(){
1548
        // delete the taxon only in second classification, this should delete only the nodes, not the taxa
1549
        Taxon testTaxon = getTestTaxon();
1550
        UUID uuid = service.save(testTaxon).getUuid();
1551
        Classification secondClassification = getTestClassification("secondClassification");
1552
        Set<TaxonNode> nodes = testTaxon.getTaxonNodes();
1553
        TaxonNode node = nodes.iterator().next();
1554
        List<TaxonNode> childNodes = node.getChildNodes();
1555
        TaxonNode childNode = childNodes.iterator().next();
1556
        UUID childUUID = childNode.getTaxon().getUuid();
1557
        childNode = secondClassification.addChildTaxon(testTaxon, null, null);
1558
        UUID childNodeUUID = childNode.getUuid();
1559

    
1560
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1561
        config.setDeleteInAllClassifications(false);
1562
       //     try {
1563
       DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, secondClassification.getUuid());
1564
/*                Assert.fail("The taxon should not be deletable because it is used in a second classification and the configuration is set to deleteInAllClassifications = false");
1565
            } catch (DataChangeNoRollbackException e) {
1566
                logger.debug(e.getMessage());
1567
            }
1568
  */
1569

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

    
1574
        //commitAndStartNewTransaction(null);
1575
        Taxon tax = (Taxon)service.find(uuid);
1576
        assertNotNull(tax);
1577
        Taxon childTaxon = (Taxon)service.find(childUUID);
1578
        assertNotNull(tax);
1579
        //when calling delete taxon and the taxon can not be deleted the children should not be deleted as well. If children should be deleted call delete taxonnode
1580
        node = nodeService.find(childNodeUUID);
1581
        assertNotNull(node);
1582
    }
1583

    
1584
    @Test
1585
    @DataSet(value="../../database/ClearDBDataSet.xml")
1586
    public final void testTaxonNodeDeletionConfiguratorMoveToParent(){
1587
        //test childHandling MOVE_TO_PARENT:
1588
        Taxon testTaxon = getTestTaxon();
1589
        UUID uuid = service.save(testTaxon).getUuid();
1590

    
1591
        Taxon topMost = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.FAMILY()), null);
1592

    
1593
        Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1594
        TaxonNode node =nodes.next();
1595
        Classification classification = node.getClassification();
1596
        classification.addParentChild(topMost, testTaxon, null, null);
1597
        UUID topMostUUID = service.save(topMost).getUuid();
1598

    
1599
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1600
        config.getTaxonNodeConfig().setChildHandling(ChildHandling.MOVE_TO_PARENT);
1601

    
1602

    
1603
        DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, classification.getUuid());
1604
        if(!result.isOk()){
1605
         	Assert.fail();
1606
       	}
1607

    
1608
        commitAndStartNewTransaction(null);
1609
        Taxon tax = (Taxon)service.find(uuid);
1610
        assertNull(tax);
1611
        tax = (Taxon)service.find(topMostUUID);
1612
        Set<TaxonNode> topMostNodes = tax.getTaxonNodes();
1613
        assertNotNull(topMostNodes);
1614
        assertEquals("there should be one taxon node", 1, topMostNodes.size());
1615
        nodes = topMostNodes.iterator();
1616
        TaxonNode topMostNode = nodes.next();
1617
        int size = topMostNode.getChildNodes().size();
1618

    
1619
        assertEquals(2, size);
1620
    }
1621

    
1622
    @Test
1623
    @DataSet(value="../../database/ClearDBDataSet.xml")
1624
    public final void testTaxonNodeDeletionConfiguratorDeleteChildren(){
1625
        //test childHandling DELETE:
1626
        Taxon testTaxon = getTestTaxon();
1627
        UUID uuid = service.save(testTaxon).getUuid();
1628

    
1629
        Taxon topMost = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.FAMILY()), null);
1630

    
1631
        Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1632
        TaxonNode node =nodes.next();
1633
        UUID taxonNodeUUID = node.getUuid();
1634
        Classification classification = node.getClassification();
1635
        classification.addParentChild(topMost, testTaxon, null, null);
1636
        UUID topMostUUID = service.save(topMost).getUuid();
1637

    
1638
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1639
        config.getTaxonNodeConfig().setChildHandling(ChildHandling.DELETE);
1640

    
1641
       // try {
1642
        DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, testTaxon.getTaxonNodes().iterator().next().getClassification().getUuid());
1643
        if(!result.isOk()){
1644
         	Assert.fail();
1645
       	}
1646
        commitAndStartNewTransaction(null);
1647
        Taxon tax = (Taxon)service.find(uuid);
1648
        assertNull(tax);
1649
        tax = (Taxon)service.find(topMostUUID);
1650
        Set<TaxonNode> topMostNodes = tax.getTaxonNodes();
1651
        assertNotNull(topMostNodes);
1652
        assertEquals("there should be one taxon node", 1, topMostNodes.size());
1653
        nodes = topMostNodes.iterator();
1654
        TaxonNode topMostNode = nodes.next();
1655
        int size = topMostNode.getChildNodes().size();
1656
        node = nodeService.find(taxonNodeUUID);
1657
        assertNull(node);
1658
        assertEquals(0, size);
1659
    }
1660

    
1661

    
1662
    @Test
1663
    @DataSet(value="../../database/ClearDBDataSet.xml")
1664
    public final void testTaxonDeletionConfiguratorDeleteMarker(){
1665
        //test childHandling DELETE:
1666
        Taxon testTaxon = getTestTaxon();
1667
        UUID uuid = service.save(testTaxon).getUuid();
1668

    
1669
        Taxon topMost = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.FAMILY()), null);
1670

    
1671
        Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1672
        TaxonNode node =nodes.next();
1673
        Classification classification = node.getClassification();
1674
        classification.addParentChild(topMost, testTaxon, null, null);
1675
        UUID topMostUUID = service.save(topMost).getUuid();
1676
        Marker marker = Marker.NewInstance(testTaxon, true, MarkerType.IS_DOUBTFUL());
1677
        testTaxon.addMarker(marker);
1678
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1679
        config.getTaxonNodeConfig().setChildHandling(ChildHandling.DELETE);
1680

    
1681
        DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, node.getClassification().getUuid());
1682

    
1683
        if(!result.isOk()){
1684
         	Assert.fail();
1685
       	}
1686
        commitAndStartNewTransaction(null);
1687
        Taxon tax = (Taxon)service.find(uuid);
1688
        assertNull(tax);
1689
        tax = (Taxon)service.find(topMostUUID);
1690
        Set<TaxonNode> topMostNodes = tax.getTaxonNodes();
1691
        assertNotNull(topMostNodes);
1692
        assertEquals("there should be one taxon node", 1, topMostNodes.size());
1693
        nodes = topMostNodes.iterator();
1694
        TaxonNode topMostNode = nodes.next();
1695
        int size = topMostNode.getChildNodes().size();
1696

    
1697
        assertEquals(0, size);
1698
    }
1699

    
1700

    
1701
    @Test
1702
    @DataSet(value="../../database/ClearDBDataSet.xml")
1703
    public final void testTaxonDeletionConfiguratorTaxonWithMisappliedName(){
1704

    
1705
        Taxon testTaxon = getTestTaxon();
1706
        UUID uuid = service.save(testTaxon).getUuid();
1707

    
1708
        Taxon misappliedName = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()), null);
1709

    
1710
        Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1711
        TaxonNode node =nodes.next();
1712
        testTaxon.addMisappliedName(misappliedName, null, null);
1713
        UUID misappliedNameUUID = service.save(misappliedName).getUuid();
1714

    
1715
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1716
        config.setDeleteMisappliedNames(true);
1717

    
1718
        DeleteResult result  = service.deleteTaxon(testTaxon.getUuid(), config, node.getClassification().getUuid());
1719
        if(!result.isOk()){
1720
         	Assert.fail();
1721
       	}
1722
        commitAndStartNewTransaction(null);
1723
        Taxon tax = (Taxon)service.find(uuid);
1724
        assertNull(tax);
1725
        tax = (Taxon)service.find(misappliedNameUUID);
1726
        //TODO: is that correct or should it be deleted because there is no relation to anything
1727
        assertNull(tax);
1728

    
1729
    }
1730
    @Test
1731
    @DataSet(value="../../database/ClearDBDataSet.xml")
1732
    public final void testTaxonDeletionConfiguratorTaxonWithMisappliedNameDoNotDelete(){
1733

    
1734
        Taxon testTaxon = getTestTaxon();
1735
        UUID uuid = service.save(testTaxon).getUuid();
1736

    
1737
        Taxon misappliedName = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()), null);
1738

    
1739
        Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1740
        TaxonNode node =nodes.next();
1741
        testTaxon.addMisappliedName(misappliedName, null, null);
1742
        UUID misappliedNameUUID = service.save(misappliedName).getUuid();
1743

    
1744
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1745
        config.setDeleteMisappliedNames(false);
1746

    
1747
        DeleteResult result = service.deleteTaxon(testTaxon.getUuid(), config, node.getClassification().getUuid());
1748
        if(!result.isOk()){
1749
         	Assert.fail();
1750
       	}
1751
        commitAndStartNewTransaction(null);
1752
        Taxon tax = (Taxon)service.find(uuid);
1753
        assertNull(tax);
1754
        tax = (Taxon)service.find(misappliedNameUUID);
1755
        //TODO: is that correct or should it be deleted because there is no relation to anything
1756
        assertNotNull(tax);
1757

    
1758
    }
1759

    
1760
    @Test
1761
    @DataSet(value="../../database/ClearDBDataSet.xml")
1762
    public final void testTaxonDeletionConfiguratorTaxonMisappliedName(){
1763

    
1764
        Taxon testTaxon = getTestTaxon();
1765
        UUID uuid = service.save(testTaxon).getUuid();
1766

    
1767
        Taxon misappliedNameTaxon = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()), null);
1768

    
1769
        Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1770
        TaxonNode node =nodes.next();
1771
        testTaxon.addMisappliedName(misappliedNameTaxon, null, null);
1772
        UUID misappliedNameUUID = service.save(misappliedNameTaxon).getUuid();
1773
        misappliedNameTaxon = (Taxon)service.find(misappliedNameUUID);
1774
        UUID misNameUUID = misappliedNameTaxon.getName().getUuid();
1775

    
1776
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1777

    
1778

    
1779
       // try {
1780
            service.deleteTaxon(misappliedNameTaxon.getUuid(), config,null);
1781
       // } catch (DataChangeNoRollbackException e) {
1782
         //   e.printStackTrace();
1783

    
1784
        //}
1785

    
1786
        commitAndStartNewTransaction(null);
1787
        Taxon tax = (Taxon)service.find(uuid);
1788
        assertNotNull(tax);
1789
        tax = (Taxon)service.find(misappliedNameUUID);
1790
        IBotanicalName name = nameService.find(misNameUUID);
1791

    
1792
        assertNull(tax);
1793
        assertNull(name);
1794

    
1795
    }
1796

    
1797
    @Test
1798
    @DataSet(value="../../database/ClearDBDataSet.xml")
1799
    public final void testLlistIncludedTaxa(){
1800
    	Reference citation = null;
1801
    	String microcitation = null;
1802

    
1803
    	//Data
1804
    	Classification cl1 = Classification.NewInstance("testClassification1");
1805
    	Classification cl2 = Classification.NewInstance("testClassification2");
1806
    	Classification cl3 = Classification.NewInstance("testClassification3");
1807

    
1808
    	classificationService.save(cl1);
1809
        classificationService.save(cl2);
1810
        classificationService.save(cl3);
1811

    
1812
    	Taxon c1Genus = Taxon.NewInstance(null, null);c1Genus.setUuid(UUID.fromString("daa24f6f-7e38-4668-b385-10c789212e4e"));
1813
    	Taxon c1Species = Taxon.NewInstance(null, null);c1Species.setUuid(UUID.fromString("1c1d0566-67d0-4806-bf23-ecf55f4b9118"));
1814
    	Taxon c1SubSpecies1 = Taxon.NewInstance(null, null);c1SubSpecies1.setUuid(UUID.fromString("96ae2fad-76df-429f-b179-42e00838fea4"));
1815
    	Taxon c1SubSpecies2 = Taxon.NewInstance(null, null);c1SubSpecies2.setUuid(UUID.fromString("5d3f6147-ca72-40e0-be8a-6c835a09a579"));
1816
    	TaxonNode c1childNodeSpecies1 = cl1.addParentChild(c1Genus, c1Species, null, null);
1817
    	nodeService.saveOrUpdate(c1childNodeSpecies1);
1818
    	TaxonNode c1childNodeSubSpecies1 =cl1.addParentChild(c1Species, c1SubSpecies1, null, null);
1819
    	nodeService.saveOrUpdate(c1childNodeSubSpecies1);
1820
    	TaxonNode c1childNodeSubSpecies2 =cl1.addParentChild(c1Species, c1SubSpecies2, null, null);
1821
    	nodeService.saveOrUpdate(c1childNodeSubSpecies2);
1822

    
1823
    	Taxon c2Genus = Taxon.NewInstance(null, null);c2Genus.setUuid(UUID.fromString("ed0ec006-3ac8-4a12-ae13-fdf2a13dedbe"));
1824
    	Taxon c2Species = Taxon.NewInstance(null, null);c2Species.setUuid(UUID.fromString("1027eb18-1c26-450e-a299-981b775ebc3c"));
1825
    	Taxon c2SubSpecies1 = Taxon.NewInstance(null, null);c2SubSpecies1.setUuid(UUID.fromString("61f039c8-01f3-4f5d-8e16-1602139774e7"));
1826
    	Taxon c2SubSpecies2 = Taxon.NewInstance(null, null);c2SubSpecies2.setUuid(UUID.fromString("2ed6b6f8-05f9-459a-a075-2bca57e3013e"));
1827
    	TaxonNode c2childNodeSpecies1 = cl2.addParentChild(c2Genus, c2Species, null, null);
1828
    	nodeService.saveOrUpdate(c2childNodeSpecies1);
1829
    	TaxonNode c2childNodeSubSpecies1 = cl2.addParentChild(c2Species, c2SubSpecies1, null, null);
1830
    	nodeService.saveOrUpdate(c2childNodeSubSpecies1);
1831
    	TaxonNode c2childNodeSubSpecies2 = cl2.addParentChild(c2Species, c2SubSpecies2, null, null);
1832
    	nodeService.saveOrUpdate(c2childNodeSubSpecies2);
1833

    
1834
    	Taxon c3Genus = Taxon.NewInstance(null, null);c3Genus.setUuid(UUID.fromString("407dfc8d-7a4f-4370-ada4-76c1a8279d1f"));
1835
    	Taxon c3Species = Taxon.NewInstance(null, null);c3Species.setUuid(UUID.fromString("b6d34fc7-4aa7-41e5-b633-86f474edbbd5"));
1836
    	Taxon c3SubSpecies1 = Taxon.NewInstance(null, null);c3SubSpecies1.setUuid(UUID.fromString("01c07585-a422-40cd-9339-a74c56901d9f"));
1837
    	Taxon c3SubSpecies2 = Taxon.NewInstance(null, null);c3SubSpecies2.setUuid(UUID.fromString("390c8e23-e05f-4f89-b417-50cf080f4c91"));
1838
    	TaxonNode c3childNodeSpecies1 = cl3.addParentChild(c3Genus, c3Species, null, null);
1839
    	nodeService.saveOrUpdate(c3childNodeSpecies1);
1840
    	TaxonNode c3childNodeSubSpecies1 = cl3.addParentChild(c3Species, c3SubSpecies1, null, null);
1841
    	nodeService.saveOrUpdate(c3childNodeSubSpecies1);
1842
    	TaxonNode c3childNodeSubSpecies2 = cl3.addParentChild(c3Species, c3SubSpecies2, null, null);
1843
    	nodeService.saveOrUpdate(c3childNodeSubSpecies2);
1844

    
1845

    
1846

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

    
1851
    	TaxonRelationship rel = c1Species.addTaxonRelation(c2Species, TaxonRelationshipType.CONGRUENT_TO(), citation, microcitation);
1852
    	rel.setDoubtful(true);
1853
    	c1Species.addTaxonRelation(c4Species, TaxonRelationshipType.INCLUDES(), citation, microcitation);
1854
    	c2Species.addTaxonRelation(c1SubSpecies2, TaxonRelationshipType.INCLUDES(), citation, microcitation);
1855

    
1856
    	service.saveOrUpdate(c1Species);
1857
       	service.saveOrUpdate(c2Species);
1858
       	service.save(c4Species);
1859

    
1860
    	//Tests
1861
       	//default starting at species 1
1862
       	IncludedTaxaDTO dto = service.listIncludedTaxa(c1Species.getUuid(), new IncludedTaxonConfiguration(null, true, false));
1863
    	Assert.assertNotNull("IncludedTaxaDTO", dto);
1864
    	Assert.assertEquals("Result should contain 7 taxa: c1Species", 7, dto.getIncludedTaxa().size());
1865
    	Assert.assertNotNull("date should not be null", dto.getDate());
1866
//    	Assert.assertTrue(dto.contains(taxonUuid));
1867
        //same without doubtful
1868
    	dto = service.listIncludedTaxa(c1Species.getUuid(), new IncludedTaxonConfiguration(null, false, false));
1869
    	Assert.assertEquals(4, dto.getIncludedTaxa().size());
1870

    
1871
    	//other example starting at Genus2
1872
    	dto = service.listIncludedTaxa(c2Genus.getUuid(), new IncludedTaxonConfiguration(null, true, false));
1873
    	Assert.assertEquals(8, dto.getIncludedTaxa().size());
1874
    	//same without doubtful
1875
    	dto = service.listIncludedTaxa(c2Genus.getUuid(), new IncludedTaxonConfiguration(null, false, false));
1876
    	Assert.assertEquals(5, dto.getIncludedTaxa().size());
1877

    
1878
    	//only congruent
1879
    	dto = service.listIncludedTaxa(c1Species.getUuid(), new IncludedTaxonConfiguration(null, true, true));
1880
    	Assert.assertEquals(2, dto.getIncludedTaxa().size());
1881
    	//same without doubtful
1882
    	dto = service.listIncludedTaxa(c1Species.getUuid(), new IncludedTaxonConfiguration(null, false, true));
1883
    	Assert.assertEquals(1, dto.getIncludedTaxa().size());
1884
    }
1885

    
1886
    @Test
1887
    public void testDeleteDescriptions(){
1888
    	try {
1889
			createTestDataSet();
1890
		} catch (FileNotFoundException e) {
1891
			// TODO Auto-generated catch block
1892
			e.printStackTrace();
1893
		}
1894
    	TaxonDescription description = TaxonDescription.NewInstance(taxWithoutSyn);
1895
    	SpecimenOrObservationBase<IIdentifiableEntityCacheStrategy<FieldUnit>> specimen = FieldUnit.NewInstance();
1896
    	UUID uuid = occurenceService.saveOrUpdate(specimen);
1897
    	DescriptionElementBase element = IndividualsAssociation.NewInstance(specimen);
1898
    	description.addElement(element);
1899
    	service.saveOrUpdate(taxWithoutSyn);
1900

    
1901

    
1902
    	Taxon tax = (Taxon)service.find(uuidTaxWithoutSyn);
1903
    	Set<TaxonDescription> descr =  tax.getDescriptions();
1904
    	assertEquals(1, descr.size());
1905
    	description = descr.iterator().next();
1906
    	UUID uuidDescr = description.getUuid();
1907
    	UUID uuidDescEl = description.getElements().iterator().next().getUuid();
1908

    
1909
    	descriptionService.deleteDescription(description);
1910
    	service.saveOrUpdate(tax);
1911

    
1912
    	description = (TaxonDescription) descriptionService.find(uuidDescr);
1913
    	specimen = occurenceService.find(uuid);
1914
    	assertNull(description);
1915
    	DeleteResult result = occurenceService.delete(specimen);
1916
    	assertTrue(result.isOk());
1917

    
1918
    }
1919

    
1920
    @Test
1921
    public void testRemoveDescriptionsFromTaxa(){
1922
        try {
1923
            createTestDataSet();
1924
        } catch (FileNotFoundException e) {
1925
            // TODO Auto-generated catch block
1926
            e.printStackTrace();
1927
        }
1928
        TaxonDescription description = TaxonDescription.NewInstance(taxWithoutSyn);
1929
        SpecimenOrObservationBase<IIdentifiableEntityCacheStrategy<FieldUnit>> specimen = FieldUnit.NewInstance();
1930
        UUID uuid = occurenceService.saveOrUpdate(specimen);
1931
        DescriptionElementBase element = IndividualsAssociation.NewInstance(specimen);
1932
        description.addElement(element);
1933
        service.saveOrUpdate(taxWithoutSyn);
1934

    
1935

    
1936
        Taxon tax = (Taxon)service.find(uuidTaxWithoutSyn);
1937
        Set<TaxonDescription> descr =  tax.getDescriptions();
1938
        assertEquals(1, descr.size());
1939
        description = descr.iterator().next();
1940
        UUID uuidDescr = description.getUuid();
1941

    
1942

    
1943
        tax.removeDescription(description, true);
1944
        service.saveOrUpdate(tax);
1945

    
1946
        description = (TaxonDescription) descriptionService.find(uuidDescr);
1947
        specimen = occurenceService.find(uuid);
1948
        assertNotNull(description);
1949
        DeleteResult result = occurenceService.delete(specimen);
1950
        assertTrue(result.isOk());
1951

    
1952
    }
1953

    
1954

    
1955
    @Override
1956
    public void createTestDataSet() throws FileNotFoundException {
1957
    	Rank rank = Rank.SPECIES();
1958

    
1959
        taxWithoutSyn = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test1", null, null, null, null, null, null, null), null);
1960
        taxWithSyn = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test3", null, null, null, null, null, null, null), null);
1961
        tax2WithSyn = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test5", null, null, null, null, null, null, null), null);
1962
        synonym = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test2", null, null, null, null, null, null, null), null);
1963
        synonym2 = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(rank, "Test4", null, null, null, null, null, null, null), null);
1964
        synonym2.getName().setHomotypicalGroup(synonym.getHomotypicGroup());
1965

    
1966
        taxWithSyn.addSynonym(synonym, SynonymType.HETEROTYPIC_SYNONYM_OF());
1967
        taxWithSyn.addSynonym(synonym2, SynonymType.HETEROTYPIC_SYNONYM_OF());
1968

    
1969
        uuidTaxWithoutSyn = service.save(taxWithoutSyn).getUuid();
1970
        uuidSyn = service.save(synonym).getUuid();
1971
        uuidSyn2 = service.save(synonym2).getUuid();
1972
        uuidTaxWithSyn =service.save(taxWithSyn).getUuid();
1973

    
1974
    }
1975

    
1976

    
1977

    
1978
        //public static UUID DESCRIPTION1_UUID = UUID.fromString("f3e061f6-c5df-465c-a253-1e18ab4c7e50");
1979
        //public static UUID DESCRIPTION2_UUID = UUID.fromString("1b009a40-ebff-4f7e-9f7f-75a850ba995d");
1980

    
1981

    
1982

    
1983
        private final Random rnd = new Random();
1984

    
1985
        public Taxon getTestTaxon(){
1986
            int descrIndex = 6000;
1987
            Person deCandolle = Person.NewInstance();
1988
            deCandolle.setTitleCache("DC.", true);
1989

    
1990
            Reference sec = ReferenceFactory.newDatabase();
1991
            sec.setTitleCache("Flora lunaea", true);
1992
            Reference citationRef = ReferenceFactory.newBook();
1993
            citationRef.setTitleCache("Sp. lunarum", true);
1994

    
1995
            //genus taxon with Name, combinationAuthor,
1996
            IBotanicalName botName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
1997
            botName.setTitleCache("Hieracium L.", true);
1998
            botName.setGenusOrUninomial("Hieracium");
1999
            botName.setCombinationAuthorship(Person.NewInstance());
2000
            botName.getCombinationAuthorship().setNomenclaturalTitle("L.");
2001
            botName.setUuid(GENUS_NAME_UUID);
2002
            Taxon genusTaxon = Taxon.NewInstance(botName, sec);
2003
            genusTaxon.setUuid(GENUS_UUID);
2004
            service.save(genusTaxon);
2005
            //a name that is the basionym of genusTaxon's name
2006
            TaxonName basionym = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
2007
            basionym.setTitleCache("Hieracilla DC.", true);
2008
            basionym.setGenusOrUninomial("Hieracilla");
2009
            basionym.setCombinationAuthorship(deCandolle);
2010
            basionym.setUuid(BASIONYM_UUID);
2011
            botName.addBasionym(basionym, null, null,"216", null);
2012
            nameService.saveOrUpdate(basionym);
2013
            //species taxon that is the child of genus taxon
2014
            IBotanicalName botSpecies = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
2015
            botSpecies.setTitleCache("Hieracium asturianum Pau", true);
2016
            botSpecies.setGenusOrUninomial("Hieracium");
2017
            botSpecies.setSpecificEpithet("asturianum");
2018
            botSpecies.setCombinationAuthorship(Person.NewInstance());
2019
            botSpecies.getCombinationAuthorship().setNomenclaturalTitle("Pau");
2020
            botSpecies.setUuid(SPECIES1_NAME_UUID);
2021
            Taxon childTaxon = Taxon.NewInstance(botSpecies, sec);
2022
            childTaxon.setUuid(SPECIES1_UUID);
2023
            TaxonDescription taxDesc = getTestDescription(descrIndex++);
2024
            //taxDesc.setUuid(DESCRIPTION1_UUID);
2025
            childTaxon.addDescription(taxDesc);
2026
            service.saveOrUpdate(childTaxon);
2027
            Classification classification = getTestClassification("TestClassification");
2028
            classification.addParentChild(genusTaxon, childTaxon, citationRef, "456");
2029
//            childTaxon.setTaxonomicParent(genusTaxon, citationRef, "456");
2030
            classificationService.save(classification);
2031
            //homotypic synonym of childTaxon1
2032
            IBotanicalName botSpecies4= TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
2033
            botSpecies4.setTitleCache("Hieracium gueri DC.", true);
2034
            botSpecies4.setGenusOrUninomial("Hieracium");
2035
            botSpecies4.setSpecificEpithet("gueri");
2036
            botSpecies4.setCombinationAuthorship(deCandolle);
2037
            botSpecies4.setUuid(SYNONYM_NAME_UUID);
2038
            Synonym homoSynonym = Synonym.NewInstance(botSpecies4, sec);
2039
            childTaxon.addSynonym(homoSynonym, SynonymType.HOMOTYPIC_SYNONYM_OF());
2040
            service.saveOrUpdate(childTaxon);
2041

    
2042
            //2nd child species taxon that is the child of genus taxon
2043
            IBotanicalName botSpecies2= TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
2044
            botSpecies2.setTitleCache("Hieracium wolffii Zahn", true);
2045
            botSpecies2.setGenusOrUninomial("Hieracium");
2046
            botSpecies2.setSpecificEpithet("wolffii");
2047
            botSpecies2.setCombinationAuthorship(Person.NewInstance());
2048
            botSpecies2.getCombinationAuthorship().setNomenclaturalTitle("Zahn");
2049
            botSpecies2.setUuid(SPECIES2_NAME_UUID);
2050
            Taxon childTaxon2 = Taxon.NewInstance(botSpecies2, sec);
2051
            childTaxon2.setUuid(SPECIES2_UUID);
2052
            classification.addParentChild(genusTaxon, childTaxon2, citationRef, "499");
2053
            //childTaxon2.setTaxonomicParent(genusTaxon, citationRef, "499");
2054
            service.saveOrUpdate(childTaxon2);
2055
            //heterotypic synonym of childTaxon2
2056
            IBotanicalName botSpecies3= TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
2057
            botSpecies3.setTitleCache("Hieracium lupium DC.", true);
2058
            botSpecies3.setGenusOrUninomial("Hieracium");
2059
            botSpecies3.setSpecificEpithet("lupium");
2060
            botSpecies3.setCombinationAuthorship(deCandolle);
2061
            botSpecies3.setUuid(SYNONYM2_NAME_UUID);
2062
            Synonym heteroSynonym = Synonym.NewInstance(botSpecies3, sec);
2063
            childTaxon2.addSynonym(heteroSynonym, SynonymType.HETEROTYPIC_SYNONYM_OF());
2064
            service.saveOrUpdate(childTaxon2);
2065
            //missaplied Name for childTaxon2
2066
            IBotanicalName missName= TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
2067
            missName.setTitleCache("Hieracium lupium DC.", true);
2068
            missName.setGenusOrUninomial("Hieracium");
2069
            missName.setSpecificEpithet("lupium");
2070
            missName.setCombinationAuthorship(deCandolle);
2071
            missName.setUuid(SPECIES5_NAME_UUID);
2072
            Taxon misappliedNameTaxon = Taxon.NewInstance(missName, sec);
2073
            childTaxon2.addMisappliedName(misappliedNameTaxon, citationRef, "125");
2074
            taxDesc = getTestDescription(descrIndex++);
2075
           // taxDesc.setUuid(DESCRIPTION2_UUID);
2076
            genusTaxon.addDescription(taxDesc);
2077
            service.saveOrUpdate(genusTaxon);
2078
            service.save(misappliedNameTaxon);
2079

    
2080
            return genusTaxon;
2081
        }
2082

    
2083
        public TaxonDescription getTestDescription(int index){
2084
            TaxonDescription taxonDescription = TaxonDescription.NewInstance();
2085
            Language language = Language.DEFAULT();
2086
            //taxonDescription.setId(index);
2087

    
2088
            //textData
2089
            TextData textData = TextData.NewInstance();
2090
            String descriptionText = "this is a desciption for a taxon";
2091
            LanguageString languageString = LanguageString.NewInstance(descriptionText, language);
2092
            textData.putText(languageString);
2093
            taxonDescription.addElement(textData);
2094

    
2095
            //commonName
2096

    
2097
            String commonNameString = "Schönveilchen";
2098
            CommonTaxonName commonName = CommonTaxonName.NewInstance(commonNameString, language);
2099
            taxonDescription.addElement(commonName);
2100

    
2101
            return taxonDescription;
2102
        }
2103

    
2104
        public Classification getTestClassification(String name){
2105
            return Classification.NewInstance(name);
2106
        }
2107
}
(31-31/40)