3 * Copyright (C) 2009-2022 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 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;
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;
47 public class CloseIssuesOnRemovedComponentsVisitorTest {
50 public TemporaryFolder temp = new TemporaryFolder();
52 ComponentIssuesLoader issuesLoader = mock(ComponentIssuesLoader.class);
53 ComponentsWithUnprocessedIssues componentsWithUnprocessedIssues = mock(ComponentsWithUnprocessedIssues.class);
54 IssueLifecycle issueLifecycle = mock(IssueLifecycle.class);
55 ProtoIssueCache protoIssueCache;
56 VisitorsCrawler underTest;
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)));
66 public void close_issue() {
67 String fileUuid = "FILE1";
68 String issueUuid = "ABCD";
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));
75 underTest.visit(ReportComponent.builder(PROJECT, 1).build());
77 verify(issueLifecycle).doAutomaticTransition(issue);
78 CloseableIterator<DefaultIssue> issues = protoIssueCache.traverse();
79 assertThat(issues.hasNext()).isTrue();
81 DefaultIssue result = issues.next();
82 assertThat(result.key()).isEqualTo(issueUuid);
83 assertThat(result.isBeingClosed()).isTrue();
84 assertThat(result.isOnDisabledRule()).isFalse();
88 public void nothing_to_do_when_no_uuid_in_queue() {
89 when(componentsWithUnprocessedIssues.getUuids()).thenReturn(Collections.emptySet());
91 underTest.visit(ReportComponent.builder(PROJECT, 1).build());
93 verifyZeroInteractions(issueLifecycle);
94 CloseableIterator<DefaultIssue> issues = protoIssueCache.traverse();
95 assertThat(issues.hasNext()).isFalse();
99 public void do_nothing_on_directory() {
100 underTest.visit(ReportComponent.builder(DIRECTORY, 1).build());
102 verifyZeroInteractions(issueLifecycle);
103 CloseableIterator<DefaultIssue> issues = protoIssueCache.traverse();
104 assertThat(issues.hasNext()).isFalse();
108 public void do_nothing_on_file() {
109 underTest.visit(ReportComponent.builder(FILE, 1).build());
111 verifyZeroInteractions(issueLifecycle);
112 CloseableIterator<DefaultIssue> issues = protoIssueCache.traverse();
113 assertThat(issues.hasNext()).isFalse();