]> source.dussan.org Git - sonarqube.git/blob
d6f668807166d1765a06863ae3f32623f7297ef4
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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 ReportPersistAnalysisStepIT 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").setKey(PROJECT_KEY).setName("Project");
97     dbTester.components().insertComponent(projectDto);
98     ComponentDto directoryDto = ComponentTesting.newDirectory(projectDto, "CDEF", "src/main/java/dir").setKey("PROJECT_KEY:src/main/java/dir");
99     dbTester.components().insertComponent(directoryDto);
100     ComponentDto fileDto = ComponentTesting.newFileDto(projectDto, directoryDto, "DEFG").setKey("PROJECT_KEY:src/main/java/dir/Foo.java");
101     dbTester.components().insertComponent(fileDto);
102     dbTester.getSession().commit();
103
104     Component file = ReportComponent.builder(Component.Type.FILE, 3).setUuid("DEFG").setKey("PROJECT_KEY:src/main/java/dir/Foo.java").build();
105     Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid("CDEF").setKey("PROJECT_KEY:src/main/java/dir").addChildren(file).build();
106     String buildString = Optional.ofNullable(projectVersion).map(v -> randomAlphabetic(7)).orElse(null);
107     Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
108       .setUuid("ABCD")
109       .setKey(PROJECT_KEY)
110       .setProjectVersion(projectVersion)
111       .setBuildString(buildString)
112       .setScmRevisionId(REVISION_ID)
113       .addChildren(directory)
114       .build();
115     treeRootHolder.setRoot(project);
116
117     underTest.execute(new TestComputationStepContext());
118
119     assertThat(dbTester.countRowsOfTable("snapshots")).isOne();
120
121     SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
122     assertThat(projectSnapshot.getUuid()).isEqualTo(ANALYSIS_UUID);
123     assertThat(projectSnapshot.getRootComponentUuid()).isEqualTo(project.getUuid());
124     assertThat(projectSnapshot.getProjectVersion()).isEqualTo(projectVersion);
125     assertThat(projectSnapshot.getBuildString()).isEqualTo(buildString);
126     assertThat(projectSnapshot.getLast()).isFalse();
127     assertThat(projectSnapshot.getStatus()).isEqualTo("U");
128     assertThat(projectSnapshot.getCreatedAt()).isEqualTo(analysisDate);
129     assertThat(projectSnapshot.getBuildDate()).isEqualTo(now);
130     assertThat(projectSnapshot.getRevision()).isEqualTo(REVISION_ID);
131   }
132
133   @Test
134   public void persist_snapshots_with_new_code_period() {
135     ComponentDto projectDto = ComponentTesting.newPrivateProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
136     dbTester.components().insertComponent(projectDto);
137     SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(projectDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime());
138     dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
139     dbTester.getSession().commit();
140     periodsHolder.setPeriod(new Period("NUMBER_OF_DAYS", "10", analysisDate));
141
142     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
143     treeRootHolder.setRoot(project);
144
145     underTest.execute(new TestComputationStepContext());
146
147     SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
148     assertThat(projectSnapshot.getPeriodMode()).isEqualTo("NUMBER_OF_DAYS");
149     assertThat(projectSnapshot.getPeriodDate()).isEqualTo(analysisDate);
150     assertThat(projectSnapshot.getPeriodModeParameter()).isNotNull();
151   }
152
153   @Test
154   public void only_persist_snapshots_with_new_code_period_on_project_and_module() {
155     periodsHolder.setPeriod(new Period("PREVIOUS_VERSION", null, analysisDate));
156
157     ComponentDto projectDto = ComponentTesting.newPrivateProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
158     dbTester.components().insertComponent(projectDto);
159     SnapshotDto projectSnapshot = SnapshotTesting.newAnalysis(projectDto);
160     dbClient.snapshotDao().insert(dbTester.getSession(), projectSnapshot);
161
162     ComponentDto directoryDto = ComponentTesting.newDirectory(projectDto, "CDEF", "MODULE_KEY:src/main/java/dir").setKey("MODULE_KEY:src/main/java/dir");
163     dbTester.components().insertComponent(directoryDto);
164
165     ComponentDto fileDto = ComponentTesting.newFileDto(projectDto, directoryDto, "DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java");
166     dbTester.components().insertComponent(fileDto);
167
168     dbTester.getSession().commit();
169
170     Component file = ReportComponent.builder(Component.Type.FILE, 3).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
171     Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
172     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(directory).build();
173     treeRootHolder.setRoot(project);
174
175     underTest.execute(new TestComputationStepContext());
176
177     SnapshotDto newProjectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
178     assertThat(newProjectSnapshot.getPeriodMode()).isEqualTo("PREVIOUS_VERSION");
179   }
180
181   @Test
182   public void set_no_period_on_snapshots_when_no_period() {
183     ComponentDto projectDto = ComponentTesting.newPrivateProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
184     dbTester.components().insertComponent(projectDto);
185     SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(projectDto);
186     dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
187     dbTester.getSession().commit();
188
189     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
190     treeRootHolder.setRoot(project);
191
192     underTest.execute(new TestComputationStepContext());
193
194     SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
195     assertThat(projectSnapshot.getPeriodMode()).isNull();
196     assertThat(projectSnapshot.getPeriodDate()).isNull();
197     assertThat(projectSnapshot.getPeriodModeParameter()).isNull();
198   }
199
200   private SnapshotDto getUnprocessedSnapshot(String componentUuid) {
201     List<SnapshotDto> projectSnapshots = dbClient.snapshotDao().selectAnalysesByQuery(dbTester.getSession(),
202       new SnapshotQuery().setRootComponentUuid(componentUuid).setIsLast(false).setStatus(SnapshotDto.STATUS_UNPROCESSED));
203     assertThat(projectSnapshots).hasSize(1);
204     return projectSnapshots.get(0);
205   }
206
207 }