]> source.dussan.org Git - sonarqube.git/blob
ea992792b549ade2c53ff15072b17b2bbeeb9f1c
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.component;
21
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
26 import org.sonar.ce.task.projectanalysis.analysis.Branch;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.component.BranchType;
29 import org.sonar.db.component.ComponentDto;
30 import org.sonar.db.component.ComponentTesting;
31 import org.sonar.server.project.Project;
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 import static org.sonar.db.component.SnapshotTesting.newAnalysis;
37
38 public class MergeAndTargetBranchComponentUuidsTest {
39   @Rule
40   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
41
42   @Rule
43   public DbTester db = DbTester.create();
44
45   private MergeAndTargetBranchComponentUuids underTest;
46   private Branch branch = mock(Branch.class);
47
48   private ComponentDto branch1;
49   private ComponentDto branch1File;
50   private ComponentDto pr1File;
51   private ComponentDto pr2File;
52   private Project project;
53   private ComponentDto pr1;
54   private ComponentDto pr2;
55   private ComponentDto branch2;
56   private ComponentDto branch2File;
57
58   @Before
59   public void setUp() {
60     underTest = new MergeAndTargetBranchComponentUuids(analysisMetadataHolder, db.getDbClient());
61     project = mock(Project.class);
62     analysisMetadataHolder.setProject(project);
63     analysisMetadataHolder.setBranch(branch);
64
65     ComponentDto projectDto = db.components().insertMainBranch();
66     when(project.getUuid()).thenReturn(projectDto.uuid());
67     branch1 = db.components().insertProjectBranch(projectDto, b -> b.setKey("branch1"));
68     branch2 = db.components().insertProjectBranch(projectDto, b -> b.setKey("branch2"));
69     pr1 = db.components().insertProjectBranch(projectDto, b -> b.setKey("pr1").setBranchType(BranchType.PULL_REQUEST).setMergeBranchUuid(branch1.uuid()));
70     pr2 = db.components().insertProjectBranch(projectDto, b -> b.setKey("pr2").setBranchType(BranchType.PULL_REQUEST).setMergeBranchUuid(branch1.uuid()));
71     branch1File = ComponentTesting.newFileDto(branch1, null, "file").setUuid("branch1File");
72     branch2File = ComponentTesting.newFileDto(branch2, null, "file").setUuid("branch2File");
73     pr1File = ComponentTesting.newFileDto(pr1, null, "file").setUuid("file1");
74     pr2File = ComponentTesting.newFileDto(pr2, null, "file").setUuid("file2");
75     db.components().insertComponents(branch1File, pr1File, pr2File, branch2File);
76   }
77
78   @Test
79   public void should_support_db_key_when_looking_for_merge_component() {
80     when(branch.getMergeBranchUuid()).thenReturn(branch1.uuid());
81     when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
82     when(branch.getTargetBranchName()).thenReturn("notAnalyzedBranch");
83     db.components().insertSnapshot(newAnalysis(branch1));
84     assertThat(underTest.getMergeBranchComponentUuid(pr1File.getDbKey())).isEqualTo(branch1File.uuid());
85     assertThat(underTest.getTargetBranchComponentUuid(pr1File.getDbKey())).isNull();
86     assertThat(underTest.hasMergeBranchAnalysis()).isTrue();
87     assertThat(underTest.hasTargetBranchAnalysis()).isFalse();
88     assertThat(underTest.areTargetAndMergeBranchesDifferent()).isTrue();
89     assertThat(underTest.getMergeBranchName()).isEqualTo("branch1");
90   }
91
92   @Test
93   public void should_support_db_key_when_looking_for_target_component() {
94     when(branch.getMergeBranchUuid()).thenReturn(branch1.uuid());
95     when(branch.getTargetBranchName()).thenReturn("branch2");
96     when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
97     db.components().insertSnapshot(newAnalysis(branch1));
98     db.components().insertSnapshot(newAnalysis(branch2));
99     assertThat(underTest.getMergeBranchComponentUuid(pr1File.getDbKey())).isEqualTo(branch1File.uuid());
100     assertThat(underTest.getTargetBranchComponentUuid(pr1File.getDbKey())).isEqualTo(branch2File.uuid());
101     assertThat(underTest.hasMergeBranchAnalysis()).isTrue();
102     assertThat(underTest.hasTargetBranchAnalysis()).isTrue();
103     assertThat(underTest.areTargetAndMergeBranchesDifferent()).isTrue();
104   }
105
106   @Test
107   public void should_support_key_when_looking_for_merge_component() {
108     when(branch.getMergeBranchUuid()).thenReturn(branch1.uuid());
109     when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
110     when(branch.getTargetBranchName()).thenReturn("notAnalyzedBranch");
111     db.components().insertSnapshot(newAnalysis(branch1));
112     assertThat(underTest.getMergeBranchComponentUuid(pr1File.getKey())).isEqualTo(branch1File.uuid());
113   }
114
115   @Test
116   public void return_null_if_file_doesnt_exist() {
117     when(branch.getMergeBranchUuid()).thenReturn(branch1.uuid());
118     when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
119     when(branch.getTargetBranchName()).thenReturn("notAnalyzedBranch");
120     db.components().insertSnapshot(newAnalysis(branch1));
121     assertThat(underTest.getMergeBranchComponentUuid("doesnt exist")).isNull();
122   }
123
124   @Test
125   public void skip_init_if_no_merge_branch_analysis() {
126     when(branch.getMergeBranchUuid()).thenReturn(branch1.uuid());
127     when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
128     when(branch.getTargetBranchName()).thenReturn("notAnalyzedBranch");
129     assertThat(underTest.getMergeBranchComponentUuid(pr1File.getDbKey())).isNull();
130   }
131
132   @Test
133   public void should_skip_target_components_init_on_branches() {
134     when(branch.getMergeBranchUuid()).thenReturn(branch1.uuid());
135     when(branch.getType()).thenReturn(BranchType.BRANCH);
136     when(branch.getTargetBranchName()).thenThrow(new IllegalStateException("Unsupported on branches"));
137     db.components().insertSnapshot(newAnalysis(branch1));
138
139     assertThat(underTest.getMergeBranchComponentUuid(branch2File.getDbKey())).isEqualTo(branch1File.uuid());
140     assertThat(underTest.getTargetBranchComponentUuid(branch2File.getDbKey())).isNull();
141     assertThat(underTest.hasMergeBranchAnalysis()).isTrue();
142     assertThat(underTest.hasTargetBranchAnalysis()).isFalse();
143     assertThat(underTest.areTargetAndMergeBranchesDifferent()).isTrue();
144     assertThat(underTest.getMergeBranchName()).isEqualTo("branch1");
145   }
146 }