source: josm/trunk/test/unit/org/openstreetmap/josm/actions/downloadtasks/AbstractDownloadTaskTest.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: 2.0 KB
RevLine 
[12557]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.downloadtasks;
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;
8
9import org.junit.Rule;
10import org.openstreetmap.josm.TestUtils;
11import org.openstreetmap.josm.testutils.JOSMTestRules;
12
13import com.github.tomakehurst.wiremock.junit.WireMockRule;
14
15import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16
17/**
18 * Superclass of {@link DownloadGpsTaskTest}, {@link DownloadOsmTaskTest} and {@link DownloadNotesTaskTest}.
19 */
20public abstract class AbstractDownloadTaskTest {
21
22 /**
23 * Setup test.
24 */
25 @Rule
26 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
27 public JOSMTestRules test = new JOSMTestRules().https();
28
29 /**
30 * HTTP mock.
31 */
32 @Rule
33 public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort().usingFilesUnderDirectory(TestUtils.getTestDataRoot()));
34
35 /**
36 * Returns the path to remote test file to download via http.
37 * @return the path to remote test file, relative to JOSM root directory
38 */
39 protected abstract String getRemoteFile();
40
41 /**
42 * Returns the http URL to remote test file to download.
43 * @return the http URL to remote test file to download
44 */
45 protected final String getRemoteFileUrl() {
46 return "http://localhost:" + wireMockRule.port() + "/" + getRemoteFile();
47 }
48
49 /**
50 * Mock the HTTP server.
51 */
52 protected final void mockHttp() {
53 wireMockRule.stubFor(get(urlEqualTo("/" + getRemoteFile()))
54 .willReturn(aResponse()
55 .withStatus(200)
56 .withHeader("Content-Type", "text/xml")
57 .withBodyFile(getRemoteFile())));
58 }
59}
Note: See TracBrowser for help on using the repository browser.