| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions.downloadtasks;
|
|---|
| 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 |
|
|---|
| 9 | import org.junit.Rule;
|
|---|
| 10 | import org.openstreetmap.josm.TestUtils;
|
|---|
| 11 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
|---|
| 12 |
|
|---|
| 13 | import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
|---|
| 14 |
|
|---|
| 15 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * Superclass of {@link DownloadGpsTaskTest}, {@link DownloadOsmTaskTest} and {@link DownloadNotesTaskTest}.
|
|---|
| 19 | */
|
|---|
| 20 | public 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 | }
|
|---|