3 * Copyright (C) 2009-2019 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.base.Optional;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
26 import org.sonar.ce.task.projectanalysis.component.Component;
27 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
28 import org.sonar.ce.task.projectanalysis.component.ReportModulesPath;
29 import org.sonar.ce.task.projectanalysis.filemove.MovedFilesRepository;
30 import org.sonar.core.issue.DefaultIssue;
31 import org.sonar.core.issue.tracking.Input;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.source.FileSourceDao;
35 import org.sonar.server.issue.IssueFieldsSetter;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.mockito.ArgumentMatchers.any;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.times;
41 import static org.mockito.Mockito.verify;
42 import static org.mockito.Mockito.when;
44 public class TrackerBaseInputFactoryTest {
45 private static final String FILE_UUID = "uuid";
46 private static final String DIR_UUID = "dir";
47 private static final ReportComponent FILE = ReportComponent.builder(Component.Type.FILE, 1).setUuid(FILE_UUID).build();
48 private static final ReportComponent FOLDER = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid(DIR_UUID).build();
51 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
53 private ComponentIssuesLoader issuesLoader = mock(ComponentIssuesLoader.class);
54 private DbClient dbClient = mock(DbClient.class);
55 private DbSession dbSession = mock(DbSession.class);
56 private FileSourceDao fileSourceDao = mock(FileSourceDao.class);
58 private MovedFilesRepository movedFilesRepository = mock(MovedFilesRepository.class);
60 private TrackerBaseInputFactory underTest = new TrackerBaseInputFactory(issuesLoader, dbClient, movedFilesRepository, mock(ReportModulesPath.class), analysisMetadataHolder,
61 new IssueFieldsSetter(), mock(ComponentsWithUnprocessedIssues.class));
64 public void setUp() throws Exception {
65 when(dbClient.openSession(false)).thenReturn(dbSession);
66 when(dbClient.fileSourceDao()).thenReturn(fileSourceDao);
67 when(movedFilesRepository.getOriginalFile(any(Component.class)))
68 .thenReturn(Optional.absent());
72 public void create_returns_Input_which_retrieves_lines_hashes_of_specified_file_component_when_it_has_no_original_file() {
73 underTest.create(FILE).getLineHashSequence();
75 verify(fileSourceDao).selectLineHashes(dbSession, FILE_UUID);
79 public void create_returns_empty_Input_for_folders() {
80 Input<DefaultIssue> input = underTest.create(FOLDER);
81 assertThat(input.getIssues()).isEmpty();
85 public void create_returns_Input_which_retrieves_lines_hashes_of_original_file_of_component_when_it_has_one() {
86 String originalUuid = "original uuid";
88 when(movedFilesRepository.getOriginalFile(FILE)).thenReturn(
89 Optional.of(new MovedFilesRepository.OriginalFile(6542, originalUuid, "original key")));
91 underTest.create(FILE).getLineHashSequence();
93 verify(fileSourceDao).selectLineHashes(dbSession, originalUuid);
94 verify(fileSourceDao, times(0)).selectLineHashes(dbSession, FILE_UUID);
98 public void create_returns_Input_which_retrieves_issues_of_specified_file_component_when_it_has_no_original_file() {
99 underTest.create(FILE).getIssues();
101 verify(issuesLoader).loadOpenIssues(FILE_UUID);
105 public void create_returns_Input_which_retrieves_issues_of_original_file_of_component_when_it_has_one() {
106 String originalUuid = "original uuid";
108 when(movedFilesRepository.getOriginalFile(FILE)).thenReturn(
109 Optional.of(new MovedFilesRepository.OriginalFile(6542, originalUuid, "original key")));
111 underTest.create(FILE).getIssues();
113 verify(issuesLoader).loadOpenIssues(originalUuid);
114 verify(issuesLoader, times(0)).loadOpenIssues(FILE_UUID);