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.Optional;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.utils.System2;
26 import org.sonar.ce.task.projectanalysis.analysis.MutableAnalysisMetadataHolderRule;
27 import org.sonar.ce.task.projectanalysis.component.Component;
28 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
29 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
30 import org.sonar.ce.task.step.TestComputationStepContext;
31 import org.sonar.db.DbTester;
32 import org.sonar.db.component.ComponentDto;
33 import org.sonar.db.component.ComponentTesting;
34 import org.sonar.db.component.SnapshotDto;
35 import org.sonar.db.component.SnapshotTesting;
37 import static org.assertj.core.api.Assertions.assertThat;
39 public class EnableAnalysisStepIT {
41 private static final ReportComponent REPORT_PROJECT = ReportComponent.builder(Component.Type.PROJECT, 1).build();
42 private static final String PREVIOUS_ANALYSIS_UUID = "ANALYSIS_1";
43 private static final String CURRENT_ANALYSIS_UUID = "ANALYSIS_2";
46 public DbTester db = DbTester.create(System2.INSTANCE);
49 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
52 public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
54 private EnableAnalysisStep underTest = new EnableAnalysisStep(db.getDbClient(), treeRootHolder, analysisMetadataHolder);
57 public void switch_islast_flag_and_mark_analysis_as_processed() {
58 ComponentDto project = ComponentTesting.newPrivateProjectDto(REPORT_PROJECT.getUuid());
59 db.components().insertComponent(project);
60 insertAnalysis(project, PREVIOUS_ANALYSIS_UUID, SnapshotDto.STATUS_PROCESSED, true);
61 insertAnalysis(project, CURRENT_ANALYSIS_UUID, SnapshotDto.STATUS_UNPROCESSED, false);
63 treeRootHolder.setRoot(REPORT_PROJECT);
64 analysisMetadataHolder.setUuid(CURRENT_ANALYSIS_UUID);
66 underTest.execute(new TestComputationStepContext());
68 verifyAnalysis(PREVIOUS_ANALYSIS_UUID, SnapshotDto.STATUS_PROCESSED, false);
69 verifyAnalysis(CURRENT_ANALYSIS_UUID, SnapshotDto.STATUS_PROCESSED, true);
73 public void set_islast_flag_and_mark_as_processed_if_no_previous_analysis() {
74 ComponentDto project = ComponentTesting.newPrivateProjectDto(REPORT_PROJECT.getUuid());
75 db.components().insertComponent(project);
76 insertAnalysis(project, CURRENT_ANALYSIS_UUID, SnapshotDto.STATUS_UNPROCESSED, false);
78 treeRootHolder.setRoot(REPORT_PROJECT);
79 analysisMetadataHolder.setUuid(CURRENT_ANALYSIS_UUID);
81 underTest.execute(new TestComputationStepContext());
83 verifyAnalysis(CURRENT_ANALYSIS_UUID, SnapshotDto.STATUS_PROCESSED, true);
86 private void verifyAnalysis(String uuid, String expectedStatus, boolean expectedLastFlag) {
87 Optional<SnapshotDto> analysis = db.getDbClient().snapshotDao().selectByUuid(db.getSession(), uuid);
88 assertThat(analysis.get().getStatus()).isEqualTo(expectedStatus);
89 assertThat(analysis.get().getLast()).isEqualTo(expectedLastFlag);
92 private void insertAnalysis(ComponentDto project, String uuid, String status, boolean isLastFlag) {
93 SnapshotDto snapshot = SnapshotTesting.newAnalysis(project)
97 db.getDbClient().snapshotDao().insert(db.getSession(), snapshot);