]> source.dussan.org Git - sonarqube.git/blob
4eb4ab9bc5ea3f65a40e24660906eb8d68b758bf
[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.builtin;
21
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.RuleDto;
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;
36
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;
45
46
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";
50
51   @Rule
52   public DbTester db = DbTester.create();
53
54   private final DbClient dbClient = db.getDbClient();
55   private final ServerRuleFinder ruleFinder = new DefaultRuleFinder(dbClient);
56
57   @Test
58   public void create_qprofile_with_rule() {
59     RuleDto rule1 = db.rules().insert();
60     RuleDto rule2 = db.rules().insert();
61     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);
64
65     underTest.initialize();
66
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()));
75   }
76
77   @Test
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));
81
82     underTest.initialize();
83
84     assertThat(underTest.get())
85       .extracting(BuiltInQProfile::getLanguage, BuiltInQProfile::isDefault)
86       .containsExactly(tuple(FOO_LANGUAGE.getKey(), true));
87   }
88
89   @Test
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));
93
94     underTest.initialize();
95
96     assertThat(underTest.get())
97       .extracting(BuiltInQProfile::getLanguage, BuiltInQProfile::isDefault)
98       .containsExactly(tuple(FOO_LANGUAGE.getKey(), true));
99   }
100
101   @Test
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);
107
108     underTest.initialize();
109
110     assertThat(underTest.get())
111       .extracting(BuiltInQProfile::getName, BuiltInQProfile::isDefault)
112       .containsExactlyInAnyOrder(tuple(firstName, true), tuple(secondName, false));
113   }
114
115   @Test
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));
121
122     underTest.initialize();
123
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);
129   }
130
131   @Test
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));
136
137     underTest.initialize();
138
139     assertThat(underTest.get())
140       .filteredOn(b -> FOO_LANGUAGE.getKey().equals(b.getLanguage()))
141       .filteredOn(BuiltInQProfile::isDefault)
142       .extracting(BuiltInQProfile::getName)
143       .containsExactly("goo");
144   }
145
146   @Test
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));
152
153     underTest.initialize();
154
155     assertThat(underTest.get())
156       .filteredOn(b -> FOO_LANGUAGE.getKey().equals(b.getLanguage()))
157       .filteredOn(BuiltInQProfile::isDefault)
158       .extracting(BuiltInQProfile::getName)
159       .containsExactly("goo");
160   }
161
162   @Test
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));
166
167     underTest.initialize();
168
169     assertThat(underTest.get()).isEmpty();
170   }
171
172   @Test
173   public void create_qprofile_with_deprecated_rule() {
174     RuleDto rule1 = db.rules().insert();
175     db.rules().insertDeprecatedKey(d -> d.setRuleUuid(rule1.getUuid()).setOldRepositoryKey("oldRepo").setOldRuleKey("oldKey"));
176     RuleDto 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);
180
181     underTest.initialize();
182
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()));
191   }
192
193   @Test
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);
197
198     assertThatThrownBy(underTest::initialize)
199       .isInstanceOf(IllegalStateException.class)
200       .hasMessage(String.format("Rule with key '%s' not found", EXTERNAL_XOO));
201   }
202
203   @Test
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));
207
208     assertThatThrownBy(underTest::initialize)
209       .isInstanceOf(IllegalStateException.class)
210       .hasMessage("Several Quality profiles are flagged as default for the language foo: [foo1, foo2]");
211   }
212
213   @Test
214   public void get_throws_ISE_if_called_before_initialize() {
215     BuiltInQProfileRepositoryImpl underTest = new BuiltInQProfileRepositoryImpl(mock(DbClient.class), ruleFinder, new Languages());
216
217     assertThatThrownBy(underTest::get)
218       .isInstanceOf(IllegalStateException.class)
219       .hasMessage("initialize must be called first");
220   }
221
222   @Test
223   public void initialize_throws_ISE_if_called_twice() {
224     BuiltInQProfileRepositoryImpl underTest = new BuiltInQProfileRepositoryImpl(mock(DbClient.class), ruleFinder, new Languages());
225     underTest.initialize();
226
227     assertThatThrownBy(underTest::initialize)
228       .isInstanceOf(IllegalStateException.class)
229       .hasMessage("initialize must be called only once");
230   }
231
232   @Test
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));
235
236     assertThatThrownBy(underTest::initialize)
237       .isInstanceOf(IllegalStateException.class)
238       .hasMessage("The following languages have no built-in quality profiles: foo");
239   }
240
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;
246
247     private DummyProfileDefinition(String language, String name, boolean defaultProfile, List<RuleKey> activeRuleKeys) {
248       this.language = language;
249       this.name = name;
250       this.defaultProfile = defaultProfile;
251       this.activeRuleKeys = activeRuleKeys;
252     }
253
254     private DummyProfileDefinition(String language, String name, boolean defaultProfile) {
255       this(language, name, defaultProfile, emptyList());
256     }
257
258     @Override
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();
264     }
265
266     String getName() {
267       return name;
268     }
269
270   }
271
272 }