1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.imagery;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
7 |
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.nio.file.Files;
|
---|
10 | import java.nio.file.Paths;
|
---|
11 | import java.util.Collections;
|
---|
12 |
|
---|
13 | import org.junit.jupiter.api.Test;
|
---|
14 | import org.openstreetmap.gui.jmapviewer.interfaces.TemplatedTileSource;
|
---|
15 | import org.openstreetmap.josm.TestUtils;
|
---|
16 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
17 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
18 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
19 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
20 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
---|
21 | import org.openstreetmap.josm.testutils.annotations.BasicWiremock;
|
---|
22 | import org.openstreetmap.josm.testutils.annotations.Projection;
|
---|
23 |
|
---|
24 | import com.github.tomakehurst.wiremock.WireMockServer;
|
---|
25 | import com.github.tomakehurst.wiremock.client.WireMock;
|
---|
26 |
|
---|
27 | @BasicPreferences(true)
|
---|
28 | @BasicWiremock
|
---|
29 | @Projection
|
---|
30 | class WMSEndpointTileSourceTest implements TileSourceTest {
|
---|
31 | @BasicWiremock
|
---|
32 | WireMockServer tileServer;
|
---|
33 |
|
---|
34 | private void basicMock() {
|
---|
35 | final byte[] response = assertDoesNotThrow(() -> Files.readAllBytes(
|
---|
36 | Paths.get(TestUtils.getTestDataRoot() + "wms/geofabrik-osm-inspector.xml")));
|
---|
37 | tileServer.stubFor(
|
---|
38 | WireMock.get(WireMock.urlEqualTo("/capabilities?SERVICE=WMS&REQUEST=GetCapabilities"))
|
---|
39 | .willReturn(WireMock.aResponse().withBody(response))
|
---|
40 | );
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public ImageryInfo getInfo() {
|
---|
45 | this.basicMock();
|
---|
46 | final ImageryInfo info = new ImageryInfo("WMSEndpointTileSourceTest");
|
---|
47 | info.setExtendedUrl(tileServer.url("/capabilities"));
|
---|
48 | info.setDefaultLayers(Collections.singletonList(new DefaultLayer(ImageryType.WMS_ENDPOINT,
|
---|
49 | "single_node_in_way", "default", null)));
|
---|
50 | info.setImageryType(ImageryType.WMS_ENDPOINT);
|
---|
51 | return info;
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public TemplatedTileSource getTileSource(ImageryInfo info) {
|
---|
56 | return new WMSEndpointTileSource(info, ProjectionRegistry.getProjection());
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Test
|
---|
60 | void testDefaultLayerSetInMaps() throws Exception {
|
---|
61 |
|
---|
62 | tileServer.stubFor(
|
---|
63 | WireMock.get(WireMock.urlEqualTo("/capabilities?SERVICE=WMS&REQUEST=GetCapabilities"))
|
---|
64 | .willReturn(
|
---|
65 | WireMock.aResponse()
|
---|
66 | .withBody(Files.readAllBytes(Paths.get(TestUtils.getTestDataRoot() + "wms/geofabrik-osm-inspector.xml")))
|
---|
67 | )
|
---|
68 | );
|
---|
69 |
|
---|
70 | tileServer.stubFor(WireMock.get(WireMock.urlEqualTo("//maps")).willReturn(WireMock.aResponse().withBody(
|
---|
71 | "<?xml version='1.0' encoding='UTF-8'?>\n" +
|
---|
72 | "<imagery xmlns=\"http://josm.openstreetmap.de/maps-1.0\">\n" +
|
---|
73 | "<entry>\n" +
|
---|
74 | "<name>OSM Inspector: Geometry</name>\n" +
|
---|
75 | "<id>OSM_Inspector-Geometry</id>\n" +
|
---|
76 | "<type>wms_endpoint</type>\n" +
|
---|
77 | "<url><![CDATA[" + tileServer.url("/capabilities") + "]]></url>\n" +
|
---|
78 | "<icon>data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAAsSAAALEgHS3X78AAAB5UlEQVQ4y4WTwWsTURDG" +
|
---|
79 | "fy8W1yYmXZOqtGJJFyGw6KF7CEigwYuS0kthrYUi4i0iORS9BU9hQdA/ILcixVBrwENKLz1FUBB0wWOwYFAqxUNYTZq6BfM8yC5d05iBObz3vfnmm3kz4sqDh/zP" +
|
---|
80 | "7szdlG5I+Of1zQ1xFA8xxI4GH2cjg4Cl+UUJcC4SJq6c7FPkKRlIoPQk0+NnuDwxHrhvuYd83+8OVuBlHouE/eDXzW8+/qO9DyHB0vyiVHoy2INSNiPdeg23XuPs" +
|
---|
81 | "3icmIoofPKXGmFJjjEUjgf4EFNi2TT6fJ5FI0Gg0ePrkMRfnbvn41QsJgEAJAQUdbYZyuQxAcvoSpmnydesFAF+cn8f2KUCw/fGt6GgzWJbF706bVCoFwGxyktnk" +
|
---|
82 | "5N8kB79QepL1zQ3xbOulCJWyGbkQHZWlbEZ6JIZhBDI1nQ5Np8P2zi4t9zAwGyNe3QALti11XSedTvsPYrEY73f3Bk+irusAnI6qrNy7z43sNUbFCQC6LYdCoYBb" +
|
---|
83 | "r/k1/2sh690HUalUaH7eIRxXA+6RFItF3HqN6+dP9REIb5lK2Yy0bdsHDMMgl8vRbTkAhOMqlmVhmibLq2ui7xsf1d+IV+0D3zVNw7KsPiXVapXnd2/Lodu4vLom" +
|
---|
84 | "TNMcSvIHY6bDkqJtEqIAAAAASUVORK5CYII=</icon>\n" +
|
---|
85 | "<attribution-text mandatory=\"true\">© Geofabrik GmbH, OpenStreetMap contributors, CC-BY-SA</attribution-text>\n" +
|
---|
86 | "<attribution-url>http://tools.geofabrik.de/osmi/</attribution-url>\n" +
|
---|
87 | "<max-zoom>18</max-zoom>\n" +
|
---|
88 | "<valid-georeference>true</valid-georeference>\n" +
|
---|
89 | "<default-layers>" +
|
---|
90 | "<layer name=\"single_node_in_way\" style=\"default\" />" +
|
---|
91 | "</default-layers>" +
|
---|
92 | "</entry>\n" +
|
---|
93 | "</imagery>"
|
---|
94 | )));
|
---|
95 |
|
---|
96 | Config.getPref().putList("imagery.layers.sites", Collections.singletonList(tileServer.url("//maps")));
|
---|
97 | ImageryLayerInfo.instance.loadDefaults(true, null, false);
|
---|
98 | assertEquals(1, ImageryLayerInfo.instance.getDefaultLayers().size());
|
---|
99 | ImageryInfo wmsImageryInfo = ImageryLayerInfo.instance.getDefaultLayers().get(0);
|
---|
100 | assertEquals("single_node_in_way", wmsImageryInfo.getDefaultLayers().get(0).getLayerName());
|
---|
101 | WMSEndpointTileSource tileSource = new WMSEndpointTileSource(wmsImageryInfo, ProjectionRegistry.getProjection());
|
---|
102 | tileSource.initProjection(Projections.getProjectionByCode("EPSG:3857"));
|
---|
103 | assertEquals("https://tools.geofabrik.de/osmi/views/geometry/wxs?FORMAT=image/png&TRANSPARENT=TRUE&VERSION=1.3.0&SERVICE=WMS&"
|
---|
104 | + "REQUEST=GetMap&LAYERS=single_node_in_way&STYLES=default&"
|
---|
105 | + "CRS=EPSG:3857&WIDTH=512&HEIGHT=512&"
|
---|
106 | + "BBOX=20037508.3427893,-60112525.0283678,60112525.0283678,-20037508.3427893", tileSource.getTileUrl(1, 1, 1));
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Test
|
---|
110 | void testCustomHeadersServerSide() throws IOException {
|
---|
111 | tileServer.stubFor(
|
---|
112 | WireMock.get(WireMock.urlEqualTo("/capabilities?SERVICE=WMS&REQUEST=GetCapabilities"))
|
---|
113 | .willReturn(
|
---|
114 | WireMock.aResponse()
|
---|
115 | .withBody(Files.readAllBytes(Paths.get(TestUtils.getTestDataRoot() + "wms/webatlas.no.xml")))
|
---|
116 | )
|
---|
117 | );
|
---|
118 |
|
---|
119 | tileServer.stubFor(WireMock.get(WireMock.urlEqualTo("//maps")).willReturn(WireMock.aResponse().withBody(
|
---|
120 | "<?xml version='1.0' encoding='UTF-8'?>\n" +
|
---|
121 | "<imagery xmlns=\"http://josm.openstreetmap.de/maps-1.0\">\n" +
|
---|
122 | " <entry>\n" +
|
---|
123 | " <name>Norway Orthophoto (historic)</name>\n" +
|
---|
124 | " <name lang=\"nb\">Norge i Bilder (historisk)</name>\n" +
|
---|
125 | " <id>geovekst-nib-historic</id>\n" +
|
---|
126 | " <type>wms_endpoint</type>\n" +
|
---|
127 | " <country-code>NO</country-code>\n" +
|
---|
128 | " <description lang=\"en\">Historic Norwegian orthophotos and maps, courtesy of Geovekst and Norkart.</description>\n" +
|
---|
129 | " <url><![CDATA[" + tileServer.url("/capabilities?SERVICE=WMS&REQUEST=GetCapabilities") + "]]></url>\n" +
|
---|
130 | " <custom-http-header header-name=\"X-WAAPI-TOKEN\" header-value=\"b8e36d51-119a-423b-b156-d744d54123d5\" />\n" +
|
---|
131 | " <attribution-text>© Geovekst</attribution-text>\n" +
|
---|
132 | " <attribution-url>https://www.norgeibilder.no/</attribution-url>\n" +
|
---|
133 | " <permission-ref>https://forum.openstreetmap.org/viewtopic.php?id=62083</permission-ref>\n" +
|
---|
134 | " <icon>https://register.geonorge.no/data/organizations/_L_norgeibilder96x96.png</icon>\n" +
|
---|
135 | " <max-zoom>21</max-zoom>\n" +
|
---|
136 | " <valid-georeference>true</valid-georeference>\n" +
|
---|
137 | "</entry>\n" +
|
---|
138 | "</imagery>"
|
---|
139 | )));
|
---|
140 |
|
---|
141 | Config.getPref().putList("imagery.layers.sites", Collections.singletonList(tileServer.url("//maps")));
|
---|
142 | ImageryLayerInfo.instance.loadDefaults(true, null, false);
|
---|
143 | ImageryInfo wmsImageryInfo = ImageryLayerInfo.instance.getDefaultLayers().get(0);
|
---|
144 | wmsImageryInfo.setDefaultLayers(Collections.singletonList(new DefaultLayer(ImageryType.WMS_ENDPOINT, "historiske-ortofoto", "", "")));
|
---|
145 | WMSEndpointTileSource tileSource = new WMSEndpointTileSource(wmsImageryInfo, ProjectionRegistry.getProjection());
|
---|
146 | tileSource.initProjection(Projections.getProjectionByCode("EPSG:3857"));
|
---|
147 | assertEquals("b8e36d51-119a-423b-b156-d744d54123d5", wmsImageryInfo.getCustomHttpHeaders().get("X-WAAPI-TOKEN"));
|
---|
148 | assertEquals("b8e36d51-119a-423b-b156-d744d54123d5", tileSource.getHeaders().get("X-WAAPI-TOKEN"));
|
---|
149 | assertTrue(wmsImageryInfo.isGeoreferenceValid());
|
---|
150 | tileServer.verify(
|
---|
151 | WireMock.getRequestedFor(WireMock.urlEqualTo("/capabilities?SERVICE=WMS&REQUEST=GetCapabilities"))
|
---|
152 | .withHeader("X-WAAPI-TOKEN", WireMock.equalTo("b8e36d51-119a-423b-b156-d744d54123d5")));
|
---|
153 | assertEquals("http://waapi.webatlas.no/wms-orto-hist/?"
|
---|
154 | + "FORMAT=image/png&"
|
---|
155 | + "TRANSPARENT=TRUE&"
|
---|
156 | + "VERSION=1.1.1&"
|
---|
157 | + "SERVICE=WMS&"
|
---|
158 | + "REQUEST=GetMap&"
|
---|
159 | + "LAYERS=historiske-ortofoto&"
|
---|
160 | + "STYLES=&"
|
---|
161 | + "SRS=EPSG:3857&"
|
---|
162 | + "WIDTH=512&"
|
---|
163 | + "HEIGHT=512&"
|
---|
164 | + "BBOX=20037508.3427893,-60112525.0283678,60112525.0283678,-20037508.3427893",
|
---|
165 | tileSource.getTileUrl(1, 1, 1));
|
---|
166 | }
|
---|
167 | }
|
---|