[11003] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
[12557] | 4 | import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
---|
| 5 | import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
---|
| 6 | import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
---|
| 7 | import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
|
---|
[11003] | 8 | import static org.junit.Assert.assertEquals;
|
---|
[11916] | 9 | import static org.junit.Assert.assertTrue;
|
---|
[11003] | 10 |
|
---|
[11917] | 11 | import java.util.regex.Matcher;
|
---|
| 12 |
|
---|
[12557] | 13 | import org.junit.Before;
|
---|
[11003] | 14 | import org.junit.Rule;
|
---|
| 15 | import org.junit.Test;
|
---|
[12557] | 16 | import org.openstreetmap.josm.TestUtils;
|
---|
[11003] | 17 | import org.openstreetmap.josm.data.Bounds;
|
---|
[11916] | 18 | import org.openstreetmap.josm.io.OverpassDownloadReader.OverpassOutpoutFormat;
|
---|
[11003] | 19 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
| 20 | import org.openstreetmap.josm.tools.OverpassTurboQueryWizard;
|
---|
| 21 | import org.openstreetmap.josm.tools.Utils;
|
---|
| 22 |
|
---|
[12557] | 23 | import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
---|
| 24 |
|
---|
[11009] | 25 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
| 26 |
|
---|
[11003] | 27 | /**
|
---|
| 28 | * Unit tests of {@link OverpassDownloadReader} class.
|
---|
| 29 | */
|
---|
| 30 | public class OverpassDownloadReaderTest {
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * Base test environment is enough
|
---|
| 34 | */
|
---|
| 35 | @Rule
|
---|
| 36 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
[12557] | 37 | public JOSMTestRules test = new JOSMTestRules().preferences();
|
---|
[11003] | 38 |
|
---|
[12557] | 39 | /**
|
---|
| 40 | * HTTP mock.
|
---|
| 41 | */
|
---|
| 42 | @Rule
|
---|
| 43 | public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort().usingFilesUnderDirectory(TestUtils.getTestDataRoot()));
|
---|
| 44 |
|
---|
| 45 | private static final String NOMINATIM_URL_PATH = "/search?format=xml&q=";
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * Setup test.
|
---|
| 49 | */
|
---|
| 50 | @Before
|
---|
| 51 | public void setUp() {
|
---|
| 52 | NameFinder.NOMINATIM_URL_PROP.put("http://localhost:" + wireMockRule.port() + NOMINATIM_URL_PATH);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[11003] | 55 | private String getExpandedQuery(String search) {
|
---|
| 56 | final String query = OverpassTurboQueryWizard.getInstance().constructQuery(search);
|
---|
[12557] | 57 | final String request = new OverpassDownloadReader(new Bounds(1, 2, 3, 4), null, query)
|
---|
[11003] | 58 | .getRequestForBbox(1, 2, 3, 4)
|
---|
| 59 | .substring("interpreter?data=".length());
|
---|
| 60 | return Utils.decodeUrl(request);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Tests evaluating the extended query feature {@code bbox}.
|
---|
| 65 | */
|
---|
| 66 | @Test
|
---|
| 67 | public void testBbox() {
|
---|
| 68 | final String query = getExpandedQuery("amenity=drinking_water");
|
---|
| 69 | assertEquals("" +
|
---|
| 70 | "[out:xml][timeout:25][bbox:2.0,1.0,4.0,3.0];\n" +
|
---|
| 71 | "(\n" +
|
---|
| 72 | " node[\"amenity\"=\"drinking_water\"];\n" +
|
---|
| 73 | " way[\"amenity\"=\"drinking_water\"];\n" +
|
---|
| 74 | " relation[\"amenity\"=\"drinking_water\"];\n" +
|
---|
| 75 | ");\n" +
|
---|
| 76 | "(._;>;);\n" +
|
---|
| 77 | "out meta;", query);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[12557] | 80 | private void stubNominatim(String query) {
|
---|
| 81 | wireMockRule.stubFor(get(urlEqualTo(NOMINATIM_URL_PATH + query))
|
---|
| 82 | .willReturn(aResponse()
|
---|
| 83 | .withStatus(200)
|
---|
| 84 | .withHeader("Content-Type", "text/xml")
|
---|
| 85 | .withBodyFile("nominatim/" + query + ".xml")));
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[11003] | 88 | /**
|
---|
| 89 | * Tests evaluating the extended query feature {@code geocodeArea}.
|
---|
| 90 | */
|
---|
| 91 | @Test
|
---|
| 92 | public void testGeocodeArea() {
|
---|
[12557] | 93 | stubNominatim("London");
|
---|
[11003] | 94 | final String query = getExpandedQuery("amenity=drinking_water in London");
|
---|
| 95 | assertEquals("" +
|
---|
| 96 | "[out:xml][timeout:25];\n" +
|
---|
| 97 | "area(3600065606)->.searchArea;\n" +
|
---|
| 98 | "(\n" +
|
---|
| 99 | " node[\"amenity\"=\"drinking_water\"](area.searchArea);\n" +
|
---|
| 100 | " way[\"amenity\"=\"drinking_water\"](area.searchArea);\n" +
|
---|
| 101 | " relation[\"amenity\"=\"drinking_water\"](area.searchArea);\n" +
|
---|
| 102 | ");\n" +
|
---|
| 103 | "(._;>;);\n" +
|
---|
| 104 | "out meta;", query);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
| 108 | * Tests evaluating the extended query feature {@code geocodeArea}.
|
---|
| 109 | */
|
---|
| 110 | @Test
|
---|
| 111 | public void testGeocodeUnknownArea() {
|
---|
[12557] | 112 | stubNominatim("foo-bar-baz-does-not-exist");
|
---|
[11003] | 113 | final String query = OverpassDownloadReader.expandExtendedQueries("{{geocodeArea:foo-bar-baz-does-not-exist}}");
|
---|
| 114 | assertEquals("// Failed to evaluate {{geocodeArea:foo-bar-baz-does-not-exist}}\n", query);
|
---|
| 115 | }
|
---|
[11916] | 116 |
|
---|
| 117 | /**
|
---|
| 118 | * Tests evaluating the overpass output format statements.
|
---|
| 119 | */
|
---|
| 120 | @Test
|
---|
| 121 | public void testOutputFormatStatement() {
|
---|
| 122 | for (OverpassOutpoutFormat oof : OverpassOutpoutFormat.values()) {
|
---|
[11917] | 123 | Matcher m = OverpassDownloadReader.OUTPUT_FORMAT_STATEMENT.matcher("[out:"+oof.getDirective()+"]");
|
---|
| 124 | assertTrue(m.matches());
|
---|
| 125 | assertEquals(oof.getDirective(), m.group(1));
|
---|
[11916] | 126 | }
|
---|
| 127 |
|
---|
| 128 | assertTrue(OverpassDownloadReader.OUTPUT_FORMAT_STATEMENT.matcher(
|
---|
| 129 | "[out:pbf][timeout:25][bbox:{{bbox}}];\n" +
|
---|
| 130 | "(\n" +
|
---|
| 131 | " node[\"amenity\"=\"pharmacy\"];\n" +
|
---|
| 132 | " way[\"amenity\"=\"pharmacy\"];\n" +
|
---|
| 133 | " relation[\"amenity\"=\"pharmacy\"];\n" +
|
---|
| 134 | ");\n" +
|
---|
| 135 | "(._;>;);\n" +
|
---|
| 136 | "out meta;").matches());
|
---|
| 137 | }
|
---|
[11009] | 138 | }
|
---|