]> source.dussan.org Git - sonarqube.git/blob
ff18ae503386f6e99b8144ec751feadc864fa792
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 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.step;
21
22 import java.util.Collection;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.utils.System2;
26 import org.sonar.api.utils.log.LogTester;
27 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
28 import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
29 import org.sonar.ce.task.step.ComputationStep;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.component.BranchDto;
32 import org.sonar.db.component.ComponentDto;
33 import org.sonar.scanner.protocol.output.ScannerReport;
34 import org.sonar.server.project.Project;
35
36 import static java.util.Collections.emptyList;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.assertj.core.api.Assertions.assertThatThrownBy;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41 import static org.sonar.api.utils.log.LoggerLevel.TRACE;
42
43 public class UpdateMainBranchStepTest {
44
45   @Rule
46   public DbTester dbTester = DbTester.create(System2.INSTANCE);
47
48   @Rule
49   public LogTester logTester = new LogTester().setLevel(TRACE);
50
51   private final AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
52   BatchReportReader batchReportReader = mock(BatchReportReader.class);
53
54   private final UpdateMainBranchStep underTest = new UpdateMainBranchStep(batchReportReader, dbTester.getDbClient(), analysisMetadataHolder);
55   private ComputationStep.Context context = mock(ComputationStep.Context.class);
56
57   @Test
58   public void update_main_branch_on_first_analysis() {
59     ScannerReport.Metadata metadata = ScannerReport.Metadata.newBuilder().setGitDefaultMainBranch("new_name").buildPartial();
60     when(batchReportReader.readMetadata()).thenReturn(metadata);
61     when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(true);
62     ComponentDto privateProject = dbTester.components().insertPrivateProject();
63     when(analysisMetadataHolder.getProject())
64       .thenReturn(new Project(privateProject.uuid(), privateProject.getKey(), privateProject.name(), privateProject.description(), emptyList()));
65
66     assertMainBranchName(privateProject, "master");
67
68     underTest.execute(context);
69
70     assertMainBranchName(privateProject, "new_name");
71     assertThat(logTester.logs()).contains("GIT default main branch detected is [new_name]");
72     assertThat(logTester.logs()).contains(String.format("updating project [%s] default main branch to [new_name]", privateProject.getKey()));
73   }
74
75   @Test
76   public void do_not_update_main_branch_on_second_analysis() {
77     ScannerReport.Metadata metadata = ScannerReport.Metadata.newBuilder().setGitDefaultMainBranch("new_name").buildPartial();
78     when(batchReportReader.readMetadata()).thenReturn(metadata);
79     when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(false);
80     ComponentDto privateProject = dbTester.components().insertPrivateProject();
81     when(analysisMetadataHolder.getProject())
82       .thenReturn(new Project(privateProject.uuid(), privateProject.getKey(), privateProject.name(), privateProject.description(), emptyList()));
83
84     assertMainBranchName(privateProject, "master");
85
86     underTest.execute(context);
87
88     assertMainBranchName(privateProject, "master");
89     assertThat(logTester.logs()).isEmpty();
90   }
91
92   @Test
93   public void do_not_update_main_branch_if_no_git_info_found() {
94     String emptyGitMainBranchInfo = "";
95     ScannerReport.Metadata metadata = ScannerReport.Metadata.newBuilder().setGitDefaultMainBranch(emptyGitMainBranchInfo).buildPartial();
96     when(batchReportReader.readMetadata()).thenReturn(metadata);
97     when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(true);
98     ComponentDto privateProject = dbTester.components().insertPrivateProject();
99     when(analysisMetadataHolder.getProject())
100       .thenReturn(new Project(privateProject.uuid(), privateProject.getKey(), privateProject.name(), privateProject.description(), emptyList()));
101
102     assertMainBranchName(privateProject, "master");
103
104     underTest.execute(context);
105
106     assertMainBranchName(privateProject, "master");
107     assertThat(logTester.logs()).contains("GIT default main branch detected is empty");
108   }
109
110   @Test
111   public void fail_on_invalid_project_key() {
112     ScannerReport.Metadata metadata = ScannerReport.Metadata.newBuilder().setGitDefaultMainBranch("new_name").buildPartial();
113     when(batchReportReader.readMetadata()).thenReturn(metadata);
114     when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(true);
115     ComponentDto privateProject = dbTester.components().insertPrivateProject();
116     when(analysisMetadataHolder.getProject())
117       .thenReturn(new Project(privateProject.uuid(), "invalid project key", privateProject.name(), privateProject.description(), emptyList()));
118
119     assertThatThrownBy(() -> underTest.execute(context))
120       .isInstanceOf(IllegalStateException.class)
121       .hasMessage("root component key [invalid project key] is not a project");
122     assertThat(logTester.logs()).contains("GIT default main branch detected is [new_name]");
123   }
124
125   @Test
126   public void getDescription() {
127     assertThat(underTest.getDescription()).isNotEmpty();
128   }
129
130   private void assertMainBranchName(ComponentDto privateProject, String expectedBranchName) {
131     Collection<BranchDto> branches = dbTester.getDbClient().branchDao().selectByComponent(dbTester.getSession(), privateProject);
132     assertThat(branches).isNotEmpty();
133     assertThat(branches).hasSize(1);
134     assertThat(branches.iterator().next().getKey()).isEqualTo(expectedBranchName);
135   }
136 }