]> source.dussan.org Git - sonarqube.git/blob
e51107c878d3fdbb59eb5333c303b8e4b6ec4853
[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.ws;
21
22 import java.util.List;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.ClassRule;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.rule.RuleKey;
29 import org.sonar.api.rule.RuleStatus;
30 import org.sonar.api.rule.Severity;
31 import org.sonar.core.permission.GlobalPermissions;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.RowNotFoundException;
35 import org.sonar.db.qualityprofile.ActiveRuleDto;
36 import org.sonar.db.qualityprofile.QualityProfileDto;
37 import org.sonar.db.rule.RuleDto;
38 import org.sonar.server.es.SearchOptions;
39 import org.sonar.server.exceptions.ForbiddenException;
40 import org.sonar.server.qualityprofile.QProfileName;
41 import org.sonar.server.qualityprofile.QProfileTesting;
42 import org.sonar.server.qualityprofile.RuleActivator;
43 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
44 import org.sonar.server.rule.index.RuleIndex2;
45 import org.sonar.server.rule.index.RuleIndexer;
46 import org.sonar.server.rule.index.RuleQuery;
47 import org.sonar.server.tester.ServerTester;
48 import org.sonar.server.tester.UserSessionRule;
49 import org.sonar.server.ws.WsTester;
50
51 import static org.assertj.core.api.Assertions.assertThat;
52
53 public class ChangeParentActionMediumTest {
54
55   // TODO Replace with DbTester + EsTester once DaoV2 is removed
56   @ClassRule
57   public static ServerTester tester = new ServerTester().withEsIndexes();
58   @Rule
59   public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
60
61   QProfilesWs ws;
62   DbClient db;
63   DbSession session;
64   WsTester wsTester;
65   RuleIndexer ruleIndexer;
66   ActiveRuleIndexer activeRuleIndexer;
67   RuleIndex2 ruleIndex;
68
69   @Before
70   public void setUp() {
71     tester.clearDbAndIndexes();
72     db = tester.get(DbClient.class);
73     ws = tester.get(QProfilesWs.class);
74     wsTester = tester.get(WsTester.class);
75     session = db.openSession(false);
76     ruleIndexer = tester.get(RuleIndexer.class);
77     ruleIndexer.setEnabled(true);
78     activeRuleIndexer = tester.get(ActiveRuleIndexer.class);
79     activeRuleIndexer.setEnabled(true);
80     ruleIndex = tester.get(RuleIndex2.class);
81     userSessionRule.login("gandalf").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
82   }
83
84   @After
85   public void after() {
86     session.close();
87   }
88
89   @Test
90   public void change_parent_with_no_parent_before() throws Exception {
91     QualityProfileDto parent1 = createProfile("xoo", "Parent 1");
92     QualityProfileDto child = createProfile("xoo", "Child");
93
94     RuleDto rule1 = createRule("xoo", "rule1");
95     createActiveRule(rule1, parent1);
96     session.commit();
97     ruleIndexer.index();
98     activeRuleIndexer.index();
99
100     assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
101
102     // Set parent
103     wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
104       .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKey())
105       .setParam("parentKey", parent1.getKey())
106       .execute();
107     session.clearCache();
108
109     // Check rule 1 enabled
110     List<ActiveRuleDto> activeRules1 = db.activeRuleDao().selectByProfileKey(session, child.getKey());
111     assertThat(activeRules1).hasSize(1);
112     assertThat(activeRules1.get(0).getKey().ruleKey().rule()).isEqualTo("rule1");
113
114     assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).hasSize(1);
115   }
116
117   @Test
118   public void replace_existing_parent() throws Exception {
119     QualityProfileDto parent1 = createProfile("xoo", "Parent 1");
120     QualityProfileDto parent2 = createProfile("xoo", "Parent 2");
121     QualityProfileDto child = createProfile("xoo", "Child");
122
123     RuleDto rule1 = createRule("xoo", "rule1");
124     RuleDto rule2 = createRule("xoo", "rule2");
125     createActiveRule(rule1, parent1);
126     createActiveRule(rule2, parent2);
127     session.commit();
128     ruleIndexer.index();
129     activeRuleIndexer.index();
130
131     // Set parent 1
132     tester.get(RuleActivator.class).setParent(child.getKey(), parent1.getKey());
133     session.clearCache();
134
135     // Set parent 2 through WS
136     wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
137       .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKey())
138       .setParam("parentKey", parent2.getKey())
139       .execute();
140     session.clearCache();
141
142     // Check rule 2 enabled
143     List<ActiveRuleDto> activeRules2 = db.activeRuleDao().selectByProfileKey(session, child.getKey());
144     assertThat(activeRules2).hasSize(1);
145     assertThat(activeRules2.get(0).getKey().ruleKey().rule()).isEqualTo("rule2");
146
147     assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).hasSize(1);
148   }
149
150   @Test
151   public void remove_parent() throws Exception {
152     QualityProfileDto parent = createProfile("xoo", "Parent 1");
153     QualityProfileDto child = createProfile("xoo", "Child");
154
155     RuleDto rule1 = createRule("xoo", "rule1");
156     createActiveRule(rule1, parent);
157     session.commit();
158     ruleIndexer.index();
159     activeRuleIndexer.index();
160
161     // Set parent
162     tester.get(RuleActivator.class).setParent(child.getKey(), parent.getKey());
163     session.clearCache();
164
165     // Remove parent through WS
166     wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
167       .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKey())
168       .execute();
169     session.clearCache();
170
171     // Check no rule enabled
172     List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(session, child.getKey());
173     assertThat(activeRules).isEmpty();
174
175     assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).isEmpty();
176   }
177
178   @Test
179   public void change_parent_with_names() throws Exception {
180     QualityProfileDto parent1 = createProfile("xoo", "Parent 1");
181     QualityProfileDto parent2 = createProfile("xoo", "Parent 2");
182     QualityProfileDto child = createProfile("xoo", "Child");
183
184     RuleDto rule1 = createRule("xoo", "rule1");
185     RuleDto rule2 = createRule("xoo", "rule2");
186     createActiveRule(rule1, parent1);
187     createActiveRule(rule2, parent2);
188     session.commit();
189     ruleIndexer.index();
190     activeRuleIndexer.index();
191
192     assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
193
194     // 1. Set parent 1
195     wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
196       .setParam(QProfileIdentificationParamUtils.PARAM_LANGUAGE, "xoo")
197       .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_NAME, child.getName())
198       .setParam("parentName", parent1.getName())
199       .execute();
200     session.clearCache();
201
202     // 1. check rule 1 enabled
203     List<ActiveRuleDto> activeRules1 = db.activeRuleDao().selectByProfileKey(session, child.getKey());
204     assertThat(activeRules1).hasSize(1);
205     assertThat(activeRules1.get(0).getKey().ruleKey().rule()).isEqualTo("rule1");
206     assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).hasSize(1);
207
208     // 2. Set parent 2
209     wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
210       .setParam(QProfileIdentificationParamUtils.PARAM_LANGUAGE, "xoo")
211       .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_NAME, child.getName())
212       .setParam("parentName", parent2.getName())
213       .execute();
214     session.clearCache();
215
216     // 2. check rule 2 enabled
217     List<ActiveRuleDto> activeRules2 = db.activeRuleDao().selectByProfileKey(session, child.getKey());
218     assertThat(activeRules2).hasSize(1);
219     assertThat(activeRules2.get(0).getKey().ruleKey().rule()).isEqualTo("rule2");
220
221     // 3. Remove parent
222     wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
223       .setParam(QProfileIdentificationParamUtils.PARAM_LANGUAGE, "xoo")
224       .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_NAME, child.getName())
225       .setParam("parentName", "")
226       .execute();
227     session.clearCache();
228
229     // 3. check no rule enabled
230     List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(session, child.getKey());
231     assertThat(activeRules).isEmpty();
232     assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).isEmpty();
233   }
234
235   @Test
236   public void remove_parent_with_empty_key() throws Exception {
237     QualityProfileDto parent = createProfile("xoo", "Parent 1");
238     QualityProfileDto child = createProfile("xoo", "Child");
239
240     RuleDto rule1 = createRule("xoo", "rule1");
241     createActiveRule(rule1, parent);
242     session.commit();
243     ruleIndexer.index();
244     activeRuleIndexer.index();
245
246     assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
247
248     // Set parent
249     tester.get(RuleActivator.class).setParent(child.getKey(), parent.getKey());
250     session.clearCache();
251
252     // Remove parent
253     wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
254       .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKey())
255       .setParam("parentKey", "")
256       .execute();
257     session.clearCache();
258
259     // Check no rule enabled
260     List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(session, child.getKey());
261     assertThat(activeRules).isEmpty();
262     assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).isEmpty();
263   }
264
265   @Test(expected = IllegalArgumentException.class)
266   public void fail_if_parent_key_and_name_both_set() throws Exception {
267     QualityProfileDto child = createProfile("xoo", "Child");
268     session.commit();
269
270     assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
271     assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).isEmpty();
272
273     wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
274       .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKee())
275       .setParam("parentName", "polop")
276       .setParam("parentKey", "palap")
277       .execute();
278   }
279
280   @Test(expected = RowNotFoundException.class)
281   public void fail_if_profile_key_and_name_both_set() throws Exception {
282     QualityProfileDto child = createProfile("xoo", "Child");
283     session.commit();
284
285     assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
286     assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).isEmpty();
287
288     wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
289       .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKee())
290       .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_NAME, child.getName())
291       .setParam("parentKey", "palap")
292       .execute();
293   }
294
295   @Test(expected = ForbiddenException.class)
296   public void fail_if_missing_permission() throws Exception {
297     userSessionRule.login("anakin");
298     wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
299       .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, "polop")
300       .setParam("parentKey", "pulup")
301       .execute();
302   }
303
304   private QualityProfileDto createProfile(String lang, String name) {
305     QualityProfileDto profile = QProfileTesting.newQProfileDto(new QProfileName(lang, name), "p" + lang + "-" + name.toLowerCase());
306     db.qualityProfileDao().insert(session, profile);
307     return profile;
308   }
309
310   private RuleDto createRule(String lang, String id) {
311     RuleDto rule = RuleDto.createFor(RuleKey.of("blah", id))
312       .setLanguage(lang)
313       .setSeverity(Severity.BLOCKER)
314       .setStatus(RuleStatus.READY);
315     db.ruleDao().insert(session, rule);
316     return rule;
317   }
318
319   private ActiveRuleDto createActiveRule(RuleDto rule, QualityProfileDto profile) {
320     ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule)
321       .setSeverity(rule.getSeverityString());
322     db.activeRuleDao().insert(session, activeRule);
323     return activeRule;
324   }
325 }