3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info 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.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;
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;
52 public class CompareActionTest {
55 public DbTester db = DbTester.create();
57 public UserSessionRule userSessionRule = UserSessionRule.standalone();
59 public UserSessionRule userSession = UserSessionRule.standalone();
61 private DbClient dbClient = db.getDbClient();
62 private DbSession session = db.getSession();
64 private WsActionTester ws = new WsActionTester(
65 new CompareAction(db.getDbClient(), new QProfileComparison(db.getDbClient()), new Languages(LanguageTesting.newLanguage("xoo", "Xoo"))));
68 public void compare_nominal() {
69 createRepository("blah", "xoo", "Blah");
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");
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"
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);
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"
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);
105 .setParam("leftKey", profile1.getKee())
106 .setParam("rightKey", profile2.getKee())
107 .execute().assertJson(this.getClass(), "compare_nominal.json");
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);
121 .setParam("leftKey", profile1.getKee())
122 .setParam("rightKey", profile2.getKee())
123 .execute().assertJson(this.getClass(), "compare_param_on_left.json");
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");
137 .setParam("leftKey", profile1.getKee())
138 .setParam("rightKey", profile2.getKee())
139 .execute().assertJson(this.getClass(), "compare_param_on_right.json");
143 public void fail_on_missing_left_param() {
144 assertThatThrownBy(() -> {
146 .setParam("rightKey", "polop")
149 .isInstanceOf(IllegalArgumentException.class);
153 public void fail_on_missing_right_param() {
154 assertThatThrownBy(() -> {
156 .setParam("leftKey", "polop")
159 .isInstanceOf(IllegalArgumentException.class);
163 public void fail_on_left_profile_not_found() {
164 createProfile("xoo", "Right", "xoo-right-12345");
166 assertThatThrownBy(() -> {
168 .setParam("leftKey", "polop")
169 .setParam("rightKey", "xoo-right-12345")
172 .isInstanceOf(IllegalArgumentException.class);
176 public void fail_on_right_profile_not_found() {
177 createProfile("xoo", "Left", "xoo-left-12345");
179 assertThatThrownBy(() -> {
181 .setParam("leftKey", "xoo-left-12345")
182 .setParam("rightKey", "polop")
185 .isInstanceOf(IllegalArgumentException.class);
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();
199 private QProfileDto createProfile(String lang, String name, String key) {
200 return db.qualityProfiles().insert(p -> p.setKee(key).setName(name).setLanguage(lang));
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))
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;
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);
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);
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);
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);
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));