- added abstract method createTestData() to CdmIntegrationalTest
[cdmlib.git] / cdmlib-services / src / test / java / eu / etaxonomy / cdm / api / service / TransmissionEngineDistributionTest.java
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.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;
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(DescriptionServiceImplTest.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.SPECIES();
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() throws FileNotFoundException {
130
131 Set extensions = termService.load(PresenceTerm.CULTIVATED().getUuid()).getExtensions();
132 assertEquals(TransmissionEngineDistribution.EXTENSION_VALUE_PREFIX + "45", ((Extension)extensions.iterator().next()).getValue());
133 }
134
135 @Test
136 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
137 public void test_ignore() throws FileNotFoundException {
138
139 addDistributions(
140 T_LAPSANA_COMMUNIS_ALPINA_UUID,
141 new Distribution[] {
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()),
149 }
150 );
151
152 engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
153
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
157
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)){
166 numExpectedFound++;
167 assertEquals("aggregated status of area YUG-KO wrong", AbsenceTerm.INTRODUCED_FORMERLY_INTRODUCED().getLabel(), distribution.getStatus().getLabel());
168 }
169 }
170 assertEquals("All three expected areas should have been found before", numExpectedFound, 1);
171 }
172
173 @Test
174 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
175 public void testArea_area() throws FileNotFoundException {
176
177 addDistributions(
178 T_LAPSANA_COMMUNIS_ALPINA_UUID,
179 new Distribution[] {
180 Distribution.NewInstance(yug_mn, PresenceTerm.CULTIVATED()),
181 Distribution.NewInstance(yug_ko, PresenceTerm.NATIVE()), // should succeed
182 Distribution.NewInstance(yug_bh, PresenceTerm.INTRODUCED())
183 }
184 );
185
186 Taxon lapsana_communis_alpina = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_ALPINA_UUID);
187 assertEquals(1, lapsana_communis_alpina.getDescriptions().size());
188
189 engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, classification, null);
190
191 lapsana_communis_alpina = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_ALPINA_UUID);
192 assertEquals(2, lapsana_communis_alpina.getDescriptions().size());
193
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());
200 }
201 }
202 assertTrue("The areae YUG should have been found", expectedAreaFound);
203 }
204
205 @Test
206 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
207 public void testArea_rank_and_area() throws FileNotFoundException {
208
209 addDistributions(
210 T_LAPSANA_COMMUNIS_ALPINA_UUID,
211 new Distribution[] {
212 Distribution.NewInstance(yug_mn, PresenceTerm.CULTIVATED()),
213 Distribution.NewInstance(yug_ko, PresenceTerm.NATIVE()), // should succeed
214 }
215 );
216 addDistributions(
217 T_LAPSANA_COMMUNIS_UUID,
218 new Distribution[] {
219 Distribution.NewInstance(yug_mn, PresenceTerm.INTRODUCED_UNCERTAIN_DEGREE_OF_NATURALISATION()),
220 Distribution.NewInstance(yug_ko, PresenceTerm.CULTIVATED()),
221 }
222 );
223
224 engine.accumulate(AggregationMode.byAreasAndRanks, superAreas, lowerRank, upperRank, null, null);
225
226 Taxon lapsana_communis = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_UUID);
227 assertEquals(2, lapsana_communis.getDescriptions().size());
228
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)){
237 numExpectedFound++;
238 assertEquals("aggregated status of area YUG is wrong", PresenceTerm.NATIVE().getLabel(), distribution.getStatus().getLabel());
239 }
240 if(distribution.getArea().equals(yug_mn)){
241 numExpectedFound++;
242 assertEquals("aggregated status of area YUG-MN is wrong", PresenceTerm.CULTIVATED().getLabel(), distribution.getStatus().getLabel());
243 }
244 if(distribution.getArea().equals(yug_ko)){
245 numExpectedFound++;
246 assertEquals("aggregated status of area YUG-KO wrong", PresenceTerm.NATIVE().getLabel(), distribution.getStatus().getLabel());
247 }
248 }
249 assertEquals("All three expected areas should have been found before", numExpectedFound, 3);
250 }
251
252 /**
253 * creates a new description for the taxon identified by the UUIDs
254 * @param taxonUuid
255 * @param distributions
256 */
257 private void addDistributions(UUID taxonUuid, Distribution[] distributions) {
258 Taxon taxon = (Taxon) taxonService.load(taxonUuid);
259 if(taxon == null) {
260 throw new NullPointerException("No taxon found for " + taxonUuid);
261 }
262 TaxonDescription description = TaxonDescription.NewInstance(taxon);
263
264 for (Distribution distribution : distributions) {
265 description.addElement(distribution);
266 }
267 taxonService.saveOrUpdate(taxon);
268 // need to write to database for transmission engine
269 commitAndStartNewTransaction(null);
270 }
271
272
273 // @Test // uncomment to create test data file//
274 @Override
275 public void createTestDataSet() throws FileNotFoundException {
276
277 // --- References --- //
278 Reference sec = ReferenceFactory.newDatabase();
279 sec.setTitleCache("Test", true);
280 Reference nomRef = ReferenceFactory.newBook();
281 sec.setTitleCache("Sp.Pl.", true);
282
283 referenceService.save(sec);
284 referenceService.save(nomRef);
285
286
287 // --- Taxa --- //
288 // Lapsana
289 // L. communis
290 // L. communis subsp. communis
291 // L. communis subsp. adenophora
292 // L. communis subsp. alpina
293 // Sonchella
294 // S. dentata
295 // S. stenoma
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);
301
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);
307
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);
313
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);
319
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);
325
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);
336
337 // --- Distributions --- //
338 // tdwg3 level YUG : Yugoslavia
339 // contains tdwg4 level areas :
340 // YUG-BH Bosnia-Herzegovina
341 // YUG-CR Croatia
342 // YUG-KO Kosovo
343 // YUG-MA Macedonia
344 // YUG-MN Montenegro
345
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
352 // );
353
354 commitAndStartNewTransaction(null);
355
356 writeDbUnitDataSetFile(new String[] {
357 "TAXONBASE", "TAXONNAMEBASE",
358 "REFERENCE", "DESCRIPTIONELEMENTBASE", "DESCRIPTIONBASE",
359 "AGENTBASE", "CLASSIFICATION", "TAXONNODE",
360 "HOMOTYPICALGROUP", "LANGUAGESTRING",
361 });
362
363 }
364
365 }