import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
import org.sonar.server.computation.task.step.ComputationStep;
+import static java.util.Collections.singleton;
+
public class PersistScannerContextStep implements ComputationStep {
private final BatchReportReader reportReader;
private final DbClient dbClient;
try (CloseableIterator<String> logsIterator = reportReader.readScannerLogs()) {
if (logsIterator.hasNext()) {
try (DbSession dbSession = dbClient.openSession(false)) {
+ // in case the task was restarted, the context might have been already persisted
+ // for total reliability, we rather delete the existing row as we don't want to assume the content
+ // consistent with the report
+ dbClient.ceScannerContextDao().deleteByUuids(dbSession, singleton(ceTask.getUuid()));
dbClient.ceScannerContextDao().insert(dbSession, ceTask.getUuid(), logsIterator);
dbSession.commit();
}
*/
package org.sonar.server.computation.task.projectanalysis.step;
+import java.util.Arrays;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.utils.System2;
import org.sonar.ce.queue.CeTask;
+import org.sonar.core.util.CloseableIterator;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
.isEmpty();
}
+ /**
+ * @see SONAR-8306
+ */
+ @Test
+ public void execute_does_not_fail_if_scanner_context_has_already_been_persisted() {
+ dbClient.ceScannerContextDao().insert(dbTester.getSession(), ANALYSIS_UUID, CloseableIterator.from(Arrays.asList("a", "b", "c").iterator()));
+ dbTester.commit();
+ reportReader.setScannerLogs(asList("1", "2", "3"));
+ when(ceTask.getUuid()).thenReturn(ANALYSIS_UUID);
+
+ underTest.execute();
+
+ assertThat(dbClient.ceScannerContextDao().selectScannerContext(dbTester.getSession(), ANALYSIS_UUID))
+ .contains("1" + '\n' + "2" + '\n' + "3");
+ }
}