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.Collections;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.ce.task.projectanalysis.component.Component;
27 import org.sonar.core.issue.DefaultIssue;
28 import org.sonar.core.issue.tracking.Input;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.component.ComponentDto;
31 import org.sonar.db.component.ComponentTesting;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
37 public class TrackerSourceBranchInputFactoryTest {
38 private final static String COMPONENT_KEY = "file1";
39 private final static String COMPONENT_UUID = "uuid1";
42 public DbTester db = DbTester.create();
44 private final ComponentIssuesLoader componentIssuesLoader = mock(ComponentIssuesLoader.class);
45 private final SourceBranchComponentUuids sourceBranchComponentUuids = mock(SourceBranchComponentUuids.class);
46 private TrackerSourceBranchInputFactory underTest;
50 underTest = new TrackerSourceBranchInputFactory(componentIssuesLoader, sourceBranchComponentUuids, db.getDbClient());
54 public void gets_issues_and_hashes_in_matching_component() {
55 DefaultIssue issue1 = new DefaultIssue();
56 when(sourceBranchComponentUuids.getSourceBranchComponentUuid(COMPONENT_KEY)).thenReturn(COMPONENT_UUID);
57 when(componentIssuesLoader.loadOpenIssuesWithChanges(COMPONENT_UUID)).thenReturn(Collections.singletonList(issue1));
58 ComponentDto fileDto = ComponentTesting.newFileDto(ComponentTesting.newPublicProjectDto()).setUuid(COMPONENT_UUID);
59 db.fileSources().insertFileSource(fileDto, 3);
61 Component component = mock(Component.class);
62 when(component.getDbKey()).thenReturn(COMPONENT_KEY);
63 when(component.getType()).thenReturn(Component.Type.FILE);
64 Input<DefaultIssue> input = underTest.createForSourceBranch(component);
66 assertThat(input.getIssues()).containsOnly(issue1);
67 assertThat(input.getLineHashSequence().length()).isEqualTo(3);
71 public void get_issues_without_line_hashes() {
72 DefaultIssue issue1 = new DefaultIssue();
73 when(sourceBranchComponentUuids.getSourceBranchComponentUuid(COMPONENT_KEY)).thenReturn(COMPONENT_UUID);
74 when(componentIssuesLoader.loadOpenIssuesWithChanges(COMPONENT_UUID)).thenReturn(Collections.singletonList(issue1));
75 ComponentDto fileDto = ComponentTesting.newFileDto(ComponentTesting.newPublicProjectDto()).setUuid(COMPONENT_UUID);
76 db.fileSources().insertFileSource(fileDto, 0);
78 Component component = mock(Component.class);
79 when(component.getDbKey()).thenReturn(COMPONENT_KEY);
80 when(component.getType()).thenReturn(Component.Type.FILE);
81 Input<DefaultIssue> input = underTest.createForSourceBranch(component);
83 assertThat(input.getIssues()).containsOnly(issue1);
84 assertThat(input.getLineHashSequence().length()).isZero();
88 public void gets_nothing_when_there_is_no_matching_component() {
89 Component component = mock(Component.class);
90 when(component.getDbKey()).thenReturn(COMPONENT_KEY);
91 when(component.getType()).thenReturn(Component.Type.FILE);
92 Input<DefaultIssue> input = underTest.createForSourceBranch(component);
94 assertThat(input.getIssues()).isEmpty();
95 assertThat(input.getLineHashSequence().length()).isZero();
99 public void hasSourceBranchAnalysis_returns_true_if_source_branch_of_pr_was_analysed() {
100 when(sourceBranchComponentUuids.hasSourceBranchAnalysis()).thenReturn(true);
102 assertThat(underTest.hasSourceBranchAnalysis()).isTrue();
106 public void hasSourceBranchAnalysis_returns_false_if_no_source_branch_analysis() {
107 when(sourceBranchComponentUuids.hasSourceBranchAnalysis()).thenReturn(false);
109 assertThat(underTest.hasSourceBranchAnalysis()).isFalse();