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.builtin;
22 import java.util.List;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.resources.Language;
26 import org.sonar.api.resources.Languages;
27 import org.sonar.api.rule.RuleKey;
28 import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.rule.RuleDefinitionDto;
32 import org.sonar.server.language.LanguageTesting;
33 import org.sonar.server.qualityprofile.builtin.BuiltInQProfile.ActiveRule;
34 import org.sonar.server.rule.DefaultRuleFinder;
35 import org.sonar.server.rule.ServerRuleFinder;
37 import static java.util.Arrays.asList;
38 import static java.util.Collections.emptyList;
39 import static java.util.Collections.singletonList;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.assertj.core.api.Assertions.assertThatThrownBy;
42 import static org.assertj.core.groups.Tuple.tuple;
43 import static org.mockito.Mockito.mock;
44 import static org.sonar.db.rule.RuleTesting.EXTERNAL_XOO;
47 public class BuiltInQProfileRepositoryImplTest {
48 private static final Language FOO_LANGUAGE = LanguageTesting.newLanguage("foo", "foo", "foo");
49 private static final String SONAR_WAY_QP_NAME = "Sonar way";
52 public DbTester db = DbTester.create();
54 private final DbClient dbClient = db.getDbClient();
55 private final ServerRuleFinder ruleFinder = new DefaultRuleFinder(dbClient);
58 public void create_qprofile_with_rule() {
59 RuleDefinitionDto rule1 = db.rules().insert();
60 RuleDefinitionDto rule2 = db.rules().insert();
62 DummyProfileDefinition definition = new DummyProfileDefinition("foo", "foo", false, asList(rule1.getKey(), rule2.getKey()));
63 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(dbClient, ruleFinder, new Languages(FOO_LANGUAGE), definition);
65 underTest.initialize();
67 assertThat(underTest.get())
68 .extracting(BuiltInQProfile::getName)
69 .containsExactlyInAnyOrder("foo");
70 assertThat(underTest.get().get(0).getActiveRules())
71 .extracting(ActiveRule::getRuleUuid, ActiveRule::getRuleKey)
72 .containsExactlyInAnyOrder(
73 tuple(rule1.getUuid(), rule1.getKey()),
74 tuple(rule2.getUuid(), rule2.getKey()));
78 public void make_single_profile_of_a_language_default_even_if_not_flagged_as_so() {
79 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(dbClient, ruleFinder, new Languages(FOO_LANGUAGE),
80 new DummyProfileDefinition("foo", "foo1", false));
82 underTest.initialize();
84 assertThat(underTest.get())
85 .extracting(BuiltInQProfile::getLanguage, BuiltInQProfile::isDefault)
86 .containsExactly(tuple(FOO_LANGUAGE.getKey(), true));
90 public void make_single_profile_of_a_language_default_even_if_flagged_as_so() {
91 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(dbClient, ruleFinder, new Languages(FOO_LANGUAGE),
92 new DummyProfileDefinition("foo", "foo1", true));
94 underTest.initialize();
96 assertThat(underTest.get())
97 .extracting(BuiltInQProfile::getLanguage, BuiltInQProfile::isDefault)
98 .containsExactly(tuple(FOO_LANGUAGE.getKey(), true));
102 public void make_first_profile_of_a_language_default_when_none_flagged_as_so() {
103 DummyProfileDefinition[] definitions = new DummyProfileDefinition[] {new DummyProfileDefinition("foo", "foo1", false), new DummyProfileDefinition("foo", "foo2", false)};
104 String firstName = definitions[0].getName();
105 String secondName = definitions[1].getName();
106 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(dbClient, ruleFinder, new Languages(FOO_LANGUAGE), definitions);
108 underTest.initialize();
110 assertThat(underTest.get())
111 .extracting(BuiltInQProfile::getName, BuiltInQProfile::isDefault)
112 .containsExactlyInAnyOrder(tuple(firstName, true), tuple(secondName, false));
116 public void create_profile_Sonar_Way_as_default_if_none_other_is_defined_default_for_a_given_language() {
117 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(
118 dbClient, ruleFinder, new Languages(FOO_LANGUAGE),
119 new DummyProfileDefinition("foo", "doh", false), new DummyProfileDefinition("foo", "boo", false),
120 new DummyProfileDefinition("foo", SONAR_WAY_QP_NAME, false), new DummyProfileDefinition("foo", "goo", false));
122 underTest.initialize();
124 assertThat(underTest.get())
125 .filteredOn(b -> FOO_LANGUAGE.getKey().equals(b.getLanguage()))
126 .filteredOn(BuiltInQProfile::isDefault)
127 .extracting(BuiltInQProfile::getName)
128 .containsExactly(SONAR_WAY_QP_NAME);
132 public void do_not_create_Sonar_Way_as_default_if_other_profile_is_defined_as_default() {
133 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(
134 dbClient, ruleFinder, new Languages(FOO_LANGUAGE),
135 new DummyProfileDefinition("foo", SONAR_WAY_QP_NAME, false), new DummyProfileDefinition("foo", "goo", true));
137 underTest.initialize();
139 assertThat(underTest.get())
140 .filteredOn(b -> FOO_LANGUAGE.getKey().equals(b.getLanguage()))
141 .filteredOn(BuiltInQProfile::isDefault)
142 .extracting(BuiltInQProfile::getName)
143 .containsExactly("goo");
147 public void match_Sonar_Way_default_with_case_sensitivity() {
148 String sonarWayInOtherCase = SONAR_WAY_QP_NAME.toUpperCase();
149 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(
150 dbClient, ruleFinder, new Languages(FOO_LANGUAGE),
151 new DummyProfileDefinition("foo", "goo", false), new DummyProfileDefinition("foo", sonarWayInOtherCase, false));
153 underTest.initialize();
155 assertThat(underTest.get())
156 .filteredOn(b -> FOO_LANGUAGE.getKey().equals(b.getLanguage()))
157 .filteredOn(BuiltInQProfile::isDefault)
158 .extracting(BuiltInQProfile::getName)
159 .containsExactly("goo");
163 public void create_no_BuiltInQProfile_when_all_definitions_apply_to_non_defined_languages() {
164 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(mock(DbClient.class), ruleFinder, new Languages(),
165 new DummyProfileDefinition("foo", "P1", false));
167 underTest.initialize();
169 assertThat(underTest.get()).isEmpty();
173 public void create_qprofile_with_deprecated_rule() {
174 RuleDefinitionDto rule1 = db.rules().insert();
175 db.rules().insertDeprecatedKey(d -> d.setRuleUuid(rule1.getUuid()).setOldRepositoryKey("oldRepo").setOldRuleKey("oldKey"));
176 RuleDefinitionDto rule2 = db.rules().insert();
177 DummyProfileDefinition definition = new DummyProfileDefinition("foo", "foo", false,
178 asList(RuleKey.of("oldRepo", "oldKey"), rule2.getKey()));
179 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(dbClient, ruleFinder, new Languages(FOO_LANGUAGE), definition);
181 underTest.initialize();
183 assertThat(underTest.get())
184 .extracting(BuiltInQProfile::getName)
185 .containsExactlyInAnyOrder("foo");
186 assertThat(underTest.get().get(0).getActiveRules())
187 .extracting(ActiveRule::getRuleUuid, ActiveRule::getRuleKey)
188 .containsExactlyInAnyOrder(
189 tuple(rule1.getUuid(), rule1.getKey()),
190 tuple(rule2.getUuid(), rule2.getKey()));
194 public void fail_with_ISE_when_rule_does_not_exist() {
195 DummyProfileDefinition[] definitions = new DummyProfileDefinition[] {new DummyProfileDefinition("foo", "foo", false, singletonList(EXTERNAL_XOO))};
196 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(dbClient, ruleFinder, new Languages(FOO_LANGUAGE), definitions);
198 assertThatThrownBy(underTest::initialize)
199 .isInstanceOf(IllegalStateException.class)
200 .hasMessage(String.format("Rule with key '%s' not found", EXTERNAL_XOO));
204 public void fail_with_ISE_when_two_profiles_with_different_name_are_default_for_the_same_language() {
205 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(dbClient, ruleFinder, new Languages(FOO_LANGUAGE),
206 new DummyProfileDefinition("foo", "foo1", true), new DummyProfileDefinition("foo", "foo2", true));
208 assertThatThrownBy(underTest::initialize)
209 .isInstanceOf(IllegalStateException.class)
210 .hasMessage("Several Quality profiles are flagged as default for the language foo: [foo1, foo2]");
214 public void get_throws_ISE_if_called_before_initialize() {
215 BuiltInQProfileRepositoryImpl underTest = new BuiltInQProfileRepositoryImpl(mock(DbClient.class), ruleFinder, new Languages());
217 assertThatThrownBy(underTest::get)
218 .isInstanceOf(IllegalStateException.class)
219 .hasMessage("initialize must be called first");
223 public void initialize_throws_ISE_if_called_twice() {
224 BuiltInQProfileRepositoryImpl underTest = new BuiltInQProfileRepositoryImpl(mock(DbClient.class), ruleFinder, new Languages());
225 underTest.initialize();
227 assertThatThrownBy(underTest::initialize)
228 .isInstanceOf(IllegalStateException.class)
229 .hasMessage("initialize must be called only once");
233 public void initialize_throws_ISE_if_language_has_no_builtin_qp() {
234 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(mock(DbClient.class), ruleFinder, new Languages(FOO_LANGUAGE));
236 assertThatThrownBy(underTest::initialize)
237 .isInstanceOf(IllegalStateException.class)
238 .hasMessage("The following languages have no built-in quality profiles: foo");
241 private static final class DummyProfileDefinition implements BuiltInQualityProfilesDefinition {
242 private final String language;
243 private final String name;
244 private final boolean defaultProfile;
245 private final List<RuleKey> activeRuleKeys;
247 private DummyProfileDefinition(String language, String name, boolean defaultProfile, List<RuleKey> activeRuleKeys) {
248 this.language = language;
250 this.defaultProfile = defaultProfile;
251 this.activeRuleKeys = activeRuleKeys;
254 private DummyProfileDefinition(String language, String name, boolean defaultProfile) {
255 this(language, name, defaultProfile, emptyList());
259 public void define(Context context) {
260 NewBuiltInQualityProfile builtInQualityProfile = context.createBuiltInQualityProfile(name, language);
261 activeRuleKeys.forEach(activeRuleKey -> builtInQualityProfile.activateRule(activeRuleKey.repository(), activeRuleKey.rule()));
262 builtInQualityProfile.setDefault(defaultProfile);
263 builtInQualityProfile.done();