Project

General

Profile

Download (16.4 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.dbunit.annotation.DataSets;
26
import org.unitils.spring.annotation.SpringBeanByType;
27

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

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

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

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

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

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

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

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

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

    
68

    
69
    @SpringBeanByType
70
    private ITermService termService;
71

    
72
    @SpringBeanByType
73
    private ITaxonService taxonService;
74

    
75
    @SpringBeanByType
76
    private IClassificationService classificationService;
77

    
78
    @SpringBeanByType
79
    private IReferenceService referenceService;
80

    
81
    @SpringBeanByType
82
    private TransmissionEngineDistribution engine;
83

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

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

    
103
    private Classification classification;
104

    
105

    
106
    @Before
107
    public void setUp() {
108

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

    
115
        classification = classificationService.load(CLASSIFICATION_UUID);
116

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

    
124
        engine.updatePriorities();
125
    }
126

    
127
    @Test
128
    @DataSet
129
    public void testPriorities(){
130

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

    
135
    @Test
136
    @DataSets({
137
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
138
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
139
        @DataSet(value="TransmissionEngineDistributionTest.xml"),
140
    })
141
//  @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class) //, value="./BlankDataSet.xml")
142
    public void test_ignore() {
143

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

    
157
        engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
158

    
159
        Taxon lapsana_communis_alpina  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_ALPINA_UUID);
160
        assertEquals(2, lapsana_communis_alpina.getDescriptions().size());
161
        // TODO test for yug => ENDEMIC_FOR_THE_RELEVANT_AREA in computed description
162

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

    
178
    @Test
179
    @DataSets({
180
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
181
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
182
        @DataSet(value="TransmissionEngineDistributionTest.xml"),
183
    })
184
    public void testArea_area() {
185

    
186
        addDistributions(
187
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
188
                new Distribution[] {
189
                        Distribution.NewInstance(yug_mn, PresenceAbsenceTerm.CULTIVATED()),
190
                        Distribution.NewInstance(yug_ko, PresenceAbsenceTerm.NATIVE()), // should succeed
191
                        Distribution.NewInstance(yug_bh, PresenceAbsenceTerm.INTRODUCED())
192
               }
193
            );
194

    
195
        Taxon lapsana_communis_alpina  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_ALPINA_UUID);
196
        assertEquals(1, lapsana_communis_alpina.getDescriptions().size());
197

    
198
        engine.accumulate(AggregationMode.byAreas, superAreas, lowerRank, upperRank, classification, null);
199

    
200
        lapsana_communis_alpina  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_ALPINA_UUID);
201
        assertEquals(2, lapsana_communis_alpina.getDescriptions().size());
202

    
203
        boolean expectedAreaFound = false;
204
        for (TaxonDescription description : lapsana_communis_alpina.getDescriptions()) {
205
            Distribution distribution = (Distribution) description.getElements().iterator().next(); // only one aggregated area expected
206
            if(distribution.getArea().equals(yug)) {
207
                expectedAreaFound = true;
208
                assertEquals("aggregated status of area YUG is wrong", PresenceAbsenceTerm.NATIVE().getLabel(), distribution.getStatus().getLabel());
209
            }
210
        }
211
        assertTrue("The areae YUG should have been found", expectedAreaFound);
212
    }
213

    
214
    @Test
215
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
216
    public void testArea_rank_and_area() {
217

    
218
        addDistributions(
219
                T_LAPSANA_COMMUNIS_ALPINA_UUID,
220
                new Distribution[] {
221
                        Distribution.NewInstance(yug_mn, PresenceAbsenceTerm.CULTIVATED()),
222
                        Distribution.NewInstance(yug_ko, PresenceAbsenceTerm.NATIVE()), // should succeed
223
               }
224
            );
225
        addDistributions(
226
                T_LAPSANA_COMMUNIS_UUID,
227
                new Distribution[] {
228
                        Distribution.NewInstance(yug_mn, PresenceAbsenceTerm.INTRODUCED_UNCERTAIN_DEGREE_OF_NATURALISATION()),
229
                        Distribution.NewInstance(yug_ko, PresenceAbsenceTerm.CULTIVATED()),
230
               }
231
            );
232

    
233
        engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
234

    
235
        Taxon lapsana_communis  = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_UUID);
236
        assertEquals(2, lapsana_communis.getDescriptions().size());
237

    
238
        Taxon lapsana = (Taxon) taxonService.load(T_LAPSANA_UUID);
239
        assertEquals(1, lapsana.getDescriptions().size());
240
        TaxonDescription description = lapsana.getDescriptions().iterator().next();
241
        assertEquals(3, description.getElements().size());
242
        int numExpectedFound = 0;
243
        for (DescriptionElementBase element : description.getElements()){
244
            Distribution distribution = (Distribution)element;
245
            if(distribution.getArea().equals(yug)){
246
                numExpectedFound++;
247
                assertEquals("aggregated status of area YUG is wrong", PresenceAbsenceTerm.NATIVE().getLabel(), distribution.getStatus().getLabel());
248
            }
249
            if(distribution.getArea().equals(yug_mn)){
250
                numExpectedFound++;
251
                assertEquals("aggregated status of area YUG-MN is wrong", PresenceAbsenceTerm.CULTIVATED().getLabel(), distribution.getStatus().getLabel());
252
            }
253
            if(distribution.getArea().equals(yug_ko)){
254
                numExpectedFound++;
255
                assertEquals("aggregated status of area YUG-KO wrong", PresenceAbsenceTerm.NATIVE().getLabel(), distribution.getStatus().getLabel());
256
            }
257
        }
258
        assertEquals("All three expected areas should have been found before", numExpectedFound, 3);
259
    }
260

    
261
    /**
262
     * creates a new description for the taxon identified by the UUIDs
263
     * @param taxonUuid
264
     * @param distributions
265
     */
266
    private void addDistributions(UUID taxonUuid, Distribution[] distributions) {
267
        Taxon taxon = (Taxon) taxonService.load(taxonUuid);
268
        if(taxon == null) {
269
            throw new NullPointerException("No taxon found for " + taxonUuid);
270
        }
271
        TaxonDescription description = TaxonDescription.NewInstance(taxon);
272

    
273
         for (Distribution distribution : distributions) {
274
             description.addElement(distribution);
275
        }
276
        taxonService.saveOrUpdate(taxon);
277
        // need to write to database for transmission engine
278
        commitAndStartNewTransaction(null);
279
    }
280

    
281

    
282
    //@Test //  uncomment to create test data file//
283
    @Override
284
    public void createTestDataSet() throws FileNotFoundException {
285

    
286
        // --- References --- //
287
        Reference sec = ReferenceFactory.newDatabase();
288
        sec.setTitleCache("Test", true);
289
        Reference nomRef = ReferenceFactory.newBook();
290
        sec.setTitleCache("Sp.Pl.", true);
291

    
292
        referenceService.save(sec);
293
        referenceService.save(nomRef);
294

    
295

    
296
        // --- Taxa --- //
297
        //  Lapsana
298
        //        L. communis
299
        //            L. communis subsp. communis
300
        //            L. communis subsp. adenophora
301
        //            L. communis subsp. alpina
302
        //  Sonchella
303
        //        S. dentata
304
        //        S. stenoma
305
        BotanicalName n_lapsana = BotanicalName.NewInstance(Rank.GENUS());
306
        n_lapsana.setTitleCache("Lapsana", true);
307
        Taxon t_lapsana = Taxon.NewInstance(n_lapsana, sec);
308
        t_lapsana.setUuid(T_LAPSANA_UUID);
309
        taxonService.saveOrUpdate(t_lapsana);
310

    
311
        BotanicalName n_lapsana_communis = BotanicalName.NewInstance(Rank.SPECIES());
312
        n_lapsana_communis.setTitleCache("L. communis", true);
313
        Taxon t_lapsana_communis = Taxon.NewInstance(n_lapsana_communis, sec);
314
        t_lapsana_communis.setUuid(T_LAPSANA_COMMUNIS_UUID);
315
        taxonService.saveOrUpdate(t_lapsana_communis);
316

    
317
        BotanicalName n_lapsana_communis_communis = BotanicalName.NewInstance(Rank.SUBSPECIES());
318
        n_lapsana_communis_communis.setTitleCache("L. communis subsp. communis", true);
319
        Taxon t_lapsana_communis_communis = Taxon.NewInstance(n_lapsana_communis_communis, sec);
320
        t_lapsana_communis_communis.setUuid(T_LAPSANA_COMMUNIS_COMMUNIS_UUID);
321
        taxonService.saveOrUpdate(t_lapsana_communis_communis);
322

    
323
        BotanicalName n_lapsana_communis_adenophora = BotanicalName.NewInstance(Rank.SUBSPECIES());
324
        n_lapsana_communis_adenophora.setTitleCache("L. communis subsp. adenophora", true);
325
        Taxon t_lapsana_communis_adenophora = Taxon.NewInstance(n_lapsana_communis_adenophora, sec);
326
        t_lapsana_communis_adenophora.setUuid(T_LAPSANA_COMMUNIS_ADENOPHORA_UUID);
327
        taxonService.saveOrUpdate(t_lapsana_communis_adenophora);
328

    
329
        BotanicalName n_lapsana_communis_alpina = BotanicalName.NewInstance(Rank.SUBSPECIES());
330
        n_lapsana_communis_alpina.setTitleCache("L. communis subsp. alpina", true);
331
        Taxon t_lapsana_communis_alpina = Taxon.NewInstance(n_lapsana_communis_alpina, sec);
332
        t_lapsana_communis_alpina.setUuid(T_LAPSANA_COMMUNIS_ALPINA_UUID);
333
        taxonService.saveOrUpdate(t_lapsana_communis_alpina);
334

    
335
        // --- Classification --- //
336
        Classification classification = Classification.NewInstance("TestClassification");
337
        classification.setUuid(CLASSIFICATION_UUID);
338
        classificationService.save(classification);
339
        TaxonNode node_lapsana = classification.addChildTaxon(t_lapsana, sec, null);
340
        TaxonNode node_lapsana_communis = node_lapsana.addChildTaxon(t_lapsana_communis, sec, null);
341
        node_lapsana_communis.addChildTaxon(t_lapsana_communis_communis, sec, null);
342
        node_lapsana_communis.addChildTaxon(t_lapsana_communis_adenophora, sec, null);
343
        node_lapsana_communis.addChildTaxon(t_lapsana_communis_alpina, sec, null);
344
        classificationService.saveOrUpdate(classification);
345

    
346
        // --- Distributions --- //
347
        // tdwg3 level YUG :  Yugoslavia
348
        // contains tdwg4 level areas :
349
        //   YUG-BH	Bosnia-Herzegovina
350
        //   YUG-CR	Croatia
351
        //   YUG-KO	Kosovo
352
        //   YUG-MA	Macedonia
353
        //   YUG-MN	Montenegro
354

    
355
        // assigning distribution information to taxa
356
        // expectations regarding the aggregation can be found in the comments below
357
//        TaxonDescription d_lapsana_communis_communis = TaxonDescription.NewInstance(t_lapsana_communis_communis);
358
//        d_lapsana_communis_communis.addElement(Distribution.NewInstance(
359
//                    TdwgArea.getAreaByTdwgAbbreviation("YUG-MN"),
360
//                    PresenceTerm.ENDEMIC_FOR_THE_RELEVANT_AREA() // should be ignored
361
//                    );
362

    
363
        commitAndStartNewTransaction(null);
364

    
365
        writeDbUnitDataSetFile(new String[] {
366
                "TAXONBASE", "TAXONNAMEBASE",
367
                "REFERENCE", "DESCRIPTIONELEMENTBASE", "DESCRIPTIONBASE",
368
                "AGENTBASE", "CLASSIFICATION",  "TAXONNODE",
369
                "HOMOTYPICALGROUP", "LANGUAGESTRING",
370
         });
371

    
372
    }
373

    
374
}
(29-29/31)