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.FileNotFoundException;
|
17
|
import java.io.IOException;
|
18
|
import java.io.InputStream;
|
19
|
import java.io.InputStreamReader;
|
20
|
import java.net.HttpURLConnection;
|
21
|
import java.net.MalformedURLException;
|
22
|
import java.net.URI;
|
23
|
import java.net.URISyntaxException;
|
24
|
import java.net.URL;
|
25
|
import java.util.ArrayList;
|
26
|
import java.util.HashMap;
|
27
|
import java.util.HashSet;
|
28
|
import java.util.List;
|
29
|
import java.util.Map;
|
30
|
import java.util.Set;
|
31
|
import java.util.UUID;
|
32
|
|
33
|
import org.apache.commons.lang.StringUtils;
|
34
|
import org.apache.http.client.ClientProtocolException;
|
35
|
import org.apache.log4j.Logger;
|
36
|
import org.junit.Assert;
|
37
|
import org.junit.Before;
|
38
|
import org.junit.Ignore;
|
39
|
import org.junit.Test;
|
40
|
import org.unitils.spring.annotation.SpringBeanByType;
|
41
|
|
42
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
43
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
44
|
import eu.etaxonomy.cdm.common.StreamUtils;
|
45
|
import eu.etaxonomy.cdm.common.UriUtils;
|
46
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
47
|
import eu.etaxonomy.cdm.model.common.Language;
|
48
|
import eu.etaxonomy.cdm.model.common.TermType;
|
49
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
50
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
51
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
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, PresenceAbsenceTerm.PRESENT()));
|
116
|
distributions.add(Distribution.NewInstance(termService.findByIdInVocabulary("DE", Country.uuidCountryVocabulary, Country.class), PresenceAbsenceTerm.INTRODUCED()));
|
117
|
Map<PresenceAbsenceTerm, Color> presenceAbsenceColorMap = new HashMap<PresenceAbsenceTerm, Color>();
|
118
|
presenceAbsenceColorMap.put(PresenceAbsenceTerm.PRESENT(), Color.BLUE);
|
119
|
presenceAbsenceColorMap.put(PresenceAbsenceTerm.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
|
null, mapping, presenceAbsenceColorMap, null,
|
126
|
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"), PresenceAbsenceTerm.PRESENT()));
|
137
|
distributions.add(Distribution.NewInstance(termService.getAreaByTdwgAbbreviation("GER"), PresenceAbsenceTerm.INTRODUCED()));
|
138
|
distributions.add(Distribution.NewInstance(termService.getAreaByTdwgAbbreviation("14"), PresenceAbsenceTerm.CULTIVATED()));
|
139
|
distributions.add(Distribution.NewInstance(termService.getAreaByTdwgAbbreviation("BGM"), PresenceAbsenceTerm.ABSENT()));
|
140
|
distributions.add(Distribution.NewInstance(termService.getAreaByTdwgAbbreviation("FRA"), PresenceAbsenceTerm.ABSENT()));
|
141
|
distributions.add(Distribution.NewInstance(termService.getAreaByTdwgAbbreviation("IND-AP"), PresenceAbsenceTerm.PRESENT()));
|
142
|
|
143
|
Map<PresenceAbsenceTerm, Color> presenceAbsenceColorMap = new HashMap<PresenceAbsenceTerm, Color>();
|
144
|
presenceAbsenceColorMap.put(PresenceAbsenceTerm.PRESENT(), Color.BLUE);
|
145
|
presenceAbsenceColorMap.put(PresenceAbsenceTerm.INTRODUCED(), Color.BLACK);
|
146
|
presenceAbsenceColorMap.put(PresenceAbsenceTerm.CULTIVATED(), Color.YELLOW);
|
147
|
presenceAbsenceColorMap.put(PresenceAbsenceTerm.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
|
null ,
|
157
|
mapping,
|
158
|
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"), PresenceAbsenceTerm.PRESENT()));
|
180
|
distributions.add(Distribution.NewInstance(divisions.get("2"), PresenceAbsenceTerm.INTRODUCED()));
|
181
|
distributions.add(Distribution.NewInstance(divisions.get("3"), PresenceAbsenceTerm.CULTIVATED()));
|
182
|
distributions.add(Distribution.NewInstance(divisions.get("4"), PresenceAbsenceTerm.ABSENT()));
|
183
|
distributions.add(Distribution.NewInstance(divisions.get("5"), PresenceAbsenceTerm.ABSENT()));
|
184
|
distributions.add(Distribution.NewInstance(divisions.get("6"), PresenceAbsenceTerm.PRESENT()));
|
185
|
|
186
|
Map<PresenceAbsenceTerm, Color> presenceAbsenceColorMap = new HashMap<PresenceAbsenceTerm, Color>();
|
187
|
presenceAbsenceColorMap.put(PresenceAbsenceTerm.PRESENT(), Color.BLUE);
|
188
|
presenceAbsenceColorMap.put(PresenceAbsenceTerm.INTRODUCED(), Color.BLACK);
|
189
|
presenceAbsenceColorMap.put(PresenceAbsenceTerm.CULTIVATED(), Color.YELLOW);
|
190
|
presenceAbsenceColorMap.put(PresenceAbsenceTerm.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
|
null ,
|
200
|
mapping,
|
201
|
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
|
// connection.setReadTimeout(10000); //timeout after 10 sec, does not seem to work
|
221
|
assertTrue(connection.getResponseCode() == 200);
|
222
|
InputStream contentStream = connection.getInputStream();
|
223
|
String content = StreamUtils.readToString(contentStream);
|
224
|
logger.debug("EditMapService response body:\n" + content);
|
225
|
assertTrue(content.startsWith("[{"));
|
226
|
assertTrue(content.endsWith("}]"));
|
227
|
assertTrue(content.matches(".*\"bbox\":.*"));
|
228
|
assertTrue(content.matches(".*\"legend\":.*"));
|
229
|
assertTrue(content.matches(".*\"layers\":.*"));
|
230
|
assertTrue(content.matches(".*\"sld\":.*"));
|
231
|
assertTrue(content.matches(".*\"geoserver\":.*"));
|
232
|
}
|
233
|
}
|
234
|
|
235
|
public static final UUID uuidCyprusDivisionsVocabulary = UUID.fromString("2119f610-1f93-4d87-af28-40aeefaca100");
|
236
|
private final Map<String, NamedArea> divisions = new HashMap<String, NamedArea>();
|
237
|
|
238
|
private boolean makeCyprusAreas() throws IOException {
|
239
|
//divisions
|
240
|
|
241
|
|
242
|
NamedAreaType areaType = NamedAreaType.NATURAL_AREA();
|
243
|
NamedAreaLevel areaLevel = NamedAreaLevel.NewInstance("Cyprus Division", "Cyprus Division", null);
|
244
|
|
245
|
termService.saveOrUpdate(areaLevel);
|
246
|
|
247
|
TermVocabulary<NamedArea> areaVocabulary = TermVocabulary.NewInstance(TermType.NamedArea, "Cyprus devisions", "Cyprus divisions", null, null);
|
248
|
areaVocabulary.setUuid(uuidCyprusDivisionsVocabulary);
|
249
|
|
250
|
|
251
|
for(int i = 1; i <= 8; i++){
|
252
|
UUID divisionUuid = getNamedAreaUuid(String.valueOf(i));
|
253
|
NamedArea division = this.newNamedArea(
|
254
|
divisionUuid,
|
255
|
"Division " + i,
|
256
|
"Cyprus: Division " + i,
|
257
|
String.valueOf(i), // id in vocab
|
258
|
areaType,
|
259
|
areaLevel,
|
260
|
areaVocabulary);
|
261
|
divisions.put(String.valueOf(i), division);
|
262
|
}
|
263
|
|
264
|
vocabService.saveOrUpdate(areaVocabulary);
|
265
|
commitAndStartNewTransaction(null);
|
266
|
|
267
|
|
268
|
// import and map shapefile attributes from csv
|
269
|
InputStream is = getClass().getClassLoader().getResourceAsStream("eu/etaxonomy/cdm/ext/geo/cyprusdivs.csv");
|
270
|
List<String> idSearchFields = new ArrayList<String>();
|
271
|
idSearchFields.add("bdcode");
|
272
|
String wmsLayerName = "cyprusdivs";
|
273
|
editGeoService.mapShapeFileToNamedAreas(new InputStreamReader(is), idSearchFields, wmsLayerName, uuidCyprusDivisionsVocabulary, null);
|
274
|
|
275
|
divisions.clear();
|
276
|
Set<DefinedTermBase> terms = vocabService.load(uuidCyprusDivisionsVocabulary).getTerms();
|
277
|
for(DefinedTermBase dtb : terms){
|
278
|
divisions.put(dtb.getIdInVocabulary(), (NamedArea) dtb);
|
279
|
}
|
280
|
|
281
|
|
282
|
// indigenousStatus = (PresenceTerm)getTermService().find(CyprusTransformer.indigenousUuid);
|
283
|
// casualStatus = (PresenceTerm)getTermService().find(CyprusTransformer.casualUuid);
|
284
|
// nonInvasiveStatus = (PresenceTerm)getTermService().find(CyprusTransformer.nonInvasiveUuid);
|
285
|
// invasiveStatus = (PresenceTerm)getTermService().find(CyprusTransformer.invasiveUuid);
|
286
|
// questionableStatus = (PresenceTerm)getTermService().find(CyprusTransformer.questionableUuid);
|
287
|
|
288
|
return true;
|
289
|
|
290
|
|
291
|
}
|
292
|
|
293
|
public static final UUID uuidDivision1 = UUID.fromString("ab17eee9-1abb-4ce9-a9a2-563f840cdbfc");
|
294
|
public static final UUID uuidDivision2 = UUID.fromString("c3606165-efb7-4224-a168-63e009eb4aa5");
|
295
|
public static final UUID uuidDivision3 = UUID.fromString("750d4e07-e34b-491f-a7b7-09723afdc960");
|
296
|
public static final UUID uuidDivision4 = UUID.fromString("8a858922-e8e5-4791-ad53-906e50633ec7");
|
297
|
public static final UUID uuidDivision5 = UUID.fromString("16057133-d541-4ebd-81d4-cb92265ec54c");
|
298
|
public static final UUID uuidDivision6 = UUID.fromString("fbf21230-4a42-4f4c-9af8-5da52123c264");
|
299
|
public static final UUID uuidDivision7 = UUID.fromString("d31dd96a-36ea-4428-871c-d8552a9565ca");
|
300
|
public static final UUID uuidDivision8 = UUID.fromString("236ea447-c3ab-486d-9e06-cc5907861acc");
|
301
|
|
302
|
|
303
|
public UUID getNamedAreaUuid(String key) {
|
304
|
if (StringUtils.isBlank(key)){return null;
|
305
|
}else if (key.equalsIgnoreCase("1")){return uuidDivision1;
|
306
|
}else if (key.equalsIgnoreCase("2")){return uuidDivision2;
|
307
|
}else if (key.equalsIgnoreCase("3")){return uuidDivision3;
|
308
|
}else if (key.equalsIgnoreCase("4")){return uuidDivision4;
|
309
|
}else if (key.equalsIgnoreCase("5")){return uuidDivision5;
|
310
|
}else if (key.equalsIgnoreCase("6")){return uuidDivision6;
|
311
|
}else if (key.equalsIgnoreCase("7")){return uuidDivision7;
|
312
|
}else if (key.equalsIgnoreCase("8")){return uuidDivision8;
|
313
|
}else{
|
314
|
return null;
|
315
|
}
|
316
|
}
|
317
|
|
318
|
protected NamedArea newNamedArea(UUID uuid, String label, String text, String IdInVocabulary, NamedAreaType areaType, NamedAreaLevel level, TermVocabulary<NamedArea> voc){
|
319
|
NamedArea namedArea = NamedArea.NewInstance(text, label, null);
|
320
|
voc.addTerm(namedArea);
|
321
|
namedArea.setType(areaType);
|
322
|
namedArea.setLevel(level);
|
323
|
namedArea.setUuid(uuid);
|
324
|
namedArea.setIdInVocabulary(IdInVocabulary);
|
325
|
return namedArea;
|
326
|
}
|
327
|
|
328
|
@Test
|
329
|
public void testGetWebServiceUrlBangka() throws ClientProtocolException, IOException, URISyntaxException {
|
330
|
NamedArea areaBangka = NamedArea.NewInstance("Bangka", "Bangka", null);
|
331
|
TermVocabulary<NamedArea> voc = TermVocabulary.NewInstance(TermType.NamedArea, "test Voc", "test voc", null, null);
|
332
|
voc.addTerm(areaBangka);
|
333
|
|
334
|
GeoServiceArea geoServiceArea = new GeoServiceArea();
|
335
|
String geoServiceLayer="vmap0_as_bnd_political_boundary_a";
|
336
|
String layerFieldName ="nam";
|
337
|
String areaValue = "PULAU BANGKA#SUMATERA SELATAN";
|
338
|
geoServiceArea.add(geoServiceLayer, layerFieldName, areaValue);
|
339
|
geoServiceArea.add(geoServiceLayer, layerFieldName, "BALI");
|
340
|
|
341
|
mapping.set(areaBangka, geoServiceArea);
|
342
|
Set<Distribution> distributions = new HashSet<Distribution>();
|
343
|
distributions.add(Distribution.NewInstance(areaBangka, PresenceAbsenceTerm.PRESENT()));
|
344
|
|
345
|
Map<PresenceAbsenceTerm, Color> presenceAbsenceColorMap = new HashMap<PresenceAbsenceTerm, Color>();
|
346
|
presenceAbsenceColorMap.put(PresenceAbsenceTerm.PRESENT(), Color.BLUE);
|
347
|
|
348
|
presenceAbsenceColorMap = null;
|
349
|
List<Language> languages = new ArrayList<Language>();
|
350
|
|
351
|
boolean subAreaPreference = false;
|
352
|
boolean statusOrderPreference = false;
|
353
|
String result = EditGeoServiceUtilities.getDistributionServiceRequestParameterString(distributions,
|
354
|
null ,
|
355
|
mapping,
|
356
|
presenceAbsenceColorMap, null, languages );
|
357
|
//TODO Set semantics is not determined
|
358
|
//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";
|
359
|
|
360
|
logger.debug(result);
|
361
|
assertTrue(result.matches(".*ad=vmap0_as_bnd_political_boundary_a%3Anam:.*"));
|
362
|
assertTrue(result.matches(".*(PULAU\\+BANGKA%23SUMATERA\\+SELATAN).*") );
|
363
|
assertTrue(result.matches(".*(BALI).*") );
|
364
|
|
365
|
// request map image from webservice
|
366
|
subTestWithEditMapService(result);
|
367
|
}
|
368
|
|
369
|
|
370
|
/* (non-Javadoc)
|
371
|
* @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
|
372
|
*/
|
373
|
@Override
|
374
|
public void createTestDataSet() throws FileNotFoundException {
|
375
|
// TODO Auto-generated method stub
|
376
|
|
377
|
}
|
378
|
|
379
|
|
380
|
}
|