]> source.dussan.org Git - sonarqube.git/blob
4be4925981a0354f45928a69caca13229268c3de
[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.server.qualityprofile.ws;
21
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.sonar.db.DbTester;
25 import org.sonar.db.qualityprofile.QProfileDto;
26 import org.sonar.db.qualityprofile.QualityProfileTesting;
27 import org.sonar.db.rule.RuleDefinitionDto;
28 import org.sonar.server.exceptions.BadRequestException;
29 import org.sonar.server.exceptions.NotFoundException;
30 import org.sonar.server.tester.UserSessionRule;
31
32 import static java.lang.String.format;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.assertj.core.api.Assertions.assertThatThrownBy;
35
36 public class QProfileWsSupportTest {
37
38   @Rule
39   public DbTester db = DbTester.create();
40   @Rule
41   public UserSessionRule userSession = UserSessionRule.standalone();
42
43   private final QProfileWsSupport underTest = new QProfileWsSupport(db.getDbClient(), userSession);
44
45   @Test
46   public void getProfile_returns_the_profile_specified_by_key() {
47     QProfileDto profile = db.qualityProfiles().insert();
48
49     QProfileDto loaded = underTest.getProfile(db.getSession(), QProfileReference.fromKey(profile.getKee()));
50
51     assertThat(loaded.getKee()).isEqualTo(profile.getKee());
52     assertThat(loaded.getLanguage()).isEqualTo(profile.getLanguage());
53     assertThat(loaded.getName()).isEqualTo(profile.getName());
54   }
55
56   @Test
57   public void getProfile_throws_NotFoundException_if_specified_key_does_not_exist() {
58     assertThatThrownBy(() -> underTest.getProfile(db.getSession(), QProfileReference.fromKey("missing")))
59       .isInstanceOf(NotFoundException.class)
60       .hasMessage("Quality Profile with key 'missing' does not exist");
61   }
62
63   @Test
64   public void getProfile_returns_the_profile_specified_by_name() {
65     QProfileDto profile = QualityProfileTesting.newQualityProfileDto();
66     db.qualityProfiles().insert(profile);
67
68     QProfileDto loaded = underTest.getProfile(db.getSession(), QProfileReference.fromName(profile.getLanguage(), profile.getName()));
69
70     assertThat(loaded.getKee()).isEqualTo(profile.getKee());
71     assertThat(loaded.getLanguage()).isEqualTo(profile.getLanguage());
72     assertThat(loaded.getName()).isEqualTo(profile.getName());
73   }
74
75   @Test
76   public void getProfile_throws_NotFoundException_if_specified_name_does_not_exist() {
77     QProfileDto profile = QualityProfileTesting.newQualityProfileDto();
78     db.qualityProfiles().insert(profile);
79
80     assertThatThrownBy(() -> underTest.getProfile(db.getSession(), QProfileReference.fromName("java", "missing")))
81       .isInstanceOf(NotFoundException.class)
82       .hasMessage("Quality Profile for language 'java' and name 'missing' does not exist");
83   }
84
85   @Test
86   public void getRule_throws_BadRequest_if_rule_is_external() {
87     RuleDefinitionDto rule = db.rules().insert(r -> r.setIsExternal(true));
88
89     assertThatThrownBy(() -> underTest.getRule(db.getSession(), rule.getKey()))
90       .isInstanceOf(BadRequestException.class)
91       .hasMessage(format("Operation forbidden for rule '%s' imported from an external rule engine.", rule.getKey()));
92   }
93 }