]> source.dussan.org Git - sonarqube.git/blob
15088a76fc5d4331038d5f26ff99c6576de2a876
[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.step;
21
22 import com.google.common.collect.ImmutableList;
23 import java.util.List;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Captor;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.sonar.ce.task.log.CeTaskMessages;
31 import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
32 import org.sonar.ce.task.step.TestComputationStepContext;
33 import org.sonar.db.ce.CeTaskMessageType;
34 import org.sonar.scanner.protocol.output.ScannerReport;
35
36 import static com.google.common.collect.ImmutableList.of;
37 import static java.util.Collections.emptyList;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.assertj.core.api.Assertions.tuple;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.times;
42 import static org.mockito.Mockito.verify;
43 import static org.mockito.Mockito.verifyNoInteractions;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class PersistAnalysisWarningsStepTest {
47
48   @Rule
49   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
50
51   @Captor
52   private ArgumentCaptor<List<CeTaskMessages.Message>> argumentCaptor;
53
54   private final CeTaskMessages ceTaskMessages = mock(CeTaskMessages.class);
55   private final PersistAnalysisWarningsStep underTest = new PersistAnalysisWarningsStep(reportReader, ceTaskMessages);
56
57   @Test
58   public void getDescription() {
59     assertThat(underTest.getDescription()).isEqualTo(PersistAnalysisWarningsStep.DESCRIPTION);
60   }
61
62   @Test
63   public void execute_persists_warnings_from_reportReader() {
64     ScannerReport.AnalysisWarning warning1 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 1").build();
65     ScannerReport.AnalysisWarning warning2 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 2").build();
66     ImmutableList<ScannerReport.AnalysisWarning> warnings = of(warning1, warning2);
67     reportReader.setAnalysisWarnings(warnings);
68
69     underTest.execute(new TestComputationStepContext());
70
71     verify(ceTaskMessages, times(1)).addAll(argumentCaptor.capture());
72     assertThat(argumentCaptor.getValue())
73       .extracting(CeTaskMessages.Message::getText, CeTaskMessages.Message::getType)
74       .containsExactly(tuple("warning 1", CeTaskMessageType.GENERIC), tuple("warning 2", CeTaskMessageType.GENERIC));
75   }
76
77   @Test
78   public void execute_does_not_persist_warnings_from_reportReader_when_empty() {
79     reportReader.setScannerLogs(emptyList());
80
81     underTest.execute(new TestComputationStepContext());
82
83     verifyNoInteractions(ceTaskMessages);
84   }
85 }