]> source.dussan.org Git - sonarqube.git/blob
f616279c85da3f694943c6e416c26e4fabbe7e2c
[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.Optional;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.sonar.api.config.internal.MapSettings;
28 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
29 import org.sonar.ce.task.projectanalysis.component.ConfigurationRepository;
30 import org.sonar.ce.task.projectanalysis.qualitygate.MutableQualityGateHolderRule;
31 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGate;
32 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGateService;
33 import org.sonar.ce.task.step.TestComputationStepContext;
34 import org.sonar.server.qualitygate.ShortLivingBranchQualityGate;
35
36 import static java.util.Collections.emptyList;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.mockito.ArgumentMatchers.any;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41
42 public class LoadQualityGateStepTest {
43
44   @Rule
45   public ExpectedException expectedException = ExpectedException.none();
46   @Rule
47   public MutableQualityGateHolderRule mutableQualityGateHolder = new MutableQualityGateHolderRule();
48
49   private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
50   private ConfigurationRepository settingsRepository = mock(ConfigurationRepository.class);
51   private QualityGateService qualityGateService = mock(QualityGateService.class);
52
53   private LoadQualityGateStep underTest = new LoadQualityGateStep(settingsRepository, qualityGateService, mutableQualityGateHolder, analysisMetadataHolder);
54
55   @Before
56   public void setUp() {
57     when(analysisMetadataHolder.isShortLivingBranch()).thenReturn(false);
58   }
59
60   @Test
61   public void add_hardcoded_QG_on_short_living_branch() {
62     when(analysisMetadataHolder.isShortLivingBranch()).thenReturn(true);
63     QualityGate qualityGate = mock(QualityGate.class);
64     when(qualityGateService.findById(ShortLivingBranchQualityGate.ID)).thenReturn(Optional.of(qualityGate));
65
66     underTest.execute(new TestComputationStepContext());
67
68     assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate);
69   }
70
71   @Test
72   public void add_hardcoded_QG_on_pull_request() {
73     when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
74     QualityGate qualityGate = mock(QualityGate.class);
75     when(qualityGateService.findById(ShortLivingBranchQualityGate.ID)).thenReturn(Optional.of(qualityGate));
76
77     underTest.execute(new TestComputationStepContext());
78
79     assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate);
80   }
81
82   @Test
83   public void execute_sets_default_QualityGate_when_project_has_no_settings() {
84     when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().asConfig());
85     QualityGate defaultGate = mock(QualityGate.class);
86     when(qualityGateService.findDefaultQualityGate(any())).thenReturn(defaultGate);
87
88     underTest.execute(new TestComputationStepContext());
89
90     assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(defaultGate);
91   }
92
93   @Test
94   public void execute_sets_default_QualityGate_when_property_value_is_not_a_long() {
95     expectedException.expect(IllegalStateException.class);
96     expectedException.expectMessage("Unsupported value (10 sds) in property sonar.qualitygate");
97
98     when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", "10 sds").asConfig());
99
100     underTest.execute(new TestComputationStepContext());
101   }
102
103   @Test
104   public void execute_sets_QualityGate_if_it_can_be_found_by_service() {
105     QualityGate qualityGate = new QualityGate(10, "name", emptyList());
106
107     when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", 10).asConfig());
108     when(qualityGateService.findById(10)).thenReturn(Optional.of(qualityGate));
109
110     underTest.execute(new TestComputationStepContext());
111
112     assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate);
113   }
114
115 }