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

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

see #15102 - first batch of HTTP unit tests mocking, using WireMock 2.7.1

  • Property svn:eol-style set to native
File size: 4.3 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 com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
8import static org.junit.Assert.assertEquals;
9import static org.junit.Assert.assertTrue;
10
11import java.util.List;
12
13import org.junit.Rule;
14import org.junit.Test;
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.TestUtils;
17import org.openstreetmap.josm.data.imagery.ImageryInfo;
18import org.openstreetmap.josm.gui.layer.TMSLayer;
19import org.openstreetmap.josm.gui.layer.WMSLayer;
20import org.openstreetmap.josm.testutils.JOSMTestRules;
21
22import com.github.tomakehurst.wiremock.junit.WireMockRule;
23
24import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
25
26/**
27 * Unit tests for class {@link AddImageryLayerAction}.
28 */
29public final class AddImageryLayerActionTest {
30 /**
31 * We need prefs for this. We need platform for actions and the OSM API for checking blacklist.
32 * The timeout is set to default httpclient read timeout + connect timeout + a small delay to ignore
33 * common but harmless network issues.
34 */
35 @Rule
36 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
37 public JOSMTestRules test = new JOSMTestRules().preferences().platform().fakeAPI().timeout(45500);
38
39 /**
40 * HTTP mock.
41 */
42 @Rule
43 public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort().usingFilesUnderDirectory(TestUtils.getTestDataRoot()));
44
45 /**
46 * Unit test of {@link AddImageryLayerAction#updateEnabledState}.
47 */
48 @Test
49 public 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 public void testActionPerformedEnabledTms() {
62 assertTrue(Main.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
63 new AddImageryLayerAction(new ImageryInfo("foo_tms", "http://bar", "tms", null, null)).actionPerformed(null);
64 List<TMSLayer> tmsLayers = Main.getLayerManager().getLayersOfType(TMSLayer.class);
65 assertEquals(1, tmsLayers.size());
66 Main.getLayerManager().removeLayer(tmsLayers.get(0));
67 }
68
69 /**
70 * Unit test of {@link AddImageryLayerAction#actionPerformed} - Enabled cases for WMS.
71 */
72 @Test
73 public void testActionPerformedEnabledWms() {
74 wireMockRule.stubFor(get(urlEqualTo("/wms?VERSION=1.1.1&SERVICE=WMS&REQUEST=GetCapabilities"))
75 .willReturn(aResponse()
76 .withStatus(200)
77 .withHeader("Content-Type", "text/xml")
78 .withBodyFile("imagery/wms-capabilities.xml")));
79 new AddImageryLayerAction(new ImageryInfo("localhost", "http://localhost:" + wireMockRule.port() + "/wms?",
80 "wms_endpoint", null, null)).actionPerformed(null);
81 List<WMSLayer> wmsLayers = Main.getLayerManager().getLayersOfType(WMSLayer.class);
82 assertEquals(1, wmsLayers.size());
83
84 Main.getLayerManager().removeLayer(wmsLayers.get(0));
85 }
86
87 /**
88 * Unit test of {@link AddImageryLayerAction#actionPerformed} - disabled case.
89 */
90 @Test
91 public void testActionPerformedDisabled() {
92 assertTrue(Main.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
93 try {
94 new AddImageryLayerAction(new ImageryInfo("foo")).actionPerformed(null);
95 } catch (IllegalArgumentException expected) {
96 assertEquals("Parameter 'info.url' must not be null", expected.getMessage());
97 }
98 assertTrue(Main.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
99 }
100}
Note: See TracBrowser for help on using the repository browser.