3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.step;
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;
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;
45 @RunWith(MockitoJUnitRunner.class)
46 public class PersistAnalysisWarningsStepTest {
49 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
52 private ArgumentCaptor<List<CeTaskMessages.Message>> argumentCaptor;
54 private final CeTaskMessages ceTaskMessages = mock(CeTaskMessages.class);
55 private final PersistAnalysisWarningsStep underTest = new PersistAnalysisWarningsStep(reportReader, ceTaskMessages);
58 public void getDescription() {
59 assertThat(underTest.getDescription()).isEqualTo(PersistAnalysisWarningsStep.DESCRIPTION);
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);
69 underTest.execute(new TestComputationStepContext());
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));
78 public void execute_does_not_persist_warnings_from_reportReader_when_empty() {
79 reportReader.setScannerLogs(emptyList());
81 underTest.execute(new TestComputationStepContext());
83 verifyNoInteractions(ceTaskMessages);