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.ws;
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;
51 import static org.assertj.core.api.Assertions.assertThat;
53 public class ChangeParentActionMediumTest {
55 // TODO Replace with DbTester + EsTester once DaoV2 is removed
57 public static ServerTester tester = new ServerTester().withEsIndexes();
59 public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
65 RuleIndexer ruleIndexer;
66 ActiveRuleIndexer activeRuleIndexer;
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);
90 public void change_parent_with_no_parent_before() throws Exception {
91 QualityProfileDto parent1 = createProfile("xoo", "Parent 1");
92 QualityProfileDto child = createProfile("xoo", "Child");
94 RuleDto rule1 = createRule("xoo", "rule1");
95 createActiveRule(rule1, parent1);
98 activeRuleIndexer.index();
100 assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
103 wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
104 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKey())
105 .setParam("parentKey", parent1.getKey())
107 session.clearCache();
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");
114 assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).hasSize(1);
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");
123 RuleDto rule1 = createRule("xoo", "rule1");
124 RuleDto rule2 = createRule("xoo", "rule2");
125 createActiveRule(rule1, parent1);
126 createActiveRule(rule2, parent2);
129 activeRuleIndexer.index();
132 tester.get(RuleActivator.class).setParent(child.getKey(), parent1.getKey());
133 session.clearCache();
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())
140 session.clearCache();
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");
147 assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).hasSize(1);
151 public void remove_parent() throws Exception {
152 QualityProfileDto parent = createProfile("xoo", "Parent 1");
153 QualityProfileDto child = createProfile("xoo", "Child");
155 RuleDto rule1 = createRule("xoo", "rule1");
156 createActiveRule(rule1, parent);
159 activeRuleIndexer.index();
162 tester.get(RuleActivator.class).setParent(child.getKey(), parent.getKey());
163 session.clearCache();
165 // Remove parent through WS
166 wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
167 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKey())
169 session.clearCache();
171 // Check no rule enabled
172 List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(session, child.getKey());
173 assertThat(activeRules).isEmpty();
175 assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).isEmpty();
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");
184 RuleDto rule1 = createRule("xoo", "rule1");
185 RuleDto rule2 = createRule("xoo", "rule2");
186 createActiveRule(rule1, parent1);
187 createActiveRule(rule2, parent2);
190 activeRuleIndexer.index();
192 assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
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())
200 session.clearCache();
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);
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())
214 session.clearCache();
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");
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", "")
227 session.clearCache();
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();
236 public void remove_parent_with_empty_key() throws Exception {
237 QualityProfileDto parent = createProfile("xoo", "Parent 1");
238 QualityProfileDto child = createProfile("xoo", "Child");
240 RuleDto rule1 = createRule("xoo", "rule1");
241 createActiveRule(rule1, parent);
244 activeRuleIndexer.index();
246 assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
249 tester.get(RuleActivator.class).setParent(child.getKey(), parent.getKey());
250 session.clearCache();
253 wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
254 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKey())
255 .setParam("parentKey", "")
257 session.clearCache();
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();
265 @Test(expected = IllegalArgumentException.class)
266 public void fail_if_parent_key_and_name_both_set() throws Exception {
267 QualityProfileDto child = createProfile("xoo", "Child");
270 assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
271 assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).isEmpty();
273 wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
274 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKee())
275 .setParam("parentName", "polop")
276 .setParam("parentKey", "palap")
280 @Test(expected = RowNotFoundException.class)
281 public void fail_if_profile_key_and_name_both_set() throws Exception {
282 QualityProfileDto child = createProfile("xoo", "Child");
285 assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
286 assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).isEmpty();
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")
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")
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);
310 private RuleDto createRule(String lang, String id) {
311 RuleDto rule = RuleDto.createFor(RuleKey.of("blah", id))
313 .setSeverity(Severity.BLOCKER)
314 .setStatus(RuleStatus.READY);
315 db.ruleDao().insert(session, rule);
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);