]> source.dussan.org Git - sonarqube.git/blob
5bcc594e84cafdf49c040ec5800273fe8c882b42
[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.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.sonar.api.config.MapSettings;
28 import org.sonar.server.computation.task.projectanalysis.component.Component;
29 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
30 import org.sonar.server.computation.task.projectanalysis.component.SettingsRepository;
31 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
32 import org.sonar.server.computation.task.projectanalysis.component.VisitException;
33 import org.sonar.server.computation.task.projectanalysis.qualitygate.Condition;
34 import org.sonar.server.computation.task.projectanalysis.qualitygate.MutableQualityGateHolderRule;
35 import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGate;
36 import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGateService;
37
38 import static java.lang.String.format;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.assertj.guava.api.Assertions.assertThat;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.verify;
43 import static org.mockito.Mockito.verifyNoMoreInteractions;
44 import static org.mockito.Mockito.when;
45 import static org.sonar.test.ExceptionCauseMatcher.hasType;
46
47 public class QualityGateLoadingStepTest {
48   private static final String PROJECT_KEY = "project key";
49   private static final ReportComponent PROJECT_ALONE = ReportComponent.builder(Component.Type.PROJECT, 1).setKey(PROJECT_KEY).build();
50
51   @Rule
52   public ExpectedException expectedException = ExpectedException.none();
53   @Rule
54   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
55   @Rule
56   public MutableQualityGateHolderRule mutableQualityGateHolder = new MutableQualityGateHolderRule();
57
58   private SettingsRepository settingsRepository = mock(SettingsRepository.class);
59   private QualityGateService qualityGateService = mock(QualityGateService.class);
60
61   private LoadQualityGateStep underTest = new LoadQualityGateStep(treeRootHolder, settingsRepository, qualityGateService, mutableQualityGateHolder);
62
63   @Test
64   public void execute_sets_default_QualityGate_when_project_has_no_settings() {
65     ReportComponent root = ReportComponent.builder(Component.Type.PROJECT, 1).setKey(PROJECT_KEY).addChildren(ReportComponent.builder(Component.Type.FILE, 2).build()).build();
66     treeRootHolder.setRoot(root);
67     when(settingsRepository.getSettings(root)).thenReturn(new MapSettings());
68
69     underTest.execute();
70
71     verifyNoQualityGate();
72
73     // verify only project is processed
74     verify(settingsRepository).getSettings(root);
75     verifyNoMoreInteractions(settingsRepository);
76   }
77
78   @Test
79   public void execute_sets_default_QualityGate_when_property_value_is_not_a_long() {
80     expectedException.expect(VisitException.class);
81     expectedException.expectCause(
82       hasType(IllegalStateException.class)
83         .andMessage(format("Unsupported value (%s) in property sonar.qualitygate", "10 sds")));
84
85     treeRootHolder.setRoot(PROJECT_ALONE);
86     when(settingsRepository.getSettings(PROJECT_ALONE)).thenReturn(new MapSettings().setProperty("sonar.qualitygate", "10 sds"));
87
88     underTest.execute();
89   }
90
91   @Test
92   public void execute_sets_default_QualityGate_if_it_can_not_be_found_by_service() {
93     treeRootHolder.setRoot(PROJECT_ALONE);
94     when(settingsRepository.getSettings(PROJECT_ALONE)).thenReturn(new MapSettings().setProperty("sonar.qualitygate", 10));
95     when(qualityGateService.findById(10)).thenReturn(Optional.<QualityGate>absent());
96
97     underTest.execute();
98
99     verifyNoQualityGate();
100   }
101
102   @Test
103   public void execute_sets_QualityGate_if_it_can_be_found_by_service() {
104     QualityGate qualityGate = new QualityGate(465, "name", Collections.<Condition>emptyList());
105
106     treeRootHolder.setRoot(PROJECT_ALONE);
107     when(settingsRepository.getSettings(PROJECT_ALONE)).thenReturn(new MapSettings().setProperty("sonar.qualitygate", 10));
108     when(qualityGateService.findById(10)).thenReturn(Optional.of(qualityGate));
109
110     underTest.execute();
111
112     assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate);
113   }
114
115   private void verifyNoQualityGate() {
116     assertThat(mutableQualityGateHolder.getQualityGate()).isAbsent();
117   }
118
119 }