Project

General

Profile

Download (28.3 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;
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.description.TransmissionEngineDistribution;
34
import eu.etaxonomy.cdm.api.service.description.TransmissionEngineDistribution.AggregationMode;
35
import eu.etaxonomy.cdm.common.JvmLimitsException;
36
import eu.etaxonomy.cdm.model.common.Extension;
37
import eu.etaxonomy.cdm.model.common.MarkerType;
38
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
39
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
40
import eu.etaxonomy.cdm.model.description.Distribution;
41
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
42
import eu.etaxonomy.cdm.model.description.TaxonDescription;
43
import eu.etaxonomy.cdm.model.location.NamedArea;
44
import eu.etaxonomy.cdm.model.name.IBotanicalName;
45
import eu.etaxonomy.cdm.model.name.Rank;
46
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
47
import eu.etaxonomy.cdm.model.reference.Reference;
48
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
49
import eu.etaxonomy.cdm.model.taxon.Classification;
50
import eu.etaxonomy.cdm.model.taxon.Taxon;
51
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
52
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
53
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
54

    
55
/**
56
 * @author a.kohlbecker
57
 * @date Feb 26, 2013
58
 *
59
 */
60
public class TransmissionEngineDistributionTest extends CdmTransactionalIntegrationTest {
61

    
62
    private static Logger logger = Logger.getLogger(TransmissionEngineDistributionTest.class);
63

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

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

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

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

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

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

    
76

    
77
    @SpringBeanByType
78
    private ITermService termService;
79

    
80
    @SpringBeanByType
81
    private ITaxonService taxonService;
82

    
83
    @SpringBeanByType
84
    private IClassificationService classificationService;
85

    
86
    @SpringBeanByType
87
    private IReferenceService referenceService;
88

    
89
    @SpringBeanByType
90
    private TransmissionEngineDistribution engine;
91

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

    
107
    List<NamedArea> superAreas = null;
108
    Rank upperRank = null;
109
    Rank lowerRank = null;
110

    
111
    private Classification classification;
112

    
113
    private Reference book_a = null;
114
    private Reference book_b = null;
115

    
116

    
117
    @Before
118
    public void setUp() {
119

    
120
        superAreas = Arrays.asList(new NamedArea[]{
121
        		termService.getAreaByTdwgAbbreviation("YUG")
122
        });
123
        lowerRank = Rank.SUBSPECIES();
124
        upperRank = Rank.GENUS();
125

    
126
        classification = classificationService.load(CLASSIFICATION_UUID);
127

    
128
        yug = termService.getAreaByTdwgAbbreviation("YUG");
129
        yug_bh = termService.getAreaByTdwgAbbreviation("YUG-BH");
130
        yug_cr = termService.getAreaByTdwgAbbreviation("YUG-CR");
131
        yug_ko = termService.getAreaByTdwgAbbreviation("YUG-KO");
132
        yug_ma = termService.getAreaByTdwgAbbreviation("YUG-MA");
133
        yug_mn = termService.getAreaByTdwgAbbreviation("YUG-MN");
134

    
135
        book_a = ReferenceFactory.newBook();
136
        book_a.setTitle("book_a");
137
        book_b = ReferenceFactory.newBook();
138
        book_b.setTitle("book_a");
139

    
140
        engine.setBatchMinFreeHeap(100 * 1024 * 1024);
141
        engine.updatePriorities();
142
    }
143

    
144
    @Test
145
    @DataSet
146
    public void testPriorities(){
147

    
148
        Set<Extension> extensions = termService.load(PresenceAbsenceTerm.CULTIVATED().getUuid()).getExtensions();
149
        assertEquals(TransmissionEngineDistribution.EXTENSION_VALUE_PREFIX + "45", extensions.iterator().next().getValue());
150
    }
151

    
152
    @Test
153
    @DataSets({
154
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
155
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
156
        @DataSet(value="TransmissionEngineDistributionTest.xml"),
157
    })
158
//  @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class) //, value="./BlankDataSet.xml")
159
    public void test_ignore() throws JvmLimitsException {
160

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

    
174
        engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
175

    
176
        Taxon lapsana_communis_alpina  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_ALPINA_UUID);
177
        assertEquals(2, lapsana_communis_alpina.getDescriptions().size());
178
        // TODO test for yug => ENDEMIC_FOR_THE_RELEVANT_AREA in computed description
179

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

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

    
203
        Set<Distribution> distributions_LCA = new HashSet<>();
204

    
205
        distributions_LCA.add(newDistribution(book_a, yug_mn, PresenceAbsenceTerm.CULTIVATED(), "1"));
206
        distributions_LCA.add(newDistribution(book_a, yug_ko, PresenceAbsenceTerm.NATIVE(), "2")); // NATIVE should succeed
207
        distributions_LCA.add(newDistribution(book_a, yug_bh, PresenceAbsenceTerm.INTRODUCED(), "3"));
208
        distributions_LCA.add(newDistribution(book_a, yug_ma, PresenceAbsenceTerm.NATIVE(), "4")); // NATIVE should succeed
209

    
210
        addDistributions(
211
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
212
                distributions_LCA
213
            );
214

    
215
        Taxon lapsana_communis_alpina  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_ALPINA_UUID);
216
        assertEquals(1, lapsana_communis_alpina.getDescriptions().size());
217

    
218
        engine.accumulate(AggregationMode.byAreas, superAreas, lowerRank, upperRank, classification, null);
219

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

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

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

    
249
        Set<Distribution> distributions_LCA = new HashSet<>();
250
        distributions_LCA.add(newDistribution(book_a, yug_mn, PresenceAbsenceTerm.CULTIVATED(), "1"));
251
        distributions_LCA.add(newDistribution(book_a, yug_ko, PresenceAbsenceTerm.NATIVE(), "2")); // NATIVE should succeed
252

    
253
        addDistributions(
254
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
255
                distributions_LCA
256
            );
257

    
258
        Set<Distribution> distributions_LC = new HashSet<>();
259
        distributions_LC.add(newDistribution(book_a, yug_mn, PresenceAbsenceTerm.CULTIVATED(), "3"));
260
        distributions_LC.add(newDistribution(book_a, yug_ko, PresenceAbsenceTerm.NATIVE(), "4")); // NATIVE should succeed
261

    
262
        commitAndStartNewTransaction(null);
263

    
264
        addDistributions(
265
                T_LAPSANA_COMMUNIS_UUID,
266
                distributions_LC
267
            );
268

    
269
        engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
270

    
271
        Taxon lapsana_communis  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_UUID);
272
        assertEquals("Lapsana communis alpina must only have 2 Descriptions", 2, lapsana_communis.getDescriptions().size());
273

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

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

    
329
        Set<Distribution> distributions_LCA = new HashSet<Distribution>();
330
        distributions_LCA.add(newDistribution(book_a, yug_ko, PresenceAbsenceTerm.NATIVE(), "1"));
331
        distributions_LCA.add(newDistribution(book_b, yug_ko, PresenceAbsenceTerm.NATIVE(), "2"));
332

    
333
        addDistributions(
334
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
335
                distributions_LCA
336
            );
337

    
338
        engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
339

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

    
360

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

    
377
        Set<Distribution> distributions_LCA = new HashSet<Distribution>();
378
        distributions_LCA.add(newDistribution(book_a, yug_ko, PresenceAbsenceTerm.NATIVE(), "1"));
379
        distributions_LCA.add(newDistribution(book_a, yug_ko, PresenceAbsenceTerm.NATIVE(), "3"));
380

    
381
        addDistributions(
382
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
383
                distributions_LCA
384
            );
385

    
386
        Set<Distribution> distributions_LC = new HashSet<>();
387
        distributions_LC.add(newDistribution(book_a, yug_ko, PresenceAbsenceTerm.NATIVE(), "1"));
388
        distributions_LC.add(newDistribution(book_b, yug_ko, PresenceAbsenceTerm.NATIVE(), "2"));
389

    
390
        addDistributions(
391
                T_LAPSANA_COMMUNIS_UUID,
392
                distributions_LC
393
            );
394

    
395
        engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
396

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

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

    
435
        Set<Distribution> distributions_LCA = new HashSet<>();
436
        distributions_LCA.add(newDistribution(book_a, yug_ko, PresenceAbsenceTerm.NATIVE(), "1"));
437

    
438
        addDistributions(
439
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
440
                distributions_LCA
441
            );
442

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

    
446
        addDistributions(
447
                T_LAPSANA_COMMUNIS_UUID,
448
                distributions_LC
449
            );
450

    
451
        engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
452

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

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

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

    
497
         for (Distribution distribution : distributions) {
498
             description.addElement(distribution);
499
        }
500
        taxonService.saveOrUpdate(taxon);
501
        // need to write to database for transmission engine
502
        commitAndStartNewTransaction(null);
503
    }
504

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

    
513

    
514
    //@Test //  uncomment to create test data file//
515
    @Override
516
    public void createTestDataSet() throws FileNotFoundException {
517

    
518
        // --- References --- //
519
        Reference sec = ReferenceFactory.newDatabase();
520
        sec.setTitleCache("Test", true);
521
        Reference nomRef = ReferenceFactory.newBook();
522
        sec.setTitleCache("Sp.Pl.", true);
523

    
524
        referenceService.save(sec);
525
        referenceService.save(nomRef);
526

    
527

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

    
543
        IBotanicalName n_lapsana_communis = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
544
        n_lapsana_communis.setTitleCache("L. communis", true);
545
        Taxon t_lapsana_communis = Taxon.NewInstance(n_lapsana_communis, sec);
546
        t_lapsana_communis.setUuid(T_LAPSANA_COMMUNIS_UUID);
547
        taxonService.saveOrUpdate(t_lapsana_communis);
548

    
549
        IBotanicalName n_lapsana_communis_communis = TaxonNameFactory.NewBotanicalInstance(Rank.SUBSPECIES());
550
        n_lapsana_communis_communis.setTitleCache("L. communis subsp. communis", true);
551
        Taxon t_lapsana_communis_communis = Taxon.NewInstance(n_lapsana_communis_communis, sec);
552
        t_lapsana_communis_communis.setUuid(T_LAPSANA_COMMUNIS_COMMUNIS_UUID);
553
        taxonService.saveOrUpdate(t_lapsana_communis_communis);
554

    
555
        IBotanicalName n_lapsana_communis_adenophora = TaxonNameFactory.NewBotanicalInstance(Rank.SUBSPECIES());
556
        n_lapsana_communis_adenophora.setTitleCache("L. communis subsp. adenophora", true);
557
        Taxon t_lapsana_communis_adenophora = Taxon.NewInstance(n_lapsana_communis_adenophora, sec);
558
        t_lapsana_communis_adenophora.setUuid(T_LAPSANA_COMMUNIS_ADENOPHORA_UUID);
559
        taxonService.saveOrUpdate(t_lapsana_communis_adenophora);
560

    
561
        IBotanicalName n_lapsana_communis_alpina = TaxonNameFactory.NewBotanicalInstance(Rank.SUBSPECIES());
562
        n_lapsana_communis_alpina.setTitleCache("L. communis subsp. alpina", true);
563
        Taxon t_lapsana_communis_alpina = Taxon.NewInstance(n_lapsana_communis_alpina, sec);
564
        t_lapsana_communis_alpina.setUuid(T_LAPSANA_COMMUNIS_ALPINA_UUID);
565
        taxonService.saveOrUpdate(t_lapsana_communis_alpina);
566

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

    
578
        // --- Distributions --- //
579
        // tdwg3 level YUG :  Yugoslavia
580
        // contains tdwg4 level areas :
581
        //   YUG-BH	Bosnia-Herzegovina
582
        //   YUG-CR	Croatia
583
        //   YUG-KO	Kosovo
584
        //   YUG-MA	Macedonia
585
        //   YUG-MN	Montenegro
586

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

    
595
        commitAndStartNewTransaction(null);
596

    
597
        writeDbUnitDataSetFile(new String[] {
598
                "TAXONBASE", "TAXONNAME",
599
                "REFERENCE", "DESCRIPTIONELEMENTBASE", "DESCRIPTIONBASE",
600
                "AGENTBASE", "CLASSIFICATION",  "TAXONNODE",
601
                "HOMOTYPICALGROUP", "LANGUAGESTRING",
602
                "HIBERNATE_SEQUENCES"
603
         });
604

    
605
    }
606

    
607
}
(31-31/34)