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;
60 public class QProfileBackuperImplTest {
62 private static final String EMPTY_BACKUP = "<?xml version='1.0' encoding='UTF-8'?>" +
63 "<profile><name>foo</name>" +
64 "<language>js</language>" +
68 private final System2 system2 = new AlwaysIncreasingSystem2();
71 public DbTester db = DbTester.create(system2);
73 private final DummyReset reset = new DummyReset();
74 private final QProfileFactory profileFactory = new DummyProfileFactory();
75 private final RuleCreator ruleCreator = mock(RuleCreator.class);
77 private final QProfileBackuper underTest = new QProfileBackuperImpl(db.getDbClient(), reset, profileFactory, ruleCreator, new QProfileParser());
80 public void backup_generates_xml_file() {
81 RuleDefinitionDto rule = createRule();
82 QProfileDto profile = createProfile(rule.getLanguage());
83 ActiveRuleDto activeRule = activate(profile, rule);
85 StringWriter writer = new StringWriter();
86 underTest.backup(db.getSession(), profile, writer);
88 assertThat(writer).hasToString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
89 "<profile><name>" + profile.getName() + "</name>" +
90 "<language>" + profile.getLanguage() + "</language>" +
93 "<repositoryKey>" + rule.getRepositoryKey() + "</repositoryKey>" +
94 "<key>" + rule.getRuleKey() + "</key>" +
95 "<type>" + RuleType.valueOf(rule.getType()).name() + "</type>" +
96 "<priority>" + activeRule.getSeverityString() + "</priority>" +
97 "<parameters></parameters>" +
104 public void backup_rules_having_parameters() {
105 RuleDefinitionDto rule = createRule();
106 RuleParamDto param = db.rules().insertRuleParam(rule);
107 QProfileDto profile = createProfile(rule.getLanguage());
108 ActiveRuleDto activeRule = activate(profile, rule, param);
110 StringWriter writer = new StringWriter();
111 underTest.backup(db.getSession(), profile, writer);
113 assertThat(writer.toString()).contains(
115 "<repositoryKey>" + rule.getRepositoryKey() + "</repositoryKey>" +
116 "<key>" + rule.getRuleKey() + "</key>" +
117 "<type>" + RuleType.valueOf(rule.getType()).name() + "</type>" +
118 "<priority>" + activeRule.getSeverityString() + "</priority>" +
119 "<parameters><parameter>" +
120 "<key>" + param.getName() + "</key>" +
121 "<value>20</value>" +
122 "</parameter></parameters>" +
127 public void backup_empty_profile() {
128 RuleDefinitionDto rule = createRule();
129 QProfileDto profile = createProfile(rule.getLanguage());
131 StringWriter writer = new StringWriter();
132 underTest.backup(db.getSession(), profile, writer);
134 assertThat(writer).hasToString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
135 "<profile><name>" + profile.getName() + "</name>" +
136 "<language>" + profile.getLanguage() + "</language>" +
142 public void backup_custom_rules_with_params() {
143 RuleDefinitionDto templateRule = db.rules().insert(ruleDefinitionDto -> ruleDefinitionDto
144 .setIsTemplate(true));
145 RuleDefinitionDto rule = db.rules().insert(ruleDefinitionDto -> ruleDefinitionDto
146 .addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(UuidFactoryFast.getInstance().create(), "custom rule description"))
147 .setName("custom rule name")
148 .setStatus(RuleStatus.READY)
149 .setTemplateUuid(templateRule.getUuid()));
150 RuleParamDto param = db.rules().insertRuleParam(rule);
151 QProfileDto profile = createProfile(rule.getLanguage());
152 ActiveRuleDto activeRule = activate(profile, rule, param);
154 StringWriter writer = new StringWriter();
155 underTest.backup(db.getSession(), profile, writer);
157 assertThat(writer).hasToString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
159 "<name>" + profile.getName() + "</name>" +
160 "<language>" + profile.getLanguage() + "</language>" +
162 "<repositoryKey>" + rule.getRepositoryKey() + "</repositoryKey>" +
163 "<key>" + rule.getKey().rule() + "</key>" +
164 "<type>" + RuleType.valueOf(rule.getType()) + "</type>" +
165 "<priority>" + activeRule.getSeverityString() + "</priority>" +
166 "<name>" + rule.getName() + "</name>" +
167 "<templateKey>" + templateRule.getKey().rule() + "</templateKey>" +
168 "<description>" + rule.getDefaultRuleDescriptionSectionDto().getDescription() + "</description>" +
169 "<parameters><parameter>" +
170 "<key>" + param.getName() + "</key>" +
171 "<value>20</value>" +
172 "</parameter></parameters>" +
173 "</rule></rules></profile>");
177 public void restore_backup_on_the_profile_specified_in_backup() {
178 Reader backup = new StringReader(EMPTY_BACKUP);
180 QProfileRestoreSummary summary = underTest.restore(db.getSession(), backup, (String) null);
182 assertThat(summary.getProfile().getName()).isEqualTo("foo");
183 assertThat(summary.getProfile().getLanguage()).isEqualTo("js");
185 assertThat(reset.calledProfile.getKee()).isEqualTo(summary.getProfile().getKee());
186 assertThat(reset.calledActivations).isEmpty();
190 public void restore_detects_deprecated_rule_keys() {
191 String ruleUuid = db.rules().insert(RuleKey.of("sonarjs", "s001")).getUuid();
192 db.rules().insertDeprecatedKey(c -> c.setRuleUuid(ruleUuid).setOldRuleKey("oldkey").setOldRepositoryKey("oldrepo"));
194 Reader backup = new StringReader("<?xml version='1.0' encoding='UTF-8'?>" +
195 "<profile><name>foo</name>" +
196 "<language>js</language>" +
199 "<repositoryKey>oldrepo</repositoryKey>" +
200 "<key>oldkey</key>" +
201 "<priority>BLOCKER</priority>" +
203 "<parameter><key>bar</key><value>baz</value></parameter>" +
209 underTest.restore(db.getSession(), backup, (String) null);
211 assertThat(reset.calledActivations).hasSize(1);
212 RuleActivation activation = reset.calledActivations.get(0);
213 assertThat(activation.getSeverity()).isEqualTo("BLOCKER");
214 assertThat(activation.getRuleUuid()).isEqualTo(ruleUuid);
215 assertThat(activation.getParameter("bar")).isEqualTo("baz");
219 public void restore_ignores_deprecated_rule_keys_if_new_key_is_already_present() {
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>MAJOR</priority>" +
232 "<parameter><key>bar</key><value>baz</value></parameter>" +
236 "<repositoryKey>sonarjs</repositoryKey>" +
238 "<priority>BLOCKER</priority>" +
240 "<parameter><key>bar2</key><value>baz2</value></parameter>" +
246 underTest.restore(db.getSession(), backup, (String) null);
248 assertThat(reset.calledActivations).hasSize(1);
249 RuleActivation activation = reset.calledActivations.get(0);
250 assertThat(activation.getSeverity()).isEqualTo("BLOCKER");
251 assertThat(activation.getRuleUuid()).isEqualTo(ruleUuid);
252 assertThat(activation.getParameter("bar2")).isEqualTo("baz2");
256 public void restore_backup_on_profile_having_different_name() {
257 Reader backup = new StringReader(EMPTY_BACKUP);
259 QProfileRestoreSummary summary = underTest.restore(db.getSession(), backup, "bar");
261 assertThat(summary.getProfile().getName()).isEqualTo("bar");
262 assertThat(summary.getProfile().getLanguage()).isEqualTo("js");
264 assertThat(reset.calledProfile.getKee()).isEqualTo(summary.getProfile().getKee());
265 assertThat(reset.calledActivations).isEmpty();
269 public void restore_resets_the_activated_rules() {
270 String ruleUuid = db.rules().insert(RuleKey.of("sonarjs", "s001")).getUuid();
271 Reader backup = new StringReader("<?xml version='1.0' encoding='UTF-8'?>" +
272 "<profile><name>foo</name>" +
273 "<language>js</language>" +
276 "<repositoryKey>sonarjs</repositoryKey>" +
278 "<priority>BLOCKER</priority>" +
280 "<parameter><key>bar</key><value>baz</value></parameter>" +
286 underTest.restore(db.getSession(), backup, (String) null);
288 assertThat(reset.calledActivations).hasSize(1);
289 RuleActivation activation = reset.calledActivations.get(0);
290 assertThat(activation.getSeverity()).isEqualTo("BLOCKER");
291 assertThat(activation.getRuleUuid()).isEqualTo(ruleUuid);
292 assertThat(activation.getParameter("bar")).isEqualTo("baz");
296 public void restore_custom_rule() {
297 when(ruleCreator.create(any(), anyList())).then(invocation -> Collections.singletonList(db.rules().insert(RuleKey.of("sonarjs", "s001")).getKey()));
299 Reader backup = new StringReader("<?xml version='1.0' encoding='UTF-8'?>" +
301 "<name>custom rule</name>" +
302 "<language>js</language>" +
304 "<repositoryKey>sonarjs</repositoryKey>" +
306 "<type>CODE_SMELL</type>" +
307 "<priority>CRITICAL</priority>" +
308 "<name>custom rule name</name>" +
309 "<templateKey>rule_mc8</templateKey>" +
310 "<description>custom rule description</description>" +
311 "<parameters><parameter>" +
313 "<value>baz</value>" +
316 "</rule></rules></profile>");
318 underTest.restore(db.getSession(), backup, (String) null);
320 assertThat(reset.calledActivations).hasSize(1);
321 RuleActivation activation = reset.calledActivations.get(0);
322 assertThat(activation.getSeverity()).isEqualTo("CRITICAL");
323 assertThat(activation.getParameter("bar")).isEqualTo("baz");
327 public void restore_skips_rule_without_template_key_and_db_definition() {
328 String ruleUuid = db.rules().insert(RuleKey.of("sonarjs", "s001")).getUuid();
329 Reader backup = new StringReader("<?xml version='1.0' encoding='UTF-8'?>" +
330 "<profile><name>foo</name>" +
331 "<language>js</language>" +
334 "<repositoryKey>sonarjs</repositoryKey>" +
336 "<priority>BLOCKER</priority>" +
338 "<parameter><key>bar</key><value>baz</value></parameter>" +
342 "<repositoryKey>sonarjs</repositoryKey>" +
344 "<priority>MAJOR</priority>" +
349 underTest.restore(db.getSession(), backup, (String) null);
351 assertThat(reset.calledActivations).hasSize(1);
352 RuleActivation activation = reset.calledActivations.get(0);
353 assertThat(activation.getRuleUuid()).isEqualTo(ruleUuid);
354 assertThat(activation.getSeverity()).isEqualTo("BLOCKER");
355 assertThat(activation.getParameter("bar")).isEqualTo("baz");
359 public void copy_profile() {
360 RuleDefinitionDto rule = createRule();
361 RuleParamDto param = db.rules().insertRuleParam(rule);
362 QProfileDto from = createProfile(rule.getLanguage());
363 ActiveRuleDto activeRule = activate(from, rule, param);
365 QProfileDto to = createProfile(rule.getLanguage());
366 underTest.copy(db.getSession(), from, to);
368 assertThat(reset.calledActivations).extracting(RuleActivation::getRuleUuid).containsOnly(activeRule.getRuleUuid());
369 assertThat(reset.calledActivations.get(0).getParameter(param.getName())).isEqualTo("20");
370 assertThat(reset.calledProfile).isEqualTo(to);
374 public void fail_to_restore_if_bad_xml_format() {
375 DbSession session = db.getSession();
376 StringReader backup = new StringReader("<rules><rule></rules>");
377 IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> underTest.restore(session, backup, (String) null));
378 assertThat(thrown).hasMessage("Backup XML is not valid. Root element must be <profile>.");
379 assertThat(reset.calledProfile).isNull();
383 public void fail_to_restore_if_not_xml_backup() {
384 DbSession session = db.getSession();
385 StringReader backup = new StringReader("foo");
386 assertThrows(IllegalArgumentException.class, () -> underTest.restore(session, backup, (String) null));
387 assertThat(reset.calledProfile).isNull();
391 public void fail_to_restore_if_xml_is_not_well_formed() {
392 assertThatThrownBy(() -> {
393 String notWellFormedXml = "<?xml version='1.0' encoding='UTF-8'?><profile><name>\"profil\"</name><language>\"language\"</language><rules/></profile";
395 underTest.restore(db.getSession(), new StringReader(notWellFormedXml), (String) null);
397 .isInstanceOf(IllegalArgumentException.class)
398 .hasMessage("Fail to restore Quality profile backup, XML document is not well formed");
402 public void fail_to_restore_if_duplicate_rule() throws Exception {
403 DbSession session = db.getSession();
404 String xml = Resources.toString(getClass().getResource("QProfileBackuperTest/duplicates-xml-backup.xml"), UTF_8);
405 StringReader backup = new StringReader(xml);
406 IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> underTest.restore(session, backup, (String) null));
407 assertThat(thrown).hasMessage("The quality profile cannot be restored as it contains duplicates for the following rules: xoo:x1, xoo:x2");
408 assertThat(reset.calledProfile).isNull();
412 public void fail_to_restore_external_rule() {
413 db.rules().insert(RuleKey.of("sonarjs", "s001"), r -> r.setIsExternal(true));
414 Reader backup = new StringReader("<?xml version='1.0' encoding='UTF-8'?>" +
415 "<profile><name>foo</name>" +
416 "<language>js</language>" +
419 "<repositoryKey>sonarjs</repositoryKey>" +
421 "<priority>BLOCKER</priority>" +
423 "<parameter><key>bar</key><value>baz</value></parameter>" +
429 assertThatThrownBy(() -> {
430 underTest.restore(db.getSession(), backup, (String) null);
432 .isInstanceOf(IllegalArgumentException.class)
433 .hasMessage("The quality profile cannot be restored as it contains rules from external rule engines: sonarjs:s001");
436 private RuleDefinitionDto createRule() {
437 return db.rules().insert();
440 private QProfileDto createProfile(String language) {
441 return db.qualityProfiles().insert(p -> p.setLanguage(language));
444 private ActiveRuleDto activate(QProfileDto profile, RuleDefinitionDto rule) {
445 return db.qualityProfiles().activateRule(profile, rule);
448 private ActiveRuleDto activate(QProfileDto profile, RuleDefinitionDto rule, RuleParamDto param) {
449 ActiveRuleDto activeRule = db.qualityProfiles().activateRule(profile, rule);
450 ActiveRuleParamDto dto = ActiveRuleParamDto.createFor(param)
452 .setActiveRuleUuid(activeRule.getUuid());
453 db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule, dto);
457 private static class DummyReset implements QProfileReset {
458 private QProfileDto calledProfile;
459 private List<RuleActivation> calledActivations;
462 public BulkChangeResult reset(DbSession dbSession, QProfileDto profile, Collection<RuleActivation> activations) {
463 this.calledProfile = profile;
464 this.calledActivations = new ArrayList<>(activations);
465 return new BulkChangeResult();
469 private static class DummyProfileFactory implements QProfileFactory {
471 public QProfileDto getOrCreateCustom(DbSession dbSession, QProfileName key) {
472 return QualityProfileTesting.newQualityProfileDto()
473 .setLanguage(key.getLanguage())
474 .setName(key.getName());
478 public QProfileDto checkAndCreateCustom(DbSession dbSession, QProfileName name) {
479 throw new UnsupportedOperationException();
483 public QProfileDto createCustom(DbSession dbSession, QProfileName name, @Nullable String parentKey) {
484 throw new UnsupportedOperationException();
488 public void delete(DbSession dbSession, Collection<QProfileDto> profiles) {
489 throw new UnsupportedOperationException();