[10116] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.io.remotecontrol.handler;
|
---|
| 3 |
|
---|
[16589] | 4 | import static org.hamcrest.CoreMatchers.hasItem;
|
---|
[16618] | 5 | import static org.hamcrest.MatcherAssert.assertThat;
|
---|
[17275] | 6 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
[16618] | 7 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
---|
| 8 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
---|
[16588] | 9 |
|
---|
[16589] | 10 | import java.util.Arrays;
|
---|
| 11 | import java.util.List;
|
---|
| 12 |
|
---|
[17275] | 13 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
| 14 | import org.junit.jupiter.api.Test;
|
---|
[16588] | 15 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
[10116] | 16 | import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerBadRequestException;
|
---|
[12558] | 17 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
[10116] | 18 |
|
---|
[12558] | 19 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
| 20 |
|
---|
[10116] | 21 | /**
|
---|
| 22 | * Unit tests of {@link ImageryHandler} class.
|
---|
| 23 | */
|
---|
[17275] | 24 | class ImageryHandlerTest {
|
---|
[10116] | 25 | /**
|
---|
| 26 | * Setup test.
|
---|
| 27 | */
|
---|
[17275] | 28 | @RegisterExtension
|
---|
[12558] | 29 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
[14138] | 30 | public JOSMTestRules test = new JOSMTestRules();
|
---|
[10116] | 31 |
|
---|
[10973] | 32 | private static ImageryHandler newHandler(String url) throws RequestHandlerBadRequestException {
|
---|
[10116] | 33 | ImageryHandler req = new ImageryHandler();
|
---|
| 34 | if (url != null)
|
---|
| 35 | req.setUrl(url);
|
---|
| 36 | return req;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * Unit test for bad request - no param.
|
---|
| 41 | */
|
---|
| 42 | @Test
|
---|
[17275] | 43 | void testBadRequestNoParam() {
|
---|
[16618] | 44 | Exception e = assertThrows(RequestHandlerBadRequestException.class, () -> newHandler(null).handle());
|
---|
| 45 | assertEquals("Parameter must not be null", e.getMessage());
|
---|
| 46 |
|
---|
[10116] | 47 | }
|
---|
| 48 |
|
---|
| 49 | /**
|
---|
| 50 | * Unit test for bad request - invalid URL.
|
---|
| 51 | */
|
---|
| 52 | @Test
|
---|
[17275] | 53 | void testBadRequestInvalidUrl() {
|
---|
[16618] | 54 | Exception e = assertThrows(RequestHandlerBadRequestException.class, () -> newHandler("invalid_url").handle());
|
---|
| 55 | assertEquals("The following keys are mandatory, but have not been provided: url", e.getMessage());
|
---|
[10116] | 56 | }
|
---|
| 57 |
|
---|
| 58 | /**
|
---|
| 59 | * Unit test for bad request - incomplete URL.
|
---|
| 60 | */
|
---|
| 61 | @Test
|
---|
[17275] | 62 | void testBadRequestIncompleteUrl() {
|
---|
[16618] | 63 | Exception e = assertThrows(RequestHandlerBadRequestException.class, () -> newHandler("https://localhost").handle());
|
---|
| 64 | assertEquals("The following keys are mandatory, but have not been provided: url", e.getMessage());
|
---|
[10116] | 65 | }
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * Unit test for nominal request - local data file.
|
---|
| 69 | */
|
---|
| 70 | @Test
|
---|
[17275] | 71 | void testNominalRequest() {
|
---|
[16618] | 72 | assertDoesNotThrow(() -> newHandler("https://localhost?url=foo").handle());
|
---|
[10116] | 73 | }
|
---|
[16588] | 74 |
|
---|
| 75 | /**
|
---|
[16589] | 76 | * Unit test for {@link ImageryHandler#getOptionalParams()}
|
---|
| 77 | * @throws Exception if any error occurs
|
---|
| 78 | */
|
---|
| 79 | @Test
|
---|
[17275] | 80 | void testOptionalParams() throws Exception {
|
---|
[16589] | 81 | List<String> optionalParams = Arrays.asList(newHandler("").getOptionalParams());
|
---|
| 82 | assertThat(optionalParams, hasItem("type"));
|
---|
| 83 | assertThat(optionalParams, hasItem("min-zoom"));
|
---|
| 84 | assertThat(optionalParams, hasItem("max-zoom"));
|
---|
| 85 | assertThat(optionalParams, hasItem("category"));
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
[16588] | 89 | * Unit test for {@link ImageryHandler#buildImageryInfo()}
|
---|
| 90 | * @throws Exception if any error occurs
|
---|
| 91 | */
|
---|
| 92 | @Test
|
---|
[17275] | 93 | void testBuildImageryInfo() throws Exception {
|
---|
[16588] | 94 | String url = "https://localhost/imagery?title=osm"
|
---|
[16589] | 95 | + "&type=tms&min_zoom=3&max_zoom=23&category=osmbasedmap&country_code=XA"
|
---|
[16588] | 96 | + "&url=https://a.tile.openstreetmap.org/%7Bzoom%7D/%7Bx%7D/%7By%7D.png";
|
---|
| 97 | ImageryInfo imageryInfo = newHandler(url).buildImageryInfo();
|
---|
| 98 | assertEquals("osm", imageryInfo.getName());
|
---|
| 99 | assertEquals(ImageryInfo.ImageryType.TMS, imageryInfo.getImageryType());
|
---|
| 100 | assertEquals("https://a.tile.openstreetmap.org/{zoom}/{x}/{y}.png", imageryInfo.getUrl());
|
---|
| 101 | assertEquals(3, imageryInfo.getMinZoom());
|
---|
| 102 | assertEquals(23, imageryInfo.getMaxZoom());
|
---|
[16589] | 103 | assertEquals(ImageryInfo.ImageryCategory.OSMBASEDMAP, imageryInfo.getImageryCategory());
|
---|
| 104 | assertEquals("XA", imageryInfo.getCountryCode());
|
---|
[16588] | 105 | }
|
---|
[16734] | 106 |
|
---|
| 107 | /**
|
---|
| 108 | * Non-regression test for bug #19483.
|
---|
| 109 | * @throws Exception if any error occurs
|
---|
| 110 | */
|
---|
| 111 | @Test
|
---|
[17275] | 112 | void testTicket19483() throws Exception {
|
---|
[16734] | 113 | String url = "https://localhost/imagery?url=" +
|
---|
| 114 | "tms[3-7]%3Ahttps%3A%2F%2Fservices.digitalglobe.com%2Fearthservice%2Ftmsaccess%2F" +
|
---|
| 115 | "tms%2F1.0.0%2FDigitalGlobe%3AImageryTileService%40EPSG%3A3857%40jpg%2F%7Bz%7D%2F%7Bx%7D%2F%7B-y%7D.jpg%3F" +
|
---|
| 116 | "connectId%3D0123456789";
|
---|
| 117 | ImageryInfo imageryInfo = newHandler(url).buildImageryInfo();
|
---|
[16736] | 118 | assertEquals(ImageryInfo.ImageryType.WMS, imageryInfo.getImageryType());
|
---|
| 119 | /* do not interpret the URL, take it as is and error later */
|
---|
[16739] | 120 | assertEquals("tms[3-7]:https://services.digitalglobe.com/earthservice/tmsaccess/tms/1.0.0/DigitalGlobe:ImageryTileService" +
|
---|
[16734] | 121 | "@EPSG:3857@jpg/{z}/{x}/{-y}.jpg?connectId=0123456789", imageryInfo.getUrl());
|
---|
| 122 | }
|
---|
[10116] | 123 | }
|
---|