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