3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.ext
.geo
;
13 import static org
.junit
.Assert
.assertTrue
;
15 import java
.awt
.Color
;
16 import java
.io
.IOException
;
17 import java
.io
.InputStream
;
18 import java
.net
.HttpURLConnection
;
19 import java
.net
.MalformedURLException
;
21 import java
.net
.URISyntaxException
;
23 import java
.util
.ArrayList
;
24 import java
.util
.HashMap
;
25 import java
.util
.HashSet
;
26 import java
.util
.List
;
29 import java
.util
.UUID
;
31 import org
.apache
.http
.client
.ClientProtocolException
;
32 import org
.apache
.log4j
.Logger
;
33 import org
.junit
.After
;
34 import org
.junit
.AfterClass
;
35 import org
.junit
.Before
;
36 import org
.junit
.Test
;
37 import org
.unitils
.spring
.annotation
.SpringBeanByType
;
39 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
40 import eu
.etaxonomy
.cdm
.common
.StreamUtils
;
41 import eu
.etaxonomy
.cdm
.common
.UriUtils
;
42 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
43 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
44 import eu
.etaxonomy
.cdm
.model
.description
.AbsenceTerm
;
45 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
46 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTermBase
;
47 import eu
.etaxonomy
.cdm
.model
.description
.PresenceTerm
;
48 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
49 import eu
.etaxonomy
.cdm
.model
.location
.NamedAreaLevel
;
50 import eu
.etaxonomy
.cdm
.model
.location
.NamedAreaType
;
51 import eu
.etaxonomy
.cdm
.model
.location
.TdwgArea
;
52 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IDefinedTermDao
;
53 import eu
.etaxonomy
.cdm
.test
.integration
.CdmIntegrationTest
;
60 public class EditGeoServiceTest
extends CdmIntegrationTest
{
61 @SuppressWarnings("unused")
62 private static final Logger logger
= Logger
.getLogger(EditGeoServiceTest
.class);
64 private static final String EDIT_MAPSERVICE_URI_STING
= "http://edit.br.fgov.be/edit_wp5/v1/areas.php";
65 private static URI editMapServiceUri
;
68 private IDefinedTermDao termDao
;
71 private GeoServiceAreaAnnotatedMapping mapping
;
75 // * @throws java.lang.Exception
78 // public static void setUpBeforeClass() throws Exception {
79 // DefaultTermInitializer initializer = new DefaultTermInitializer();
80 // initializer.initialize();
84 * @throws java.lang.Exception
87 public static void tearDownAfterClass() throws Exception
{
91 * @throws java.lang.Exception
94 public void setUp() throws Exception
{
95 EditGeoServiceUtilities
.setTermDao(termDao
);
96 System
.setProperty("ONLY-A-TEST", "TRUE"); // allows EditGeoServiceUtilities to skip some line of code
97 editMapServiceUri
= new URI(EDIT_MAPSERVICE_URI_STING
);
101 * @throws java.lang.Exception
104 public void tearDown() throws Exception
{
107 //******************************************** TESTS**************
109 public void testGetWebServiceUrlTdwg() throws MalformedURLException
, IOException
{
110 //String webServiceUrl = "http://www.test.de/webservice";
111 Set
<Distribution
> distributions
= new HashSet
<Distribution
>();
112 distributions
.add(Distribution
.NewInstance(TdwgArea
.getAreaByTdwgAbbreviation("SPA"), PresenceTerm
.PRESENT()));
113 distributions
.add(Distribution
.NewInstance(TdwgArea
.getAreaByTdwgAbbreviation("GER"), PresenceTerm
.INTRODUCED()));
114 distributions
.add(Distribution
.NewInstance(TdwgArea
.getAreaByTdwgAbbreviation("14"), PresenceTerm
.CULTIVATED()));
115 distributions
.add(Distribution
.NewInstance(TdwgArea
.getAreaByTdwgAbbreviation("BGM"), AbsenceTerm
.ABSENT()));
116 distributions
.add(Distribution
.NewInstance(TdwgArea
.getAreaByTdwgAbbreviation("FRA"), AbsenceTerm
.ABSENT()));
117 distributions
.add(Distribution
.NewInstance(TdwgArea
.getAreaByTdwgAbbreviation("IND-AP"), PresenceTerm
.PRESENT()));
119 Map
<PresenceAbsenceTermBase
<?
>, Color
> presenceAbsenceColorMap
= new HashMap
<PresenceAbsenceTermBase
<?
>, Color
>();
120 presenceAbsenceColorMap
.put(PresenceTerm
.PRESENT(), Color
.BLUE
);
121 presenceAbsenceColorMap
.put(PresenceTerm
.INTRODUCED(), Color
.BLACK
);
122 presenceAbsenceColorMap
.put(PresenceTerm
.CULTIVATED(), Color
.YELLOW
);
123 presenceAbsenceColorMap
.put(AbsenceTerm
.ABSENT(), Color
.DARK_GRAY
);
124 String backLayer
="";
125 presenceAbsenceColorMap
= null;
126 String bbox
="-20,0,120,70";
127 List
<Language
> languages
= new ArrayList
<Language
>();
129 String result
= EditGeoServiceUtilities
.getDistributionServiceRequestParameterString(distributions
, mapping
, presenceAbsenceColorMap
, 600, 300, bbox
,backLayer
, null, languages
);
130 //TODO Set semantics is not determined
131 //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";
132 System
.out
.println(result
);
133 assertTrue(result
.matches(".*l=earth.*"));
134 assertTrue(result
.matches(".*ms=600,300.*"));
135 assertTrue(result
.matches(".*ad=tdwg[1-4].*"));
136 assertTrue(result
.matches(".*tdwg2:[a-d]:14[\\|&].*") );
137 assertTrue(result
.matches(".*[a-d]:FRA,BGM[\\|&].*") || result
.matches(".*[a-d]:BGM,FRA[\\|&].*") );
138 assertTrue(result
.matches(".*[a-d]:GER[\\|&].*") );
139 assertTrue(result
.matches(".*[a-d]:SPA[\\|&].*") );
140 assertTrue(result
.matches(".*tdwg4:[a-d]:INDAP[\\|&].*") );
141 //assertTrue(result.matches("0000ff"));
144 // request map image from webservice
145 subTestWithEditMapService(result
);
149 public void testGetWebServiceUrlCyprus() throws ClientProtocolException
, IOException
, URISyntaxException
{
151 Set
<Distribution
> distributions
= new HashSet
<Distribution
>();
152 distributions
.add(Distribution
.NewInstance(divisions
.get("1"), PresenceTerm
.PRESENT()));
153 distributions
.add(Distribution
.NewInstance(divisions
.get("2"), PresenceTerm
.INTRODUCED()));
154 distributions
.add(Distribution
.NewInstance(divisions
.get("3"), PresenceTerm
.CULTIVATED()));
155 distributions
.add(Distribution
.NewInstance(divisions
.get("4"), AbsenceTerm
.ABSENT()));
156 distributions
.add(Distribution
.NewInstance(divisions
.get("5"), AbsenceTerm
.ABSENT()));
157 distributions
.add(Distribution
.NewInstance(divisions
.get("6"), PresenceTerm
.PRESENT()));
159 Map
<PresenceAbsenceTermBase
<?
>, Color
> presenceAbsenceColorMap
= new HashMap
<PresenceAbsenceTermBase
<?
>, Color
>();
160 presenceAbsenceColorMap
.put(PresenceTerm
.PRESENT(), Color
.BLUE
);
161 presenceAbsenceColorMap
.put(PresenceTerm
.INTRODUCED(), Color
.BLACK
);
162 presenceAbsenceColorMap
.put(PresenceTerm
.CULTIVATED(), Color
.YELLOW
);
163 presenceAbsenceColorMap
.put(AbsenceTerm
.ABSENT(), Color
.DARK_GRAY
);
164 String backLayer
="";
165 presenceAbsenceColorMap
= null;
166 String bbox
="-20,0,120,70";
167 List
<Language
> languages
= new ArrayList
<Language
>();
169 String result
= EditGeoServiceUtilities
.getDistributionServiceRequestParameterString(distributions
, mapping
, presenceAbsenceColorMap
, 600, 300, bbox
,backLayer
, null, languages
);
170 //TODO Set semantics is not determined
171 //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";
172 assertTrue(result
.matches(".*l=earth.*"));
173 assertTrue(result
.matches(".*ms=600,300.*"));
174 assertTrue(result
.matches(".*ad=cyprusdivs%3Abdcode:.*"));
175 assertTrue(result
.matches(".*[a-d]:5,4[\\|&].*") || result
.matches(".*[a-d]:4,5[\\|&].*") );
176 assertTrue(result
.matches(".*[a-d]:1,6[\\|&].*") || result
.matches(".*[a-d]:6,1[\\|&].*") );
177 assertTrue(result
.matches(".*[a-d]:2[\\|&].*") );
178 assertTrue(result
.matches(".*[a-d]:3[\\|&].*") );
180 // request map image from webservice
181 subTestWithEditMapService(result
);
184 private void subTestWithEditMapService(String result
)throws MalformedURLException
, IOException
{
185 if(UriUtils
.isServiceAvailable(editMapServiceUri
)){
186 URL requestUrl
= new URL(editMapServiceUri
.toString() + "?img=false&" + result
);
187 HttpURLConnection connection
= (HttpURLConnection
) requestUrl
.openConnection();
188 connection
.connect();
189 assertTrue(connection
.getResponseCode() == 200);
190 InputStream contentStream
= connection
.getInputStream();
191 String content
= StreamUtils
.readToString(contentStream
);
192 System
.out
.println(content
);
193 assertTrue(content
.startsWith("[{"));
194 assertTrue(content
.endsWith("}]"));
195 assertTrue(content
.matches(".*\"bbox\":.*"));
196 assertTrue(content
.matches(".*\"legend\":.*"));
197 assertTrue(content
.matches(".*\"layers\":.*"));
198 assertTrue(content
.matches(".*\"sld\":.*"));
199 assertTrue(content
.matches(".*\"geoserver\":.*"));
203 public static final UUID uuidCyprusDivisionsVocabulary
= UUID
.fromString("2119f610-1f93-4d87-af28-40aeefaca100");
204 private Map
<String
, NamedArea
> divisions
= new HashMap
<String
, NamedArea
>();
206 private boolean makeCyprusAreas() {
210 NamedAreaType areaType
= NamedAreaType
.NATURAL_AREA();
211 NamedAreaLevel areaLevel
= NamedAreaLevel
.NewInstance("Cyprus Division", "Cyprus Division", null);
213 TermVocabulary areaVocabulary
= TermVocabulary
.NewInstance("Cyprus devisions", "Cyprus divisions", null, null);
214 areaVocabulary
.setUuid(uuidCyprusDivisionsVocabulary
);
216 for(int i
= 1; i
<= 8; i
++){
217 UUID divisionUuid
= getNamedAreaUuid(String
.valueOf(i
));
218 NamedArea division
= this.getNamedArea(divisionUuid
, "Division " + i
, "Cyprus: Division " + i
, String
.valueOf(i
), areaType
, areaLevel
, areaVocabulary
);
219 divisions
.put(String
.valueOf(i
), division
);
222 // indigenousStatus = (PresenceTerm)getTermService().find(CyprusTransformer.indigenousUuid);
223 // casualStatus = (PresenceTerm)getTermService().find(CyprusTransformer.casualUuid);
224 // nonInvasiveStatus = (PresenceTerm)getTermService().find(CyprusTransformer.nonInvasiveUuid);
225 // invasiveStatus = (PresenceTerm)getTermService().find(CyprusTransformer.invasiveUuid);
226 // questionableStatus = (PresenceTerm)getTermService().find(CyprusTransformer.questionableUuid);
233 public static final UUID uuidDivision1
= UUID
.fromString("ab17eee9-1abb-4ce9-a9a2-563f840cdbfc");
234 public static final UUID uuidDivision2
= UUID
.fromString("c3606165-efb7-4224-a168-63e009eb4aa5");
235 public static final UUID uuidDivision3
= UUID
.fromString("750d4e07-e34b-491f-a7b7-09723afdc960");
236 public static final UUID uuidDivision4
= UUID
.fromString("8a858922-e8e5-4791-ad53-906e50633ec7");
237 public static final UUID uuidDivision5
= UUID
.fromString("16057133-d541-4ebd-81d4-cb92265ec54c");
238 public static final UUID uuidDivision6
= UUID
.fromString("fbf21230-4a42-4f4c-9af8-5da52123c264");
239 public static final UUID uuidDivision7
= UUID
.fromString("d31dd96a-36ea-4428-871c-d8552a9565ca");
240 public static final UUID uuidDivision8
= UUID
.fromString("236ea447-c3ab-486d-9e06-cc5907861acc");
243 public UUID
getNamedAreaUuid(String key
) {
244 if (CdmUtils
.isEmpty(key
)){return null;
245 }else if (key
.equalsIgnoreCase("1")){return uuidDivision1
;
246 }else if (key
.equalsIgnoreCase("2")){return uuidDivision2
;
247 }else if (key
.equalsIgnoreCase("3")){return uuidDivision3
;
248 }else if (key
.equalsIgnoreCase("4")){return uuidDivision4
;
249 }else if (key
.equalsIgnoreCase("5")){return uuidDivision5
;
250 }else if (key
.equalsIgnoreCase("6")){return uuidDivision6
;
251 }else if (key
.equalsIgnoreCase("7")){return uuidDivision7
;
252 }else if (key
.equalsIgnoreCase("8")){return uuidDivision8
;
258 protected NamedArea
getNamedArea(UUID uuid
, String label
, String text
, String labelAbbrev
, NamedAreaType areaType
, NamedAreaLevel level
, TermVocabulary voc
){
259 NamedArea namedArea
= NamedArea
.NewInstance(text
, label
, labelAbbrev
);
260 voc
.addTerm(namedArea
);
261 namedArea
.setType(areaType
);
262 namedArea
.setLevel(level
);
263 namedArea
.setUuid(uuid
);
268 public void testGetWebServiceUrlBangka() throws ClientProtocolException
, IOException
, URISyntaxException
{
269 NamedArea areaBangka
= NamedArea
.NewInstance("Bangka", "Bangka", null);
270 TermVocabulary
<NamedArea
> voc
= TermVocabulary
.NewInstance("test Voc", "test voc", null, null);
271 voc
.addTerm(areaBangka
);
273 GeoServiceArea geoServiceArea
= new GeoServiceArea();
274 String geoServiceLayer
="vmap0_as_bnd_political_boundary_a";
275 String layerFieldName
="nam";
276 String areaValue
= "PULAU BANGKA#SUMATERA SELATAN";
277 geoServiceArea
.add(geoServiceLayer
, layerFieldName
, areaValue
);
278 geoServiceArea
.add(geoServiceLayer
, layerFieldName
, "BALI");
280 mapping
.set(areaBangka
, geoServiceArea
);
281 Set
<Distribution
> distributions
= new HashSet
<Distribution
>();
282 distributions
.add(Distribution
.NewInstance(areaBangka
, PresenceTerm
.PRESENT()));
284 Map
<PresenceAbsenceTermBase
<?
>, Color
> presenceAbsenceColorMap
= new HashMap
<PresenceAbsenceTermBase
<?
>, Color
>();
285 presenceAbsenceColorMap
.put(PresenceTerm
.PRESENT(), Color
.BLUE
);
287 String backLayer
="";
288 presenceAbsenceColorMap
= null;
289 String bbox
="90,-8,130,8";
290 List
<Language
> languages
= new ArrayList
<Language
>();
292 String result
= EditGeoServiceUtilities
.getDistributionServiceRequestParameterString(distributions
, mapping
, presenceAbsenceColorMap
, 600, 300, bbox
,backLayer
, null, languages
);
293 //TODO Set semantics is not determined
294 //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";
296 System
.out
.println(result
);
298 assertTrue(result
.matches(".*l=earth.*"));
299 assertTrue(result
.matches(".*ms=600,300.*"));
300 assertTrue(result
.matches(".*ad=vmap0_as_bnd_political_boundary_a%3Anam:.*"));
301 assertTrue(result
.matches(".*(PULAU\\+BANGKA%23SUMATERA\\+SELATAN).*") );
302 assertTrue(result
.matches(".*(BALI).*") );
304 // request map image from webservice
305 subTestWithEditMapService(result
);