]> source.dussan.org Git - sonarqube.git/blob
2f364997b3358c1092a67c5b15f92ac8125e85b6
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 SonarSource SA
4  * mailto:info 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.apache.commons.lang.StringUtils;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.resources.Languages;
26 import org.sonar.api.rule.RuleKey;
27 import org.sonar.api.rule.RuleStatus;
28 import org.sonar.api.rule.Severity;
29 import org.sonar.api.server.rule.RuleParamType;
30 import org.sonar.api.server.ws.WebService;
31 import org.sonar.core.util.Uuids;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.DbTester;
35 import org.sonar.db.qualityprofile.ActiveRuleDto;
36 import org.sonar.db.qualityprofile.ActiveRuleParamDto;
37 import org.sonar.db.qualityprofile.QProfileDto;
38 import org.sonar.db.rule.RuleDefinitionDto;
39 import org.sonar.db.rule.RuleDto;
40 import org.sonar.db.rule.RuleDto.Scope;
41 import org.sonar.db.rule.RuleParamDto;
42 import org.sonar.db.rule.RuleRepositoryDto;
43 import org.sonar.server.language.LanguageTesting;
44 import org.sonar.server.qualityprofile.QProfileComparison;
45 import org.sonar.server.tester.UserSessionRule;
46 import org.sonar.server.ws.WsActionTester;
47
48 import static java.util.Collections.singletonList;
49 import static org.assertj.core.api.Assertions.assertThat;
50 import static org.assertj.core.api.Assertions.assertThatThrownBy;
51
52 public class CompareActionTest {
53
54   @Rule
55   public DbTester db = DbTester.create();
56   @Rule
57   public UserSessionRule userSessionRule = UserSessionRule.standalone();
58   @Rule
59   public UserSessionRule userSession = UserSessionRule.standalone();
60
61   private DbClient dbClient = db.getDbClient();
62   private DbSession session = db.getSession();
63
64   private WsActionTester ws = new WsActionTester(
65     new CompareAction(db.getDbClient(), new QProfileComparison(db.getDbClient()), new Languages(LanguageTesting.newLanguage("xoo", "Xoo"))));
66
67   @Test
68   public void compare_nominal() {
69     createRepository("blah", "xoo", "Blah");
70
71     RuleDefinitionDto rule1 = createRule("xoo", "rule1");
72     RuleDefinitionDto rule2 = createRule("xoo", "rule2");
73     RuleDefinitionDto rule3 = createRule("xoo", "rule3");
74     RuleDefinitionDto rule4 = createRuleWithParam("xoo", "rule4");
75     RuleDefinitionDto rule5 = createRule("xoo", "rule5");
76
77     /*
78      * Profile 1:
79      * - rule 1 active (on both profiles) => "same"
80      * - rule 2 active (only in this profile) => "inLeft"
81      * - rule 4 active with different parameters => "modified"
82      * - rule 5 active with different severity => "modified"
83      */
84     QProfileDto profile1 = createProfile("xoo", "Profile 1", "xoo-profile-1-01234");
85     createActiveRule(rule1, profile1);
86     createActiveRule(rule2, profile1);
87     createActiveRuleWithParam(rule4, profile1, "polop");
88     createActiveRuleWithSeverity(rule5, profile1, Severity.MINOR);
89     session.commit();
90
91     /*
92      * Profile 1:
93      * - rule 1 active (on both profiles) => "same"
94      * - rule 3 active (only in this profile) => "inRight"
95      * - rule 4 active with different parameters => "modified"
96      */
97     QProfileDto profile2 = createProfile("xoo", "Profile 2", "xoo-profile-2-12345");
98     createActiveRule(rule1, profile2);
99     createActiveRule(rule3, profile2);
100     createActiveRuleWithParam(rule4, profile2, "palap");
101     createActiveRuleWithSeverity(rule5, profile2, Severity.MAJOR);
102     session.commit();
103
104     ws.newRequest()
105       .setParam("leftKey", profile1.getKee())
106       .setParam("rightKey", profile2.getKee())
107       .execute().assertJson(this.getClass(), "compare_nominal.json");
108   }
109
110   @Test
111   public void compare_param_on_left() {
112     RuleDefinitionDto rule1 = createRuleWithParam("xoo", "rule1");
113     createRepository("blah", "xoo", "Blah");
114     QProfileDto profile1 = createProfile("xoo", "Profile 1", "xoo-profile-1-01234");
115     createActiveRuleWithParam(rule1, profile1, "polop");
116     QProfileDto profile2 = createProfile("xoo", "Profile 2", "xoo-profile-2-12345");
117     createActiveRule(rule1, profile2);
118     session.commit();
119
120     ws.newRequest()
121       .setParam("leftKey", profile1.getKee())
122       .setParam("rightKey", profile2.getKee())
123       .execute().assertJson(this.getClass(), "compare_param_on_left.json");
124   }
125
126   @Test
127   public void compare_param_on_right() {
128     RuleDefinitionDto rule1 = createRuleWithParam("xoo", "rule1");
129     createRepository("blah", "xoo", "Blah");
130     QProfileDto profile1 = createProfile("xoo", "Profile 1", "xoo-profile-1-01234");
131     createActiveRule(rule1, profile1);
132     QProfileDto profile2 = createProfile("xoo", "Profile 2", "xoo-profile-2-12345");
133     createActiveRuleWithParam(rule1, profile2, "polop");
134     session.commit();
135
136     ws.newRequest()
137       .setParam("leftKey", profile1.getKee())
138       .setParam("rightKey", profile2.getKee())
139       .execute().assertJson(this.getClass(), "compare_param_on_right.json");
140   }
141
142   @Test
143   public void fail_on_missing_left_param() {
144     assertThatThrownBy(() -> {
145       ws.newRequest()
146         .setParam("rightKey", "polop")
147         .execute();
148     })
149       .isInstanceOf(IllegalArgumentException.class);
150   }
151
152   @Test
153   public void fail_on_missing_right_param() {
154     assertThatThrownBy(() -> {
155       ws.newRequest()
156         .setParam("leftKey", "polop")
157         .execute();
158     })
159       .isInstanceOf(IllegalArgumentException.class);
160   }
161
162   @Test
163   public void fail_on_left_profile_not_found() {
164     createProfile("xoo", "Right", "xoo-right-12345");
165
166     assertThatThrownBy(() -> {
167       ws.newRequest()
168         .setParam("leftKey", "polop")
169         .setParam("rightKey", "xoo-right-12345")
170         .execute();
171     })
172       .isInstanceOf(IllegalArgumentException.class);
173   }
174
175   @Test
176   public void fail_on_right_profile_not_found() {
177     createProfile("xoo", "Left", "xoo-left-12345");
178
179     assertThatThrownBy(() -> {
180       ws.newRequest()
181         .setParam("leftKey", "xoo-left-12345")
182         .setParam("rightKey", "polop")
183         .execute();
184     })
185       .isInstanceOf(IllegalArgumentException.class);
186   }
187
188   @Test
189   public void definition() {
190     WebService.Action definition = ws.getDef();
191     assertThat(definition).isNotNull();
192     assertThat(definition.isPost()).isFalse();
193     assertThat(definition.isInternal()).isTrue();
194     assertThat(definition.params()).hasSize(2).extracting("key").containsOnly(
195       "leftKey", "rightKey");
196     assertThat(definition.responseExampleAsString()).isNotEmpty();
197   }
198
199   private QProfileDto createProfile(String lang, String name, String key) {
200     return db.qualityProfiles().insert(p -> p.setKee(key).setName(name).setLanguage(lang));
201   }
202
203   private RuleDefinitionDto createRule(String lang, String id) {
204     RuleDto rule = RuleDto.createFor(RuleKey.of("blah", id))
205       .setUuid(Uuids.createFast())
206       .setName(StringUtils.capitalize(id))
207       .setLanguage(lang)
208       .setSeverity(Severity.BLOCKER)
209       .setScope(Scope.MAIN)
210       .setStatus(RuleStatus.READY);
211     RuleDefinitionDto ruleDefinition = rule.getDefinition();
212     dbClient.ruleDao().insert(session, ruleDefinition);
213     return ruleDefinition;
214   }
215
216   private RuleDefinitionDto createRuleWithParam(String lang, String id) {
217     RuleDefinitionDto rule = createRule(lang, id);
218     RuleParamDto param = RuleParamDto.createFor(rule)
219       .setName("param_" + id)
220       .setType(RuleParamType.STRING.toString());
221     dbClient.ruleDao().insertRuleParam(session, rule, param);
222     return rule;
223   }
224
225   private ActiveRuleDto createActiveRule(RuleDefinitionDto rule, QProfileDto profile) {
226     ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule)
227       .setSeverity(rule.getSeverityString());
228     dbClient.activeRuleDao().insert(session, activeRule);
229     return activeRule;
230   }
231
232   private ActiveRuleDto createActiveRuleWithParam(RuleDefinitionDto rule, QProfileDto profile, String value) {
233     ActiveRuleDto activeRule = createActiveRule(rule, profile);
234     RuleParamDto paramDto = dbClient.ruleDao().selectRuleParamsByRuleKey(session, rule.getKey()).get(0);
235     ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(paramDto).setValue(value);
236     dbClient.activeRuleDao().insertParam(session, activeRule, activeRuleParam);
237     return activeRule;
238   }
239
240   private ActiveRuleDto createActiveRuleWithSeverity(RuleDefinitionDto rule, QProfileDto profile, String severity) {
241     ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule)
242       .setSeverity(severity);
243     dbClient.activeRuleDao().insert(session, activeRule);
244     return activeRule;
245   }
246
247   private void createRepository(String repositoryKey, String repositoryLanguage, String repositoryName) {
248     RuleRepositoryDto dto = new RuleRepositoryDto(repositoryKey, repositoryLanguage, repositoryName);
249     dbClient.ruleRepositoryDao().insert(session, singletonList(dto));
250     session.commit();
251   }
252 }