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;
22 import com.google.common.io.Resources;
23 import java.io.Reader;
24 import java.io.StringReader;
25 import java.io.StringWriter;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.List;
30 import javax.annotation.Nullable;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
34 import org.sonar.api.rule.RuleKey;
35 import org.sonar.api.rule.RuleStatus;
36 import org.sonar.api.rules.RuleType;
37 import org.sonar.api.utils.System2;
38 import org.sonar.core.util.UuidFactoryFast;
39 import org.sonar.db.DbSession;
40 import org.sonar.db.DbTester;
41 import org.sonar.db.qualityprofile.ActiveRuleDto;
42 import org.sonar.db.qualityprofile.ActiveRuleParamDto;
43 import org.sonar.db.qualityprofile.QProfileDto;
44 import org.sonar.db.qualityprofile.QualityProfileTesting;
45 import org.sonar.db.rule.RuleDefinitionDto;
46 import org.sonar.db.rule.RuleParamDto;
47 import org.sonar.server.qualityprofile.builtin.QProfileName;
48 import org.sonar.server.rule.RuleCreator;
50 import static java.nio.charset.StandardCharsets.UTF_8;
51 import static org.assertj.core.api.Assertions.assertThat;
52 import static org.assertj.core.api.Assertions.assertThatThrownBy;
53 import static org.junit.Assert.assertThrows;
54 import static org.mockito.ArgumentMatchers.any;
55 import static org.mockito.ArgumentMatchers.anyList;
56 import static org.mockito.Mockito.mock;
57 import static org.mockito.Mockito.when;
58 import static org.sonar.db.rule.RuleDescriptionSectionDto.createDefaultRuleDescriptionSection;
59 import static org.sonar.db.rule.RuleTesting.newRuleWithoutDescriptionSection;
61 public class QProfileBackuperImplTest {
63 private static final String EMPTY_BACKUP = "<?xml version='1.0' encoding='UTF-8'?>" +
64 "<profile><name>foo</name>" +
65 "<language>js</language>" +
69 private final System2 system2 = new AlwaysIncreasingSystem2();
72 public DbTester db = DbTester.create(system2);
74 private final DummyReset reset = new DummyReset();
75 private final QProfileFactory profileFactory = new DummyProfileFactory();
76 private final RuleCreator ruleCreator = mock(RuleCreator.class);
78 private final QProfileBackuper underTest = new QProfileBackuperImpl(db.getDbClient(), reset, profileFactory, ruleCreator, new QProfileParser());
81 public void backup_generates_xml_file() {
82 RuleDefinitionDto rule = createRule();
83 QProfileDto profile = createProfile(rule.getLanguage());
84 ActiveRuleDto activeRule = activate(profile, rule);
86 StringWriter writer = new StringWriter();
87 underTest.backup(db.getSession(), profile, writer);
89 assertThat(writer).hasToString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
90 "<profile><name>" + profile.getName() + "</name>" +
91 "<language>" + profile.getLanguage() + "</language>" +
94 "<repositoryKey>" + rule.getRepositoryKey() + "</repositoryKey>" +
95 "<key>" + rule.getRuleKey() + "</key>" +
96 "<type>" + RuleType.valueOf(rule.getType()).name() + "</type>" +
97 "<priority>" + activeRule.getSeverityString() + "</priority>" +
98 "<parameters></parameters>" +
105 public void backup_rules_having_parameters() {
106 RuleDefinitionDto rule = createRule();
107 RuleParamDto param = db.rules().insertRuleParam(rule);
108 QProfileDto profile = createProfile(rule.getLanguage());
109 ActiveRuleDto activeRule = activate(profile, rule, param);
111 StringWriter writer = new StringWriter();
112 underTest.backup(db.getSession(), profile, writer);
114 assertThat(writer.toString()).contains(
116 "<repositoryKey>" + rule.getRepositoryKey() + "</repositoryKey>" +
117 "<key>" + rule.getRuleKey() + "</key>" +
118 "<type>" + RuleType.valueOf(rule.getType()).name() + "</type>" +
119 "<priority>" + activeRule.getSeverityString() + "</priority>" +
120 "<parameters><parameter>" +
121 "<key>" + param.getName() + "</key>" +
122 "<value>20</value>" +
123 "</parameter></parameters>" +
128 public void backup_empty_profile() {
129 RuleDefinitionDto rule = createRule();
130 QProfileDto profile = createProfile(rule.getLanguage());
132 StringWriter writer = new StringWriter();
133 underTest.backup(db.getSession(), profile, writer);
135 assertThat(writer).hasToString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
136 "<profile><name>" + profile.getName() + "</name>" +
137 "<language>" + profile.getLanguage() + "</language>" +
143 public void backup_custom_rules_with_params() {
144 RuleDefinitionDto templateRule = db.rules().insert(ruleDefinitionDto -> ruleDefinitionDto
145 .setIsTemplate(true));
146 RuleDefinitionDto rule = db.rules().insert(ruleDefinitionDto -> ruleDefinitionDto
147 .addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(UuidFactoryFast.getInstance().create(), "custom rule description"))
148 .setName("custom rule name")
149 .setStatus(RuleStatus.READY)
150 .setTemplateUuid(templateRule.getUuid()));
151 RuleParamDto param = db.rules().insertRuleParam(rule);
152 QProfileDto profile = createProfile(rule.getLanguage());
153 ActiveRuleDto activeRule = activate(profile, rule, param);
155 StringWriter writer = new StringWriter();
156 underTest.backup(db.getSession(), profile, writer);
158 assertThat(writer).hasToString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
160 "<name>" + profile.getName() + "</name>" +
161 "<language>" + profile.getLanguage() + "</language>" +
163 "<repositoryKey>" + rule.getRepositoryKey() + "</repositoryKey>" +
164 "<key>" + rule.getKey().rule() + "</key>" +
165 "<type>" + RuleType.valueOf(rule.getType()) + "</type>" +
166 "<priority>" + activeRule.getSeverityString() + "</priority>" +
167 "<name>" + rule.getName() + "</name>" +
168 "<templateKey>" + templateRule.getKey().rule() + "</templateKey>" +
169 "<description>" + rule.getDefaultRuleDescriptionSectionDto().getDescription() + "</description>" +
170 "<descriptionSections><descriptionSection><key>default</key><content>" + rule.getDefaultRuleDescriptionSectionDto().getDescription() + "</content></descriptionSection></descriptionSections>" +
171 "<parameters><parameter>" +
172 "<key>" + param.getName() + "</key>" +
173 "<value>20</value>" +
174 "</parameter></parameters>" +
175 "</rule></rules></profile>");
179 public void backup_custom_rules_without_description_section() {
180 var rule = newRuleWithoutDescriptionSection();
181 db.rules().insert(rule);
182 RuleParamDto param = db.rules().insertRuleParam(rule);
183 QProfileDto profile = createProfile(rule.getLanguage());
184 ActiveRuleDto activeRule = activate(profile, rule, param);
186 StringWriter writer = new StringWriter();
187 underTest.backup(db.getSession(), profile, writer);
189 assertThat(writer).hasToString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
191 "<name>" + profile.getName() + "</name>" +
192 "<language>" + profile.getLanguage() + "</language>" +
194 "<repositoryKey>" + rule.getRepositoryKey() + "</repositoryKey>" +
195 "<key>" + rule.getKey().rule() + "</key>" +
196 "<type>" + RuleType.valueOf(rule.getType()) + "</type>" +
197 "<priority>" + activeRule.getSeverityString() + "</priority>" +
198 "<parameters><parameter>" +
199 "<key>" + param.getName() + "</key>" +
200 "<value>20</value>" +
201 "</parameter></parameters>" +
202 "</rule></rules></profile>");
206 public void restore_backup_on_the_profile_specified_in_backup() {
207 Reader backup = new StringReader(EMPTY_BACKUP);
209 QProfileRestoreSummary summary = underTest.restore(db.getSession(), backup, (String) null);
211 assertThat(summary.getProfile().getName()).isEqualTo("foo");
212 assertThat(summary.getProfile().getLanguage()).isEqualTo("js");
214 assertThat(reset.calledProfile.getKee()).isEqualTo(summary.getProfile().getKee());
215 assertThat(reset.calledActivations).isEmpty();
219 public void restore_detects_deprecated_rule_keys() {
220 String ruleUuid = db.rules().insert(RuleKey.of("sonarjs", "s001")).getUuid();
221 db.rules().insertDeprecatedKey(c -> c.setRuleUuid(ruleUuid).setOldRuleKey("oldkey").setOldRepositoryKey("oldrepo"));
223 Reader backup = new StringReader("<?xml version='1.0' encoding='UTF-8'?>" +
224 "<profile><name>foo</name>" +
225 "<language>js</language>" +
228 "<repositoryKey>oldrepo</repositoryKey>" +
229 "<key>oldkey</key>" +
230 "<priority>BLOCKER</priority>" +
232 "<parameter><key>bar</key><value>baz</value></parameter>" +
238 underTest.restore(db.getSession(), backup, (String) null);
240 assertThat(reset.calledActivations).hasSize(1);
241 RuleActivation activation = reset.calledActivations.get(0);
242 assertThat(activation.getSeverity()).isEqualTo("BLOCKER");
243 assertThat(activation.getRuleUuid()).isEqualTo(ruleUuid);
244 assertThat(activation.getParameter("bar")).isEqualTo("baz");
248 public void restore_ignores_deprecated_rule_keys_if_new_key_is_already_present() {
249 String ruleUuid = db.rules().insert(RuleKey.of("sonarjs", "s001")).getUuid();
250 db.rules().insertDeprecatedKey(c -> c.setRuleUuid(ruleUuid).setOldRuleKey("oldkey").setOldRepositoryKey("oldrepo"));
252 Reader backup = new StringReader("<?xml version='1.0' encoding='UTF-8'?>" +
253 "<profile><name>foo</name>" +
254 "<language>js</language>" +
257 "<repositoryKey>oldrepo</repositoryKey>" +
258 "<key>oldkey</key>" +
259 "<priority>MAJOR</priority>" +
261 "<parameter><key>bar</key><value>baz</value></parameter>" +
265 "<repositoryKey>sonarjs</repositoryKey>" +
267 "<priority>BLOCKER</priority>" +
269 "<parameter><key>bar2</key><value>baz2</value></parameter>" +
275 underTest.restore(db.getSession(), backup, (String) null);
277 assertThat(reset.calledActivations).hasSize(1);
278 RuleActivation activation = reset.calledActivations.get(0);
279 assertThat(activation.getSeverity()).isEqualTo("BLOCKER");
280 assertThat(activation.getRuleUuid()).isEqualTo(ruleUuid);
281 assertThat(activation.getParameter("bar2")).isEqualTo("baz2");
285 public void restore_backup_on_profile_having_different_name() {
286 Reader backup = new StringReader(EMPTY_BACKUP);
288 QProfileRestoreSummary summary = underTest.restore(db.getSession(), backup, "bar");
290 assertThat(summary.getProfile().getName()).isEqualTo("bar");
291 assertThat(summary.getProfile().getLanguage()).isEqualTo("js");
293 assertThat(reset.calledProfile.getKee()).isEqualTo(summary.getProfile().getKee());
294 assertThat(reset.calledActivations).isEmpty();
298 public void restore_resets_the_activated_rules() {
299 String ruleUuid = db.rules().insert(RuleKey.of("sonarjs", "s001")).getUuid();
300 Reader backup = new StringReader("<?xml version='1.0' encoding='UTF-8'?>" +
301 "<profile><name>foo</name>" +
302 "<language>js</language>" +
305 "<repositoryKey>sonarjs</repositoryKey>" +
307 "<priority>BLOCKER</priority>" +
309 "<parameter><key>bar</key><value>baz</value></parameter>" +
315 underTest.restore(db.getSession(), backup, (String) null);
317 assertThat(reset.calledActivations).hasSize(1);
318 RuleActivation activation = reset.calledActivations.get(0);
319 assertThat(activation.getSeverity()).isEqualTo("BLOCKER");
320 assertThat(activation.getRuleUuid()).isEqualTo(ruleUuid);
321 assertThat(activation.getParameter("bar")).isEqualTo("baz");
325 public void restore_custom_rule() {
326 when(ruleCreator.create(any(), anyList())).then(invocation -> Collections.singletonList(db.rules().insert(RuleKey.of("sonarjs", "s001")).getKey()));
328 Reader backup = new StringReader("<?xml version='1.0' encoding='UTF-8'?>" +
330 "<name>custom rule</name>" +
331 "<language>js</language>" +
333 "<repositoryKey>sonarjs</repositoryKey>" +
335 "<type>CODE_SMELL</type>" +
336 "<priority>CRITICAL</priority>" +
337 "<name>custom rule name</name>" +
338 "<templateKey>rule_mc8</templateKey>" +
339 "<description>custom rule description</description>" +
340 "<parameters><parameter>" +
342 "<value>baz</value>" +
345 "</rule></rules></profile>");
347 underTest.restore(db.getSession(), backup, (String) null);
349 assertThat(reset.calledActivations).hasSize(1);
350 RuleActivation activation = reset.calledActivations.get(0);
351 assertThat(activation.getSeverity()).isEqualTo("CRITICAL");
352 assertThat(activation.getParameter("bar")).isEqualTo("baz");
356 public void restore_skips_rule_without_template_key_and_db_definition() {
357 String ruleUuid = db.rules().insert(RuleKey.of("sonarjs", "s001")).getUuid();
358 Reader backup = new StringReader("<?xml version='1.0' encoding='UTF-8'?>" +
359 "<profile><name>foo</name>" +
360 "<language>js</language>" +
363 "<repositoryKey>sonarjs</repositoryKey>" +
365 "<priority>BLOCKER</priority>" +
367 "<parameter><key>bar</key><value>baz</value></parameter>" +
371 "<repositoryKey>sonarjs</repositoryKey>" +
373 "<priority>MAJOR</priority>" +
378 underTest.restore(db.getSession(), backup, (String) null);
380 assertThat(reset.calledActivations).hasSize(1);
381 RuleActivation activation = reset.calledActivations.get(0);
382 assertThat(activation.getRuleUuid()).isEqualTo(ruleUuid);
383 assertThat(activation.getSeverity()).isEqualTo("BLOCKER");
384 assertThat(activation.getParameter("bar")).isEqualTo("baz");
388 public void copy_profile() {
389 RuleDefinitionDto rule = createRule();
390 RuleParamDto param = db.rules().insertRuleParam(rule);
391 QProfileDto from = createProfile(rule.getLanguage());
392 ActiveRuleDto activeRule = activate(from, rule, param);
394 QProfileDto to = createProfile(rule.getLanguage());
395 underTest.copy(db.getSession(), from, to);
397 assertThat(reset.calledActivations).extracting(RuleActivation::getRuleUuid).containsOnly(activeRule.getRuleUuid());
398 assertThat(reset.calledActivations.get(0).getParameter(param.getName())).isEqualTo("20");
399 assertThat(reset.calledProfile).isEqualTo(to);
403 public void fail_to_restore_if_bad_xml_format() {
404 DbSession session = db.getSession();
405 StringReader backup = new StringReader("<rules><rule></rules>");
406 IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> underTest.restore(session, backup, (String) null));
407 assertThat(thrown).hasMessage("Backup XML is not valid. Root element must be <profile>.");
408 assertThat(reset.calledProfile).isNull();
412 public void fail_to_restore_if_not_xml_backup() {
413 DbSession session = db.getSession();
414 StringReader backup = new StringReader("foo");
415 assertThrows(IllegalArgumentException.class, () -> underTest.restore(session, backup, (String) null));
416 assertThat(reset.calledProfile).isNull();
420 public void fail_to_restore_if_xml_is_not_well_formed() {
421 assertThatThrownBy(() -> {
422 String notWellFormedXml = "<?xml version='1.0' encoding='UTF-8'?><profile><name>\"profil\"</name><language>\"language\"</language><rules/></profile";
424 underTest.restore(db.getSession(), new StringReader(notWellFormedXml), (String) null);
426 .isInstanceOf(IllegalArgumentException.class)
427 .hasMessage("Fail to restore Quality profile backup, XML document is not well formed");
431 public void fail_to_restore_if_duplicate_rule() throws Exception {
432 DbSession session = db.getSession();
433 String xml = Resources.toString(getClass().getResource("QProfileBackuperTest/duplicates-xml-backup.xml"), UTF_8);
434 StringReader backup = new StringReader(xml);
435 IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> underTest.restore(session, backup, (String) null));
436 assertThat(thrown).hasMessage("The quality profile cannot be restored as it contains duplicates for the following rules: xoo:x1, xoo:x2");
437 assertThat(reset.calledProfile).isNull();
441 public void fail_to_restore_external_rule() {
442 db.rules().insert(RuleKey.of("sonarjs", "s001"), r -> r.setIsExternal(true));
443 Reader backup = new StringReader("<?xml version='1.0' encoding='UTF-8'?>" +
444 "<profile><name>foo</name>" +
445 "<language>js</language>" +
448 "<repositoryKey>sonarjs</repositoryKey>" +
450 "<priority>BLOCKER</priority>" +
452 "<parameter><key>bar</key><value>baz</value></parameter>" +
458 assertThatThrownBy(() -> {
459 underTest.restore(db.getSession(), backup, (String) null);
461 .isInstanceOf(IllegalArgumentException.class)
462 .hasMessage("The quality profile cannot be restored as it contains rules from external rule engines: sonarjs:s001");
465 private RuleDefinitionDto createRule() {
466 return db.rules().insert();
469 private QProfileDto createProfile(String language) {
470 return db.qualityProfiles().insert(p -> p.setLanguage(language));
473 private ActiveRuleDto activate(QProfileDto profile, RuleDefinitionDto rule) {
474 return db.qualityProfiles().activateRule(profile, rule);
477 private ActiveRuleDto activate(QProfileDto profile, RuleDefinitionDto rule, RuleParamDto param) {
478 ActiveRuleDto activeRule = db.qualityProfiles().activateRule(profile, rule);
479 ActiveRuleParamDto dto = ActiveRuleParamDto.createFor(param)
481 .setActiveRuleUuid(activeRule.getUuid());
482 db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule, dto);
486 private static class DummyReset implements QProfileReset {
487 private QProfileDto calledProfile;
488 private List<RuleActivation> calledActivations;
491 public BulkChangeResult reset(DbSession dbSession, QProfileDto profile, Collection<RuleActivation> activations) {
492 this.calledProfile = profile;
493 this.calledActivations = new ArrayList<>(activations);
494 return new BulkChangeResult();
498 private static class DummyProfileFactory implements QProfileFactory {
500 public QProfileDto getOrCreateCustom(DbSession dbSession, QProfileName key) {
501 return QualityProfileTesting.newQualityProfileDto()
502 .setLanguage(key.getLanguage())
503 .setName(key.getName());
507 public QProfileDto checkAndCreateCustom(DbSession dbSession, QProfileName name) {
508 throw new UnsupportedOperationException();
512 public QProfileDto createCustom(DbSession dbSession, QProfileName name, @Nullable String parentKey) {
513 throw new UnsupportedOperationException();
517 public void delete(DbSession dbSession, Collection<QProfileDto> profiles) {
518 throw new UnsupportedOperationException();