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