]> source.dussan.org Git - sonarqube.git/blob
abf7293d8d8b0cf78196a8da993ac84e2ae84f6d
[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 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;
44
45 import java.util.List;
46
47 import static org.assertj.core.api.Assertions.assertThat;
48
49 public class ChangeParentActionMediumTest {
50
51   // TODO Replace with DbTester + EsTester once DaoV2 is removed
52   @ClassRule
53   public static ServerTester tester = new ServerTester();
54   @Rule
55   public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
56
57   QProfilesWs ws;
58   DbClient db;
59   DbSession session;
60   WsTester wsTester;
61
62   @Before
63   public void setUp() {
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);
70   }
71
72   @After
73   public void after() {
74     session.close();
75   }
76
77   @Test
78   public void change_parent_with_no_parent_before() throws Exception {
79     QualityProfileDto parent1 = createProfile("xoo", "Parent 1");
80     QualityProfileDto child = createProfile("xoo", "Child");
81
82     RuleDto rule1 = createRule("xoo", "rule1");
83     createActiveRule(rule1, parent1);
84     session.commit();
85
86     assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
87
88     // Set parent
89     wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
90       .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKey())
91       .setParam("parentKey", parent1.getKey().toString())
92       .execute();
93     session.clearCache();
94
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");
99   }
100
101   @Test
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");
106
107     RuleDto rule1 = createRule("xoo", "rule1");
108     RuleDto rule2 = createRule("xoo", "rule2");
109     createActiveRule(rule1, parent1);
110     createActiveRule(rule2, parent2);
111     session.commit();
112
113     // Set parent 1
114     tester.get(RuleActivator.class).setParent(child.getKey(), parent1.getKey());
115     session.clearCache();
116
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())
121       .execute();
122     session.clearCache();
123
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");
128   }
129
130   @Test
131   public void remove_parent() throws Exception {
132     QualityProfileDto parent = createProfile("xoo", "Parent 1");
133     QualityProfileDto child = createProfile("xoo", "Child");
134
135     RuleDto rule1 = createRule("xoo", "rule1");
136     createActiveRule(rule1, parent);
137     session.commit();
138
139     // Set parent
140     tester.get(RuleActivator.class).setParent(child.getKey(), parent.getKey());
141     session.clearCache();
142
143     // Remove parent through WS
144     wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
145       .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKey())
146       .execute();
147     session.clearCache();
148
149     // Check no rule enabled
150     List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(session, child.getKey());
151     assertThat(activeRules).isEmpty();
152   }
153
154   @Test
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");
159
160     RuleDto rule1 = createRule("xoo", "rule1");
161     RuleDto rule2 = createRule("xoo", "rule2");
162     createActiveRule(rule1, parent1);
163     createActiveRule(rule2, parent2);
164     session.commit();
165
166     assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
167
168     // 1. Set parent 1
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())
173       .execute();
174     session.clearCache();
175
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");
180
181     // 2. Set parent 2
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())
186       .execute();
187     session.clearCache();
188
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");
193
194     // 3. Remove parent
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", "")
199       .execute();
200     session.clearCache();
201
202     // 3. check no rule enabled
203     List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(session, child.getKey());
204     assertThat(activeRules).isEmpty();
205   }
206
207   @Test
208   public void remove_parent_with_empty_key() throws Exception {
209     QualityProfileDto parent = createProfile("xoo", "Parent 1");
210     QualityProfileDto child = createProfile("xoo", "Child");
211
212     RuleDto rule1 = createRule("xoo", "rule1");
213     createActiveRule(rule1, parent);
214     session.commit();
215
216     assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
217
218     // Set parent
219     tester.get(RuleActivator.class).setParent(child.getKey(), parent.getKey());
220     session.clearCache();
221
222     // Remove parent
223     wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
224       .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKey())
225       .setParam("parentKey", "")
226       .execute();
227     session.clearCache();
228
229     // Check no rule enabled
230     List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(session, child.getKey());
231     assertThat(activeRules).isEmpty();
232   }
233
234   @Test(expected = IllegalArgumentException.class)
235   public void fail_if_parent_key_and_name_both_set() throws Exception {
236     QualityProfileDto child = createProfile("xoo", "Child");
237     session.commit();
238
239     assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
240
241     wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent")
242       .setParam(QProfileIdentificationParamUtils.PARAM_PROFILE_KEY, child.getKee())
243       .setParam("parentName", "polop")
244       .setParam("parentKey", "palap")
245       .execute();
246   }
247
248   @Test(expected = RowNotFoundException.class)
249   public void fail_if_profile_key_and_name_both_set() throws Exception {
250     QualityProfileDto child = createProfile("xoo", "Child");
251     session.commit();
252
253     assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
254
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")
259       .execute();
260   }
261
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")
268       .execute();
269   }
270
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);
274     return profile;
275   }
276
277   private RuleDto createRule(String lang, String id) {
278     RuleDto rule = RuleDto.createFor(RuleKey.of("blah", id))
279       .setLanguage(lang)
280       .setSeverity(Severity.BLOCKER)
281       .setStatus(RuleStatus.READY);
282     db.deprecatedRuleDao().insert(session, rule);
283     return rule;
284   }
285
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);
290     return activeRule;
291   }
292 }