]> source.dussan.org Git - sonarqube.git/blob
809c2505fd6038dd8f5287aa4efe9ecdae56b1c6
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.junit.rules.ExpectedException;
28 import org.sonar.api.config.internal.MapSettings;
29 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
30 import org.sonar.ce.task.projectanalysis.component.ConfigurationRepository;
31 import org.sonar.ce.task.projectanalysis.metric.Metric;
32 import org.sonar.ce.task.projectanalysis.metric.MetricImpl;
33 import org.sonar.ce.task.projectanalysis.qualitygate.Condition;
34 import org.sonar.ce.task.projectanalysis.qualitygate.MutableQualityGateHolderRule;
35 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGate;
36 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGateService;
37 import org.sonar.ce.task.step.TestComputationStepContext;
38
39 import static java.util.Collections.emptyList;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.mockito.ArgumentMatchers.any;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44
45 public class LoadQualityGateStepTest {
46
47   @Rule
48   public ExpectedException expectedException = ExpectedException.none();
49   @Rule
50   public MutableQualityGateHolderRule mutableQualityGateHolder = new MutableQualityGateHolderRule();
51
52   private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
53   private ConfigurationRepository settingsRepository = mock(ConfigurationRepository.class);
54   private QualityGateService qualityGateService = mock(QualityGateService.class);
55
56   private LoadQualityGateStep underTest = new LoadQualityGateStep(settingsRepository, qualityGateService, mutableQualityGateHolder, analysisMetadataHolder);
57
58   @Before
59   public void setUp() {
60     when(analysisMetadataHolder.isShortLivingBranch()).thenReturn(false);
61   }
62
63   @Test
64   public void filter_conditions_on_short_living_branch() {
65     when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().asConfig());
66
67     Metric newMetric = new MetricImpl(1, "new_key", "name", Metric.MetricType.INT);
68     Metric metric = new MetricImpl(2, "key", "name", Metric.MetricType.INT);
69     Condition variation = new Condition(newMetric, Condition.Operator.GREATER_THAN.getDbValue(), "1.0");
70     Condition condition = new Condition(metric, Condition.Operator.GREATER_THAN.getDbValue(), "1.0");
71
72     when(analysisMetadataHolder.isSLBorPR()).thenReturn(true);
73     QualityGate defaultGate = new QualityGate(1, "qg", Arrays.asList(variation, condition));
74     when(qualityGateService.findDefaultQualityGate(any())).thenReturn(defaultGate);
75
76     underTest.execute(new TestComputationStepContext());
77
78     assertThat(mutableQualityGateHolder.getQualityGate().get().getConditions()).containsExactly(variation);
79   }
80
81   @Test
82   public void filter_conditions_on_pull_request() {
83     when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().asConfig());
84
85     Metric newMetric = new MetricImpl(1, "new_key", "name", Metric.MetricType.INT);
86     Metric metric = new MetricImpl(2, "key", "name", Metric.MetricType.INT);
87     Condition variation = new Condition(newMetric, Condition.Operator.GREATER_THAN.getDbValue(), "1.0");
88     Condition condition = new Condition(metric, Condition.Operator.GREATER_THAN.getDbValue(), "1.0");
89
90     when(analysisMetadataHolder.isSLBorPR()).thenReturn(true);
91     QualityGate defaultGate = new QualityGate(1, "qg", Arrays.asList(variation, condition));
92     when(qualityGateService.findDefaultQualityGate(any())).thenReturn(defaultGate);
93
94     underTest.execute(new TestComputationStepContext());
95
96     assertThat(mutableQualityGateHolder.getQualityGate().get().getConditions()).containsExactly(variation);
97   }
98
99   @Test
100   public void execute_sets_default_QualityGate_when_project_has_no_settings() {
101     when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().asConfig());
102     QualityGate defaultGate = mock(QualityGate.class);
103     when(qualityGateService.findDefaultQualityGate(any())).thenReturn(defaultGate);
104
105     underTest.execute(new TestComputationStepContext());
106
107     assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(defaultGate);
108   }
109
110   @Test
111   public void execute_sets_default_QualityGate_when_property_value_is_not_a_long() {
112     expectedException.expect(IllegalStateException.class);
113     expectedException.expectMessage("Unsupported value (10 sds) in property sonar.qualitygate");
114
115     when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", "10 sds").asConfig());
116
117     underTest.execute(new TestComputationStepContext());
118   }
119
120   @Test
121   public void execute_sets_QualityGate_if_it_can_be_found_by_service() {
122     QualityGate qualityGate = new QualityGate(10, "name", emptyList());
123
124     when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", 10).asConfig());
125     when(qualityGateService.findById(10)).thenReturn(Optional.of(qualityGate));
126
127     underTest.execute(new TestComputationStepContext());
128
129     assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate);
130   }
131
132 }