1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.server;
|
---|
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.hamcrest.CoreMatchers.containsString;
|
---|
9 | import static org.hamcrest.MatcherAssert.assertThat;
|
---|
10 | import static org.junit.Assert.assertFalse;
|
---|
11 | import static org.junit.Assert.assertTrue;
|
---|
12 |
|
---|
13 | import java.awt.Component;
|
---|
14 |
|
---|
15 | import javax.swing.JLabel;
|
---|
16 |
|
---|
17 | import org.junit.Rule;
|
---|
18 | import org.junit.Test;
|
---|
19 | import org.openstreetmap.josm.TestUtils;
|
---|
20 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
21 | import org.openstreetmap.josm.tools.Logging;
|
---|
22 |
|
---|
23 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
24 |
|
---|
25 | import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Unit tests of {@link ApiUrlTestTask} class.
|
---|
29 | */
|
---|
30 | public 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 | }
|
---|