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