3 * Copyright (C) 2009-2019 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.junit.rules.ExpectedException;
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.projectanalysis.analysis.AnalysisMetadataHolderRule;
31 import org.sonar.ce.task.projectanalysis.analysis.Branch;
32 import org.sonar.ce.task.projectanalysis.component.Component;
33 import org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl;
34 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
35 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
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.BranchType;
40 import org.sonar.db.component.ComponentDto;
41 import org.sonar.db.component.ComponentTesting;
42 import org.sonar.db.component.SnapshotTesting;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
47 public class ValidateProjectStepTest {
49 static long DEFAULT_ANALYSIS_TIME = 1433131200000L; // 2015-06-01
50 static final String PROJECT_KEY = "PROJECT_KEY";
51 static final Branch DEFAULT_BRANCH = new DefaultBranchImpl();
54 public DbTester dbTester = DbTester.create(System2.INSTANCE);
57 public ExpectedException thrown = ExpectedException.none();
60 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
63 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
64 .setAnalysisDate(new Date(DEFAULT_ANALYSIS_TIME))
65 .setBranch(DEFAULT_BRANCH);
67 DbClient dbClient = dbTester.getDbClient();
69 ValidateProjectStep underTest = new ValidateProjectStep(dbClient, treeRootHolder, analysisMetadataHolder);
72 public void fail_if_slb_is_targeting_master_with_modules() {
73 ComponentDto masterProject = dbTester.components().insertMainBranch();
74 dbClient.componentDao().insert(dbTester.getSession(), ComponentTesting.newModuleDto(masterProject));
75 setBranch(BranchType.SHORT, masterProject.uuid());
76 dbTester.getSession().commit();
78 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
82 thrown.expect(MessageException.class);
83 thrown.expectMessage("Due to an upgrade, you need first to re-analyze the target branch 'master' before analyzing this short-lived branch.");
84 underTest.execute(new TestComputationStepContext());
88 public void fail_if_pr_is_targeting_branch_with_modules() {
89 ComponentDto masterProject = dbTester.components().insertMainBranch();
90 ComponentDto mergeBranch = dbTester.components().insertProjectBranch(masterProject, b -> b.setKey("mergeBranch"));
91 dbClient.componentDao().insert(dbTester.getSession(), ComponentTesting.newModuleDto(mergeBranch));
92 setBranch(BranchType.PULL_REQUEST, mergeBranch.uuid());
93 dbTester.getSession().commit();
95 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
99 thrown.expect(MessageException.class);
100 thrown.expectMessage("Due to an upgrade, you need first to re-analyze the target branch 'mergeBranch' before analyzing this pull request.");
101 underTest.execute(new TestComputationStepContext());
105 public void dont_fail_if_slb_is_targeting_branch_without_modules() {
106 ComponentDto masterProject = dbTester.components().insertMainBranch();
107 setBranch(BranchType.SHORT, masterProject.uuid());
108 dbTester.getSession().commit();
110 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
114 underTest.execute(new TestComputationStepContext());
118 public void dont_fail_for_long_forked_from_master_with_modules() {
119 ComponentDto masterProject = dbTester.components().insertMainBranch();
120 dbClient.componentDao().insert(dbTester.getSession(), ComponentTesting.newModuleDto(masterProject));
121 setBranch(BranchType.LONG, masterProject.uuid());
122 dbTester.getSession().commit();
124 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
128 underTest.execute(new TestComputationStepContext());
131 private void setBranch(BranchType type, @Nullable String mergeBranchUuid) {
132 Branch branch = mock(Branch.class);
133 when(branch.getType()).thenReturn(type);
134 when(branch.getMergeBranchUuid()).thenReturn(mergeBranchUuid);
135 analysisMetadataHolder.setBranch(branch);
139 public void not_fail_if_analysis_date_is_after_last_analysis() {
140 ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD").setDbKey(PROJECT_KEY);
141 dbClient.componentDao().insert(dbTester.getSession(), project);
142 dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newAnalysis(project).setCreatedAt(1420088400000L)); // 2015-01-01
143 dbTester.getSession().commit();
145 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
147 underTest.execute(new TestComputationStepContext());
151 public void fail_if_analysis_date_is_before_last_analysis() {
152 analysisMetadataHolder.setAnalysisDate(DateUtils.parseDate("2015-01-01"));
154 ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD").setDbKey(PROJECT_KEY);
155 dbClient.componentDao().insert(dbTester.getSession(), project);
156 dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newAnalysis(project).setCreatedAt(1433131200000L)); // 2015-06-01
157 dbTester.getSession().commit();
159 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
161 thrown.expect(MessageException.class);
162 thrown.expectMessage("Validation of project failed:");
163 thrown.expectMessage("Date of analysis cannot be older than the date of the last known analysis on this project. Value: ");
164 thrown.expectMessage("Latest analysis: ");
166 underTest.execute(new TestComputationStepContext());