source: josm/trunk/test/unit/org/openstreetmap/josm/actions/AddImageryLayerActionTest.java@ 18106

Last change on this file since 18106 was 18106, checked in by Don-vip, 4 years ago

fix #21150 - Add JUnit5 annotation for WireMockServer (patch by taylor.smock)

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