Project

General

Profile

Download (28.6 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2013 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.api.service.description;
10

    
11
import static org.junit.Assert.assertEquals;
12
import static org.junit.Assert.assertNotNull;
13
import static org.junit.Assert.assertNull;
14
import static org.junit.Assert.assertTrue;
15

    
16
import java.io.FileNotFoundException;
17
import java.util.Arrays;
18
import java.util.Collection;
19
import java.util.HashSet;
20
import java.util.Iterator;
21
import java.util.List;
22
import java.util.Set;
23
import java.util.UUID;
24

    
25
import org.apache.log4j.Logger;
26
import org.junit.Before;
27
import org.junit.Ignore;
28
import org.junit.Test;
29
import org.unitils.dbunit.annotation.DataSet;
30
import org.unitils.dbunit.annotation.DataSets;
31
import org.unitils.spring.annotation.SpringBeanByType;
32

    
33
import eu.etaxonomy.cdm.api.service.IClassificationService;
34
import eu.etaxonomy.cdm.api.service.IReferenceService;
35
import eu.etaxonomy.cdm.api.service.ITaxonService;
36
import eu.etaxonomy.cdm.api.service.ITermService;
37
import eu.etaxonomy.cdm.api.service.description.TransmissionEngineDistribution;
38
import eu.etaxonomy.cdm.api.service.description.TransmissionEngineDistribution.AggregationMode;
39
import eu.etaxonomy.cdm.common.JvmLimitsException;
40
import eu.etaxonomy.cdm.model.common.Extension;
41
import eu.etaxonomy.cdm.model.common.MarkerType;
42
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
43
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
44
import eu.etaxonomy.cdm.model.description.Distribution;
45
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
46
import eu.etaxonomy.cdm.model.description.TaxonDescription;
47
import eu.etaxonomy.cdm.model.location.NamedArea;
48
import eu.etaxonomy.cdm.model.name.IBotanicalName;
49
import eu.etaxonomy.cdm.model.name.Rank;
50
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
51
import eu.etaxonomy.cdm.model.reference.Reference;
52
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
53
import eu.etaxonomy.cdm.model.taxon.Classification;
54
import eu.etaxonomy.cdm.model.taxon.Taxon;
55
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
56
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
57
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
58

    
59
/**
60
 * @author a.kohlbecker
61
 * @since Feb 26, 2013
62
 *
63
 */
64
public class TransmissionEngineDistributionTest extends CdmTransactionalIntegrationTest {
65

    
66
    private static Logger logger = Logger.getLogger(TransmissionEngineDistributionTest.class);
67

    
68
    private static final UUID T_LAPSANA_UUID = UUID.fromString("f65d47bd-4f49-4ab1-bc4a-bc4551eaa1a8");
69

    
70
    private static final UUID T_LAPSANA_COMMUNIS_UUID = UUID.fromString("2a5ceebb-4830-4524-b330-78461bf8cb6b");
71

    
72
    private static final UUID T_LAPSANA_COMMUNIS_COMMUNIS_UUID = UUID.fromString("441a3c40-0c84-11de-8c30-0800200c9a66");
73

    
74
    private static final UUID T_LAPSANA_COMMUNIS_ADENOPHORA_UUID = UUID.fromString("e4acf200-63b6-11dd-ad8b-0800200c9a66");
75

    
76
    private static final UUID T_LAPSANA_COMMUNIS_ALPINA_UUID = UUID.fromString("596b1325-be50-4b0a-9aa2-3ecd610215f2");
77

    
78
    private static final UUID CLASSIFICATION_UUID = UUID.fromString("4b266053-a841-4980-b548-3f21d8d7d712");
79

    
80

    
81
    @SpringBeanByType
82
    private ITermService termService;
83

    
84
    @SpringBeanByType
85
    private ITaxonService taxonService;
86

    
87
    @SpringBeanByType
88
    private IClassificationService classificationService;
89

    
90
    @SpringBeanByType
91
    private IReferenceService referenceService;
92

    
93
    @SpringBeanByType
94
    private TransmissionEngineDistribution engine;
95

    
96
    // --- Distributions --- //
97
    // tdwg3 level YUG :  Yugoslavia
98
    // contains tdwg4 level areas :
99
    //   YUG-BH	Bosnia-Herzegovina
100
    //   YUG-CR	Croatia
101
    //   YUG-KO	Kosovo
102
    //   YUG-MA	Macedonia
103
    //   YUG-MN	Montenegro
104
    private NamedArea yug = null;
105
    private NamedArea yug_bh = null;
106
    private NamedArea yug_cr = null;
107
    private NamedArea yug_ko = null;
108
    private NamedArea yug_ma = null;
109
    private NamedArea yug_mn = null;
110

    
111
    List<NamedArea> superAreas = null;
112
    Rank upperRank = null;
113
    Rank lowerRank = null;
114

    
115
    private Classification classification;
116

    
117
    private Reference book_a = null;
118
    private Reference book_b = null;
119

    
120

    
121
    @Before
122
    public void setUp() {
123

    
124
        superAreas = Arrays.asList(new NamedArea[]{
125
        		termService.getAreaByTdwgAbbreviation("YUG")
126
        });
127
        lowerRank = Rank.SUBSPECIES();
128
        upperRank = Rank.GENUS();
129

    
130
        classification = classificationService.load(CLASSIFICATION_UUID);
131

    
132
        yug = termService.getAreaByTdwgAbbreviation("YUG");
133
        yug_bh = termService.getAreaByTdwgAbbreviation("YUG-BH");
134
        yug_cr = termService.getAreaByTdwgAbbreviation("YUG-CR");
135
        yug_ko = termService.getAreaByTdwgAbbreviation("YUG-KO");
136
        yug_ma = termService.getAreaByTdwgAbbreviation("YUG-MA");
137
        yug_mn = termService.getAreaByTdwgAbbreviation("YUG-MN");
138

    
139
        book_a = ReferenceFactory.newBook();
140
        book_a.setTitle("book_a");
141
        book_b = ReferenceFactory.newBook();
142
        book_b.setTitle("book_a");
143

    
144
        engine.setBatchMinFreeHeap(100 * 1024 * 1024);
145
        engine.updatePriorities();
146
    }
147

    
148
    @Test
149
    @DataSet
150
    public void testPriorities(){
151

    
152
        Set<Extension> extensions = termService.load(PresenceAbsenceTerm.CULTIVATED().getUuid()).getExtensions();
153
        assertEquals(TransmissionEngineDistribution.EXTENSION_VALUE_PREFIX + "45", extensions.iterator().next().getValue());
154
    }
155

    
156
    @Test
157
    @DataSets({
158
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
159
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
160
        @DataSet(value="TransmissionEngineDistributionTest.xml"),
161
    })
162
//  @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class) //, value="./ClearDBDataSet.xml")
163
    public void test_ignore() throws JvmLimitsException {
164

    
165
        addDistributions(
166
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
167
                Arrays.asList(new Distribution[] {
168
                        // should succeed during area aggregation be ignored by rank aggregation
169
                        // => yug will get status ENDEMIC_FOR_THE_RELEVANT_AREA
170
                        //    but only for LAPSANA_COMMUNIS_ALPINA
171
                        Distribution.NewInstance(yug_mn, PresenceAbsenceTerm.ENDEMIC_FOR_THE_RELEVANT_AREA()),
172
                        // should be ignored by area aggregation
173
                        // => LAPSANA_COMMUNIS will wave distribution with yug_ko and INTRODUCED_FORMERLY_INTRODUCED
174
                        Distribution.NewInstance(yug_ko, PresenceAbsenceTerm.INTRODUCED_FORMERLY_INTRODUCED()),
175
               })
176
            );
177

    
178
        engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
179

    
180
        Taxon lapsana_communis_alpina  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_ALPINA_UUID);
181
        assertEquals(2, lapsana_communis_alpina.getDescriptions().size());
182
        // TODO test for yug => ENDEMIC_FOR_THE_RELEVANT_AREA in computed description
183

    
184
        Taxon lapsana_communis  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_UUID);
185
        assertEquals(1, lapsana_communis.getDescriptions().size());
186
        TaxonDescription description = lapsana_communis.getDescriptions().iterator().next();
187
        assertEquals(1, description.getElements().size());
188
        int numExpectedFound = 0;
189
        for (DescriptionElementBase element : description.getElements()){
190
            Distribution distribution = (Distribution)element;
191
            if(distribution.getArea().equals(yug_ko)){
192
                numExpectedFound++;
193
                assertEquals("aggregated status of area YUG-KO wrong", PresenceAbsenceTerm.INTRODUCED_FORMERLY_INTRODUCED().getLabel(), distribution.getStatus().getLabel());
194
            }
195
        }
196
        assertEquals("All three expected areas should have been found before", numExpectedFound, 1);
197
    }
198

    
199
    @Test
200
    @DataSets({
201
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
202
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
203
        @DataSet(value="TransmissionEngineDistributionTest.xml"),
204
    })
205
    public void testArea_area() throws JvmLimitsException {
206

    
207
        Set<Distribution> distributions_LCA = new HashSet<>();
208

    
209
        distributions_LCA.add(newDistribution(book_a, yug_mn, PresenceAbsenceTerm.CULTIVATED(), "1"));
210
        distributions_LCA.add(newDistribution(book_a, yug_ko, PresenceAbsenceTerm.NATIVE(), "2")); // NATIVE should succeed
211
        distributions_LCA.add(newDistribution(book_a, yug_bh, PresenceAbsenceTerm.INTRODUCED(), "3"));
212
        distributions_LCA.add(newDistribution(book_a, yug_ma, PresenceAbsenceTerm.NATIVE(), "4")); // NATIVE should succeed
213

    
214
        addDistributions(
215
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
216
                distributions_LCA
217
            );
218

    
219
        Taxon lapsana_communis_alpina  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_ALPINA_UUID);
220
        assertEquals(1, lapsana_communis_alpina.getDescriptions().size());
221

    
222
        engine.accumulate(AggregationMode.byAreas, superAreas, lowerRank, upperRank, classification, null);
223

    
224
        lapsana_communis_alpina  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_ALPINA_UUID);
225
        assertEquals(2, lapsana_communis_alpina.getDescriptions().size());
226

    
227
        Distribution accumulatedDistribution = null;
228
        for (TaxonDescription description : lapsana_communis_alpina.getDescriptions()) {
229
            if(description.hasMarker(MarkerType.COMPUTED(), true)) {
230
                assertNull("only one computed Distribution should exists", accumulatedDistribution);
231
                assertEquals("the computed Decsription should have only one element", 1, description.getElements().size());
232
                accumulatedDistribution = (Distribution) description.getElements().iterator().next();
233
                assertEquals("Expecting area to be YUG", yug, accumulatedDistribution.getArea());
234
                assertEquals("Expecting status to be NATIVE", PresenceAbsenceTerm.NATIVE().getLabel(), accumulatedDistribution.getStatus().getLabel());
235
            }
236
        }
237
        assertNotNull("The area YUG should have been found", accumulatedDistribution);
238
        assertEquals("Expecting two source references", 2, accumulatedDistribution.getSources().size());
239
        Iterator<DescriptionElementSource> sourceIt = accumulatedDistribution.getSources().iterator();
240
        // should contain source_LCA_yug_ma and source_LCA_yug_ko, testing the microreference which is unique in the tests
241
        assertTrue(" 2  4 ".contains(" " + sourceIt.next().getCitationMicroReference() + " "));
242
        assertTrue(" 2  4 ".contains(" " + sourceIt.next().getCitationMicroReference() + " "));
243
    }
244

    
245
    @Test
246
    @DataSets({
247
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
248
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
249
        @DataSet(value="TransmissionEngineDistributionTest.xml"),
250
    })
251
    public void testArea_rank_and_area_1() throws JvmLimitsException {
252

    
253
        Set<Distribution> distributions_LCA = new HashSet<>();
254
        distributions_LCA.add(newDistribution(book_a, yug_mn, PresenceAbsenceTerm.CULTIVATED(), "1"));
255
        distributions_LCA.add(newDistribution(book_a, yug_ko, PresenceAbsenceTerm.NATIVE(), "2")); // NATIVE should succeed
256

    
257
        addDistributions(
258
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
259
                distributions_LCA
260
            );
261

    
262
        Set<Distribution> distributions_LC = new HashSet<>();
263
        distributions_LC.add(newDistribution(book_a, yug_mn, PresenceAbsenceTerm.CULTIVATED(), "3"));
264
        distributions_LC.add(newDistribution(book_a, yug_ko, PresenceAbsenceTerm.NATIVE(), "4")); // NATIVE should succeed
265

    
266
        commitAndStartNewTransaction(null);
267

    
268
        addDistributions(
269
                T_LAPSANA_COMMUNIS_UUID,
270
                distributions_LC
271
            );
272

    
273
        engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
274

    
275
        Taxon lapsana_communis  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_UUID);
276
        assertEquals("Lapsana communis alpina must only have 2 Descriptions", 2, lapsana_communis.getDescriptions().size());
277

    
278
        Taxon lapsana = (Taxon) taxonService.load(T_LAPSANA_UUID);
279
        assertEquals("Lapsana communis must only have 1 Description", 1, lapsana.getDescriptions().size());
280
        TaxonDescription description = lapsana.getDescriptions().iterator().next();
281
        assertTrue(description.hasMarker(MarkerType.COMPUTED(), true));
282
        assertEquals(3, description.getElements().size());
283
        int numExpectedFound = 0;
284
        for (DescriptionElementBase element : description.getElements()){
285
            Distribution distribution = (Distribution)element;
286
            if(distribution.getArea().equals(yug)){
287
                numExpectedFound++;
288
                assertEquals("aggregated status of area YUG is wrong", PresenceAbsenceTerm.NATIVE().getLabel(), distribution.getStatus().getLabel());
289
                assertEquals(2, distribution.getSources().size());
290
                Iterator<DescriptionElementSource> sourceIt = distribution.getSources().iterator();
291
                // should contain source_LCA_yug_ma and source_LCA_yug_ko, testing the microreference which is unique in the tests
292
                assertTrue(" 2  4 ".contains(" " + sourceIt.next().getCitationMicroReference() + " "));
293
                assertTrue(" 2  4 ".contains(" " + sourceIt.next().getCitationMicroReference() + " "));
294
            }
295
            if(distribution.getArea().equals(yug_mn)){
296
                numExpectedFound++;
297
                assertEquals("aggregated status of area YUG-MN is wrong", PresenceAbsenceTerm.CULTIVATED().getLabel(), distribution.getStatus().getLabel());
298
                assertEquals(2, distribution.getSources().size());
299
                Iterator<DescriptionElementSource> sourceIt = distribution.getSources().iterator();
300
                // should contain source_LCA_yug_ma and source_LCA_yug_ko, testing the microreference which is unique in the tests
301
                assertTrue(" 1  3 ".contains(" " + sourceIt.next().getCitationMicroReference() + " "));
302
                assertTrue(" 1  3 ".contains(" " + sourceIt.next().getCitationMicroReference() + " "));
303
            }
304
            if(distribution.getArea().equals(yug_ko)){
305
                numExpectedFound++;
306
                assertEquals("aggregated status of area YUG-KO wrong", PresenceAbsenceTerm.NATIVE().getLabel(), distribution.getStatus().getLabel());
307
                assertEquals(2, distribution.getSources().size());
308
                Iterator<DescriptionElementSource> sourceIt = distribution.getSources().iterator();
309
                // should contain source_LCA_yug_ma and source_LCA_yug_ko, testing the microreference which is unique in the tests
310
                assertTrue(" 2  4 ".contains(" " + sourceIt.next().getCitationMicroReference() + " "));
311
                assertTrue(" 2  4 ".contains(" " + sourceIt.next().getCitationMicroReference() + " "));
312
            }
313
        }
314
        assertEquals("All three expected areas should have been found before", numExpectedFound, 3);
315
    }
316

    
317
    /**
318
     * Variant of {@link #testArea_rank_and_area_1()} with alternate source references to check the
319
     * suppression of duplicates.
320
     *
321
     * This test relies on {@link #testArea_rank_and_area_1()}
322
     * an makes assertions only on the alternative source references
323
     * @throws JvmLimitsException
324
     */
325
    @Test
326
    @DataSets({
327
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
328
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
329
        @DataSet(value="TransmissionEngineDistributionTest.xml"),
330
    })
331
    public void testArea_rank_and_area_2() throws JvmLimitsException {
332

    
333
        Set<Distribution> distributions_LCA = new HashSet<Distribution>();
334
        distributions_LCA.add(newDistribution(book_a, yug_ko, PresenceAbsenceTerm.NATIVE(), "1"));
335
        distributions_LCA.add(newDistribution(book_b, yug_ko, PresenceAbsenceTerm.NATIVE(), "2"));
336

    
337
        addDistributions(
338
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
339
                distributions_LCA
340
            );
341

    
342
        engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
343

    
344
        Taxon lapsana_communis = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_UUID);
345
        int computedDescriptionsCnt = 0;
346
        for(TaxonDescription description : lapsana_communis.getDescriptions()) {
347
            if(description.hasMarker(MarkerType.COMPUTED(), true)) {
348
                computedDescriptionsCnt++;
349
                assertEquals(2, description.getElements().size()); // yug, yug_ko
350
                for(DescriptionElementBase distribution : description.getElements()) {
351
                    logger.debug(((Distribution)distribution).getArea() + " " + sourcesToString(distribution));
352
                    if(((Distribution)distribution).getArea().equals(yug_ko)){
353
                        assertEquals(2, distribution.getSources().size());
354
                    }
355
                    if(((Distribution)distribution).getArea().equals(yug)){
356
                        assertEquals(2, distribution.getSources().size());
357
                    }
358
                }
359
            }
360
        }
361
        assertEquals(1, computedDescriptionsCnt);
362
    }
363

    
364

    
365
    /**
366
     * Variant of {@link #testArea_rank_and_area_1()} with alternate source references to check the
367
     * suppression of duplicates.
368
     *
369
     * This test relies on {@link #testArea_rank_and_area_1()}
370
     * an makes assertions only on the alternative source references
371
     * @throws JvmLimitsException
372
     */
373
    @Test
374
    @DataSets({
375
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
376
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
377
        @DataSet(value="TransmissionEngineDistributionTest.xml"),
378
    })
379
    public void testArea_rank_and_area_3() throws JvmLimitsException {
380

    
381
        Set<Distribution> distributions_LCA = new HashSet<Distribution>();
382
        distributions_LCA.add(newDistribution(book_a, yug_ko, PresenceAbsenceTerm.NATIVE(), "1"));
383
        distributions_LCA.add(newDistribution(book_a, yug_ko, PresenceAbsenceTerm.NATIVE(), "3"));
384

    
385
        addDistributions(
386
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
387
                distributions_LCA
388
            );
389

    
390
        Set<Distribution> distributions_LC = new HashSet<>();
391
        distributions_LC.add(newDistribution(book_a, yug_ko, PresenceAbsenceTerm.NATIVE(), "1"));
392
        distributions_LC.add(newDistribution(book_b, yug_ko, PresenceAbsenceTerm.NATIVE(), "2"));
393

    
394
        addDistributions(
395
                T_LAPSANA_COMMUNIS_UUID,
396
                distributions_LC
397
            );
398

    
399
        engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
400

    
401
        Taxon lapsana_communis = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_UUID);
402
        int computedDescriptionsCnt = 0;
403
        for(TaxonDescription description : lapsana_communis.getDescriptions()) {
404
            if(description.hasMarker(MarkerType.COMPUTED(), true)) {
405
                computedDescriptionsCnt++;
406
                assertEquals(2, description.getElements().size());
407
                for(DescriptionElementBase distribution : description.getElements()) {
408
                    logger.debug(((Distribution)distribution).getArea() + " " + sourcesToString(distribution));
409
                    if(((Distribution)distribution).getArea().equals(yug_ko)){
410
                        assertEquals(2, distribution.getSources().size());
411
                    }
412
                    if(((Distribution)distribution).getArea().equals(yug)){
413
                        assertEquals(3, distribution.getSources().size());
414
                    }
415
                }
416
            }
417
        }
418
        assertEquals(1, computedDescriptionsCnt);
419
    }
420

    
421
    /**
422
     * Variant of {@link #testArea_rank_and_area_1()} with alternate source references to
423
     * check the handling of the case where the target taxon already has the distribution which is the
424
     * result of the aggregation (see http://dev.e-taxonomy.eu/trac/ticket/4366#comment:12)
425
     *
426
     * This test relies on {@link #testArea_rank_and_area_1()}
427
     * an makes assertions only on the alternative source references
428
     * @throws JvmLimitsException
429
     */
430
    @Test
431
    @Ignore
432
    @DataSets({
433
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
434
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
435
        @DataSet(value="TransmissionEngineDistributionTest.xml"),
436
    })
437
    public void testArea_rank_and_area_4() throws JvmLimitsException {
438

    
439
        Set<Distribution> distributions_LCA = new HashSet<>();
440
        distributions_LCA.add(newDistribution(book_a, yug_ko, PresenceAbsenceTerm.NATIVE(), "1"));
441

    
442
        addDistributions(
443
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
444
                distributions_LCA
445
            );
446

    
447
        Set<Distribution> distributions_LC = new HashSet<>();
448
        distributions_LC.add(newDistribution(book_a, yug, PresenceAbsenceTerm.NATIVE(), "2")); //  should succeed
449

    
450
        addDistributions(
451
                T_LAPSANA_COMMUNIS_UUID,
452
                distributions_LC
453
            );
454

    
455
        engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
456

    
457
        Taxon lapsana_communis = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_UUID);
458
        int computedDescriptionsCnt = 0;
459
        for(TaxonDescription description : lapsana_communis.getDescriptions()) {
460
            if(description.hasMarker(MarkerType.COMPUTED(), true)) {
461
                computedDescriptionsCnt++;
462
                assertEquals(2, description.getElements().size());
463
                Distribution distribution = (Distribution)description.getElements().iterator().next();
464
                if(distribution.getArea().equals(yug_ko)){
465
                    assertEquals(2, distribution.getSources().size());
466
                    DescriptionElementSource source = distribution.getSources().iterator().next();
467
                    assertEquals("2", source.getCitationMicroReference());
468
                }
469
            }
470
        }
471
        assertEquals(1, computedDescriptionsCnt);
472
    }
473

    
474
    /**
475
     * @param referenceTitle
476
     * @param area
477
     * @param status
478
     * @param microCitation
479
     * @return
480
     */
481
    private Distribution newDistribution(Reference reference, NamedArea area, PresenceAbsenceTerm status,
482
            String microCitation) {
483
        DescriptionElementSource source = DescriptionElementSource.NewPrimarySourceInstance(reference, microCitation);
484
        Distribution distribution = Distribution.NewInstance(area, status);
485
        distribution.getSources().add(source);
486
        return distribution;
487
    }
488

    
489
    /**
490
     * creates a new description for the taxon identified by the UUIDs
491
     * @param taxonUuid
492
     * @param distributions
493
     */
494
    private void addDistributions(UUID taxonUuid, Collection<Distribution> distributions) {
495
        Taxon taxon = (Taxon) taxonService.load(taxonUuid);
496
        if(taxon == null) {
497
            throw new NullPointerException("No taxon found for " + taxonUuid);
498
        }
499
        TaxonDescription description = TaxonDescription.NewInstance(taxon);
500

    
501
         for (Distribution distribution : distributions) {
502
             description.addElement(distribution);
503
        }
504
        taxonService.saveOrUpdate(taxon);
505
        // need to write to database for transmission engine
506
        commitAndStartNewTransaction(null);
507
    }
508

    
509
    private String sourcesToString(DescriptionElementBase deb) {
510
        StringBuffer out = new StringBuffer();
511
        for ( DescriptionElementSource source : deb.getSources()) {
512
            out.append(source.getCitation().getTitle() + " : " + source.getCitationMicroReference() + ", ");
513
        }
514
        return out.toString();
515
    }
516

    
517

    
518
    //@Test //  uncomment to create test data file//
519
    @Override
520
    public void createTestDataSet() throws FileNotFoundException {
521

    
522
        // --- References --- //
523
        Reference sec = ReferenceFactory.newDatabase();
524
        sec.setTitleCache("Test", true);
525
        Reference nomRef = ReferenceFactory.newBook();
526
        sec.setTitleCache("Sp.Pl.", true);
527

    
528
        referenceService.save(sec);
529
        referenceService.save(nomRef);
530

    
531

    
532
        // --- Taxa --- //
533
        //  Lapsana
534
        //        L. communis
535
        //            L. communis subsp. communis
536
        //            L. communis subsp. adenophora
537
        //            L. communis subsp. alpina
538
        //  Sonchella
539
        //        S. dentata
540
        //        S. stenoma
541
        IBotanicalName n_lapsana = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
542
        n_lapsana.setTitleCache("Lapsana", true);
543
        Taxon t_lapsana = Taxon.NewInstance(n_lapsana, sec);
544
        t_lapsana.setUuid(T_LAPSANA_UUID);
545
        taxonService.saveOrUpdate(t_lapsana);
546

    
547
        IBotanicalName n_lapsana_communis = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
548
        n_lapsana_communis.setTitleCache("L. communis", true);
549
        Taxon t_lapsana_communis = Taxon.NewInstance(n_lapsana_communis, sec);
550
        t_lapsana_communis.setUuid(T_LAPSANA_COMMUNIS_UUID);
551
        taxonService.saveOrUpdate(t_lapsana_communis);
552

    
553
        IBotanicalName n_lapsana_communis_communis = TaxonNameFactory.NewBotanicalInstance(Rank.SUBSPECIES());
554
        n_lapsana_communis_communis.setTitleCache("L. communis subsp. communis", true);
555
        Taxon t_lapsana_communis_communis = Taxon.NewInstance(n_lapsana_communis_communis, sec);
556
        t_lapsana_communis_communis.setUuid(T_LAPSANA_COMMUNIS_COMMUNIS_UUID);
557
        taxonService.saveOrUpdate(t_lapsana_communis_communis);
558

    
559
        IBotanicalName n_lapsana_communis_adenophora = TaxonNameFactory.NewBotanicalInstance(Rank.SUBSPECIES());
560
        n_lapsana_communis_adenophora.setTitleCache("L. communis subsp. adenophora", true);
561
        Taxon t_lapsana_communis_adenophora = Taxon.NewInstance(n_lapsana_communis_adenophora, sec);
562
        t_lapsana_communis_adenophora.setUuid(T_LAPSANA_COMMUNIS_ADENOPHORA_UUID);
563
        taxonService.saveOrUpdate(t_lapsana_communis_adenophora);
564

    
565
        IBotanicalName n_lapsana_communis_alpina = TaxonNameFactory.NewBotanicalInstance(Rank.SUBSPECIES());
566
        n_lapsana_communis_alpina.setTitleCache("L. communis subsp. alpina", true);
567
        Taxon t_lapsana_communis_alpina = Taxon.NewInstance(n_lapsana_communis_alpina, sec);
568
        t_lapsana_communis_alpina.setUuid(T_LAPSANA_COMMUNIS_ALPINA_UUID);
569
        taxonService.saveOrUpdate(t_lapsana_communis_alpina);
570

    
571
        // --- Classification --- //
572
        Classification classification = Classification.NewInstance("TestClassification");
573
        classification.setUuid(CLASSIFICATION_UUID);
574
        classificationService.save(classification);
575
        TaxonNode node_lapsana = classification.addChildTaxon(t_lapsana, sec, null);
576
        TaxonNode node_lapsana_communis = node_lapsana.addChildTaxon(t_lapsana_communis, sec, null);
577
        node_lapsana_communis.addChildTaxon(t_lapsana_communis_communis, sec, null);
578
        node_lapsana_communis.addChildTaxon(t_lapsana_communis_adenophora, sec, null);
579
        node_lapsana_communis.addChildTaxon(t_lapsana_communis_alpina, sec, null);
580
        classificationService.saveOrUpdate(classification);
581

    
582
        // --- Distributions --- //
583
        // tdwg3 level YUG :  Yugoslavia
584
        // contains tdwg4 level areas :
585
        //   YUG-BH	Bosnia-Herzegovina
586
        //   YUG-CR	Croatia
587
        //   YUG-KO	Kosovo
588
        //   YUG-MA	Macedonia
589
        //   YUG-MN	Montenegro
590

    
591
        // assigning distribution information to taxa
592
        // expectations regarding the aggregation can be found in the comments below
593
//        TaxonDescription d_lapsana_communis_communis = TaxonDescription.NewInstance(t_lapsana_communis_communis);
594
//        d_lapsana_communis_communis.addElement(Distribution.NewInstance(
595
//                    TdwgArea.getAreaByTdwgAbbreviation("YUG-MN"),
596
//                    PresenceTerm.ENDEMIC_FOR_THE_RELEVANT_AREA() // should be ignored
597
//                    );
598

    
599
        commitAndStartNewTransaction(null);
600

    
601
        writeDbUnitDataSetFile(new String[] {
602
                "TAXONBASE", "TAXONNAME",
603
                "REFERENCE", "DESCRIPTIONELEMENTBASE", "DESCRIPTIONBASE",
604
                "AGENTBASE", "CLASSIFICATION",  "TAXONNODE",
605
                "HOMOTYPICALGROUP", "LANGUAGESTRING",
606
                "HIBERNATE_SEQUENCES"
607
         });
608

    
609
    }
610

    
611
}
    (1-1/1)