1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools.bugreport;
|
---|
3 |
|
---|
4 | import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
---|
5 | import static com.github.tomakehurst.wiremock.client.WireMock.containing;
|
---|
6 | import static com.github.tomakehurst.wiremock.client.WireMock.exactly;
|
---|
7 | import static com.github.tomakehurst.wiremock.client.WireMock.post;
|
---|
8 | import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
|
---|
9 | import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
---|
10 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
11 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
12 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
13 | import static org.junit.jupiter.api.Assertions.assertNull;
|
---|
14 |
|
---|
15 | import java.net.URI;
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | import org.junit.jupiter.api.Test;
|
---|
19 | import org.openstreetmap.josm.actions.ShowStatusReportAction;
|
---|
20 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
21 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
---|
22 | import org.openstreetmap.josm.testutils.annotations.BasicWiremock;
|
---|
23 | import org.openstreetmap.josm.testutils.annotations.HTTP;
|
---|
24 | import org.openstreetmap.josm.testutils.mockers.OpenBrowserMocker;
|
---|
25 |
|
---|
26 | import com.github.tomakehurst.wiremock.WireMockServer;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Unit tests of {@link BugReportSender} class.
|
---|
30 | */
|
---|
31 | @BasicPreferences
|
---|
32 | @BasicWiremock
|
---|
33 | @HTTP
|
---|
34 | class BugReportSenderTest {
|
---|
35 | /**
|
---|
36 | * HTTP mock
|
---|
37 | */
|
---|
38 | @BasicWiremock
|
---|
39 | WireMockServer wireMockServer;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Unit test for {@link BugReportSender#BugReportSender}.
|
---|
43 | * @throws InterruptedException if the thread is interrupted
|
---|
44 | */
|
---|
45 | @Test
|
---|
46 | void testBugReportSender() throws InterruptedException {
|
---|
47 | Config.getPref().put("josm.url", wireMockServer.baseUrl());
|
---|
48 | wireMockServer.stubFor(post(urlEqualTo("/josmticket"))
|
---|
49 | .willReturn(aResponse()
|
---|
50 | .withStatus(200)
|
---|
51 | .withHeader("Content-Type", "text/xml")
|
---|
52 | .withBody("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
---|
53 | "<josmticket status=\"ok\">\n" +
|
---|
54 | " <preparedid>6bccff5c0417217bfbbe5fff</preparedid>\n" +
|
---|
55 | "</josmticket>\n")));
|
---|
56 | new OpenBrowserMocker();
|
---|
57 |
|
---|
58 | BugReportSender sender = BugReportSender.reportBug(ShowStatusReportAction.getReportHeader());
|
---|
59 | assertNotNull(sender);
|
---|
60 | synchronized (sender) {
|
---|
61 | while (sender.isAlive()) {
|
---|
62 | sender.wait();
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | assertFalse(sender.isAlive());
|
---|
67 | assertNull(sender.getErrorMessage(), sender.getErrorMessage());
|
---|
68 | wireMockServer.verify(exactly(1), postRequestedFor(urlEqualTo("/josmticket")).withRequestBody(containing("pdata=")));
|
---|
69 |
|
---|
70 | List<URI> calledURIs = OpenBrowserMocker.getCalledURIs();
|
---|
71 | assertEquals(1, calledURIs.size());
|
---|
72 | assertEquals(wireMockServer.url("/josmticket?pdata_stored=6bccff5c0417217bfbbe5fff"), calledURIs.get(0).toString());
|
---|
73 | }
|
---|
74 | }
|
---|