3 * Copyright (C) 2009-2023 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.measure;
22 import org.junit.Test;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.InOrder;
25 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
26 import org.sonar.ce.task.projectanalysis.analysis.Branch;
27 import org.sonar.ce.task.projectanalysis.measure.PreMeasuresComputationCheck.Context;
28 import org.sonar.ce.task.step.TestComputationStepContext;
29 import org.sonar.db.component.BranchType;
30 import org.sonar.server.project.Project;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.mockito.ArgumentMatchers.any;
34 import static org.mockito.Mockito.inOrder;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.verify;
37 import static org.mockito.Mockito.when;
38 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
40 public class PreMeasuresComputationChecksStepTest {
42 public AnalysisMetadataHolderRule analysisMetadataHolder = mock(AnalysisMetadataHolderRule.class);
45 public void execute_extensions() {
46 PreMeasuresComputationCheck check1 = mock(PreMeasuresComputationCheck.class);
47 PreMeasuresComputationCheck check2 = mock(PreMeasuresComputationCheck.class);
49 newStep(check1, check2).execute(new TestComputationStepContext());
51 InOrder inOrder = inOrder(check1, check2);
52 inOrder.verify(check1).onCheck(any(Context.class));
53 inOrder.verify(check2).onCheck(any(Context.class));
57 public void context_contains_project_uuid_from_analysis_metadata_holder() {
58 Project project = Project.from(newPrivateProjectDto());
59 when(analysisMetadataHolder.getProject()).thenReturn(project);
60 PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
62 newStep(check).execute(new TestComputationStepContext());
64 ArgumentCaptor<Context> contextArgumentCaptor = ArgumentCaptor.forClass(Context.class);
65 verify(check).onCheck(contextArgumentCaptor.capture());
66 assertThat(contextArgumentCaptor.getValue().getProjectUuid()).isEqualTo(project.getUuid());
70 public void context_contains_pullRequest_key_from_analysis_metadata_holder() {
72 PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
74 newStep(check).execute(new TestComputationStepContext());
76 ArgumentCaptor<Context> contextArgumentCaptor = ArgumentCaptor.forClass(Context.class);
77 verify(check).onCheck(contextArgumentCaptor.capture());
78 assertThat(contextArgumentCaptor.getValue().getBranch().getPullRequestKey()).isEqualTo("pr1");
82 public void context_contains_branch_from_analysis_metadata_holder() {
83 mockBranch("branchName");
84 PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
86 newStep(check).execute(new TestComputationStepContext());
88 ArgumentCaptor<Context> contextArgumentCaptor = ArgumentCaptor.forClass(Context.class);
89 verify(check).onCheck(contextArgumentCaptor.capture());
90 assertThat(contextArgumentCaptor.getValue().getBranch().getName()).isEqualTo("branchName");
94 public void test_getDescription() {
95 assertThat(newStep().getDescription()).isNotEmpty();
98 private PreMeasuresComputationChecksStep newStep(PreMeasuresComputationCheck... preMeasuresComputationChecks) {
99 if (preMeasuresComputationChecks.length == 0) {
100 return new PreMeasuresComputationChecksStep(analysisMetadataHolder);
102 return new PreMeasuresComputationChecksStep(analysisMetadataHolder, preMeasuresComputationChecks);
105 private void mockBranch(String branchName) {
106 Branch branch = mock(Branch.class);
107 when(branch.getName()).thenReturn(branchName);
108 when(branch.getType()).thenReturn(BranchType.BRANCH);
109 when(analysisMetadataHolder.getBranch()).thenReturn(branch);
112 private void mockPr(String pullRequestKey) {
113 Branch branch = mock(Branch.class);
114 when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
115 when(branch.getPullRequestKey()).thenReturn(pullRequestKey);
116 when(analysisMetadataHolder.getBranch()).thenReturn(branch);