]> source.dussan.org Git - sonarqube.git/blob
0e36782973a4f387b2c5dee3533c118ec6844f8c
[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.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;
42
43 import static org.assertj.core.api.Assertions.assertThatThrownBy;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
46
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
50
51   static final String PROJECT_KEY = "PROJECT_KEY";
52   static final Branch DEFAULT_BRANCH = new DefaultBranchImpl();
53
54   @Rule
55   public DbTester db = DbTester.create(System2.INSTANCE);
56   @Rule
57   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
58   @Rule
59   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
60     .setAnalysisDate(new Date(DEFAULT_ANALYSIS_TIME))
61     .setBranch(DEFAULT_BRANCH);
62
63   private final DbClient dbClient = db.getDbClient();
64
65   private final ValidateProjectStep underTest = new ValidateProjectStep(dbClient, treeRootHolder, analysisMetadataHolder);
66
67   @Test
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();
73
74     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
75       .setKey("branch")
76       .build());
77
78     underTest.execute(new TestComputationStepContext());
79   }
80
81   @Test
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();
87
88     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
89
90     underTest.execute(new TestComputationStepContext());
91   }
92
93   @Test
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();
100
101     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
102       .setKey("branch")
103       .build());
104
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.");
109   }
110
111   @Test
112   public void fail_if_analysis_date_is_before_last_analysis() {
113     analysisMetadataHolder.setAnalysisDate(DateUtils.parseDate("2015-01-01"));
114
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();
119
120     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
121
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: ");
128   }
129
130   @Test
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())
137       .build());
138
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.");
146   }
147
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);
153   }
154 }