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.ReportComponent;
40 import org.sonar.server.computation.period.Period;
41 import org.sonar.server.computation.period.PeriodsHolderRule;
43 import static org.assertj.core.api.Assertions.assertThat;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
46 import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DATE;
47 import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS;
50 public class ReportPersistAnalysisStepTest extends BaseStepTest {
52 private static final String PROJECT_KEY = "PROJECT_KEY";
53 private static final String ANALYSIS_UUID = "U1";
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 PersistAnalysisStep underTest;
78 analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
79 analysisMetadataHolder.setUuid(ANALYSIS_UUID);
80 analysisMetadataHolder.setAnalysisDate(analysisDate);
81 dbIdsRepository = new DbIdsRepositoryImpl();
83 now = DateUtils.parseDateQuietly("2015-06-02").getTime();
85 when(system2.now()).thenReturn(now);
87 underTest = new PersistAnalysisStep(system2, dbClient, treeRootHolder, analysisMetadataHolder, periodsHolder);
89 // initialize PeriodHolder to empty by default
90 periodsHolder.setPeriods();
94 protected ComputationStep step() {
99 public void persist_analysis() {
100 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
101 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
102 ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setKey("MODULE_KEY").setName("Module");
103 dbClient.componentDao().insert(dbTester.getSession(), moduleDto);
104 ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/main/java/dir").setKey("MODULE_KEY:src/main/java/dir");
105 dbClient.componentDao().insert(dbTester.getSession(), directoryDto);
106 ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, "DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java");
107 dbClient.componentDao().insert(dbTester.getSession(), fileDto);
108 dbTester.getSession().commit();
110 Component file = ReportComponent.builder(Component.Type.FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
111 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
112 Component module = ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY").setVersion("1.1").addChildren(directory).build();
113 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).setVersion("1.0").addChildren(module).build();
114 treeRootHolder.setRoot(project);
116 dbIdsRepository.setComponentId(project, projectDto.getId());
117 dbIdsRepository.setComponentId(module, moduleDto.getId());
118 dbIdsRepository.setComponentId(directory, directoryDto.getId());
119 dbIdsRepository.setComponentId(file, fileDto.getId());
123 assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(1);
125 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
126 assertThat(projectSnapshot.getComponentUuid()).isEqualTo(project.getUuid());
127 assertThat(projectSnapshot.getVersion()).isEqualTo("1.0");
128 assertThat(projectSnapshot.getLast()).isFalse();
129 assertThat(projectSnapshot.getStatus()).isEqualTo("U");
130 assertThat(projectSnapshot.getCreatedAt()).isEqualTo(analysisDate);
131 assertThat(projectSnapshot.getBuildDate()).isEqualTo(now);
133 assertThat(dbIdsRepository.getComponentId(module)).isEqualTo(moduleDto.getId());
134 assertThat(dbIdsRepository.getComponentId(directory)).isEqualTo(directoryDto.getId());
135 assertThat(dbIdsRepository.getComponentId(file)).isEqualTo(fileDto.getId());
139 public void persist_snapshots_with_periods() {
140 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
141 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
142 SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(projectDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime());
143 dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
144 dbTester.getSession().commit();
145 periodsHolder.setPeriods(new Period(1, TIMEMACHINE_MODE_DATE, "2015-01-01", analysisDate, "u1"));
147 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
148 treeRootHolder.setRoot(project);
149 dbIdsRepository.setComponentId(project, projectDto.getId());
153 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
154 assertThat(projectSnapshot.getPeriodMode(1)).isEqualTo(TIMEMACHINE_MODE_DATE);
155 assertThat(projectSnapshot.getPeriodDate(1)).isEqualTo(analysisDate);
156 assertThat(projectSnapshot.getPeriodModeParameter(1)).isNotNull();
160 public void only_persist_snapshots_with_periods_on_project_and_module() {
161 periodsHolder.setPeriods(new Period(1, TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, analysisDate, "u1"));
163 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
164 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
165 SnapshotDto projectSnapshot = SnapshotTesting.newAnalysis(projectDto);
166 dbClient.snapshotDao().insert(dbTester.getSession(), projectSnapshot);
168 ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setKey("MODULE_KEY").setName("Module");
169 dbClient.componentDao().insert(dbTester.getSession(), moduleDto);
171 ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/main/java/dir").setKey("MODULE_KEY:src/main/java/dir");
172 dbClient.componentDao().insert(dbTester.getSession(), directoryDto);
174 ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, "DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java");
175 dbClient.componentDao().insert(dbTester.getSession(), fileDto);
177 dbTester.getSession().commit();
179 Component file = ReportComponent.builder(Component.Type.FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
180 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
181 Component module = ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY").addChildren(directory).build();
182 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(module).build();
183 treeRootHolder.setRoot(project);
185 dbIdsRepository.setComponentId(project, projectDto.getId());
186 dbIdsRepository.setComponentId(module, moduleDto.getId());
187 dbIdsRepository.setComponentId(directory, directoryDto.getId());
188 dbIdsRepository.setComponentId(file, fileDto.getId());
192 SnapshotDto newProjectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
193 assertThat(newProjectSnapshot.getPeriodMode(1)).isEqualTo(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
197 public void set_no_period_on_snapshots_when_no_period() {
198 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
199 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
200 SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(projectDto);
201 dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
202 dbTester.getSession().commit();
204 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
205 treeRootHolder.setRoot(project);
206 dbIdsRepository.setComponentId(project, projectDto.getId());
210 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
211 assertThat(projectSnapshot.getPeriodMode(1)).isNull();
212 assertThat(projectSnapshot.getPeriodDate(1)).isNull();
213 assertThat(projectSnapshot.getPeriodModeParameter(1)).isNull();
216 private SnapshotDto getUnprocessedSnapshot(String componentUuid) {
217 List<SnapshotDto> projectSnapshots = dbClient.snapshotDao().selectAnalysesByQuery(dbTester.getSession(),
218 new SnapshotQuery().setComponentUuid(componentUuid).setIsLast(false).setStatus(SnapshotDto.STATUS_UNPROCESSED));
219 assertThat(projectSnapshots).hasSize(1);
220 return projectSnapshots.get(0);