]> source.dussan.org Git - sonarqube.git/blob
1c0ccb8d0158f8267fbc9ed914fa7d09389fbde5
[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.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;
49
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;
53
54 public class RegisterQualityProfilesMediumTest {
55
56   ServerTester tester;
57   DbSession dbSession;
58
59   @After
60   public void tearDown() {
61     if (dbSession != null) {
62       dbSession.close();
63     }
64     if (tester != null) {
65       tester.stop();
66     }
67   }
68
69   @Test
70   public void register_existing_profile_definitions() {
71     tester = new ServerTester().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
72     tester.start();
73     dbSession = dbClient().openSession(false);
74
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();
80
81     // Check ActiveRules in DB
82     ActiveRuleDao activeRuleDao = dbClient().activeRuleDao();
83     assertThat(activeRuleDao.selectByProfileKey(dbSession, profile.getKey())).hasSize(2);
84
85     RuleKey ruleKey = RuleKey.of("xoo", "x1");
86     ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), ruleKey);
87     assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();
88
89     // Check in ES
90     assertThat(tester.get(RuleIndex2.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey);
91
92     tester.get(Platform.class).restart();
93
94     assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();
95
96     // Check ActiveRules
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);
101
102     // Check in ES
103     assertThat(tester.get(RuleIndex2.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey);
104
105     // TODO
106     // Check ActiveRuleParameters in DB
107     Map<String, ActiveRuleParamDto> params =
108       ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleKey(dbSession, activeRule.getKey()));
109     assertThat(params).hasSize(2);
110     // set by profile
111     assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
112     // default value
113     assertThat(params.get("max").getValue()).isEqualTo("10");
114   }
115
116   @Test
117   public void register_profile_definitions() {
118     tester = new ServerTester().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
119     tester.start();
120     dbSession = dbClient().openSession(false);
121
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();
127
128     // Check Default Profile
129     verifyDefaultProfile("xoo", "Basic");
130
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");
135
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);
140
141     // Check ActiveRuleParameters in DB
142     Map<String, ActiveRuleParamDto> params =
143       ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleKey(dbSession, activeRule.getKey()));
144     assertThat(params).hasSize(2);
145     // set by profile
146     assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
147     // default value
148     assertThat(params.get("max").getValue()).isEqualTo("10");
149   }
150
151   @Test
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);
155     tester.start();
156     dbSession = dbClient().openSession(false);
157
158     // Check Profile in DB
159     QualityProfileDao qualityProfileDao = dbClient().qualityProfileDao();
160     assertThat(qualityProfileDao.selectAll(dbSession)).hasSize(0);
161   }
162
163   @Test
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));
166
167     try {
168       tester.start();
169     } catch (IllegalStateException e) {
170       assertThat(e).hasMessage("Several Quality profiles are flagged as default for the language xoo: [one, two]");
171     }
172   }
173
174   @Test
175   public void mark_profile_as_default() {
176     tester = new ServerTester().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("one", false), new SimpleProfileDefinition("two", true));
177
178     tester.start();
179     verifyDefaultProfile("xoo", "two");
180   }
181
182   @Test
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));
185
186     tester.start();
187     verifyDefaultProfile("xoo", "Sonar way");
188   }
189
190   @Test
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));
193     tester.start();
194
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());
199     session.commit();
200
201     verifyDefaultProfile("xoo", "two");
202
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");
206   }
207
208   /**
209    * Probably for db migration
210    */
211   @Test
212   public void clean_up_profiles_if_missing_loaded_template() {
213     tester = new ServerTester().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
214     tester.start();
215
216     dbSession = dbClient().openSession(false);
217     String templateKey = RegisterQualityProfiles.templateKey(new QProfileName("xoo", "Basic"));
218     dbClient().loadedTemplateDao().delete(dbSession, LoadedTemplateDto.QUALITY_PROFILE_TYPE, templateKey);
219     dbSession.commit();
220     assertThat(dbClient().loadedTemplateDao().countByTypeAndKey(LoadedTemplateDto.QUALITY_PROFILE_TYPE, templateKey, dbSession)).isEqualTo(0);
221     dbSession.close();
222
223     tester.get(Platform.class).restart();
224
225     // do not fail
226   }
227
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);
232   }
233
234   private DbClient dbClient() {
235     return tester.get(DbClient.class);
236   }
237
238   public static class XooProfileDefinition extends ProfileDefinition {
239     @Override
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");
246
247       profile.activateRule(org.sonar.api.rules.Rule.create("xoo", "x2"), RulePriority.INFO);
248       return profile;
249     }
250   }
251
252   public static class XooRulesDefinition implements RulesDefinition {
253     @Override
254     public void define(Context context) {
255       NewRepository repository = context.createRepository("xoo", ServerTester.Xoo.KEY).setName("Xoo Repo");
256       NewRule x1 = repository.createRule("x1")
257         .setName("x1 name")
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");
268
269       repository.createRule("x2")
270         .setName("x2 name")
271         .setHtmlDescription("x2 desc")
272         .setSeverity(Severity.INFO);
273       repository.done();
274     }
275   }
276
277   public static class SimpleProfileDefinition extends ProfileDefinition {
278     private final boolean asDefault;
279     private final String name;
280
281     public SimpleProfileDefinition(String name, boolean asDefault) {
282       this.name = name;
283       this.asDefault = asDefault;
284     }
285
286     @Override
287     public RulesProfile createProfile(ValidationMessages validation) {
288       RulesProfile profile = RulesProfile.create(name, "xoo");
289       profile.setDefaultProfile(asDefault);
290       return profile;
291     }
292   }
293 }