source: josm/trunk/test/unit/org/openstreetmap/josm/io/OverpassDownloadReaderTest.java @ 12557

Last change on this file since 12557 was 12557, checked in by Don-vip, 2 weeks ago

see #15102 - first batch of HTTP unit tests mocking, using WireMock 2.7.1

File size: 5.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
5import static com.github.tomakehurst.wiremock.client.WireMock.get;
6import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
7import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
8import static org.junit.Assert.assertEquals;
9import static org.junit.Assert.assertTrue;
10
11import java.util.regex.Matcher;
12
13import org.junit.Before;
14import org.junit.Rule;
15import org.junit.Test;
16import org.openstreetmap.josm.TestUtils;
17import org.openstreetmap.josm.data.Bounds;
18import org.openstreetmap.josm.io.OverpassDownloadReader.OverpassOutpoutFormat;
19import org.openstreetmap.josm.testutils.JOSMTestRules;
20import org.openstreetmap.josm.tools.OverpassTurboQueryWizard;
21import org.openstreetmap.josm.tools.Utils;
22
23import com.github.tomakehurst.wiremock.junit.WireMockRule;
24
25import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
26
27/**
28 * Unit tests of {@link OverpassDownloadReader} class.
29 */
30public class OverpassDownloadReaderTest {
31
32    /**
33     * Base test environment is enough
34     */
35    @Rule
36    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
37    public JOSMTestRules test = new JOSMTestRules().preferences();
38
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
55    private String getExpandedQuery(String search) {
56        final String query = OverpassTurboQueryWizard.getInstance().constructQuery(search);
57        final String request = new OverpassDownloadReader(new Bounds(1, 2, 3, 4), null, query)
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
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
88    /**
89     * Tests evaluating the extended query feature {@code geocodeArea}.
90     */
91    @Test
92    public void testGeocodeArea() {
93        stubNominatim("London");
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() {
112        stubNominatim("foo-bar-baz-does-not-exist");
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    }
116
117    /**
118     * Tests evaluating the overpass output format statements.
119     */
120    @Test
121    public void testOutputFormatStatement() {
122        for (OverpassOutpoutFormat oof : OverpassOutpoutFormat.values()) {
123            Matcher m = OverpassDownloadReader.OUTPUT_FORMAT_STATEMENT.matcher("[out:"+oof.getDirective()+"]");
124            assertTrue(m.matches());
125            assertEquals(oof.getDirective(), m.group(1));
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    }
138}
Note: See TracBrowser for help on using the repository browser.