3 * Copyright (C) 2009-2017 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.server.computation.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.core.issue.DefaultIssue;
30 import org.sonar.core.util.CloseableIterator;
31 import org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor;
32 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
33 import org.sonar.server.computation.task.projectanalysis.component.VisitorsCrawler;
35 import static com.google.common.collect.Sets.newHashSet;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.verifyZeroInteractions;
40 import static org.mockito.Mockito.when;
41 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.DIRECTORY;
42 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.FILE;
43 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.MODULE;
44 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT;
46 public class CloseIssuesOnRemovedComponentsVisitorTest {
49 public TemporaryFolder temp = new TemporaryFolder();
51 ComponentIssuesLoader issuesLoader = mock(ComponentIssuesLoader.class);
52 ComponentsWithUnprocessedIssues componentsWithUnprocessedIssues = mock(ComponentsWithUnprocessedIssues.class);
53 IssueLifecycle issueLifecycle = mock(IssueLifecycle.class);
54 IssueCache issueCache;
55 VisitorsCrawler underTest;
58 public void setUp() throws Exception {
59 issueCache = new IssueCache(temp.newFile(), System2.INSTANCE);
60 underTest = new VisitorsCrawler(Arrays.<ComponentVisitor>asList(new CloseIssuesOnRemovedComponentsVisitor(issuesLoader, componentsWithUnprocessedIssues, issueCache, issueLifecycle)));
64 public void close_issue() throws Exception {
65 String fileUuid = "FILE1";
66 String issueUuid = "ABCD";
68 when(componentsWithUnprocessedIssues.getUuids()).thenReturn(newHashSet(fileUuid));
69 DefaultIssue issue = new DefaultIssue().setKey(issueUuid);
70 when(issuesLoader.loadForComponentUuid(fileUuid)).thenReturn(Collections.singletonList(issue));
72 underTest.visit(ReportComponent.builder(PROJECT, 1).build());
74 verify(issueLifecycle).doAutomaticTransition(issue);
75 CloseableIterator<DefaultIssue> issues = issueCache.traverse();
76 assertThat(issues.hasNext()).isTrue();
78 DefaultIssue result = issues.next();
79 assertThat(result.key()).isEqualTo(issueUuid);
80 assertThat(result.isBeingClosed()).isTrue();
81 assertThat(result.isOnDisabledRule()).isFalse();
85 public void nothing_to_do_when_no_uuid_in_queue() throws Exception {
86 when(componentsWithUnprocessedIssues.getUuids()).thenReturn(Collections.<String>emptySet());
88 underTest.visit(ReportComponent.builder(PROJECT, 1).build());
90 verifyZeroInteractions(issueLifecycle);
91 CloseableIterator<DefaultIssue> issues = issueCache.traverse();
92 assertThat(issues.hasNext()).isFalse();
96 public void do_nothing_on_module() throws Exception {
97 underTest.visit(ReportComponent.builder(MODULE, 1).build());
99 verifyZeroInteractions(issueLifecycle);
100 CloseableIterator<DefaultIssue> issues = issueCache.traverse();
101 assertThat(issues.hasNext()).isFalse();
105 public void do_nothing_on_directory() throws Exception {
106 underTest.visit(ReportComponent.builder(DIRECTORY, 1).build());
108 verifyZeroInteractions(issueLifecycle);
109 CloseableIterator<DefaultIssue> issues = issueCache.traverse();
110 assertThat(issues.hasNext()).isFalse();
114 public void do_nothing_on_file() throws Exception {
115 underTest.visit(ReportComponent.builder(FILE, 1).build());
117 verifyZeroInteractions(issueLifecycle);
118 CloseableIterator<DefaultIssue> issues = issueCache.traverse();
119 assertThat(issues.hasNext()).isFalse();