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.

QualityProfileExportDaoIT.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.db.qualityprofile;
  21. import com.google.common.collect.Sets;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import java.util.Optional;
  25. import java.util.stream.Collectors;
  26. import java.util.stream.IntStream;
  27. import java.util.stream.Stream;
  28. import javax.annotation.Nullable;
  29. import org.junit.Assert;
  30. import org.junit.Rule;
  31. import org.junit.Test;
  32. import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
  33. import org.sonar.api.rule.RuleStatus;
  34. import org.sonar.api.rules.RuleType;
  35. import org.sonar.db.DbSession;
  36. import org.sonar.db.DbTester;
  37. import org.sonar.db.rule.RuleDto;
  38. import org.sonar.db.rule.RuleParamDto;
  39. import static org.assertj.core.api.Assertions.assertThat;
  40. public class QualityProfileExportDaoIT {
  41. @Rule
  42. public DbTester db = DbTester.create(new AlwaysIncreasingSystem2());
  43. private final DbSession dbSession = db.getSession();
  44. private final QualityProfileExportDao underTest = db.getDbClient().qualityProfileExportDao();
  45. @Test
  46. public void selectRulesByProfile_ready_rules_only() {
  47. String language = "java";
  48. RuleDto rule1 = createRule(language);
  49. RuleDto rule2 = createRule(language);
  50. RuleDto rule3 = createRule(language);
  51. createRule(language, RuleStatus.REMOVED);
  52. QProfileDto profile = createProfile(language);
  53. activate(profile, rule1, rule2, rule3);
  54. List<ExportRuleDto> results = underTest.selectRulesByProfile(dbSession, profile);
  55. assertThat(results).isNotNull();
  56. assertThat(results)
  57. .extracting("ruleKey")
  58. .containsOnly(rule1.getKey(), rule2.getKey(), rule3.getKey());
  59. }
  60. @Test
  61. public void selectRulesByProfile_verify_columns() {
  62. String language = "java";
  63. RuleDto ruleTemplate = createRule(language);
  64. RuleDto customRule = createRule(language, RuleStatus.READY, ruleTemplate.getUuid());
  65. customRule.setNoteData("Extended description")
  66. .setTags(Sets.newHashSet("tag1", "tag2", "tag3"));
  67. db.rules().update(customRule);
  68. var customRuleContent = customRule.getDefaultRuleDescriptionSection().getContent();
  69. RuleDto rule = createRule(language, RuleStatus.READY, null);
  70. var ruleContent = rule.getDefaultRuleDescriptionSection().getContent();
  71. QProfileDto profile = createProfile(language);
  72. List<ActiveRuleDto> activeRules = activate(profile, customRule, rule);
  73. List<ExportRuleDto> results = underTest.selectRulesByProfile(dbSession, profile);
  74. assertThat(results)
  75. .isNotNull()
  76. .isNotEmpty();
  77. // verify custom rule
  78. ExportRuleDto exportCustomRuleDto = results.stream().filter(ExportRuleDto::isCustomRule).findFirst().get();
  79. assertThat(exportCustomRuleDto).isNotNull();
  80. assertThat(exportCustomRuleDto.isCustomRule()).isTrue();
  81. assertThat(exportCustomRuleDto.getParams()).isEmpty();
  82. assertThat(exportCustomRuleDto.getDescriptionOrThrow()).isEqualTo(customRuleContent);
  83. assertThat(exportCustomRuleDto.getExtendedDescription()).isEqualTo(customRule.getNoteData());
  84. assertThat(exportCustomRuleDto.getName()).isEqualTo(customRule.getName());
  85. assertThat(exportCustomRuleDto.getRuleKey()).isEqualTo(customRule.getKey());
  86. assertThat(exportCustomRuleDto.getRuleType()).isEqualTo(RuleType.valueOf(customRule.getType()));
  87. assertThat(exportCustomRuleDto.getTags()).isEqualTo(String.join(",", customRule.getTags()));
  88. assertThat(exportCustomRuleDto.getTemplateRuleKey()).isEqualTo(ruleTemplate.getKey());
  89. ActiveRuleDto activeCustomRule = activeRules.stream().filter(activeRuleDto -> activeRuleDto.getRuleKey().equals(customRule.getKey())).findFirst().get();
  90. assertThat(exportCustomRuleDto.getSeverityString()).isEqualTo(activeCustomRule.getSeverityString());
  91. // verify regular rule
  92. ExportRuleDto exportRuleDto = results.stream().filter(regularRule -> !regularRule.isCustomRule()).findFirst().get();
  93. assertThat(exportRuleDto).isNotNull();
  94. assertThat(exportRuleDto.isCustomRule()).isFalse();
  95. assertThat(exportRuleDto.getParams()).isEmpty();
  96. assertThat(exportRuleDto.getDescriptionOrThrow()).isEqualTo(ruleContent);
  97. assertThat(exportRuleDto.getExtendedDescription()).isEqualTo(rule.getNoteData());
  98. assertThat(exportRuleDto.getName()).isEqualTo(rule.getName());
  99. assertThat(exportRuleDto.getRuleKey()).isEqualTo(rule.getKey());
  100. assertThat(exportRuleDto.getRuleType()).isEqualTo(RuleType.valueOf(rule.getType()));
  101. ActiveRuleDto activeRule = activeRules.stream().filter(activeRuleDto -> activeRuleDto.getRuleKey().equals(rule.getKey())).findFirst().get();
  102. assertThat(exportRuleDto.getSeverityString()).isEqualTo(activeRule.getSeverityString());
  103. }
  104. @Test
  105. public void selectRulesByProfile_verify_rows_over_1000() {
  106. String language = "java";
  107. int numberOfParamsToCreate = 1005;
  108. RuleDto rule = createRule(language);
  109. List<RuleParamDto> ruleParams = addParamsToRule(rule, numberOfParamsToCreate);
  110. QProfileDto profile = createProfile(language);
  111. ActiveRuleDto activatedRule = activate(profile, rule, ruleParams);
  112. List<ExportRuleDto> results = underTest.selectRulesByProfile(dbSession, profile);
  113. assertThat(results)
  114. .extracting("activeRuleUuid")
  115. .containsOnly(activatedRule.getUuid());
  116. assertThat(results.get(0).getParams())
  117. .extracting("key")
  118. .containsOnly(ruleParams.stream().map(RuleParamDto::getName).toArray());
  119. }
  120. @Test
  121. public void selectRulesByProfile_params_assigned_correctly() {
  122. String language = "java";
  123. RuleDto firstRule = createRule(language);
  124. List<RuleParamDto> ruleParamsOfFirstRule = addParamsToRule(firstRule, 2);
  125. RuleDto secondRule = createRule(language);
  126. List<RuleParamDto> ruleParamsOfSecondRule = addParamsToRule(secondRule, 3);
  127. String otherLanguage = "js";
  128. RuleDto thirdRule = createRule(otherLanguage);
  129. List<RuleParamDto> ruleParamsOfThirdRule = addParamsToRule(thirdRule, 4);
  130. QProfileDto profile = createProfile(language);
  131. QProfileDto otherProfile = createProfile(otherLanguage);
  132. ActiveRuleDto firstActivatedRule = activate(profile, firstRule, ruleParamsOfFirstRule);
  133. ActiveRuleDto secondActivatedRule = activate(profile, secondRule, ruleParamsOfSecondRule);
  134. ActiveRuleDto thirdActivatedRule = activate(otherProfile, thirdRule, ruleParamsOfThirdRule);
  135. List<ExportRuleDto> results = underTest.selectRulesByProfile(dbSession, profile);
  136. List<ExportRuleDto> otherProfileResults = underTest.selectRulesByProfile(dbSession, otherProfile);
  137. assertThat(results)
  138. .extracting("activeRuleUuid")
  139. .containsOnly(firstActivatedRule.getUuid(), secondActivatedRule.getUuid());
  140. assertThat(otherProfileResults)
  141. .extracting("activeRuleUuid")
  142. .containsOnly(thirdActivatedRule.getUuid());
  143. ExportRuleDto firstExportedRule = findExportedRuleByUuid(firstActivatedRule.getUuid(), results);
  144. assertThat(firstExportedRule.getParams())
  145. .extracting("key")
  146. .containsOnly(ruleParamsOfFirstRule.stream().map(RuleParamDto::getName).toArray());
  147. ExportRuleDto secondExportedRule = findExportedRuleByUuid(secondActivatedRule.getUuid(), results);
  148. assertThat(secondExportedRule.getParams())
  149. .extracting("key")
  150. .containsOnly(ruleParamsOfSecondRule.stream().map(RuleParamDto::getName).toArray());
  151. ExportRuleDto thirdExportedRule = findExportedRuleByUuid(thirdActivatedRule.getUuid(), otherProfileResults);
  152. assertThat(thirdExportedRule.getParams())
  153. .extracting("key")
  154. .containsOnly(ruleParamsOfThirdRule.stream().map(RuleParamDto::getName).toArray());
  155. }
  156. private ExportRuleDto findExportedRuleByUuid(String uuid, List<ExportRuleDto> results) {
  157. Optional<ExportRuleDto> found = results.stream().filter(exportRuleDto -> uuid.equals(exportRuleDto.getActiveRuleUuid())).findFirst();
  158. if (!found.isPresent()) {
  159. Assert.fail();
  160. }
  161. return found.get();
  162. }
  163. private List<RuleParamDto> addParamsToRule(RuleDto firstRule, int numberOfParams) {
  164. return IntStream.range(0, numberOfParams)
  165. .mapToObj(value -> db.rules().insertRuleParam(firstRule,
  166. ruleParamDto -> ruleParamDto.setName("name_" + firstRule.getUuid() + "_" + value)))
  167. .toList();
  168. }
  169. private RuleDto createRule(String language) {
  170. return createRule(language, RuleStatus.READY);
  171. }
  172. private RuleDto createRule(String language, RuleStatus status) {
  173. return createRule(language, status, null);
  174. }
  175. private RuleDto createRule(String language, RuleStatus status, @Nullable String templateUuid) {
  176. return db.rules().insert(ruleDefinitionDto -> ruleDefinitionDto.setRepositoryKey("repoKey").setLanguage(language).setStatus(status)
  177. .setTemplateUuid(templateUuid));
  178. }
  179. private QProfileDto createProfile(String lanugage) {
  180. return db.qualityProfiles().insert(p -> p.setLanguage(lanugage));
  181. }
  182. private List<ActiveRuleDto> activate(QProfileDto profile, RuleDto... rules) {
  183. return Stream.of(rules)
  184. .map(ruleDefinitionDto -> db.qualityProfiles().activateRule(profile, ruleDefinitionDto))
  185. .toList();
  186. }
  187. private ActiveRuleDto activate(QProfileDto profile, RuleDto rule, Collection<RuleParamDto> params) {
  188. ActiveRuleDto activeRule = db.qualityProfiles().activateRule(profile, rule);
  189. params.forEach(ruleParamDto -> {
  190. ActiveRuleParamDto dto = ActiveRuleParamDto.createFor(ruleParamDto)
  191. .setKey(ruleParamDto.getName())
  192. .setValue("20")
  193. .setActiveRuleUuid(activeRule.getUuid());
  194. db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule, dto);
  195. });
  196. return activeRule;
  197. }
  198. }