3 * Copyright (C) 2009-2022 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.Date;
23 import javax.annotation.Nullable;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.utils.DateUtils;
27 import org.sonar.api.utils.MessageException;
28 import org.sonar.api.utils.System2;
29 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
30 import org.sonar.ce.task.projectanalysis.analysis.Branch;
31 import org.sonar.ce.task.projectanalysis.component.Component;
32 import org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl;
33 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
34 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
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.BranchType;
39 import org.sonar.db.component.ComponentDto;
40 import org.sonar.db.component.ComponentTesting;
41 import org.sonar.db.component.SnapshotTesting;
43 import static org.assertj.core.api.Assertions.assertThatThrownBy;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
47 public class ValidateProjectStepTest {
48 static long PAST_ANALYSIS_TIME = 1_420_088_400_000L; // 2015-01-01
49 static long DEFAULT_ANALYSIS_TIME = 1_433_131_200_000L; // 2015-06-01
51 static final String PROJECT_KEY = "PROJECT_KEY";
52 static final Branch DEFAULT_BRANCH = new DefaultBranchImpl();
55 public DbTester db = DbTester.create(System2.INSTANCE);
57 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
59 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
60 .setAnalysisDate(new Date(DEFAULT_ANALYSIS_TIME))
61 .setBranch(DEFAULT_BRANCH);
63 private final DbClient dbClient = db.getDbClient();
65 private final ValidateProjectStep underTest = new ValidateProjectStep(dbClient, treeRootHolder, analysisMetadataHolder);
68 public void dont_fail_for_long_forked_from_master_with_modules() {
69 ComponentDto masterProject = db.components().insertPublicProject();
70 dbClient.componentDao().insert(db.getSession(), ComponentTesting.newModuleDto(masterProject));
71 setBranch(BranchType.BRANCH, masterProject.uuid());
72 db.getSession().commit();
74 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
78 underTest.execute(new TestComputationStepContext());
82 public void not_fail_if_analysis_date_is_after_last_analysis() {
83 ComponentDto project = ComponentTesting.newPrivateProjectDto("ABCD").setKey(PROJECT_KEY);
84 db.components().insertComponent(project);
85 dbClient.snapshotDao().insert(db.getSession(), SnapshotTesting.newAnalysis(project).setCreatedAt(PAST_ANALYSIS_TIME));
86 db.getSession().commit();
88 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
90 underTest.execute(new TestComputationStepContext());
94 public void fail_if_pr_is_targeting_branch_with_modules() {
95 ComponentDto masterProject = db.components().insertPublicProject();
96 ComponentDto mergeBranch = db.components().insertProjectBranch(masterProject, b -> b.setKey("mergeBranch"));
97 dbClient.componentDao().insert(db.getSession(), ComponentTesting.newModuleDto(mergeBranch));
98 setBranch(BranchType.PULL_REQUEST, mergeBranch.uuid());
99 db.getSession().commit();
101 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
105 var stepContext = new TestComputationStepContext();
106 assertThatThrownBy(() -> underTest.execute(stepContext))
107 .isInstanceOf(MessageException.class)
108 .hasMessage("Due to an upgrade, you need first to re-analyze the target branch 'mergeBranch' before analyzing this pull request.");
112 public void fail_if_analysis_date_is_before_last_analysis() {
113 analysisMetadataHolder.setAnalysisDate(DateUtils.parseDate("2015-01-01"));
115 ComponentDto project = ComponentTesting.newPrivateProjectDto("ABCD").setKey(PROJECT_KEY);
116 db.components().insertComponent(project);
117 dbClient.snapshotDao().insert(db.getSession(), SnapshotTesting.newAnalysis(project).setCreatedAt(1433131200000L)); // 2015-06-01
118 db.getSession().commit();
120 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
122 var stepContext = new TestComputationStepContext();
123 assertThatThrownBy(() -> underTest.execute(stepContext))
124 .isInstanceOf(MessageException.class)
125 .hasMessageContainingAll("Validation of project failed:",
126 "Date of analysis cannot be older than the date of the last known analysis on this project. Value: ",
127 "Latest analysis: ");
131 public void fail_when_project_key_is_invalid() {
132 ComponentDto project = db.components().insertPrivateProject(p -> p.setKey("inv$lid!"));
133 db.components().insertSnapshot(project, a -> a.setCreatedAt(PAST_ANALYSIS_TIME));
134 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1)
135 .setUuid(project.uuid())
136 .setKey(project.getKey())
139 var stepContext = new TestComputationStepContext();
140 assertThatThrownBy(() -> underTest.execute(stepContext))
141 .isInstanceOf(MessageException.class)
142 .hasMessageContainingAll("Validation of project failed:",
143 "The project key ‘inv$lid!’ contains invalid characters.",
144 "Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.",
145 "You should update the project key with the expected format.");
148 private void setBranch(BranchType type, @Nullable String mergeBranchUuid) {
149 Branch branch = mock(Branch.class);
150 when(branch.getType()).thenReturn(type);
151 when(branch.getReferenceBranchUuid()).thenReturn(mergeBranchUuid);
152 analysisMetadataHolder.setBranch(branch);