]> source.dussan.org Git - sonarqube.git/blob
6abfb9f5b44218e7faa8feb621cdf015ce629cca
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.step;
21
22 import com.google.common.base.Optional;
23 import java.util.Collections;
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.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
30 import org.sonar.server.computation.task.projectanalysis.component.ConfigurationRepository;
31 import org.sonar.server.computation.task.projectanalysis.qualitygate.MutableQualityGateHolderRule;
32 import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGate;
33 import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGateService;
34 import org.sonar.server.qualitygate.ShortLivingBranchQualityGate;
35
36 import static java.lang.String.format;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.assertj.guava.api.Assertions.assertThat;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.verifyNoMoreInteractions;
42 import static org.mockito.Mockito.when;
43
44 public class LoadQualityGateStepTest {
45
46   @Rule
47   public ExpectedException expectedException = ExpectedException.none();
48   @Rule
49   public MutableQualityGateHolderRule mutableQualityGateHolder = new MutableQualityGateHolderRule();
50
51   private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
52   private ConfigurationRepository settingsRepository = mock(ConfigurationRepository.class);
53   private QualityGateService qualityGateService = mock(QualityGateService.class);
54
55   private LoadQualityGateStep underTest = new LoadQualityGateStep(settingsRepository, qualityGateService, mutableQualityGateHolder, analysisMetadataHolder);
56
57   @Before
58   public void setUp() {
59     when(analysisMetadataHolder.isShortLivingBranch()).thenReturn(false);
60   }
61
62   @Test
63   public void add_hardcoded_QG_on_short_living_branch() {
64     when(analysisMetadataHolder.isShortLivingBranch()).thenReturn(true);
65     QualityGate qualityGate = mock(QualityGate.class);
66     when(qualityGateService.findById(ShortLivingBranchQualityGate.ID)).thenReturn(Optional.of(qualityGate));
67
68     underTest.execute();
69
70     assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate);
71   }
72
73   @Test
74   public void execute_sets_default_QualityGate_when_project_has_no_settings() {
75     when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().asConfig());
76
77     underTest.execute();
78
79     verifyNoQualityGate();
80
81     // verify only project is processed
82     verify(settingsRepository).getConfiguration();
83     verifyNoMoreInteractions(settingsRepository);
84   }
85
86   @Test
87   public void execute_sets_default_QualityGate_when_property_value_is_not_a_long() {
88     expectedException.expect(IllegalStateException.class);
89     expectedException.expectMessage(format("Unsupported value (%s) in property sonar.qualitygate", "10 sds"));
90
91     when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", "10 sds").asConfig());
92
93     underTest.execute();
94   }
95
96   @Test
97   public void execute_sets_default_QualityGate_if_it_can_not_be_found_by_service() {
98     when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", 10).asConfig());
99     when(qualityGateService.findById(10)).thenReturn(Optional.absent());
100
101     underTest.execute();
102
103     verifyNoQualityGate();
104   }
105
106   @Test
107   public void execute_sets_QualityGate_if_it_can_be_found_by_service() {
108     QualityGate qualityGate = new QualityGate(465, "name", Collections.emptyList());
109
110     when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", 10).asConfig());
111     when(qualityGateService.findById(10)).thenReturn(Optional.of(qualityGate));
112
113     underTest.execute();
114
115     assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate);
116   }
117
118   private void verifyNoQualityGate() {
119     assertThat(mutableQualityGateHolder.getQualityGate()).isAbsent();
120   }
121
122 }