Project

General

Profile

Download (13.4 KB) Statistics
| Branch: | Tag: | Revision:
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.DescriptionType;
26
import eu.etaxonomy.cdm.model.description.Distribution;
27
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
28
import eu.etaxonomy.cdm.model.description.TaxonDescription;
29
import eu.etaxonomy.cdm.model.location.Country;
30
import eu.etaxonomy.cdm.model.location.NamedArea;
31
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
32

    
33
/**
34
 * @author a.kohlbecker
35
 * @since Jan 27, 2014
36
 */
37
public class DescriptionUtilityTest extends CdmTransactionalIntegrationTest {
38

    
39
    @SpringBeanByType
40
    private ITermService termService;
41

    
42
    private Collection<Distribution> distributions = null;
43
    private Collection<Distribution> filteredDistributions = null;
44
    private boolean subAreaPreference = false;
45
    private boolean statusOrderPreference = false;
46
    private Set<MarkerType> hideMarkedAreas = null;
47
    private NamedArea berlin = null;
48

    
49
    @Before
50
    public void setup(){
51
        distributions = new ArrayList<>();
52

    
53
        berlin = NamedArea.NewInstance("Berlin", "Berlin", "BER");
54
        berlin.setPartOf(Country.GERMANY());
55
        termService.saveOrUpdate(berlin);
56
    }
57

    
58
    @Test
59
    public void testFilterDistributions_computed(){
60

    
61
        /* 1.
62
         * Computed elements are preferred over entered or imported elements.
63
         * (Computed description elements are identified by belonging to descriptions
64
         * which have the type DescriptionType#AGGREGATED_DISTRIBUTION).
65
         * This means if a entered or imported status information exist for the same
66
         * area for which computed data is available, the computed data has to be
67
         * given preference over other data.
68
         * Note by AM: be aware that according to #5050 the preference of computed
69
         * distributions is not valid anymore (for the E+M usecase). However, the functionality
70
         * might be interesting for future use-cases.
71
         */
72
        TaxonDescription aggregatedDescription = TaxonDescription.NewInstance();
73
        aggregatedDescription.addType(DescriptionType.AGGREGATED_DISTRIBUTION);
74

    
75
        Distribution germanyNative = Distribution.NewInstance(Country.GERMANY(), PresenceAbsenceTerm.NATIVE());
76
        distributions.add(germanyNative);
77

    
78
        Distribution germanyIntroduced = Distribution.NewInstance(Country.GERMANY(), PresenceAbsenceTerm.INTRODUCED());
79
        aggregatedDescription.addElement(germanyIntroduced);
80

    
81
        distributions.add(germanyIntroduced);
82

    
83
        statusOrderPreference= true;
84
        boolean preferComputed = true;
85
        filteredDistributions = DescriptionUtility.filterDistributions(distributions, hideMarkedAreas, preferComputed, statusOrderPreference, subAreaPreference);
86
        Assert.assertEquals(1, filteredDistributions.size());
87
        Assert.assertEquals("expecting to see computed status INTRODUCED even it has lower preference than NATIVE", PresenceAbsenceTerm.INTRODUCED(), filteredDistributions.iterator().next().getStatus());
88

    
89
       /* distributions for parent areas are only
90
        * removed if direct sub areas have the same status and if subAreaPreference=TRUE which is not the case here
91
        */
92
        TaxonDescription aggParentDescription = TaxonDescription.NewInstance();
93
        aggParentDescription.addType(DescriptionType.AGGREGATED_DISTRIBUTION);
94

    
95
        Distribution parentComputedDistribution = Distribution.NewInstance(berlin, PresenceAbsenceTerm.INTRODUCED());
96
        aggParentDescription.addElement(parentComputedDistribution);
97
        distributions.add(parentComputedDistribution);
98

    
99
        filteredDistributions = DescriptionUtility.filterDistributions(distributions, hideMarkedAreas, preferComputed, statusOrderPreference, subAreaPreference);
100
        Assert.assertEquals(2, filteredDistributions.size());
101
    }
102

    
103
    @Test
104
    public void testFilterDistributions_statusOrderPreference(){
105
        statusOrderPreference = true;
106

    
107
        /*
108
         * Status order preference rule: In case of multiple distribution status
109
         * (PresenceAbsenceTermBase) for the same area the status with the
110
         * highest order is preferred, see
111
         * OrderedTermBase.compareTo(OrderedTermBase)
112
         */
113
        distributions.add(Distribution.NewInstance(Country.GERMANY(), PresenceAbsenceTerm.NATIVE()));
114
        distributions.add(Distribution.NewInstance(Country.GERMANY(), PresenceAbsenceTerm.INTRODUCED()));
115
        filteredDistributions = DescriptionUtility.filterDistributions(distributions, hideMarkedAreas, true, statusOrderPreference, subAreaPreference);
116
        Assert.assertEquals(1, filteredDistributions.size());
117
        Assert.assertEquals(PresenceAbsenceTerm.NATIVE(), filteredDistributions.iterator().next().getStatus());
118
    }
119

    
120
    @Test
121
    public void testFilterDistributions_subAreaPreference(){
122
        subAreaPreference = true;
123
        boolean preferComputed = true;
124

    
125
        /*
126
         * Sub area preference rule: If there is an area with a direct sub area
127
         * and both areas have the same status only the information on
128
         * the sub area should be reported, whereas the super area should be
129
         * ignored.
130
         * TODO Note by AM  to me this test is unclear, there seems to be no difference between
131
         * "no", "mixed" and "all". From what I saw in the code the "preferComputed" rule
132
         * works only on the exact same area so as we use Germany versus Berlin here it may not
133
         * have any influence and the last 2 tests could be deleted.
134
         * NOTE2: From now on the marker computed on distributions has no effect anymore.
135
         * Computed (or better Aggregated_Distribution) can only be defined on description
136
         * level not on description element level. But this change had no effect on this test
137
         * so also from this perspective the 2 "Computed" tests can be deleted.
138
         */
139
        Distribution distGermany = Distribution.NewInstance(Country.GERMANY(), PresenceAbsenceTerm.NATIVE());
140
        Distribution distBerlin = Distribution.NewInstance(berlin, PresenceAbsenceTerm.NATIVE());
141

    
142
        // no computed data
143
        distributions.add(distGermany);
144
        distributions.add(distBerlin);
145
        filteredDistributions = DescriptionUtility.filterDistributions(distributions,
146
                hideMarkedAreas, preferComputed, statusOrderPreference, subAreaPreference);
147
        Assert.assertEquals(1, filteredDistributions.size());
148
        Assert.assertEquals(berlin, filteredDistributions.iterator().next().getArea());
149

    
150
        // mixed situation
151
        distGermany.addMarker(Marker.NewInstance(MarkerType.COMPUTED(), true));
152
        filteredDistributions = DescriptionUtility.filterDistributions(distributions,
153
                hideMarkedAreas, preferComputed, statusOrderPreference, subAreaPreference);
154
        Assert.assertEquals(1, filteredDistributions.size());
155
        Assert.assertEquals(berlin, filteredDistributions.iterator().next().getArea());
156

    
157
        // all computed
158
        distBerlin.addMarker(Marker.NewInstance(MarkerType.COMPUTED(), true));
159
        filteredDistributions = DescriptionUtility.filterDistributions(distributions,
160
                hideMarkedAreas, preferComputed, statusOrderPreference, subAreaPreference);
161
        Assert.assertEquals(1, filteredDistributions.size());
162
        Assert.assertEquals(berlin, filteredDistributions.iterator().next().getArea());
163
    }
164

    
165
    @Test
166
    public void testFilterDistributions_markedAreaFilter(){
167
        /*
168
         * Marked area filter: Skip distributions where the area has a Marker
169
         * with one of the specified MarkerTypes
170
         */
171
        NamedArea germany = NamedArea.NewInstance("Germany", "Germany", "GER");
172
        NamedArea france = NamedArea.NewInstance("France", "France", "FRA");
173
        NamedArea belgium = NamedArea.NewInstance("Belgium", "Belgium", "BEL");
174

    
175
        Distribution distGermany = Distribution.NewInstance(germany, PresenceAbsenceTerm.NATIVE());
176
        Distribution distFrance = Distribution.NewInstance(france, PresenceAbsenceTerm.NATIVE());
177
        Distribution distBelgium = Distribution.NewInstance(belgium, PresenceAbsenceTerm.NATIVE());
178
        distributions.add(distGermany);
179
        distributions.add(distFrance);
180
        distributions.add(distBelgium);
181

    
182
        belgium.addMarker(Marker.NewInstance(MarkerType.TO_BE_CHECKED(), true));
183
        france.addMarker(Marker.NewInstance(MarkerType.IMPORTED(), true));
184

    
185
        hideMarkedAreas = new HashSet<>();
186
        hideMarkedAreas.add(MarkerType.TO_BE_CHECKED());
187
        hideMarkedAreas.add(MarkerType.IMPORTED());
188

    
189
        filteredDistributions = DescriptionUtility.filterDistributions(distributions, hideMarkedAreas, true, statusOrderPreference, subAreaPreference);
190
        Assert.assertEquals(1, filteredDistributions.size());
191
        Assert.assertEquals(germany, filteredDistributions.iterator().next().getArea());
192
    }
193

    
194
    @Test
195
    public void testFilterDistributions_fallbackArea_hidden(){
196

    
197
        boolean preferComputed = true;
198

    
199
        NamedArea jugoslavia = NamedArea.NewInstance("Former Yugoslavia ", "", "Ju");
200
        jugoslavia.setIdInVocabulary("Ju");
201
        NamedArea serbia = NamedArea.NewInstance("Serbia", "", "Sr");
202
        serbia.setIdInVocabulary("Sr");
203
        jugoslavia.addIncludes(serbia);
204

    
205
        Distribution distJugoslavia = Distribution.NewInstance(jugoslavia, PresenceAbsenceTerm.NATIVE());
206
        Distribution distSerbia = Distribution.NewInstance(serbia, PresenceAbsenceTerm.NATIVE());
207

    
208
        distributions.add(distSerbia);
209
        distributions.add(distJugoslavia);
210

    
211
        // using TO_BE_CHECKED to mark Ju as fallback area
212
        jugoslavia.addMarker(Marker.NewInstance(MarkerType.TO_BE_CHECKED(), true));
213

    
214
        hideMarkedAreas = new HashSet<>();
215
        hideMarkedAreas.add(MarkerType.TO_BE_CHECKED());
216

    
217
        filteredDistributions = DescriptionUtility.filterDistributions(
218
                distributions,
219
                hideMarkedAreas,
220
                preferComputed,
221
                statusOrderPreference,
222
                subAreaPreference);
223

    
224
        Assert.assertEquals(1, filteredDistributions.size());
225
        Assert.assertEquals(serbia, filteredDistributions.iterator().next().getArea());
226
    }
227

    
228
    @Test
229
    public void testFilterDistributions_fallbackArea_shown_1(){
230

    
231
        boolean preferComputed = true;
232

    
233
        NamedArea jugoslavia = NamedArea.NewInstance("Former Yugoslavia ", "", "Ju");
234
        jugoslavia.setIdInVocabulary("Ju");
235
        NamedArea serbia = NamedArea.NewInstance("Serbia", "", "Sr");
236
        serbia.setIdInVocabulary("Sr");
237
        jugoslavia.addIncludes(serbia);
238

    
239
        Distribution distJugoslavia = Distribution.NewInstance(jugoslavia, PresenceAbsenceTerm.NATIVE());
240
        Distribution distSerbia = Distribution.NewInstance(serbia, PresenceAbsenceTerm.NATIVE());
241

    
242
        distributions.add(distSerbia);
243
        distributions.add(distJugoslavia);
244

    
245
        // using TO_BE_CHECKED to mark Ju as fallback area
246
        jugoslavia.addMarker(Marker.NewInstance(MarkerType.TO_BE_CHECKED(), true));
247
        // this hides serbia so jugoslavia should be shown
248
        serbia.addMarker(Marker.NewInstance(MarkerType.TO_BE_CHECKED(), true));
249

    
250
        hideMarkedAreas = new HashSet<>();
251
        hideMarkedAreas.add(MarkerType.TO_BE_CHECKED());
252

    
253
        filteredDistributions = DescriptionUtility.filterDistributions(
254
                distributions,
255
                hideMarkedAreas,
256
                preferComputed,
257
                statusOrderPreference,
258
                subAreaPreference);
259

    
260
        Assert.assertEquals(1, filteredDistributions.size());
261
        Assert.assertEquals(jugoslavia, filteredDistributions.iterator().next().getArea());
262
    }
263

    
264
    @Test
265
    public void testFilterDistributions_fallbackArea_shown_2(){
266

    
267
        NamedArea jugoslavia = NamedArea.NewInstance("Former Yugoslavia ", "", "Ju");
268
        jugoslavia.setIdInVocabulary("Ju");
269
        NamedArea serbia = NamedArea.NewInstance("Serbia", "", "Sr");
270
        serbia.setIdInVocabulary("Sr");
271
        jugoslavia.addIncludes(serbia);
272

    
273
        Distribution distJugoslavia = Distribution.NewInstance(jugoslavia, PresenceAbsenceTerm.NATIVE());
274
        distributions.add(distJugoslavia);
275
        // no Distribution for any of the sub areas of jugoslavia, so it should be shown
276

    
277
        // using TO_BE_CHECKED to mark Ju as fallback area
278
        jugoslavia.addMarker(Marker.NewInstance(MarkerType.TO_BE_CHECKED(), true));
279

    
280
        hideMarkedAreas = new HashSet<>();
281
        hideMarkedAreas.add(MarkerType.TO_BE_CHECKED());
282

    
283
        filteredDistributions = DescriptionUtility.filterDistributions(
284
                distributions,
285
                hideMarkedAreas,
286
                true,
287
                statusOrderPreference,
288
                subAreaPreference);
289

    
290
        Assert.assertEquals(1, filteredDistributions.size());
291
        Assert.assertEquals(jugoslavia, filteredDistributions.iterator().next().getArea());
292
    }
293

    
294
    @Override
295
    public void createTestDataSet() throws FileNotFoundException {}
296

    
297
}
(3-3/3)