]> source.dussan.org Git - sonarqube.git/blob
f0d791aae40dc28040d706aa51df4e8b46e00930
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.step;
21
22 import java.util.Arrays;
23 import java.util.Optional;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
28 import org.sonar.ce.task.projectanalysis.metric.Metric;
29 import org.sonar.ce.task.projectanalysis.metric.MetricImpl;
30 import org.sonar.ce.task.projectanalysis.qualitygate.Condition;
31 import org.sonar.ce.task.projectanalysis.qualitygate.MutableQualityGateHolderRule;
32 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGate;
33 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGateService;
34 import org.sonar.ce.task.step.TestComputationStepContext;
35 import org.sonar.server.project.Project;
36
37 import static java.util.Collections.emptyList;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.ArgumentMatchers.any;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
42
43 public class LoadQualityGateStepTest {
44
45   @Rule
46   public MutableQualityGateHolderRule mutableQualityGateHolder = new MutableQualityGateHolderRule();
47
48   private final AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
49   private final QualityGateService qualityGateService = mock(QualityGateService.class);
50
51   private final LoadQualityGateStep underTest = new LoadQualityGateStep(qualityGateService, mutableQualityGateHolder, analysisMetadataHolder);
52
53   @Before
54   public void setUp() {
55   }
56
57   @Test
58   public void filter_conditions_on_pull_request() {
59     Metric newMetric = new MetricImpl("1", "new_key", "name", Metric.MetricType.INT);
60     Metric metric = new MetricImpl("2", "key", "name", Metric.MetricType.INT);
61     Condition variation = new Condition(newMetric, Condition.Operator.GREATER_THAN.getDbValue(), "1.0");
62     Condition condition = new Condition(metric, Condition.Operator.GREATER_THAN.getDbValue(), "1.0");
63
64     when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
65     QualityGate defaultGate = new QualityGate("1", "qg", Arrays.asList(variation, condition));
66     when(qualityGateService.findDefaultQualityGate()).thenReturn(defaultGate);
67
68     underTest.execute(new TestComputationStepContext());
69
70     assertThat(mutableQualityGateHolder.getQualityGate().get().getConditions()).containsExactly(variation);
71   }
72
73   @Test
74   public void execute_sets_default_QualityGate_when_project_has_no_settings() {
75     QualityGate defaultGate = mock(QualityGate.class);
76     when(qualityGateService.findDefaultQualityGate()).thenReturn(defaultGate);
77
78     underTest.execute(new TestComputationStepContext());
79
80     assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(defaultGate);
81   }
82
83   @Test
84   public void execute_sets_QualityGate_if_it_can_be_found_by_service() {
85     QualityGate qualityGate = new QualityGate("10", "name", emptyList());
86
87     when(analysisMetadataHolder.getProject()).thenReturn(mock(Project.class));
88     when(qualityGateService.findQualityGate(any(Project.class))).thenReturn(Optional.of(qualityGate));
89
90     underTest.execute(new TestComputationStepContext());
91
92     assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate);
93   }
94
95 }