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.

IssueUpdaterTest.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.server.issue.ws;
  21. import java.util.Date;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.ExpectedException;
  25. import org.mockito.ArgumentCaptor;
  26. import org.sonar.api.rule.RuleStatus;
  27. import org.sonar.api.utils.System2;
  28. import org.sonar.core.issue.DefaultIssue;
  29. import org.sonar.core.issue.IssueChangeContext;
  30. import org.sonar.db.DbClient;
  31. import org.sonar.db.DbTester;
  32. import org.sonar.db.component.BranchType;
  33. import org.sonar.db.component.ComponentDto;
  34. import org.sonar.db.issue.IssueDto;
  35. import org.sonar.db.rule.RuleDefinitionDto;
  36. import org.sonar.db.user.UserDto;
  37. import org.sonar.server.es.EsTester;
  38. import org.sonar.server.issue.IssueFieldsSetter;
  39. import org.sonar.server.issue.TestIssueChangePostProcessor;
  40. import org.sonar.server.issue.WebIssueStorage;
  41. import org.sonar.server.issue.index.IssueIndexer;
  42. import org.sonar.server.issue.index.IssueIteratorFactory;
  43. import org.sonar.server.issue.notification.IssuesChangesNotification;
  44. import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder;
  45. import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue;
  46. import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange;
  47. import org.sonar.server.issue.notification.IssuesChangesNotificationSerializer;
  48. import org.sonar.server.notification.NotificationManager;
  49. import org.sonar.server.organization.DefaultOrganizationProvider;
  50. import org.sonar.server.organization.TestDefaultOrganizationProvider;
  51. import org.sonar.server.rule.DefaultRuleFinder;
  52. import static org.assertj.core.api.Assertions.assertThat;
  53. import static org.mockito.Mockito.mock;
  54. import static org.mockito.Mockito.verify;
  55. import static org.mockito.Mockito.verifyZeroInteractions;
  56. import static org.sonar.api.issue.Issue.RESOLUTION_FIXED;
  57. import static org.sonar.api.rule.Severity.BLOCKER;
  58. import static org.sonar.api.rule.Severity.MAJOR;
  59. import static org.sonar.db.component.BranchType.BRANCH;
  60. import static org.sonar.db.component.ComponentTesting.newFileDto;
  61. import static org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.projectBranchOf;
  62. import static org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.projectOf;
  63. import static org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.ruleOf;
  64. import static org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.userOf;
  65. public class IssueUpdaterTest {
  66. private System2 system2 = mock(System2.class);
  67. @Rule
  68. public ExpectedException expectedException = ExpectedException.none();
  69. @Rule
  70. public DbTester db = DbTester.create(system2);
  71. @Rule
  72. public EsTester es = EsTester.create();
  73. private DbClient dbClient = db.getDbClient();
  74. private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
  75. private IssueFieldsSetter issueFieldsSetter = new IssueFieldsSetter();
  76. private NotificationManager notificationManager = mock(NotificationManager.class);
  77. private ArgumentCaptor<IssuesChangesNotification> notificationArgumentCaptor = ArgumentCaptor.forClass(IssuesChangesNotification.class);
  78. private IssueIndexer issueIndexer = new IssueIndexer(es.client(), dbClient, new IssueIteratorFactory(dbClient));
  79. private TestIssueChangePostProcessor issueChangePostProcessor = new TestIssueChangePostProcessor();
  80. private IssuesChangesNotificationSerializer issuesChangesSerializer = new IssuesChangesNotificationSerializer();
  81. private IssueUpdater underTest = new IssueUpdater(dbClient,
  82. new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(dbClient, defaultOrganizationProvider), issueIndexer), notificationManager, issueChangePostProcessor,
  83. issuesChangesSerializer);
  84. @Test
  85. public void update_issue() {
  86. DefaultIssue issue = db.issues().insertIssue(i -> i.setSeverity(MAJOR)).toDefaultIssue();
  87. UserDto user = db.users().insertUser();
  88. IssueChangeContext context = IssueChangeContext.createUser(new Date(), user.getUuid());
  89. issueFieldsSetter.setSeverity(issue, BLOCKER, context);
  90. underTest.saveIssueAndPreloadSearchResponseData(db.getSession(), issue, context, false);
  91. IssueDto issueReloaded = dbClient.issueDao().selectByKey(db.getSession(), issue.key()).get();
  92. assertThat(issueReloaded.getSeverity()).isEqualTo(BLOCKER);
  93. }
  94. @Test
  95. public void verify_notification_without_resolution() {
  96. UserDto assignee = db.users().insertUser();
  97. RuleDefinitionDto rule = db.rules().insertIssueRule();
  98. ComponentDto project = db.components().insertMainBranch();
  99. ComponentDto file = db.components().insertComponent(newFileDto(project));
  100. DefaultIssue issue = db.issues().insertIssue(rule, project, file,
  101. t -> t.setSeverity(MAJOR).setAssigneeUuid(assignee.getUuid()))
  102. .toDefaultIssue();
  103. UserDto changeAuthor = db.users().insertUser();
  104. IssueChangeContext context = IssueChangeContext.createUser(new Date(), changeAuthor.getUuid());
  105. issueFieldsSetter.setSeverity(issue, BLOCKER, context);
  106. underTest.saveIssueAndPreloadSearchResponseData(db.getSession(), issue, context, false);
  107. verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture());
  108. IssuesChangesNotification issueChangeNotification = notificationArgumentCaptor.getValue();
  109. IssuesChangesNotificationBuilder builder = issuesChangesSerializer.from(issueChangeNotification);
  110. assertThat(builder.getIssues()).hasSize(1);
  111. ChangedIssue changedIssue = builder.getIssues().iterator().next();
  112. assertThat(changedIssue.getKey()).isEqualTo(issue.key());
  113. assertThat(changedIssue.getNewStatus()).isEqualTo(issue.status());
  114. assertThat(changedIssue.getNewResolution()).isEmpty();
  115. assertThat(changedIssue.getAssignee()).contains(userOf(assignee));
  116. assertThat(changedIssue.getRule()).isEqualTo(ruleOf(rule));
  117. assertThat(changedIssue.getProject()).isEqualTo(projectOf(project));
  118. assertThat(builder.getChange()).isEqualTo(new UserChange(issue.updateDate().getTime(), userOf(changeAuthor)));
  119. }
  120. @Test
  121. public void verify_notification_with_resolution() {
  122. UserDto assignee = db.users().insertUser();
  123. RuleDefinitionDto rule = db.rules().insertIssueRule();
  124. ComponentDto project = db.components().insertMainBranch();
  125. ComponentDto file = db.components().insertComponent(newFileDto(project));
  126. DefaultIssue issue = db.issues().insertIssue(rule, project, file,
  127. t -> t.setSeverity(MAJOR).setAssigneeUuid(assignee.getUuid()))
  128. .toDefaultIssue();
  129. UserDto changeAuthor = db.users().insertUser();
  130. IssueChangeContext context = IssueChangeContext.createUser(new Date(), changeAuthor.getUuid());
  131. issueFieldsSetter.setResolution(issue, RESOLUTION_FIXED, context);
  132. underTest.saveIssueAndPreloadSearchResponseData(db.getSession(), issue, context, false);
  133. verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture());
  134. IssuesChangesNotification issueChangeNotification = notificationArgumentCaptor.getValue();
  135. IssuesChangesNotificationBuilder builder = issuesChangesSerializer.from(issueChangeNotification);
  136. assertThat(builder.getIssues()).hasSize(1);
  137. ChangedIssue changedIssue = builder.getIssues().iterator().next();
  138. assertThat(changedIssue.getKey()).isEqualTo(issue.key());
  139. assertThat(changedIssue.getNewStatus()).isEqualTo(issue.status());
  140. assertThat(changedIssue.getNewResolution()).contains(RESOLUTION_FIXED);
  141. assertThat(changedIssue.getAssignee()).contains(userOf(assignee));
  142. assertThat(changedIssue.getRule()).isEqualTo(ruleOf(rule));
  143. assertThat(changedIssue.getProject()).isEqualTo(projectOf(project));
  144. assertThat(builder.getChange()).isEqualTo(new UserChange(issue.updateDate().getTime(), userOf(changeAuthor)));
  145. }
  146. @Test
  147. public void verify_notification_on_branch() {
  148. RuleDefinitionDto rule = db.rules().insertIssueRule();
  149. ComponentDto project = db.components().insertMainBranch();
  150. ComponentDto branch = db.components().insertProjectBranch(project, t -> t.setBranchType(BRANCH));
  151. ComponentDto file = db.components().insertComponent(newFileDto(branch));
  152. DefaultIssue issue = db.issues().insertIssue(rule, branch, file,
  153. t -> t.setSeverity(MAJOR)).toDefaultIssue();
  154. UserDto changeAuthor = db.users().insertUser();
  155. IssueChangeContext context = IssueChangeContext.createUser(new Date(), changeAuthor.getUuid());
  156. issueFieldsSetter.setSeverity(issue, BLOCKER, context);
  157. underTest.saveIssueAndPreloadSearchResponseData(db.getSession(), issue, context, false);
  158. verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture());
  159. IssuesChangesNotification issueChangeNotification = notificationArgumentCaptor.getValue();
  160. IssuesChangesNotificationBuilder builder = issuesChangesSerializer.from(issueChangeNotification);
  161. assertThat(builder.getIssues()).hasSize(1);
  162. ChangedIssue changedIssue = builder.getIssues().iterator().next();
  163. assertThat(changedIssue.getKey()).isEqualTo(issue.key());
  164. assertThat(changedIssue.getNewStatus()).isEqualTo(issue.status());
  165. assertThat(changedIssue.getNewResolution()).isEmpty();
  166. assertThat(changedIssue.getAssignee()).isEmpty();
  167. assertThat(changedIssue.getRule()).isEqualTo(ruleOf(rule));
  168. assertThat(changedIssue.getProject()).isEqualTo(projectBranchOf(db, branch));
  169. assertThat(builder.getChange()).isEqualTo(new UserChange(issue.updateDate().getTime(), userOf(changeAuthor)));
  170. }
  171. @Test
  172. public void verify_no_notification_on_pr() {
  173. RuleDefinitionDto rule = db.rules().insertIssueRule();
  174. ComponentDto project = db.components().insertMainBranch();
  175. ComponentDto branch = db.components().insertProjectBranch(project, t -> t.setBranchType(BranchType.PULL_REQUEST));
  176. ComponentDto file = db.components().insertComponent(newFileDto(branch));
  177. DefaultIssue issue = db.issues().insertIssue(rule, branch, file, t -> t.setSeverity(MAJOR)).toDefaultIssue();
  178. IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid");
  179. issueFieldsSetter.setSeverity(issue, BLOCKER, context);
  180. underTest.saveIssueAndPreloadSearchResponseData(db.getSession(), issue, context, false);
  181. verifyZeroInteractions(notificationManager);
  182. }
  183. @Test
  184. public void verify_notification_when_issue_is_linked_on_removed_rule() {
  185. RuleDefinitionDto rule = db.rules().insertIssueRule(r -> r.setStatus(RuleStatus.REMOVED));
  186. ComponentDto project = db.components().insertMainBranch();
  187. ComponentDto file = db.components().insertComponent(newFileDto(project));
  188. DefaultIssue issue = db.issues().insertIssue(rule, project, file, t -> t.setSeverity(MAJOR)).toDefaultIssue();
  189. IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid");
  190. issueFieldsSetter.setSeverity(issue, BLOCKER, context);
  191. underTest.saveIssueAndPreloadSearchResponseData(db.getSession(), issue, context, false);
  192. verifyZeroInteractions(notificationManager);
  193. }
  194. @Test
  195. public void verify_notification_when_assignee_has_changed() {
  196. UserDto oldAssignee = db.users().insertUser();
  197. RuleDefinitionDto rule = db.rules().insertIssueRule();
  198. ComponentDto project = db.components().insertMainBranch();
  199. ComponentDto file = db.components().insertComponent(newFileDto(project));
  200. DefaultIssue issue = db.issues().insertIssue(rule, project, file, t -> t.setAssigneeUuid(oldAssignee.getUuid()))
  201. .toDefaultIssue();
  202. UserDto changeAuthor = db.users().insertUser();
  203. IssueChangeContext context = IssueChangeContext.createUser(new Date(), changeAuthor.getUuid());
  204. UserDto newAssignee = db.users().insertUser();
  205. issueFieldsSetter.assign(issue, newAssignee, context);
  206. underTest.saveIssueAndPreloadSearchResponseData(db.getSession(), issue, context, false);
  207. verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture());
  208. IssuesChangesNotification issueChangeNotification = notificationArgumentCaptor.getValue();
  209. IssuesChangesNotificationBuilder builder = issuesChangesSerializer.from(issueChangeNotification);
  210. assertThat(builder.getIssues()).hasSize(1);
  211. ChangedIssue changedIssue = builder.getIssues().iterator().next();
  212. assertThat(changedIssue.getKey()).isEqualTo(issue.key());
  213. assertThat(changedIssue.getNewStatus()).isEqualTo(issue.status());
  214. assertThat(changedIssue.getNewResolution()).isEmpty();
  215. assertThat(changedIssue.getAssignee()).contains(userOf(newAssignee));
  216. assertThat(changedIssue.getRule()).isEqualTo(ruleOf(rule));
  217. assertThat(changedIssue.getProject()).isEqualTo(projectOf(project));
  218. assertThat(builder.getChange()).isEqualTo(new UserChange(issue.updateDate().getTime(), userOf(changeAuthor)));
  219. }
  220. @Test
  221. public void saveIssue_populates_specified_SearchResponseData_with_rule_project_and_component_retrieved_from_DB() {
  222. RuleDefinitionDto rule = db.rules().insertIssueRule();
  223. ComponentDto project = db.components().insertMainBranch();
  224. ComponentDto file = db.components().insertComponent(newFileDto(project));
  225. IssueDto issueDto = db.issues().insertIssue(rule, project, file);
  226. DefaultIssue issue = issueDto.setSeverity(MAJOR).toDefaultIssue();
  227. UserDto changeAuthor = db.users().insertUser();
  228. IssueChangeContext context = IssueChangeContext.createUser(new Date(), changeAuthor.getUuid());
  229. issueFieldsSetter.setSeverity(issue, BLOCKER, context);
  230. SearchResponseData preloadedSearchResponseData = underTest.saveIssueAndPreloadSearchResponseData(db.getSession(), issue, context, true);
  231. assertThat(preloadedSearchResponseData.getIssues())
  232. .hasSize(1);
  233. assertThat(preloadedSearchResponseData.getIssues().iterator().next())
  234. .isNotSameAs(issueDto);
  235. assertThat(preloadedSearchResponseData.getRules())
  236. .extracting(RuleDefinitionDto::getKey)
  237. .containsOnly(rule.getKey());
  238. assertThat(preloadedSearchResponseData.getComponents())
  239. .extracting(ComponentDto::uuid)
  240. .containsOnly(project.uuid(), file.uuid());
  241. assertThat(issueChangePostProcessor.calledComponents()).containsExactlyInAnyOrder(file);
  242. }
  243. @Test
  244. public void saveIssue_populates_specified_SearchResponseData_with_no_rule_but_with_project_and_component_if_rule_is_removed() {
  245. RuleDefinitionDto rule = db.rules().insertIssueRule(r -> r.setStatus(RuleStatus.REMOVED));
  246. ComponentDto project = db.components().insertMainBranch();
  247. ComponentDto file = db.components().insertComponent(newFileDto(project));
  248. IssueDto issueDto = db.issues().insertIssue(rule, project, file);
  249. DefaultIssue issue = issueDto.setSeverity(MAJOR).toDefaultIssue();
  250. IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid");
  251. issueFieldsSetter.setSeverity(issue, BLOCKER, context);
  252. SearchResponseData preloadedSearchResponseData = underTest.saveIssueAndPreloadSearchResponseData(db.getSession(), issue, context, false);
  253. assertThat(preloadedSearchResponseData.getIssues())
  254. .hasSize(1);
  255. assertThat(preloadedSearchResponseData.getIssues().iterator().next())
  256. .isNotSameAs(issueDto);
  257. assertThat(preloadedSearchResponseData.getRules()).isNullOrEmpty();
  258. assertThat(preloadedSearchResponseData.getComponents())
  259. .extracting(ComponentDto::uuid)
  260. .containsOnly(project.uuid(), file.uuid());
  261. }
  262. }