]> source.dussan.org Git - sonarqube.git/blob
68bca2c288f33d99df84fe20d9ebe31d8fcc878b
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.step;
21
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.DbIdsRepositoryImpl;
32 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
33 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
34 import org.sonar.ce.task.projectanalysis.period.Period;
35 import org.sonar.ce.task.projectanalysis.period.PeriodHolderRule;
36 import org.sonar.ce.task.step.ComputationStep;
37 import org.sonar.ce.task.step.TestComputationStepContext;
38 import org.sonar.db.DbClient;
39 import org.sonar.db.DbTester;
40 import org.sonar.db.component.ComponentDto;
41 import org.sonar.db.component.ComponentTesting;
42 import org.sonar.db.component.SnapshotDto;
43 import org.sonar.db.component.SnapshotQuery;
44 import org.sonar.db.component.SnapshotTesting;
45 import org.sonar.db.organization.OrganizationDto;
46
47 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
48 import static org.assertj.core.api.Assertions.assertThat;
49 import static org.mockito.Mockito.mock;
50 import static org.mockito.Mockito.when;
51 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DATE;
52 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_VERSION;
53
54 public class ReportPersistAnalysisStepTest extends BaseStepTest {
55
56   private static final String PROJECT_KEY = "PROJECT_KEY";
57   private static final String ANALYSIS_UUID = "U1";
58   private static final String REVISION_ID = "5f6432a1";
59
60   @Rule
61   public DbTester dbTester = DbTester.create(System2.INSTANCE);
62   @Rule
63   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
64   @Rule
65   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
66   @Rule
67   public PeriodHolderRule periodsHolder = new PeriodHolderRule();
68
69   private System2 system2 = mock(System2.class);
70   private DbIdsRepositoryImpl dbIdsRepository;
71   private DbClient dbClient = dbTester.getDbClient();
72   private long analysisDate;
73   private long now;
74   private PersistAnalysisStep underTest;
75
76   @Before
77   public void setup() {
78     analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
79     analysisMetadataHolder.setUuid(ANALYSIS_UUID);
80     analysisMetadataHolder.setAnalysisDate(analysisDate);
81     analysisMetadataHolder.setScmRevisionId(REVISION_ID);
82     dbIdsRepository = new DbIdsRepositoryImpl();
83
84     now = DateUtils.parseDateQuietly("2015-06-02").getTime();
85
86     when(system2.now()).thenReturn(now);
87
88     underTest = new PersistAnalysisStep(system2, dbClient, treeRootHolder, analysisMetadataHolder, periodsHolder);
89
90     // initialize PeriodHolder to empty by default
91     periodsHolder.setPeriod(null);
92   }
93
94   @Override
95   protected ComputationStep step() {
96     return underTest;
97   }
98
99   @Test
100   public void persist_analysis() {
101     String projectVersion = randomAlphabetic(10);
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();
112
113     Component file = ReportComponent.builder(Component.Type.FILE, 3).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
114     Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
115     String buildString = Optional.ofNullable(projectVersion).map(v -> randomAlphabetic(7)).orElse(null);
116     Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
117       .setUuid("ABCD")
118       .setKey(PROJECT_KEY)
119       .setProjectVersion(projectVersion)
120       .setBuildString(buildString)
121       .setScmRevisionId(REVISION_ID)
122       .addChildren(directory)
123       .build();
124     treeRootHolder.setRoot(project);
125
126     dbIdsRepository.setComponentId(project, projectDto.getId());
127     dbIdsRepository.setComponentId(directory, directoryDto.getId());
128     dbIdsRepository.setComponentId(file, fileDto.getId());
129
130     underTest.execute(new TestComputationStepContext());
131
132     assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(1);
133
134     SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
135     assertThat(projectSnapshot.getUuid()).isEqualTo(ANALYSIS_UUID);
136     assertThat(projectSnapshot.getComponentUuid()).isEqualTo(project.getUuid());
137     assertThat(projectSnapshot.getProjectVersion()).isEqualTo(projectVersion);
138     assertThat(projectSnapshot.getBuildString()).isEqualTo(buildString);
139     assertThat(projectSnapshot.getLast()).isFalse();
140     assertThat(projectSnapshot.getStatus()).isEqualTo("U");
141     assertThat(projectSnapshot.getCreatedAt()).isEqualTo(analysisDate);
142     assertThat(projectSnapshot.getBuildDate()).isEqualTo(now);
143     assertThat(projectSnapshot.getRevision()).isEqualTo(REVISION_ID);
144
145     assertThat(dbIdsRepository.getComponentId(directory)).isEqualTo(directoryDto.getId());
146     assertThat(dbIdsRepository.getComponentId(file)).isEqualTo(fileDto.getId());
147   }
148
149   @Test
150   public void persist_snapshots_with_leak_period() {
151     OrganizationDto organizationDto = dbTester.organizations().insert();
152     ComponentDto projectDto = ComponentTesting.newPrivateProjectDto(organizationDto, "ABCD").setDbKey(PROJECT_KEY).setName("Project");
153     dbClient.componentDao().insert(dbTester.getSession(), projectDto);
154     SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(projectDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime());
155     dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
156     dbTester.getSession().commit();
157     periodsHolder.setPeriod(new Period(LEAK_PERIOD_MODE_DATE, "2015-01-01", analysisDate, "u1"));
158
159     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
160     treeRootHolder.setRoot(project);
161     dbIdsRepository.setComponentId(project, projectDto.getId());
162
163     underTest.execute(new TestComputationStepContext());
164
165     SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
166     assertThat(projectSnapshot.getPeriodMode()).isEqualTo(LEAK_PERIOD_MODE_DATE);
167     assertThat(projectSnapshot.getPeriodDate()).isEqualTo(analysisDate);
168     assertThat(projectSnapshot.getPeriodModeParameter()).isNotNull();
169   }
170
171   @Test
172   public void only_persist_snapshots_with_leak_period_on_project_and_module() {
173     periodsHolder.setPeriod(new Period(LEAK_PERIOD_MODE_PREVIOUS_VERSION, null, analysisDate, "u1"));
174
175     OrganizationDto organizationDto = dbTester.organizations().insert();
176     ComponentDto projectDto = ComponentTesting.newPrivateProjectDto(organizationDto, "ABCD").setDbKey(PROJECT_KEY).setName("Project");
177     dbClient.componentDao().insert(dbTester.getSession(), projectDto);
178     SnapshotDto projectSnapshot = SnapshotTesting.newAnalysis(projectDto);
179     dbClient.snapshotDao().insert(dbTester.getSession(), projectSnapshot);
180
181     ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setDbKey("MODULE_KEY").setName("Module");
182     dbClient.componentDao().insert(dbTester.getSession(), moduleDto);
183
184     ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/main/java/dir").setDbKey("MODULE_KEY:src/main/java/dir");
185     dbClient.componentDao().insert(dbTester.getSession(), directoryDto);
186
187     ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, directoryDto, "DEFG").setDbKey("MODULE_KEY:src/main/java/dir/Foo.java");
188     dbClient.componentDao().insert(dbTester.getSession(), fileDto);
189
190     dbTester.getSession().commit();
191
192     Component file = ReportComponent.builder(Component.Type.FILE, 3).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
193     Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
194     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(directory).build();
195     treeRootHolder.setRoot(project);
196
197     dbIdsRepository.setComponentId(project, projectDto.getId());
198     dbIdsRepository.setComponentId(directory, directoryDto.getId());
199     dbIdsRepository.setComponentId(file, fileDto.getId());
200
201     underTest.execute(new TestComputationStepContext());
202
203     SnapshotDto newProjectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
204     assertThat(newProjectSnapshot.getPeriodMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION);
205   }
206
207   @Test
208   public void set_no_period_on_snapshots_when_no_period() {
209     ComponentDto projectDto = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD").setDbKey(PROJECT_KEY).setName("Project");
210     dbClient.componentDao().insert(dbTester.getSession(), projectDto);
211     SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(projectDto);
212     dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
213     dbTester.getSession().commit();
214
215     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
216     treeRootHolder.setRoot(project);
217     dbIdsRepository.setComponentId(project, projectDto.getId());
218
219     underTest.execute(new TestComputationStepContext());
220
221     SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
222     assertThat(projectSnapshot.getPeriodMode()).isNull();
223     assertThat(projectSnapshot.getPeriodDate()).isNull();
224     assertThat(projectSnapshot.getPeriodModeParameter()).isNull();
225   }
226
227   private SnapshotDto getUnprocessedSnapshot(String componentUuid) {
228     List<SnapshotDto> projectSnapshots = dbClient.snapshotDao().selectAnalysesByQuery(dbTester.getSession(),
229       new SnapshotQuery().setComponentUuid(componentUuid).setIsLast(false).setStatus(SnapshotDto.STATUS_UNPROCESSED));
230     assertThat(projectSnapshots).hasSize(1);
231     return projectSnapshots.get(0);
232   }
233
234 }