Project

General

Profile

Download (28.2 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2013 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
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.Arrays;
19
import java.util.Collection;
20
import java.util.HashSet;
21
import java.util.Iterator;
22
import java.util.List;
23
import java.util.Set;
24
import java.util.UUID;
25

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

    
34
import eu.etaxonomy.cdm.api.service.description.TransmissionEngineDistribution;
35
import eu.etaxonomy.cdm.api.service.description.TransmissionEngineDistribution.AggregationMode;
36
import eu.etaxonomy.cdm.common.JvmLimitsException;
37
import eu.etaxonomy.cdm.model.common.Extension;
38
import eu.etaxonomy.cdm.model.common.MarkerType;
39
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
40
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
41
import eu.etaxonomy.cdm.model.description.Distribution;
42
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
43
import eu.etaxonomy.cdm.model.description.TaxonDescription;
44
import eu.etaxonomy.cdm.model.location.NamedArea;
45
import eu.etaxonomy.cdm.model.name.BotanicalName;
46
import eu.etaxonomy.cdm.model.name.Rank;
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.updatePriorities();
141
    }
142

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
261
        commitAndStartNewTransaction(null);
262

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

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

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

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

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

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

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

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

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

    
359

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
512

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

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

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

    
526

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

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

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

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

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

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

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

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

    
594
        commitAndStartNewTransaction(null);
595

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

    
604
    }
605

    
606
}
(29-29/31)