]> source.dussan.org Git - sonarqube.git/blob
6654d5edb1b091e613cc8def9ef7ca9745a6350c
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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.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;
32
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;
42
43 public class PreMeasuresComputationChecksStepTest {
44
45   public AnalysisMetadataHolderRule analysisMetadataHolder = mock();
46   public CeTaskMessages ceTaskMessages = mock();
47
48   @Test
49   public void execute_extensions() throws PreMeasuresComputationCheckException {
50     PreMeasuresComputationCheck check1 = mock(PreMeasuresComputationCheck.class);
51     PreMeasuresComputationCheck check2 = mock(PreMeasuresComputationCheck.class);
52
53     newStep(check1, check2).execute(new TestComputationStepContext());
54
55     InOrder inOrder = inOrder(check1, check2);
56     inOrder.verify(check1).onCheck(any(Context.class));
57     inOrder.verify(check2).onCheck(any(Context.class));
58   }
59
60   @Test
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);
65
66     newStep(check).execute(new TestComputationStepContext());
67
68     ArgumentCaptor<Context> contextArgumentCaptor = ArgumentCaptor.forClass(Context.class);
69     verify(check).onCheck(contextArgumentCaptor.capture());
70     assertThat(contextArgumentCaptor.getValue().getProjectUuid()).isEqualTo(project.getUuid());
71   }
72
73   @Test
74   public void context_contains_pullRequest_key_from_analysis_metadata_holder() throws PreMeasuresComputationCheckException {
75     mockPr("pr1");
76     PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
77
78     newStep(check).execute(new TestComputationStepContext());
79
80     ArgumentCaptor<Context> contextArgumentCaptor = ArgumentCaptor.forClass(Context.class);
81     verify(check).onCheck(contextArgumentCaptor.capture());
82     assertThat(contextArgumentCaptor.getValue().getBranch().getPullRequestKey()).isEqualTo("pr1");
83   }
84
85   @Test
86   public void context_contains_branch_from_analysis_metadata_holder() throws PreMeasuresComputationCheckException {
87     mockBranch("branchName");
88     PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
89
90     newStep(check).execute(new TestComputationStepContext());
91
92     ArgumentCaptor<Context> contextArgumentCaptor = ArgumentCaptor.forClass(Context.class);
93     verify(check).onCheck(contextArgumentCaptor.capture());
94     assertThat(contextArgumentCaptor.getValue().getBranch().getName()).isEqualTo("branchName");
95   }
96
97   @Test
98   public void whenCheckThrows_thenLogCeMessage() throws PreMeasuresComputationCheckException {
99     PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
100     doThrow(new PreMeasuresComputationCheckException("error"))
101       .when(check).onCheck(any());
102
103     newStep(check).execute(new TestComputationStepContext());
104
105     var messageCaptor = ArgumentCaptor.forClass(CeTaskMessages.Message.class);
106     verify(ceTaskMessages).add(messageCaptor.capture());
107     assertThat(messageCaptor.getValue().getText()).isEqualTo("error");
108   }
109
110   @Test
111   public void test_getDescription() {
112     assertThat(newStep().getDescription()).isNotEmpty();
113   }
114
115   private PreMeasuresComputationChecksStep newStep(PreMeasuresComputationCheck... preMeasuresComputationChecks) {
116     if (preMeasuresComputationChecks.length == 0) {
117       return new PreMeasuresComputationChecksStep(analysisMetadataHolder, ceTaskMessages);
118     }
119     return new PreMeasuresComputationChecksStep(analysisMetadataHolder, ceTaskMessages, preMeasuresComputationChecks);
120   }
121
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);
127   }
128
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);
134   }
135
136 }