3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.issue;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.stream.Collectors;
25 import java.util.stream.Stream;
26 import org.junit.Test;
27 import org.sonar.api.rule.RuleKey;
28 import org.sonar.ce.task.projectanalysis.component.Component;
29 import org.sonar.ce.task.projectanalysis.component.ComponentImpl;
30 import org.sonar.ce.task.projectanalysis.component.ProjectAttributes;
31 import org.sonar.ce.task.projectanalysis.component.ReportAttributes;
32 import org.sonar.core.issue.AnticipatedTransition;
33 import org.sonar.core.issue.DefaultIssue;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.verify;
38 import static org.mockito.Mockito.verifyNoInteractions;
39 import static org.mockito.Mockito.when;
40 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
42 public class TransitionIssuesToAnticipatedStatesVisitorTest {
44 private final IssueLifecycle issueLifecycle = mock(IssueLifecycle.class);
46 private final AnticipatedTransitionRepository anticipatedTransitionRepository = mock(AnticipatedTransitionRepository.class);
48 private final TransitionIssuesToAnticipatedStatesVisitor underTest = new TransitionIssuesToAnticipatedStatesVisitor(anticipatedTransitionRepository, issueLifecycle);
51 public void givenMatchingAnticipatedTransitions_transitionsShouldBeAppliedToIssues() {
52 Component component = getComponent(Component.Type.FILE);
53 when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component)).thenReturn(getAnticipatedTransitions("projectKey", "fileName"));
55 DefaultIssue issue = getDefaultIssue(1, "abcdefghi", "issue message");
57 underTest.beforeComponent(component);
58 underTest.onIssue(component, issue);
59 underTest.afterComponent(component);
61 assertThat(issue.isBeingClosed()).isTrue();
62 assertThat(issue.hasAnticipatedTransitions()).isTrue();
63 verify(issueLifecycle).doManualTransition(issue, "wontfix", "admin");
64 verify(issueLifecycle).addComment(issue, "doing the transition in an anticipated way", "admin");
68 public void givenNonMatchingAnticipatedTransitions_transitionsAreNotAppliedToIssues() {
69 Component component = getComponent(Component.Type.FILE);
70 when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component)).thenReturn(getAnticipatedTransitions("projectKey", "fileName"));
72 DefaultIssue issue = getDefaultIssue(2, "abcdefghf", "another issue message");
74 underTest.beforeComponent(component);
75 underTest.onIssue(component, issue);
76 underTest.afterComponent(component);
78 assertThat(issue.isBeingClosed()).isFalse();
79 assertThat(issue.hasAnticipatedTransitions()).isFalse();
80 verifyNoInteractions(issueLifecycle);
84 public void givenAFileComponent_theRepositoryIsHitForFetchingAnticipatedTransitions() {
85 Component component = getComponent(Component.Type.FILE);
86 when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component)).thenReturn(Collections.emptyList());
88 underTest.beforeComponent(component);
90 verify(anticipatedTransitionRepository).getAnticipatedTransitionByComponent(component);
94 public void givenAProjecComponent_theRepositoryIsNotQueriedForAnticipatedTransitions() {
95 Component component = getComponent(PROJECT);
96 when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component)).thenReturn(Collections.emptyList());
98 underTest.beforeComponent(component);
100 verifyNoInteractions(anticipatedTransitionRepository);
103 private Collection<AnticipatedTransition> getAnticipatedTransitions(String projecKey, String fileName) {
104 return Stream.of(new AnticipatedTransition(projecKey, null, "admin", RuleKey.parse("repo:id"), "issue message", fileName, 1, "abcdefghi", "wontfix", "doing the transition in an anticipated way")).collect(Collectors.toList());
107 private Component getComponent(Component.Type type) {
108 ComponentImpl.Builder builder = ComponentImpl.builder(type)
109 .setUuid("componentUuid")
110 .setKey("projectKey:filename")
112 .setStatus(Component.Status.ADDED)
113 .setShortName("filename")
114 .setReportAttributes(mock(ReportAttributes.class));
116 if (PROJECT.equals(type)) {
117 builder.setProjectAttributes(mock(ProjectAttributes.class));
120 return builder.build();
123 private DefaultIssue getDefaultIssue(Integer line, String hash, String message) {
124 DefaultIssue defaultIssue = new DefaultIssue();
125 defaultIssue.setLine(line);
126 defaultIssue.setChecksum(hash);
127 defaultIssue.setMessage(message);
128 defaultIssue.setRuleKey(RuleKey.of("repo", "id"));