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.List;
23 import java.util.Optional;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.api.utils.DateUtils;
28 import org.sonar.api.utils.System2;
29 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
30 import org.sonar.ce.task.projectanalysis.component.Component;
31 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
32 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
33 import org.sonar.ce.task.projectanalysis.period.Period;
34 import org.sonar.ce.task.projectanalysis.period.PeriodHolderRule;
35 import org.sonar.ce.task.step.ComputationStep;
36 import org.sonar.ce.task.step.TestComputationStepContext;
37 import org.sonar.db.DbClient;
38 import org.sonar.db.DbTester;
39 import org.sonar.db.component.ComponentDto;
40 import org.sonar.db.component.ComponentTesting;
41 import org.sonar.db.component.SnapshotDto;
42 import org.sonar.db.component.SnapshotQuery;
43 import org.sonar.db.component.SnapshotTesting;
45 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
46 import static org.assertj.core.api.Assertions.assertThat;
47 import static org.mockito.Mockito.mock;
48 import static org.mockito.Mockito.when;
50 public class ReportPersistAnalysisStepIT extends BaseStepTest {
52 private static final String PROJECT_KEY = "PROJECT_KEY";
53 private static final String ANALYSIS_UUID = "U1";
54 private static final String REVISION_ID = "5f6432a1";
57 public DbTester dbTester = DbTester.create(System2.INSTANCE);
59 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
61 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
63 public PeriodHolderRule periodsHolder = new PeriodHolderRule();
65 private System2 system2 = mock(System2.class);
66 private DbClient dbClient = dbTester.getDbClient();
67 private long analysisDate;
69 private PersistAnalysisStep underTest;
73 analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
74 analysisMetadataHolder.setUuid(ANALYSIS_UUID);
75 analysisMetadataHolder.setAnalysisDate(analysisDate);
76 analysisMetadataHolder.setScmRevision(REVISION_ID);
78 now = DateUtils.parseDateQuietly("2015-06-02").getTime();
80 when(system2.now()).thenReturn(now);
82 underTest = new PersistAnalysisStep(system2, dbClient, treeRootHolder, analysisMetadataHolder, periodsHolder);
84 // initialize PeriodHolder to empty by default
85 periodsHolder.setPeriod(null);
89 protected ComputationStep step() {
94 public void persist_analysis() {
95 String projectVersion = randomAlphabetic(10);
96 ComponentDto projectDto = ComponentTesting.newPrivateProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
97 dbTester.components().insertComponent(projectDto);
98 ComponentDto directoryDto = ComponentTesting.newDirectory(projectDto, "CDEF", "src/main/java/dir").setKey("PROJECT_KEY:src/main/java/dir");
99 dbTester.components().insertComponent(directoryDto);
100 ComponentDto fileDto = ComponentTesting.newFileDto(projectDto, directoryDto, "DEFG").setKey("PROJECT_KEY:src/main/java/dir/Foo.java");
101 dbTester.components().insertComponent(fileDto);
102 dbTester.getSession().commit();
104 Component file = ReportComponent.builder(Component.Type.FILE, 3).setUuid("DEFG").setKey("PROJECT_KEY:src/main/java/dir/Foo.java").build();
105 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid("CDEF").setKey("PROJECT_KEY:src/main/java/dir").addChildren(file).build();
106 String buildString = Optional.ofNullable(projectVersion).map(v -> randomAlphabetic(7)).orElse(null);
107 Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
110 .setProjectVersion(projectVersion)
111 .setBuildString(buildString)
112 .setScmRevisionId(REVISION_ID)
113 .addChildren(directory)
115 treeRootHolder.setRoot(project);
117 underTest.execute(new TestComputationStepContext());
119 assertThat(dbTester.countRowsOfTable("snapshots")).isOne();
121 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
122 assertThat(projectSnapshot.getUuid()).isEqualTo(ANALYSIS_UUID);
123 assertThat(projectSnapshot.getRootComponentUuid()).isEqualTo(project.getUuid());
124 assertThat(projectSnapshot.getProjectVersion()).isEqualTo(projectVersion);
125 assertThat(projectSnapshot.getBuildString()).isEqualTo(buildString);
126 assertThat(projectSnapshot.getLast()).isFalse();
127 assertThat(projectSnapshot.getStatus()).isEqualTo("U");
128 assertThat(projectSnapshot.getCreatedAt()).isEqualTo(analysisDate);
129 assertThat(projectSnapshot.getBuildDate()).isEqualTo(now);
130 assertThat(projectSnapshot.getRevision()).isEqualTo(REVISION_ID);
134 public void persist_snapshots_with_new_code_period() {
135 ComponentDto projectDto = ComponentTesting.newPrivateProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
136 dbTester.components().insertComponent(projectDto);
137 SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(projectDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime());
138 dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
139 dbTester.getSession().commit();
140 periodsHolder.setPeriod(new Period("NUMBER_OF_DAYS", "10", analysisDate));
142 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
143 treeRootHolder.setRoot(project);
145 underTest.execute(new TestComputationStepContext());
147 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
148 assertThat(projectSnapshot.getPeriodMode()).isEqualTo("NUMBER_OF_DAYS");
149 assertThat(projectSnapshot.getPeriodDate()).isEqualTo(analysisDate);
150 assertThat(projectSnapshot.getPeriodModeParameter()).isNotNull();
154 public void only_persist_snapshots_with_new_code_period_on_project_and_module() {
155 periodsHolder.setPeriod(new Period("PREVIOUS_VERSION", null, analysisDate));
157 ComponentDto projectDto = ComponentTesting.newPrivateProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
158 dbTester.components().insertComponent(projectDto);
159 SnapshotDto projectSnapshot = SnapshotTesting.newAnalysis(projectDto);
160 dbClient.snapshotDao().insert(dbTester.getSession(), projectSnapshot);
162 ComponentDto directoryDto = ComponentTesting.newDirectory(projectDto, "CDEF", "MODULE_KEY:src/main/java/dir").setKey("MODULE_KEY:src/main/java/dir");
163 dbTester.components().insertComponent(directoryDto);
165 ComponentDto fileDto = ComponentTesting.newFileDto(projectDto, directoryDto, "DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java");
166 dbTester.components().insertComponent(fileDto);
168 dbTester.getSession().commit();
170 Component file = ReportComponent.builder(Component.Type.FILE, 3).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
171 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
172 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(directory).build();
173 treeRootHolder.setRoot(project);
175 underTest.execute(new TestComputationStepContext());
177 SnapshotDto newProjectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
178 assertThat(newProjectSnapshot.getPeriodMode()).isEqualTo("PREVIOUS_VERSION");
182 public void set_no_period_on_snapshots_when_no_period() {
183 ComponentDto projectDto = ComponentTesting.newPrivateProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
184 dbTester.components().insertComponent(projectDto);
185 SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(projectDto);
186 dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
187 dbTester.getSession().commit();
189 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
190 treeRootHolder.setRoot(project);
192 underTest.execute(new TestComputationStepContext());
194 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
195 assertThat(projectSnapshot.getPeriodMode()).isNull();
196 assertThat(projectSnapshot.getPeriodDate()).isNull();
197 assertThat(projectSnapshot.getPeriodModeParameter()).isNull();
200 private SnapshotDto getUnprocessedSnapshot(String componentUuid) {
201 List<SnapshotDto> projectSnapshots = dbClient.snapshotDao().selectAnalysesByQuery(dbTester.getSession(),
202 new SnapshotQuery().setRootComponentUuid(componentUuid).setIsLast(false).setStatus(SnapshotDto.STATUS_UNPROCESSED));
203 assertThat(projectSnapshots).hasSize(1);
204 return projectSnapshots.get(0);