1
|
/**
|
2
|
* Copyright (C) 2014 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.api.utility;
|
10
|
|
11
|
import java.io.FileNotFoundException;
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Collection;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.junit.Assert;
|
18
|
import org.junit.Before;
|
19
|
import org.junit.Test;
|
20
|
import org.unitils.spring.annotation.SpringBeanByType;
|
21
|
|
22
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
23
|
import eu.etaxonomy.cdm.model.common.Marker;
|
24
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
25
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
26
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
27
|
import eu.etaxonomy.cdm.model.location.Country;
|
28
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
29
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
30
|
|
31
|
/**
|
32
|
* @author a.kohlbecker
|
33
|
* @date Jan 27, 2014
|
34
|
*
|
35
|
*/
|
36
|
public class DescriptionUtilityTest extends CdmTransactionalIntegrationTest {
|
37
|
|
38
|
|
39
|
@SpringBeanByType
|
40
|
private ITermService termService;
|
41
|
|
42
|
Collection<Distribution> distributions = null;
|
43
|
Collection<Distribution> filteredDistributions = null;
|
44
|
boolean subAreaPreference = false;
|
45
|
boolean statusOrderPreference = false;
|
46
|
Set<MarkerType> hideMarkedAreas = null;
|
47
|
MarkerType fallbackAreaMarkerType = null;
|
48
|
NamedArea berlin = null;
|
49
|
|
50
|
@Before
|
51
|
public void setup(){
|
52
|
distributions = new ArrayList<Distribution>();
|
53
|
|
54
|
berlin = NamedArea.NewInstance("Berlin", "Berlin", "BER");
|
55
|
berlin.setPartOf(Country.GERMANY());
|
56
|
termService.saveOrUpdate(berlin);
|
57
|
}
|
58
|
|
59
|
@Test
|
60
|
public void testFilterDistributions_computed(){
|
61
|
|
62
|
/* 1.
|
63
|
* Computed elements are preferred over entered or imported elements.
|
64
|
* (Computed description elements are identified by the
|
65
|
* MarkerType.COMPUTED()). This means if a entered or imported status
|
66
|
* information exist for the same area for which computed data is
|
67
|
* available, the computed data has to be given preference over other
|
68
|
* data.
|
69
|
*/
|
70
|
distributions.add(Distribution.NewInstance(Country.GERMANY(), PresenceAbsenceTerm.NATIVE()));
|
71
|
|
72
|
Distribution computedDistribution = Distribution.NewInstance(Country.GERMANY(), PresenceAbsenceTerm.INTRODUCED());
|
73
|
computedDistribution.addMarker(Marker.NewInstance(MarkerType.COMPUTED(), true));
|
74
|
distributions.add(computedDistribution);
|
75
|
|
76
|
statusOrderPreference= true;
|
77
|
filteredDistributions = DescriptionUtility.filterDistributions(distributions, hideMarkedAreas, true, statusOrderPreference, subAreaPreference);
|
78
|
Assert.assertEquals(1, filteredDistributions.size());
|
79
|
Assert.assertEquals("expecting to see computed status INTRODUCED even it has lower preference than NATIVE", PresenceAbsenceTerm.INTRODUCED(), filteredDistributions.iterator().next().getStatus());
|
80
|
|
81
|
/* distributions for parent areas are only
|
82
|
* removed if direct sub areas have the same status and if subAreaPreference=TRUE which is not the case here
|
83
|
*/
|
84
|
Distribution parentComputedDistribution = Distribution.NewInstance(berlin, PresenceAbsenceTerm.INTRODUCED());
|
85
|
parentComputedDistribution.addMarker(Marker.NewInstance(MarkerType.COMPUTED(), true));
|
86
|
distributions.add(parentComputedDistribution);
|
87
|
|
88
|
filteredDistributions = DescriptionUtility.filterDistributions(distributions, hideMarkedAreas, true, statusOrderPreference, subAreaPreference);
|
89
|
Assert.assertEquals(2, filteredDistributions.size());
|
90
|
|
91
|
}
|
92
|
|
93
|
@Test
|
94
|
public void testFilterDistributions_statusOrderPreference(){
|
95
|
statusOrderPreference = true;
|
96
|
|
97
|
/*
|
98
|
* Status order preference rule: In case of multiple distribution status
|
99
|
* (PresenceAbsenceTermBase) for the same area the status with the
|
100
|
* highest order is preferred, see
|
101
|
* OrderedTermBase.compareTo(OrderedTermBase)
|
102
|
*/
|
103
|
distributions.add(Distribution.NewInstance(Country.GERMANY(), PresenceAbsenceTerm.NATIVE()));
|
104
|
distributions.add(Distribution.NewInstance(Country.GERMANY(), PresenceAbsenceTerm.INTRODUCED()));
|
105
|
filteredDistributions = DescriptionUtility.filterDistributions(distributions, hideMarkedAreas, true, statusOrderPreference, subAreaPreference);
|
106
|
Assert.assertEquals(1, filteredDistributions.size());
|
107
|
Assert.assertEquals(PresenceAbsenceTerm.NATIVE(), filteredDistributions.iterator().next().getStatus());
|
108
|
}
|
109
|
|
110
|
|
111
|
@Test
|
112
|
public void testFilterDistributions_subAreaPreference(){
|
113
|
subAreaPreference = true;
|
114
|
|
115
|
/*
|
116
|
* Sub area preference rule: If there is an area with a direct sub area
|
117
|
* and both areas have the same status only the information on
|
118
|
* the sub area should be reported, whereas the super area should be
|
119
|
* ignored.
|
120
|
*/
|
121
|
Distribution distGermany = Distribution.NewInstance(Country.GERMANY(), PresenceAbsenceTerm.NATIVE());
|
122
|
Distribution distBerlin = Distribution.NewInstance(berlin, PresenceAbsenceTerm.NATIVE());
|
123
|
|
124
|
// no computed data
|
125
|
distributions.add(distGermany);
|
126
|
distributions.add(distBerlin);
|
127
|
filteredDistributions = DescriptionUtility.filterDistributions(distributions, hideMarkedAreas, true, statusOrderPreference, subAreaPreference);
|
128
|
Assert.assertEquals(1, filteredDistributions.size());
|
129
|
Assert.assertEquals(berlin, filteredDistributions.iterator().next().getArea());
|
130
|
|
131
|
// mixed situation
|
132
|
distGermany.addMarker(Marker.NewInstance(MarkerType.COMPUTED(), true));
|
133
|
filteredDistributions = DescriptionUtility.filterDistributions(distributions, hideMarkedAreas, true, statusOrderPreference, subAreaPreference);
|
134
|
Assert.assertEquals(1, filteredDistributions.size());
|
135
|
Assert.assertEquals(berlin, filteredDistributions.iterator().next().getArea());
|
136
|
|
137
|
// all computed
|
138
|
distBerlin.addMarker(Marker.NewInstance(MarkerType.COMPUTED(), true));
|
139
|
filteredDistributions = DescriptionUtility.filterDistributions(distributions, hideMarkedAreas, true, statusOrderPreference, subAreaPreference);
|
140
|
Assert.assertEquals(1, filteredDistributions.size());
|
141
|
Assert.assertEquals(berlin, filteredDistributions.iterator().next().getArea());
|
142
|
}
|
143
|
|
144
|
@Test
|
145
|
public void testFilterDistributions_markedAreaFilter(){
|
146
|
/*
|
147
|
* Marked area filter: Skip distributions where the area has a Marker
|
148
|
* with one of the specified MarkerTypes
|
149
|
*/
|
150
|
NamedArea germany = NamedArea.NewInstance("Germany", "Germany", "GER");
|
151
|
NamedArea france = NamedArea.NewInstance("France", "France", "FRA");
|
152
|
NamedArea belgium = NamedArea.NewInstance("Belgium", "Belgium", "BEL");
|
153
|
|
154
|
Distribution distGermany = Distribution.NewInstance(germany, PresenceAbsenceTerm.NATIVE());
|
155
|
Distribution distFrance = Distribution.NewInstance(france, PresenceAbsenceTerm.NATIVE());
|
156
|
Distribution distBelgium = Distribution.NewInstance(belgium, PresenceAbsenceTerm.NATIVE());
|
157
|
distributions.add(distGermany);
|
158
|
distributions.add(distFrance);
|
159
|
distributions.add(distBelgium);
|
160
|
|
161
|
belgium.addMarker(Marker.NewInstance(MarkerType.TO_BE_CHECKED(), true));
|
162
|
france.addMarker(Marker.NewInstance(MarkerType.IMPORTED(), true));
|
163
|
|
164
|
hideMarkedAreas = new HashSet<MarkerType>();
|
165
|
hideMarkedAreas.add(MarkerType.TO_BE_CHECKED());
|
166
|
hideMarkedAreas.add(MarkerType.IMPORTED());
|
167
|
|
168
|
filteredDistributions = DescriptionUtility.filterDistributions(distributions, hideMarkedAreas, true, statusOrderPreference, subAreaPreference);
|
169
|
Assert.assertEquals(1, filteredDistributions.size());
|
170
|
Assert.assertEquals(germany, filteredDistributions.iterator().next().getArea());
|
171
|
}
|
172
|
|
173
|
@Test
|
174
|
public void testFilterDistributions_fallbackArea_hidden(){
|
175
|
|
176
|
NamedArea jugoslavia = NamedArea.NewInstance("Former Yugoslavia ", "", "Ju");
|
177
|
jugoslavia.setIdInVocabulary("Ju");
|
178
|
NamedArea serbia = NamedArea.NewInstance("Serbia", "", "Sr");
|
179
|
serbia.setIdInVocabulary("Sr");
|
180
|
jugoslavia.addIncludes(serbia);
|
181
|
|
182
|
Distribution distJugoslavia = Distribution.NewInstance(jugoslavia, PresenceAbsenceTerm.NATIVE());
|
183
|
Distribution distSerbia = Distribution.NewInstance(serbia, PresenceAbsenceTerm.NATIVE());
|
184
|
|
185
|
distributions.add(distSerbia);
|
186
|
distributions.add(distJugoslavia);
|
187
|
|
188
|
// using TO_BE_CHECKED to mark Ju as fallback area
|
189
|
jugoslavia.addMarker(Marker.NewInstance(MarkerType.TO_BE_CHECKED(), true));
|
190
|
|
191
|
hideMarkedAreas = new HashSet<MarkerType>();
|
192
|
hideMarkedAreas.add(MarkerType.TO_BE_CHECKED());
|
193
|
|
194
|
filteredDistributions = DescriptionUtility.filterDistributions(
|
195
|
distributions,
|
196
|
hideMarkedAreas,
|
197
|
true,
|
198
|
statusOrderPreference,
|
199
|
subAreaPreference);
|
200
|
|
201
|
Assert.assertEquals(1, filteredDistributions.size());
|
202
|
Assert.assertEquals(serbia, filteredDistributions.iterator().next().getArea());
|
203
|
}
|
204
|
|
205
|
@Test
|
206
|
public void testFilterDistributions_fallbackArea_shown_1(){
|
207
|
|
208
|
NamedArea jugoslavia = NamedArea.NewInstance("Former Yugoslavia ", "", "Ju");
|
209
|
jugoslavia.setIdInVocabulary("Ju");
|
210
|
NamedArea serbia = NamedArea.NewInstance("Serbia", "", "Sr");
|
211
|
serbia.setIdInVocabulary("Sr");
|
212
|
jugoslavia.addIncludes(serbia);
|
213
|
|
214
|
Distribution distJugoslavia = Distribution.NewInstance(jugoslavia, PresenceAbsenceTerm.NATIVE());
|
215
|
Distribution distSerbia = Distribution.NewInstance(serbia, PresenceAbsenceTerm.NATIVE());
|
216
|
|
217
|
distributions.add(distSerbia);
|
218
|
distributions.add(distJugoslavia);
|
219
|
|
220
|
// using TO_BE_CHECKED to mark Ju as fallback area
|
221
|
jugoslavia.addMarker(Marker.NewInstance(MarkerType.TO_BE_CHECKED(), true));
|
222
|
// this hides serbia so jugoslavia should be shown
|
223
|
serbia.addMarker(Marker.NewInstance(MarkerType.TO_BE_CHECKED(), true));
|
224
|
|
225
|
hideMarkedAreas = new HashSet<MarkerType>();
|
226
|
hideMarkedAreas.add(MarkerType.TO_BE_CHECKED());
|
227
|
|
228
|
filteredDistributions = DescriptionUtility.filterDistributions(
|
229
|
distributions,
|
230
|
hideMarkedAreas,
|
231
|
true,
|
232
|
statusOrderPreference,
|
233
|
subAreaPreference);
|
234
|
|
235
|
Assert.assertEquals(1, filteredDistributions.size());
|
236
|
Assert.assertEquals(jugoslavia, filteredDistributions.iterator().next().getArea());
|
237
|
}
|
238
|
|
239
|
@Test
|
240
|
public void testFilterDistributions_fallbackArea_shown_2(){
|
241
|
|
242
|
NamedArea jugoslavia = NamedArea.NewInstance("Former Yugoslavia ", "", "Ju");
|
243
|
jugoslavia.setIdInVocabulary("Ju");
|
244
|
NamedArea serbia = NamedArea.NewInstance("Serbia", "", "Sr");
|
245
|
serbia.setIdInVocabulary("Sr");
|
246
|
jugoslavia.addIncludes(serbia);
|
247
|
|
248
|
Distribution distJugoslavia = Distribution.NewInstance(jugoslavia, PresenceAbsenceTerm.NATIVE());
|
249
|
distributions.add(distJugoslavia);
|
250
|
// no Distribution for any of the sub areas of jugoslavia, so it should be shown
|
251
|
|
252
|
// using TO_BE_CHECKED to mark Ju as fallback area
|
253
|
jugoslavia.addMarker(Marker.NewInstance(MarkerType.TO_BE_CHECKED(), true));
|
254
|
|
255
|
hideMarkedAreas = new HashSet<MarkerType>();
|
256
|
hideMarkedAreas.add(MarkerType.TO_BE_CHECKED());
|
257
|
|
258
|
filteredDistributions = DescriptionUtility.filterDistributions(
|
259
|
distributions,
|
260
|
hideMarkedAreas,
|
261
|
true,
|
262
|
statusOrderPreference,
|
263
|
subAreaPreference);
|
264
|
|
265
|
Assert.assertEquals(1, filteredDistributions.size());
|
266
|
Assert.assertEquals(jugoslavia, filteredDistributions.iterator().next().getArea());
|
267
|
}
|
268
|
|
269
|
|
270
|
|
271
|
/* (non-Javadoc)
|
272
|
* @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
|
273
|
*/
|
274
|
@Override
|
275
|
public void createTestDataSet() throws FileNotFoundException {
|
276
|
// TODO Auto-generated method stub
|
277
|
|
278
|
}
|
279
|
|
280
|
}
|