3 * Copyright (C) 2009-2021 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.impl.utils.TestSystem2;
27 import org.sonar.api.utils.DateUtils;
28 import org.sonar.api.utils.MessageException;
29 import org.sonar.api.utils.System2;
30 import org.sonar.ce.task.log.CeTaskMessages;
31 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
32 import org.sonar.ce.task.projectanalysis.analysis.Branch;
33 import org.sonar.ce.task.projectanalysis.component.Component;
34 import org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl;
35 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
36 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
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.BranchType;
41 import org.sonar.db.component.ComponentDto;
42 import org.sonar.db.component.ComponentTesting;
43 import org.sonar.db.component.SnapshotTesting;
45 import static org.assertj.core.api.Assertions.assertThatThrownBy;
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.verifyNoInteractions;
48 import static org.mockito.Mockito.when;
50 public class ValidateProjectStepTest {
52 static long PAST_ANALYSIS_TIME = 1_420_088_400_000L; // 2015-01-01
53 static long DEFAULT_ANALYSIS_TIME = 1_433_131_200_000L; // 2015-06-01
54 static long NOW = 1_500_000_000_000L;
56 static final String PROJECT_KEY = "PROJECT_KEY";
57 static final Branch DEFAULT_BRANCH = new DefaultBranchImpl();
60 public DbTester db = DbTester.create(System2.INSTANCE);
62 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
64 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
65 .setAnalysisDate(new Date(DEFAULT_ANALYSIS_TIME))
66 .setBranch(DEFAULT_BRANCH);
67 private final System2 system2 = new TestSystem2().setNow(NOW);
69 private final CeTaskMessages taskMessages = mock(CeTaskMessages.class);
70 private final DbClient dbClient = db.getDbClient();
72 private final ValidateProjectStep underTest = new ValidateProjectStep(dbClient, treeRootHolder, analysisMetadataHolder, taskMessages, system2);
75 public void dont_fail_for_long_forked_from_master_with_modules() {
76 ComponentDto masterProject = db.components().insertPublicProject();
77 dbClient.componentDao().insert(db.getSession(), ComponentTesting.newModuleDto(masterProject));
78 setBranch(BranchType.BRANCH, masterProject.uuid());
79 db.getSession().commit();
81 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
85 underTest.execute(new TestComputationStepContext());
86 verifyNoInteractions(taskMessages);
90 public void not_fail_if_analysis_date_is_after_last_analysis() {
91 ComponentDto project = ComponentTesting.newPrivateProjectDto("ABCD").setDbKey(PROJECT_KEY);
92 db.components().insertComponent(project);
93 dbClient.snapshotDao().insert(db.getSession(), SnapshotTesting.newAnalysis(project).setCreatedAt(PAST_ANALYSIS_TIME));
94 db.getSession().commit();
96 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
98 underTest.execute(new TestComputationStepContext());
102 public void fail_if_pr_is_targeting_branch_with_modules() {
103 ComponentDto masterProject = db.components().insertPublicProject();
104 ComponentDto mergeBranch = db.components().insertProjectBranch(masterProject, b -> b.setKey("mergeBranch"));
105 dbClient.componentDao().insert(db.getSession(), ComponentTesting.newModuleDto(mergeBranch));
106 setBranch(BranchType.PULL_REQUEST, mergeBranch.uuid());
107 db.getSession().commit();
109 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
113 var stepContext = new TestComputationStepContext();
114 assertThatThrownBy(() -> underTest.execute(stepContext))
115 .isInstanceOf(MessageException.class)
116 .hasMessage("Due to an upgrade, you need first to re-analyze the target branch 'mergeBranch' before analyzing this pull request.");
120 public void fail_if_analysis_date_is_before_last_analysis() {
121 analysisMetadataHolder.setAnalysisDate(DateUtils.parseDate("2015-01-01"));
123 ComponentDto project = ComponentTesting.newPrivateProjectDto("ABCD").setDbKey(PROJECT_KEY);
124 db.components().insertComponent(project);
125 dbClient.snapshotDao().insert(db.getSession(), SnapshotTesting.newAnalysis(project).setCreatedAt(1433131200000L)); // 2015-06-01
126 db.getSession().commit();
128 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
130 var stepContext = new TestComputationStepContext();
131 assertThatThrownBy(() -> underTest.execute(stepContext))
132 .isInstanceOf(MessageException.class)
133 .hasMessageContainingAll("Validation of project failed:",
134 "Date of analysis cannot be older than the date of the last known analysis on this project. Value: ",
135 "Latest analysis: ");
139 public void fail_when_project_key_is_invalid() {
140 ComponentDto project = db.components().insertPrivateProject(p -> p.setDbKey("inv$lid!"));
141 db.components().insertSnapshot(project, a -> a.setCreatedAt(PAST_ANALYSIS_TIME));
142 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1)
143 .setUuid(project.uuid())
144 .setKey(project.getKey())
147 var stepContext = new TestComputationStepContext();
148 assertThatThrownBy(() -> underTest.execute(stepContext))
149 .isInstanceOf(MessageException.class)
150 .hasMessageContainingAll("Validation of project failed:",
151 "The project key ‘inv$lid!’ contains invalid characters.",
152 "Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.",
153 "You should update the project key with the expected format.");
156 private void setBranch(BranchType type, @Nullable String mergeBranchUuid) {
157 Branch branch = mock(Branch.class);
158 when(branch.getType()).thenReturn(type);
159 when(branch.getReferenceBranchUuid()).thenReturn(mergeBranchUuid);
160 analysisMetadataHolder.setBranch(branch);