1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.wikipedia.io;
|
---|
3 |
|
---|
4 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
5 | import org.junit.Rule;
|
---|
6 | import org.junit.Test;
|
---|
7 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
8 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
9 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
10 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
11 |
|
---|
12 | import java.io.ByteArrayInputStream;
|
---|
13 | import java.io.InputStream;
|
---|
14 | import java.io.UnsupportedEncodingException;
|
---|
15 | import java.nio.charset.StandardCharsets;
|
---|
16 | import java.util.Arrays;
|
---|
17 | import java.util.List;
|
---|
18 |
|
---|
19 | import static org.hamcrest.CoreMatchers.is;
|
---|
20 | import static org.junit.Assert.assertThat;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Unit tests of {@link WikosmDownloadReader} class.
|
---|
24 | */
|
---|
25 | public class WikosmDownloadReaderTest {
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Base test environment is enough
|
---|
29 | */
|
---|
30 | @Rule
|
---|
31 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
32 | public JOSMTestRules test = new JOSMTestRules().preferences();
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Tests point generation
|
---|
36 | */
|
---|
37 | @Test
|
---|
38 | public void testPoint() throws UnsupportedEncodingException {
|
---|
39 | assertThat(WikosmDownloadReader.point(9.5, 47.16),
|
---|
40 | is("\"Point(9.5 47.16)\"^^geo:wktLiteral"));
|
---|
41 | assertThat(WikosmDownloadReader.boxParams(1.1, 2.2, 3.3, 4.4),
|
---|
42 | is("\nbd:serviceParam wikibase:cornerWest \"Point(1.1 2.2)\"^^geo:wktLiteral." +
|
---|
43 | "\nbd:serviceParam wikibase:cornerEast \"Point(3.3 4.4)\"^^geo:wktLiteral.\n"));
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Tests server response parsing
|
---|
48 | */
|
---|
49 | @Test
|
---|
50 | public void testIdParsing() throws UnsupportedEncodingException {
|
---|
51 | String json = String.join("\n",
|
---|
52 | "{\"results\":{\"bindings\":[",
|
---|
53 | "{\"a\":{\"type\": \"uri\", \"value\": \"https://www.openstreetmap.org/node/12345\"}},",
|
---|
54 | "{\"a\":{\"type\": \"uri\", \"value\": \"https://www.openstreetmap.org/way/1234512345\"}},",
|
---|
55 | "{\"a\":{\"type\": \"uri\", \"value\": \"https://www.openstreetmap.org/relation/98765\"}}",
|
---|
56 | "]}}"
|
---|
57 | );
|
---|
58 |
|
---|
59 | InputStream stream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8.name()));
|
---|
60 | List<PrimitiveId> actual = WikosmDownloadReader.getPrimitiveIds(stream);
|
---|
61 |
|
---|
62 | List<PrimitiveId> expected = Arrays.asList(new PrimitiveId[]{
|
---|
63 | new SimplePrimitiveId(12345, OsmPrimitiveType.NODE),
|
---|
64 | new SimplePrimitiveId(1234512345, OsmPrimitiveType.WAY),
|
---|
65 | new SimplePrimitiveId(98765, OsmPrimitiveType.RELATION),
|
---|
66 | });
|
---|
67 |
|
---|
68 | assertThat(actual, is(expected));
|
---|
69 | }
|
---|
70 | }
|
---|