rename getByIdInVocabulary
[cdmlib.git] / cdmlib-ext / src / test / java / eu / etaxonomy / cdm / ext / geo / EditGeoServiceTest.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 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
11 package eu.etaxonomy.cdm.ext.geo;
12
13 import static org.junit.Assert.assertTrue;
14
15 import java.awt.Color;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.InputStreamReader;
19 import java.net.HttpURLConnection;
20 import java.net.MalformedURLException;
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import java.net.URL;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.UUID;
31
32 import org.apache.commons.lang.StringUtils;
33 import org.apache.http.client.ClientProtocolException;
34 import org.apache.log4j.Logger;
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.unitils.spring.annotation.SpringBeanByType;
39
40 import eu.etaxonomy.cdm.api.service.ITermService;
41 import eu.etaxonomy.cdm.api.service.IVocabularyService;
42 import eu.etaxonomy.cdm.common.StreamUtils;
43 import eu.etaxonomy.cdm.common.UriUtils;
44 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
45 import eu.etaxonomy.cdm.model.common.Language;
46 import eu.etaxonomy.cdm.model.common.TermType;
47 import eu.etaxonomy.cdm.model.common.TermVocabulary;
48 import eu.etaxonomy.cdm.model.description.AbsenceTerm;
49 import eu.etaxonomy.cdm.model.description.Distribution;
50 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
51 import eu.etaxonomy.cdm.model.description.PresenceTerm;
52 import eu.etaxonomy.cdm.model.location.Country;
53 import eu.etaxonomy.cdm.model.location.NamedArea;
54 import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
55 import eu.etaxonomy.cdm.model.location.NamedAreaType;
56 import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
57 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
58
59 /**
60 * @author a.mueller
61 * @created 08.10.2008
62 */
63 public class EditGeoServiceTest extends CdmTransactionalIntegrationTest {
64 private static final Logger logger = Logger.getLogger(EditGeoServiceTest.class);
65
66 private static final String EDIT_MAPSERVICE_URI_STING = "http://edit.africamuseum.be/edit_wp5/v1.2/rest_gen.php";
67 private static URI editMapServiceUri;
68
69 //@SpringBeanByType
70 private IDefinedTermDao termDao;
71
72 @SpringBeanByType
73 private ITermService termService;
74
75 @SpringBeanByType
76 private IVocabularyService vocabService;
77
78 @SpringBeanByType
79 private GeoServiceAreaAnnotatedMapping mapping;
80
81 @SpringBeanByType
82 private IEditGeoService editGeoService;
83 //
84 // /**
85 // * @throws java.lang.Exception
86 // */
87 // @BeforeClass
88 // public static void setUpBeforeClass() throws Exception {
89 // DefaultTermInitializer initializer = new DefaultTermInitializer();
90 // initializer.initialize();
91 // }
92
93 /**
94 * @throws java.lang.Exception
95 */
96 @Before
97 public void setUp() throws Exception {
98 EditGeoServiceUtilities.setTermDao(termDao);
99 System.setProperty("ONLY-A-TEST", "TRUE"); // allows EditGeoServiceUtilities to skip some line of code
100 editMapServiceUri = new URI(EDIT_MAPSERVICE_URI_STING);
101 }
102
103
104 //******************************************** TESTS**************
105
106 @Test
107 public void testGetWebServiceUrlCountry() throws MalformedURLException, IOException {
108 Set<Distribution> distributions = new HashSet<Distribution>();
109 Country germany = termService.findByIdInVocabulary("DEU", Country.uuidCountryVocabulary, Country.class);
110 // germany = (Country)termService.find(665);
111 // germany = (Country)termService.find(UUID.fromString("cbe7ce69-2952-4309-85dd-0d7d4a4830a1"));
112
113 // germany = Country.GERMANY();
114
115 distributions.add(Distribution.NewInstance(germany, PresenceTerm.PRESENT()));
116 distributions.add(Distribution.NewInstance(termService.findByIdInVocabulary("DE", Country.uuidCountryVocabulary, Country.class), PresenceTerm.INTRODUCED()));
117 Map<PresenceAbsenceTermBase<?>, Color> presenceAbsenceColorMap = new HashMap<PresenceAbsenceTermBase<?>, Color>();
118 presenceAbsenceColorMap.put(PresenceTerm.PRESENT(), Color.BLUE);
119 presenceAbsenceColorMap.put(PresenceTerm.INTRODUCED(), Color.BLACK);
120 List<Language> languages = new ArrayList<Language>();
121
122 boolean subAreaPreference = false;
123 boolean statusOrderPreference = false;
124 String result = EditGeoServiceUtilities.getDistributionServiceRequestParameterString(distributions,
125 subAreaPreference, statusOrderPreference, null, mapping,
126 presenceAbsenceColorMap, null, languages );
127 logger.warn(result);
128 Assert.assertTrue("WebServiceUrl must contain country part for Germany", result.matches(".*ad=country_earth(%3A|:)gmi_cntry:a:DEU.*"));
129
130 }
131
132 @Test
133 public void testGetWebServiceUrlTdwg() throws MalformedURLException, IOException {
134 //String webServiceUrl = "http://www.test.de/webservice";
135 Set<Distribution> distributions = new HashSet<Distribution>();
136 distributions.add(Distribution.NewInstance(termService.getAreaByTdwgAbbreviation("SPA"), PresenceTerm.PRESENT()));
137 distributions.add(Distribution.NewInstance(termService.getAreaByTdwgAbbreviation("GER"), PresenceTerm.INTRODUCED()));
138 distributions.add(Distribution.NewInstance(termService.getAreaByTdwgAbbreviation("14"), PresenceTerm.CULTIVATED()));
139 distributions.add(Distribution.NewInstance(termService.getAreaByTdwgAbbreviation("BGM"), AbsenceTerm.ABSENT()));
140 distributions.add(Distribution.NewInstance(termService.getAreaByTdwgAbbreviation("FRA"), AbsenceTerm.ABSENT()));
141 distributions.add(Distribution.NewInstance(termService.getAreaByTdwgAbbreviation("IND-AP"), PresenceTerm.PRESENT()));
142
143 Map<PresenceAbsenceTermBase<?>, Color> presenceAbsenceColorMap = new HashMap<PresenceAbsenceTermBase<?>, Color>();
144 presenceAbsenceColorMap.put(PresenceTerm.PRESENT(), Color.BLUE);
145 presenceAbsenceColorMap.put(PresenceTerm.INTRODUCED(), Color.BLACK);
146 presenceAbsenceColorMap.put(PresenceTerm.CULTIVATED(), Color.YELLOW);
147 presenceAbsenceColorMap.put(AbsenceTerm.ABSENT(), Color.DARK_GRAY);
148 String backLayer ="";
149 presenceAbsenceColorMap = null;
150 String bbox="-20,0,120,70";
151 List<Language> languages = new ArrayList<Language>();
152
153 boolean subAreaPreference = false;
154 boolean statusOrderPreference = false;
155 String result = EditGeoServiceUtilities.getDistributionServiceRequestParameterString(distributions,
156 subAreaPreference ,
157 statusOrderPreference,
158 null, mapping, presenceAbsenceColorMap, null, languages );
159 //TODO Set semantics is not determined
160 //String expected = "http://www.test.de/webservice?l=tdwg3&ad=tdwg3:a:GER|b:OKL|c:BGM|b:SPA|d:FRA&as=a:005500|b:00FF00|c:FFFFFF|d:001100&bbox=-20,40,40,40&ms=400x300";
161 logger.debug(result);
162 assertTrue(result.matches(".*ad=tdwg[1-4].*"));
163 assertTrue(result.matches(".*tdwg2:[a-d]:14[\\|&].*") );
164 assertTrue(result.matches(".*[a-d]:FRA,BGM[\\|&].*") || result.matches(".*[a-d]:BGM,FRA[\\|&].*") );
165 assertTrue(result.matches(".*[a-d]:GER[\\|&].*") );
166 assertTrue(result.matches(".*[a-d]:SPA[\\|&].*") );
167 assertTrue(result.matches(".*tdwg4:[a-d]:INDAP[\\|&].*") );
168 //assertTrue(result.matches("0000ff"));
169 //TODO continue
170
171 // request map image from webservice
172 subTestWithEditMapService(result);
173 }
174
175 @Test
176 public void testGetWebServiceUrlCyprus() throws ClientProtocolException, IOException, URISyntaxException {
177 makeCyprusAreas();
178 Set<Distribution> distributions = new HashSet<Distribution>();
179 distributions.add(Distribution.NewInstance(divisions.get("1"), PresenceTerm.PRESENT()));
180 distributions.add(Distribution.NewInstance(divisions.get("2"), PresenceTerm.INTRODUCED()));
181 distributions.add(Distribution.NewInstance(divisions.get("3"), PresenceTerm.CULTIVATED()));
182 distributions.add(Distribution.NewInstance(divisions.get("4"), AbsenceTerm.ABSENT()));
183 distributions.add(Distribution.NewInstance(divisions.get("5"), AbsenceTerm.ABSENT()));
184 distributions.add(Distribution.NewInstance(divisions.get("6"), PresenceTerm.PRESENT()));
185
186 Map<PresenceAbsenceTermBase<?>, Color> presenceAbsenceColorMap = new HashMap<PresenceAbsenceTermBase<?>, Color>();
187 presenceAbsenceColorMap.put(PresenceTerm.PRESENT(), Color.BLUE);
188 presenceAbsenceColorMap.put(PresenceTerm.INTRODUCED(), Color.BLACK);
189 presenceAbsenceColorMap.put(PresenceTerm.CULTIVATED(), Color.YELLOW);
190 presenceAbsenceColorMap.put(AbsenceTerm.ABSENT(), Color.DARK_GRAY);
191 String backLayer ="";
192 presenceAbsenceColorMap = null;
193 String bbox="-20,0,120,70";
194 List<Language> languages = new ArrayList<Language>();
195
196 boolean subAreaPreference = false;
197 boolean statusOrderPreference = false;
198 String result = EditGeoServiceUtilities.getDistributionServiceRequestParameterString(distributions,
199 subAreaPreference ,
200 statusOrderPreference,
201 null, mapping, presenceAbsenceColorMap, null, languages );
202 //TODO Set semantics is not determined
203 //String expected = "http://www.test.de/webservice?l=tdwg3&ad=tdwg3:a:GER|b:OKL|c:BGM|b:SPA|d:FRA&as=a:005500|b:00FF00|c:FFFFFF|d:001100&bbox=-20,40,40,40&ms=400x300";
204 assertTrue(result.matches(".*ad=cyprusdivs%3Abdcode:.*"));
205 assertTrue(result.matches(".*[a-d]:5,4[\\|&].*") || result.matches(".*[a-d]:4,5[\\|&].*") );
206 assertTrue(result.matches(".*[a-d]:1,6[\\|&].*") || result.matches(".*[a-d]:6,1[\\|&].*") );
207 assertTrue(result.matches(".*[a-d]:2[\\|&].*") );
208 assertTrue(result.matches(".*[a-d]:3[\\|&].*") );
209
210 // request map image from webservice
211 subTestWithEditMapService(result);
212 }
213
214 private void subTestWithEditMapService(String queryString)throws MalformedURLException, IOException {
215 if(UriUtils.isServiceAvailable(editMapServiceUri)){
216 URL requestUrl = new URL(editMapServiceUri.toString() + "?img=false&bbox=-180,-90,180,90&ms=1000&" + queryString);
217 logger.debug("editMapServiceUri: " + requestUrl);
218 HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
219 connection.connect();
220 assertTrue(connection.getResponseCode() == 200);
221 InputStream contentStream = connection.getInputStream();
222 String content = StreamUtils.readToString(contentStream);
223 logger.debug("EditMapService response body:\n" + content);
224 assertTrue(content.startsWith("[{"));
225 assertTrue(content.endsWith("}]"));
226 assertTrue(content.matches(".*\"bbox\":.*"));
227 assertTrue(content.matches(".*\"legend\":.*"));
228 assertTrue(content.matches(".*\"layers\":.*"));
229 assertTrue(content.matches(".*\"sld\":.*"));
230 assertTrue(content.matches(".*\"geoserver\":.*"));
231 }
232 }
233
234 public static final UUID uuidCyprusDivisionsVocabulary = UUID.fromString("2119f610-1f93-4d87-af28-40aeefaca100");
235 private final Map<String, NamedArea> divisions = new HashMap<String, NamedArea>();
236
237 private boolean makeCyprusAreas() throws IOException {
238 //divisions
239
240
241 NamedAreaType areaType = NamedAreaType.NATURAL_AREA();
242 NamedAreaLevel areaLevel = NamedAreaLevel.NewInstance("Cyprus Division", "Cyprus Division", null);
243
244 termService.saveOrUpdate(areaLevel);
245
246 TermVocabulary<NamedArea> areaVocabulary = TermVocabulary.NewInstance(TermType.NamedArea, "Cyprus devisions", "Cyprus divisions", null, null);
247 areaVocabulary.setUuid(uuidCyprusDivisionsVocabulary);
248
249
250 for(int i = 1; i <= 8; i++){
251 UUID divisionUuid = getNamedAreaUuid(String.valueOf(i));
252 NamedArea division = this.newNamedArea(
253 divisionUuid,
254 "Division " + i,
255 "Cyprus: Division " + i,
256 String.valueOf(i), // id in vocab
257 areaType,
258 areaLevel,
259 areaVocabulary);
260 divisions.put(String.valueOf(i), division);
261 }
262
263 vocabService.saveOrUpdate(areaVocabulary);
264 commitAndStartNewTransaction(null);
265
266
267 // import and map shapefile attributes from csv
268 InputStream is = getClass().getClassLoader().getResourceAsStream("eu/etaxonomy/cdm/ext/geo/cyprusdivs.csv");
269 List<String> idSearchFields = new ArrayList<String>();
270 idSearchFields.add("bdcode");
271 String wmsLayerName = "cyprusdivs";
272 editGeoService.mapShapeFileToNamedAreas(new InputStreamReader(is), idSearchFields, wmsLayerName, uuidCyprusDivisionsVocabulary, null);
273
274 divisions.clear();
275 Set<DefinedTermBase> terms = vocabService.load(uuidCyprusDivisionsVocabulary).getTerms();
276 for(DefinedTermBase dtb : terms){
277 divisions.put(dtb.getIdInVocabulary(), (NamedArea) dtb);
278 }
279
280
281 // indigenousStatus = (PresenceTerm)getTermService().find(CyprusTransformer.indigenousUuid);
282 // casualStatus = (PresenceTerm)getTermService().find(CyprusTransformer.casualUuid);
283 // nonInvasiveStatus = (PresenceTerm)getTermService().find(CyprusTransformer.nonInvasiveUuid);
284 // invasiveStatus = (PresenceTerm)getTermService().find(CyprusTransformer.invasiveUuid);
285 // questionableStatus = (PresenceTerm)getTermService().find(CyprusTransformer.questionableUuid);
286
287 return true;
288
289
290 }
291
292 public static final UUID uuidDivision1 = UUID.fromString("ab17eee9-1abb-4ce9-a9a2-563f840cdbfc");
293 public static final UUID uuidDivision2 = UUID.fromString("c3606165-efb7-4224-a168-63e009eb4aa5");
294 public static final UUID uuidDivision3 = UUID.fromString("750d4e07-e34b-491f-a7b7-09723afdc960");
295 public static final UUID uuidDivision4 = UUID.fromString("8a858922-e8e5-4791-ad53-906e50633ec7");
296 public static final UUID uuidDivision5 = UUID.fromString("16057133-d541-4ebd-81d4-cb92265ec54c");
297 public static final UUID uuidDivision6 = UUID.fromString("fbf21230-4a42-4f4c-9af8-5da52123c264");
298 public static final UUID uuidDivision7 = UUID.fromString("d31dd96a-36ea-4428-871c-d8552a9565ca");
299 public static final UUID uuidDivision8 = UUID.fromString("236ea447-c3ab-486d-9e06-cc5907861acc");
300
301
302 public UUID getNamedAreaUuid(String key) {
303 if (StringUtils.isBlank(key)){return null;
304 }else if (key.equalsIgnoreCase("1")){return uuidDivision1;
305 }else if (key.equalsIgnoreCase("2")){return uuidDivision2;
306 }else if (key.equalsIgnoreCase("3")){return uuidDivision3;
307 }else if (key.equalsIgnoreCase("4")){return uuidDivision4;
308 }else if (key.equalsIgnoreCase("5")){return uuidDivision5;
309 }else if (key.equalsIgnoreCase("6")){return uuidDivision6;
310 }else if (key.equalsIgnoreCase("7")){return uuidDivision7;
311 }else if (key.equalsIgnoreCase("8")){return uuidDivision8;
312 }else{
313 return null;
314 }
315 }
316
317 protected NamedArea newNamedArea(UUID uuid, String label, String text, String IdInVocabulary, NamedAreaType areaType, NamedAreaLevel level, TermVocabulary<NamedArea> voc){
318 NamedArea namedArea = NamedArea.NewInstance(text, label, null);
319 voc.addTerm(namedArea);
320 namedArea.setType(areaType);
321 namedArea.setLevel(level);
322 namedArea.setUuid(uuid);
323 namedArea.setIdInVocabulary(IdInVocabulary);
324 return namedArea;
325 }
326
327 @Test
328 public void testGetWebServiceUrlBangka() throws ClientProtocolException, IOException, URISyntaxException {
329 NamedArea areaBangka = NamedArea.NewInstance("Bangka", "Bangka", null);
330 TermVocabulary<NamedArea> voc = TermVocabulary.NewInstance(TermType.NamedArea, "test Voc", "test voc", null, null);
331 voc.addTerm(areaBangka);
332
333 GeoServiceArea geoServiceArea = new GeoServiceArea();
334 String geoServiceLayer="vmap0_as_bnd_political_boundary_a";
335 String layerFieldName ="nam";
336 String areaValue = "PULAU BANGKA#SUMATERA SELATAN";
337 geoServiceArea.add(geoServiceLayer, layerFieldName, areaValue);
338 geoServiceArea.add(geoServiceLayer, layerFieldName, "BALI");
339
340 mapping.set(areaBangka, geoServiceArea);
341 Set<Distribution> distributions = new HashSet<Distribution>();
342 distributions.add(Distribution.NewInstance(areaBangka, PresenceTerm.PRESENT()));
343
344 Map<PresenceAbsenceTermBase<?>, Color> presenceAbsenceColorMap = new HashMap<PresenceAbsenceTermBase<?>, Color>();
345 presenceAbsenceColorMap.put(PresenceTerm.PRESENT(), Color.BLUE);
346
347 presenceAbsenceColorMap = null;
348 List<Language> languages = new ArrayList<Language>();
349
350 boolean subAreaPreference = false;
351 boolean statusOrderPreference = false;
352 String result = EditGeoServiceUtilities.getDistributionServiceRequestParameterString(distributions,
353 subAreaPreference ,
354 statusOrderPreference,
355 null, mapping, presenceAbsenceColorMap, null, languages );
356 //TODO Set semantics is not determined
357 //String expected = "http://www.test.de/webservice?l=tdwg3&ad=tdwg3:a:GER|b:OKL|c:BGM|b:SPA|d:FRA&as=a:005500|b:00FF00|c:FFFFFF|d:001100&bbox=-20,40,40,40&ms=400x300";
358
359 logger.debug(result);
360 assertTrue(result.matches(".*ad=vmap0_as_bnd_political_boundary_a%3Anam:.*"));
361 assertTrue(result.matches(".*(PULAU\\+BANGKA%23SUMATERA\\+SELATAN).*") );
362 assertTrue(result.matches(".*(BALI).*") );
363
364 // request map image from webservice
365 subTestWithEditMapService(result);
366 }
367
368
369 }