3 * Copyright (C) 2009-2020 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.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;
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;
44 public class CloseIssuesOnRemovedComponentsVisitorTest {
47 public TemporaryFolder temp = new TemporaryFolder();
49 ComponentIssuesLoader issuesLoader = mock(ComponentIssuesLoader.class);
50 ComponentsWithUnprocessedIssues componentsWithUnprocessedIssues = mock(ComponentsWithUnprocessedIssues.class);
51 IssueLifecycle issueLifecycle = mock(IssueLifecycle.class);
52 IssueCache issueCache;
53 VisitorsCrawler underTest;
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)));
63 public void close_issue() {
64 String fileUuid = "FILE1";
65 String issueUuid = "ABCD";
67 when(componentsWithUnprocessedIssues.getUuids()).thenReturn(newHashSet(fileUuid));
68 DefaultIssue issue = new DefaultIssue().setKey(issueUuid);
69 when(issuesLoader.loadOpenIssues(fileUuid)).thenReturn(Collections.singletonList(issue));
71 underTest.visit(ReportComponent.builder(PROJECT, 1).build());
73 verify(issueLifecycle).doAutomaticTransition(issue);
74 CloseableIterator<DefaultIssue> issues = issueCache.traverse();
75 assertThat(issues.hasNext()).isTrue();
77 DefaultIssue result = issues.next();
78 assertThat(result.key()).isEqualTo(issueUuid);
79 assertThat(result.isBeingClosed()).isTrue();
80 assertThat(result.isOnDisabledRule()).isFalse();
84 public void nothing_to_do_when_no_uuid_in_queue() {
85 when(componentsWithUnprocessedIssues.getUuids()).thenReturn(Collections.emptySet());
87 underTest.visit(ReportComponent.builder(PROJECT, 1).build());
89 verifyZeroInteractions(issueLifecycle);
90 CloseableIterator<DefaultIssue> issues = issueCache.traverse();
91 assertThat(issues.hasNext()).isFalse();
95 public void do_nothing_on_directory() {
96 underTest.visit(ReportComponent.builder(DIRECTORY, 1).build());
98 verifyZeroInteractions(issueLifecycle);
99 CloseableIterator<DefaultIssue> issues = issueCache.traverse();
100 assertThat(issues.hasNext()).isFalse();
104 public void do_nothing_on_file() {
105 underTest.visit(ReportComponent.builder(FILE, 1).build());
107 verifyZeroInteractions(issueLifecycle);
108 CloseableIterator<DefaultIssue> issues = issueCache.traverse();
109 assertThat(issues.hasNext()).isFalse();