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.openstreetmap.josm.TestUtils;
|
---|
12 | import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerBadRequestException;
|
---|
13 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
14 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
---|
15 |
|
---|
16 | import com.github.tomakehurst.wiremock.WireMockServer;
|
---|
17 | import org.junit.jupiter.api.Test;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Unit tests of {@link LoadObjectHandler} class.
|
---|
21 | */
|
---|
22 | class LoadObjectHandlerTest {
|
---|
23 | private static LoadObjectHandler newHandler(String url) throws RequestHandlerBadRequestException {
|
---|
24 | LoadObjectHandler req = new LoadObjectHandler();
|
---|
25 | if (url != null)
|
---|
26 | req.setUrl(url);
|
---|
27 | return req;
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Unit test for bad request - no param.
|
---|
32 | */
|
---|
33 | @Test
|
---|
34 | void testBadRequestNoParam() {
|
---|
35 | Exception e = assertThrows(RequestHandlerBadRequestException.class, () -> newHandler(null).handle());
|
---|
36 | assertEquals("No valid object identifier has been provided", e.getMessage());
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Unit test for bad request - invalid URL.
|
---|
41 | */
|
---|
42 | @Test
|
---|
43 | void testBadRequestInvalidUrl() {
|
---|
44 | Exception e = assertThrows(RequestHandlerBadRequestException.class, () -> newHandler("invalid_url").handle());
|
---|
45 | assertEquals("The following keys are mandatory, but have not been provided: objects", e.getMessage());
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Unit test for bad request - incomplete URL.
|
---|
50 | */
|
---|
51 | @Test
|
---|
52 | void testBadRequestIncompleteUrl() {
|
---|
53 | Exception e = assertThrows(RequestHandlerBadRequestException.class, () -> newHandler("https://localhost").handle());
|
---|
54 | assertEquals("The following keys are mandatory, but have not been provided: objects", e.getMessage());
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Unit test for nominal request - local data file.
|
---|
59 | */
|
---|
60 | @Test
|
---|
61 | @BasicPreferences
|
---|
62 | void testNominalRequest() {
|
---|
63 | WireMockServer wiremock = TestUtils.getWireMockServer();
|
---|
64 | wiremock.addStubMapping(get(urlEqualTo("/capabilities")).willReturn(aResponse().withStatusMessage("OK")
|
---|
65 | .withBodyFile("api/capabilities")).build());
|
---|
66 | String w1 = "<way id=\"1\" version=\"1\"><nd ref=\"1\"/><nd ref=\"2\"/></way>";
|
---|
67 | String n1 = "<node id=\"1\" version=\"1\"/>";
|
---|
68 | String n2 = "<node id=\"2\" version=\"1\"/>";
|
---|
69 | wiremock.addStubMapping(get(urlEqualTo("/0.6/ways?ways=1")).willReturn(aResponse().withStatusMessage("OK")
|
---|
70 | .withBody(osm(w1))).build());
|
---|
71 | wiremock.addStubMapping(get(urlEqualTo("/0.6/nodes?nodes=1,2")).willReturn(aResponse().withStatusMessage("OK")
|
---|
72 | .withBody(osm(n1 + n2))).build());
|
---|
73 | wiremock.addStubMapping(get(urlEqualTo("/0.6/way/1/full")).willReturn(aResponse().withStatusMessage("OK")
|
---|
74 | .withBody(osm(n1 + n2 + w1))).build());
|
---|
75 | wiremock.start();
|
---|
76 | Config.getPref().put("osm-server.url", wiremock.baseUrl());
|
---|
77 | try {
|
---|
78 | assertDoesNotThrow(() -> newHandler("https://localhost?objects=n1,w1").handle());
|
---|
79 | } finally {
|
---|
80 | wiremock.stop();
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | private static String osm(String xml) {
|
---|
85 | return "<osm version=\"0.6\">" + xml + "</osm>";
|
---|
86 | }
|
---|
87 | }
|
---|