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.

SetTypeActionIT.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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;
  21. import com.google.common.collect.ImmutableMap;
  22. import com.google.common.collect.Lists;
  23. import java.util.Date;
  24. import java.util.Map;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.sonar.api.issue.Issue;
  28. import org.sonar.core.issue.DefaultIssue;
  29. import org.sonar.core.issue.FieldDiffs;
  30. import org.sonar.db.DbTester;
  31. import org.sonar.db.component.BranchDto;
  32. import org.sonar.db.component.ComponentDto;
  33. import org.sonar.db.issue.IssueDto;
  34. import org.sonar.db.issue.IssueTesting;
  35. import org.sonar.db.project.ProjectDto;
  36. import org.sonar.db.rule.RuleDto;
  37. import org.sonar.server.tester.UserSessionRule;
  38. import static org.assertj.core.api.Assertions.assertThat;
  39. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  40. import static org.sonar.api.rules.RuleType.BUG;
  41. import static org.sonar.api.rules.RuleType.VULNERABILITY;
  42. import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
  43. import static org.sonar.api.web.UserRole.USER;
  44. import static org.sonar.core.issue.IssueChangeContext.issueChangeContextByUserBuilder;
  45. import static org.sonar.db.component.ComponentTesting.newFileDto;
  46. import static org.sonar.db.rule.RuleTesting.newRule;
  47. public class SetTypeActionIT {
  48. private static final Date NOW = new Date(10_000_000_000L);
  49. private static final String USER_LOGIN = "john";
  50. @Rule
  51. public UserSessionRule userSession = UserSessionRule.standalone();
  52. @Rule
  53. public DbTester db = DbTester.create();
  54. private IssueFieldsSetter issueUpdater = new IssueFieldsSetter();
  55. private SetTypeAction action = new SetTypeAction(issueUpdater, userSession);
  56. @Test
  57. public void set_type() {
  58. IssueDto issueDto = newIssue().setType(BUG);
  59. DefaultIssue issue = issueDto.toDefaultIssue();
  60. setUserWithBrowseAndAdministerIssuePermission(issueDto);
  61. action.execute(ImmutableMap.of("type", VULNERABILITY.name()),
  62. new ActionContext(issue, issueChangeContextByUserBuilder(NOW, userSession.getUuid()).build(), null));
  63. assertThat(issue.type()).isEqualTo(VULNERABILITY);
  64. assertThat(issue.isChanged()).isTrue();
  65. assertThat(issue.updateDate()).isEqualTo(NOW);
  66. assertThat(issue.mustSendNotifications()).isFalse();
  67. Map<String, FieldDiffs.Diff> change = issue.currentChange().diffs();
  68. assertThat(change.get("type").newValue()).isEqualTo(VULNERABILITY);
  69. assertThat(change.get("type").oldValue()).isEqualTo(BUG);
  70. }
  71. @Test
  72. public void verify_fail_if_parameter_not_found() {
  73. assertThatThrownBy(() -> action.verify(ImmutableMap.of("unknwown", VULNERABILITY.name()), Lists.newArrayList(), userSession))
  74. .isInstanceOf(IllegalArgumentException.class)
  75. .hasMessage("Missing parameter : 'type'");
  76. }
  77. @Test
  78. public void verify_fail_if_type_is_invalid() {
  79. assertThatThrownBy(() -> action.verify(ImmutableMap.of("type", "unknown"), Lists.newArrayList(), userSession))
  80. .isInstanceOf(IllegalArgumentException.class)
  81. .hasMessage("Unknown type : unknown");
  82. }
  83. @Test
  84. public void support_only_unresolved_issues() {
  85. IssueDto issueDto = newIssue().setType(BUG);
  86. DefaultIssue issue = issueDto.toDefaultIssue();
  87. setUserWithBrowseAndAdministerIssuePermission(issueDto);
  88. assertThat(action.supports(issue.setResolution(null))).isTrue();
  89. assertThat(action.supports(issue.setResolution(Issue.RESOLUTION_FIXED))).isFalse();
  90. }
  91. @Test
  92. public void support_only_issues_with_issue_admin_permission() {
  93. IssueDto authorizedIssueDto = newIssue().setType(BUG);
  94. DefaultIssue authorizedIssue = authorizedIssueDto.toDefaultIssue();
  95. setUserWithBrowseAndAdministerIssuePermission(authorizedIssueDto);
  96. DefaultIssue unauthorizedIssue = newIssue().setType(BUG).toDefaultIssue();
  97. assertThat(action.supports(authorizedIssue.setResolution(null))).isTrue();
  98. assertThat(action.supports(unauthorizedIssue.setResolution(null))).isFalse();
  99. }
  100. private void setUserWithBrowseAndAdministerIssuePermission(IssueDto issueDto) {
  101. BranchDto branchDto = db.getDbClient().branchDao().selectByUuid(db.getSession(), issueDto.getProjectUuid())
  102. .orElseThrow(() -> new IllegalStateException("Couldn't find branch :" + issueDto.getProjectUuid()));
  103. ProjectDto project = db.getDbClient().projectDao().selectByUuid(db.getSession(), branchDto.getProjectUuid()).get();
  104. userSession.logIn(USER_LOGIN)
  105. .addProjectPermission(ISSUE_ADMIN, project)
  106. .addProjectPermission(USER, project)
  107. .registerBranches(branchDto);
  108. }
  109. private IssueDto newIssue() {
  110. RuleDto rule = db.rules().insert(newRule());
  111. ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
  112. ComponentDto file = db.components().insertComponent(newFileDto(project));
  113. return IssueTesting.newIssue(rule, project, file);
  114. }
  115. }