3 * Copyright (C) 2009-2023 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.rule.registration;
22 import java.util.HashMap;
24 import org.jetbrains.annotations.Nullable;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.sonar.api.issue.impact.Severity;
28 import org.sonar.api.issue.impact.SoftwareQuality;
29 import org.sonar.api.rule.RuleScope;
30 import org.sonar.api.rules.CleanCodeAttribute;
31 import org.sonar.api.rules.RuleType;
32 import org.sonar.api.server.rule.RulesDefinition;
33 import org.sonar.api.utils.System2;
34 import org.sonar.core.util.UuidFactory;
35 import org.sonar.db.DbClient;
36 import org.sonar.db.issue.ImpactDto;
37 import org.sonar.db.rule.RuleDto;
38 import org.sonar.server.rule.RuleDescriptionSectionsGeneratorResolver;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.assertj.core.groups.Tuple.tuple;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44 import static org.sonar.api.rule.Severity.MAJOR;
46 public class NewRuleCreatorTest {
48 private final DbClient dbClient = mock();
49 private final RuleDescriptionSectionsGeneratorResolver ruleDescriptionSectionsGeneratorResolver = mock();
50 private final UuidFactory uuidFactory = mock();
51 private final System2 system2 = mock();
52 private final RulesRegistrationContext context = mock();
54 private final NewRuleCreator underTest = new NewRuleCreator(dbClient, ruleDescriptionSectionsGeneratorResolver, uuidFactory, system2);
57 public void before() {
58 when(dbClient.ruleDao()).thenReturn(mock());
62 public void from_whenRuleDefinitionDoesntHaveCleanCodeAttribute_shouldAlwaysSetCleanCodeAttribute() {
63 RulesDefinition.Rule ruleDef = getDefaultRule();
65 RuleDto newRuleDto = underTest.createNewRule(context, ruleDef, mock());
67 assertThat(newRuleDto.getCleanCodeAttribute()).isEqualTo(CleanCodeAttribute.CONVENTIONAL);
71 public void from_whenRuleDefinitionDoesHaveCleanCodeAttribute_shouldReturnThisAttribute() {
72 RulesDefinition.Rule ruleDef = getDefaultRule(CleanCodeAttribute.TESTED);
74 RuleDto newRuleDto = underTest.createNewRule(context, ruleDef, mock());
76 assertThat(newRuleDto.getCleanCodeAttribute()).isEqualTo(CleanCodeAttribute.TESTED);
80 public void createNewRule_whenImpactDefined_shouldReturnThisImpact() {
81 RulesDefinition.Rule ruleDef = getDefaultRule();
82 Map<SoftwareQuality, Severity> singleImpact = new HashMap<>();
83 singleImpact.put(SoftwareQuality.RELIABILITY, Severity.LOW);
84 when(ruleDef.defaultImpacts()).thenReturn(singleImpact);
86 RuleDto newRuleDto = underTest.createNewRule(context, ruleDef, mock());
88 assertThat(newRuleDto.getDefaultImpacts()).extracting(ImpactDto::getSoftwareQuality, ImpactDto::getSeverity)
89 .containsOnly(tuple(SoftwareQuality.RELIABILITY, Severity.LOW));
92 private static RulesDefinition.Rule getDefaultRule(@Nullable CleanCodeAttribute attribute) {
93 RulesDefinition.Rule ruleDef = mock(RulesDefinition.Rule.class);
94 RulesDefinition.Repository repository = mock(RulesDefinition.Repository.class);
95 when(ruleDef.repository()).thenReturn(repository);
97 when(ruleDef.key()).thenReturn("key");
98 when(repository.key()).thenReturn("repoKey");
99 when(ruleDef.type()).thenReturn(RuleType.CODE_SMELL);
100 when(ruleDef.scope()).thenReturn(RuleScope.TEST);
101 when(ruleDef.cleanCodeAttribute()).thenReturn(attribute);
102 when(ruleDef.severity()).thenReturn(MAJOR);
103 when(ruleDef.defaultImpacts()).thenReturn(Map.of(SoftwareQuality.MAINTAINABILITY, Severity.LOW));
107 private static RulesDefinition.Rule getDefaultRule() {
108 return getDefaultRule(null);