1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol.handler;
|
---|
3 |
|
---|
4 | import org.junit.BeforeClass;
|
---|
5 | import org.junit.Rule;
|
---|
6 | import org.junit.Test;
|
---|
7 | import org.junit.rules.ExpectedException;
|
---|
8 | import org.openstreetmap.josm.JOSMFixture;
|
---|
9 | import org.openstreetmap.josm.Main;
|
---|
10 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
11 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
12 | import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerBadRequestException;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Unit tests of {@link AddWayHandler} class.
|
---|
16 | */
|
---|
17 | public class AddWayHandlerTest {
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Rule used for tests throwing exceptions.
|
---|
21 | */
|
---|
22 | @Rule
|
---|
23 | public ExpectedException thrown = ExpectedException.none();
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Setup test.
|
---|
27 | */
|
---|
28 | @BeforeClass
|
---|
29 | public static void setUpBeforeClass() {
|
---|
30 | JOSMFixture.createUnitTestFixture().init(true);
|
---|
31 | }
|
---|
32 |
|
---|
33 | private static AddWayHandler newHandler(String url) throws RequestHandlerBadRequestException {
|
---|
34 | AddWayHandler req = new AddWayHandler();
|
---|
35 | if (url != null)
|
---|
36 | req.setUrl(url);
|
---|
37 | return req;
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Unit test for bad request - no layer.
|
---|
42 | * @throws Exception if any error occurs
|
---|
43 | */
|
---|
44 | @Test
|
---|
45 | public void testBadRequestNoLayer() throws Exception {
|
---|
46 | thrown.expect(RequestHandlerBadRequestException.class);
|
---|
47 | thrown.expectMessage("There is no layer opened to add way");
|
---|
48 | newHandler("https://localhost?way=0,0;1,1").handle();
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Unit test for bad request - no param.
|
---|
53 | * @throws Exception if any error occurs
|
---|
54 | */
|
---|
55 | @Test
|
---|
56 | public void testBadRequestNoParam() throws Exception {
|
---|
57 | thrown.expect(RequestHandlerBadRequestException.class);
|
---|
58 | thrown.expectMessage("Invalid coordinates: []");
|
---|
59 | OsmDataLayer layer = new OsmDataLayer(new DataSet(), "", null);
|
---|
60 | try {
|
---|
61 | Main.getLayerManager().addLayer(layer);
|
---|
62 | newHandler(null).handle();
|
---|
63 | } finally {
|
---|
64 | Main.getLayerManager().removeLayer(layer);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Unit test for bad request - invalid URL.
|
---|
70 | * @throws Exception if any error occurs
|
---|
71 | */
|
---|
72 | @Test
|
---|
73 | public void testBadRequestInvalidUrl() throws Exception {
|
---|
74 | thrown.expect(RequestHandlerBadRequestException.class);
|
---|
75 | thrown.expectMessage("The following keys are mandatory, but have not been provided: way");
|
---|
76 | newHandler("invalid_url").handle();
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Unit test for bad request - incomplete URL.
|
---|
81 | * @throws Exception if any error occurs
|
---|
82 | */
|
---|
83 | @Test
|
---|
84 | public void testBadRequestIncompleteUrl() throws Exception {
|
---|
85 | thrown.expect(RequestHandlerBadRequestException.class);
|
---|
86 | thrown.expectMessage("The following keys are mandatory, but have not been provided: way");
|
---|
87 | newHandler("https://localhost").handle();
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Unit test for nominal request - local data file.
|
---|
92 | * @throws Exception if any error occurs
|
---|
93 | */
|
---|
94 | @Test
|
---|
95 | public void testNominalRequest() throws Exception {
|
---|
96 | OsmDataLayer layer = new OsmDataLayer(new DataSet(), "", null);
|
---|
97 | try {
|
---|
98 | Main.getLayerManager().addLayer(layer);
|
---|
99 | newHandler("https://localhost?way=0,0;1,1").handle();
|
---|
100 | } finally {
|
---|
101 | Main.getLayerManager().removeLayer(layer);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|