3 * Copyright (C) 2009-2022 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 ReportPersistAnalysisStepTest 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").setDbKey(PROJECT_KEY).setName("Project");
97 dbTester.components().insertComponent(projectDto);
98 ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setDbKey("MODULE_KEY").setName("Module");
99 dbTester.components().insertComponent(moduleDto);
100 ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/main/java/dir").setDbKey("MODULE_KEY:src/main/java/dir");
101 dbTester.components().insertComponent(directoryDto);
102 ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, directoryDto, "DEFG").setDbKey("MODULE_KEY:src/main/java/dir/Foo.java");
103 dbTester.components().insertComponent(fileDto);
104 dbTester.getSession().commit();
106 Component file = ReportComponent.builder(Component.Type.FILE, 3).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
107 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
108 String buildString = Optional.ofNullable(projectVersion).map(v -> randomAlphabetic(7)).orElse(null);
109 Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
112 .setProjectVersion(projectVersion)
113 .setBuildString(buildString)
114 .setScmRevisionId(REVISION_ID)
115 .addChildren(directory)
117 treeRootHolder.setRoot(project);
119 underTest.execute(new TestComputationStepContext());
121 assertThat(dbTester.countRowsOfTable("snapshots")).isOne();
123 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
124 assertThat(projectSnapshot.getUuid()).isEqualTo(ANALYSIS_UUID);
125 assertThat(projectSnapshot.getComponentUuid()).isEqualTo(project.getUuid());
126 assertThat(projectSnapshot.getProjectVersion()).isEqualTo(projectVersion);
127 assertThat(projectSnapshot.getBuildString()).isEqualTo(buildString);
128 assertThat(projectSnapshot.getLast()).isFalse();
129 assertThat(projectSnapshot.getStatus()).isEqualTo("U");
130 assertThat(projectSnapshot.getCreatedAt()).isEqualTo(analysisDate);
131 assertThat(projectSnapshot.getBuildDate()).isEqualTo(now);
132 assertThat(projectSnapshot.getRevision()).isEqualTo(REVISION_ID);
136 public void persist_snapshots_with_new_code_period() {
137 ComponentDto projectDto = ComponentTesting.newPrivateProjectDto("ABCD").setDbKey(PROJECT_KEY).setName("Project");
138 dbTester.components().insertComponent(projectDto);
139 SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(projectDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime());
140 dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
141 dbTester.getSession().commit();
142 periodsHolder.setPeriod(new Period("NUMBER_OF_DAYS", "10", analysisDate));
144 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
145 treeRootHolder.setRoot(project);
147 underTest.execute(new TestComputationStepContext());
149 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
150 assertThat(projectSnapshot.getPeriodMode()).isEqualTo("NUMBER_OF_DAYS");
151 assertThat(projectSnapshot.getPeriodDate()).isEqualTo(analysisDate);
152 assertThat(projectSnapshot.getPeriodModeParameter()).isNotNull();
156 public void only_persist_snapshots_with_new_code_period_on_project_and_module() {
157 periodsHolder.setPeriod(new Period("PREVIOUS_VERSION", null, analysisDate));
159 ComponentDto projectDto = ComponentTesting.newPrivateProjectDto("ABCD").setDbKey(PROJECT_KEY).setName("Project");
160 dbTester.components().insertComponent(projectDto);
161 SnapshotDto projectSnapshot = SnapshotTesting.newAnalysis(projectDto);
162 dbClient.snapshotDao().insert(dbTester.getSession(), projectSnapshot);
164 ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setDbKey("MODULE_KEY").setName("Module");
165 dbTester.components().insertComponent(moduleDto);
167 ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/main/java/dir").setDbKey("MODULE_KEY:src/main/java/dir");
168 dbTester.components().insertComponent(directoryDto);
170 ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, directoryDto, "DEFG").setDbKey("MODULE_KEY:src/main/java/dir/Foo.java");
171 dbTester.components().insertComponent(fileDto);
173 dbTester.getSession().commit();
175 Component file = ReportComponent.builder(Component.Type.FILE, 3).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
176 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
177 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(directory).build();
178 treeRootHolder.setRoot(project);
180 underTest.execute(new TestComputationStepContext());
182 SnapshotDto newProjectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
183 assertThat(newProjectSnapshot.getPeriodMode()).isEqualTo("PREVIOUS_VERSION");
187 public void set_no_period_on_snapshots_when_no_period() {
188 ComponentDto projectDto = ComponentTesting.newPrivateProjectDto("ABCD").setDbKey(PROJECT_KEY).setName("Project");
189 dbTester.components().insertComponent(projectDto);
190 SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(projectDto);
191 dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
192 dbTester.getSession().commit();
194 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
195 treeRootHolder.setRoot(project);
197 underTest.execute(new TestComputationStepContext());
199 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
200 assertThat(projectSnapshot.getPeriodMode()).isNull();
201 assertThat(projectSnapshot.getPeriodDate()).isNull();
202 assertThat(projectSnapshot.getPeriodModeParameter()).isNull();
205 private SnapshotDto getUnprocessedSnapshot(String componentUuid) {
206 List<SnapshotDto> projectSnapshots = dbClient.snapshotDao().selectAnalysesByQuery(dbTester.getSession(),
207 new SnapshotQuery().setComponentUuid(componentUuid).setIsLast(false).setStatus(SnapshotDto.STATUS_UNPROCESSED));
208 assertThat(projectSnapshots).hasSize(1);
209 return projectSnapshots.get(0);