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.

DefaultRuleFinderTest.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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;
  21. import com.google.common.collect.ImmutableSet;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import org.sonar.api.rule.RuleKey;
  25. import org.sonar.api.rule.RuleStatus;
  26. import org.sonar.api.rules.Rule;
  27. import org.sonar.api.rules.RuleQuery;
  28. import org.sonar.api.utils.System2;
  29. import org.sonar.db.DbClient;
  30. import org.sonar.db.DbSession;
  31. import org.sonar.db.DbTester;
  32. import org.sonar.db.rule.RuleDto;
  33. import org.sonar.db.rule.RuleDto.Scope;
  34. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. public class DefaultRuleFinderTest {
  37. @org.junit.Rule
  38. public DbTester dbTester = DbTester.create(System2.INSTANCE);
  39. private final DbClient dbClient = dbTester.getDbClient();
  40. private final DbSession session = dbTester.getSession();
  41. private final RuleDto rule1 = new RuleDto()
  42. .setName("Check Header")
  43. .setConfigKey("Checker/Treewalker/HeaderCheck")
  44. .setRuleKey("com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck")
  45. .setRepositoryKey("checkstyle")
  46. .setSeverity(4)
  47. .setScope(Scope.MAIN)
  48. .setStatus(RuleStatus.READY);
  49. private final RuleDto rule2 = new RuleDto()
  50. .setName("Disabled checked")
  51. .setConfigKey("Checker/Treewalker/DisabledCheck")
  52. .setRuleKey("DisabledCheck")
  53. .setRepositoryKey("checkstyle")
  54. .setSeverity(4)
  55. .setScope(Scope.MAIN)
  56. .setStatus(RuleStatus.REMOVED);
  57. private final RuleDto rule3 = new RuleDto()
  58. .setName("Check Annotation")
  59. .setConfigKey("Checker/Treewalker/AnnotationUseStyleCheck")
  60. .setRuleKey("com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck")
  61. .setRepositoryKey("checkstyle")
  62. .setSeverity(4)
  63. .setScope(Scope.MAIN)
  64. .setStatus(RuleStatus.READY);
  65. private final RuleDto rule4 = new RuleDto()
  66. .setName("Call Super First")
  67. .setConfigKey("rulesets/android.xml/CallSuperFirst")
  68. .setRuleKey("CallSuperFirst")
  69. .setRepositoryKey("pmd")
  70. .setSeverity(2)
  71. .setScope(Scope.MAIN)
  72. .setStatus(RuleStatus.READY);
  73. private final DefaultRuleFinder underTest = new DefaultRuleFinder(dbClient);
  74. @Before
  75. public void setup() {
  76. dbTester.rules().insertRule(rule1);
  77. dbTester.rules().insertRule(rule2);
  78. dbTester.rules().insertRule(rule3);
  79. dbTester.rules().insertRule(rule4);
  80. session.commit();
  81. }
  82. @Test
  83. public void should_success_finder_wrap() {
  84. // should_find_by_key
  85. Rule rule = underTest.findByKey("checkstyle", "com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck");
  86. assertThat(rule).isNotNull();
  87. assertThat(rule.getKey()).isEqualTo(("com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"));
  88. assertThat(rule.isEnabled()).isTrue();
  89. // find_should_return_null_if_no_results
  90. assertThat(underTest.findByKey("checkstyle", "unknown")).isNull();
  91. assertThat(underTest.find(RuleQuery.create().withRepositoryKey("checkstyle").withConfigKey("unknown"))).isNull();
  92. // find_repository_rules
  93. assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey("checkstyle"))).hasSize(2);
  94. // find_all_enabled
  95. assertThat(underTest.findAll(RuleQuery.create())).extracting(Rule::ruleKey).containsOnly(rule1.getKey(), rule3.getKey(), rule4.getKey());
  96. // find_all
  97. assertThat(underTest.findAll()).extracting(RuleDto::getRuleKey).containsOnly(rule1.getKey().rule(), rule3.getKey().rule(), rule4.getKey().rule());
  98. // do_not_find_disabled_rules
  99. assertThat(underTest.findByKey("checkstyle", "DisabledCheck")).isNull();
  100. // do_not_find_unknown_rules
  101. assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey("unknown_repository"))).isEmpty();
  102. assertThat(underTest.findDtoByKey(RuleKey.of("pmd", "CallSuperFirst")).get().getUuid()).isEqualTo(rule4.getUuid());
  103. assertThat(underTest.findDtoByUuid(rule4.getUuid())).isPresent();
  104. }
  105. @Test
  106. public void should_fail_find() {
  107. assertThat(underTest.findDtoByKey(RuleKey.of("pmd", "unknown"))).isEmpty();
  108. assertThat(underTest.findDtoByUuid("unknown")).isEmpty();
  109. }
  110. @Test
  111. public void find_all_not_include_removed_rule() {
  112. // rule 3 is REMOVED
  113. assertThat(underTest.findAll(RuleQuery.create())).extracting(Rule::ruleKey).containsOnly(rule1.getKey(), rule3.getKey(), rule4.getKey());
  114. assertThat(underTest.findAll()).extracting(RuleDto::getRuleKey).containsOnly(rule1.getKey().rule(), rule3.getKey().rule(), rule4.getKey().rule());
  115. }
  116. @Test
  117. public void findByKey_populates_system_tags_but_not_tags() {
  118. RuleDto ruleDto = dbTester.rules()
  119. .insert(t -> t.setSystemTags(ImmutableSet.of(randomAlphanumeric(5), randomAlphanumeric(6))));
  120. dbTester.rules().insertRule();
  121. Rule rule = underTest.findByKey(ruleDto.getKey());
  122. assertThat(rule.getSystemTags())
  123. .containsOnlyElementsOf(ruleDto.getSystemTags());
  124. assertThat(rule.getTags()).isEmpty();
  125. rule = underTest.findByKey(ruleDto.getRepositoryKey(), ruleDto.getRuleKey());
  126. assertThat(rule.getSystemTags())
  127. .containsOnlyElementsOf(ruleDto.getSystemTags());
  128. assertThat(rule.getTags()).isEmpty();
  129. }
  130. }