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 AbstractDownloadTaskTestParent {
|
---|
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 {@code Content-Type} with which to serve the file referenced
|
---|
43 | * by {@link #getRemoteFile()}
|
---|
44 | * @return the {@code Content-Type} string for file {@link #getRemoteFile()}
|
---|
45 | */
|
---|
46 | protected String getRemoteContentType() {
|
---|
47 | return "text/xml";
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Returns the http URL to remote test file to download.
|
---|
52 | * @return the http URL to remote test file to download
|
---|
53 | */
|
---|
54 | protected final String getRemoteFileUrl() {
|
---|
55 | return wireMockRule.url(getRemoteFile());
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Mock the HTTP server.
|
---|
60 | */
|
---|
61 | protected final void mockHttp() {
|
---|
62 | wireMockRule.stubFor(get(urlEqualTo("/" + getRemoteFile()))
|
---|
63 | .willReturn(aResponse()
|
---|
64 | .withStatus(200)
|
---|
65 | .withHeader("Content-Type", getRemoteContentType())
|
---|
66 | .withBodyFile(getRemoteFile())));
|
---|
67 | }
|
---|
68 | }
|
---|