source: josm/trunk/test/unit/org/openstreetmap/josm/io/OsmJsonReaderTest.java@ 17275

Last change on this file since 17275 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 9.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertFalse;
6import static org.junit.jupiter.api.Assertions.assertTrue;
7
8import java.io.ByteArrayInputStream;
9import java.io.InputStream;
10import java.nio.charset.StandardCharsets;
11import java.util.Iterator;
12
13import org.junit.jupiter.api.BeforeAll;
14import org.junit.jupiter.api.Test;
15import org.junit.jupiter.api.extension.RegisterExtension;
16import org.openstreetmap.josm.data.coor.LatLon;
17import org.openstreetmap.josm.data.osm.DataSet;
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.data.osm.Relation;
20import org.openstreetmap.josm.data.osm.RelationMember;
21import org.openstreetmap.josm.data.osm.Way;
22import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
23import org.openstreetmap.josm.testutils.JOSMTestRules;
24import org.openstreetmap.josm.tools.date.DateUtils;
25
26import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27
28/**
29 * Unit tests of {@link OsmReader} class.
30 */
31class OsmJsonReaderTest {
32
33 /**
34 * Setup rule
35 */
36 @RegisterExtension
37 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
38 public JOSMTestRules test = new JOSMTestRules();
39
40 /**
41 * Setup test
42 */
43 @BeforeAll
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 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 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 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 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 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 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 /**
242 * Test a relation example without members.
243 * @throws Exception never
244 */
245 @Test
246 void testEmptyRelation() throws Exception {
247 DataSet ds = parse("{\n" +
248 " \"type\": \"relation\",\n" +
249 " \"id\": 1,\n" +
250 " \"tags\": {}\n" +
251 "}");
252 Relation r = ds.getRelations().iterator().next();
253 assertEquals(1, r.getUniqueId());
254 assertEquals(0, r.getMembersCount());
255 }
256
257 /**
258 * Test reading remark from Overpass API.
259 * @throws Exception if any error occurs
260 */
261 @Test
262 void testRemark() throws Exception {
263 DataSet ds = parse("", "," +
264 " \"remark\": \"runtime error: Query ran out of memory in \\\"query\\\" at line 5.\"\n");
265 assertEquals("runtime error: Query ran out of memory in \"query\" at line 5.", ds.getRemark());
266 }
267}
Note: See TracBrowser for help on using the repository browser.