You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReportPublisherTest.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.report;
  21. import java.io.IOException;
  22. import java.io.PipedInputStream;
  23. import java.io.PipedOutputStream;
  24. import java.nio.charset.StandardCharsets;
  25. import java.nio.file.Files;
  26. import java.nio.file.Path;
  27. import java.util.Optional;
  28. import org.junit.Before;
  29. import org.junit.Rule;
  30. import org.junit.Test;
  31. import org.junit.rules.ExpectedException;
  32. import org.junit.rules.TemporaryFolder;
  33. import org.mockito.ArgumentCaptor;
  34. import org.mockito.Mockito;
  35. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  36. import org.sonar.api.batch.fs.internal.DefaultInputModule;
  37. import org.sonar.api.batch.fs.internal.InputModuleHierarchy;
  38. import org.sonar.api.platform.Server;
  39. import org.sonar.api.utils.MessageException;
  40. import org.sonar.api.utils.TempFolder;
  41. import org.sonar.api.utils.log.LogTester;
  42. import org.sonar.api.utils.log.LoggerLevel;
  43. import org.sonar.scanner.bootstrap.GlobalAnalysisMode;
  44. import org.sonar.scanner.bootstrap.ScannerWsClient;
  45. import org.sonar.scanner.scan.ScanProperties;
  46. import org.sonar.scanner.scan.branch.BranchConfiguration;
  47. import org.sonarqube.ws.Ce;
  48. import org.sonarqube.ws.client.HttpException;
  49. import org.sonarqube.ws.client.WsRequest;
  50. import org.sonarqube.ws.client.WsResponse;
  51. import static org.apache.commons.io.FileUtils.readFileToString;
  52. import static org.assertj.core.api.Assertions.assertThat;
  53. import static org.mockito.ArgumentMatchers.any;
  54. import static org.mockito.Mockito.mock;
  55. import static org.mockito.Mockito.verify;
  56. import static org.mockito.Mockito.when;
  57. import static org.sonar.scanner.scan.branch.BranchType.LONG;
  58. import static org.sonar.scanner.scan.branch.BranchType.PULL_REQUEST;
  59. import static org.sonar.scanner.scan.branch.BranchType.SHORT;
  60. public class ReportPublisherTest {
  61. @Rule
  62. public LogTester logTester = new LogTester();
  63. @Rule
  64. public TemporaryFolder temp = new TemporaryFolder();
  65. @Rule
  66. public ExpectedException exception = ExpectedException.none();
  67. GlobalAnalysisMode mode = mock(GlobalAnalysisMode.class);
  68. ScanProperties properties = mock(ScanProperties.class);
  69. ScannerWsClient wsClient = mock(ScannerWsClient.class, Mockito.RETURNS_DEEP_STUBS);
  70. Server server = mock(Server.class);
  71. InputModuleHierarchy moduleHierarchy = mock(InputModuleHierarchy.class);
  72. DefaultInputModule root;
  73. AnalysisContextReportPublisher contextPublisher = mock(AnalysisContextReportPublisher.class);
  74. BranchConfiguration branchConfiguration = mock(BranchConfiguration.class);
  75. ReportPublisher underTest = new ReportPublisher(properties, wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class),
  76. new ReportPublisherStep[0], branchConfiguration);
  77. @Before
  78. public void setUp() throws IOException {
  79. root = new DefaultInputModule(ProjectDefinition.create().setKey("org.sonarsource.sonarqube:sonarqube").setBaseDir(temp.newFolder()).setWorkDir(temp.getRoot()));
  80. when(moduleHierarchy.root()).thenReturn(root);
  81. when(server.getPublicRootUrl()).thenReturn("https://localhost");
  82. when(server.getVersion()).thenReturn("6.4");
  83. when(properties.metadataFilePath()).thenReturn(temp.newFolder().toPath()
  84. .resolve("folder")
  85. .resolve("report-task.txt"));
  86. }
  87. @Test
  88. public void log_and_dump_information_about_report_uploading() throws IOException {
  89. when(properties.organizationKey()).thenReturn(Optional.of("MyOrg"));
  90. underTest.logSuccess("TASK-123");
  91. assertThat(logTester.logs(LoggerLevel.INFO))
  92. .contains("ANALYSIS SUCCESSFUL, you can browse https://localhost/dashboard?id=org.sonarsource.sonarqube%3Asonarqube")
  93. .contains("Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report")
  94. .contains("More about the report processing at https://localhost/api/ce/task?id=TASK-123");
  95. assertThat(readFileToString(properties.metadataFilePath().toFile(), StandardCharsets.UTF_8)).isEqualTo(
  96. "organization=MyOrg\n" +
  97. "projectKey=org.sonarsource.sonarqube:sonarqube\n" +
  98. "serverUrl=https://localhost\n" +
  99. "serverVersion=6.4\n" +
  100. "dashboardUrl=https://localhost/dashboard?id=org.sonarsource.sonarqube%3Asonarqube\n" +
  101. "ceTaskId=TASK-123\n" +
  102. "ceTaskUrl=https://localhost/api/ce/task?id=TASK-123\n");
  103. }
  104. @Test
  105. public void parse_upload_error_message() throws IOException {
  106. HttpException ex = new HttpException("url", 404, "{\"errors\":[{\"msg\":\"Organization with key 'MyOrg' does not exist\"}]}");
  107. WsResponse response = mock(WsResponse.class);
  108. when(response.failIfNotSuccessful()).thenThrow(ex);
  109. when(wsClient.call(any(WsRequest.class))).thenReturn(response);
  110. exception.expect(MessageException.class);
  111. exception.expectMessage("Failed to upload report - Organization with key 'MyOrg' does not exist");
  112. underTest.upload(temp.newFile());
  113. }
  114. @Test
  115. public void log_public_url_if_defined_for_main_branch() throws IOException {
  116. when(server.getPublicRootUrl()).thenReturn("https://publicserver/sonarqube");
  117. underTest.logSuccess("TASK-123");
  118. assertThat(logTester.logs(LoggerLevel.INFO))
  119. .contains("ANALYSIS SUCCESSFUL, you can browse https://publicserver/sonarqube/dashboard?id=org.sonarsource.sonarqube%3Asonarqube")
  120. .contains("More about the report processing at https://publicserver/sonarqube/api/ce/task?id=TASK-123");
  121. assertThat(readFileToString(properties.metadataFilePath().toFile(), StandardCharsets.UTF_8)).isEqualTo(
  122. "projectKey=org.sonarsource.sonarqube:sonarqube\n" +
  123. "serverUrl=https://publicserver/sonarqube\n" +
  124. "serverVersion=6.4\n" +
  125. "dashboardUrl=https://publicserver/sonarqube/dashboard?id=org.sonarsource.sonarqube%3Asonarqube\n" +
  126. "ceTaskId=TASK-123\n" +
  127. "ceTaskUrl=https://publicserver/sonarqube/api/ce/task?id=TASK-123\n");
  128. }
  129. @Test
  130. public void log_public_url_if_defined_for_long_living_branches() throws IOException {
  131. when(server.getPublicRootUrl()).thenReturn("https://publicserver/sonarqube");
  132. when(branchConfiguration.branchType()).thenReturn(LONG);
  133. when(branchConfiguration.branchName()).thenReturn("branch-6.7");
  134. ReportPublisher underTest = new ReportPublisher(properties, wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class),
  135. new ReportPublisherStep[0], branchConfiguration);
  136. underTest.logSuccess("TASK-123");
  137. assertThat(logTester.logs(LoggerLevel.INFO))
  138. .contains("ANALYSIS SUCCESSFUL, you can browse https://publicserver/sonarqube/dashboard?id=org.sonarsource.sonarqube%3Asonarqube&branch=branch-6.7")
  139. .contains("More about the report processing at https://publicserver/sonarqube/api/ce/task?id=TASK-123");
  140. assertThat(readFileToString(properties.metadataFilePath().toFile(), StandardCharsets.UTF_8)).isEqualTo(
  141. "projectKey=org.sonarsource.sonarqube:sonarqube\n" +
  142. "serverUrl=https://publicserver/sonarqube\n" +
  143. "serverVersion=6.4\n" +
  144. "dashboardUrl=https://publicserver/sonarqube/dashboard?id=org.sonarsource.sonarqube%3Asonarqube&branch=branch-6.7\n" +
  145. "ceTaskId=TASK-123\n" +
  146. "ceTaskUrl=https://publicserver/sonarqube/api/ce/task?id=TASK-123\n");
  147. }
  148. @Test
  149. public void log_public_url_if_defined_for_short_living_branches() throws IOException {
  150. when(server.getPublicRootUrl()).thenReturn("https://publicserver/sonarqube");
  151. when(branchConfiguration.branchType()).thenReturn(SHORT);
  152. when(branchConfiguration.branchName()).thenReturn("branch-6.7");
  153. ReportPublisher underTest = new ReportPublisher(properties, wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class),
  154. new ReportPublisherStep[0], branchConfiguration);
  155. underTest.logSuccess("TASK-123");
  156. assertThat(logTester.logs(LoggerLevel.INFO))
  157. .contains("ANALYSIS SUCCESSFUL, you can browse https://publicserver/sonarqube/dashboard?id=org.sonarsource.sonarqube%3Asonarqube&branch=branch-6.7&resolved=false")
  158. .contains("More about the report processing at https://publicserver/sonarqube/api/ce/task?id=TASK-123");
  159. assertThat(readFileToString(properties.metadataFilePath().toFile(), StandardCharsets.UTF_8)).isEqualTo(
  160. "projectKey=org.sonarsource.sonarqube:sonarqube\n" +
  161. "serverUrl=https://publicserver/sonarqube\n" +
  162. "serverVersion=6.4\n" +
  163. "dashboardUrl=https://publicserver/sonarqube/dashboard?id=org.sonarsource.sonarqube%3Asonarqube&branch=branch-6.7&resolved=false\n" +
  164. "ceTaskId=TASK-123\n" +
  165. "ceTaskUrl=https://publicserver/sonarqube/api/ce/task?id=TASK-123\n");
  166. }
  167. @Test
  168. public void log_public_url_if_defined_for_pull_request() throws IOException {
  169. when(server.getPublicRootUrl()).thenReturn("https://publicserver/sonarqube");
  170. when(branchConfiguration.branchName()).thenReturn("Bitbucket cloud Widget");
  171. when(branchConfiguration.branchType()).thenReturn(PULL_REQUEST);
  172. when(branchConfiguration.pullRequestKey()).thenReturn("105");
  173. ReportPublisher underTest = new ReportPublisher(properties, wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class),
  174. new ReportPublisherStep[0], branchConfiguration);
  175. underTest.logSuccess("TASK-123");
  176. assertThat(logTester.logs(LoggerLevel.INFO))
  177. .contains("ANALYSIS SUCCESSFUL, you can browse https://publicserver/sonarqube/dashboard?id=org.sonarsource.sonarqube%3Asonarqube&pullRequest=105")
  178. .contains("More about the report processing at https://publicserver/sonarqube/api/ce/task?id=TASK-123");
  179. assertThat(readFileToString(properties.metadataFilePath().toFile(), StandardCharsets.UTF_8)).isEqualTo(
  180. "projectKey=org.sonarsource.sonarqube:sonarqube\n" +
  181. "serverUrl=https://publicserver/sonarqube\n" +
  182. "serverVersion=6.4\n" +
  183. "dashboardUrl=https://publicserver/sonarqube/dashboard?id=org.sonarsource.sonarqube%3Asonarqube&pullRequest=105\n" +
  184. "ceTaskId=TASK-123\n" +
  185. "ceTaskUrl=https://publicserver/sonarqube/api/ce/task?id=TASK-123\n");
  186. }
  187. @Test
  188. public void fail_if_public_url_malformed() {
  189. when(server.getPublicRootUrl()).thenReturn("invalid");
  190. exception.expect(MessageException.class);
  191. exception.expectMessage("Failed to parse public URL set in SonarQube server: invalid");
  192. underTest.start();
  193. }
  194. @Test
  195. public void log_but_not_dump_information_when_report_is_not_uploaded() throws IOException {
  196. underTest.logSuccess(/* report not uploaded, no server task */null);
  197. assertThat(logTester.logs(LoggerLevel.INFO))
  198. .contains("ANALYSIS SUCCESSFUL")
  199. .doesNotContain("dashboard/index");
  200. assertThat(properties.metadataFilePath()).doesNotExist();
  201. }
  202. @Test
  203. public void log_and_dump_information_to_custom_path() throws IOException {
  204. underTest.logSuccess("TASK-123");
  205. assertThat(properties.metadataFilePath()).exists();
  206. assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Report metadata written to " + properties.metadataFilePath());
  207. }
  208. @Test
  209. public void should_not_delete_report_if_property_is_set() throws IOException {
  210. when(properties.shouldKeepReport()).thenReturn(true);
  211. Path reportDir = temp.getRoot().toPath().resolve("scanner-report");
  212. Files.createDirectory(reportDir);
  213. underTest.start();
  214. underTest.stop();
  215. assertThat(reportDir).isDirectory();
  216. }
  217. @Test
  218. public void should_delete_report_by_default() throws IOException {
  219. Path reportDir = temp.getRoot().toPath().resolve("scanner-report");
  220. Files.createDirectory(reportDir);
  221. underTest.start();
  222. underTest.stop();
  223. assertThat(reportDir).doesNotExist();
  224. }
  225. @Test
  226. public void test_ws_parameters() throws Exception {
  227. when(properties.organizationKey()).thenReturn(Optional.of("MyOrg"));
  228. WsResponse response = mock(WsResponse.class);
  229. PipedOutputStream out = new PipedOutputStream();
  230. PipedInputStream in = new PipedInputStream(out);
  231. Ce.SubmitResponse.newBuilder().build().writeTo(out);
  232. out.close();
  233. when(response.failIfNotSuccessful()).thenReturn(response);
  234. when(response.contentStream()).thenReturn(in);
  235. when(wsClient.call(any(WsRequest.class))).thenReturn(response);
  236. underTest.upload(temp.newFile());
  237. ArgumentCaptor<WsRequest> capture = ArgumentCaptor.forClass(WsRequest.class);
  238. verify(wsClient).call(capture.capture());
  239. WsRequest wsRequest = capture.getValue();
  240. assertThat(wsRequest.getParameters().getKeys()).containsOnly("organization", "projectKey");
  241. assertThat(wsRequest.getParameters().getValue("organization")).isEqualTo("MyOrg");
  242. assertThat(wsRequest.getParameters().getValue("projectKey")).isEqualTo("org.sonarsource.sonarqube:sonarqube");
  243. }
  244. @Test
  245. public void test_send_branches_characteristics() throws Exception {
  246. String orgName = "MyOrg";
  247. when(properties.organizationKey()).thenReturn(Optional.of(orgName));
  248. String branchName = "feature";
  249. when(branchConfiguration.branchName()).thenReturn(branchName);
  250. when(branchConfiguration.branchType()).thenReturn(SHORT);
  251. WsResponse response = mock(WsResponse.class);
  252. PipedOutputStream out = new PipedOutputStream();
  253. PipedInputStream in = new PipedInputStream(out);
  254. Ce.SubmitResponse.newBuilder().build().writeTo(out);
  255. out.close();
  256. when(response.failIfNotSuccessful()).thenReturn(response);
  257. when(response.contentStream()).thenReturn(in);
  258. when(wsClient.call(any(WsRequest.class))).thenReturn(response);
  259. underTest.upload(temp.newFile());
  260. ArgumentCaptor<WsRequest> capture = ArgumentCaptor.forClass(WsRequest.class);
  261. verify(wsClient).call(capture.capture());
  262. WsRequest wsRequest = capture.getValue();
  263. assertThat(wsRequest.getParameters().getKeys()).hasSize(3);
  264. assertThat(wsRequest.getParameters().getValues("organization")).containsExactly(orgName);
  265. assertThat(wsRequest.getParameters().getValues("projectKey")).containsExactly("org.sonarsource.sonarqube:sonarqube");
  266. assertThat(wsRequest.getParameters().getValues("characteristic"))
  267. .containsExactlyInAnyOrder("branch=" + branchName, "branchType=" + SHORT.name());
  268. }
  269. @Test
  270. public void send_pull_request_characteristic() throws Exception {
  271. String orgName = "MyOrg";
  272. when(properties.organizationKey()).thenReturn(Optional.of(orgName));
  273. String branchName = "feature";
  274. String pullRequestId = "pr-123";
  275. when(branchConfiguration.branchName()).thenReturn(branchName);
  276. when(branchConfiguration.branchType()).thenReturn(PULL_REQUEST);
  277. when(branchConfiguration.pullRequestKey()).thenReturn(pullRequestId);
  278. WsResponse response = mock(WsResponse.class);
  279. PipedOutputStream out = new PipedOutputStream();
  280. PipedInputStream in = new PipedInputStream(out);
  281. Ce.SubmitResponse.newBuilder().build().writeTo(out);
  282. out.close();
  283. when(response.failIfNotSuccessful()).thenReturn(response);
  284. when(response.contentStream()).thenReturn(in);
  285. when(wsClient.call(any(WsRequest.class))).thenReturn(response);
  286. underTest.upload(temp.newFile());
  287. ArgumentCaptor<WsRequest> capture = ArgumentCaptor.forClass(WsRequest.class);
  288. verify(wsClient).call(capture.capture());
  289. WsRequest wsRequest = capture.getValue();
  290. assertThat(wsRequest.getParameters().getKeys()).hasSize(3);
  291. assertThat(wsRequest.getParameters().getValues("organization")).containsExactly(orgName);
  292. assertThat(wsRequest.getParameters().getValues("projectKey")).containsExactly("org.sonarsource.sonarqube:sonarqube");
  293. assertThat(wsRequest.getParameters().getValues("characteristic"))
  294. .containsExactlyInAnyOrder("pullRequest=" + pullRequestId);
  295. }
  296. }