source: josm/trunk/test/unit/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTaskTest.java@ 17360

Last change on this file since 17360 was 17194, checked in by simon04, 4 years ago

see #15102 - see #16637 - get rid of real HTTP calls to https://api.openstreetmap.org in OsmServerHistoryReaderTest, mock them

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.server;
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.hamcrest.CoreMatchers.containsString;
9import static org.hamcrest.MatcherAssert.assertThat;
10import static org.junit.Assert.assertFalse;
11import static org.junit.Assert.assertTrue;
12
13import java.awt.Component;
14
15import javax.swing.JLabel;
16
17import org.junit.Rule;
18import org.junit.Test;
19import org.openstreetmap.josm.TestUtils;
20import org.openstreetmap.josm.testutils.JOSMTestRules;
21import org.openstreetmap.josm.tools.Logging;
22
23import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
24
25import com.github.tomakehurst.wiremock.junit.WireMockRule;
26
27/**
28 * Unit tests of {@link ApiUrlTestTask} class.
29 */
30public class ApiUrlTestTaskTest {
31
32 /**
33 * Setup tests
34 */
35 @Rule
36 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
37 public JOSMTestRules test = new JOSMTestRules().preferences().timeout(30000);
38
39 /**
40 * HTTP mock.
41 */
42 @Rule
43 public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort().usingFilesUnderDirectory(TestUtils.getTestDataRoot()));
44
45 private static final Component PARENT = new JLabel();
46
47 /**
48 * Unit test of {@link ApiUrlTestTask#ApiUrlTestTask} - null url.
49 */
50 @Test(expected = IllegalArgumentException.class)
51 public void testNullApiUrl() {
52 new ApiUrlTestTask(PARENT, null);
53 }
54
55 /**
56 * Unit test of {@link ApiUrlTestTask} - nominal url.
57 */
58 @Test
59 public void testNominalUrl() {
60 ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockRule.url("/__files/api"));
61 task.run();
62 assertTrue(task.isSuccess());
63 }
64
65 /**
66 * Unit test of {@link ApiUrlTestTask#alertInvalidUrl} - malformed url.
67 */
68 @Test
69 public void testAlertInvalidUrl() {
70 Logging.clearLastErrorAndWarnings();
71 ApiUrlTestTask task = new ApiUrlTestTask(PARENT, "malformed url");
72 task.run();
73 assertFalse(task.isSuccess());
74 assertThat(Logging.getLastErrorAndWarnings().toString(), containsString(
75 "<html>'malformed url' is not a valid OSM API URL.<br>Please check the spelling and validate again.</html>"));
76 }
77
78 /**
79 * Unit test of {@link ApiUrlTestTask} - unknown host.
80 */
81 @Test
82 public void testUnknownHost() {
83 Logging.clearLastErrorAndWarnings();
84 ApiUrlTestTask task = new ApiUrlTestTask(PARENT, "http://unknown");
85 task.run();
86 assertFalse(task.isSuccess());
87 assertThat(Logging.getLastErrorAndWarnings().toString(), containsString(
88 "java.net.UnknownHostException: unknown"));
89 }
90
91 /**
92 * Unit test of {@link ApiUrlTestTask#alertInvalidServerResult} - http 404.
93 */
94 @Test
95 public void testAlertInvalidServerResult() {
96 Logging.clearLastErrorAndWarnings();
97 wireMockRule.stubFor(get(urlEqualTo("/does-not-exist/0.6/capabilities"))
98 .willReturn(aResponse().withStatus(404)));
99
100 ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockRule.url("/does-not-exist"));
101 task.run();
102 assertFalse(task.isSuccess());
103 assertThat(Logging.getLastErrorAndWarnings().toString(), containsString(
104 "The server responded with the return code 404 instead of 200."));
105 }
106
107 /**
108 * Unit test of {@link ApiUrlTestTask#alertInvalidCapabilities} - invalid contents.
109 */
110 @Test
111 public void testAlertInvalidCapabilities() {
112 Logging.clearLastErrorAndWarnings();
113 ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockRule.url("/__files/invalid_api"));
114 task.run();
115 assertFalse(task.isSuccess());
116 assertThat(Logging.getLastErrorAndWarnings().toString(), containsString(
117 "The OSM API server at 'XXX' did not return a valid response.<br>It is likely that 'XXX' is not an OSM API server."
118 .replace("XXX", wireMockRule.url("/__files/invalid_api"))));
119 }
120}
Note: See TracBrowser for help on using the repository browser.