3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.qualityprofile.ws;
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;
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;
36 public class QProfileWsSupportTest {
39 public DbTester db = DbTester.create();
41 public UserSessionRule userSession = UserSessionRule.standalone();
43 private final QProfileWsSupport underTest = new QProfileWsSupport(db.getDbClient(), userSession);
46 public void getProfile_returns_the_profile_specified_by_key() {
47 QProfileDto profile = db.qualityProfiles().insert();
49 QProfileDto loaded = underTest.getProfile(db.getSession(), QProfileReference.fromKey(profile.getKee()));
51 assertThat(loaded.getKee()).isEqualTo(profile.getKee());
52 assertThat(loaded.getLanguage()).isEqualTo(profile.getLanguage());
53 assertThat(loaded.getName()).isEqualTo(profile.getName());
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");
64 public void getProfile_returns_the_profile_specified_by_name() {
65 QProfileDto profile = QualityProfileTesting.newQualityProfileDto();
66 db.qualityProfiles().insert(profile);
68 QProfileDto loaded = underTest.getProfile(db.getSession(), QProfileReference.fromName(profile.getLanguage(), profile.getName()));
70 assertThat(loaded.getKee()).isEqualTo(profile.getKee());
71 assertThat(loaded.getLanguage()).isEqualTo(profile.getLanguage());
72 assertThat(loaded.getName()).isEqualTo(profile.getName());
76 public void getProfile_throws_NotFoundException_if_specified_name_does_not_exist() {
77 QProfileDto profile = QualityProfileTesting.newQualityProfileDto();
78 db.qualityProfiles().insert(profile);
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");
86 public void getRule_throws_BadRequest_if_rule_is_external() {
87 RuleDefinitionDto rule = db.rules().insert(r -> r.setIsExternal(true));
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()));