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.period;
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
26 import org.sonar.db.DbTester;
27 import org.sonar.db.component.BranchType;
28 import org.sonar.db.component.ComponentDto;
29 import org.sonar.db.component.ComponentTesting;
30 import org.sonar.db.newcodeperiod.NewCodePeriodType;
31 import org.sonar.server.project.Project;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.assertj.core.api.Assertions.assertThatThrownBy;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
37 import static org.sonar.db.component.SnapshotTesting.newAnalysis;
39 public class NewCodeReferenceBranchComponentUuidsTest {
41 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
44 public PeriodHolderRule periodHolder = new PeriodHolderRule();
46 public DbTester db = DbTester.create();
48 private NewCodeReferenceBranchComponentUuids underTest = new NewCodeReferenceBranchComponentUuids(analysisMetadataHolder, periodHolder, db.getDbClient());
50 private ComponentDto branch1;
51 private ComponentDto branch1File;
52 private ComponentDto pr1File;
53 private ComponentDto pr2File;
54 private Project project = mock(Project.class);
55 private ComponentDto pr1;
56 private ComponentDto pr2;
57 private ComponentDto branch2;
58 private ComponentDto branch2File;
62 analysisMetadataHolder.setProject(project);
64 ComponentDto projectDto = db.components().insertPublicProject();
65 when(project.getUuid()).thenReturn(projectDto.uuid());
66 branch1 = db.components().insertProjectBranch(projectDto, b -> b.setKey("branch1"));
67 branch2 = db.components().insertProjectBranch(projectDto, b -> b.setKey("branch2"));
68 pr1 = db.components().insertProjectBranch(projectDto, b -> b.setKey("pr1").setBranchType(BranchType.PULL_REQUEST).setMergeBranchUuid(branch1.uuid()));
69 pr2 = db.components().insertProjectBranch(projectDto, b -> b.setKey("pr2").setBranchType(BranchType.PULL_REQUEST).setMergeBranchUuid(branch1.uuid()));
70 branch1File = ComponentTesting.newFileDto(branch1, null, "file").setUuid("branch1File");
71 branch2File = ComponentTesting.newFileDto(branch2, null, "file").setUuid("branch2File");
72 pr1File = ComponentTesting.newFileDto(pr1, null, "file").setUuid("file1");
73 pr2File = ComponentTesting.newFileDto(pr2, null, "file").setUuid("file2");
74 db.components().insertComponents(branch1File, pr1File, pr2File, branch2File);
78 public void should_support_db_key_when_looking_for_reference_component() {
79 periodHolder.setPeriod(new Period(NewCodePeriodType.REFERENCE_BRANCH.name(), "branch1", null));
80 db.components().insertSnapshot(newAnalysis(branch1));
81 assertThat(underTest.getComponentUuid(pr1File.getDbKey())).isEqualTo(branch1File.uuid());
85 public void should_support_key_when_looking_for_reference_component() {
86 periodHolder.setPeriod(new Period(NewCodePeriodType.REFERENCE_BRANCH.name(), "branch1", null));
87 db.components().insertSnapshot(newAnalysis(branch1));
88 assertThat(underTest.getComponentUuid(pr1File.getKey())).isEqualTo(branch1File.uuid());
92 public void return_null_if_file_doesnt_exist() {
93 periodHolder.setPeriod(new Period(NewCodePeriodType.REFERENCE_BRANCH.name(), "branch1", null));
94 db.components().insertSnapshot(newAnalysis(branch1));
95 assertThat(underTest.getComponentUuid("doesnt exist")).isNull();
99 public void skip_init_if_no_reference_branch_analysis() {
100 periodHolder.setPeriod(new Period(NewCodePeriodType.REFERENCE_BRANCH.name(), "branch1", null));
101 assertThat(underTest.getComponentUuid(pr1File.getDbKey())).isNull();
105 public void skip_init_if_branch_not_found() {
106 periodHolder.setPeriod(new Period(NewCodePeriodType.REFERENCE_BRANCH.name(), "unknown", null));
107 assertThat(underTest.getComponentUuid(pr1File.getDbKey())).isNull();
111 public void throw_ise_if_mode_is_not_reference_branch() {
112 periodHolder.setPeriod(new Period(NewCodePeriodType.NUMBER_OF_DAYS.name(), "10", 1000L));
113 assertThatThrownBy(() -> underTest.getComponentUuid(pr1File.getDbKey()))
114 .isInstanceOf(IllegalStateException.class);