]> source.dussan.org Git - sonarqube.git/blob
d85d967f1036d56c98dd97b6a27a7219a886539c
[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 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;
34
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;
40
41 public class PersistScannerContextStepIT {
42   private static final String ANALYSIS_UUID = "UUID";
43
44   @ClassRule
45   public static final DbTester dbTester = DbTester.create(System2.INSTANCE);
46
47   @Rule
48   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
49   @Rule
50   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
51     .setUuid(ANALYSIS_UUID);
52
53   private DbClient dbClient = dbTester.getDbClient();
54   private CeTask ceTask = mock(CeTask.class);
55   private PersistScannerContextStep underTest = new PersistScannerContextStep(reportReader, dbClient, ceTask);
56
57   @Test
58   public void getDescription() {
59     assertThat(underTest.getDescription()).isEqualTo("Persist scanner context");
60   }
61
62   @Test
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"));
67
68     underTest.execute(new TestComputationStepContext());
69
70     assertThat(dbClient.ceScannerContextDao().selectScannerContext(dbTester.getSession(), taskUuid))
71       .contains("log1" + '\n' + "log2");
72   }
73
74   @Test
75   public void executes_persist_does_not_persist_any_scanner_context_if_iterator_is_empty() {
76     reportReader.setScannerLogs(emptyList());
77
78     underTest.execute(new TestComputationStepContext());
79
80     assertThat(dbClient.ceScannerContextDao().selectScannerContext(dbTester.getSession(), ANALYSIS_UUID))
81       .isEmpty();
82   }
83
84   /**
85    * SONAR-8306
86    */
87   @Test
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()));
90     dbTester.commit();
91     reportReader.setScannerLogs(asList("1", "2", "3"));
92     when(ceTask.getUuid()).thenReturn(ANALYSIS_UUID);
93
94     underTest.execute(new TestComputationStepContext());
95
96     assertThat(dbClient.ceScannerContextDao().selectScannerContext(dbTester.getSession(), ANALYSIS_UUID))
97       .contains("1" + '\n' + "2" + '\n' + "3");
98   }
99 }