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.DbSession;
36 import org.sonar.db.qualityprofile.ActiveRuleDto;
37 import org.sonar.db.qualityprofile.ActiveRuleKey;
38 import org.sonar.db.qualityprofile.ActiveRuleParamDto;
39 import org.sonar.db.qualityprofile.QualityProfileDao;
40 import org.sonar.db.qualityprofile.QualityProfileDto;
41 import org.sonar.db.loadedtemplate.LoadedTemplateDto;
42 import org.sonar.server.db.DbClient;
43 import org.sonar.server.platform.Platform;
44 import org.sonar.server.qualityprofile.db.ActiveRuleDao;
45 import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
46 import org.sonar.server.tester.ServerTester;
48 import static com.google.common.collect.Lists.newArrayList;
49 import static org.assertj.core.api.Assertions.assertThat;
51 public class RegisterQualityProfilesMediumTest {
57 public void tearDown() {
58 if (dbSession != null) {
67 public void register_existing_profile_definitions() {
68 tester = new ServerTester().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
70 dbSession = dbClient().openSession(false);
72 // Check Profile in DB
73 QualityProfileDao qualityProfileDao = dbClient().qualityProfileDao();
74 assertThat(qualityProfileDao.selectAll(dbSession)).hasSize(1);
75 QualityProfileDto profile = qualityProfileDao.selectByNameAndLanguage("Basic", "xoo", dbSession);
76 assertThat(profile).isNotNull();
78 // Check ActiveRules in DB
79 ActiveRuleDao activeRuleDao = dbClient().activeRuleDao();
80 assertThat(activeRuleDao.selectByProfileKey(dbSession, profile.getKey())).hasSize(2);
81 RuleKey ruleKey = RuleKey.of("xoo", "x1");
82 ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), ruleKey);
84 // 0. Check and clear ES
85 assertThat(tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey)).isNotNull();
86 tester.clearIndexes();
87 assertThat(tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey)).isNull();
88 tester.get(Platform.class).restart();
89 assertThat(tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey)).isNotNull();
91 // Check ActiveRules in ES
92 org.sonar.server.qualityprofile.ActiveRule activeRule = tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey);
93 assertThat(activeRule.key().qProfile()).isEqualTo(profile.getKee());
94 assertThat(activeRule.key().ruleKey()).isEqualTo(ruleKey);
95 assertThat(activeRule.severity()).isEqualTo(Severity.CRITICAL);
98 // Check ActiveRuleParameters in DB
99 Map<String, ActiveRuleParamDto> params =
100 ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleKey(dbSession, activeRule.key()));
101 assertThat(params).hasSize(2);
103 assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
105 assertThat(params.get("max").getValue()).isEqualTo("10");
110 public void register_profile_definitions() {
111 tester = new ServerTester().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
113 dbSession = dbClient().openSession(false);
115 // Check Profile in DB
116 QualityProfileDao qualityProfileDao = dbClient().qualityProfileDao();
117 assertThat(qualityProfileDao.selectAll(dbSession)).hasSize(1);
118 QualityProfileDto profile = qualityProfileDao.selectByNameAndLanguage("Basic", "xoo", dbSession);
119 assertThat(profile).isNotNull();
121 // Check Default Profile
122 verifyDefaultProfile("xoo", "Basic");
124 // Check ActiveRules in DB
125 ActiveRuleDao activeRuleDao = dbClient().activeRuleDao();
126 assertThat(activeRuleDao.selectByProfileKey(dbSession, profile.getKey())).hasSize(2);
127 RuleKey ruleKey = RuleKey.of("xoo", "x1");
129 ActiveRuleDto activeRule = activeRuleDao.getNullableByKey(dbSession, ActiveRuleKey.of(profile.getKey(), ruleKey));
130 assertThat(activeRule.getKey().qProfile()).isEqualTo(profile.getKey());
131 assertThat(activeRule.getKey().ruleKey()).isEqualTo(ruleKey);
132 assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
134 // Check ActiveRuleParameters in DB
135 Map<String, ActiveRuleParamDto> params =
136 ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleKey(dbSession, activeRule.getKey()));
137 assertThat(params).hasSize(2);
139 assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
141 assertThat(params.get("max").getValue()).isEqualTo("10");
145 public void do_not_register_profile_if_missing_language() {
146 // xoo language is not installed
147 tester = new ServerTester().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
149 dbSession = dbClient().openSession(false);
151 // Check Profile in DB
152 QualityProfileDao qualityProfileDao = dbClient().qualityProfileDao();
153 assertThat(qualityProfileDao.selectAll(dbSession)).hasSize(0);
157 public void fail_if_two_definitions_are_marked_as_default_on_the_same_language() {
158 tester = new ServerTester().addXoo().addComponents(new SimpleProfileDefinition("one", true), new SimpleProfileDefinition("two", true));
162 } catch (IllegalStateException e) {
163 assertThat(e).hasMessage("Several Quality profiles are flagged as default for the language xoo: [one, two]");
168 public void mark_profile_as_default() {
169 tester = new ServerTester().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("one", false), new SimpleProfileDefinition("two", true));
172 verifyDefaultProfile("xoo", "two");
176 public void use_sonar_way_as_default_profile_if_none_are_marked_as_default() {
177 tester = new ServerTester().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("Sonar way", false), new SimpleProfileDefinition("Other way", false));
180 verifyDefaultProfile("xoo", "Sonar way");
184 public void do_not_reset_default_profile_if_still_valid() {
185 tester = new ServerTester().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("one", true), new SimpleProfileDefinition("two", false));
188 QualityProfileDao profileDao = dbClient().qualityProfileDao();
189 DbSession session = dbClient().openSession(false);
190 QualityProfileDto profileTwo = profileDao.selectByNameAndLanguage("two", "xoo", session);
191 tester.get(QProfileFactory.class).setDefault(session, profileTwo.getKee());
194 verifyDefaultProfile("xoo", "two");
196 tester.get(Platform.class).restart();
197 // restart must keep "two" as default profile, even if "one" is marked as it
198 verifyDefaultProfile("xoo", "two");
202 * Probably for db migration
205 public void clean_up_profiles_if_missing_loaded_template() {
206 tester = new ServerTester().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
209 dbSession = dbClient().openSession(false);
210 String templateKey = RegisterQualityProfiles.templateKey(new QProfileName("xoo", "Basic"));
211 dbClient().loadedTemplateDao().delete(dbSession, LoadedTemplateDto.QUALITY_PROFILE_TYPE, templateKey);
213 assertThat(dbClient().loadedTemplateDao().countByTypeAndKey(LoadedTemplateDto.QUALITY_PROFILE_TYPE, templateKey, dbSession)).isEqualTo(0);
216 tester.get(Platform.class).restart();
221 private void verifyDefaultProfile(String language, String name) {
222 QualityProfileDto defaultProfile = dbClient().qualityProfileDao().selectDefaultProfile(language);
223 assertThat(defaultProfile).isNotNull();
224 assertThat(defaultProfile.getName()).isEqualTo(name);
227 private DbClient dbClient() {
228 return tester.get(DbClient.class);
231 public static class XooProfileDefinition extends ProfileDefinition {
233 public RulesProfile createProfile(ValidationMessages validation) {
234 final RulesProfile profile = RulesProfile.create("Basic", ServerTester.Xoo.KEY);
235 ActiveRule activeRule1 = profile.activateRule(
236 org.sonar.api.rules.Rule.create("xoo", "x1").setParams(newArrayList(new RuleParam().setKey("acceptWhitespace"))),
237 RulePriority.CRITICAL);
238 activeRule1.setParameter("acceptWhitespace", "true");
240 profile.activateRule(org.sonar.api.rules.Rule.create("xoo", "x2"), RulePriority.INFO);
245 public static class XooRulesDefinition implements RulesDefinition {
247 public void define(Context context) {
248 NewRepository repository = context.createRepository("xoo", ServerTester.Xoo.KEY).setName("Xoo Repo");
249 NewRule x1 = repository.createRule("x1")
251 .setHtmlDescription("x1 desc")
252 .setSeverity(Severity.MINOR);
253 x1.createParam("acceptWhitespace")
254 .setDefaultValue("false")
255 .setType(RuleParamType.BOOLEAN)
256 .setDescription("Accept whitespaces on the line");
257 x1.createParam("max")
258 .setDefaultValue("10")
259 .setType(RuleParamType.INTEGER)
260 .setDescription("Maximum");
262 repository.createRule("x2")
264 .setHtmlDescription("x2 desc")
265 .setSeverity(Severity.INFO);
270 public static class SimpleProfileDefinition extends ProfileDefinition {
271 private final boolean asDefault;
272 private final String name;
274 public SimpleProfileDefinition(String name, boolean asDefault) {
276 this.asDefault = asDefault;
280 public RulesProfile createProfile(ValidationMessages validation) {
281 RulesProfile profile = RulesProfile.create(name, "xoo");
282 profile.setDefaultProfile(asDefault);