]> source.dussan.org Git - sonarqube.git/blob
e96c977b34dcf0b62d7facca4fc66c4ecfb3dbe1
[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.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;
36
37 import static org.assertj.core.api.Assertions.assertThat;
38
39 public class EnableAnalysisStepIT {
40
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";
44
45   @Rule
46   public DbTester db = DbTester.create(System2.INSTANCE);
47
48   @Rule
49   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
50
51   @Rule
52   public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
53
54   private EnableAnalysisStep underTest = new EnableAnalysisStep(db.getDbClient(), treeRootHolder, analysisMetadataHolder);
55
56   @Test
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);
62     db.commit();
63     treeRootHolder.setRoot(REPORT_PROJECT);
64     analysisMetadataHolder.setUuid(CURRENT_ANALYSIS_UUID);
65
66     underTest.execute(new TestComputationStepContext());
67
68     verifyAnalysis(PREVIOUS_ANALYSIS_UUID, SnapshotDto.STATUS_PROCESSED, false);
69     verifyAnalysis(CURRENT_ANALYSIS_UUID, SnapshotDto.STATUS_PROCESSED, true);
70   }
71
72   @Test
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);
77     db.commit();
78     treeRootHolder.setRoot(REPORT_PROJECT);
79     analysisMetadataHolder.setUuid(CURRENT_ANALYSIS_UUID);
80
81     underTest.execute(new TestComputationStepContext());
82
83     verifyAnalysis(CURRENT_ANALYSIS_UUID, SnapshotDto.STATUS_PROCESSED, true);
84   }
85
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);
90   }
91
92   private void insertAnalysis(ComponentDto project, String uuid, String status, boolean isLastFlag) {
93     SnapshotDto snapshot = SnapshotTesting.newAnalysis(project)
94       .setLast(isLastFlag)
95       .setStatus(status)
96       .setUuid(uuid);
97     db.getDbClient().snapshotDao().insert(db.getSession(), snapshot);
98   }
99 }