]> source.dussan.org Git - sonarqube.git/blob
ea5b6f3ba4b5ce63b29ade569fbe39e4273e9d27
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.issue;
21
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;
32
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
36
37 public class TrackerSourceBranchInputFactoryTest {
38   private final static String COMPONENT_KEY = "file1";
39   private final static String COMPONENT_UUID = "uuid1";
40
41   @Rule
42   public DbTester db = DbTester.create();
43
44   private final ComponentIssuesLoader componentIssuesLoader = mock(ComponentIssuesLoader.class);
45   private final SourceBranchComponentUuids sourceBranchComponentUuids = mock(SourceBranchComponentUuids.class);
46   private TrackerSourceBranchInputFactory underTest;
47
48   @Before
49   public void setUp() {
50     underTest = new TrackerSourceBranchInputFactory(componentIssuesLoader, sourceBranchComponentUuids, db.getDbClient());
51   }
52
53   @Test
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);
60
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);
65
66     assertThat(input.getIssues()).containsOnly(issue1);
67     assertThat(input.getLineHashSequence().length()).isEqualTo(3);
68   }
69
70   @Test
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);
77
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);
82
83     assertThat(input.getIssues()).containsOnly(issue1);
84     assertThat(input.getLineHashSequence().length()).isZero();
85   }
86
87   @Test
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);
93
94     assertThat(input.getIssues()).isEmpty();
95     assertThat(input.getLineHashSequence().length()).isZero();
96   }
97
98   @Test
99   public void hasSourceBranchAnalysis_returns_true_if_source_branch_of_pr_was_analysed() {
100     when(sourceBranchComponentUuids.hasSourceBranchAnalysis()).thenReturn(true);
101
102     assertThat(underTest.hasSourceBranchAnalysis()).isTrue();
103   }
104
105   @Test
106   public void hasSourceBranchAnalysis_returns_false_if_no_source_branch_analysis() {
107     when(sourceBranchComponentUuids.hasSourceBranchAnalysis()).thenReturn(false);
108
109     assertThat(underTest.hasSourceBranchAnalysis()).isFalse();
110   }
111 }