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 org.junit.After;
23 import org.junit.Before;
24 import org.junit.ClassRule;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.api.rule.RuleKey;
28 import org.sonar.api.rule.RuleStatus;
29 import org.sonar.api.rule.Severity;
30 import org.sonar.core.permission.GlobalPermissions;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.RowNotFoundException;
33 import org.sonar.db.qualityprofile.ActiveRuleDto;
34 import org.sonar.db.qualityprofile.QualityProfileDto;
35 import org.sonar.db.rule.RuleDto;
36 import org.sonar.server.db.DbClient;
37 import org.sonar.server.exceptions.ForbiddenException;
38 import org.sonar.server.qualityprofile.QProfileName;
39 import org.sonar.server.qualityprofile.QProfileTesting;
40 import org.sonar.server.qualityprofile.RuleActivator;
41 import org.sonar.server.tester.ServerTester;
42 import org.sonar.server.tester.UserSessionRule;
43 import org.sonar.server.ws.WsTester;
45 import java.util.List;
47 import static org.assertj.core.api.Assertions.assertThat;
49 public class ChangeParentActionMediumTest {
51 // TODO Replace with DbTester + EsTester once DaoV2 is removed
53 public static ServerTester tester = new ServerTester();
55 public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
64 tester.clearDbAndIndexes();
65 db = tester.get(DbClient.class);
66 ws = tester.get(QProfilesWs.class);
67 wsTester = tester.get(WsTester.class);
68 session = db.openSession(false);
69 userSessionRule.login("gandalf").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
78 public void change_parent_with_no_parent_before() throws Exception {
79 QualityProfileDto parent1 = createProfile("xoo", "Parent 1");
80 QualityProfileDto child = createProfile("xoo", "Child");
82 RuleDto rule1 = createRule("xoo", "rule1");
83 createActiveRule(rule1, parent1);
86 assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
89 wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
90 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKey())
91 .setParam("parentKey", parent1.getKey().toString())
95 // Check rule 1 enabled
96 List<ActiveRuleDto> activeRules1 = db.activeRuleDao().selectByProfileKey(session, child.getKey());
97 assertThat(activeRules1).hasSize(1);
98 assertThat(activeRules1.get(0).getKey().ruleKey().rule()).isEqualTo("rule1");
102 public void replace_existing_parent() throws Exception {
103 QualityProfileDto parent1 = createProfile("xoo", "Parent 1");
104 QualityProfileDto parent2 = createProfile("xoo", "Parent 2");
105 QualityProfileDto child = createProfile("xoo", "Child");
107 RuleDto rule1 = createRule("xoo", "rule1");
108 RuleDto rule2 = createRule("xoo", "rule2");
109 createActiveRule(rule1, parent1);
110 createActiveRule(rule2, parent2);
114 tester.get(RuleActivator.class).setParent(child.getKey(), parent1.getKey());
115 session.clearCache();
117 // Set parent 2 through WS
118 wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
119 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKey())
120 .setParam("parentKey", parent2.getKey().toString())
122 session.clearCache();
124 // Check rule 2 enabled
125 List<ActiveRuleDto> activeRules2 = db.activeRuleDao().selectByProfileKey(session, child.getKey());
126 assertThat(activeRules2).hasSize(1);
127 assertThat(activeRules2.get(0).getKey().ruleKey().rule()).isEqualTo("rule2");
131 public void remove_parent() throws Exception {
132 QualityProfileDto parent = createProfile("xoo", "Parent 1");
133 QualityProfileDto child = createProfile("xoo", "Child");
135 RuleDto rule1 = createRule("xoo", "rule1");
136 createActiveRule(rule1, parent);
140 tester.get(RuleActivator.class).setParent(child.getKey(), parent.getKey());
141 session.clearCache();
143 // Remove parent through WS
144 wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
145 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKey())
147 session.clearCache();
149 // Check no rule enabled
150 List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(session, child.getKey());
151 assertThat(activeRules).isEmpty();
155 public void change_parent_with_names() throws Exception {
156 QualityProfileDto parent1 = createProfile("xoo", "Parent 1");
157 QualityProfileDto parent2 = createProfile("xoo", "Parent 2");
158 QualityProfileDto child = createProfile("xoo", "Child");
160 RuleDto rule1 = createRule("xoo", "rule1");
161 RuleDto rule2 = createRule("xoo", "rule2");
162 createActiveRule(rule1, parent1);
163 createActiveRule(rule2, parent2);
166 assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
169 wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
170 .setParam(QProfileIdentificationParamUtils.PARAM_LANGUAGE, "xoo")
171 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_NAME, child.getName())
172 .setParam("parentName", parent1.getName())
174 session.clearCache();
176 // 1. check rule 1 enabled
177 List<ActiveRuleDto> activeRules1 = db.activeRuleDao().selectByProfileKey(session, child.getKey());
178 assertThat(activeRules1).hasSize(1);
179 assertThat(activeRules1.get(0).getKey().ruleKey().rule()).isEqualTo("rule1");
182 wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
183 .setParam(QProfileIdentificationParamUtils.PARAM_LANGUAGE, "xoo")
184 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_NAME, child.getName())
185 .setParam("parentName", parent2.getName())
187 session.clearCache();
189 // 2. check rule 2 enabled
190 List<ActiveRuleDto> activeRules2 = db.activeRuleDao().selectByProfileKey(session, child.getKey());
191 assertThat(activeRules2).hasSize(1);
192 assertThat(activeRules2.get(0).getKey().ruleKey().rule()).isEqualTo("rule2");
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", "")
200 session.clearCache();
202 // 3. check no rule enabled
203 List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(session, child.getKey());
204 assertThat(activeRules).isEmpty();
208 public void remove_parent_with_empty_key() throws Exception {
209 QualityProfileDto parent = createProfile("xoo", "Parent 1");
210 QualityProfileDto child = createProfile("xoo", "Child");
212 RuleDto rule1 = createRule("xoo", "rule1");
213 createActiveRule(rule1, parent);
216 assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
219 tester.get(RuleActivator.class).setParent(child.getKey(), parent.getKey());
220 session.clearCache();
223 wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
224 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKey())
225 .setParam("parentKey", "")
227 session.clearCache();
229 // Check no rule enabled
230 List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(session, child.getKey());
231 assertThat(activeRules).isEmpty();
234 @Test(expected = IllegalArgumentException.class)
235 public void fail_if_parent_key_and_name_both_set() throws Exception {
236 QualityProfileDto child = createProfile("xoo", "Child");
239 assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
241 wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
242 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKee())
243 .setParam("parentName", "polop")
244 .setParam("parentKey", "palap")
248 @Test(expected = RowNotFoundException.class)
249 public void fail_if_profile_key_and_name_both_set() throws Exception {
250 QualityProfileDto child = createProfile("xoo", "Child");
253 assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
255 wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
256 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKee())
257 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_NAME, child.getName())
258 .setParam("parentKey", "palap")
262 @Test(expected = ForbiddenException.class)
263 public void fail_if_missing_permission() throws Exception {
264 userSessionRule.login("anakin");
265 wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
266 .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, "polop")
267 .setParam("parentKey", "pulup")
271 private QualityProfileDto createProfile(String lang, String name) {
272 QualityProfileDto profile = QProfileTesting.newQProfileDto(new QProfileName(lang, name), "p" + lang + "-" + name.toLowerCase());
273 db.qualityProfileDao().insert(session, profile);
277 private RuleDto createRule(String lang, String id) {
278 RuleDto rule = RuleDto.createFor(RuleKey.of("blah", id))
280 .setSeverity(Severity.BLOCKER)
281 .setStatus(RuleStatus.READY);
282 db.deprecatedRuleDao().insert(session, rule);
286 private ActiveRuleDto createActiveRule(RuleDto rule, QualityProfileDto profile) {
287 ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule)
288 .setSeverity(rule.getSeverityString());
289 db.activeRuleDao().insert(session, activeRule);