]> source.dussan.org Git - sonarqube.git/blob
06cddbc136d8ea7a7283962ad9fa4f88440afe1e
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.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;
44
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;
49
50 public class ReportPersistAnalysisStepTest extends BaseStepTest {
51
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";
55
56   @Rule
57   public DbTester dbTester = DbTester.create(System2.INSTANCE);
58   @Rule
59   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
60   @Rule
61   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
62   @Rule
63   public PeriodHolderRule periodsHolder = new PeriodHolderRule();
64
65   private System2 system2 = mock(System2.class);
66   private DbClient dbClient = dbTester.getDbClient();
67   private long analysisDate;
68   private long now;
69   private PersistAnalysisStep underTest;
70
71   @Before
72   public void setup() {
73     analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
74     analysisMetadataHolder.setUuid(ANALYSIS_UUID);
75     analysisMetadataHolder.setAnalysisDate(analysisDate);
76     analysisMetadataHolder.setScmRevision(REVISION_ID);
77
78     now = DateUtils.parseDateQuietly("2015-06-02").getTime();
79
80     when(system2.now()).thenReturn(now);
81
82     underTest = new PersistAnalysisStep(system2, dbClient, treeRootHolder, analysisMetadataHolder, periodsHolder);
83
84     // initialize PeriodHolder to empty by default
85     periodsHolder.setPeriod(null);
86   }
87
88   @Override
89   protected ComputationStep step() {
90     return underTest;
91   }
92
93   @Test
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();
105
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)
110       .setUuid("ABCD")
111       .setKey(PROJECT_KEY)
112       .setProjectVersion(projectVersion)
113       .setBuildString(buildString)
114       .setScmRevisionId(REVISION_ID)
115       .addChildren(directory)
116       .build();
117     treeRootHolder.setRoot(project);
118
119     underTest.execute(new TestComputationStepContext());
120
121     assertThat(dbTester.countRowsOfTable("snapshots")).isOne();
122
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);
133   }
134
135   @Test
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));
143
144     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
145     treeRootHolder.setRoot(project);
146
147     underTest.execute(new TestComputationStepContext());
148
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();
153   }
154
155   @Test
156   public void only_persist_snapshots_with_new_code_period_on_project_and_module() {
157     periodsHolder.setPeriod(new Period("PREVIOUS_VERSION", null, analysisDate));
158
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);
163
164     ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setDbKey("MODULE_KEY").setName("Module");
165     dbTester.components().insertComponent(moduleDto);
166
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);
169
170     ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, directoryDto, "DEFG").setDbKey("MODULE_KEY:src/main/java/dir/Foo.java");
171     dbTester.components().insertComponent(fileDto);
172
173     dbTester.getSession().commit();
174
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);
179
180     underTest.execute(new TestComputationStepContext());
181
182     SnapshotDto newProjectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
183     assertThat(newProjectSnapshot.getPeriodMode()).isEqualTo("PREVIOUS_VERSION");
184   }
185
186   @Test
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();
193
194     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
195     treeRootHolder.setRoot(project);
196
197     underTest.execute(new TestComputationStepContext());
198
199     SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
200     assertThat(projectSnapshot.getPeriodMode()).isNull();
201     assertThat(projectSnapshot.getPeriodDate()).isNull();
202     assertThat(projectSnapshot.getPeriodModeParameter()).isNull();
203   }
204
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);
210   }
211
212 }