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.

ReportAnalysisFailureNotificationExecutionListenerIT.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.ce.notification;
  21. import java.time.Duration;
  22. import java.time.temporal.ChronoUnit;
  23. import java.util.Arrays;
  24. import java.util.Optional;
  25. import java.util.Random;
  26. import javax.annotation.Nullable;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.mockito.ArgumentCaptor;
  30. import org.sonar.api.notifications.Notification;
  31. import org.sonar.api.resources.Qualifiers;
  32. import org.sonar.api.utils.System2;
  33. import org.sonar.ce.task.CeTask;
  34. import org.sonar.ce.task.CeTaskResult;
  35. import org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotification;
  36. import org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotificationBuilder;
  37. import org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotificationSerializer;
  38. import org.sonar.db.DbClient;
  39. import org.sonar.db.DbTester;
  40. import org.sonar.db.RowNotFoundException;
  41. import org.sonar.db.ce.CeActivityDto;
  42. import org.sonar.db.ce.CeQueueDto;
  43. import org.sonar.db.ce.CeTaskTypes;
  44. import org.sonar.db.component.BranchDto;
  45. import org.sonar.db.component.ComponentDto;
  46. import org.sonar.db.component.ComponentTesting;
  47. import org.sonar.db.component.ProjectData;
  48. import org.sonar.db.project.CreationMethod;
  49. import org.sonar.db.project.ProjectDto;
  50. import org.sonar.server.notification.NotificationService;
  51. import static java.util.Collections.singleton;
  52. import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
  53. import static org.assertj.core.api.Assertions.assertThat;
  54. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  55. import static org.assertj.core.api.Assertions.fail;
  56. import static org.mockito.ArgumentMatchers.any;
  57. import static org.mockito.ArgumentMatchers.same;
  58. import static org.mockito.Mockito.mock;
  59. import static org.mockito.Mockito.verify;
  60. import static org.mockito.Mockito.verifyNoInteractions;
  61. import static org.mockito.Mockito.when;
  62. import static org.sonar.db.component.ComponentTesting.newDirectory;
  63. public class ReportAnalysisFailureNotificationExecutionListenerIT {
  64. @Rule
  65. public DbTester dbTester = DbTester.create(System2.INSTANCE);
  66. private final Random random = new Random();
  67. private final DbClient dbClient = dbTester.getDbClient();
  68. private final NotificationService notificationService = mock(NotificationService.class);
  69. private final ReportAnalysisFailureNotificationSerializer serializer = mock(ReportAnalysisFailureNotificationSerializer.class);
  70. private final System2 system2 = mock(System2.class);
  71. private final DbClient dbClientMock = mock(DbClient.class);
  72. private final CeTask ceTaskMock = mock(CeTask.class);
  73. private final Throwable throwableMock = mock(Throwable.class);
  74. private final CeTaskResult ceTaskResultMock = mock(CeTaskResult.class);
  75. private final ReportAnalysisFailureNotificationExecutionListener fullMockedUnderTest = new ReportAnalysisFailureNotificationExecutionListener(
  76. notificationService, dbClientMock, serializer, system2);
  77. private final ReportAnalysisFailureNotificationExecutionListener underTest = new ReportAnalysisFailureNotificationExecutionListener(
  78. notificationService, dbClient, serializer, system2);
  79. @Test
  80. public void onStart_has_no_effect() {
  81. CeTask mockedCeTask = mock(CeTask.class);
  82. fullMockedUnderTest.onStart(mockedCeTask);
  83. verifyNoInteractions(mockedCeTask, notificationService, dbClientMock, serializer, system2);
  84. }
  85. @Test
  86. public void onEnd_has_no_effect_if_status_is_SUCCESS() {
  87. fullMockedUnderTest.onEnd(ceTaskMock, CeActivityDto.Status.SUCCESS, randomDuration(), ceTaskResultMock, throwableMock);
  88. verifyNoInteractions(ceTaskMock, ceTaskResultMock, throwableMock, notificationService, dbClientMock, serializer, system2);
  89. }
  90. @Test
  91. public void onEnd_has_no_effect_if_CeTask_type_is_not_report() {
  92. when(ceTaskMock.getType()).thenReturn(randomAlphanumeric(12));
  93. fullMockedUnderTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), ceTaskResultMock, throwableMock);
  94. verifyNoInteractions(ceTaskResultMock, throwableMock, notificationService, dbClientMock, serializer, system2);
  95. }
  96. @Test
  97. public void onEnd_has_no_effect_if_CeTask_has_no_component_uuid() {
  98. when(ceTaskMock.getType()).thenReturn(CeTaskTypes.REPORT);
  99. fullMockedUnderTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), ceTaskResultMock, throwableMock);
  100. verifyNoInteractions(ceTaskResultMock, throwableMock, notificationService, dbClientMock, serializer, system2);
  101. }
  102. @Test
  103. public void onEnd_has_no_effect_if_there_is_no_subscriber_for_ReportAnalysisFailureNotification_type() {
  104. ProjectData projectData = dbTester.components().insertPrivateProject();
  105. when(ceTaskMock.getType()).thenReturn(CeTaskTypes.REPORT);
  106. when(ceTaskMock.getComponent()).thenReturn(Optional.of(new CeTask.Component(projectData.getMainBranchDto().getUuid(), null, null)));
  107. when(notificationService.hasProjectSubscribersForTypes(projectData.projectUuid(), singleton(ReportAnalysisFailureNotification.class)))
  108. .thenReturn(false);
  109. underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), ceTaskResultMock, throwableMock);
  110. verifyNoInteractions(ceTaskResultMock, throwableMock, serializer, system2);
  111. }
  112. @Test
  113. public void onEnd_fails_with_ISE_if_project_does_not_exist_in_DB() {
  114. BranchDto branchDto = dbTester.components().insertProjectBranch(new ProjectDto().setUuid("uuid").setKee("kee").setName("name"));
  115. String componentUuid = branchDto.getUuid();
  116. when(ceTaskMock.getType()).thenReturn(CeTaskTypes.REPORT);
  117. when(ceTaskMock.getComponent()).thenReturn(Optional.of(new CeTask.Component(componentUuid, null, null)));
  118. when(notificationService.hasProjectSubscribersForTypes(branchDto.getProjectUuid(), singleton(ReportAnalysisFailureNotification.class)))
  119. .thenReturn(true);
  120. Duration randomDuration = randomDuration();
  121. assertThatThrownBy(() -> underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration, ceTaskResultMock, throwableMock))
  122. .isInstanceOf(IllegalStateException.class)
  123. .hasMessage("Could not find project uuid " + branchDto.getProjectUuid());
  124. }
  125. @Test
  126. public void onEnd_fails_with_ISE_if_branch_does_not_exist_in_DB() {
  127. String componentUuid = randomAlphanumeric(6);
  128. ProjectDto project = new ProjectDto().setUuid(componentUuid).setKey(randomAlphanumeric(5)).setQualifier(Qualifiers.PROJECT).setCreationMethod(CreationMethod.LOCAL_API);
  129. dbTester.getDbClient().projectDao().insert(dbTester.getSession(), project);
  130. dbTester.getSession().commit();
  131. when(ceTaskMock.getType()).thenReturn(CeTaskTypes.REPORT);
  132. when(ceTaskMock.getComponent()).thenReturn(Optional.of(new CeTask.Component(componentUuid, null, null)));
  133. when(notificationService.hasProjectSubscribersForTypes(componentUuid, singleton(ReportAnalysisFailureNotification.class)))
  134. .thenReturn(true);
  135. Duration randomDuration = randomDuration();
  136. assertThatThrownBy(() -> underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration, ceTaskResultMock, throwableMock))
  137. .isInstanceOf(IllegalStateException.class)
  138. .hasMessage("Could not find a branch with uuid " + componentUuid);
  139. }
  140. @Test
  141. public void onEnd_fails_with_IAE_if_component_is_not_a_branch() {
  142. when(ceTaskMock.getType()).thenReturn(CeTaskTypes.REPORT);
  143. ComponentDto mainBranch = dbTester.components().insertPrivateProject().getMainBranchComponent();
  144. ComponentDto directory = dbTester.components().insertComponent(newDirectory(mainBranch, randomAlphanumeric(12)));
  145. ComponentDto file = dbTester.components().insertComponent(ComponentTesting.newFileDto(mainBranch));
  146. ComponentDto view = dbTester.components().insertComponent(ComponentTesting.newPortfolio());
  147. ComponentDto subView = dbTester.components().insertComponent(ComponentTesting.newSubPortfolio(view));
  148. ComponentDto projectCopy = dbTester.components().insertComponent(ComponentTesting.newProjectCopy(mainBranch, subView));
  149. ComponentDto application = dbTester.components().insertComponent(ComponentTesting.newApplication());
  150. Arrays.asList(directory, file, view, subView, projectCopy, application)
  151. .forEach(component -> {
  152. when(ceTaskMock.getComponent()).thenReturn(Optional.of(new CeTask.Component(component.uuid(), null, null)));
  153. when(notificationService.hasProjectSubscribersForTypes(component.uuid(), singleton(ReportAnalysisFailureNotification.class)))
  154. .thenReturn(true);
  155. Duration randomDuration = randomDuration();
  156. try {
  157. underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration, ceTaskResultMock, throwableMock);
  158. fail("An IllegalArgumentException should have been thrown for component " + component);
  159. } catch (IllegalStateException e) {
  160. assertThat(e.getMessage()).isIn("Could not find project uuid " + component.uuid(), "Could not find a branch with uuid " + component.uuid());
  161. }
  162. });
  163. }
  164. @Test
  165. public void onEnd_fails_with_RowNotFoundException_if_activity_for_task_does_not_exist_in_DB() {
  166. ProjectData projectData = dbTester.components().insertPrivateProject();
  167. ComponentDto mainBranch = projectData.getMainBranchComponent();
  168. String taskUuid = randomAlphanumeric(6);
  169. when(ceTaskMock.getType()).thenReturn(CeTaskTypes.REPORT);
  170. when(ceTaskMock.getUuid()).thenReturn(taskUuid);
  171. when(ceTaskMock.getComponent()).thenReturn(Optional.of(new CeTask.Component(mainBranch.uuid(), null, null)));
  172. when(notificationService.hasProjectSubscribersForTypes(projectData.projectUuid(), singleton(ReportAnalysisFailureNotification.class)))
  173. .thenReturn(true);
  174. Duration randomDuration = randomDuration();
  175. assertThatThrownBy(() -> underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration, ceTaskResultMock, throwableMock))
  176. .isInstanceOf(RowNotFoundException.class)
  177. .hasMessage("CeActivity with uuid '" + taskUuid + "' not found");
  178. }
  179. @Test
  180. public void onEnd_creates_notification_with_data_from_activity_and_project_and_deliver_it() {
  181. String taskUuid = randomAlphanumeric(12);
  182. int createdAt = random.nextInt(999_999);
  183. long executedAt = random.nextInt(999_999);
  184. ProjectData project = initMocksToPassConditions(taskUuid, createdAt, executedAt);
  185. Notification notificationMock = mockSerializer();
  186. underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), ceTaskResultMock, throwableMock);
  187. ArgumentCaptor<ReportAnalysisFailureNotificationBuilder> notificationCaptor = verifyAndCaptureSerializedNotification();
  188. verify(notificationService).deliver(same(notificationMock));
  189. ReportAnalysisFailureNotificationBuilder reportAnalysisFailureNotificationBuilder = notificationCaptor.getValue();
  190. ReportAnalysisFailureNotificationBuilder.Project notificationProject = reportAnalysisFailureNotificationBuilder.project();
  191. assertThat(notificationProject.name()).isEqualTo(project.getProjectDto().getName());
  192. assertThat(notificationProject.key()).isEqualTo(project.getProjectDto().getKey());
  193. assertThat(notificationProject.uuid()).isEqualTo(project.getProjectDto().getUuid());
  194. assertThat(notificationProject.branchName()).isNull();
  195. ReportAnalysisFailureNotificationBuilder.Task notificationTask = reportAnalysisFailureNotificationBuilder.task();
  196. assertThat(notificationTask.uuid()).isEqualTo(taskUuid);
  197. assertThat(notificationTask.createdAt()).isEqualTo(createdAt);
  198. assertThat(notificationTask.failedAt()).isEqualTo(executedAt);
  199. }
  200. @Test
  201. public void onEnd_shouldCreateNotificationWithDataFromActivity_whenNonMainBranchIsFailing() {
  202. String taskUuid = randomAlphanumeric(12);
  203. int createdAt = random.nextInt(999_999);
  204. long executedAt = random.nextInt(999_999);
  205. ProjectData project = random.nextBoolean() ? dbTester.components().insertPrivateProject() : dbTester.components().insertPublicProject();
  206. ComponentDto branchComponent = dbTester.components().insertProjectBranch(project.getMainBranchComponent(), b -> b.setKey("otherbranch"));
  207. initMocksToPassConditionsForBranch(branchComponent, project, taskUuid, createdAt, executedAt);
  208. Notification notificationMock = mockSerializer();
  209. underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), ceTaskResultMock, throwableMock);
  210. ArgumentCaptor<ReportAnalysisFailureNotificationBuilder> notificationCaptor = verifyAndCaptureSerializedNotification();
  211. verify(notificationService).deliver(same(notificationMock));
  212. ReportAnalysisFailureNotificationBuilder reportAnalysisFailureNotificationBuilder = notificationCaptor.getValue();
  213. ReportAnalysisFailureNotificationBuilder.Project notificationProject = reportAnalysisFailureNotificationBuilder.project();
  214. assertThat(notificationProject.name()).isEqualTo(project.getProjectDto().getName());
  215. assertThat(notificationProject.key()).isEqualTo(project.getProjectDto().getKey());
  216. assertThat(notificationProject.uuid()).isEqualTo(project.getProjectDto().getUuid());
  217. assertThat(notificationProject.branchName()).isEqualTo("otherbranch");
  218. ReportAnalysisFailureNotificationBuilder.Task notificationTask = reportAnalysisFailureNotificationBuilder.task();
  219. assertThat(notificationTask.uuid()).isEqualTo(taskUuid);
  220. assertThat(notificationTask.createdAt()).isEqualTo(createdAt);
  221. assertThat(notificationTask.failedAt()).isEqualTo(executedAt);
  222. }
  223. @Test
  224. public void onEnd_creates_notification_with_error_message_from_Throwable_argument_message() {
  225. initMocksToPassConditions(randomAlphanumeric(12), random.nextInt(999_999), (long) random.nextInt(999_999));
  226. String message = randomAlphanumeric(66);
  227. when(throwableMock.getMessage()).thenReturn(message);
  228. underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), ceTaskResultMock, throwableMock);
  229. ArgumentCaptor<ReportAnalysisFailureNotificationBuilder> notificationCaptor = verifyAndCaptureSerializedNotification();
  230. ReportAnalysisFailureNotificationBuilder reportAnalysisFailureNotificationBuilder = notificationCaptor.getValue();
  231. assertThat(reportAnalysisFailureNotificationBuilder.errorMessage()).isEqualTo(message);
  232. }
  233. @Test
  234. public void onEnd_creates_notification_with_null_error_message_if_Throwable_is_null() {
  235. String taskUuid = randomAlphanumeric(12);
  236. initMocksToPassConditions(taskUuid, random.nextInt(999_999), (long) random.nextInt(999_999));
  237. Notification notificationMock = mockSerializer();
  238. underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), ceTaskResultMock, null);
  239. verify(notificationService).deliver(same(notificationMock));
  240. ArgumentCaptor<ReportAnalysisFailureNotificationBuilder> notificationCaptor = verifyAndCaptureSerializedNotification();
  241. ReportAnalysisFailureNotificationBuilder reportAnalysisFailureNotificationBuilder = notificationCaptor.getValue();
  242. assertThat(reportAnalysisFailureNotificationBuilder.errorMessage()).isNull();
  243. }
  244. @Test
  245. public void onEnd_ignores_null_CeTaskResult_argument() {
  246. String taskUuid = randomAlphanumeric(12);
  247. initMocksToPassConditions(taskUuid, random.nextInt(999_999), (long) random.nextInt(999_999));
  248. Notification notificationMock = mockSerializer();
  249. underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), null, null);
  250. verify(notificationService).deliver(same(notificationMock));
  251. }
  252. @Test
  253. public void onEnd_ignores_CeTaskResult_argument() {
  254. String taskUuid = randomAlphanumeric(12);
  255. initMocksToPassConditions(taskUuid, random.nextInt(999_999), (long) random.nextInt(999_999));
  256. Notification notificationMock = mockSerializer();
  257. underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), ceTaskResultMock, null);
  258. verify(notificationService).deliver(same(notificationMock));
  259. verifyNoInteractions(ceTaskResultMock);
  260. }
  261. @Test
  262. public void onEnd_uses_system_data_as_failedAt_if_task_has_no_executedAt() {
  263. String taskUuid = randomAlphanumeric(12);
  264. initMocksToPassConditions(taskUuid, random.nextInt(999_999), null);
  265. long now = random.nextInt(999_999);
  266. when(system2.now()).thenReturn(now);
  267. Notification notificationMock = mockSerializer();
  268. underTest.onEnd(ceTaskMock, CeActivityDto.Status.FAILED, randomDuration(), ceTaskResultMock, null);
  269. verify(notificationService).deliver(same(notificationMock));
  270. ArgumentCaptor<ReportAnalysisFailureNotificationBuilder> notificationCaptor = verifyAndCaptureSerializedNotification();
  271. assertThat(notificationCaptor.getValue().task().failedAt()).isEqualTo(now);
  272. }
  273. private ReportAnalysisFailureNotification mockSerializer() {
  274. ReportAnalysisFailureNotification notificationMock = mock(ReportAnalysisFailureNotification.class);
  275. when(serializer.toNotification(any(ReportAnalysisFailureNotificationBuilder.class))).thenReturn(notificationMock);
  276. return notificationMock;
  277. }
  278. private ProjectData initMocksToPassConditions(String taskUuid, int createdAt, @Nullable Long executedAt) {
  279. ProjectData projectData = random.nextBoolean() ? dbTester.components().insertPrivateProject() : dbTester.components().insertPublicProject();
  280. mockCeTask(taskUuid, createdAt, executedAt, projectData, projectData.getMainBranchComponent());
  281. return projectData;
  282. }
  283. private ComponentDto initMocksToPassConditionsForBranch(ComponentDto branchComponent, ProjectData projectData, String taskUuid, int createdAt, @Nullable Long executedAt) {
  284. mockCeTask(taskUuid, createdAt, executedAt, projectData, branchComponent);
  285. return branchComponent;
  286. }
  287. private void mockCeTask(String taskUuid, int createdAt, @org.jetbrains.annotations.Nullable Long executedAt, ProjectData projectData, ComponentDto branchComponent) {
  288. when(ceTaskMock.getType()).thenReturn(CeTaskTypes.REPORT);
  289. when(ceTaskMock.getComponent()).thenReturn(Optional.of(new CeTask.Component(branchComponent.uuid(), null, null)));
  290. when(ceTaskMock.getUuid()).thenReturn(taskUuid);
  291. when(notificationService.hasProjectSubscribersForTypes(projectData.projectUuid(), singleton(ReportAnalysisFailureNotification.class)))
  292. .thenReturn(true);
  293. insertActivityDto(taskUuid, createdAt, executedAt, branchComponent);
  294. }
  295. private void insertActivityDto(String taskUuid, int createdAt, @Nullable Long executedAt, ComponentDto branch) {
  296. dbClient.ceActivityDao().insert(dbTester.getSession(), new CeActivityDto(new CeQueueDto()
  297. .setUuid(taskUuid)
  298. .setTaskType(CeTaskTypes.REPORT)
  299. .setComponentUuid(branch.uuid())
  300. .setCreatedAt(createdAt))
  301. .setExecutedAt(executedAt)
  302. .setStatus(CeActivityDto.Status.FAILED));
  303. dbTester.getSession().commit();
  304. }
  305. private ArgumentCaptor<ReportAnalysisFailureNotificationBuilder> verifyAndCaptureSerializedNotification() {
  306. ArgumentCaptor<ReportAnalysisFailureNotificationBuilder> notificationCaptor = ArgumentCaptor.forClass(ReportAnalysisFailureNotificationBuilder.class);
  307. verify(serializer).toNotification(notificationCaptor.capture());
  308. return notificationCaptor;
  309. }
  310. private Duration randomDuration() {
  311. return Duration.of(random.nextLong(), ChronoUnit.MILLIS);
  312. }
  313. }