Mercurial > lada > lada-server
comparison src/test/java/de/intevation/lada/test/ServiceTest.java @ 1239:59bdb52bac1c
Test output latitude and longitude against input geometry.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Tue, 13 Dec 2016 20:34:36 +0100 |
parents | 186d602e031a |
children | 95d04f56266d |
comparison
equal
deleted
inserted
replaced
1238:a81de70d321d | 1239:59bdb52bac1c |
---|---|
10 import java.io.InputStream; | 10 import java.io.InputStream; |
11 import java.io.StringReader; | 11 import java.io.StringReader; |
12 import java.net.URL; | 12 import java.net.URL; |
13 import java.sql.Timestamp; | 13 import java.sql.Timestamp; |
14 import java.util.List; | 14 import java.util.List; |
15 import java.util.ArrayList; | |
15 import java.util.Map.Entry; | 16 import java.util.Map.Entry; |
16 import java.util.Scanner; | 17 import java.util.Scanner; |
17 | 18 |
18 import javax.json.Json; | 19 import javax.json.Json; |
19 import javax.json.JsonException; | 20 import javax.json.JsonException; |
29 import javax.ws.rs.core.Response; | 30 import javax.ws.rs.core.Response; |
30 | 31 |
31 import org.apache.commons.lang.WordUtils; | 32 import org.apache.commons.lang.WordUtils; |
32 import org.junit.Assert; | 33 import org.junit.Assert; |
33 | 34 |
35 import com.vividsolutions.jts.io.ParseException; | |
36 import com.vividsolutions.jts.io.WKTReader; | |
37 import com.vividsolutions.jts.geom.Geometry; | |
38 import com.vividsolutions.jts.geom.Point; | |
39 | |
34 import de.intevation.lada.BaseTest; | 40 import de.intevation.lada.BaseTest; |
35 import de.intevation.lada.Protocol; | 41 import de.intevation.lada.Protocol; |
36 import de.intevation.lada.test.land.ProbeTest; | 42 import de.intevation.lada.test.land.ProbeTest; |
37 | 43 |
38 public class ServiceTest { | 44 public class ServiceTest { |
39 | 45 |
46 private static final String LAT_KEY = "latitude"; | |
47 private static final String LONG_KEY = "longitude"; | |
48 | |
40 protected List<Protocol> protocol; | 49 protected List<Protocol> protocol; |
41 | 50 |
42 protected List<String> timestampAttributes; | 51 protected List<String> timestampAttributes; |
52 protected List<String> geomPointAttributes = new ArrayList<String>(); | |
43 | 53 |
44 protected URL baseUrl; | 54 protected URL baseUrl; |
45 | 55 |
46 public void init(URL baseUrl, List<Protocol> protocol) { | 56 public void init(URL baseUrl, List<Protocol> protocol) { |
47 this.baseUrl = baseUrl; | 57 this.baseUrl = baseUrl; |
71 protected JsonObjectBuilder convertObject(JsonObject object) { | 81 protected JsonObjectBuilder convertObject(JsonObject object) { |
72 JsonObjectBuilder builder = Json.createObjectBuilder(); | 82 JsonObjectBuilder builder = Json.createObjectBuilder(); |
73 for (Entry<String, JsonValue> entry : object.entrySet()) { | 83 for (Entry<String, JsonValue> entry : object.entrySet()) { |
74 String key = WordUtils.capitalize( | 84 String key = WordUtils.capitalize( |
75 entry.getKey(), new char[]{'_'}).replaceAll("_",""); | 85 entry.getKey(), new char[]{'_'}).replaceAll("_",""); |
76 key = key.replaceFirst(key.substring(0, 1), key.substring(0, 1).toLowerCase()); | 86 key = key.replaceFirst( |
87 key.substring(0, 1), key.substring(0, 1).toLowerCase()); | |
77 if (timestampAttributes.contains(key)) { | 88 if (timestampAttributes.contains(key)) { |
78 Timestamp timestamp = Timestamp.valueOf(entry.getValue().toString().replaceAll("\"", "")); | 89 Timestamp timestamp = Timestamp.valueOf( |
90 entry.getValue().toString().replaceAll("\"", "")); | |
79 builder.add(key, timestamp.getTime()); | 91 builder.add(key, timestamp.getTime()); |
92 } | |
93 else if (geomPointAttributes.contains(key)) { | |
94 // Convert EWKT to latitude and longitude | |
95 String wkt = entry.getValue().toString().split(";")[1]; | |
96 try { | |
97 Geometry geom = new WKTReader().read(wkt); | |
98 if (!(geom instanceof Point)) { | |
99 throw new IllegalArgumentException( | |
100 "WKT does not represent a point"); | |
101 } | |
102 Point point = (Point)geom; | |
103 builder.add(LONG_KEY, point.getX()); | |
104 builder.add(LAT_KEY, point.getY()); | |
105 } catch (ParseException | IllegalArgumentException e) { | |
106 Protocol prot = new Protocol(); | |
107 prot.addInfo("exception", e.getMessage()); | |
108 protocol.add(prot); | |
109 Assert.fail("Exception while parsing WKT '" | |
110 + wkt + "':\n" | |
111 + e.getMessage()); | |
112 } | |
80 } | 113 } |
81 else { | 114 else { |
82 builder.add(key, entry.getValue()); | 115 builder.add(key, entry.getValue()); |
83 } | 116 } |
84 } | 117 } |