]> source.dussan.org Git - sonarqube.git/blob
5f2481025f05c0ebf497c3ccb0dafa3da18f58d4
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.measure;
21
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;
31
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;
39
40 public class PreMeasuresComputationChecksStepTest {
41
42   public AnalysisMetadataHolderRule analysisMetadataHolder = mock(AnalysisMetadataHolderRule.class);
43
44   @Test
45   public void execute_extensions() {
46     PreMeasuresComputationCheck check1 = mock(PreMeasuresComputationCheck.class);
47     PreMeasuresComputationCheck check2 = mock(PreMeasuresComputationCheck.class);
48
49     newStep(check1, check2).execute(new TestComputationStepContext());
50
51     InOrder inOrder = inOrder(check1, check2);
52     inOrder.verify(check1).onCheck(any(Context.class));
53     inOrder.verify(check2).onCheck(any(Context.class));
54   }
55
56   @Test
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);
61
62     newStep(check).execute(new TestComputationStepContext());
63
64     ArgumentCaptor<Context> contextArgumentCaptor = ArgumentCaptor.forClass(Context.class);
65     verify(check).onCheck(contextArgumentCaptor.capture());
66     assertThat(contextArgumentCaptor.getValue().getProjectUuid()).isEqualTo(project.getUuid());
67   }
68
69   @Test
70   public void context_contains_pullRequest_key_from_analysis_metadata_holder() {
71     mockPr("pr1");
72     PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
73
74     newStep(check).execute(new TestComputationStepContext());
75
76     ArgumentCaptor<Context> contextArgumentCaptor = ArgumentCaptor.forClass(Context.class);
77     verify(check).onCheck(contextArgumentCaptor.capture());
78     assertThat(contextArgumentCaptor.getValue().getBranch().getPullRequestKey()).isEqualTo("pr1");
79   }
80
81   @Test
82   public void context_contains_branch_from_analysis_metadata_holder() {
83     mockBranch("branchName");
84     PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
85
86     newStep(check).execute(new TestComputationStepContext());
87
88     ArgumentCaptor<Context> contextArgumentCaptor = ArgumentCaptor.forClass(Context.class);
89     verify(check).onCheck(contextArgumentCaptor.capture());
90     assertThat(contextArgumentCaptor.getValue().getBranch().getName()).isEqualTo("branchName");
91   }
92
93   @Test
94   public void test_getDescription() {
95     assertThat(newStep().getDescription()).isNotEmpty();
96   }
97
98   private PreMeasuresComputationChecksStep newStep(PreMeasuresComputationCheck... preMeasuresComputationChecks) {
99     if (preMeasuresComputationChecks.length == 0) {
100       return new PreMeasuresComputationChecksStep(analysisMetadataHolder);
101     }
102     return new PreMeasuresComputationChecksStep(analysisMetadataHolder, preMeasuresComputationChecks);
103   }
104
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);
110   }
111
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);
117   }
118
119 }