]> source.dussan.org Git - sonarqube.git/blob
d16a6f9122c0ceffc44cf979de864c510fb7df83
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.rule.registration;
21
22 import java.util.HashMap;
23 import java.util.Map;
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;
39
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;
45
46 public class NewRuleCreatorTest {
47
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();
53
54   private final NewRuleCreator underTest = new NewRuleCreator(dbClient, ruleDescriptionSectionsGeneratorResolver, uuidFactory, system2);
55
56   @Before
57   public void before() {
58     when(dbClient.ruleDao()).thenReturn(mock());
59   }
60
61   @Test
62   public void from_whenRuleDefinitionDoesntHaveCleanCodeAttribute_shouldAlwaysSetCleanCodeAttribute() {
63     RulesDefinition.Rule ruleDef = getDefaultRule();
64
65     RuleDto newRuleDto = underTest.createNewRule(context, ruleDef, mock());
66
67     assertThat(newRuleDto.getCleanCodeAttribute()).isEqualTo(CleanCodeAttribute.CONVENTIONAL);
68   }
69
70   @Test
71   public void from_whenRuleDefinitionDoesHaveCleanCodeAttribute_shouldReturnThisAttribute() {
72     RulesDefinition.Rule ruleDef = getDefaultRule(CleanCodeAttribute.TESTED);
73
74     RuleDto newRuleDto = underTest.createNewRule(context, ruleDef, mock());
75
76     assertThat(newRuleDto.getCleanCodeAttribute()).isEqualTo(CleanCodeAttribute.TESTED);
77   }
78
79   @Test
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);
85
86     RuleDto newRuleDto = underTest.createNewRule(context, ruleDef, mock());
87
88     assertThat(newRuleDto.getDefaultImpacts()).extracting(ImpactDto::getSoftwareQuality, ImpactDto::getSeverity)
89       .containsOnly(tuple(SoftwareQuality.RELIABILITY, Severity.LOW));
90   }
91
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);
96
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));
104     return ruleDef;
105   }
106
107   private static RulesDefinition.Rule getDefaultRule() {
108     return getDefaultRule(null);
109   }
110 }