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 com.google.common.collect.ImmutableMap;
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.sonar.api.config.internal.MapSettings;
29 import org.sonar.api.utils.System2;
30 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
31 import org.sonar.ce.task.projectanalysis.component.Component;
32 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
33 import org.sonar.ce.task.projectanalysis.component.ReportModulesPath;
34 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRulesHolderRule;
35 import org.sonar.core.issue.DefaultIssue;
36 import org.sonar.db.DbClient;
37 import org.sonar.db.DbTester;
38 import org.sonar.db.component.ComponentDto;
39 import org.sonar.db.issue.IssueDto;
40 import org.sonar.db.rule.RuleDefinitionDto;
41 import org.sonar.server.issue.IssueFieldsSetter;
43 import static org.assertj.core.api.Assertions.assertThat;
44 import static org.assertj.core.api.Assertions.tuple;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.when;
47 import static org.sonar.api.utils.DateUtils.parseDate;
48 import static org.sonar.db.component.ComponentTesting.newDirectory;
49 import static org.sonar.db.component.ComponentTesting.newFileDto;
50 import static org.sonar.db.component.ComponentTesting.newModuleDto;
52 public class ProjectTrackerBaseLazyInputTest {
54 private static final Date ANALYSIS_DATE = parseDate("2016-06-01");
57 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule().setAnalysisDate(ANALYSIS_DATE);
59 public DbTester dbTester = DbTester.create(System2.INSTANCE);
61 public ActiveRulesHolderRule activeRulesHolderRule = new ActiveRulesHolderRule();
63 public RuleRepositoryRule ruleRepositoryRule = new RuleRepositoryRule();
65 private DbClient dbClient = dbTester.getDbClient();
66 private ProjectTrackerBaseLazyInput underTest;
67 private RuleDefinitionDto rule;
68 private ComponentDto rootProjectDto;
69 private ComponentIssuesLoader issuesLoader = new ComponentIssuesLoader(dbTester.getDbClient(), ruleRepositoryRule, activeRulesHolderRule, new MapSettings().asConfig(),
70 System2.INSTANCE, mock(IssueChangesToDeleteRepository.class));
71 private ReportModulesPath reportModulesPath;
74 public void prepare() {
75 rule = dbTester.rules().insert();
76 ruleRepositoryRule.add(rule.getKey());
77 rootProjectDto = dbTester.components().insertPublicProject();
78 ReportComponent rootProject = ReportComponent.builder(Component.Type.FILE, 1)
79 .setKey(rootProjectDto.getDbKey())
80 .setUuid(rootProjectDto.uuid()).build();
81 reportModulesPath = mock(ReportModulesPath.class);
82 underTest = new ProjectTrackerBaseLazyInput(analysisMetadataHolder, mock(ComponentsWithUnprocessedIssues.class), dbClient, new IssueFieldsSetter(), issuesLoader,
83 reportModulesPath, rootProject);
87 public void return_only_open_project_issues_if_no_modules_and_folders() {
88 ComponentDto file = dbTester.components().insertComponent(newFileDto(rootProjectDto));
89 IssueDto openIssueOnProject = dbTester.issues().insert(rule, rootProjectDto, rootProjectDto, i -> i.setStatus("OPEN").setResolution(null));
90 IssueDto closedIssueOnProject = dbTester.issues().insert(rule, rootProjectDto, rootProjectDto, i -> i.setStatus("CLOSED").setResolution("FIXED"));
91 IssueDto openIssue1OnFile = dbTester.issues().insert(rule, rootProjectDto, file, i -> i.setStatus("OPEN").setResolution(null));
93 assertThat(underTest.loadIssues()).extracting(DefaultIssue::key).containsOnly(openIssueOnProject.getKey());
97 public void migrate_and_return_folder_issues_on_root_project() {
98 when(reportModulesPath.get()).thenReturn(Collections.emptyMap());
99 ComponentDto folder = dbTester.components().insertComponent(newDirectory(rootProjectDto, "src"));
100 ComponentDto file = dbTester.components().insertComponent(newFileDto(rootProjectDto));
101 IssueDto openIssueOnProject = dbTester.issues().insert(rule, rootProjectDto, rootProjectDto, i -> i.setStatus("OPEN").setResolution(null));
102 IssueDto openIssueOnDir = dbTester.issues().insert(rule, rootProjectDto, folder, i -> i.setStatus("OPEN").setMessage("Issue on dir").setResolution(null));
103 IssueDto openIssue1OnFile = dbTester.issues().insert(rule, rootProjectDto, file, i -> i.setStatus("OPEN").setResolution(null));
105 assertThat(underTest.loadIssues()).extracting(DefaultIssue::key, DefaultIssue::getMessage)
106 .containsExactlyInAnyOrder(
107 tuple(openIssueOnProject.getKey(), openIssueOnProject.getMessage()),
108 tuple(openIssueOnDir.getKey(), "[src] Issue on dir"));
113 public void migrate_and_return_module_and_folder_issues_on_module() {
114 ComponentDto module = dbTester.components().insertComponent(newModuleDto(rootProjectDto).setPath("moduleAInDb"));
115 when(reportModulesPath.get()).thenReturn(ImmutableMap.of(module.getDbKey(), "moduleAInReport"));
116 ComponentDto folder = dbTester.components().insertComponent(newDirectory(module, "src"));
117 ComponentDto file = dbTester.components().insertComponent(newFileDto(module));
118 IssueDto openIssueOnProject = dbTester.issues().insert(rule, rootProjectDto, rootProjectDto, i -> i.setStatus("OPEN").setResolution(null));
119 IssueDto openIssueOnModule = dbTester.issues().insert(rule, rootProjectDto, module, i -> i.setStatus("OPEN").setMessage("Issue on module").setResolution(null));
120 IssueDto openIssueOnDir = dbTester.issues().insert(rule, rootProjectDto, folder, i -> i.setStatus("OPEN").setMessage("Issue on dir").setResolution(null));
121 IssueDto openIssue1OnFile = dbTester.issues().insert(rule, rootProjectDto, file, i -> i.setStatus("OPEN").setResolution(null));
123 assertThat(underTest.loadIssues()).extracting(DefaultIssue::key, DefaultIssue::getMessage)
124 .containsExactlyInAnyOrder(
125 tuple(openIssueOnProject.getKey(), openIssueOnProject.getMessage()),
126 tuple(openIssueOnModule.getKey(), "[moduleAInReport] Issue on module"),
127 tuple(openIssueOnDir.getKey(), "[moduleAInReport/src] Issue on dir"));
132 public void use_db_path_if_module_missing_in_report() {
133 ComponentDto module = dbTester.components().insertComponent(newModuleDto(rootProjectDto).setPath("moduleAInDb"));
134 when(reportModulesPath.get()).thenReturn(Collections.emptyMap());
135 ComponentDto folder = dbTester.components().insertComponent(newDirectory(module, "src"));
136 IssueDto openIssueOnProject = dbTester.issues().insert(rule, rootProjectDto, rootProjectDto, i -> i.setStatus("OPEN").setResolution(null));
137 IssueDto openIssueOnModule = dbTester.issues().insert(rule, rootProjectDto, module, i -> i.setStatus("OPEN").setMessage("Issue on module").setResolution(null));
138 IssueDto openIssueOnDir = dbTester.issues().insert(rule, rootProjectDto, folder, i -> i.setStatus("OPEN").setMessage("Issue on dir").setResolution(null));
140 assertThat(underTest.loadIssues()).extracting(DefaultIssue::key, DefaultIssue::getMessage)
141 .containsExactlyInAnyOrder(
142 tuple(openIssueOnProject.getKey(), openIssueOnProject.getMessage()),
143 tuple(openIssueOnModule.getKey(), "[moduleAInDb] Issue on module"),
144 tuple(openIssueOnDir.getKey(), "[moduleAInDb/src] Issue on dir"));
149 public void empty_path_if_module_missing_in_report_and_db_and_for_slash_folder() {
150 ComponentDto module = dbTester.components().insertComponent(newModuleDto(rootProjectDto).setPath(null));
151 when(reportModulesPath.get()).thenReturn(Collections.emptyMap());
152 ComponentDto folder = dbTester.components().insertComponent(newDirectory(module, "/"));
153 IssueDto openIssueOnProject = dbTester.issues().insert(rule, rootProjectDto, rootProjectDto, i -> i.setStatus("OPEN").setResolution(null));
154 IssueDto openIssueOnModule = dbTester.issues().insert(rule, rootProjectDto, module, i -> i.setStatus("OPEN").setMessage("Issue on module").setResolution(null));
155 IssueDto openIssueOnDir = dbTester.issues().insert(rule, rootProjectDto, folder, i -> i.setStatus("OPEN").setMessage("Issue on dir").setResolution(null));
157 assertThat(underTest.loadIssues()).extracting(DefaultIssue::key, DefaultIssue::getMessage)
158 .containsExactlyInAnyOrder(
159 tuple(openIssueOnProject.getKey(), openIssueOnProject.getMessage()),
160 tuple(openIssueOnModule.getKey(), "Issue on module"),
161 tuple(openIssueOnDir.getKey(), "Issue on dir"));