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 java.util.Arrays;
23 import org.junit.ClassRule;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.utils.System2;
27 import org.sonar.ce.task.CeTask;
28 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
29 import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
30 import org.sonar.ce.task.step.TestComputationStepContext;
31 import org.sonar.core.util.CloseableIterator;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbTester;
35 import static java.util.Arrays.asList;
36 import static java.util.Collections.emptyList;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
41 public class PersistScannerContextStepIT {
42 private static final String ANALYSIS_UUID = "UUID";
45 public static final DbTester dbTester = DbTester.create(System2.INSTANCE);
48 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
50 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
51 .setUuid(ANALYSIS_UUID);
53 private DbClient dbClient = dbTester.getDbClient();
54 private CeTask ceTask = mock(CeTask.class);
55 private PersistScannerContextStep underTest = new PersistScannerContextStep(reportReader, dbClient, ceTask);
58 public void getDescription() {
59 assertThat(underTest.getDescription()).isEqualTo("Persist scanner context");
63 public void executes_persist_lines_of_reportReader() {
64 String taskUuid = "task uuid";
65 when(ceTask.getUuid()).thenReturn(taskUuid);
66 reportReader.setScannerLogs(asList("log1", "log2"));
68 underTest.execute(new TestComputationStepContext());
70 assertThat(dbClient.ceScannerContextDao().selectScannerContext(dbTester.getSession(), taskUuid))
71 .contains("log1" + '\n' + "log2");
75 public void executes_persist_does_not_persist_any_scanner_context_if_iterator_is_empty() {
76 reportReader.setScannerLogs(emptyList());
78 underTest.execute(new TestComputationStepContext());
80 assertThat(dbClient.ceScannerContextDao().selectScannerContext(dbTester.getSession(), ANALYSIS_UUID))
88 public void execute_does_not_fail_if_scanner_context_has_already_been_persisted() {
89 dbClient.ceScannerContextDao().insert(dbTester.getSession(), ANALYSIS_UUID, CloseableIterator.from(Arrays.asList("a", "b", "c").iterator()));
91 reportReader.setScannerLogs(asList("1", "2", "3"));
92 when(ceTask.getUuid()).thenReturn(ANALYSIS_UUID);
94 underTest.execute(new TestComputationStepContext());
96 assertThat(dbClient.ceScannerContextDao().selectScannerContext(dbTester.getSession(), ANALYSIS_UUID))
97 .contains("1" + '\n' + "2" + '\n' + "3");