]> source.dussan.org Git - sonarqube.git/blob
51164c3080c13de09381df829ca452ff7b734571
[sonarqube.git] /
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.ce.task.projectanalysis.issue;
21
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;
34
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;
41
42 public class TransitionIssuesToAnticipatedStatesVisitorTest {
43
44   private final IssueLifecycle issueLifecycle = mock(IssueLifecycle.class);
45
46   private final AnticipatedTransitionRepository anticipatedTransitionRepository = mock(AnticipatedTransitionRepository.class);
47
48   private final TransitionIssuesToAnticipatedStatesVisitor underTest = new TransitionIssuesToAnticipatedStatesVisitor(anticipatedTransitionRepository, issueLifecycle);
49
50   @Test
51   public void givenMatchingAnticipatedTransitions_transitionsShouldBeAppliedToIssues() {
52     Component component = getComponent(Component.Type.FILE);
53     when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component)).thenReturn(getAnticipatedTransitions("projectKey", "fileName"));
54
55     DefaultIssue issue = getDefaultIssue(1, "abcdefghi", "issue message");
56
57     underTest.beforeComponent(component);
58     underTest.onIssue(component, issue);
59     underTest.afterComponent(component);
60
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");
65   }
66
67   @Test
68   public void givenNonMatchingAnticipatedTransitions_transitionsAreNotAppliedToIssues() {
69     Component component = getComponent(Component.Type.FILE);
70     when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component)).thenReturn(getAnticipatedTransitions("projectKey", "fileName"));
71
72     DefaultIssue issue = getDefaultIssue(2, "abcdefghf", "another issue message");
73
74     underTest.beforeComponent(component);
75     underTest.onIssue(component, issue);
76     underTest.afterComponent(component);
77
78     assertThat(issue.isBeingClosed()).isFalse();
79     assertThat(issue.hasAnticipatedTransitions()).isFalse();
80     verifyNoInteractions(issueLifecycle);
81   }
82
83   @Test
84   public void givenAFileComponent_theRepositoryIsHitForFetchingAnticipatedTransitions() {
85     Component component = getComponent(Component.Type.FILE);
86     when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component)).thenReturn(Collections.emptyList());
87
88     underTest.beforeComponent(component);
89
90     verify(anticipatedTransitionRepository).getAnticipatedTransitionByComponent(component);
91   }
92
93   @Test
94   public void givenAProjecComponent_theRepositoryIsNotQueriedForAnticipatedTransitions() {
95     Component component = getComponent(PROJECT);
96     when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component)).thenReturn(Collections.emptyList());
97
98     underTest.beforeComponent(component);
99
100     verifyNoInteractions(anticipatedTransitionRepository);
101   }
102
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());
105   }
106
107   private Component getComponent(Component.Type type) {
108     ComponentImpl.Builder builder = ComponentImpl.builder(type)
109       .setUuid("componentUuid")
110       .setKey("projectKey:filename")
111       .setName("filename")
112       .setStatus(Component.Status.ADDED)
113       .setShortName("filename")
114       .setReportAttributes(mock(ReportAttributes.class));
115
116     if (PROJECT.equals(type)) {
117       builder.setProjectAttributes(mock(ProjectAttributes.class));
118     }
119
120     return builder.build();
121   }
122
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"));
129     return defaultIssue;
130   }
131
132 }