1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 |
|
---|
8 | import java.io.ByteArrayInputStream;
|
---|
9 | import java.io.InputStream;
|
---|
10 | import java.nio.charset.StandardCharsets;
|
---|
11 | import java.util.Iterator;
|
---|
12 |
|
---|
13 | import org.junit.BeforeClass;
|
---|
14 | import org.junit.Rule;
|
---|
15 | import org.junit.Test;
|
---|
16 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
17 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
18 | import org.openstreetmap.josm.data.osm.Node;
|
---|
19 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
20 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
21 | import org.openstreetmap.josm.data.osm.Way;
|
---|
22 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
23 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
24 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
25 |
|
---|
26 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Unit tests of {@link OsmReader} class.
|
---|
30 | */
|
---|
31 | public class OsmJsonReaderTest {
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Setup rule
|
---|
35 | */
|
---|
36 | @Rule
|
---|
37 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
38 | public JOSMTestRules test = new JOSMTestRules();
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Setup test
|
---|
42 | */
|
---|
43 | @BeforeClass
|
---|
44 | public static void setUp() {
|
---|
45 | DateUtils.newIsoDateTimeFormat().setTimeZone(DateUtils.UTC);
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Parse JSON.
|
---|
50 | * @param osm OSM data in JSON format, without header/footer
|
---|
51 | * @return data set
|
---|
52 | * @throws Exception if any error occurs
|
---|
53 | */
|
---|
54 | private static DataSet parse(String osm) throws Exception {
|
---|
55 | return parse(osm, "");
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Parse JSON.
|
---|
60 | * @param osm OSM data in JSON format, without header/footer
|
---|
61 | * @param extraContent extra content added after OSM elements
|
---|
62 | * @return data set
|
---|
63 | * @throws Exception if any error occurs
|
---|
64 | */
|
---|
65 | private static DataSet parse(String osm, String extraContent) throws Exception {
|
---|
66 | try (InputStream in = new ByteArrayInputStream((
|
---|
67 | "{\n" +
|
---|
68 | " \"version\": 0.6,\n" +
|
---|
69 | " \"generator\": \"Overpass API\",\n" +
|
---|
70 | " \"osm3s\": {\n" +
|
---|
71 | " \"timestamp_osm_base\": \"date\",\n" +
|
---|
72 | " \"copyright\": \"The data included in this document is from www.openstreetmap.org. " +
|
---|
73 | "It has there been collected by a large group of contributors. " +
|
---|
74 | "For individual attribution of each item please refer to " +
|
---|
75 | "http://www.openstreetmap.org/api/0.6/[node|way|relation]/#id/history\"\n" +
|
---|
76 | " },\n" +
|
---|
77 | " \"elements\": [" + osm + "]\n" +
|
---|
78 | extraContent +
|
---|
79 | "}")
|
---|
80 | .getBytes(StandardCharsets.UTF_8))) {
|
---|
81 | return OsmJsonReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Test an example without data.
|
---|
88 | * @throws Exception never
|
---|
89 | */
|
---|
90 | @Test
|
---|
91 | public void testHeader() throws Exception {
|
---|
92 | DataSet ds = parse("");
|
---|
93 | assertEquals("0.6", ds.getVersion());
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Test an example with the spatial data only.
|
---|
98 | * @throws Exception never
|
---|
99 | */
|
---|
100 | @Test
|
---|
101 | public void testNodeSpatialData() throws Exception {
|
---|
102 | DataSet ds = parse("{\n" +
|
---|
103 | " \"type\": \"node\",\n" +
|
---|
104 | " \"id\": 1,\n" +
|
---|
105 | " \"lat\": 2.0,\n" +
|
---|
106 | " \"lon\": -3.0\n" +
|
---|
107 | "}");
|
---|
108 | Node n = ds.getNodes().iterator().next();
|
---|
109 | assertEquals(1, n.getUniqueId());
|
---|
110 | assertEquals(new LatLon(2.0, -3.0), n.getCoor());
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Test an example with the meta data.
|
---|
115 | * @throws Exception never
|
---|
116 | */
|
---|
117 | @Test
|
---|
118 | public void testNodeMetaData() throws Exception {
|
---|
119 | DataSet ds = parse("{\n" +
|
---|
120 | " \"type\": \"node\",\n" +
|
---|
121 | " \"id\": 1,\n" +
|
---|
122 | " \"lat\": 2.0,\n" +
|
---|
123 | " \"lon\": -3.0,\n" +
|
---|
124 | " \"timestamp\": \"2018-01-01T00:00:00Z\",\n" +
|
---|
125 | " \"version\": 4,\n" +
|
---|
126 | " \"changeset\": 5,\n" +
|
---|
127 | " \"user\": \"somebody\",\n" +
|
---|
128 | " \"uid\": 6\n" +
|
---|
129 | "}");
|
---|
130 | Node n = ds.getNodes().iterator().next();
|
---|
131 | assertEquals(1, n.getUniqueId());
|
---|
132 | assertEquals(new LatLon(2.0, -3.0), n.getCoor());
|
---|
133 | assertEquals("2018-01-01T00:00:00Z", DateUtils.newIsoDateTimeFormat().format(n.getTimestamp()));
|
---|
134 | assertEquals(4, n.getVersion());
|
---|
135 | assertEquals(5, n.getChangesetId());
|
---|
136 | assertEquals(6, n.getUser().getId());
|
---|
137 | assertEquals("somebody", n.getUser().getName());
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Test an example with tags.
|
---|
142 | * @throws Exception never
|
---|
143 | */
|
---|
144 | @Test
|
---|
145 | public void testNodeTags() throws Exception {
|
---|
146 | DataSet ds = parse("{\n" +
|
---|
147 | " \"type\": \"node\",\n" +
|
---|
148 | " \"id\": 1,\n" +
|
---|
149 | " \"lat\": 2.0,\n" +
|
---|
150 | " \"lon\": -3.0,\n" +
|
---|
151 | " \"tags\": {\n" +
|
---|
152 | " \"highway\": \"bus_stop\",\n" +
|
---|
153 | " \"name\": \"Main Street\"\n" +
|
---|
154 | " }" +
|
---|
155 | "}");
|
---|
156 | Node n = ds.getNodes().iterator().next();
|
---|
157 | assertEquals(1, n.getUniqueId());
|
---|
158 | assertEquals(new LatLon(2.0, -3.0), n.getCoor());
|
---|
159 | assertTrue(n.isTagged());
|
---|
160 | assertEquals("bus_stop", n.get("highway"));
|
---|
161 | assertEquals("Main Street", n.get("name"));
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Test a way example.
|
---|
166 | * @throws Exception never
|
---|
167 | */
|
---|
168 | @Test
|
---|
169 | public void testWay() throws Exception {
|
---|
170 | DataSet ds = parse("{\n" +
|
---|
171 | " \"type\": \"way\",\n" +
|
---|
172 | " \"id\": 1,\n" +
|
---|
173 | " \"nodes\": [\n" +
|
---|
174 | " 10,\n" +
|
---|
175 | " 11,\n" +
|
---|
176 | " 12\n" +
|
---|
177 | " ],\n" +
|
---|
178 | " \"tags\": {\n" +
|
---|
179 | " \"highway\": \"tertiary\",\n" +
|
---|
180 | " \"name\": \"Main Street\"\n" +
|
---|
181 | " }\n" +
|
---|
182 | "}");
|
---|
183 | Way w = ds.getWays().iterator().next();
|
---|
184 | assertEquals(1, w.getUniqueId());
|
---|
185 | assertEquals(3, w.getNodesCount());
|
---|
186 | Iterator<Node> it = w.getNodes().iterator();
|
---|
187 | assertEquals(10, it.next().getUniqueId());
|
---|
188 | assertEquals(11, it.next().getUniqueId());
|
---|
189 | assertEquals(12, it.next().getUniqueId());
|
---|
190 | assertFalse(it.hasNext());
|
---|
191 | assertTrue(w.isTagged());
|
---|
192 | assertEquals("tertiary", w.get("highway"));
|
---|
193 | assertEquals("Main Street", w.get("name"));
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Test a relation example.
|
---|
198 | * @throws Exception never
|
---|
199 | */
|
---|
200 | @Test
|
---|
201 | public void testRelation() throws Exception {
|
---|
202 | DataSet ds = parse("{\n" +
|
---|
203 | " \"type\": \"relation\",\n" +
|
---|
204 | " \"id\": 1,\n" +
|
---|
205 | " \"members\": [\n" +
|
---|
206 | " {\n" +
|
---|
207 | " \"type\": \"way\",\n" +
|
---|
208 | " \"ref\": 1745069,\n" +
|
---|
209 | " \"role\": \"\"\n" +
|
---|
210 | " },\n" +
|
---|
211 | " {\n" +
|
---|
212 | " \"type\": \"way\",\n" +
|
---|
213 | " \"ref\": 172789,\n" +
|
---|
214 | " \"role\": \"\"\n" +
|
---|
215 | " }\n" +
|
---|
216 | " ],\n" +
|
---|
217 | " \"tags\": {\n" +
|
---|
218 | " \"from\": \"Konrad-Adenauer-Platz\",\n" +
|
---|
219 | " \"name\": \"VRS 636\",\n" +
|
---|
220 | " \"network\": \"VRS\",\n" +
|
---|
221 | " \"operator\": \"SWB\",\n" +
|
---|
222 | " \"ref\": \"636\",\n" +
|
---|
223 | " \"route\": \"bus\",\n" +
|
---|
224 | " \"to\": \"Gielgen\",\n" +
|
---|
225 | " \"type\": \"route\",\n" +
|
---|
226 | " \"via\": \"Ramersdorf\"\n" +
|
---|
227 | " }\n" +
|
---|
228 | "}");
|
---|
229 | Relation r = ds.getRelations().iterator().next();
|
---|
230 | assertEquals(1, r.getUniqueId());
|
---|
231 | assertEquals(2, r.getMembersCount());
|
---|
232 | Iterator<RelationMember> it = r.getMembers().iterator();
|
---|
233 | assertEquals(1745069, it.next().getUniqueId());
|
---|
234 | assertEquals(172789, it.next().getUniqueId());
|
---|
235 | assertFalse(it.hasNext());
|
---|
236 | assertTrue(r.isTagged());
|
---|
237 | assertEquals("route", r.get("type"));
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Test reading remark from Overpass API.
|
---|
242 | * @throws Exception if any error occurs
|
---|
243 | */
|
---|
244 | @Test
|
---|
245 | public void testRemark() throws Exception {
|
---|
246 | DataSet ds = parse("", "," +
|
---|
247 | " \"remark\": \"runtime error: Query ran out of memory in \\\"query\\\" at line 5.\"\n");
|
---|
248 | assertEquals("runtime error: Query ran out of memory in \"query\" at line 5.", ds.getRemark());
|
---|
249 | }
|
---|
250 | }
|
---|