3 * Copyright (C) 2009-2021 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.junit.rules.ExpectedException;
26 import org.sonar.api.resources.Languages;
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.api.server.rule.RuleParamType;
31 import org.sonar.api.server.ws.WebService;
32 import org.sonar.core.util.Uuids;
33 import org.sonar.db.DbClient;
34 import org.sonar.db.DbSession;
35 import org.sonar.db.DbTester;
36 import org.sonar.db.qualityprofile.ActiveRuleDto;
37 import org.sonar.db.qualityprofile.ActiveRuleParamDto;
38 import org.sonar.db.qualityprofile.QProfileDto;
39 import org.sonar.db.rule.RuleDefinitionDto;
40 import org.sonar.db.rule.RuleDto;
41 import org.sonar.db.rule.RuleDto.Scope;
42 import org.sonar.db.rule.RuleParamDto;
43 import org.sonar.db.rule.RuleRepositoryDto;
44 import org.sonar.server.language.LanguageTesting;
45 import org.sonar.server.qualityprofile.QProfileComparison;
46 import org.sonar.server.tester.UserSessionRule;
47 import org.sonar.server.ws.WsActionTester;
49 import static java.util.Collections.singletonList;
50 import static org.assertj.core.api.Assertions.assertThat;
52 public class CompareActionTest {
55 public DbTester db = DbTester.create();
57 public UserSessionRule userSessionRule = UserSessionRule.standalone();
59 public ExpectedException expectedException = ExpectedException.none();
61 public UserSessionRule userSession = UserSessionRule.standalone();
63 private DbClient dbClient = db.getDbClient();
64 private DbSession session = db.getSession();
66 private WsActionTester ws = new WsActionTester(
67 new CompareAction(db.getDbClient(), new QProfileComparison(db.getDbClient()), new Languages(LanguageTesting.newLanguage("xoo", "Xoo"))));
70 public void compare_nominal() {
71 createRepository("blah", "xoo", "Blah");
73 RuleDefinitionDto rule1 = createRule("xoo", "rule1");
74 RuleDefinitionDto rule2 = createRule("xoo", "rule2");
75 RuleDefinitionDto rule3 = createRule("xoo", "rule3");
76 RuleDefinitionDto rule4 = createRuleWithParam("xoo", "rule4");
77 RuleDefinitionDto rule5 = createRule("xoo", "rule5");
81 * - rule 1 active (on both profiles) => "same"
82 * - rule 2 active (only in this profile) => "inLeft"
83 * - rule 4 active with different parameters => "modified"
84 * - rule 5 active with different severity => "modified"
86 QProfileDto profile1 = createProfile("xoo", "Profile 1", "xoo-profile-1-01234");
87 createActiveRule(rule1, profile1);
88 createActiveRule(rule2, profile1);
89 createActiveRuleWithParam(rule4, profile1, "polop");
90 createActiveRuleWithSeverity(rule5, profile1, Severity.MINOR);
95 * - rule 1 active (on both profiles) => "same"
96 * - rule 3 active (only in this profile) => "inRight"
97 * - rule 4 active with different parameters => "modified"
99 QProfileDto profile2 = createProfile("xoo", "Profile 2", "xoo-profile-2-12345");
100 createActiveRule(rule1, profile2);
101 createActiveRule(rule3, profile2);
102 createActiveRuleWithParam(rule4, profile2, "palap");
103 createActiveRuleWithSeverity(rule5, profile2, Severity.MAJOR);
107 .setParam("leftKey", profile1.getKee())
108 .setParam("rightKey", profile2.getKee())
109 .execute().assertJson(this.getClass(), "compare_nominal.json");
113 public void compare_param_on_left() {
114 RuleDefinitionDto rule1 = createRuleWithParam("xoo", "rule1");
115 createRepository("blah", "xoo", "Blah");
116 QProfileDto profile1 = createProfile("xoo", "Profile 1", "xoo-profile-1-01234");
117 createActiveRuleWithParam(rule1, profile1, "polop");
118 QProfileDto profile2 = createProfile("xoo", "Profile 2", "xoo-profile-2-12345");
119 createActiveRule(rule1, profile2);
123 .setParam("leftKey", profile1.getKee())
124 .setParam("rightKey", profile2.getKee())
125 .execute().assertJson(this.getClass(), "compare_param_on_left.json");
129 public void compare_param_on_right() {
130 RuleDefinitionDto rule1 = createRuleWithParam("xoo", "rule1");
131 createRepository("blah", "xoo", "Blah");
132 QProfileDto profile1 = createProfile("xoo", "Profile 1", "xoo-profile-1-01234");
133 createActiveRule(rule1, profile1);
134 QProfileDto profile2 = createProfile("xoo", "Profile 2", "xoo-profile-2-12345");
135 createActiveRuleWithParam(rule1, profile2, "polop");
139 .setParam("leftKey", profile1.getKee())
140 .setParam("rightKey", profile2.getKee())
141 .execute().assertJson(this.getClass(), "compare_param_on_right.json");
144 @Test(expected = IllegalArgumentException.class)
145 public void fail_on_missing_left_param() {
147 .setParam("rightKey", "polop")
151 @Test(expected = IllegalArgumentException.class)
152 public void fail_on_missing_right_param() {
154 .setParam("leftKey", "polop")
158 @Test(expected = IllegalArgumentException.class)
159 public void fail_on_left_profile_not_found() {
160 createProfile("xoo", "Right", "xoo-right-12345");
162 .setParam("leftKey", "polop")
163 .setParam("rightKey", "xoo-right-12345")
167 @Test(expected = IllegalArgumentException.class)
168 public void fail_on_right_profile_not_found() {
169 createProfile("xoo", "Left", "xoo-left-12345");
171 .setParam("leftKey", "xoo-left-12345")
172 .setParam("rightKey", "polop")
177 public void definition() {
178 WebService.Action definition = ws.getDef();
179 assertThat(definition).isNotNull();
180 assertThat(definition.isPost()).isFalse();
181 assertThat(definition.isInternal()).isTrue();
182 assertThat(definition.params()).hasSize(2).extracting("key").containsOnly(
183 "leftKey", "rightKey");
184 assertThat(definition.responseExampleAsString()).isNotEmpty();
187 private QProfileDto createProfile(String lang, String name, String key) {
188 return db.qualityProfiles().insert(p -> p.setKee(key).setName(name).setLanguage(lang));
191 private RuleDefinitionDto createRule(String lang, String id) {
192 RuleDto rule = RuleDto.createFor(RuleKey.of("blah", id))
193 .setUuid(Uuids.createFast())
194 .setName(StringUtils.capitalize(id))
196 .setSeverity(Severity.BLOCKER)
197 .setScope(Scope.MAIN)
198 .setStatus(RuleStatus.READY);
199 RuleDefinitionDto ruleDefinition = rule.getDefinition();
200 dbClient.ruleDao().insert(session, ruleDefinition);
201 return ruleDefinition;
204 private RuleDefinitionDto createRuleWithParam(String lang, String id) {
205 RuleDefinitionDto rule = createRule(lang, id);
206 RuleParamDto param = RuleParamDto.createFor(rule)
207 .setName("param_" + id)
208 .setType(RuleParamType.STRING.toString());
209 dbClient.ruleDao().insertRuleParam(session, rule, param);
213 private ActiveRuleDto createActiveRule(RuleDefinitionDto rule, QProfileDto profile) {
214 ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule)
215 .setSeverity(rule.getSeverityString());
216 dbClient.activeRuleDao().insert(session, activeRule);
220 private ActiveRuleDto createActiveRuleWithParam(RuleDefinitionDto rule, QProfileDto profile, String value) {
221 ActiveRuleDto activeRule = createActiveRule(rule, profile);
222 RuleParamDto paramDto = dbClient.ruleDao().selectRuleParamsByRuleKey(session, rule.getKey()).get(0);
223 ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(paramDto).setValue(value);
224 dbClient.activeRuleDao().insertParam(session, activeRule, activeRuleParam);
228 private ActiveRuleDto createActiveRuleWithSeverity(RuleDefinitionDto rule, QProfileDto profile, String severity) {
229 ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule)
230 .setSeverity(severity);
231 dbClient.activeRuleDao().insert(session, activeRule);
235 private void createRepository(String repositoryKey, String repositoryLanguage, String repositoryName) {
236 RuleRepositoryDto dto = new RuleRepositoryDto(repositoryKey, repositoryLanguage, repositoryName);
237 dbClient.ruleRepositoryDao().insertOrUpdate(session, singletonList(dto));