Project

General

Profile

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

    
12
import static org.junit.Assert.assertEquals;
13
import static org.junit.Assert.assertTrue;
14

    
15
import java.io.FileNotFoundException;
16
import java.util.Arrays;
17
import java.util.List;
18
import java.util.Set;
19
import java.util.UUID;
20

    
21
import org.apache.log4j.Logger;
22
import org.junit.Before;
23
import org.junit.Test;
24
import org.unitils.dbunit.annotation.DataSet;
25
import org.unitils.spring.annotation.SpringBeanByType;
26

    
27
import eu.etaxonomy.cdm.api.service.description.TransmissionEngineDistribution;
28
import eu.etaxonomy.cdm.api.service.description.TransmissionEngineDistribution.AggregationMode;
29
import eu.etaxonomy.cdm.model.common.Extension;
30
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
31
import eu.etaxonomy.cdm.model.description.Distribution;
32
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
33
import eu.etaxonomy.cdm.model.description.TaxonDescription;
34
import eu.etaxonomy.cdm.model.location.NamedArea;
35
import eu.etaxonomy.cdm.model.name.BotanicalName;
36
import eu.etaxonomy.cdm.model.name.Rank;
37
import eu.etaxonomy.cdm.model.reference.Reference;
38
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
39
import eu.etaxonomy.cdm.model.taxon.Classification;
40
import eu.etaxonomy.cdm.model.taxon.Taxon;
41
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
42
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
43
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
44

    
45
/**
46
 * @author a.kohlbecker
47
 * @date Feb 26, 2013
48
 *
49
 */
50
public class TransmissionEngineDistributionTest extends CdmTransactionalIntegrationTest {
51

    
52
    @SuppressWarnings("unused")
53
    private static Logger logger = Logger.getLogger(TransmissionEngineDistributionTest.class);
54

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

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

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

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

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

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

    
67

    
68
    @SpringBeanByType
69
    private ITermService termService;
70

    
71
    @SpringBeanByType
72
    private ITaxonService taxonService;
73

    
74
    @SpringBeanByType
75
    private IClassificationService classificationService;
76

    
77
    @SpringBeanByType
78
    private IReferenceService referenceService;
79

    
80
    @SpringBeanByType
81
    private TransmissionEngineDistribution engine;
82

    
83
    // --- Distributions --- //
84
    // tdwg3 level YUG :  Yugoslavia
85
    // contains tdwg4 level areas :
86
    //   YUG-BH	Bosnia-Herzegovina
87
    //   YUG-CR	Croatia
88
    //   YUG-KO	Kosovo
89
    //   YUG-MA	Macedonia
90
    //   YUG-MN	Montenegro
91
    private NamedArea yug = null;
92
    private NamedArea yug_bh = null;
93
    private NamedArea yug_cr = null;
94
    private NamedArea yug_ko = null;
95
    private NamedArea yug_ma = null;
96
    private NamedArea yug_mn = null;
97

    
98
    List<NamedArea> superAreas = null;
99
    Rank upperRank = null;
100
    Rank lowerRank = null;
101

    
102
    private Classification classification;
103

    
104

    
105
    @Before
106
    public void setUp() {
107

    
108
        superAreas = Arrays.asList(new NamedArea[]{
109
        		termService.getAreaByTdwgAbbreviation("YUG")
110
        });
111
        lowerRank = Rank.SPECIES();
112
        upperRank = Rank.GENUS();
113

    
114
        classification = classificationService.load(CLASSIFICATION_UUID);
115

    
116
        yug = termService.getAreaByTdwgAbbreviation("YUG");
117
        yug_bh = termService.getAreaByTdwgAbbreviation("YUG-BH");
118
        yug_cr = termService.getAreaByTdwgAbbreviation("YUG-CR");
119
        yug_ko = termService.getAreaByTdwgAbbreviation("YUG-KO");
120
        yug_ma = termService.getAreaByTdwgAbbreviation("YUG-MA");
121
        yug_mn = termService.getAreaByTdwgAbbreviation("YUG-MN");
122

    
123
        engine.updatePriorities();
124
    }
125

    
126
    @Test
127
    @DataSet
128
    public void testPriorities() throws FileNotFoundException {
129

    
130
        Set extensions = termService.load(PresenceAbsenceTerm.CULTIVATED().getUuid()).getExtensions();
131
        assertEquals(TransmissionEngineDistribution.EXTENSION_VALUE_PREFIX + "45", ((Extension)extensions.iterator().next()).getValue());
132
    }
133

    
134
    @Test
135
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
136
    public void test_ignore() throws FileNotFoundException {
137

    
138
        addDistributions(
139
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
140
                new Distribution[] {
141
                        // should succeed during area aggregation be ignored by rank aggregation
142
                        // => yug will get status ENDEMIC_FOR_THE_RELEVANT_AREA
143
                        //    but only for LAPSANA_COMMUNIS_ALPINA
144
                        Distribution.NewInstance(yug_mn, PresenceAbsenceTerm.ENDEMIC_FOR_THE_RELEVANT_AREA()),
145
                        // should be ignored by area aggregation
146
                        // => LAPSANA_COMMUNIS will wave distribution with yug_ko and INTRODUCED_FORMERLY_INTRODUCED
147
                        Distribution.NewInstance(yug_ko, PresenceAbsenceTerm.INTRODUCED_FORMERLY_INTRODUCED()),
148
               }
149
            );
150

    
151
        engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
152

    
153
        Taxon lapsana_communis_alpina  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_ALPINA_UUID);
154
        assertEquals(2, lapsana_communis_alpina.getDescriptions().size());
155
        // TODO test for yug => ENDEMIC_FOR_THE_RELEVANT_AREA in computed description
156

    
157
        Taxon lapsana_communis  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_UUID);
158
        assertEquals(1, lapsana_communis.getDescriptions().size());
159
        TaxonDescription description = lapsana_communis.getDescriptions().iterator().next();
160
        assertEquals(1, description.getElements().size());
161
        int numExpectedFound = 0;
162
        for (DescriptionElementBase element : description.getElements()){
163
            Distribution distribution = (Distribution)element;
164
            if(distribution.getArea().equals(yug_ko)){
165
                numExpectedFound++;
166
                assertEquals("aggregated status of area YUG-KO wrong", PresenceAbsenceTerm.INTRODUCED_FORMERLY_INTRODUCED().getLabel(), distribution.getStatus().getLabel());
167
            }
168
        }
169
        assertEquals("All three expected areas should have been found before", numExpectedFound, 1);
170
    }
171

    
172
    @Test
173
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
174
    public void testArea_area() {
175

    
176
        addDistributions(
177
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
178
                new Distribution[] {
179
                        Distribution.NewInstance(yug_mn, PresenceAbsenceTerm.CULTIVATED()),
180
                        Distribution.NewInstance(yug_ko, PresenceAbsenceTerm.NATIVE()), // should succeed
181
                        Distribution.NewInstance(yug_bh, PresenceAbsenceTerm.INTRODUCED())
182
               }
183
            );
184

    
185
        Taxon lapsana_communis_alpina  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_ALPINA_UUID);
186
        assertEquals(1, lapsana_communis_alpina.getDescriptions().size());
187

    
188
        engine.accumulate(AggregationMode.byAreas, superAreas, lowerRank, upperRank, classification, null);
189

    
190
        lapsana_communis_alpina  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_ALPINA_UUID);
191
        assertEquals(2, lapsana_communis_alpina.getDescriptions().size());
192

    
193
        boolean expectedAreaFound = false;
194
        for (TaxonDescription description : lapsana_communis_alpina.getDescriptions()) {
195
            Distribution distribution = (Distribution) description.getElements().iterator().next(); // only one aggregated area expected
196
            if(distribution.getArea().equals(yug)) {
197
                expectedAreaFound = true;
198
                assertEquals("aggregated status of area YUG is wrong", PresenceAbsenceTerm.NATIVE().getLabel(), distribution.getStatus().getLabel());
199
            }
200
        }
201
        assertTrue("The areae YUG should have been found", expectedAreaFound);
202
    }
203

    
204
    @Test
205
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
206
    public void testArea_rank_and_area() {
207

    
208
        addDistributions(
209
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
210
                new Distribution[] {
211
                        Distribution.NewInstance(yug_mn, PresenceAbsenceTerm.CULTIVATED()),
212
                        Distribution.NewInstance(yug_ko, PresenceAbsenceTerm.NATIVE()), // should succeed
213
               }
214
            );
215
        addDistributions(
216
                T_LAPSANA_COMMUNIS_UUID,
217
                new Distribution[] {
218
                        Distribution.NewInstance(yug_mn, PresenceAbsenceTerm.INTRODUCED_UNCERTAIN_DEGREE_OF_NATURALISATION()),
219
                        Distribution.NewInstance(yug_ko, PresenceAbsenceTerm.CULTIVATED()),
220
               }
221
            );
222

    
223
        engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
224

    
225
        Taxon lapsana_communis  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_UUID);
226
        assertEquals(2, lapsana_communis.getDescriptions().size());
227

    
228
        Taxon lapsana = (Taxon) taxonService.load(T_LAPSANA_UUID);
229
        assertEquals(1, lapsana.getDescriptions().size());
230
        TaxonDescription description = lapsana.getDescriptions().iterator().next();
231
        assertEquals(3, description.getElements().size());
232
        int numExpectedFound = 0;
233
        for (DescriptionElementBase element : description.getElements()){
234
            Distribution distribution = (Distribution)element;
235
            if(distribution.getArea().equals(yug)){
236
                numExpectedFound++;
237
                assertEquals("aggregated status of area YUG is wrong", PresenceAbsenceTerm.NATIVE().getLabel(), distribution.getStatus().getLabel());
238
            }
239
            if(distribution.getArea().equals(yug_mn)){
240
                numExpectedFound++;
241
                assertEquals("aggregated status of area YUG-MN is wrong", PresenceAbsenceTerm.CULTIVATED().getLabel(), distribution.getStatus().getLabel());
242
            }
243
            if(distribution.getArea().equals(yug_ko)){
244
                numExpectedFound++;
245
                assertEquals("aggregated status of area YUG-KO wrong", PresenceAbsenceTerm.NATIVE().getLabel(), distribution.getStatus().getLabel());
246
            }
247
        }
248
        assertEquals("All three expected areas should have been found before", numExpectedFound, 3);
249
    }
250

    
251
    /**
252
     * creates a new description for the taxon identified by the UUIDs
253
     * @param taxonUuid
254
     * @param distributions
255
     */
256
    private void addDistributions(UUID taxonUuid, Distribution[] distributions) {
257
        Taxon taxon = (Taxon) taxonService.load(taxonUuid);
258
        if(taxon == null) {
259
            throw new NullPointerException("No taxon found for " + taxonUuid);
260
        }
261
        TaxonDescription description = TaxonDescription.NewInstance(taxon);
262

    
263
         for (Distribution distribution : distributions) {
264
             description.addElement(distribution);
265
        }
266
        taxonService.saveOrUpdate(taxon);
267
        // need to write to database for transmission engine
268
        commitAndStartNewTransaction(null);
269
    }
270

    
271

    
272
//    @Test //  uncomment to create test data file//
273
    @Override
274
    public void createTestDataSet() throws FileNotFoundException {
275

    
276
        // --- References --- //
277
        Reference sec = ReferenceFactory.newDatabase();
278
        sec.setTitleCache("Test", true);
279
        Reference nomRef = ReferenceFactory.newBook();
280
        sec.setTitleCache("Sp.Pl.", true);
281

    
282
        referenceService.save(sec);
283
        referenceService.save(nomRef);
284

    
285

    
286
        // --- Taxa --- //
287
        //  Lapsana
288
        //        L. communis
289
        //            L. communis subsp. communis
290
        //            L. communis subsp. adenophora
291
        //            L. communis subsp. alpina
292
        //  Sonchella
293
        //        S. dentata
294
        //        S. stenoma
295
        BotanicalName n_lapsana = BotanicalName.NewInstance(Rank.GENUS());
296
        n_lapsana.setTitleCache("Lapsana", true);
297
        Taxon t_lapsana = Taxon.NewInstance(n_lapsana, sec);
298
        t_lapsana.setUuid(T_LAPSANA_UUID);
299
        taxonService.saveOrUpdate(t_lapsana);
300

    
301
        BotanicalName n_lapsana_communis = BotanicalName.NewInstance(Rank.SPECIES());
302
        n_lapsana_communis.setTitleCache("L. communis", true);
303
        Taxon t_lapsana_communis = Taxon.NewInstance(n_lapsana_communis, sec);
304
        t_lapsana_communis.setUuid(T_LAPSANA_COMMUNIS_UUID);
305
        taxonService.saveOrUpdate(t_lapsana_communis);
306

    
307
        BotanicalName n_lapsana_communis_communis = BotanicalName.NewInstance(Rank.SUBSPECIES());
308
        n_lapsana_communis_communis.setTitleCache("L. communis subsp. communis", true);
309
        Taxon t_lapsana_communis_communis = Taxon.NewInstance(n_lapsana_communis_communis, sec);
310
        t_lapsana_communis_communis.setUuid(T_LAPSANA_COMMUNIS_COMMUNIS_UUID);
311
        taxonService.saveOrUpdate(t_lapsana_communis_communis);
312

    
313
        BotanicalName n_lapsana_communis_adenophora = BotanicalName.NewInstance(Rank.SUBSPECIES());
314
        n_lapsana_communis_adenophora.setTitleCache("L. communis subsp. adenophora", true);
315
        Taxon t_lapsana_communis_adenophora = Taxon.NewInstance(n_lapsana_communis_adenophora, sec);
316
        t_lapsana_communis_adenophora.setUuid(T_LAPSANA_COMMUNIS_ADENOPHORA_UUID);
317
        taxonService.saveOrUpdate(t_lapsana_communis_adenophora);
318

    
319
        BotanicalName n_lapsana_communis_alpina = BotanicalName.NewInstance(Rank.SUBSPECIES());
320
        n_lapsana_communis_alpina.setTitleCache("L. communis subsp. alpina", true);
321
        Taxon t_lapsana_communis_alpina = Taxon.NewInstance(n_lapsana_communis_alpina, sec);
322
        t_lapsana_communis_alpina.setUuid(T_LAPSANA_COMMUNIS_ALPINA_UUID);
323
        taxonService.saveOrUpdate(t_lapsana_communis_alpina);
324

    
325
        // --- Classification --- //
326
        Classification classification = Classification.NewInstance("TestClassification");
327
        classification.setUuid(CLASSIFICATION_UUID);
328
        classificationService.save(classification);
329
        TaxonNode node_lapsana = classification.addChildTaxon(t_lapsana, sec, null);
330
        TaxonNode node_lapsana_communis = node_lapsana.addChildTaxon(t_lapsana_communis, sec, null);
331
        node_lapsana_communis.addChildTaxon(t_lapsana_communis_communis, sec, null);
332
        node_lapsana_communis.addChildTaxon(t_lapsana_communis_adenophora, sec, null);
333
        node_lapsana_communis.addChildTaxon(t_lapsana_communis_alpina, sec, null);
334
        classificationService.saveOrUpdate(classification);
335

    
336
        // --- Distributions --- //
337
        // tdwg3 level YUG :  Yugoslavia
338
        // contains tdwg4 level areas :
339
        //   YUG-BH	Bosnia-Herzegovina
340
        //   YUG-CR	Croatia
341
        //   YUG-KO	Kosovo
342
        //   YUG-MA	Macedonia
343
        //   YUG-MN	Montenegro
344

    
345
        // assigning distribution information to taxa
346
        // expectations regarding the aggregation can be found in the comments below
347
//        TaxonDescription d_lapsana_communis_communis = TaxonDescription.NewInstance(t_lapsana_communis_communis);
348
//        d_lapsana_communis_communis.addElement(Distribution.NewInstance(
349
//                    TdwgArea.getAreaByTdwgAbbreviation("YUG-MN"),
350
//                    PresenceTerm.ENDEMIC_FOR_THE_RELEVANT_AREA() // should be ignored
351
//                    );
352

    
353
        commitAndStartNewTransaction(null);
354

    
355
        writeDbUnitDataSetFile(new String[] {
356
                "TAXONBASE", "TAXONNAMEBASE",
357
                "REFERENCE", "DESCRIPTIONELEMENTBASE", "DESCRIPTIONBASE",
358
                "AGENTBASE", "CLASSIFICATION",  "TAXONNODE",
359
                "HOMOTYPICALGROUP", "LANGUAGESTRING",
360
         });
361

    
362
    }
363

    
364
}
(29-29/31)