3 * Copyright (C) 2009-2024 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.log.CeTaskMessages;
26 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
27 import org.sonar.ce.task.projectanalysis.analysis.Branch;
28 import org.sonar.ce.task.projectanalysis.measure.PreMeasuresComputationCheck.Context;
29 import org.sonar.ce.task.step.TestComputationStepContext;
30 import org.sonar.db.component.BranchType;
31 import org.sonar.server.project.Project;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.ArgumentMatchers.any;
35 import static org.mockito.Mockito.doThrow;
36 import static org.mockito.Mockito.inOrder;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.when;
40 import static org.sonar.ce.task.projectanalysis.measure.PreMeasuresComputationCheck.PreMeasuresComputationCheckException;
41 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
43 public class PreMeasuresComputationChecksStepTest {
45 public AnalysisMetadataHolderRule analysisMetadataHolder = mock();
46 public CeTaskMessages ceTaskMessages = mock();
49 public void execute_extensions() throws PreMeasuresComputationCheckException {
50 PreMeasuresComputationCheck check1 = mock(PreMeasuresComputationCheck.class);
51 PreMeasuresComputationCheck check2 = mock(PreMeasuresComputationCheck.class);
53 newStep(check1, check2).execute(new TestComputationStepContext());
55 InOrder inOrder = inOrder(check1, check2);
56 inOrder.verify(check1).onCheck(any(Context.class));
57 inOrder.verify(check2).onCheck(any(Context.class));
61 public void context_contains_project_uuid_from_analysis_metadata_holder() throws PreMeasuresComputationCheckException {
62 Project project = Project.from(newPrivateProjectDto());
63 when(analysisMetadataHolder.getProject()).thenReturn(project);
64 PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
66 newStep(check).execute(new TestComputationStepContext());
68 ArgumentCaptor<Context> contextArgumentCaptor = ArgumentCaptor.forClass(Context.class);
69 verify(check).onCheck(contextArgumentCaptor.capture());
70 assertThat(contextArgumentCaptor.getValue().getProjectUuid()).isEqualTo(project.getUuid());
74 public void context_contains_pullRequest_key_from_analysis_metadata_holder() throws PreMeasuresComputationCheckException {
76 PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
78 newStep(check).execute(new TestComputationStepContext());
80 ArgumentCaptor<Context> contextArgumentCaptor = ArgumentCaptor.forClass(Context.class);
81 verify(check).onCheck(contextArgumentCaptor.capture());
82 assertThat(contextArgumentCaptor.getValue().getBranch().getPullRequestKey()).isEqualTo("pr1");
86 public void context_contains_branch_from_analysis_metadata_holder() throws PreMeasuresComputationCheckException {
87 mockBranch("branchName");
88 PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
90 newStep(check).execute(new TestComputationStepContext());
92 ArgumentCaptor<Context> contextArgumentCaptor = ArgumentCaptor.forClass(Context.class);
93 verify(check).onCheck(contextArgumentCaptor.capture());
94 assertThat(contextArgumentCaptor.getValue().getBranch().getName()).isEqualTo("branchName");
98 public void whenCheckThrows_thenLogCeMessage() throws PreMeasuresComputationCheckException {
99 PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
100 doThrow(new PreMeasuresComputationCheckException("error"))
101 .when(check).onCheck(any());
103 newStep(check).execute(new TestComputationStepContext());
105 var messageCaptor = ArgumentCaptor.forClass(CeTaskMessages.Message.class);
106 verify(ceTaskMessages).add(messageCaptor.capture());
107 assertThat(messageCaptor.getValue().getText()).isEqualTo("error");
111 public void test_getDescription() {
112 assertThat(newStep().getDescription()).isNotEmpty();
115 private PreMeasuresComputationChecksStep newStep(PreMeasuresComputationCheck... preMeasuresComputationChecks) {
116 if (preMeasuresComputationChecks.length == 0) {
117 return new PreMeasuresComputationChecksStep(analysisMetadataHolder, ceTaskMessages);
119 return new PreMeasuresComputationChecksStep(analysisMetadataHolder, ceTaskMessages, preMeasuresComputationChecks);
122 private void mockBranch(String branchName) {
123 Branch branch = mock(Branch.class);
124 when(branch.getName()).thenReturn(branchName);
125 when(branch.getType()).thenReturn(BranchType.BRANCH);
126 when(analysisMetadataHolder.getBranch()).thenReturn(branch);
129 private void mockPr(String pullRequestKey) {
130 Branch branch = mock(Branch.class);
131 when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
132 when(branch.getPullRequestKey()).thenReturn(pullRequestKey);
133 when(analysisMetadataHolder.getBranch()).thenReturn(branch);