3 * Copyright (C) 2009-2023 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.ce.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.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
29 import org.sonar.ce.task.projectanalysis.component.Component;
30 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
31 import org.sonar.ce.task.projectanalysis.component.ViewsComponent;
32 import org.sonar.ce.task.projectanalysis.period.Period;
33 import org.sonar.ce.task.projectanalysis.period.PeriodHolderRule;
34 import org.sonar.ce.task.step.ComputationStep;
35 import org.sonar.ce.task.step.TestComputationStepContext;
36 import org.sonar.db.DbClient;
37 import org.sonar.db.DbTester;
38 import org.sonar.db.component.ComponentDto;
39 import org.sonar.db.component.ComponentTesting;
40 import org.sonar.db.component.SnapshotDto;
41 import org.sonar.db.component.SnapshotQuery;
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.ce.task.projectanalysis.component.Component.Type.PROJECT_VIEW;
47 import static org.sonar.ce.task.projectanalysis.component.Component.Type.SUBVIEW;
48 import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW;
49 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
51 public class ViewsPersistAnalysisStepIT extends BaseStepTest {
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 PeriodHolderRule periodsHolder = new PeriodHolderRule();
64 private System2 system2 = mock(System2.class);
65 private DbClient dbClient = dbTester.getDbClient();
66 private long analysisDate;
68 private PersistAnalysisStep underTest;
72 analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
73 analysisMetadataHolder.setUuid(ANALYSIS_UUID);
74 analysisMetadataHolder.setAnalysisDate(analysisDate);
76 now = DateUtils.parseDateQuietly("2015-06-02").getTime();
78 when(system2.now()).thenReturn(now);
80 underTest = new PersistAnalysisStep(system2, dbClient, treeRootHolder, analysisMetadataHolder, periodsHolder);
82 // initialize PeriodHolder to empty by default
83 periodsHolder.setPeriod(null);
87 protected ComputationStep step() {
92 public void persist_analysis() {
93 ComponentDto viewDto = save(ComponentTesting.newPortfolio("UUID_VIEW").setKey("KEY_VIEW"));
94 save(ComponentTesting.newSubPortfolio(viewDto, "UUID_SUBVIEW", "KEY_SUBVIEW"));
95 save(newPrivateProjectDto("proj"));
96 dbTester.getSession().commit();
98 Component projectView = ViewsComponent.builder(PROJECT_VIEW, "KEY_PROJECT_COPY").setUuid("UUID_PROJECT_COPY").build();
99 Component subView = ViewsComponent.builder(SUBVIEW, "KEY_SUBVIEW").setUuid("UUID_SUBVIEW").addChildren(projectView).build();
100 Component view = ViewsComponent.builder(VIEW, "KEY_VIEW").setUuid("UUID_VIEW").addChildren(subView).build();
101 treeRootHolder.setRoot(view);
103 underTest.execute(new TestComputationStepContext());
105 assertThat(dbTester.countRowsOfTable("snapshots")).isOne();
107 SnapshotDto viewSnapshot = getUnprocessedSnapshot(viewDto.uuid());
108 assertThat(viewSnapshot.getUuid()).isEqualTo(ANALYSIS_UUID);
109 assertThat(viewSnapshot.getRootComponentUuid()).isEqualTo(view.getUuid());
110 assertThat(viewSnapshot.getProjectVersion()).isNull();
111 assertThat(viewSnapshot.getLast()).isFalse();
112 assertThat(viewSnapshot.getStatus()).isEqualTo("U");
113 assertThat(viewSnapshot.getCreatedAt()).isEqualTo(analysisDate);
114 assertThat(viewSnapshot.getBuildDate()).isEqualTo(now);
118 public void persist_snapshots_with_new_code_period() {
119 ComponentDto viewDto = save(ComponentTesting.newPortfolio("UUID_VIEW").setKey("KEY_VIEW"));
120 ComponentDto subViewDto = save(ComponentTesting.newSubPortfolio(viewDto, "UUID_SUBVIEW", "KEY_SUBVIEW"));
121 dbTester.getSession().commit();
123 Component subView = ViewsComponent.builder(SUBVIEW, "KEY_SUBVIEW").setUuid("UUID_SUBVIEW").build();
124 Component view = ViewsComponent.builder(VIEW, "KEY_VIEW").setUuid("UUID_VIEW").addChildren(subView).build();
125 treeRootHolder.setRoot(view);
127 periodsHolder.setPeriod(new Period("NUMBER_OF_DAYS", "30", analysisDate));
129 underTest.execute(new TestComputationStepContext());
131 SnapshotDto viewSnapshot = getUnprocessedSnapshot(viewDto.uuid());
132 assertThat(viewSnapshot.getPeriodMode()).isEqualTo("NUMBER_OF_DAYS");
133 assertThat(viewSnapshot.getPeriodDate()).isEqualTo(analysisDate);
134 assertThat(viewSnapshot.getPeriodModeParameter()).isNotNull();
137 private ComponentDto save(ComponentDto componentDto) {
138 return dbTester.components().insertComponent(componentDto);
141 private SnapshotDto getUnprocessedSnapshot(String componentUuid) {
142 List<SnapshotDto> projectSnapshots = dbClient.snapshotDao().selectAnalysesByQuery(dbTester.getSession(),
143 new SnapshotQuery().setRootComponentUuid(componentUuid).setIsLast(false).setStatus(SnapshotDto.STATUS_UNPROCESSED));
144 assertThat(projectSnapshots).hasSize(1);
145 return projectSnapshots.get(0);