3 * Copyright (C) 2013 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.api
.service
;
12 import static org
.junit
.Assert
.assertEquals
;
13 import static org
.junit
.Assert
.assertTrue
;
15 import java
.io
.FileNotFoundException
;
16 import java
.util
.Arrays
;
17 import java
.util
.List
;
19 import java
.util
.UUID
;
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
;
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
;
46 * @author a.kohlbecker
50 public class TransmissionEngineDistributionTest
extends CdmTransactionalIntegrationTest
{
52 @SuppressWarnings("unused")
53 private static Logger logger
= Logger
.getLogger(DescriptionServiceImplTest
.class);
55 private static final UUID T_LAPSANA_UUID
= UUID
.fromString("f65d47bd-4f49-4ab1-bc4a-bc4551eaa1a8");
57 private static final UUID T_LAPSANA_COMMUNIS_UUID
= UUID
.fromString("2a5ceebb-4830-4524-b330-78461bf8cb6b");
59 private static final UUID T_LAPSANA_COMMUNIS_COMMUNIS_UUID
= UUID
.fromString("441a3c40-0c84-11de-8c30-0800200c9a66");
61 private static final UUID T_LAPSANA_COMMUNIS_ADENOPHORA_UUID
= UUID
.fromString("e4acf200-63b6-11dd-ad8b-0800200c9a66");
63 private static final UUID T_LAPSANA_COMMUNIS_ALPINA_UUID
= UUID
.fromString("596b1325-be50-4b0a-9aa2-3ecd610215f2");
65 private static final UUID CLASSIFICATION_UUID
= UUID
.fromString("4b266053-a841-4980-b548-3f21d8d7d712");
69 private ITermService termService
;
72 private ITaxonService taxonService
;
75 private IClassificationService classificationService
;
78 private IReferenceService referenceService
;
81 private TransmissionEngineDistribution engine
;
83 // --- Distributions --- //
84 // tdwg3 level YUG : Yugoslavia
85 // contains tdwg4 level areas :
86 // YUG-BH Bosnia-Herzegovina
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;
98 List
<NamedArea
> superAreas
= null;
99 Rank upperRank
= null;
100 Rank lowerRank
= null;
102 private Classification classification
;
106 public void setUp() {
108 superAreas
= Arrays
.asList(new NamedArea
[]{
109 termService
.getAreaByTdwgAbbreviation("YUG")
111 lowerRank
= Rank
.SPECIES();
112 upperRank
= Rank
.GENUS();
114 classification
= classificationService
.load(CLASSIFICATION_UUID
);
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");
123 engine
.updatePriorities();
128 public void testPriorities() throws FileNotFoundException
{
130 Set extensions
= termService
.load(PresenceAbsenceTerm
.CULTIVATED().getUuid()).getExtensions();
131 assertEquals(TransmissionEngineDistribution
.EXTENSION_VALUE_PREFIX
+ "45", ((Extension
)extensions
.iterator().next()).getValue());
135 @DataSet(loadStrategy
=CleanSweepInsertLoadStrategy
.class)
136 public void test_ignore() throws FileNotFoundException
{
139 T_LAPSANA_COMMUNIS_ALPINA_UUID
,
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()),
151 engine
.accumulate(AggregationMode
.byAreasAndRanks
, superAreas
, lowerRank
, upperRank
, null, null);
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
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
)){
166 assertEquals("aggregated status of area YUG-KO wrong", PresenceAbsenceTerm
.INTRODUCED_FORMERLY_INTRODUCED().getLabel(), distribution
.getStatus().getLabel());
169 assertEquals("All three expected areas should have been found before", numExpectedFound
, 1);
173 @DataSet(loadStrategy
=CleanSweepInsertLoadStrategy
.class)
174 public void testArea_area() throws FileNotFoundException
{
177 T_LAPSANA_COMMUNIS_ALPINA_UUID
,
179 Distribution
.NewInstance(yug_mn
, PresenceAbsenceTerm
.CULTIVATED()),
180 Distribution
.NewInstance(yug_ko
, PresenceAbsenceTerm
.NATIVE()), // should succeed
181 Distribution
.NewInstance(yug_bh
, PresenceAbsenceTerm
.INTRODUCED())
185 Taxon lapsana_communis_alpina
= (Taxon
) taxonService
.load(T_LAPSANA_COMMUNIS_ALPINA_UUID
);
186 assertEquals(1, lapsana_communis_alpina
.getDescriptions().size());
188 engine
.accumulate(AggregationMode
.byAreasAndRanks
, superAreas
, lowerRank
, upperRank
, classification
, null);
190 lapsana_communis_alpina
= (Taxon
) taxonService
.load(T_LAPSANA_COMMUNIS_ALPINA_UUID
);
191 assertEquals(2, lapsana_communis_alpina
.getDescriptions().size());
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());
201 assertTrue("The areae YUG should have been found", expectedAreaFound
);
205 @DataSet(loadStrategy
=CleanSweepInsertLoadStrategy
.class)
206 public void testArea_rank_and_area() throws FileNotFoundException
{
209 T_LAPSANA_COMMUNIS_ALPINA_UUID
,
211 Distribution
.NewInstance(yug_mn
, PresenceAbsenceTerm
.CULTIVATED()),
212 Distribution
.NewInstance(yug_ko
, PresenceAbsenceTerm
.NATIVE()), // should succeed
216 T_LAPSANA_COMMUNIS_UUID
,
218 Distribution
.NewInstance(yug_mn
, PresenceAbsenceTerm
.INTRODUCED_UNCERTAIN_DEGREE_OF_NATURALISATION()),
219 Distribution
.NewInstance(yug_ko
, PresenceAbsenceTerm
.CULTIVATED()),
223 engine
.accumulate(AggregationMode
.byAreasAndRanks
, superAreas
, lowerRank
, upperRank
, null, null);
225 Taxon lapsana_communis
= (Taxon
) taxonService
.load(T_LAPSANA_COMMUNIS_UUID
);
226 assertEquals(2, lapsana_communis
.getDescriptions().size());
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
)){
237 assertEquals("aggregated status of area YUG is wrong", PresenceAbsenceTerm
.NATIVE().getLabel(), distribution
.getStatus().getLabel());
239 if(distribution
.getArea().equals(yug_mn
)){
241 assertEquals("aggregated status of area YUG-MN is wrong", PresenceAbsenceTerm
.CULTIVATED().getLabel(), distribution
.getStatus().getLabel());
243 if(distribution
.getArea().equals(yug_ko
)){
245 assertEquals("aggregated status of area YUG-KO wrong", PresenceAbsenceTerm
.NATIVE().getLabel(), distribution
.getStatus().getLabel());
248 assertEquals("All three expected areas should have been found before", numExpectedFound
, 3);
252 * creates a new description for the taxon identified by the UUIDs
254 * @param distributions
256 private void addDistributions(UUID taxonUuid
, Distribution
[] distributions
) {
257 Taxon taxon
= (Taxon
) taxonService
.load(taxonUuid
);
259 throw new NullPointerException("No taxon found for " + taxonUuid
);
261 TaxonDescription description
= TaxonDescription
.NewInstance(taxon
);
263 for (Distribution distribution
: distributions
) {
264 description
.addElement(distribution
);
266 taxonService
.saveOrUpdate(taxon
);
267 // need to write to database for transmission engine
268 commitAndStartNewTransaction(null);
272 // @Test // uncomment to create test data file//
274 public void createTestDataSet() throws FileNotFoundException
{
276 // --- References --- //
277 Reference sec
= ReferenceFactory
.newDatabase();
278 sec
.setTitleCache("Test", true);
279 Reference nomRef
= ReferenceFactory
.newBook();
280 sec
.setTitleCache("Sp.Pl.", true);
282 referenceService
.save(sec
);
283 referenceService
.save(nomRef
);
289 // L. communis subsp. communis
290 // L. communis subsp. adenophora
291 // L. communis subsp. alpina
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
);
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
);
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
);
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
);
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
);
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
);
336 // --- Distributions --- //
337 // tdwg3 level YUG : Yugoslavia
338 // contains tdwg4 level areas :
339 // YUG-BH Bosnia-Herzegovina
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
353 commitAndStartNewTransaction(null);
355 writeDbUnitDataSetFile(new String
[] {
356 "TAXONBASE", "TAXONNAMEBASE",
357 "REFERENCE", "DESCRIPTIONELEMENTBASE", "DESCRIPTIONBASE",
358 "AGENTBASE", "CLASSIFICATION", "TAXONNODE",
359 "HOMOTYPICALGROUP", "LANGUAGESTRING",