3 * Copyright (C) 2009-2020 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 private DbClient dbClient = dbTester.getDbClient();
68 private ValidateProjectStep underTest = new ValidateProjectStep(dbClient, treeRootHolder, analysisMetadataHolder);
71 public void fail_if_pr_is_targeting_branch_with_modules() {
72 ComponentDto masterProject = dbTester.components().insertPublicProject();
73 ComponentDto mergeBranch = dbTester.components().insertProjectBranch(masterProject, b -> b.setKey("mergeBranch"));
74 dbClient.componentDao().insert(dbTester.getSession(), ComponentTesting.newModuleDto(mergeBranch));
75 setBranch(BranchType.PULL_REQUEST, mergeBranch.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 'mergeBranch' before analyzing this pull request.");
84 underTest.execute(new TestComputationStepContext());
88 public void dont_fail_for_long_forked_from_master_with_modules() {
89 ComponentDto masterProject = dbTester.components().insertPublicProject();
90 dbClient.componentDao().insert(dbTester.getSession(), ComponentTesting.newModuleDto(masterProject));
91 setBranch(BranchType.BRANCH, masterProject.uuid());
92 dbTester.getSession().commit();
94 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
98 underTest.execute(new TestComputationStepContext());
101 private void setBranch(BranchType type, @Nullable String mergeBranchUuid) {
102 Branch branch = mock(Branch.class);
103 when(branch.getType()).thenReturn(type);
104 when(branch.getReferenceBranchUuid()).thenReturn(mergeBranchUuid);
105 analysisMetadataHolder.setBranch(branch);
109 public void not_fail_if_analysis_date_is_after_last_analysis() {
110 ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD").setDbKey(PROJECT_KEY);
111 dbClient.componentDao().insert(dbTester.getSession(), project);
112 dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newAnalysis(project).setCreatedAt(1420088400000L)); // 2015-01-01
113 dbTester.getSession().commit();
115 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
117 underTest.execute(new TestComputationStepContext());
121 public void fail_if_analysis_date_is_before_last_analysis() {
122 analysisMetadataHolder.setAnalysisDate(DateUtils.parseDate("2015-01-01"));
124 ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD").setDbKey(PROJECT_KEY);
125 dbClient.componentDao().insert(dbTester.getSession(), project);
126 dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newAnalysis(project).setCreatedAt(1433131200000L)); // 2015-06-01
127 dbTester.getSession().commit();
129 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
131 thrown.expect(MessageException.class);
132 thrown.expectMessage("Validation of project failed:");
133 thrown.expectMessage("Date of analysis cannot be older than the date of the last known analysis on this project. Value: ");
134 thrown.expectMessage("Latest analysis: ");
136 underTest.execute(new TestComputationStepContext());