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

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

fix #16546 - Support Overpass API JSON format

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