You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NewRuleCreatorTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. import java.util.HashMap;
  22. import java.util.Map;
  23. import org.jetbrains.annotations.Nullable;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. import org.sonar.api.issue.impact.Severity;
  27. import org.sonar.api.issue.impact.SoftwareQuality;
  28. import org.sonar.api.rule.RuleScope;
  29. import org.sonar.api.rules.CleanCodeAttribute;
  30. import org.sonar.api.rules.RuleType;
  31. import org.sonar.api.server.rule.RulesDefinition;
  32. import org.sonar.api.utils.System2;
  33. import org.sonar.core.util.UuidFactory;
  34. import org.sonar.db.DbClient;
  35. import org.sonar.db.issue.ImpactDto;
  36. import org.sonar.db.rule.RuleDto;
  37. import org.sonar.server.rule.RuleDescriptionSectionsGeneratorResolver;
  38. import static org.assertj.core.api.Assertions.assertThat;
  39. import static org.assertj.core.groups.Tuple.tuple;
  40. import static org.mockito.Mockito.mock;
  41. import static org.mockito.Mockito.when;
  42. import static org.sonar.api.rule.Severity.MAJOR;
  43. public class NewRuleCreatorTest {
  44. private final DbClient dbClient = mock();
  45. private final RuleDescriptionSectionsGeneratorResolver ruleDescriptionSectionsGeneratorResolver = mock();
  46. private final UuidFactory uuidFactory = mock();
  47. private final System2 system2 = mock();
  48. private final RulesRegistrationContext context = mock();
  49. private final NewRuleCreator underTest = new NewRuleCreator(dbClient, ruleDescriptionSectionsGeneratorResolver, uuidFactory, system2);
  50. @Before
  51. public void before() {
  52. when(dbClient.ruleDao()).thenReturn(mock());
  53. }
  54. @Test
  55. public void from_whenRuleDefinitionDoesntHaveCleanCodeAttribute_shouldAlwaysSetCleanCodeAttribute() {
  56. RulesDefinition.Rule ruleDef = getDefaultRule();
  57. RuleDto newRuleDto = underTest.createNewRule(context, ruleDef, mock());
  58. assertThat(newRuleDto.getCleanCodeAttribute()).isEqualTo(CleanCodeAttribute.CONVENTIONAL);
  59. }
  60. @Test
  61. public void from_whenRuleDefinitionDoesHaveCleanCodeAttribute_shouldReturnThisAttribute() {
  62. RulesDefinition.Rule ruleDef = getDefaultRule(CleanCodeAttribute.TESTED);
  63. RuleDto newRuleDto = underTest.createNewRule(context, ruleDef, mock());
  64. assertThat(newRuleDto.getCleanCodeAttribute()).isEqualTo(CleanCodeAttribute.TESTED);
  65. }
  66. @Test
  67. public void createNewRule_whenImpactDefined_shouldReturnThisImpact() {
  68. RulesDefinition.Rule ruleDef = getDefaultRule();
  69. Map<SoftwareQuality, Severity> singleImpact = new HashMap<>();
  70. singleImpact.put(SoftwareQuality.RELIABILITY, Severity.LOW);
  71. when(ruleDef.defaultImpacts()).thenReturn(singleImpact);
  72. RuleDto newRuleDto = underTest.createNewRule(context, ruleDef, mock());
  73. assertThat(newRuleDto.getDefaultImpacts()).extracting(ImpactDto::getSoftwareQuality, ImpactDto::getSeverity)
  74. .containsOnly(tuple(SoftwareQuality.RELIABILITY, Severity.LOW));
  75. }
  76. private static RulesDefinition.Rule getDefaultRule(@Nullable CleanCodeAttribute attribute) {
  77. RulesDefinition.Rule ruleDef = mock(RulesDefinition.Rule.class);
  78. RulesDefinition.Repository repository = mock(RulesDefinition.Repository.class);
  79. when(ruleDef.repository()).thenReturn(repository);
  80. when(ruleDef.key()).thenReturn("key");
  81. when(repository.key()).thenReturn("repoKey");
  82. when(ruleDef.type()).thenReturn(RuleType.CODE_SMELL);
  83. when(ruleDef.scope()).thenReturn(RuleScope.TEST);
  84. when(ruleDef.cleanCodeAttribute()).thenReturn(attribute);
  85. when(ruleDef.severity()).thenReturn(MAJOR);
  86. when(ruleDef.defaultImpacts()).thenReturn(Map.of(SoftwareQuality.MAINTAINABILITY, Severity.LOW));
  87. return ruleDef;
  88. }
  89. private static RulesDefinition.Rule getDefaultRule() {
  90. return getDefaultRule(null);
  91. }
  92. }