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.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;
52 import static org.assertj.core.api.Assertions.assertThat;
54 public class ChangeParentActionMediumTest {
56 // TODO Replace with DbTester + EsTester once DaoV2 is removed
58 public static ServerTester tester = new ServerTester().withEsIndexes();
60 public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
66 RuleIndexer ruleIndexer;
67 ActiveRuleIndexer activeRuleIndexer;
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);
89 public void change_parent_with_no_parent_before() throws Exception {
90 QualityProfileDto parent1 = createProfile("xoo", "Parent 1");
91 QualityProfileDto child = createProfile("xoo", "Child");
93 RuleDto rule1 = createRule("xoo", "rule1");
94 createActiveRule(rule1, parent1);
97 activeRuleIndexer.index();
99 assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
102 wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
103 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKey())
104 .setParam("parentKey", parent1.getKey())
106 session.clearCache();
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");
113 assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).hasSize(1);
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");
122 RuleDto rule1 = createRule("xoo", "rule1");
123 RuleDto rule2 = createRule("xoo", "rule2");
124 createActiveRule(rule1, parent1);
125 createActiveRule(rule2, parent2);
128 activeRuleIndexer.index();
131 tester.get(RuleActivator.class).setParent(child.getKey(), parent1.getKey());
132 session.clearCache();
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())
139 session.clearCache();
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");
146 assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).hasSize(1);
150 public void remove_parent() throws Exception {
151 QualityProfileDto parent = createProfile("xoo", "Parent 1");
152 QualityProfileDto child = createProfile("xoo", "Child");
154 RuleDto rule1 = createRule("xoo", "rule1");
155 createActiveRule(rule1, parent);
158 activeRuleIndexer.index();
161 tester.get(RuleActivator.class).setParent(child.getKey(), parent.getKey());
162 session.clearCache();
164 // Remove parent through WS
165 wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
166 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKey())
168 session.clearCache();
170 // Check no rule enabled
171 List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(session, child.getKey());
172 assertThat(activeRules).isEmpty();
174 assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).isEmpty();
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");
183 RuleDto rule1 = createRule("xoo", "rule1");
184 RuleDto rule2 = createRule("xoo", "rule2");
185 createActiveRule(rule1, parent1);
186 createActiveRule(rule2, parent2);
189 activeRuleIndexer.index();
191 assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
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())
199 session.clearCache();
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);
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())
213 session.clearCache();
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");
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", "")
226 session.clearCache();
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();
235 public void remove_parent_with_empty_key() throws Exception {
236 QualityProfileDto parent = createProfile("xoo", "Parent 1");
237 QualityProfileDto child = createProfile("xoo", "Child");
239 RuleDto rule1 = createRule("xoo", "rule1");
240 createActiveRule(rule1, parent);
243 activeRuleIndexer.index();
245 assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
248 tester.get(RuleActivator.class).setParent(child.getKey(), parent.getKey());
249 session.clearCache();
252 wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
253 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKey())
254 .setParam("parentKey", "")
256 session.clearCache();
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();
264 @Test(expected = IllegalArgumentException.class)
265 public void fail_if_parent_key_and_name_both_set() throws Exception {
266 QualityProfileDto child = createProfile("xoo", "Child");
269 assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
270 assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).isEmpty();
272 wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
273 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKee())
274 .setParam("parentName", "polop")
275 .setParam("parentKey", "palap")
279 @Test(expected = RowNotFoundException.class)
280 public void fail_if_profile_key_and_name_both_set() throws Exception {
281 QualityProfileDto child = createProfile("xoo", "Child");
284 assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
285 assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).isEmpty();
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")
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")
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);
309 private RuleDto createRule(String lang, String id) {
310 RuleDto rule = RuleTesting.newDto(RuleKey.of("blah", id))
312 .setSeverity(Severity.BLOCKER)
313 .setStatus(RuleStatus.READY);
314 db.ruleDao().insert(session, rule);
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);