3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.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.db.organization.OrganizationDto;
36 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
37 import org.sonar.server.computation.task.projectanalysis.component.Component;
38 import org.sonar.server.computation.task.projectanalysis.component.DbIdsRepositoryImpl;
39 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
40 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
41 import org.sonar.server.computation.task.projectanalysis.period.Period;
42 import org.sonar.server.computation.task.projectanalysis.period.PeriodHolderRule;
43 import org.sonar.server.computation.task.step.ComputationStep;
45 import static org.assertj.core.api.Assertions.assertThat;
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.when;
48 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DATE;
49 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS;
51 public class ReportPersistAnalysisStepTest extends BaseStepTest {
53 private static final String PROJECT_KEY = "PROJECT_KEY";
54 private static final String ANALYSIS_UUID = "U1";
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);
67 private DbIdsRepositoryImpl dbIdsRepository;
69 private DbClient dbClient = dbTester.getDbClient();
71 private long analysisDate;
75 PersistAnalysisStep underTest;
79 analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
80 analysisMetadataHolder.setUuid(ANALYSIS_UUID);
81 analysisMetadataHolder.setAnalysisDate(analysisDate);
82 dbIdsRepository = new DbIdsRepositoryImpl();
84 now = DateUtils.parseDateQuietly("2015-06-02").getTime();
86 when(system2.now()).thenReturn(now);
88 underTest = new PersistAnalysisStep(system2, dbClient, treeRootHolder, analysisMetadataHolder, periodsHolder);
90 // initialize PeriodHolder to empty by default
91 periodsHolder.setPeriod(null);
95 protected ComputationStep step() {
100 public void persist_analysis() {
101 analysisMetadataHolder.setIncrementalAnalysis(false);
102 OrganizationDto organizationDto = dbTester.organizations().insert();
103 ComponentDto projectDto = ComponentTesting.newPrivateProjectDto(organizationDto, "ABCD").setDbKey(PROJECT_KEY).setName("Project");
104 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
105 ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setDbKey("MODULE_KEY").setName("Module");
106 dbClient.componentDao().insert(dbTester.getSession(), moduleDto);
107 ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/main/java/dir").setDbKey("MODULE_KEY:src/main/java/dir");
108 dbClient.componentDao().insert(dbTester.getSession(), directoryDto);
109 ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, directoryDto, "DEFG").setDbKey("MODULE_KEY:src/main/java/dir/Foo.java");
110 dbClient.componentDao().insert(dbTester.getSession(), fileDto);
111 dbTester.getSession().commit();
113 Component file = ReportComponent.builder(Component.Type.FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
114 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
115 Component module = ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY").setVersion("1.1").addChildren(directory).build();
116 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).setVersion("1.0").addChildren(module).build();
117 treeRootHolder.setRoot(project);
119 dbIdsRepository.setComponentId(project, projectDto.getId());
120 dbIdsRepository.setComponentId(module, moduleDto.getId());
121 dbIdsRepository.setComponentId(directory, directoryDto.getId());
122 dbIdsRepository.setComponentId(file, fileDto.getId());
126 assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(1);
128 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
129 assertThat(projectSnapshot.getUuid()).isEqualTo(ANALYSIS_UUID);
130 assertThat(projectSnapshot.getComponentUuid()).isEqualTo(project.getUuid());
131 assertThat(projectSnapshot.getVersion()).isEqualTo("1.0");
132 assertThat(projectSnapshot.getLast()).isFalse();
133 assertThat(projectSnapshot.getStatus()).isEqualTo("U");
134 assertThat(projectSnapshot.getCreatedAt()).isEqualTo(analysisDate);
135 assertThat(projectSnapshot.getBuildDate()).isEqualTo(now);
136 assertThat(projectSnapshot.getIncremental()).isFalse();
138 assertThat(dbIdsRepository.getComponentId(module)).isEqualTo(moduleDto.getId());
139 assertThat(dbIdsRepository.getComponentId(directory)).isEqualTo(directoryDto.getId());
140 assertThat(dbIdsRepository.getComponentId(file)).isEqualTo(fileDto.getId());
144 public void persist_snapshots_with_leak_period() {
145 analysisMetadataHolder.setIncrementalAnalysis(false);
146 OrganizationDto organizationDto = dbTester.organizations().insert();
147 ComponentDto projectDto = ComponentTesting.newPrivateProjectDto(organizationDto, "ABCD").setDbKey(PROJECT_KEY).setName("Project");
148 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
149 SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(projectDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime());
150 dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
151 dbTester.getSession().commit();
152 periodsHolder.setPeriod(new Period(LEAK_PERIOD_MODE_DATE, "2015-01-01", analysisDate, "u1"));
154 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
155 treeRootHolder.setRoot(project);
156 dbIdsRepository.setComponentId(project, projectDto.getId());
160 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
161 assertThat(projectSnapshot.getPeriodMode()).isEqualTo(LEAK_PERIOD_MODE_DATE);
162 assertThat(projectSnapshot.getPeriodDate()).isEqualTo(analysisDate);
163 assertThat(projectSnapshot.getPeriodModeParameter()).isNotNull();
167 public void only_persist_snapshots_with_leak_period_on_project_and_module() {
168 analysisMetadataHolder.setIncrementalAnalysis(false);
169 periodsHolder.setPeriod(new Period(LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS, null, analysisDate, "u1"));
171 OrganizationDto organizationDto = dbTester.organizations().insert();
172 ComponentDto projectDto = ComponentTesting.newPrivateProjectDto(organizationDto, "ABCD").setDbKey(PROJECT_KEY).setName("Project");
173 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
174 SnapshotDto projectSnapshot = SnapshotTesting.newAnalysis(projectDto);
175 dbClient.snapshotDao().insert(dbTester.getSession(), projectSnapshot);
177 ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setDbKey("MODULE_KEY").setName("Module");
178 dbClient.componentDao().insert(dbTester.getSession(), moduleDto);
180 ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/main/java/dir").setDbKey("MODULE_KEY:src/main/java/dir");
181 dbClient.componentDao().insert(dbTester.getSession(), directoryDto);
183 ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, directoryDto, "DEFG").setDbKey("MODULE_KEY:src/main/java/dir/Foo.java");
184 dbClient.componentDao().insert(dbTester.getSession(), fileDto);
186 dbTester.getSession().commit();
188 Component file = ReportComponent.builder(Component.Type.FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
189 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
190 Component module = ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY").addChildren(directory).build();
191 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(module).build();
192 treeRootHolder.setRoot(project);
194 dbIdsRepository.setComponentId(project, projectDto.getId());
195 dbIdsRepository.setComponentId(module, moduleDto.getId());
196 dbIdsRepository.setComponentId(directory, directoryDto.getId());
197 dbIdsRepository.setComponentId(file, fileDto.getId());
201 SnapshotDto newProjectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
202 assertThat(newProjectSnapshot.getPeriodMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS);
206 public void set_no_period_on_snapshots_when_no_period() {
207 analysisMetadataHolder.setIncrementalAnalysis(false);
208 ComponentDto projectDto = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD").setDbKey(PROJECT_KEY).setName("Project");
209 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
210 SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(projectDto);
211 dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
212 dbTester.getSession().commit();
214 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
215 treeRootHolder.setRoot(project);
216 dbIdsRepository.setComponentId(project, projectDto.getId());
220 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
221 assertThat(projectSnapshot.getPeriodMode()).isNull();
222 assertThat(projectSnapshot.getPeriodDate()).isNull();
223 assertThat(projectSnapshot.getPeriodModeParameter()).isNull();
226 private SnapshotDto getUnprocessedSnapshot(String componentUuid) {
227 List<SnapshotDto> projectSnapshots = dbClient.snapshotDao().selectAnalysesByQuery(dbTester.getSession(),
228 new SnapshotQuery().setComponentUuid(componentUuid).setIsLast(false).setStatus(SnapshotDto.STATUS_UNPROCESSED));
229 assertThat(projectSnapshots).hasSize(1);
230 return projectSnapshots.get(0);