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.

TransitionActionIT.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 org.junit.Before;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.sonar.api.issue.Issue;
  28. import org.sonar.api.rules.RuleType;
  29. import org.sonar.core.issue.DefaultIssue;
  30. import org.sonar.core.util.Uuids;
  31. import org.sonar.db.component.BranchDto;
  32. import org.sonar.db.component.BranchType;
  33. import org.sonar.db.component.ComponentDto;
  34. import org.sonar.db.component.ComponentTesting;
  35. import org.sonar.db.issue.IssueDto;
  36. import org.sonar.db.issue.IssueTesting;
  37. import org.sonar.db.project.ProjectDto;
  38. import org.sonar.db.rule.RuleDto;
  39. import org.sonar.server.issue.workflow.FunctionExecutor;
  40. import org.sonar.server.issue.workflow.IssueWorkflow;
  41. import org.sonar.server.tester.UserSessionRule;
  42. import static java.util.Collections.emptyList;
  43. import static org.assertj.core.api.Assertions.assertThat;
  44. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  45. import static org.mockito.Mockito.mock;
  46. import static org.mockito.Mockito.when;
  47. import static org.sonar.api.issue.Issue.STATUS_CLOSED;
  48. import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
  49. import static org.sonar.core.issue.IssueChangeContext.issueChangeContextByUserBuilder;
  50. import static org.sonar.db.component.ComponentTesting.newFileDto;
  51. import static org.sonar.db.rule.RuleTesting.newRule;
  52. public class TransitionActionIT {
  53. @Rule
  54. public UserSessionRule userSession = UserSessionRule.standalone();
  55. private final IssueFieldsSetter updater = new IssueFieldsSetter();
  56. private final IssueWorkflow workflow = new IssueWorkflow(new FunctionExecutor(updater), updater);
  57. private final TransitionService transitionService = new TransitionService(userSession, workflow);
  58. private final Action.Context context = mock(Action.Context.class);
  59. private final DefaultIssue issue = newIssue().toDefaultIssue();
  60. private final TransitionAction action = new TransitionAction(transitionService);
  61. @Before
  62. public void setUp() {
  63. workflow.start();
  64. when(context.issue()).thenReturn(issue);
  65. when(context.issueChangeContext()).thenReturn(issueChangeContextByUserBuilder(new Date(), "user_uuid").build());
  66. }
  67. @Test
  68. public void execute() {
  69. loginAndAddProjectPermission("john", ISSUE_ADMIN);
  70. if (issue.type() == RuleType.SECURITY_HOTSPOT) {
  71. // this transition is not done for hotspots
  72. issue.setType(RuleType.CODE_SMELL);
  73. }
  74. issue.setStatus(Issue.STATUS_RESOLVED);
  75. issue.setResolution(Issue.RESOLUTION_FIXED);
  76. action.execute(ImmutableMap.of("transition", "reopen"), context);
  77. assertThat(issue.status()).isEqualTo(Issue.STATUS_REOPENED);
  78. assertThat(issue.resolution()).isNull();
  79. }
  80. @Test
  81. public void does_not_execute_if_transition_is_not_available() {
  82. loginAndAddProjectPermission("john", ISSUE_ADMIN);
  83. issue.setStatus(STATUS_CLOSED);
  84. action.execute(ImmutableMap.of("transition", "reopen"), context);
  85. assertThat(issue.status()).isEqualTo(STATUS_CLOSED);
  86. }
  87. @Test
  88. public void test_verify() {
  89. assertThat(action.verify(ImmutableMap.of("transition", "reopen"), emptyList(), userSession)).isTrue();
  90. assertThat(action.verify(ImmutableMap.of("transition", "close"), emptyList(), userSession)).isTrue();
  91. }
  92. @Test
  93. public void fail_to_verify_when_parameter_not_found() {
  94. assertThatThrownBy(() -> action.verify(ImmutableMap.of("unknown", "reopen"), Lists.newArrayList(), userSession))
  95. .isInstanceOf(IllegalArgumentException.class)
  96. .hasMessage("Missing parameter : 'transition'");
  97. }
  98. @Test
  99. public void should_support_all_issues() {
  100. assertThat(action.supports(new DefaultIssue().setResolution(null))).isTrue();
  101. assertThat(action.supports(new DefaultIssue().setResolution(Issue.RESOLUTION_FIXED))).isTrue();
  102. }
  103. private IssueDto newIssue() {
  104. RuleDto rule = newRule().setUuid(Uuids.createFast());
  105. ComponentDto project = ComponentTesting.newPrivateProjectDto();
  106. ComponentDto file = (newFileDto(project));
  107. return IssueTesting.newIssue(rule, project, file);
  108. }
  109. private void loginAndAddProjectPermission(String login, String permission) {
  110. ProjectDto projectDto = ComponentTesting.newProjectDto();
  111. BranchDto branchDto = ComponentTesting.newBranchDto(projectDto.getUuid(), BranchType.BRANCH).setIsMain(true).setUuid(issue.projectUuid());
  112. userSession.logIn(login).addProjectPermission(permission, projectDto)
  113. .registerBranches(branchDto);
  114. }
  115. }