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

Last change on this file since 19155 was 19155, checked in by taylor.smock, 7 months ago

Fix tests broken by r19152

  • Property svn:eol-style set to native
File size: 4.8 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 com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
13import org.junit.jupiter.api.Test;
14import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
15import org.openstreetmap.josm.data.imagery.ImageryInfo;
16import org.openstreetmap.josm.gui.MainApplication;
17import org.openstreetmap.josm.gui.layer.TMSLayer;
18import org.openstreetmap.josm.gui.layer.WMSLayer;
19import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
20import org.openstreetmap.josm.testutils.annotations.BasicWiremock;
21import org.openstreetmap.josm.testutils.annotations.OsmApi;
22import org.openstreetmap.josm.testutils.annotations.Projection;
23
24/**
25 * Unit tests for class {@link AddImageryLayerAction}.
26 */
27@BasicPreferences
28@BasicWiremock
29@OsmApi(OsmApi.APIType.FAKE)
30@Projection
31final class AddImageryLayerActionTest {
32 /**
33 * Unit test of {@link AddImageryLayerAction#updateEnabledState}.
34 */
35 @Test
36 void testEnabledState() {
37 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo")).isEnabled());
38 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_tms", "http://bar", "tms", null, null)).isEnabled());
39 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_bing", "http://bar", "bing", null, null)).isEnabled());
40 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_scanex", "http://bar", "scanex", null, null)).isEnabled());
41 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_wms_endpoint", "http://bar", "wms_endpoint", null, null)).isEnabled());
42 }
43
44 /**
45 * Unit test of {@link AddImageryLayerAction#actionPerformed} - Enabled cases for TMS.
46 */
47 @Test
48 void testActionPerformedEnabledTms() {
49 assertTrue(MainApplication.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
50 new AddImageryLayerAction(new ImageryInfo("foo_tms", "http://bar", "tms", null, null)).actionPerformed(null);
51 List<TMSLayer> tmsLayers = MainApplication.getLayerManager().getLayersOfType(TMSLayer.class);
52 assertEquals(1, tmsLayers.size());
53 MainApplication.getLayerManager().removeLayer(tmsLayers.get(0));
54 }
55
56 /**
57 * Unit test of {@link AddImageryLayerAction#actionPerformed} - Enabled cases for WMS.
58 */
59 @Test
60 void testActionPerformedEnabledWms(WireMockRuntimeInfo wireMockRuntimeInfo) {
61 wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/wms?apikey=random_key&SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.1.1"))
62 .willReturn(aResponse()
63 .withStatus(200)
64 .withHeader("Content-Type", "text/xml")
65 .withBodyFile("imagery/wms-capabilities.xml")));
66 wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/wms?apikey=random_key&SERVICE=WMS&REQUEST=GetCapabilities"))
67 .willReturn(aResponse()
68 .withStatus(404)));
69 wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/wms?apikey=random_key&SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.3.0"))
70 .willReturn(aResponse()
71 .withStatus(404)));
72
73 try {
74 FeatureAdapter.registerApiKeyAdapter(id -> "random_key");
75 final ImageryInfo imageryInfo = new ImageryInfo("localhost", wireMockRuntimeInfo.getHttpBaseUrl() + "/wms?apikey={apikey}",
76 "wms_endpoint", null, null);
77 imageryInfo.setId("testActionPerformedEnabledWms");
78 new AddImageryLayerAction(imageryInfo).actionPerformed(null);
79 List<WMSLayer> wmsLayers = MainApplication.getLayerManager().getLayersOfType(WMSLayer.class);
80 assertEquals(1, wmsLayers.size());
81
82 MainApplication.getLayerManager().removeLayer(wmsLayers.get(0));
83 } finally {
84 FeatureAdapter.registerApiKeyAdapter(new FeatureAdapter.DefaultApiKeyAdapter());
85 }
86 }
87
88 /**
89 * Unit test of {@link AddImageryLayerAction#actionPerformed} - disabled case.
90 */
91 @Test
92 void testActionPerformedDisabled() {
93 assertTrue(MainApplication.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
94 try {
95 new AddImageryLayerAction(new ImageryInfo("foo")).actionPerformed(null);
96 } catch (IllegalArgumentException expected) {
97 assertEquals("Parameter 'info.url' must not be null", expected.getMessage());
98 }
99 assertTrue(MainApplication.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
100 }
101}
Note: See TracBrowser for help on using the repository browser.