3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.server.computation.step;
22 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.utils.DateUtils;
27 import org.sonar.api.utils.System2;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.component.ComponentDto;
31 import org.sonar.db.component.ComponentTesting;
32 import org.sonar.db.component.SnapshotDto;
33 import org.sonar.db.component.SnapshotQuery;
34 import org.sonar.db.component.SnapshotTesting;
35 import org.sonar.server.computation.analysis.AnalysisMetadataHolderRule;
36 import org.sonar.server.computation.batch.TreeRootHolderRule;
37 import org.sonar.server.computation.component.Component;
38 import org.sonar.server.computation.component.DbIdsRepositoryImpl;
39 import org.sonar.server.computation.component.FileAttributes;
40 import org.sonar.server.computation.component.ReportComponent;
41 import org.sonar.server.computation.period.Period;
42 import org.sonar.server.computation.period.PeriodsHolderRule;
44 import static org.assertj.core.api.Assertions.assertThat;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.when;
47 import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DATE;
48 import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS;
51 public class ReportPersistSnapshotsStepTest extends BaseStepTest {
53 private static final String PROJECT_KEY = "PROJECT_KEY";
56 public DbTester dbTester = DbTester.create(System2.INSTANCE);
58 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
60 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
62 public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
64 System2 system2 = mock(System2.class);
66 DbIdsRepositoryImpl dbIdsRepository;
68 DbClient dbClient = dbTester.getDbClient();
74 PersistSnapshotsStep underTest;
78 analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
79 analysisMetadataHolder.setAnalysisDate(analysisDate);
80 dbIdsRepository = new DbIdsRepositoryImpl();
82 now = DateUtils.parseDateQuietly("2015-06-02").getTime();
84 when(system2.now()).thenReturn(now);
86 underTest = new PersistSnapshotsStep(system2, dbClient, treeRootHolder, analysisMetadataHolder, dbIdsRepository, periodsHolder);
88 // initialize PeriodHolder to empty by default
89 periodsHolder.setPeriods();
93 protected ComputationStep step() {
98 public void persist_snapshots() {
99 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
100 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
101 ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setKey("MODULE_KEY").setName("Module");
102 dbClient.componentDao().insert(dbTester.getSession(), moduleDto);
103 ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/main/java/dir").setKey("MODULE_KEY:src/main/java/dir");
104 dbClient.componentDao().insert(dbTester.getSession(), directoryDto);
105 ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, "DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java");
106 dbClient.componentDao().insert(dbTester.getSession(), fileDto);
107 dbTester.getSession().commit();
109 Component file = ReportComponent.builder(Component.Type.FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
110 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
111 Component module = ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY").setVersion("1.1").addChildren(directory).build();
112 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).setVersion("1.0").addChildren(module).build();
113 treeRootHolder.setRoot(project);
115 dbIdsRepository.setComponentId(project, projectDto.getId());
116 dbIdsRepository.setComponentId(module, moduleDto.getId());
117 dbIdsRepository.setComponentId(directory, directoryDto.getId());
118 dbIdsRepository.setComponentId(file, fileDto.getId());
122 assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(4);
124 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
125 assertThat(projectSnapshot.getComponentUuid()).isEqualTo(project.getUuid());
126 assertThat(projectSnapshot.getRootComponentUuid()).isEqualTo(project.getUuid());
127 assertThat(projectSnapshot.getRootId()).isNull();
128 assertThat(projectSnapshot.getParentId()).isNull();
129 assertThat(projectSnapshot.getDepth()).isEqualTo(0);
130 assertThat(projectSnapshot.getPath()).isNullOrEmpty();
131 assertThat(projectSnapshot.getQualifier()).isEqualTo("TRK");
132 assertThat(projectSnapshot.getScope()).isEqualTo("PRJ");
133 assertThat(projectSnapshot.getVersion()).isEqualTo("1.0");
134 assertThat(projectSnapshot.getLast()).isFalse();
135 assertThat(projectSnapshot.getStatus()).isEqualTo("U");
136 assertThat(projectSnapshot.getCreatedAt()).isEqualTo(analysisDate);
137 assertThat(projectSnapshot.getBuildDate()).isEqualTo(now);
139 SnapshotDto moduleSnapshot = getUnprocessedSnapshot(moduleDto.uuid());
140 assertThat(moduleSnapshot.getComponentUuid()).isEqualTo(module.getUuid());
141 assertThat(moduleSnapshot.getRootComponentUuid()).isEqualTo(project.getUuid());
142 assertThat(moduleSnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
143 assertThat(moduleSnapshot.getParentId()).isEqualTo(projectSnapshot.getId());
144 assertThat(moduleSnapshot.getDepth()).isEqualTo(1);
145 assertThat(moduleSnapshot.getPath()).isEqualTo(projectSnapshot.getId() + ".");
146 assertThat(moduleSnapshot.getQualifier()).isEqualTo("BRC");
147 assertThat(moduleSnapshot.getScope()).isEqualTo("PRJ");
148 assertThat(moduleSnapshot.getVersion()).isEqualTo("1.1");
149 assertThat(moduleSnapshot.getLast()).isFalse();
150 assertThat(moduleSnapshot.getStatus()).isEqualTo("U");
151 assertThat(moduleSnapshot.getCreatedAt()).isEqualTo(analysisDate);
152 assertThat(moduleSnapshot.getBuildDate()).isEqualTo(now);
154 SnapshotDto directorySnapshot = getUnprocessedSnapshot(directoryDto.uuid());
155 assertThat(directorySnapshot.getComponentUuid()).isEqualTo(directory.getUuid());
156 assertThat(directorySnapshot.getRootComponentUuid()).isEqualTo(project.getUuid());
157 assertThat(directorySnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
158 assertThat(directorySnapshot.getParentId()).isEqualTo(moduleSnapshot.getId());
159 assertThat(directorySnapshot.getDepth()).isEqualTo(2);
160 assertThat(directorySnapshot.getPath()).isEqualTo(projectSnapshot.getId() + "." + moduleSnapshot.getId() + ".");
161 assertThat(directorySnapshot.getQualifier()).isEqualTo("DIR");
162 assertThat(directorySnapshot.getScope()).isEqualTo("DIR");
163 assertThat(directorySnapshot.getVersion()).isNull();
164 assertThat(directorySnapshot.getLast()).isFalse();
165 assertThat(directorySnapshot.getStatus()).isEqualTo("U");
166 assertThat(directorySnapshot.getCreatedAt()).isEqualTo(analysisDate);
167 assertThat(directorySnapshot.getBuildDate()).isEqualTo(now);
169 SnapshotDto fileSnapshot = getUnprocessedSnapshot(fileDto.uuid());
170 assertThat(fileSnapshot.getComponentUuid()).isEqualTo(file.getUuid());
171 assertThat(fileSnapshot.getRootComponentUuid()).isEqualTo(project.getUuid());
172 assertThat(fileSnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
173 assertThat(fileSnapshot.getParentId()).isEqualTo(directorySnapshot.getId());
174 assertThat(fileSnapshot.getDepth()).isEqualTo(3);
175 assertThat(fileSnapshot.getPath()).isEqualTo(projectSnapshot.getId() + "." + moduleSnapshot.getId() + "." + directorySnapshot.getId() + ".");
176 assertThat(fileSnapshot.getQualifier()).isEqualTo("FIL");
177 assertThat(fileSnapshot.getScope()).isEqualTo("FIL");
178 assertThat(fileSnapshot.getVersion()).isNull();
179 assertThat(fileSnapshot.getLast()).isFalse();
180 assertThat(fileSnapshot.getStatus()).isEqualTo("U");
181 assertThat(fileSnapshot.getCreatedAt()).isEqualTo(analysisDate);
182 assertThat(fileSnapshot.getBuildDate()).isEqualTo(now);
184 assertThat(dbIdsRepository.getSnapshotId(project)).isEqualTo(projectSnapshot.getId());
185 assertThat(dbIdsRepository.getComponentId(module)).isEqualTo(moduleDto.getId());
186 assertThat(dbIdsRepository.getComponentId(directory)).isEqualTo(directoryDto.getId());
187 assertThat(dbIdsRepository.getComponentId(file)).isEqualTo(fileDto.getId());
191 public void persist_unit_test() {
192 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
193 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
194 ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setKey("MODULE_KEY").setName("Module");
195 dbClient.componentDao().insert(dbTester.getSession(), moduleDto);
196 ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/test/java/dir").setKey("MODULE_KEY:src/test/java/dir");
197 dbClient.componentDao().insert(dbTester.getSession(), directoryDto);
198 ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, "DEFG").setKey("MODULE_KEY:src/test/java/dir/FooTest.java").setQualifier("UTS");
199 dbClient.componentDao().insert(dbTester.getSession(), fileDto);
200 dbTester.getSession().commit();
202 Component file = ReportComponent.builder(Component.Type.FILE, 3).setUuid("DEFG").setKey(PROJECT_KEY + ":src/main/java/dir/Foo.java")
203 .setFileAttributes(new FileAttributes(true, null)).build();
204 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid("CDEF").setKey(PROJECT_KEY + ":src/main/java/dir").addChildren(file).build();
205 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(directory).build();
206 treeRootHolder.setRoot(project);
208 dbIdsRepository.setComponentId(project, projectDto.getId());
209 dbIdsRepository.setComponentId(directory, directoryDto.getId());
210 dbIdsRepository.setComponentId(file, fileDto.getId());
214 SnapshotDto fileSnapshot = getUnprocessedSnapshot(fileDto.uuid());
215 assertThat(fileSnapshot.getQualifier()).isEqualTo("UTS");
216 assertThat(fileSnapshot.getScope()).isEqualTo("FIL");
220 public void persist_snapshots_on_multi_modules() {
221 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY);
222 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
223 ComponentDto moduleADto = ComponentTesting.newModuleDto("BCDE", projectDto).setKey("MODULE_A");
224 dbClient.componentDao().insert(dbTester.getSession(), moduleADto);
225 ComponentDto subModuleADto = ComponentTesting.newModuleDto("CDEF", moduleADto).setKey("SUB_MODULE_A");
226 dbClient.componentDao().insert(dbTester.getSession(), subModuleADto);
227 ComponentDto moduleBDto = ComponentTesting.newModuleDto("DEFG", projectDto).setKey("MODULE_B");
228 dbClient.componentDao().insert(dbTester.getSession(), moduleBDto);
229 dbTester.getSession().commit();
231 Component moduleB = ReportComponent.builder(Component.Type.MODULE, 4).setUuid("DEFG").setKey("MODULE_B").build();
232 Component subModuleA = ReportComponent.builder(Component.Type.MODULE, 3).setUuid("CDEF").setKey("SUB_MODULE_A").build();
233 Component moduleA = ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_A").addChildren(subModuleA).build();
234 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(moduleA, moduleB).build();
235 treeRootHolder.setRoot(project);
237 dbIdsRepository.setComponentId(project, projectDto.getId());
238 dbIdsRepository.setComponentId(moduleA, moduleADto.getId());
239 dbIdsRepository.setComponentId(subModuleA, subModuleADto.getId());
240 dbIdsRepository.setComponentId(moduleB, moduleBDto.getId());
244 assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(4);
246 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
247 assertThat(projectSnapshot.getRootComponentUuid()).isEqualTo(project.getUuid());
248 assertThat(projectSnapshot.getRootId()).isNull();
249 assertThat(projectSnapshot.getParentId()).isNull();
250 assertThat(projectSnapshot.getDepth()).isEqualTo(0);
251 assertThat(projectSnapshot.getPath()).isNullOrEmpty();
253 SnapshotDto moduleASnapshot = getUnprocessedSnapshot(moduleADto.uuid());
254 assertThat(moduleASnapshot.getRootComponentUuid()).isEqualTo(project.getUuid());
255 assertThat(moduleASnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
256 assertThat(moduleASnapshot.getParentId()).isEqualTo(projectSnapshot.getId());
257 assertThat(moduleASnapshot.getDepth()).isEqualTo(1);
258 assertThat(moduleASnapshot.getPath()).isEqualTo(projectSnapshot.getId() + ".");
260 SnapshotDto subModuleASnapshot = getUnprocessedSnapshot(subModuleADto.uuid());
261 assertThat(subModuleASnapshot.getRootComponentUuid()).isEqualTo(project.getUuid());
262 assertThat(subModuleASnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
263 assertThat(subModuleASnapshot.getParentId()).isEqualTo(moduleASnapshot.getId());
264 assertThat(subModuleASnapshot.getDepth()).isEqualTo(2);
265 assertThat(subModuleASnapshot.getPath()).isEqualTo(projectSnapshot.getId() + "." + moduleASnapshot.getId() + ".");
267 SnapshotDto moduleBSnapshot = getUnprocessedSnapshot(moduleBDto.uuid());
268 assertThat(moduleBSnapshot.getRootComponentUuid()).isEqualTo(project.getUuid());
269 assertThat(moduleBSnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
270 assertThat(moduleBSnapshot.getParentId()).isEqualTo(projectSnapshot.getId());
271 assertThat(moduleBSnapshot.getDepth()).isEqualTo(1);
272 assertThat(moduleBSnapshot.getPath()).isEqualTo(projectSnapshot.getId() + ".");
276 public void persist_snapshots_with_periods() {
277 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
278 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
279 SnapshotDto snapshotDto = SnapshotTesting.newSnapshotForProject(projectDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime());
280 dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
281 dbTester.getSession().commit();
282 periodsHolder.setPeriods(new Period(1, TIMEMACHINE_MODE_DATE, "2015-01-01", analysisDate, 123L));
284 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
285 treeRootHolder.setRoot(project);
286 dbIdsRepository.setComponentId(project, projectDto.getId());
290 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
291 assertThat(projectSnapshot.getPeriodMode(1)).isEqualTo(TIMEMACHINE_MODE_DATE);
292 assertThat(projectSnapshot.getPeriodDate(1)).isEqualTo(analysisDate);
293 assertThat(projectSnapshot.getPeriodModeParameter(1)).isNotNull();
297 public void only_persist_snapshots_with_periods_on_project_and_module() {
298 periodsHolder.setPeriods(new Period(1, TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, analysisDate, 123L));
300 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
301 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
302 SnapshotDto projectSnapshot = SnapshotTesting.newSnapshotForProject(projectDto);
303 dbClient.snapshotDao().insert(dbTester.getSession(), projectSnapshot);
305 ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setKey("MODULE_KEY").setName("Module");
306 dbClient.componentDao().insert(dbTester.getSession(), moduleDto);
307 SnapshotDto moduleSnapshot = SnapshotTesting.createForComponent(moduleDto, projectSnapshot);
308 dbClient.snapshotDao().insert(dbTester.getSession(), moduleSnapshot);
310 ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/main/java/dir").setKey("MODULE_KEY:src/main/java/dir");
311 dbClient.componentDao().insert(dbTester.getSession(), directoryDto);
312 SnapshotDto directorySnapshot = SnapshotTesting.createForComponent(directoryDto, moduleSnapshot);
313 dbClient.snapshotDao().insert(dbTester.getSession(), directorySnapshot);
315 ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, "DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java");
316 dbClient.componentDao().insert(dbTester.getSession(), fileDto);
317 SnapshotDto fileSnapshot = SnapshotTesting.createForComponent(fileDto, directorySnapshot);
318 dbClient.snapshotDao().insert(dbTester.getSession(), fileSnapshot);
320 dbTester.getSession().commit();
322 Component file = ReportComponent.builder(Component.Type.FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
323 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
324 Component module = ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY").addChildren(directory).build();
325 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(module).build();
326 treeRootHolder.setRoot(project);
328 dbIdsRepository.setComponentId(project, projectDto.getId());
329 dbIdsRepository.setComponentId(module, moduleDto.getId());
330 dbIdsRepository.setComponentId(directory, directoryDto.getId());
331 dbIdsRepository.setComponentId(file, fileDto.getId());
335 SnapshotDto newProjectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
336 assertThat(newProjectSnapshot.getPeriodMode(1)).isEqualTo(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
338 SnapshotDto newModuleSnapshot = getUnprocessedSnapshot(moduleDto.uuid());
339 assertThat(newModuleSnapshot.getPeriodMode(1)).isEqualTo(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
341 SnapshotDto newDirectorySnapshot = getUnprocessedSnapshot(directoryDto.uuid());
342 assertThat(newDirectorySnapshot.getPeriodMode(1)).isNull();
344 SnapshotDto newFileSnapshot = getUnprocessedSnapshot(fileDto.uuid());
345 assertThat(newFileSnapshot.getPeriodMode(1)).isNull();
349 public void set_no_period_on_snapshots_when_no_period() {
350 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
351 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
352 SnapshotDto snapshotDto = SnapshotTesting.newSnapshotForProject(projectDto);
353 dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
354 dbTester.getSession().commit();
356 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
357 treeRootHolder.setRoot(project);
358 dbIdsRepository.setComponentId(project, projectDto.getId());
362 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
363 assertThat(projectSnapshot.getPeriodMode(1)).isNull();
364 assertThat(projectSnapshot.getPeriodDate(1)).isNull();
365 assertThat(projectSnapshot.getPeriodModeParameter(1)).isNull();
368 private SnapshotDto getUnprocessedSnapshot(String componentUuid) {
369 List<SnapshotDto> projectSnapshots = dbClient.snapshotDao().selectSnapshotsByQuery(dbTester.getSession(),
370 new SnapshotQuery().setComponentUuid(componentUuid).setIsLast(false).setStatus(SnapshotDto.STATUS_UNPROCESSED));
371 assertThat(projectSnapshots).hasSize(1);
372 return projectSnapshots.get(0);