]> source.dussan.org Git - sonarqube.git/blob
70e8382e096972877a38030af1bcf1719d4d680a
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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;
21
22 import java.util.Map;
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;
47
48 import static com.google.common.collect.Lists.newArrayList;
49 import static org.assertj.core.api.Assertions.assertThat;
50
51 public class RegisterQualityProfilesMediumTest {
52
53   ServerTester tester;
54   DbSession dbSession;
55
56   @After
57   public void tearDown() {
58     if (dbSession != null) {
59       dbSession.close();
60     }
61     if (tester != null) {
62       tester.stop();
63     }
64   }
65
66   @Test
67   public void register_existing_profile_definitions() {
68     tester = new ServerTester().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
69     tester.start();
70     dbSession = dbClient().openSession(false);
71
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();
77
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);
83
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();
90
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);
96
97     // TODO
98     // Check ActiveRuleParameters in DB
99     Map<String, ActiveRuleParamDto> params =
100       ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleKey(dbSession, activeRule.key()));
101     assertThat(params).hasSize(2);
102     // set by profile
103     assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
104     // default value
105     assertThat(params.get("max").getValue()).isEqualTo("10");
106
107   }
108
109   @Test
110   public void register_profile_definitions() {
111     tester = new ServerTester().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
112     tester.start();
113     dbSession = dbClient().openSession(false);
114
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();
120
121     // Check Default Profile
122     verifyDefaultProfile("xoo", "Basic");
123
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");
128
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);
133
134     // Check ActiveRuleParameters in DB
135     Map<String, ActiveRuleParamDto> params =
136       ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleKey(dbSession, activeRule.getKey()));
137     assertThat(params).hasSize(2);
138     // set by profile
139     assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
140     // default value
141     assertThat(params.get("max").getValue()).isEqualTo("10");
142   }
143
144   @Test
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);
148     tester.start();
149     dbSession = dbClient().openSession(false);
150
151     // Check Profile in DB
152     QualityProfileDao qualityProfileDao = dbClient().qualityProfileDao();
153     assertThat(qualityProfileDao.selectAll(dbSession)).hasSize(0);
154   }
155
156   @Test
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));
159
160     try {
161       tester.start();
162     } catch (IllegalStateException e) {
163       assertThat(e).hasMessage("Several Quality profiles are flagged as default for the language xoo: [one, two]");
164     }
165   }
166
167   @Test
168   public void mark_profile_as_default() {
169     tester = new ServerTester().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("one", false), new SimpleProfileDefinition("two", true));
170
171     tester.start();
172     verifyDefaultProfile("xoo", "two");
173   }
174
175   @Test
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));
178
179     tester.start();
180     verifyDefaultProfile("xoo", "Sonar way");
181   }
182
183   @Test
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));
186     tester.start();
187
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());
192     session.commit();
193
194     verifyDefaultProfile("xoo", "two");
195
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");
199   }
200
201   /**
202    * Probably for db migration
203    */
204   @Test
205   public void clean_up_profiles_if_missing_loaded_template() {
206     tester = new ServerTester().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
207     tester.start();
208
209     dbSession = dbClient().openSession(false);
210     String templateKey = RegisterQualityProfiles.templateKey(new QProfileName("xoo", "Basic"));
211     dbClient().loadedTemplateDao().delete(dbSession, LoadedTemplateDto.QUALITY_PROFILE_TYPE, templateKey);
212     dbSession.commit();
213     assertThat(dbClient().loadedTemplateDao().countByTypeAndKey(LoadedTemplateDto.QUALITY_PROFILE_TYPE, templateKey, dbSession)).isEqualTo(0);
214     dbSession.close();
215
216     tester.get(Platform.class).restart();
217
218     // do not fail
219   }
220
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);
225   }
226
227   private DbClient dbClient() {
228     return tester.get(DbClient.class);
229   }
230
231   public static class XooProfileDefinition extends ProfileDefinition {
232     @Override
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");
239
240       profile.activateRule(org.sonar.api.rules.Rule.create("xoo", "x2"), RulePriority.INFO);
241       return profile;
242     }
243   }
244
245   public static class XooRulesDefinition implements RulesDefinition {
246     @Override
247     public void define(Context context) {
248       NewRepository repository = context.createRepository("xoo", ServerTester.Xoo.KEY).setName("Xoo Repo");
249       NewRule x1 = repository.createRule("x1")
250         .setName("x1 name")
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");
261
262       repository.createRule("x2")
263         .setName("x2 name")
264         .setHtmlDescription("x2 desc")
265         .setSeverity(Severity.INFO);
266       repository.done();
267     }
268   }
269
270   public static class SimpleProfileDefinition extends ProfileDefinition {
271     private final boolean asDefault;
272     private final String name;
273
274     public SimpleProfileDefinition(String name, boolean asDefault) {
275       this.name = name;
276       this.asDefault = asDefault;
277     }
278
279     @Override
280     public RulesProfile createProfile(ValidationMessages validation) {
281       RulesProfile profile = RulesProfile.create(name, "xoo");
282       profile.setDefaultProfile(asDefault);
283       return profile;
284     }
285   }
286 }