Project

General

Profile

Revision b997cab9

IDb997cab90a323a6df317079aa4994192eead570b
Parent 753627ae
Child ac2cee48

Added by Andreas Müller 5 months ago

ref #8651 minor changes to TransmissionEngineDistribution and moving test

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/description/TransmissionEngineDistribution.java
33 33

  
34 34
import eu.etaxonomy.cdm.api.service.IClassificationService;
35 35
import eu.etaxonomy.cdm.api.service.IDescriptionService;
36
import eu.etaxonomy.cdm.api.service.INameService;
37 36
import eu.etaxonomy.cdm.api.service.ITaxonService;
38 37
import eu.etaxonomy.cdm.api.service.ITermService;
38
import eu.etaxonomy.cdm.api.service.UpdateResult;
39 39
import eu.etaxonomy.cdm.common.DynamicBatch;
40 40
import eu.etaxonomy.cdm.common.JvmLimitsException;
41 41
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
......
90 90
 * @since Feb 22, 2013
91 91
 */
92 92
@Service
93
public class TransmissionEngineDistribution { //TODO extends IoBase?
93
public class TransmissionEngineDistribution
94
            extends DescriptionAggregationBase<TransmissionEngineDistribution, DescriptionAggregationConfiguration>{ //TODO extends IoBase?
94 95

  
95 96
    public static final Logger logger = Logger.getLogger(TransmissionEngineDistribution.class);
96 97

  
......
143 144
    private IClassificationService classificationService;
144 145

  
145 146
    @Autowired
146
    private INameService mameService;
147

  
148
    @Autowired
149 147
    private HibernateTransactionManager transactionManager;
150 148

  
151 149
    private List<PresenceAbsenceTerm> byAreaIgnoreStatusList = null;
......
1015 1013
        commitTransaction(txStatus);
1016 1014
    }
1017 1015

  
1018
    public void addSourcesDeduplicated(Set<DescriptionElementSource> target, Set<DescriptionElementSource> sources) {
1016
    private void addSourcesDeduplicated(Set<DescriptionElementSource> target, Set<DescriptionElementSource> sources) {
1019 1017
        for(DescriptionElementSource source : sources) {
1020 1018
            boolean contained = false;
1021 1019
            for(DescriptionElementSource existingSource: target) {
......
1059 1057
            addSourcesDeduplicated(this.sources, sources);
1060 1058
        }
1061 1059
    }
1060

  
1061
    @Override
1062
    protected UpdateResult invokeOnSingleTaxon() {
1063
        // TODO Auto-generated method stub
1064
        return null;
1065
    }
1066

  
1067
    @Override
1068
    protected UpdateResult removeExistingAggregationOnTaxon() {
1069
        // TODO Auto-generated method stub
1070
        return null;
1071
    }
1072

  
1073
    @Override
1074
    protected UpdateResult invokeHigherRankAggregation() {
1075
        // TODO Auto-generated method stub
1076
        return null;
1077
    }
1062 1078
}
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/TransmissionEngineDistributionTest.java
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
 * @since 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="./ClearDBDataSet.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
}
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/description/TransmissionEngineDistributionTest.java
1
/**
2
* Copyright (C) 2013 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.api.service.description;
10

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

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

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

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

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

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

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

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

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

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

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

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

  
80

  
81
    @SpringBeanByType
82
    private ITermService termService;
83

  
84
    @SpringBeanByType
85
    private ITaxonService taxonService;
86

  
87
    @SpringBeanByType
88
    private IClassificationService classificationService;
89

  
90
    @SpringBeanByType
91
    private IReferenceService referenceService;
92

  
93
    @SpringBeanByType
94
    private TransmissionEngineDistribution engine;
95

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

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

  
115
    private Classification classification;
116

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

  
120

  
121
    @Before
122
    public void setUp() {
123

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

  
130
        classification = classificationService.load(CLASSIFICATION_UUID);
131

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  
266
        commitAndStartNewTransaction(null);
267

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

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

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

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

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

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

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

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

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

  
364

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  
517

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

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

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

  
531

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

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

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

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

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

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

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

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

  
599
        commitAndStartNewTransaction(null);
600

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

  
609
    }
610

  
611
}
cdmlib-services/src/test/java/eu/etaxonomy/cdm/test/suite/TestsShouldNotFailInSuite_s5.java
17 17
@RunWith(Suite.class)
18 18
@Suite.SuiteClasses(
19 19
		{
20
		    eu.etaxonomy.cdm.api.service.TransmissionEngineDistributionTest.class,
20
		    eu.etaxonomy.cdm.api.service.description.TransmissionEngineDistributionTest.class,
21 21
		    eu.etaxonomy.cdm.api.service.UserAndGroupServiceImplTest.class,
22 22
		    eu.etaxonomy.cdm.api.utility.DerivedUnitConverterIntegrationTest.class
23 23
		    /*

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)