]> source.dussan.org Git - sonarqube.git/blob
f5880143bd03dc3cc8b38c3f0602c2a3ed4b2003
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.Arrays;
23 import java.util.Collections;
24 import java.util.Date;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.TemporaryFolder;
29 import org.sonar.api.rule.RuleKey;
30 import org.sonar.api.rules.RuleType;
31 import org.sonar.api.utils.System2;
32 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
33 import org.sonar.ce.task.projectanalysis.component.VisitorsCrawler;
34 import org.sonar.core.issue.DefaultIssue;
35 import org.sonar.core.util.CloseableIterator;
36
37 import static com.google.common.collect.Sets.newHashSet;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.verifyZeroInteractions;
42 import static org.mockito.Mockito.when;
43 import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY;
44 import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
45 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
46
47 public class CloseIssuesOnRemovedComponentsVisitorTest {
48
49   @Rule
50   public TemporaryFolder temp = new TemporaryFolder();
51
52   ComponentIssuesLoader issuesLoader = mock(ComponentIssuesLoader.class);
53   ComponentsWithUnprocessedIssues componentsWithUnprocessedIssues = mock(ComponentsWithUnprocessedIssues.class);
54   IssueLifecycle issueLifecycle = mock(IssueLifecycle.class);
55   ProtoIssueCache protoIssueCache;
56   VisitorsCrawler underTest;
57
58   @Before
59   public void setUp() throws Exception {
60     protoIssueCache = new ProtoIssueCache(temp.newFile(), System2.INSTANCE);
61     underTest = new VisitorsCrawler(
62       Arrays.asList(new CloseIssuesOnRemovedComponentsVisitor(issuesLoader, componentsWithUnprocessedIssues, protoIssueCache, issueLifecycle)));
63   }
64
65   @Test
66   public void close_issue() {
67     String fileUuid = "FILE1";
68     String issueUuid = "ABCD";
69
70     when(componentsWithUnprocessedIssues.getUuids()).thenReturn(newHashSet(fileUuid));
71     DefaultIssue issue = new DefaultIssue().setKey(issueUuid).setType(RuleType.BUG).setCreationDate(new Date())
72       .setComponentKey("c").setProjectUuid("u").setProjectKey("k").setRuleKey(RuleKey.of("r", "r")).setStatus("OPEN");
73     when(issuesLoader.loadOpenIssues(fileUuid)).thenReturn(Collections.singletonList(issue));
74
75     underTest.visit(ReportComponent.builder(PROJECT, 1).build());
76
77     verify(issueLifecycle).doAutomaticTransition(issue);
78     CloseableIterator<DefaultIssue> issues = protoIssueCache.traverse();
79     assertThat(issues.hasNext()).isTrue();
80
81     DefaultIssue result = issues.next();
82     assertThat(result.key()).isEqualTo(issueUuid);
83     assertThat(result.isBeingClosed()).isTrue();
84     assertThat(result.isOnDisabledRule()).isFalse();
85   }
86
87   @Test
88   public void nothing_to_do_when_no_uuid_in_queue() {
89     when(componentsWithUnprocessedIssues.getUuids()).thenReturn(Collections.emptySet());
90
91     underTest.visit(ReportComponent.builder(PROJECT, 1).build());
92
93     verifyZeroInteractions(issueLifecycle);
94     CloseableIterator<DefaultIssue> issues = protoIssueCache.traverse();
95     assertThat(issues.hasNext()).isFalse();
96   }
97
98   @Test
99   public void do_nothing_on_directory() {
100     underTest.visit(ReportComponent.builder(DIRECTORY, 1).build());
101
102     verifyZeroInteractions(issueLifecycle);
103     CloseableIterator<DefaultIssue> issues = protoIssueCache.traverse();
104     assertThat(issues.hasNext()).isFalse();
105   }
106
107   @Test
108   public void do_nothing_on_file() {
109     underTest.visit(ReportComponent.builder(FILE, 1).build());
110
111     verifyZeroInteractions(issueLifecycle);
112     CloseableIterator<DefaultIssue> issues = protoIssueCache.traverse();
113     assertThat(issues.hasNext()).isFalse();
114   }
115 }