1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.geoimage;
|
---|
3 |
|
---|
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;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
9 |
|
---|
10 | import java.net.URL;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import org.junit.jupiter.api.AfterEach;
|
---|
14 | import org.junit.jupiter.api.Test;
|
---|
15 | import org.openstreetmap.josm.data.Bounds;
|
---|
16 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
---|
17 | import org.openstreetmap.josm.testutils.annotations.HTTP;
|
---|
18 | import org.openstreetmap.josm.testutils.annotations.Main;
|
---|
19 |
|
---|
20 | import com.github.tomakehurst.wiremock.WireMockServer;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Unit test of {@link WikimediaCommonsLoader}
|
---|
24 | */
|
---|
25 | @BasicPreferences
|
---|
26 | @HTTP
|
---|
27 | @Main
|
---|
28 | class WikimediaCommonsLoaderTest {
|
---|
29 | @AfterEach
|
---|
30 | void tearDown() {
|
---|
31 | if (ImageViewerDialog.hasInstance()) {
|
---|
32 | ImageViewerDialog.getInstance().destroy();
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Unit test of {@link WikimediaCommonsLoader}
|
---|
38 | *
|
---|
39 | * @throws Exception if an error occurs
|
---|
40 | */
|
---|
41 | @Test
|
---|
42 | void test() throws Exception {
|
---|
43 | WireMockServer wireMock = mockHttp();
|
---|
44 | wireMock.start();
|
---|
45 |
|
---|
46 | WikimediaCommonsLoader loader = new WikimediaCommonsLoader(new Bounds(47., 11., 48., 12.));
|
---|
47 | loader.apiUrl = wireMock.url("/w/api.php");
|
---|
48 | loader.realRun();
|
---|
49 | wireMock.stop();
|
---|
50 |
|
---|
51 | List<ImageEntry> images = loader.layer.getImages();
|
---|
52 | assertEquals(1, images.size());
|
---|
53 | ImageEntry image = images.get(0);
|
---|
54 | assertEquals("File:ISS053-E-105875_-_View_of_Earth.jpg", image.getDisplayName());
|
---|
55 | assertEquals(new URL("https://upload.wikimedia.org/wikipedia/commons/e/e8/ISS053-E-105875_-_View_of_Earth.jpg"),
|
---|
56 | image.getImageUrl());
|
---|
57 | }
|
---|
58 |
|
---|
59 | private static WireMockServer mockHttp() {
|
---|
60 | String xml =
|
---|
61 | "<api batchcomplete=\"\">\n" +
|
---|
62 | "<query>\n" +
|
---|
63 | "<geosearch>\n" +
|
---|
64 | "<gs pageid=\"102860206\" ns=\"6\" title=\"File:ISS053-E-105875 - View of Earth.jpg\" lat=\"47.2\" lon=\"11.3\" " +
|
---|
65 | "dist=\"0\" primary=\"\" type=\"camera\"/>\n" +
|
---|
66 | "</geosearch>\n" +
|
---|
67 | "</query>\n" +
|
---|
68 | "</api>";
|
---|
69 | final WireMockServer wireMock = new WireMockServer(options().dynamicPort());
|
---|
70 | wireMock.stubFor(get(urlEqualTo("/w/api.php?format=xml&action=query&list=geosearch" +
|
---|
71 | "&gsnamespace=6&gslimit=500&gsprop=type%7Cname&gsbbox=48.0%7C11.0%7C47.0%7C12.0"))
|
---|
72 | .willReturn(aResponse().withBody(xml)));
|
---|
73 | return wireMock;
|
---|
74 | }
|
---|
75 | }
|
---|