1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.imagery;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertNull;
|
---|
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.List;
|
---|
12 |
|
---|
13 | import org.junit.jupiter.api.Test;
|
---|
14 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
15 | import org.openstreetmap.josm.TestUtils;
|
---|
16 | import org.openstreetmap.josm.io.imagery.WMSImagery.WMSGetCapabilitiesException;
|
---|
17 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
18 | import org.openstreetmap.josm.testutils.annotations.BasicWiremock;
|
---|
19 |
|
---|
20 | import com.github.tomakehurst.wiremock.WireMockServer;
|
---|
21 | import com.github.tomakehurst.wiremock.client.WireMock;
|
---|
22 |
|
---|
23 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Unit tests of {@link WMSImagery} class.
|
---|
27 | */
|
---|
28 | @BasicWiremock
|
---|
29 | class WMSImageryTest {
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Setup test
|
---|
33 | */
|
---|
34 | @RegisterExtension
|
---|
35 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
36 | JOSMTestRules test = new JOSMTestRules().projection();
|
---|
37 |
|
---|
38 | @BasicWiremock
|
---|
39 | WireMockServer tileServer;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Unit test of {@code WMSImagery.WMSGetCapabilitiesException} class
|
---|
43 | */
|
---|
44 | @Test
|
---|
45 | void testWMSGetCapabilitiesException() {
|
---|
46 | Exception cause = new Exception("test");
|
---|
47 | WMSGetCapabilitiesException exc = new WMSGetCapabilitiesException(cause, "bar");
|
---|
48 | assertEquals(cause, exc.getCause());
|
---|
49 | assertEquals("bar", exc.getIncomingData());
|
---|
50 | exc = new WMSGetCapabilitiesException("foo", "bar");
|
---|
51 | assertEquals("foo", exc.getMessage());
|
---|
52 | assertEquals("bar", exc.getIncomingData());
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Non-regression test for bug #15730.
|
---|
57 | * @throws IOException if any I/O error occurs
|
---|
58 | * @throws WMSGetCapabilitiesException never
|
---|
59 | */
|
---|
60 | @Test
|
---|
61 | void testTicket15730() throws IOException, WMSGetCapabilitiesException {
|
---|
62 | tileServer.stubFor(WireMock.get(WireMock.anyUrl()).willReturn(WireMock.aResponse().withBody(
|
---|
63 | Files.readAllBytes(Paths.get(TestUtils.getRegressionDataDir(15730), "capabilities.xml"))
|
---|
64 | )));
|
---|
65 |
|
---|
66 | WMSImagery wms = new WMSImagery(tileServer.url("capabilities.xml"));
|
---|
67 | assertEquals(1, wms.getLayers().size());
|
---|
68 | assertTrue(wms.getLayers().get(0).getAbstract().startsWith("South Carolina NAIP Imagery 2017 Resolution: 100CM "));
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Test
|
---|
72 | void testNestedLayers() throws Exception {
|
---|
73 | tileServer.stubFor(WireMock.get(WireMock.anyUrl()).willReturn(WireMock.aResponse().withBody(
|
---|
74 | Files.readAllBytes(Paths.get(TestUtils.getTestDataRoot() + "wms/mapa-um-warszawa-pl.xml")))));
|
---|
75 | WMSImagery wmsi = new WMSImagery(tileServer.url("/serwis"));
|
---|
76 | assertEquals(1, wmsi.getLayers().size());
|
---|
77 | assertEquals("Server WMS m.st. Warszawy", wmsi.getLayers().get(0).toString());
|
---|
78 | assertEquals(202, wmsi.getLayers().get(0).getChildren().size());
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Non-regression test for bug #16248.
|
---|
83 | * @throws IOException if any I/O error occurs
|
---|
84 | * @throws WMSGetCapabilitiesException never
|
---|
85 | */
|
---|
86 | @Test
|
---|
87 | void testTicket16248() throws IOException, WMSGetCapabilitiesException {
|
---|
88 | byte[] capabilities = Files.readAllBytes(Paths.get(TestUtils.getRegressionDataFile(16248, "capabilities.xml")));
|
---|
89 | tileServer.stubFor(WireMock.get(WireMock.anyUrl()).willReturn(WireMock.aResponse().withBody(capabilities)));
|
---|
90 | WMSImagery wms = new WMSImagery(tileServer.url("any"));
|
---|
91 | assertEquals("http://wms.hgis.cartomatic.pl/topo/3857/m25k?", wms.buildRootUrl());
|
---|
92 | assertEquals("wms.hgis.cartomatic.pl", wms.getLayers().get(0).getName());
|
---|
93 | assertEquals("http://wms.hgis.cartomatic.pl/topo/3857/m25k?FORMAT=image/png&TRANSPARENT=TRUE&VERSION=1.3.0&SERVICE=WMS&REQUEST=GetMap&"
|
---|
94 | + "LAYERS=wms.hgis.cartomatic.pl&STYLES=&CRS={proj}&WIDTH={width}&HEIGHT={height}&BBOX={bbox}",
|
---|
95 | wms.buildGetMapUrl(wms.getLayers(), (List<String>) null, true));
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Non-regression test for bug #19193.
|
---|
100 | * @throws IOException if any I/O error occurs
|
---|
101 | * @throws WMSGetCapabilitiesException never
|
---|
102 | */
|
---|
103 | @Test
|
---|
104 | void testTicket19193() throws IOException, WMSGetCapabilitiesException {
|
---|
105 | byte[] capabilities = Files.readAllBytes(Paths.get(TestUtils.getRegressionDataFile(19193, "capabilities.xml")));
|
---|
106 | tileServer.stubFor(WireMock.get(WireMock.anyUrl()).willReturn(WireMock.aResponse().withBody(capabilities)));
|
---|
107 | WMSImagery wms = new WMSImagery(tileServer.url("any"));
|
---|
108 | assertEquals("https://inspire.brandenburg.de/services/gn_alkis_wms?", wms.buildRootUrl());
|
---|
109 | assertEquals(1, wms.getLayers().size());
|
---|
110 | assertNull(wms.getLayers().get(0).getName());
|
---|
111 | assertEquals("INSPIRE GN ALKIS BB", wms.getLayers().get(0).getTitle());
|
---|
112 | assertEquals("https://inspire.brandenburg.de/services/gn_alkis_wms?FORMAT=image/png&TRANSPARENT=TRUE&VERSION=1.3.0&"
|
---|
113 | + "SERVICE=WMS&REQUEST=GetMap&LAYERS=null&STYLES=&CRS={proj}&WIDTH={width}&HEIGHT={height}&BBOX={bbox}",
|
---|
114 | wms.buildGetMapUrl(wms.getLayers(), (List<String>) null, true));
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Regression test for bug #16333 (null style name)
|
---|
119 | * @throws IOException if any I/O error occurs
|
---|
120 | * @throws WMSGetCapabilitiesException never
|
---|
121 | */
|
---|
122 | @Test
|
---|
123 | void testTicket16333() throws IOException, WMSGetCapabilitiesException {
|
---|
124 | tileServer.stubFor(
|
---|
125 | WireMock.get(WireMock.anyUrl())
|
---|
126 | .willReturn(WireMock.aResponse().withBody(
|
---|
127 | Files.readAllBytes(Paths.get(TestUtils.getRegressionDataFile(16333, "capabilities.xml")))
|
---|
128 | ))
|
---|
129 | );
|
---|
130 | WMSImagery wms = new WMSImagery(tileServer.url("any"));
|
---|
131 | assertEquals("https://duinoord.xs4all.nl/geoserver/ows?SERVICE=WMS&", wms.buildRootUrl());
|
---|
132 | assertNull(wms.getLayers().get(0).getName());
|
---|
133 | assertEquals("", wms.getLayers().get(0).getTitle());
|
---|
134 |
|
---|
135 | assertEquals("bag:Matching Street", wms.getLayers().get(0).getChildren().get(0).getName());
|
---|
136 | assertEquals("Dichtstbijzijnde straat", wms.getLayers().get(0).getChildren().get(0).getTitle());
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Test
|
---|
140 | void testForTitleWithinAttribution_ticket16940() throws IOException, WMSGetCapabilitiesException {
|
---|
141 | tileServer.stubFor(
|
---|
142 | WireMock.get(WireMock.anyUrl())
|
---|
143 | .willReturn(WireMock.aResponse().withBody(
|
---|
144 | Files.readAllBytes(Paths.get(TestUtils.getRegressionDataFile(16940, "capabilities.xml")))
|
---|
145 | ))
|
---|
146 | );
|
---|
147 | WMSImagery wms = new WMSImagery(tileServer.url("any"));
|
---|
148 | assertEquals("Hipsográfico", wms.getLayers().stream().findFirst().get().getTitle());
|
---|
149 | }
|
---|
150 | }
|
---|