Project

General

Profile

Download (28.3 KB) Statistics
| Branch: | Tag: | Revision:
1 0a71e914 Andreas Kohlbecker
/**
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 28cf9db3 Andreas Kohlbecker
import static org.junit.Assert.assertEquals;
12 f6299e47 Andreas Kohlbecker
import static org.junit.Assert.assertNotNull;
13
import static org.junit.Assert.assertNull;
14 f5295f3f Andreas Kohlbecker
import static org.junit.Assert.assertTrue;
15 28cf9db3 Andreas Kohlbecker
16 0a71e914 Andreas Kohlbecker
import java.io.FileNotFoundException;
17
import java.util.Arrays;
18 f5295f3f Andreas Kohlbecker
import java.util.Collection;
19
import java.util.HashSet;
20
import java.util.Iterator;
21 0a71e914 Andreas Kohlbecker
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 87926c7b Andreas Kohlbecker
import org.junit.Ignore;
28 0a71e914 Andreas Kohlbecker
import org.junit.Test;
29
import org.unitils.dbunit.annotation.DataSet;
30 2638be38 Andreas Müller
import org.unitils.dbunit.annotation.DataSets;
31 0a71e914 Andreas Kohlbecker
import org.unitils.spring.annotation.SpringBeanByType;
32
33 38ca1a7b Andreas Kohlbecker
import eu.etaxonomy.cdm.api.service.description.TransmissionEngineDistribution;
34 b1385f0b Andreas Kohlbecker
import eu.etaxonomy.cdm.api.service.description.TransmissionEngineDistribution.AggregationMode;
35 9b7db8db Andreas Kohlbecker
import eu.etaxonomy.cdm.common.JvmLimitsException;
36 0a71e914 Andreas Kohlbecker
import eu.etaxonomy.cdm.model.common.Extension;
37 f6299e47 Andreas Kohlbecker
import eu.etaxonomy.cdm.model.common.MarkerType;
38 28cf9db3 Andreas Kohlbecker
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
39 f6299e47 Andreas Kohlbecker
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
40 0a71e914 Andreas Kohlbecker
import eu.etaxonomy.cdm.model.description.Distribution;
41 ccf1f2f1 Andreas Müller
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
42 0a71e914 Andreas Kohlbecker
import eu.etaxonomy.cdm.model.description.TaxonDescription;
43
import eu.etaxonomy.cdm.model.location.NamedArea;
44 e6d7b501 Andreas Müller
import eu.etaxonomy.cdm.model.name.IBotanicalName;
45 0a71e914 Andreas Kohlbecker
import eu.etaxonomy.cdm.model.name.Rank;
46 fe34a9eb Andreas Müller
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
47 0a71e914 Andreas Kohlbecker
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 28cf9db3 Andreas Kohlbecker
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
54 0a71e914 Andreas Kohlbecker
55
/**
56
 * @author a.kohlbecker
57 53db84af Andreas Müller
 * @since Feb 26, 2013
58 0a71e914 Andreas Kohlbecker
 *
59
 */
60 38ca1a7b Andreas Kohlbecker
public class TransmissionEngineDistributionTest extends CdmTransactionalIntegrationTest {
61 0a71e914 Andreas Kohlbecker
62 cfe49ed0 Andreas Müller
    private static Logger logger = Logger.getLogger(TransmissionEngineDistributionTest.class);
63 0a71e914 Andreas Kohlbecker
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 28cf9db3 Andreas Kohlbecker
    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 0a71e914 Andreas Kohlbecker
    @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 38ca1a7b Andreas Kohlbecker
    private TransmissionEngineDistribution engine;
91 0a71e914 Andreas Kohlbecker
92 28cf9db3 Andreas Kohlbecker
    // --- 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 f5295f3f Andreas Kohlbecker
    private Reference book_a = null;
114
    private Reference book_b = null;
115
116 0a71e914 Andreas Kohlbecker
117
    @Before
118
    public void setUp() {
119 28cf9db3 Andreas Kohlbecker
120
        superAreas = Arrays.asList(new NamedArea[]{
121 df9d27cd Andreas Müller
        		termService.getAreaByTdwgAbbreviation("YUG")
122 0a71e914 Andreas Kohlbecker
        });
123 848f7204 Andreas Kohlbecker
        lowerRank = Rank.SUBSPECIES();
124 28cf9db3 Andreas Kohlbecker
        upperRank = Rank.GENUS();
125
126
        classification = classificationService.load(CLASSIFICATION_UUID);
127
128 df9d27cd Andreas Müller
        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 28cf9db3 Andreas Kohlbecker
135 f5295f3f Andreas Kohlbecker
        book_a = ReferenceFactory.newBook();
136
        book_a.setTitle("book_a");
137
        book_b = ReferenceFactory.newBook();
138
        book_b.setTitle("book_a");
139
140 d3752fac Andreas Kohlbecker
        engine.setBatchMinFreeHeap(100 * 1024 * 1024);
141 0a71e914 Andreas Kohlbecker
        engine.updatePriorities();
142
    }
143
144 28cf9db3 Andreas Kohlbecker
    @Test
145 0a71e914 Andreas Kohlbecker
    @DataSet
146 2638be38 Andreas Müller
    public void testPriorities(){
147 28cf9db3 Andreas Kohlbecker
148 2638be38 Andreas Müller
        Set<Extension> extensions = termService.load(PresenceAbsenceTerm.CULTIVATED().getUuid()).getExtensions();
149
        assertEquals(TransmissionEngineDistribution.EXTENSION_VALUE_PREFIX + "45", extensions.iterator().next().getValue());
150 28cf9db3 Andreas Kohlbecker
    }
151
152
    @Test
153 2638be38 Andreas Müller
    @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 9b7db8db Andreas Kohlbecker
    public void test_ignore() throws JvmLimitsException {
160 28cf9db3 Andreas Kohlbecker
161
        addDistributions(
162
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
163 f5295f3f Andreas Kohlbecker
                Arrays.asList(new Distribution[] {
164 28cf9db3 Andreas Kohlbecker
                        // 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 ccf1f2f1 Andreas Müller
                        Distribution.NewInstance(yug_mn, PresenceAbsenceTerm.ENDEMIC_FOR_THE_RELEVANT_AREA()),
168 28cf9db3 Andreas Kohlbecker
                        // should be ignored by area aggregation
169
                        // => LAPSANA_COMMUNIS will wave distribution with yug_ko and INTRODUCED_FORMERLY_INTRODUCED
170 ccf1f2f1 Andreas Müller
                        Distribution.NewInstance(yug_ko, PresenceAbsenceTerm.INTRODUCED_FORMERLY_INTRODUCED()),
171 f5295f3f Andreas Kohlbecker
               })
172 28cf9db3 Andreas Kohlbecker
            );
173
174 b1385f0b Andreas Kohlbecker
        engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
175 28cf9db3 Andreas Kohlbecker
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 ccf1f2f1 Andreas Müller
                assertEquals("aggregated status of area YUG-KO wrong", PresenceAbsenceTerm.INTRODUCED_FORMERLY_INTRODUCED().getLabel(), distribution.getStatus().getLabel());
190 28cf9db3 Andreas Kohlbecker
            }
191
        }
192
        assertEquals("All three expected areas should have been found before", numExpectedFound, 1);
193
    }
194
195
    @Test
196 2638be38 Andreas Müller
    @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 9b7db8db Andreas Kohlbecker
    public void testArea_area() throws JvmLimitsException {
202 28cf9db3 Andreas Kohlbecker
203 f5295f3f Andreas Kohlbecker
        Set<Distribution> distributions_LCA = new HashSet<>();
204 f6299e47 Andreas Kohlbecker
205 f5295f3f Andreas Kohlbecker
        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 f6299e47 Andreas Kohlbecker
210 28cf9db3 Andreas Kohlbecker
        addDistributions(
211
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
212 f5295f3f Andreas Kohlbecker
                distributions_LCA
213 28cf9db3 Andreas Kohlbecker
            );
214
215
        Taxon lapsana_communis_alpina  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_ALPINA_UUID);
216
        assertEquals(1, lapsana_communis_alpina.getDescriptions().size());
217
218 2a7668fb Andreas Kohlbecker
        engine.accumulate(AggregationMode.byAreas, superAreas, lowerRank, upperRank, classification, null);
219 28cf9db3 Andreas Kohlbecker
220
        lapsana_communis_alpina  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_ALPINA_UUID);
221
        assertEquals(2, lapsana_communis_alpina.getDescriptions().size());
222
223 f6299e47 Andreas Kohlbecker
        Distribution accumulatedDistribution = null;
224 28cf9db3 Andreas Kohlbecker
        for (TaxonDescription description : lapsana_communis_alpina.getDescriptions()) {
225 f6299e47 Andreas Kohlbecker
            if(description.hasMarker(MarkerType.COMPUTED(), true)) {
226
                assertNull("only one computed Distribution should exists", accumulatedDistribution);
227 f5295f3f Andreas Kohlbecker
                assertEquals("the computed Decsription should have only one element", 1, description.getElements().size());
228 f6299e47 Andreas Kohlbecker
                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 28cf9db3 Andreas Kohlbecker
            }
232
        }
233 f6299e47 Andreas Kohlbecker
        assertNotNull("The area YUG should have been found", accumulatedDistribution);
234 f5295f3f Andreas Kohlbecker
        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 0a71e914 Andreas Kohlbecker
    }
240
241
    @Test
242 87926c7b Andreas Kohlbecker
    @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 9b7db8db Andreas Kohlbecker
    public void testArea_rank_and_area_1() throws JvmLimitsException {
248 f5295f3f Andreas Kohlbecker
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 28cf9db3 Andreas Kohlbecker
253
        addDistributions(
254
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
255 f5295f3f Andreas Kohlbecker
                distributions_LCA
256 28cf9db3 Andreas Kohlbecker
            );
257 f5295f3f Andreas Kohlbecker
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 28cf9db3 Andreas Kohlbecker
        addDistributions(
265
                T_LAPSANA_COMMUNIS_UUID,
266 f5295f3f Andreas Kohlbecker
                distributions_LC
267 28cf9db3 Andreas Kohlbecker
            );
268
269 b1385f0b Andreas Kohlbecker
        engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
270 28cf9db3 Andreas Kohlbecker
271
        Taxon lapsana_communis  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_UUID);
272 f5295f3f Andreas Kohlbecker
        assertEquals("Lapsana communis alpina must only have 2 Descriptions", 2, lapsana_communis.getDescriptions().size());
273 28cf9db3 Andreas Kohlbecker
274
        Taxon lapsana = (Taxon) taxonService.load(T_LAPSANA_UUID);
275 f5295f3f Andreas Kohlbecker
        assertEquals("Lapsana communis must only have 1 Description", 1, lapsana.getDescriptions().size());
276 28cf9db3 Andreas Kohlbecker
        TaxonDescription description = lapsana.getDescriptions().iterator().next();
277 f5295f3f Andreas Kohlbecker
        assertTrue(description.hasMarker(MarkerType.COMPUTED(), true));
278 28cf9db3 Andreas Kohlbecker
        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 ccf1f2f1 Andreas Müller
                assertEquals("aggregated status of area YUG is wrong", PresenceAbsenceTerm.NATIVE().getLabel(), distribution.getStatus().getLabel());
285 f5295f3f Andreas Kohlbecker
                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 28cf9db3 Andreas Kohlbecker
            }
291
            if(distribution.getArea().equals(yug_mn)){
292
                numExpectedFound++;
293 ccf1f2f1 Andreas Müller
                assertEquals("aggregated status of area YUG-MN is wrong", PresenceAbsenceTerm.CULTIVATED().getLabel(), distribution.getStatus().getLabel());
294 f5295f3f Andreas Kohlbecker
                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 28cf9db3 Andreas Kohlbecker
            }
300
            if(distribution.getArea().equals(yug_ko)){
301
                numExpectedFound++;
302 ccf1f2f1 Andreas Müller
                assertEquals("aggregated status of area YUG-KO wrong", PresenceAbsenceTerm.NATIVE().getLabel(), distribution.getStatus().getLabel());
303 f5295f3f Andreas Kohlbecker
                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 28cf9db3 Andreas Kohlbecker
            }
309
        }
310
        assertEquals("All three expected areas should have been found before", numExpectedFound, 3);
311
    }
312
313 f5295f3f Andreas Kohlbecker
    /**
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 9b7db8db Andreas Kohlbecker
     * @throws JvmLimitsException
320 f5295f3f Andreas Kohlbecker
     */
321
    @Test
322 87926c7b Andreas Kohlbecker
    @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 9b7db8db Andreas Kohlbecker
    public void testArea_rank_and_area_2() throws JvmLimitsException {
328 f5295f3f Andreas Kohlbecker
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 9b7db8db Andreas Kohlbecker
     * @throws JvmLimitsException
368 f5295f3f Andreas Kohlbecker
     */
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 9b7db8db Andreas Kohlbecker
    public void testArea_rank_and_area_3() throws JvmLimitsException {
376 f5295f3f Andreas Kohlbecker
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 9b7db8db Andreas Kohlbecker
     * @throws JvmLimitsException
425 f5295f3f Andreas Kohlbecker
     */
426
    @Test
427 87926c7b Andreas Kohlbecker
    @Ignore
428 f5295f3f Andreas Kohlbecker
    @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 9b7db8db Andreas Kohlbecker
    public void testArea_rank_and_area_4() throws JvmLimitsException {
434 f5295f3f Andreas Kohlbecker
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 28cf9db3 Andreas Kohlbecker
    /**
486
     * creates a new description for the taxon identified by the UUIDs
487
     * @param taxonUuid
488
     * @param distributions
489
     */
490 f5295f3f Andreas Kohlbecker
    private void addDistributions(UUID taxonUuid, Collection<Distribution> distributions) {
491 28cf9db3 Andreas Kohlbecker
        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 c1a377e9 Andreas Kohlbecker
        // need to write to database for transmission engine
502
        commitAndStartNewTransaction(null);
503 28cf9db3 Andreas Kohlbecker
    }
504
505 f5295f3f Andreas Kohlbecker
    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 28cf9db3 Andreas Kohlbecker
514 848f7204 Andreas Kohlbecker
    //@Test //  uncomment to create test data file//
515 d06ba760 Patric Plitzner
    @Override
516
    public void createTestDataSet() throws FileNotFoundException {
517 0a71e914 Andreas Kohlbecker
518
        // --- References --- //
519 531fc7bc Andreas Müller
        Reference sec = ReferenceFactory.newDatabase();
520 0a71e914 Andreas Kohlbecker
        sec.setTitleCache("Test", true);
521 531fc7bc Andreas Müller
        Reference nomRef = ReferenceFactory.newBook();
522 0a71e914 Andreas Kohlbecker
        sec.setTitleCache("Sp.Pl.", true);
523
524
        referenceService.save(sec);
525
        referenceService.save(nomRef);
526
527 28cf9db3 Andreas Kohlbecker
528 0a71e914 Andreas Kohlbecker
        // --- 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 e6d7b501 Andreas Müller
        IBotanicalName n_lapsana = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
538 0a71e914 Andreas Kohlbecker
        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 e6d7b501 Andreas Müller
        IBotanicalName n_lapsana_communis = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
544 28cf9db3 Andreas Kohlbecker
        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 0a71e914 Andreas Kohlbecker
        taxonService.saveOrUpdate(t_lapsana_communis);
548
549 e6d7b501 Andreas Müller
        IBotanicalName n_lapsana_communis_communis = TaxonNameFactory.NewBotanicalInstance(Rank.SUBSPECIES());
550 28cf9db3 Andreas Kohlbecker
        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 0a71e914 Andreas Kohlbecker
        taxonService.saveOrUpdate(t_lapsana_communis_communis);
554
555 e6d7b501 Andreas Müller
        IBotanicalName n_lapsana_communis_adenophora = TaxonNameFactory.NewBotanicalInstance(Rank.SUBSPECIES());
556 28cf9db3 Andreas Kohlbecker
        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 0a71e914 Andreas Kohlbecker
        taxonService.saveOrUpdate(t_lapsana_communis_adenophora);
560
561 e6d7b501 Andreas Müller
        IBotanicalName n_lapsana_communis_alpina = TaxonNameFactory.NewBotanicalInstance(Rank.SUBSPECIES());
562 28cf9db3 Andreas Kohlbecker
        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 0a71e914 Andreas Kohlbecker
        taxonService.saveOrUpdate(t_lapsana_communis_alpina);
566
567
        // --- Classification --- //
568
        Classification classification = Classification.NewInstance("TestClassification");
569 28cf9db3 Andreas Kohlbecker
        classification.setUuid(CLASSIFICATION_UUID);
570 0a71e914 Andreas Kohlbecker
        classificationService.save(classification);
571 efdb6847 Andreas Müller
        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 28cf9db3 Andreas Kohlbecker
        classificationService.saveOrUpdate(classification);
577 0a71e914 Andreas Kohlbecker
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 28cf9db3 Andreas Kohlbecker
        commitAndStartNewTransaction(null);
596 0a71e914 Andreas Kohlbecker
597 28cf9db3 Andreas Kohlbecker
        writeDbUnitDataSetFile(new String[] {
598 9dc896c9 Andreas Müller
                "TAXONBASE", "TAXONNAME",
599 28cf9db3 Andreas Kohlbecker
                "REFERENCE", "DESCRIPTIONELEMENTBASE", "DESCRIPTIONBASE",
600 c93bf342 Katja Luther
                "AGENTBASE", "CLASSIFICATION",  "TAXONNODE",
601 28cf9db3 Andreas Kohlbecker
                "HOMOTYPICALGROUP", "LANGUAGESTRING",
602 f5295f3f Andreas Kohlbecker
                "HIBERNATE_SEQUENCES"
603 28cf9db3 Andreas Kohlbecker
         });
604 0a71e914 Andreas Kohlbecker
605
    }
606
607
}