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.Assert.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.Rule;
|
---|
14 | import org.junit.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 | public class ImageryHandlerTest {
|
---|
25 | /**
|
---|
26 | * Setup test.
|
---|
27 | */
|
---|
28 | @Rule
|
---|
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 | public 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 | public 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 | public 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 | public 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 | public 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 | public 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 | }
|
---|