1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol.handler;
|
---|
3 |
|
---|
4 | import static org.hamcrest.CoreMatchers.hasItem;
|
---|
5 | import static org.hamcrest.MatcherAssert.assertThat;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
---|
9 |
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
14 | import org.junit.jupiter.api.Test;
|
---|
15 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
16 | import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerBadRequestException;
|
---|
17 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
18 |
|
---|
19 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Unit tests of {@link ImageryHandler} class.
|
---|
23 | */
|
---|
24 | class ImageryHandlerTest {
|
---|
25 | /**
|
---|
26 | * Setup test.
|
---|
27 | */
|
---|
28 | @RegisterExtension
|
---|
29 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
30 | public JOSMTestRules test = new JOSMTestRules();
|
---|
31 |
|
---|
32 | private static ImageryHandler newHandler(String url) throws RequestHandlerBadRequestException {
|
---|
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
|
---|
43 | void testBadRequestNoParam() {
|
---|
44 | Exception e = assertThrows(RequestHandlerBadRequestException.class, () -> newHandler(null).handle());
|
---|
45 | assertEquals("Parameter must not be null", e.getMessage());
|
---|
46 |
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Unit test for bad request - invalid URL.
|
---|
51 | */
|
---|
52 | @Test
|
---|
53 | void testBadRequestInvalidUrl() {
|
---|
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());
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Unit test for bad request - incomplete URL.
|
---|
60 | */
|
---|
61 | @Test
|
---|
62 | void testBadRequestIncompleteUrl() {
|
---|
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());
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Unit test for nominal request - local data file.
|
---|
69 | */
|
---|
70 | @Test
|
---|
71 | void testNominalRequest() {
|
---|
72 | assertDoesNotThrow(() -> newHandler("https://localhost?url=foo").handle());
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Unit test for {@link ImageryHandler#getOptionalParams()}
|
---|
77 | * @throws Exception if any error occurs
|
---|
78 | */
|
---|
79 | @Test
|
---|
80 | void testOptionalParams() throws Exception {
|
---|
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 | /**
|
---|
89 | * Unit test for {@link ImageryHandler#buildImageryInfo()}
|
---|
90 | * @throws Exception if any error occurs
|
---|
91 | */
|
---|
92 | @Test
|
---|
93 | void testBuildImageryInfo() throws Exception {
|
---|
94 | String url = "https://localhost/imagery?title=osm"
|
---|
95 | + "&type=tms&min_zoom=3&max_zoom=23&category=osmbasedmap&country_code=XA"
|
---|
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());
|
---|
103 | assertEquals(ImageryInfo.ImageryCategory.OSMBASEDMAP, imageryInfo.getImageryCategory());
|
---|
104 | assertEquals("XA", imageryInfo.getCountryCode());
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Non-regression test for bug #19483.
|
---|
109 | * @throws Exception if any error occurs
|
---|
110 | */
|
---|
111 | @Test
|
---|
112 | void testTicket19483() throws Exception {
|
---|
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();
|
---|
118 | assertEquals(ImageryInfo.ImageryType.WMS, imageryInfo.getImageryType());
|
---|
119 | /* do not interpret the URL, take it as is and error later */
|
---|
120 | assertEquals("tms[3-7]:https://services.digitalglobe.com/earthservice/tmsaccess/tms/1.0.0/DigitalGlobe:ImageryTileService" +
|
---|
121 | "@EPSG:3857@jpg/{z}/{x}/{-y}.jpg?connectId=0123456789", imageryInfo.getUrl());
|
---|
122 | }
|
---|
123 | }
|
---|