]> source.dussan.org Git - sonarqube.git/blob
0a72338702db0e8fd1d46364375362be45a826a8
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.language;
21
22 import com.google.common.collect.ImmutableList;
23 import java.util.List;
24 import java.util.Optional;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.ArgumentCaptor;
29 import org.mockito.Captor;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.sonar.api.utils.System2;
32 import org.sonar.ce.task.log.CeTaskMessages;
33 import org.sonar.ce.task.log.CeTaskMessages.Message;
34 import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
35 import org.sonar.ce.task.projectanalysis.component.Component;
36 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
37 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
38 import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
39 import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
40 import org.sonar.ce.task.step.TestComputationStepContext;
41 import org.sonar.core.platform.EditionProvider;
42 import org.sonar.core.platform.PlatformEditionProvider;
43 import org.sonar.db.ce.CeTaskMessageType;
44 import org.sonar.scanner.protocol.output.ScannerReport;
45
46 import static com.google.common.collect.ImmutableList.of;
47 import static org.assertj.core.api.Assertions.assertThat;
48 import static org.assertj.core.api.Assertions.tuple;
49 import static org.mockito.ArgumentMatchers.any;
50 import static org.mockito.Mockito.mock;
51 import static org.mockito.Mockito.never;
52 import static org.mockito.Mockito.times;
53 import static org.mockito.Mockito.verify;
54 import static org.mockito.Mockito.when;
55 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
56 import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_C;
57 import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_CPP;
58 import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_CPP_KEY;
59 import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_C_KEY;
60
61 @RunWith(MockitoJUnitRunner.class)
62 public class HandleUnanalyzedLanguagesStepTest {
63
64   private static final int PROJECT_REF = 1;
65   private static final Component ROOT_PROJECT = ReportComponent.builder(PROJECT, PROJECT_REF).build();
66
67   @Rule
68   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
69   @Rule
70   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(ROOT_PROJECT);
71   @Rule
72   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
73     .add(UNANALYZED_C)
74     .add(UNANALYZED_CPP);
75   @Rule
76   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
77
78   @Captor
79   private ArgumentCaptor<Message> argumentCaptor;
80
81   private final PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
82   private final CeTaskMessages ceTaskMessages = mock(CeTaskMessages.class);
83
84   private final HandleUnanalyzedLanguagesStep underTest = new HandleUnanalyzedLanguagesStep(reportReader, ceTaskMessages, editionProvider, System2.INSTANCE, treeRootHolder,
85     metricRepository, measureRepository);
86
87   @Test
88   public void getDescription() {
89     assertThat(underTest.getDescription()).isEqualTo(HandleUnanalyzedLanguagesStep.DESCRIPTION);
90   }
91
92   @Test
93   public void add_warning_and_measures_in_SQ_community_edition_if_there_are_c_or_cpp_files() {
94     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY));
95     ScannerReport.AnalysisWarning warning1 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 1").build();
96     ScannerReport.AnalysisWarning warning2 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 2").build();
97     ImmutableList<ScannerReport.AnalysisWarning> warnings = of(warning1, warning2);
98     reportReader.setAnalysisWarnings(warnings);
99     reportReader.setMetadata(ScannerReport.Metadata.newBuilder()
100       .putNotAnalyzedFilesByLanguage("C++", 20)
101       .putNotAnalyzedFilesByLanguage("C", 10)
102       .putNotAnalyzedFilesByLanguage("SomeLang", 1000)
103       .build());
104
105     underTest.execute(new TestComputationStepContext());
106
107     verify(ceTaskMessages, times(1)).add(argumentCaptor.capture());
108     assertThat(argumentCaptor.getAllValues())
109       .extracting(Message::getText, Message::getType)
110       .containsExactly(tuple(
111         "10 unanalyzed C, 20 unanalyzed C++ and 1000 unanalyzed SomeLang files were detected in this project during the last analysis. C," +
112           " C++ and SomeLang cannot be analyzed with your current SonarQube edition. Please consider" +
113           " <a target=\"_blank\" href=\"https://www.sonarqube.org/trial-request/developer-edition/?referrer=sonarqube-cpp\">upgrading to Developer Edition</a> to find Bugs," +
114           " Code Smells, Vulnerabilities and Security Hotspots in these files.",
115         CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE));
116     assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_C_KEY).get().getIntValue()).isEqualTo(10);
117     assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_CPP_KEY).get().getIntValue()).isEqualTo(20);
118   }
119
120   @Test
121   public void adds_warning_and_measures_in_SQ_community_edition_if_there_are_c_files() {
122     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY));
123     reportReader.setMetadata(ScannerReport.Metadata.newBuilder()
124       .putNotAnalyzedFilesByLanguage("C", 10)
125       .build());
126
127     underTest.execute(new TestComputationStepContext());
128
129     verify(ceTaskMessages, times(1)).add(argumentCaptor.capture());
130     List<CeTaskMessages.Message> messages = argumentCaptor.getAllValues();
131     assertThat(messages).extracting(CeTaskMessages.Message::getText).containsExactly(
132       "10 unanalyzed C files were detected in this project during the last analysis. C cannot be analyzed with your current SonarQube edition. Please" +
133         " consider <a target=\"_blank\" href=\"https://www.sonarqube.org/trial-request/developer-edition/?referrer=sonarqube-cpp\">upgrading to Developer" +
134         " Edition</a> to find Bugs, Code Smells, Vulnerabilities and Security Hotspots in this file.");
135     assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_C_KEY).get().getIntValue()).isEqualTo(10);
136     assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_CPP_KEY)).isEmpty();
137   }
138
139   @Test
140   public void adds_warning_in_SQ_community_edition_if_there_are_cpp_files() {
141     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY));
142     reportReader.setMetadata(ScannerReport.Metadata.newBuilder()
143       .putNotAnalyzedFilesByLanguage("C++", 1)
144       .build());
145
146     underTest.execute(new TestComputationStepContext());
147
148     verify(ceTaskMessages, times(1)).add(argumentCaptor.capture());
149     List<CeTaskMessages.Message> messages = argumentCaptor.getAllValues();
150     assertThat(messages).extracting(CeTaskMessages.Message::getText).containsExactly(
151       "1 unanalyzed C++ file was detected in this project during the last analysis. C++ cannot be analyzed with your current SonarQube edition. Please" +
152         " consider <a target=\"_blank\" href=\"https://www.sonarqube.org/trial-request/developer-edition/?referrer=sonarqube-cpp\">upgrading to Developer" +
153         " Edition</a> to find Bugs, Code Smells, Vulnerabilities and Security Hotspots in this file.");
154     assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_CPP_KEY).get().getIntValue()).isOne();
155     assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_C_KEY)).isEmpty();
156   }
157
158   @Test
159   public void do_nothing_SQ_community_edition_if_cpp_files_in_report_is_zero() {
160     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY));
161     ScannerReport.AnalysisWarning warning1 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 1").build();
162     ScannerReport.AnalysisWarning warning2 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 2").build();
163     ImmutableList<ScannerReport.AnalysisWarning> warnings = of(warning1, warning2);
164     reportReader.setAnalysisWarnings(warnings);
165     reportReader.setMetadata(ScannerReport.Metadata.newBuilder().putNotAnalyzedFilesByLanguage("C++", 0).build());
166
167     underTest.execute(new TestComputationStepContext());
168
169     verify(ceTaskMessages, never()).add(any());
170
171     assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_C_KEY)).isEmpty();
172     assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_CPP_KEY)).isEmpty();
173   }
174
175   @Test
176   public void execute_does_not_add_a_warning_in_SQ_community_edition_if_no_c_or_cpp_files() {
177     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY));
178     ScannerReport.AnalysisWarning warning1 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 1").build();
179     ScannerReport.AnalysisWarning warning2 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 2").build();
180     ImmutableList<ScannerReport.AnalysisWarning> warnings = of(warning1, warning2);
181     reportReader.setAnalysisWarnings(warnings);
182     reportReader.setMetadata(ScannerReport.Metadata.newBuilder().build());
183
184     underTest.execute(new TestComputationStepContext());
185
186     verify(ceTaskMessages, never()).add(any());
187     assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_C_KEY)).isEmpty();
188     assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_CPP_KEY)).isEmpty();
189   }
190
191   @Test
192   public void execute_does_not_add_a_warning_in_SQ_non_community_edition() {
193     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.ENTERPRISE));
194     ScannerReport.AnalysisWarning warning1 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 1").build();
195     ScannerReport.AnalysisWarning warning2 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 2").build();
196     ImmutableList<ScannerReport.AnalysisWarning> warnings = of(warning1, warning2);
197     reportReader.setAnalysisWarnings(warnings);
198     reportReader.setMetadata(ScannerReport.Metadata.newBuilder().putNotAnalyzedFilesByLanguage("C++", 20).build());
199
200     underTest.execute(new TestComputationStepContext());
201
202     verify(ceTaskMessages, never()).add(any());
203     assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_C_KEY)).isEmpty();
204     assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_CPP_KEY)).isEmpty();
205   }
206 }