test for fallbackArea rule in DescriptionUtility.filterDistrubution for EuroPlusMed...
[cdmlib.git] / cdmlib-services / src / test / java / eu / etaxonomy / cdm / api / utility / DescriptionUtilityTest.java
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, subAreaPreference, statusOrderPreference, hideMarkedAreas, fallbackAreaMarkerType);
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, subAreaPreference, statusOrderPreference, hideMarkedAreas, fallbackAreaMarkerType);
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, subAreaPreference, statusOrderPreference, hideMarkedAreas, fallbackAreaMarkerType);
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, subAreaPreference, statusOrderPreference, hideMarkedAreas, fallbackAreaMarkerType);
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, subAreaPreference, statusOrderPreference, hideMarkedAreas, fallbackAreaMarkerType);
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, subAreaPreference, statusOrderPreference, hideMarkedAreas, fallbackAreaMarkerType);
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 Distribution distGermany = Distribution.NewInstance(Country.GERMANY(), PresenceAbsenceTerm.NATIVE());
151 Distribution distFrance = Distribution.NewInstance(Country.FRANCEFRENCHREPUBLIC(), PresenceAbsenceTerm.NATIVE());
152 Distribution distBelgium = Distribution.NewInstance(Country.BELGIUMKINGDOMOF(), PresenceAbsenceTerm.NATIVE());
153 distributions.add(distGermany);
154 distributions.add(distFrance);
155 distributions.add(distBelgium);
156
157 Country.BELGIUMKINGDOMOF().addMarker(Marker.NewInstance(MarkerType.TO_BE_CHECKED(), true));
158 Country.FRANCEFRENCHREPUBLIC().addMarker(Marker.NewInstance(MarkerType.IMPORTED(), true));
159
160 hideMarkedAreas = new HashSet<MarkerType>();
161 hideMarkedAreas.add(MarkerType.TO_BE_CHECKED());
162 hideMarkedAreas.add(MarkerType.IMPORTED());
163
164 filteredDistributions = DescriptionUtility.filterDistributions(distributions, subAreaPreference, statusOrderPreference, hideMarkedAreas, fallbackAreaMarkerType);
165 Assert.assertEquals(1, filteredDistributions.size());
166 Assert.assertEquals(Country.GERMANY(), filteredDistributions.iterator().next().getArea());
167 }
168
169 @Test
170 public void testFilterDistributions_fallbackAreaFilter(){
171
172 NamedArea jugoslavia = NamedArea.NewInstance("Former Yugoslavia ", "", "Ju");
173 jugoslavia.setIdInVocabulary("Ju");
174 NamedArea serbia = NamedArea.NewInstance("Serbia", "", "Sr");
175 serbia.setIdInVocabulary("Sr");
176 serbia.setPartOf(jugoslavia);
177
178 Distribution distJugoslavia = Distribution.NewInstance(jugoslavia, PresenceAbsenceTerm.NATIVE());
179 Distribution distSerbia = Distribution.NewInstance(serbia, PresenceAbsenceTerm.NATIVE());
180
181 distributions.add(distSerbia);
182 distributions.add(distJugoslavia);
183
184 // using TO_BE_CHECKED to mark Ju as fallback area
185 jugoslavia.addMarker(Marker.NewInstance(MarkerType.TO_BE_CHECKED(), true));
186
187 filteredDistributions = DescriptionUtility.filterDistributions(distributions,
188 subAreaPreference,
189 statusOrderPreference,
190 hideMarkedAreas,
191 MarkerType.TO_BE_CHECKED());
192
193 Assert.assertEquals(1, filteredDistributions.size());
194 Assert.assertEquals(serbia, filteredDistributions.iterator().next().getArea());
195 }
196
197
198
199 /* (non-Javadoc)
200 * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
201 */
202 @Override
203 public void createTestDataSet() throws FileNotFoundException {
204 // TODO Auto-generated method stub
205
206 }
207
208 }