1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
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 com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
|
---|
8 | import static org.junit.Assert.assertEquals;
|
---|
9 | import static org.junit.Assert.assertTrue;
|
---|
10 |
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import org.junit.Rule;
|
---|
14 | import org.junit.Test;
|
---|
15 | import org.openstreetmap.josm.TestUtils;
|
---|
16 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
17 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
18 | import org.openstreetmap.josm.gui.layer.TMSLayer;
|
---|
19 | import org.openstreetmap.josm.gui.layer.WMSLayer;
|
---|
20 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
21 |
|
---|
22 | import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
---|
23 |
|
---|
24 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Unit tests for class {@link AddImageryLayerAction}.
|
---|
28 | */
|
---|
29 | public final class AddImageryLayerActionTest {
|
---|
30 | /**
|
---|
31 | * We need prefs for this. We need platform for actions and the OSM API for checking blacklist.
|
---|
32 | */
|
---|
33 | @Rule
|
---|
34 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
35 | public JOSMTestRules test = new JOSMTestRules().preferences().fakeAPI();
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * HTTP mock.
|
---|
39 | */
|
---|
40 | @Rule
|
---|
41 | public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort().usingFilesUnderDirectory(TestUtils.getTestDataRoot()));
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Unit test of {@link AddImageryLayerAction#updateEnabledState}.
|
---|
45 | */
|
---|
46 | @Test
|
---|
47 | public void testEnabledState() {
|
---|
48 | assertTrue(new AddImageryLayerAction(new ImageryInfo("foo")).isEnabled());
|
---|
49 | assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_tms", "http://bar", "tms", null, null)).isEnabled());
|
---|
50 | assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_bing", "http://bar", "bing", null, null)).isEnabled());
|
---|
51 | assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_scanex", "http://bar", "scanex", null, null)).isEnabled());
|
---|
52 | assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_wms_endpoint", "http://bar", "wms_endpoint", null, null)).isEnabled());
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Unit test of {@link AddImageryLayerAction#actionPerformed} - Enabled cases for TMS.
|
---|
57 | */
|
---|
58 | @Test
|
---|
59 | public void testActionPerformedEnabledTms() {
|
---|
60 | assertTrue(MainApplication.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
|
---|
61 | new AddImageryLayerAction(new ImageryInfo("foo_tms", "http://bar", "tms", null, null)).actionPerformed(null);
|
---|
62 | List<TMSLayer> tmsLayers = MainApplication.getLayerManager().getLayersOfType(TMSLayer.class);
|
---|
63 | assertEquals(1, tmsLayers.size());
|
---|
64 | MainApplication.getLayerManager().removeLayer(tmsLayers.get(0));
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Unit test of {@link AddImageryLayerAction#actionPerformed} - Enabled cases for WMS.
|
---|
69 | */
|
---|
70 | @Test
|
---|
71 | public void testActionPerformedEnabledWms() {
|
---|
72 | wireMockRule.stubFor(get(urlEqualTo("/wms?SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.1.1"))
|
---|
73 | .willReturn(aResponse()
|
---|
74 | .withStatus(200)
|
---|
75 | .withHeader("Content-Type", "text/xml")
|
---|
76 | .withBodyFile("imagery/wms-capabilities.xml")));
|
---|
77 | wireMockRule.stubFor(get(urlEqualTo("/wms?SERVICE=WMS&REQUEST=GetCapabilities"))
|
---|
78 | .willReturn(aResponse()
|
---|
79 | .withStatus(404)));
|
---|
80 | wireMockRule.stubFor(get(urlEqualTo("/wms?SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.3.0"))
|
---|
81 | .willReturn(aResponse()
|
---|
82 | .withStatus(404)));
|
---|
83 |
|
---|
84 | new AddImageryLayerAction(new ImageryInfo("localhost", wireMockRule.url("/wms?"),
|
---|
85 | "wms_endpoint", null, null)).actionPerformed(null);
|
---|
86 | List<WMSLayer> wmsLayers = MainApplication.getLayerManager().getLayersOfType(WMSLayer.class);
|
---|
87 | assertEquals(1, wmsLayers.size());
|
---|
88 |
|
---|
89 | MainApplication.getLayerManager().removeLayer(wmsLayers.get(0));
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Unit test of {@link AddImageryLayerAction#actionPerformed} - disabled case.
|
---|
94 | */
|
---|
95 | @Test
|
---|
96 | public void testActionPerformedDisabled() {
|
---|
97 | assertTrue(MainApplication.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
|
---|
98 | try {
|
---|
99 | new AddImageryLayerAction(new ImageryInfo("foo")).actionPerformed(null);
|
---|
100 | } catch (IllegalArgumentException expected) {
|
---|
101 | assertEquals("Parameter 'info.url' must not be null", expected.getMessage());
|
---|
102 | }
|
---|
103 | assertTrue(MainApplication.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
|
---|
104 | }
|
---|
105 | }
|
---|