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.assertNotNull;
|
---|
7 | import static org.junit.Assert.assertTrue;
|
---|
8 | import static org.junit.Assert.fail;
|
---|
9 |
|
---|
10 | import java.io.File;
|
---|
11 | import java.io.FileInputStream;
|
---|
12 | import java.io.FileOutputStream;
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.io.OutputStreamWriter;
|
---|
15 | import java.io.PrintWriter;
|
---|
16 | import java.nio.charset.StandardCharsets;
|
---|
17 | import java.text.MessageFormat;
|
---|
18 | import java.util.ArrayList;
|
---|
19 | import java.util.Properties;
|
---|
20 | import java.util.logging.Logger;
|
---|
21 |
|
---|
22 | import org.junit.Before;
|
---|
23 | import org.junit.BeforeClass;
|
---|
24 | import org.junit.Test;
|
---|
25 | import org.openstreetmap.josm.JOSMFixture;
|
---|
26 | import org.openstreetmap.josm.Main;
|
---|
27 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
28 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
29 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
30 | import org.openstreetmap.josm.data.osm.Node;
|
---|
31 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
32 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
33 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
34 | import org.openstreetmap.josm.data.osm.Way;
|
---|
35 | import org.openstreetmap.josm.gui.io.UploadStrategy;
|
---|
36 | import org.openstreetmap.josm.gui.io.UploadStrategySpecification;
|
---|
37 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
38 |
|
---|
39 | public class MultiFetchServerObjectReaderTest {
|
---|
40 | private static Logger logger = Logger.getLogger(MultiFetchServerObjectReader.class.getName());
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * builds a large data set to be used later for testing MULTI FETCH on the server
|
---|
44 | *
|
---|
45 | * @return a large data set
|
---|
46 | */
|
---|
47 | protected static DataSet buildTestDataSet() {
|
---|
48 | DataSet ds = new DataSet();
|
---|
49 | ds.setVersion("0.6");
|
---|
50 |
|
---|
51 | int numNodes = 1000;
|
---|
52 | int numWays = 1000;
|
---|
53 | int numRelations = 1000;
|
---|
54 |
|
---|
55 | ArrayList<Node> nodes = new ArrayList<>();
|
---|
56 | ArrayList<Way> ways = new ArrayList<>();
|
---|
57 |
|
---|
58 | // create a set of nodes
|
---|
59 | //
|
---|
60 | for (int i=0; i< numNodes; i++) {
|
---|
61 | Node n = new Node();
|
---|
62 | n.setCoor(new LatLon(-36.6,47.6));
|
---|
63 | n.put("name", "node-"+i);
|
---|
64 | ds.addPrimitive(n);
|
---|
65 | nodes.add(n);
|
---|
66 | }
|
---|
67 |
|
---|
68 | // create a set of ways, each with a random number of
|
---|
69 | // nodes
|
---|
70 | //
|
---|
71 | for (int i=0; i< numWays; i++) {
|
---|
72 | Way w = new Way();
|
---|
73 | int numNodesInWay = 2 + (int)Math.round(Math.random() * 5);
|
---|
74 | int start = (int)Math.round(Math.random() * numNodes);
|
---|
75 | for (int j = 0; j < numNodesInWay;j++) {
|
---|
76 | int idx = (start + j) % numNodes;
|
---|
77 | Node n = nodes.get(idx);
|
---|
78 | w.addNode(n);
|
---|
79 | }
|
---|
80 | w.put("name", "way-"+i);
|
---|
81 | ds.addPrimitive(w);
|
---|
82 | ways.add(w);
|
---|
83 | }
|
---|
84 |
|
---|
85 | // create a set of relations each with a random number of nodes,
|
---|
86 | // and ways
|
---|
87 | //
|
---|
88 | for (int i=0; i< numRelations; i++) {
|
---|
89 | Relation r = new Relation();
|
---|
90 | r.put("name", "relation-" +i);
|
---|
91 | int numNodesInRelation = (int)Math.round(Math.random() * 10);
|
---|
92 | int start = (int)Math.round(Math.random() * numNodes);
|
---|
93 | for (int j = 0; j < numNodesInRelation;j++) {
|
---|
94 | int idx = (start + j) % 500;
|
---|
95 | Node n = nodes.get(idx);
|
---|
96 | r.addMember(new RelationMember("role-" + j, n));
|
---|
97 | }
|
---|
98 | int numWaysInRelation = (int)Math.round(Math.random() * 10);
|
---|
99 | start = (int)Math.round(Math.random() * numWays);
|
---|
100 | for (int j = 0; j < numWaysInRelation;j++) {
|
---|
101 | int idx = (start + j) % 500;
|
---|
102 | Way w = ways.get(idx);
|
---|
103 | r.addMember(new RelationMember("role-" + j, w));
|
---|
104 | }
|
---|
105 | ds.addPrimitive(r);
|
---|
106 | }
|
---|
107 |
|
---|
108 | return ds;
|
---|
109 | }
|
---|
110 |
|
---|
111 | private static DataSet testDataSet;
|
---|
112 | private static Properties testProperties;
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * creates the dataset on the server.
|
---|
116 | *
|
---|
117 | * @param ds the data set
|
---|
118 | * @throws OsmTransferException
|
---|
119 | */
|
---|
120 | public static void createDataSetOnServer(DataSet ds) throws OsmTransferException {
|
---|
121 | logger.info("creating data set on the server ...");
|
---|
122 | ArrayList<OsmPrimitive> primitives = new ArrayList<>();
|
---|
123 | primitives.addAll(testDataSet.getNodes());
|
---|
124 | primitives.addAll(testDataSet.getWays());
|
---|
125 | primitives.addAll(testDataSet.getRelations());
|
---|
126 |
|
---|
127 | OsmServerWriter writer = new OsmServerWriter();
|
---|
128 | Changeset cs = new Changeset();
|
---|
129 | writer.uploadOsm(new UploadStrategySpecification().setStrategy(UploadStrategy.SINGLE_REQUEST_STRATEGY), primitives,cs,NullProgressMonitor.INSTANCE);
|
---|
130 | OsmApi.getOsmApi().closeChangeset(cs, NullProgressMonitor.INSTANCE);
|
---|
131 | }
|
---|
132 |
|
---|
133 | @BeforeClass
|
---|
134 | public static void init() throws OsmTransferException {
|
---|
135 | logger.info("initializing ...");
|
---|
136 | JOSMFixture.createFunctionalTestFixture().init();
|
---|
137 |
|
---|
138 | // don't use atomic upload, the test API server can't cope with large diff uploads
|
---|
139 | //
|
---|
140 | Main.pref.put("osm-server.atomic-upload", false);
|
---|
141 |
|
---|
142 | File dataSetCacheOutputFile = new File(System.getProperty("java.io.tmpdir"), MultiFetchServerObjectReaderTest.class.getName() + ".dataset");
|
---|
143 |
|
---|
144 | // make sure we don't upload to production
|
---|
145 | //
|
---|
146 | String url = OsmApi.getOsmApi().getBaseUrl().toLowerCase().trim();
|
---|
147 | if (url.startsWith("http://www.openstreetmap.org") || url.startsWith("http://api.openstreetmap.org")
|
---|
148 | || url.startsWith("https://www.openstreetmap.org") || url.startsWith("https://api.openstreetmap.org")) {
|
---|
149 | fail(MessageFormat.format("configured url ''{0}'' seems to be a productive url, aborting.", url));
|
---|
150 | }
|
---|
151 |
|
---|
152 | String p = System.getProperties().getProperty("useCachedDataset");
|
---|
153 | if (p != null && Boolean.parseBoolean(p.trim().toLowerCase())) {
|
---|
154 | logger.info(MessageFormat.format("property ''{0}'' set, using cached dataset", "useCachedDataset"));
|
---|
155 | return;
|
---|
156 | }
|
---|
157 |
|
---|
158 | logger.info(MessageFormat.format("property ''{0}'' not set to true, creating test dataset on the server. property is ''{1}''", "useCachedDataset", p));
|
---|
159 |
|
---|
160 | // build and upload the test data set
|
---|
161 | //
|
---|
162 | logger.info("creating test data set ....");
|
---|
163 | testDataSet = buildTestDataSet();
|
---|
164 | logger.info("uploading test data set ...");
|
---|
165 | createDataSetOnServer(testDataSet);
|
---|
166 |
|
---|
167 | try (
|
---|
168 | PrintWriter pw = new PrintWriter(
|
---|
169 | new OutputStreamWriter(new FileOutputStream(dataSetCacheOutputFile), StandardCharsets.UTF_8)
|
---|
170 | )) {
|
---|
171 | logger.info(MessageFormat.format("caching test data set in ''{0}'' ...", dataSetCacheOutputFile.toString()));
|
---|
172 | try (OsmWriter w = new OsmWriter(pw, false, testDataSet.getVersion())) {
|
---|
173 | w.header();
|
---|
174 | w.writeDataSources(testDataSet);
|
---|
175 | w.writeContent(testDataSet);
|
---|
176 | w.footer();
|
---|
177 | }
|
---|
178 | } catch(IOException e) {
|
---|
179 | fail(MessageFormat.format("failed to open file ''{0}'' for writing", dataSetCacheOutputFile.toString()));
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | private DataSet ds;
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Setup test.
|
---|
187 | */
|
---|
188 | @Before
|
---|
189 | public void setUp() throws IOException, IllegalDataException {
|
---|
190 | File f = new File(testProperties.getProperty("test.functional.tempdir"), MultiFetchServerObjectReaderTest.class.getName() + ".dataset");
|
---|
191 | logger.info(MessageFormat.format("reading cached dataset ''{0}''", f.toString()));
|
---|
192 | ds = new DataSet();
|
---|
193 | try (FileInputStream fis = new FileInputStream(f)) {
|
---|
194 | ds = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE);
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | @Test
|
---|
199 | public void testMultiGet10Nodes() throws OsmTransferException {
|
---|
200 | MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
|
---|
201 | ArrayList<Node> nodes = new ArrayList<>(ds.getNodes());
|
---|
202 | for (int i =0; i< 10; i++) {
|
---|
203 | reader.append(nodes.get(i));
|
---|
204 | }
|
---|
205 | DataSet out = reader.parseOsm(NullProgressMonitor.INSTANCE);
|
---|
206 | assertEquals(10, out.getNodes().size());
|
---|
207 | for (Node n1:out.getNodes()) {
|
---|
208 | Node n2 = (Node)ds.getPrimitiveById(n1);
|
---|
209 | assertNotNull(n2);
|
---|
210 | assertEquals(n2.get("name"),n2.get("name"));
|
---|
211 | }
|
---|
212 | assertTrue(reader.getMissingPrimitives().isEmpty());
|
---|
213 | }
|
---|
214 |
|
---|
215 | @Test
|
---|
216 | public void testMultiGet10Ways() throws OsmTransferException {
|
---|
217 | MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
|
---|
218 | ArrayList<Way> ways= new ArrayList<>(ds.getWays());
|
---|
219 | for (int i =0; i< 10; i++) {
|
---|
220 | reader.append(ways.get(i));
|
---|
221 | }
|
---|
222 | DataSet out = reader.parseOsm(NullProgressMonitor.INSTANCE);
|
---|
223 | assertEquals(10, out.getWays().size());
|
---|
224 | for (Way w1: out.getWays()) {
|
---|
225 | Way w2 = (Way)ds.getPrimitiveById(w1);
|
---|
226 | assertNotNull(w2);
|
---|
227 | assertEquals(w2.getNodesCount(), w1.getNodesCount());
|
---|
228 | assertEquals(w2.get("name"),w1.get("name"));
|
---|
229 | }
|
---|
230 | assertTrue(reader.getMissingPrimitives().isEmpty());
|
---|
231 | }
|
---|
232 |
|
---|
233 | @Test
|
---|
234 | public void testMultiGet10Relations() throws OsmTransferException {
|
---|
235 | MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
|
---|
236 | ArrayList<Relation> relations= new ArrayList<>(ds.getRelations());
|
---|
237 | for (int i =0; i< 10; i++) {
|
---|
238 | reader.append(relations.get(i));
|
---|
239 | }
|
---|
240 | DataSet out = reader.parseOsm(NullProgressMonitor.INSTANCE);
|
---|
241 | assertEquals(10, out.getRelations().size());
|
---|
242 | for (Relation r1: out.getRelations()) {
|
---|
243 | Relation r2 = (Relation)ds.getPrimitiveById(r1);
|
---|
244 | assertNotNull(r2);
|
---|
245 | assertEquals(r2.getMembersCount(), r1.getMembersCount());
|
---|
246 | assertEquals(r2.get("name"),r2.get("name"));
|
---|
247 | }
|
---|
248 | assertTrue(reader.getMissingPrimitives().isEmpty());
|
---|
249 | }
|
---|
250 |
|
---|
251 | @Test
|
---|
252 | public void testMultiGet800Nodes() throws OsmTransferException {
|
---|
253 | MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
|
---|
254 | ArrayList<Node> nodes = new ArrayList<>(ds.getNodes());
|
---|
255 | for (int i =0; i< 812; i++) {
|
---|
256 | reader.append(nodes.get(i));
|
---|
257 | }
|
---|
258 | DataSet out = reader.parseOsm(NullProgressMonitor.INSTANCE);
|
---|
259 | assertEquals(812, out.getNodes().size());
|
---|
260 | for (Node n1:out.getNodes()) {
|
---|
261 | Node n2 = (Node)ds.getPrimitiveById(n1);
|
---|
262 | assertNotNull(n2);
|
---|
263 | assertEquals(n2.get("name"),n2.get("name"));
|
---|
264 | }
|
---|
265 | assertTrue(reader.getMissingPrimitives().isEmpty());
|
---|
266 | }
|
---|
267 |
|
---|
268 | @Test
|
---|
269 | public void multiGetWithNonExistingNode() throws OsmTransferException {
|
---|
270 | MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
|
---|
271 | ArrayList<Node> nodes = new ArrayList<>(ds.getNodes());
|
---|
272 | for (int i =0; i< 10; i++) {
|
---|
273 | reader.append(nodes.get(i));
|
---|
274 | }
|
---|
275 | Node n = new Node(9999999);
|
---|
276 | reader.append(n); // doesn't exist
|
---|
277 | DataSet out = reader.parseOsm(NullProgressMonitor.INSTANCE);
|
---|
278 | assertEquals(10, out.getNodes().size());
|
---|
279 | for (Node n1:out.getNodes()) {
|
---|
280 | Node n2 = (Node)ds.getPrimitiveById(n1);
|
---|
281 | assertNotNull(n2);
|
---|
282 | assertEquals(n2.get("name"),n2.get("name"));
|
---|
283 | }
|
---|
284 | assertFalse(reader.getMissingPrimitives().isEmpty());
|
---|
285 | assertEquals(1, reader.getMissingPrimitives().size());
|
---|
286 | assertEquals(9999999, reader.getMissingPrimitives().iterator().next().getUniqueId());
|
---|
287 | }
|
---|
288 | }
|
---|