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
.AbsenceTerm
;
31 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
32 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
33 import eu
.etaxonomy
.cdm
.model
.description
.PresenceTerm
;
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
;
47 * @author a.kohlbecker
51 public class TransmissionEngineDistributionTest
extends CdmTransactionalIntegrationTest
{
53 @SuppressWarnings("unused")
54 private static Logger logger
= Logger
.getLogger(DescriptionServiceImplTest
.class);
56 private static final UUID T_LAPSANA_UUID
= UUID
.fromString("f65d47bd-4f49-4ab1-bc4a-bc4551eaa1a8");
58 private static final UUID T_LAPSANA_COMMUNIS_UUID
= UUID
.fromString("2a5ceebb-4830-4524-b330-78461bf8cb6b");
60 private static final UUID T_LAPSANA_COMMUNIS_COMMUNIS_UUID
= UUID
.fromString("441a3c40-0c84-11de-8c30-0800200c9a66");
62 private static final UUID T_LAPSANA_COMMUNIS_ADENOPHORA_UUID
= UUID
.fromString("e4acf200-63b6-11dd-ad8b-0800200c9a66");
64 private static final UUID T_LAPSANA_COMMUNIS_ALPINA_UUID
= UUID
.fromString("596b1325-be50-4b0a-9aa2-3ecd610215f2");
66 private static final UUID CLASSIFICATION_UUID
= UUID
.fromString("4b266053-a841-4980-b548-3f21d8d7d712");
70 private ITermService termService
;
73 private ITaxonService taxonService
;
76 private IClassificationService classificationService
;
79 private IReferenceService referenceService
;
82 private TransmissionEngineDistribution engine
;
84 // --- Distributions --- //
85 // tdwg3 level YUG : Yugoslavia
86 // contains tdwg4 level areas :
87 // YUG-BH Bosnia-Herzegovina
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;
99 List
<NamedArea
> superAreas
= null;
100 Rank upperRank
= null;
101 Rank lowerRank
= null;
103 private Classification classification
;
107 public void setUp() {
109 superAreas
= Arrays
.asList(new NamedArea
[]{
110 termService
.getAreaByTdwgAbbreviation("YUG")
112 lowerRank
= Rank
.SPECIES();
113 upperRank
= Rank
.GENUS();
115 classification
= classificationService
.load(CLASSIFICATION_UUID
);
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");
124 engine
.updatePriorities();
129 public void testPriorities() throws FileNotFoundException
{
131 Set extensions
= termService
.load(PresenceTerm
.CULTIVATED().getUuid()).getExtensions();
132 assertEquals(TransmissionEngineDistribution
.EXTENSION_VALUE_PREFIX
+ "45", ((Extension
)extensions
.iterator().next()).getValue());
136 @DataSet(loadStrategy
=CleanSweepInsertLoadStrategy
.class)
137 public void test_ignore() throws FileNotFoundException
{
140 T_LAPSANA_COMMUNIS_ALPINA_UUID
,
142 // should succeed during area aggregation be ignored by rank aggregation
143 // => yug will get status ENDEMIC_FOR_THE_RELEVANT_AREA
144 // but only for LAPSANA_COMMUNIS_ALPINA
145 Distribution
.NewInstance(yug_mn
, PresenceTerm
.ENDEMIC_FOR_THE_RELEVANT_AREA()),
146 // should be ignored by area aggregation
147 // => LAPSANA_COMMUNIS will wave distribution with yug_ko and INTRODUCED_FORMERLY_INTRODUCED
148 Distribution
.NewInstance(yug_ko
, AbsenceTerm
.INTRODUCED_FORMERLY_INTRODUCED()),
152 engine
.accumulate(AggregationMode
.byAreasAndRanks
, superAreas
, lowerRank
, upperRank
, null, null);
154 Taxon lapsana_communis_alpina
= (Taxon
) taxonService
.load(T_LAPSANA_COMMUNIS_ALPINA_UUID
);
155 assertEquals(2, lapsana_communis_alpina
.getDescriptions().size());
156 // TODO test for yug => ENDEMIC_FOR_THE_RELEVANT_AREA in computed description
158 Taxon lapsana_communis
= (Taxon
) taxonService
.load(T_LAPSANA_COMMUNIS_UUID
);
159 assertEquals(1, lapsana_communis
.getDescriptions().size());
160 TaxonDescription description
= lapsana_communis
.getDescriptions().iterator().next();
161 assertEquals(1, description
.getElements().size());
162 int numExpectedFound
= 0;
163 for (DescriptionElementBase element
: description
.getElements()){
164 Distribution distribution
= (Distribution
)element
;
165 if(distribution
.getArea().equals(yug_ko
)){
167 assertEquals("aggregated status of area YUG-KO wrong", AbsenceTerm
.INTRODUCED_FORMERLY_INTRODUCED().getLabel(), distribution
.getStatus().getLabel());
170 assertEquals("All three expected areas should have been found before", numExpectedFound
, 1);
174 @DataSet(loadStrategy
=CleanSweepInsertLoadStrategy
.class)
175 public void testArea_area() throws FileNotFoundException
{
178 T_LAPSANA_COMMUNIS_ALPINA_UUID
,
180 Distribution
.NewInstance(yug_mn
, PresenceTerm
.CULTIVATED()),
181 Distribution
.NewInstance(yug_ko
, PresenceTerm
.NATIVE()), // should succeed
182 Distribution
.NewInstance(yug_bh
, PresenceTerm
.INTRODUCED())
186 Taxon lapsana_communis_alpina
= (Taxon
) taxonService
.load(T_LAPSANA_COMMUNIS_ALPINA_UUID
);
187 assertEquals(1, lapsana_communis_alpina
.getDescriptions().size());
189 engine
.accumulate(AggregationMode
.byAreasAndRanks
, superAreas
, lowerRank
, upperRank
, classification
, null);
191 lapsana_communis_alpina
= (Taxon
) taxonService
.load(T_LAPSANA_COMMUNIS_ALPINA_UUID
);
192 assertEquals(2, lapsana_communis_alpina
.getDescriptions().size());
194 boolean expectedAreaFound
= false;
195 for (TaxonDescription description
: lapsana_communis_alpina
.getDescriptions()) {
196 Distribution distribution
= (Distribution
) description
.getElements().iterator().next(); // only one aggregated area expected
197 if(distribution
.getArea().equals(yug
)) {
198 expectedAreaFound
= true;
199 assertEquals("aggregated status of area YUG is wrong", PresenceTerm
.NATIVE().getLabel(), distribution
.getStatus().getLabel());
202 assertTrue("The areae YUG should have been found", expectedAreaFound
);
206 @DataSet(loadStrategy
=CleanSweepInsertLoadStrategy
.class)
207 public void testArea_rank_and_area() throws FileNotFoundException
{
210 T_LAPSANA_COMMUNIS_ALPINA_UUID
,
212 Distribution
.NewInstance(yug_mn
, PresenceTerm
.CULTIVATED()),
213 Distribution
.NewInstance(yug_ko
, PresenceTerm
.NATIVE()), // should succeed
217 T_LAPSANA_COMMUNIS_UUID
,
219 Distribution
.NewInstance(yug_mn
, PresenceTerm
.INTRODUCED_UNCERTAIN_DEGREE_OF_NATURALISATION()),
220 Distribution
.NewInstance(yug_ko
, PresenceTerm
.CULTIVATED()),
224 engine
.accumulate(AggregationMode
.byAreasAndRanks
, superAreas
, lowerRank
, upperRank
, null, null);
226 Taxon lapsana_communis
= (Taxon
) taxonService
.load(T_LAPSANA_COMMUNIS_UUID
);
227 assertEquals(2, lapsana_communis
.getDescriptions().size());
229 Taxon lapsana
= (Taxon
) taxonService
.load(T_LAPSANA_UUID
);
230 assertEquals(1, lapsana
.getDescriptions().size());
231 TaxonDescription description
= lapsana
.getDescriptions().iterator().next();
232 assertEquals(3, description
.getElements().size());
233 int numExpectedFound
= 0;
234 for (DescriptionElementBase element
: description
.getElements()){
235 Distribution distribution
= (Distribution
)element
;
236 if(distribution
.getArea().equals(yug
)){
238 assertEquals("aggregated status of area YUG is wrong", PresenceTerm
.NATIVE().getLabel(), distribution
.getStatus().getLabel());
240 if(distribution
.getArea().equals(yug_mn
)){
242 assertEquals("aggregated status of area YUG-MN is wrong", PresenceTerm
.CULTIVATED().getLabel(), distribution
.getStatus().getLabel());
244 if(distribution
.getArea().equals(yug_ko
)){
246 assertEquals("aggregated status of area YUG-KO wrong", PresenceTerm
.NATIVE().getLabel(), distribution
.getStatus().getLabel());
249 assertEquals("All three expected areas should have been found before", numExpectedFound
, 3);
253 * creates a new description for the taxon identified by the UUIDs
255 * @param distributions
257 private void addDistributions(UUID taxonUuid
, Distribution
[] distributions
) {
258 Taxon taxon
= (Taxon
) taxonService
.load(taxonUuid
);
260 throw new NullPointerException("No taxon found for " + taxonUuid
);
262 TaxonDescription description
= TaxonDescription
.NewInstance(taxon
);
264 for (Distribution distribution
: distributions
) {
265 description
.addElement(distribution
);
267 taxonService
.saveOrUpdate(taxon
);
268 // need to write to database for transmission engine
269 commitAndStartNewTransaction(null);
273 // @Test // uncomment to create test data file//
275 public void createTestDataSet() throws FileNotFoundException
{
277 // --- References --- //
278 Reference sec
= ReferenceFactory
.newDatabase();
279 sec
.setTitleCache("Test", true);
280 Reference nomRef
= ReferenceFactory
.newBook();
281 sec
.setTitleCache("Sp.Pl.", true);
283 referenceService
.save(sec
);
284 referenceService
.save(nomRef
);
290 // L. communis subsp. communis
291 // L. communis subsp. adenophora
292 // L. communis subsp. alpina
296 BotanicalName n_lapsana
= BotanicalName
.NewInstance(Rank
.GENUS());
297 n_lapsana
.setTitleCache("Lapsana", true);
298 Taxon t_lapsana
= Taxon
.NewInstance(n_lapsana
, sec
);
299 t_lapsana
.setUuid(T_LAPSANA_UUID
);
300 taxonService
.saveOrUpdate(t_lapsana
);
302 BotanicalName n_lapsana_communis
= BotanicalName
.NewInstance(Rank
.SPECIES());
303 n_lapsana_communis
.setTitleCache("L. communis", true);
304 Taxon t_lapsana_communis
= Taxon
.NewInstance(n_lapsana_communis
, sec
);
305 t_lapsana_communis
.setUuid(T_LAPSANA_COMMUNIS_UUID
);
306 taxonService
.saveOrUpdate(t_lapsana_communis
);
308 BotanicalName n_lapsana_communis_communis
= BotanicalName
.NewInstance(Rank
.SUBSPECIES());
309 n_lapsana_communis_communis
.setTitleCache("L. communis subsp. communis", true);
310 Taxon t_lapsana_communis_communis
= Taxon
.NewInstance(n_lapsana_communis_communis
, sec
);
311 t_lapsana_communis_communis
.setUuid(T_LAPSANA_COMMUNIS_COMMUNIS_UUID
);
312 taxonService
.saveOrUpdate(t_lapsana_communis_communis
);
314 BotanicalName n_lapsana_communis_adenophora
= BotanicalName
.NewInstance(Rank
.SUBSPECIES());
315 n_lapsana_communis_adenophora
.setTitleCache("L. communis subsp. adenophora", true);
316 Taxon t_lapsana_communis_adenophora
= Taxon
.NewInstance(n_lapsana_communis_adenophora
, sec
);
317 t_lapsana_communis_adenophora
.setUuid(T_LAPSANA_COMMUNIS_ADENOPHORA_UUID
);
318 taxonService
.saveOrUpdate(t_lapsana_communis_adenophora
);
320 BotanicalName n_lapsana_communis_alpina
= BotanicalName
.NewInstance(Rank
.SUBSPECIES());
321 n_lapsana_communis_alpina
.setTitleCache("L. communis subsp. alpina", true);
322 Taxon t_lapsana_communis_alpina
= Taxon
.NewInstance(n_lapsana_communis_alpina
, sec
);
323 t_lapsana_communis_alpina
.setUuid(T_LAPSANA_COMMUNIS_ALPINA_UUID
);
324 taxonService
.saveOrUpdate(t_lapsana_communis_alpina
);
326 // --- Classification --- //
327 Classification classification
= Classification
.NewInstance("TestClassification");
328 classification
.setUuid(CLASSIFICATION_UUID
);
329 classificationService
.save(classification
);
330 TaxonNode node_lapsana
= classification
.addChildTaxon(t_lapsana
, sec
, null);
331 TaxonNode node_lapsana_communis
= node_lapsana
.addChildTaxon(t_lapsana_communis
, sec
, null);
332 node_lapsana_communis
.addChildTaxon(t_lapsana_communis_communis
, sec
, null);
333 node_lapsana_communis
.addChildTaxon(t_lapsana_communis_adenophora
, sec
, null);
334 node_lapsana_communis
.addChildTaxon(t_lapsana_communis_alpina
, sec
, null);
335 classificationService
.saveOrUpdate(classification
);
337 // --- Distributions --- //
338 // tdwg3 level YUG : Yugoslavia
339 // contains tdwg4 level areas :
340 // YUG-BH Bosnia-Herzegovina
346 // assigning distribution information to taxa
347 // expectations regarding the aggregation can be found in the comments below
348 // TaxonDescription d_lapsana_communis_communis = TaxonDescription.NewInstance(t_lapsana_communis_communis);
349 // d_lapsana_communis_communis.addElement(Distribution.NewInstance(
350 // TdwgArea.getAreaByTdwgAbbreviation("YUG-MN"),
351 // PresenceTerm.ENDEMIC_FOR_THE_RELEVANT_AREA() // should be ignored
354 commitAndStartNewTransaction(null);
356 writeDbUnitDataSetFile(new String
[] {
357 "TAXONBASE", "TAXONNAMEBASE",
358 "REFERENCE", "DESCRIPTIONELEMENTBASE", "DESCRIPTIONBASE",
359 "AGENTBASE", "CLASSIFICATION", "TAXONNODE",
360 "HOMOTYPICALGROUP", "LANGUAGESTRING",