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.

TransitionServiceIT.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 java.util.Date;
  22. import java.util.List;
  23. import org.junit.Before;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.sonar.core.issue.DefaultIssue;
  27. import org.sonar.core.issue.IssueChangeContext;
  28. import org.sonar.db.DbTester;
  29. import org.sonar.db.component.ComponentDto;
  30. import org.sonar.db.component.ProjectData;
  31. import org.sonar.db.issue.IssueDto;
  32. import org.sonar.db.rule.RuleDto;
  33. import org.sonar.server.issue.workflow.FunctionExecutor;
  34. import org.sonar.server.issue.workflow.IssueWorkflow;
  35. import org.sonar.server.issue.workflow.Transition;
  36. import org.sonar.server.tester.UserSessionRule;
  37. import static org.assertj.core.api.Assertions.assertThat;
  38. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  39. import static org.sonar.api.issue.Issue.STATUS_CONFIRMED;
  40. import static org.sonar.api.issue.Issue.STATUS_OPEN;
  41. import static org.sonar.api.rules.RuleType.CODE_SMELL;
  42. import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
  43. import static org.sonar.core.issue.IssueChangeContext.issueChangeContextByUserBuilder;
  44. import static org.sonar.db.component.ComponentTesting.newFileDto;
  45. public class TransitionServiceIT {
  46. @Rule
  47. public DbTester db = DbTester.create();
  48. @Rule
  49. public UserSessionRule userSession = UserSessionRule.standalone();
  50. private IssueFieldsSetter updater = new IssueFieldsSetter();
  51. private IssueWorkflow workflow = new IssueWorkflow(new FunctionExecutor(updater), updater);
  52. private TransitionService underTest = new TransitionService(userSession, workflow);
  53. @Before
  54. public void setUp() {
  55. workflow.start();
  56. }
  57. @Test
  58. public void list_transitions() {
  59. ProjectData project = db.components().insertPrivateProject();
  60. ComponentDto file = db.components().insertComponent(newFileDto(project.getMainBranchComponent()));
  61. RuleDto rule = db.rules().insert();
  62. IssueDto issue = db.issues().insert(rule, project.getMainBranchComponent(), file, i -> i.setStatus(STATUS_OPEN).setResolution(null).setType(CODE_SMELL));
  63. userSession.logIn().addProjectPermission(ISSUE_ADMIN, project.getProjectDto())
  64. .registerBranches(project.getMainBranchDto());
  65. List<Transition> result = underTest.listTransitions(issue.toDefaultIssue());
  66. assertThat(result).extracting(Transition::key).containsOnly("confirm", "resolve", "falsepositive", "wontfix");
  67. }
  68. @Test
  69. public void list_transitions_returns_empty_list_on_external_issue() {
  70. ProjectData project = db.components().insertPrivateProject();
  71. ComponentDto file = db.components().insertComponent(newFileDto(project.getMainBranchComponent()));
  72. RuleDto externalRule = db.rules().insert(r -> r.setIsExternal(true));
  73. IssueDto externalIssue = db.issues().insert(externalRule, project.getMainBranchComponent(), file, i -> i.setStatus(STATUS_OPEN).setResolution(null).setType(CODE_SMELL));
  74. userSession.logIn().addProjectPermission(ISSUE_ADMIN, project.getProjectDto())
  75. .registerBranches(project.getMainBranchDto());
  76. List<Transition> result = underTest.listTransitions(externalIssue.toDefaultIssue());
  77. assertThat(result).isEmpty();
  78. }
  79. @Test
  80. public void list_transitions_returns_only_transitions_that_do_not_requires_issue_admin_permission() {
  81. ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
  82. ComponentDto file = db.components().insertComponent(newFileDto(project));
  83. RuleDto rule = db.rules().insert();
  84. IssueDto issue = db.issues().insert(rule, project, file, i -> i.setStatus(STATUS_OPEN).setResolution(null).setType(CODE_SMELL));
  85. userSession.logIn();
  86. List<Transition> result = underTest.listTransitions(issue.toDefaultIssue());
  87. assertThat(result).extracting(Transition::key).containsOnly("confirm");
  88. }
  89. @Test
  90. public void list_transitions_returns_nothing_when_not_logged() {
  91. ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
  92. ComponentDto file = db.components().insertComponent(newFileDto(project));
  93. RuleDto rule = db.rules().insert();
  94. IssueDto issue = db.issues().insert(rule, project, file, i -> i.setStatus(STATUS_OPEN).setResolution(null).setType(CODE_SMELL));
  95. List<Transition> result = underTest.listTransitions(issue.toDefaultIssue());
  96. assertThat(result).isEmpty();
  97. }
  98. @Test
  99. public void do_transition() {
  100. ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
  101. ComponentDto file = db.components().insertComponent(newFileDto(project));
  102. RuleDto rule = db.rules().insert();
  103. IssueDto issue = db.issues().insert(rule, project, file, i -> i.setStatus(STATUS_OPEN).setResolution(null).setType(CODE_SMELL));
  104. DefaultIssue defaultIssue = issue.toDefaultIssue();
  105. boolean result = underTest.doTransition(defaultIssue, issueChangeContextByUserBuilder(new Date(), "user_uuid").build(), "confirm");
  106. assertThat(result).isTrue();
  107. assertThat(defaultIssue.status()).isEqualTo(STATUS_CONFIRMED);
  108. }
  109. @Test
  110. public void do_transition_fail_on_external_issue() {
  111. ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
  112. ComponentDto file = db.components().insertComponent(newFileDto(project));
  113. RuleDto externalRule = db.rules().insert(r -> r.setIsExternal(true));
  114. IssueDto externalIssue = db.issues().insert(externalRule, project, file, i -> i.setStatus(STATUS_OPEN).setResolution(null).setType(CODE_SMELL));
  115. DefaultIssue defaultIssue = externalIssue.toDefaultIssue();
  116. IssueChangeContext issueChangeContext = issueChangeContextByUserBuilder(new Date(), "user_uuid").build();
  117. assertThatThrownBy(() -> underTest.doTransition(defaultIssue, issueChangeContext, "confirm"))
  118. .isInstanceOf(IllegalArgumentException.class)
  119. .hasMessage("Transition is not allowed on issues imported from external rule engines");
  120. }
  121. }