1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol.handler;
|
---|
3 |
|
---|
4 | import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
---|
5 | import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
---|
6 | import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
9 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
---|
10 |
|
---|
11 | import org.junit.jupiter.api.Test;
|
---|
12 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
13 | import org.openstreetmap.josm.TestUtils;
|
---|
14 | import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerBadRequestException;
|
---|
15 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
16 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
17 |
|
---|
18 | import com.github.tomakehurst.wiremock.WireMockServer;
|
---|
19 |
|
---|
20 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Unit tests of {@link LoadObjectHandler} class.
|
---|
24 | */
|
---|
25 | class LoadObjectHandlerTest {
|
---|
26 | /**
|
---|
27 | * Setup test.
|
---|
28 | */
|
---|
29 | @RegisterExtension
|
---|
30 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
31 | public JOSMTestRules test = new JOSMTestRules();
|
---|
32 |
|
---|
33 | private static LoadObjectHandler newHandler(String url) throws RequestHandlerBadRequestException {
|
---|
34 | LoadObjectHandler req = new LoadObjectHandler();
|
---|
35 | if (url != null)
|
---|
36 | req.setUrl(url);
|
---|
37 | return req;
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Unit test for bad request - no param.
|
---|
42 | */
|
---|
43 | @Test
|
---|
44 | void testBadRequestNoParam() {
|
---|
45 | Exception e = assertThrows(RequestHandlerBadRequestException.class, () -> newHandler(null).handle());
|
---|
46 | assertEquals("No valid object identifier has been provided", e.getMessage());
|
---|
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: objects", 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: objects", e.getMessage());
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Unit test for nominal request - local data file.
|
---|
69 | */
|
---|
70 | @Test
|
---|
71 | void testNominalRequest() {
|
---|
72 | WireMockServer wiremock = TestUtils.getWireMockServer();
|
---|
73 | wiremock.addStubMapping(get(urlEqualTo("/capabilities")).willReturn(aResponse().withStatusMessage("OK")
|
---|
74 | .withBodyFile("api/capabilities")).build());
|
---|
75 | String w1 = "<way id=\"1\" version=\"1\"><nd ref=\"1\"/><nd ref=\"2\"/></way>";
|
---|
76 | String n1 = "<node id=\"1\" version=\"1\"/>";
|
---|
77 | String n2 = "<node id=\"2\" version=\"1\"/>";
|
---|
78 | wiremock.addStubMapping(get(urlEqualTo("/0.6/ways?ways=1")).willReturn(aResponse().withStatusMessage("OK")
|
---|
79 | .withBody(osm(w1))).build());
|
---|
80 | wiremock.addStubMapping(get(urlEqualTo("/0.6/nodes?nodes=1,2")).willReturn(aResponse().withStatusMessage("OK")
|
---|
81 | .withBody(osm(n1 + n2))).build());
|
---|
82 | wiremock.addStubMapping(get(urlEqualTo("/0.6/way/1/full")).willReturn(aResponse().withStatusMessage("OK")
|
---|
83 | .withBody(osm(n1 + n2 + w1))).build());
|
---|
84 | wiremock.start();
|
---|
85 | Config.getPref().put("osm-server.url", wiremock.baseUrl());
|
---|
86 | try {
|
---|
87 | assertDoesNotThrow(() -> newHandler("https://localhost?objects=n1,w1").handle());
|
---|
88 | } finally {
|
---|
89 | wiremock.stop();
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | private static String osm(String xml) {
|
---|
94 | return "<osm version=\"0.6\">" + xml + "</osm>";
|
---|
95 | }
|
---|
96 | }
|
---|