3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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;
23 import org.junit.After;
24 import org.junit.Test;
25 import org.sonar.api.profiles.ProfileDefinition;
26 import org.sonar.api.profiles.RulesProfile;
27 import org.sonar.api.rule.RuleKey;
28 import org.sonar.api.rule.Severity;
29 import org.sonar.api.rules.ActiveRule;
30 import org.sonar.api.rules.RuleParam;
31 import org.sonar.api.rules.RulePriority;
32 import org.sonar.api.server.rule.RuleParamType;
33 import org.sonar.api.server.rule.RulesDefinition;
34 import org.sonar.api.utils.ValidationMessages;
35 import org.sonar.db.DbClient;
36 import org.sonar.db.DbSession;
37 import org.sonar.db.loadedtemplate.LoadedTemplateDto;
38 import org.sonar.db.qualityprofile.ActiveRuleDao;
39 import org.sonar.db.qualityprofile.ActiveRuleDto;
40 import org.sonar.db.qualityprofile.ActiveRuleKey;
41 import org.sonar.db.qualityprofile.ActiveRuleParamDto;
42 import org.sonar.db.qualityprofile.QualityProfileDao;
43 import org.sonar.db.qualityprofile.QualityProfileDto;
44 import org.sonar.server.es.SearchOptions;
45 import org.sonar.server.platform.Platform;
46 import org.sonar.server.rule.index.RuleIndex2;
47 import org.sonar.server.rule.index.RuleQuery;
48 import org.sonar.server.tester.ServerTester;
50 import static com.google.common.collect.Lists.newArrayList;
51 import static org.assertj.core.api.Assertions.assertThat;
52 import static org.assertj.guava.api.Assertions.assertThat;
54 public class RegisterQualityProfilesMediumTest {
60 public void tearDown() {
61 if (dbSession != null) {
70 public void register_existing_profile_definitions() {
71 tester = new ServerTester().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
73 dbSession = dbClient().openSession(false);
75 // Check Profile in DB
76 QualityProfileDao qualityProfileDao = dbClient().qualityProfileDao();
77 assertThat(qualityProfileDao.selectAll(dbSession)).hasSize(1);
78 QualityProfileDto profile = qualityProfileDao.selectByNameAndLanguage("Basic", "xoo", dbSession);
79 assertThat(profile).isNotNull();
81 // Check ActiveRules in DB
82 ActiveRuleDao activeRuleDao = dbClient().activeRuleDao();
83 assertThat(activeRuleDao.selectByProfileKey(dbSession, profile.getKey())).hasSize(2);
85 RuleKey ruleKey = RuleKey.of("xoo", "x1");
86 ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), ruleKey);
87 assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();
90 assertThat(tester.get(RuleIndex2.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey);
92 tester.get(Platform.class).restart();
94 assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();
97 ActiveRuleDto activeRule = activeRuleDao.selectByKey(dbSession, activeRuleKey).get();
98 assertThat(activeRule.getKey().qProfile()).isEqualTo(profile.getKee());
99 assertThat(activeRule.getKey().ruleKey()).isEqualTo(ruleKey);
100 assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
103 assertThat(tester.get(RuleIndex2.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey);
106 // Check ActiveRuleParameters in DB
107 Map<String, ActiveRuleParamDto> params =
108 ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleKey(dbSession, activeRule.getKey()));
109 assertThat(params).hasSize(2);
111 assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
113 assertThat(params.get("max").getValue()).isEqualTo("10");
117 public void register_profile_definitions() {
118 tester = new ServerTester().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
120 dbSession = dbClient().openSession(false);
122 // Check Profile in DB
123 QualityProfileDao qualityProfileDao = dbClient().qualityProfileDao();
124 assertThat(qualityProfileDao.selectAll(dbSession)).hasSize(1);
125 QualityProfileDto profile = qualityProfileDao.selectByNameAndLanguage("Basic", "xoo", dbSession);
126 assertThat(profile).isNotNull();
128 // Check Default Profile
129 verifyDefaultProfile("xoo", "Basic");
131 // Check ActiveRules in DB
132 ActiveRuleDao activeRuleDao = dbClient().activeRuleDao();
133 assertThat(activeRuleDao.selectByProfileKey(dbSession, profile.getKey())).hasSize(2);
134 RuleKey ruleKey = RuleKey.of("xoo", "x1");
136 ActiveRuleDto activeRule = activeRuleDao.selectByKey(dbSession, ActiveRuleKey.of(profile.getKey(), ruleKey)).get();
137 assertThat(activeRule.getKey().qProfile()).isEqualTo(profile.getKey());
138 assertThat(activeRule.getKey().ruleKey()).isEqualTo(ruleKey);
139 assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
141 // Check ActiveRuleParameters in DB
142 Map<String, ActiveRuleParamDto> params =
143 ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleKey(dbSession, activeRule.getKey()));
144 assertThat(params).hasSize(2);
146 assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
148 assertThat(params.get("max").getValue()).isEqualTo("10");
152 public void do_not_register_profile_if_missing_language() {
153 // xoo language is not installed
154 tester = new ServerTester().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
156 dbSession = dbClient().openSession(false);
158 // Check Profile in DB
159 QualityProfileDao qualityProfileDao = dbClient().qualityProfileDao();
160 assertThat(qualityProfileDao.selectAll(dbSession)).hasSize(0);
164 public void fail_if_two_definitions_are_marked_as_default_on_the_same_language() {
165 tester = new ServerTester().addXoo().addComponents(new SimpleProfileDefinition("one", true), new SimpleProfileDefinition("two", true));
169 } catch (IllegalStateException e) {
170 assertThat(e).hasMessage("Several Quality profiles are flagged as default for the language xoo: [one, two]");
175 public void mark_profile_as_default() {
176 tester = new ServerTester().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("one", false), new SimpleProfileDefinition("two", true));
179 verifyDefaultProfile("xoo", "two");
183 public void use_sonar_way_as_default_profile_if_none_are_marked_as_default() {
184 tester = new ServerTester().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("Sonar way", false), new SimpleProfileDefinition("Other way", false));
187 verifyDefaultProfile("xoo", "Sonar way");
191 public void do_not_reset_default_profile_if_still_valid() {
192 tester = new ServerTester().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("one", true), new SimpleProfileDefinition("two", false));
195 QualityProfileDao profileDao = dbClient().qualityProfileDao();
196 DbSession session = dbClient().openSession(false);
197 QualityProfileDto profileTwo = profileDao.selectByNameAndLanguage("two", "xoo", session);
198 tester.get(QProfileFactory.class).setDefault(session, profileTwo.getKee());
201 verifyDefaultProfile("xoo", "two");
203 tester.get(Platform.class).restart();
204 // restart must keep "two" as default profile, even if "one" is marked as it
205 verifyDefaultProfile("xoo", "two");
209 * Probably for db migration
212 public void clean_up_profiles_if_missing_loaded_template() {
213 tester = new ServerTester().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
216 dbSession = dbClient().openSession(false);
217 String templateKey = RegisterQualityProfiles.templateKey(new QProfileName("xoo", "Basic"));
218 dbClient().loadedTemplateDao().delete(dbSession, LoadedTemplateDto.QUALITY_PROFILE_TYPE, templateKey);
220 assertThat(dbClient().loadedTemplateDao().countByTypeAndKey(LoadedTemplateDto.QUALITY_PROFILE_TYPE, templateKey, dbSession)).isEqualTo(0);
223 tester.get(Platform.class).restart();
228 private void verifyDefaultProfile(String language, String name) {
229 QualityProfileDto defaultProfile = dbClient().qualityProfileDao().selectDefaultProfile(language);
230 assertThat(defaultProfile).isNotNull();
231 assertThat(defaultProfile.getName()).isEqualTo(name);
234 private DbClient dbClient() {
235 return tester.get(DbClient.class);
238 public static class XooProfileDefinition extends ProfileDefinition {
240 public RulesProfile createProfile(ValidationMessages validation) {
241 final RulesProfile profile = RulesProfile.create("Basic", ServerTester.Xoo.KEY);
242 ActiveRule activeRule1 = profile.activateRule(
243 org.sonar.api.rules.Rule.create("xoo", "x1").setParams(newArrayList(new RuleParam().setKey("acceptWhitespace"))),
244 RulePriority.CRITICAL);
245 activeRule1.setParameter("acceptWhitespace", "true");
247 profile.activateRule(org.sonar.api.rules.Rule.create("xoo", "x2"), RulePriority.INFO);
252 public static class XooRulesDefinition implements RulesDefinition {
254 public void define(Context context) {
255 NewRepository repository = context.createRepository("xoo", ServerTester.Xoo.KEY).setName("Xoo Repo");
256 NewRule x1 = repository.createRule("x1")
258 .setHtmlDescription("x1 desc")
259 .setSeverity(Severity.MINOR);
260 x1.createParam("acceptWhitespace")
261 .setDefaultValue("false")
262 .setType(RuleParamType.BOOLEAN)
263 .setDescription("Accept whitespaces on the line");
264 x1.createParam("max")
265 .setDefaultValue("10")
266 .setType(RuleParamType.INTEGER)
267 .setDescription("Maximum");
269 repository.createRule("x2")
271 .setHtmlDescription("x2 desc")
272 .setSeverity(Severity.INFO);
277 public static class SimpleProfileDefinition extends ProfileDefinition {
278 private final boolean asDefault;
279 private final String name;
281 public SimpleProfileDefinition(String name, boolean asDefault) {
283 this.asDefault = asDefault;
287 public RulesProfile createProfile(ValidationMessages validation) {
288 RulesProfile profile = RulesProfile.create(name, "xoo");
289 profile.setDefaultProfile(asDefault);