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.

RuleDaoIT.java 55KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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.rule;
  21. import com.google.common.collect.ImmutableSet;
  22. import com.google.common.collect.MoreCollectors;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.Collections;
  26. import java.util.Iterator;
  27. import java.util.List;
  28. import java.util.Optional;
  29. import java.util.Set;
  30. import java.util.function.Consumer;
  31. import java.util.function.Function;
  32. import org.apache.commons.lang.RandomStringUtils;
  33. import org.apache.ibatis.exceptions.PersistenceException;
  34. import org.jetbrains.annotations.NotNull;
  35. import org.junit.Rule;
  36. import org.junit.Test;
  37. import org.sonar.api.issue.impact.SoftwareQuality;
  38. import org.sonar.api.rule.RuleKey;
  39. import org.sonar.api.rule.RuleStatus;
  40. import org.sonar.api.rule.Severity;
  41. import org.sonar.api.rules.CleanCodeAttribute;
  42. import org.sonar.api.rules.RuleQuery;
  43. import org.sonar.api.rules.RuleType;
  44. import org.sonar.api.server.debt.DebtRemediationFunction;
  45. import org.sonar.api.utils.DateUtils;
  46. import org.sonar.api.utils.System2;
  47. import org.sonar.core.util.UuidFactoryFast;
  48. import org.sonar.db.DbSession;
  49. import org.sonar.db.DbTester;
  50. import org.sonar.db.RowNotFoundException;
  51. import org.sonar.db.issue.ImpactDto;
  52. import org.sonar.db.rule.RuleDto.Scope;
  53. import static com.google.common.collect.Sets.newHashSet;
  54. import static java.util.Arrays.asList;
  55. import static java.util.Collections.emptyList;
  56. import static java.util.Collections.singletonList;
  57. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  58. import static org.assertj.core.api.Assertions.assertThat;
  59. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  60. import static org.assertj.core.api.Assertions.tuple;
  61. import static org.sonar.api.issue.impact.SoftwareQuality.MAINTAINABILITY;
  62. import static org.sonar.api.issue.impact.SoftwareQuality.RELIABILITY;
  63. import static org.sonar.api.issue.impact.SoftwareQuality.SECURITY;
  64. import static org.sonar.api.rule.RuleStatus.*;
  65. import static org.sonar.api.rule.RuleStatus.DEPRECATED;
  66. import static org.sonar.api.rule.RuleStatus.REMOVED;
  67. public class RuleDaoIT {
  68. private static final String UNKNOWN_RULE_UUID = "unknown-uuid";
  69. @Rule
  70. public DbTester db = DbTester.create(System2.INSTANCE);
  71. private final RuleDao underTest = db.getDbClient().ruleDao();
  72. @Test
  73. public void selectByKey() {
  74. RuleDto ruleDto = db.rules().insert(r -> r.addDefaultImpact(newRuleDefaultImpact(SECURITY, org.sonar.api.issue.impact.Severity.LOW)));
  75. assertThat(underTest.selectByKey(db.getSession(), RuleKey.of("foo", "bar")))
  76. .isEmpty();
  77. RuleDto actualRule = underTest.selectByKey(db.getSession(), ruleDto.getKey()).get();
  78. assertEquals(actualRule, ruleDto);
  79. assertThat(actualRule.getDefaultImpacts())
  80. .extracting(ImpactDto::getSoftwareQuality, ImpactDto::getSeverity)
  81. .containsExactlyInAnyOrder(
  82. tuple(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH),
  83. tuple(SECURITY, org.sonar.api.issue.impact.Severity.LOW));
  84. }
  85. @Test
  86. public void selectByKey_return_rule() {
  87. RuleDto ruleDto = db.rules().insert();
  88. assertThat(underTest.selectByKey(db.getSession(), ruleDto.getKey())).isNotEmpty();
  89. }
  90. @Test
  91. public void selectByUuid() {
  92. RuleDto ruleDto = db.rules().insert();
  93. assertThat(underTest.selectByUuid(ruleDto.getUuid() + 500, db.getSession())).isEmpty();
  94. RuleDto actualRule = underTest.selectByUuid(ruleDto.getUuid(), db.getSession()).get();
  95. assertEquals(actualRule, ruleDto);
  96. }
  97. @Test
  98. public void selectByUuidWithDifferentValuesOfBooleans() {
  99. for (int i = 0; i < 3; i++) {
  100. int indexBoolean = i;
  101. RuleDto ruleDto = db.rules().insert((ruleDto1 -> {
  102. ruleDto1.setIsTemplate(indexBoolean == 0);
  103. ruleDto1.setIsExternal(indexBoolean == 1);
  104. ruleDto1.setIsAdHoc(indexBoolean == 2);
  105. }));
  106. assertThat(underTest.selectByUuid(ruleDto.getUuid() + 500, db.getSession())).isEmpty();
  107. RuleDto rule = underTest.selectByUuid(ruleDto.getUuid(), db.getSession()).get();
  108. assertEquals(rule, ruleDto);
  109. }
  110. }
  111. @Test
  112. public void selectDefinitionByUuid() {
  113. RuleDto rule = db.rules().insert();
  114. assertThat(underTest.selectByUuid(UNKNOWN_RULE_UUID, db.getSession())).isEmpty();
  115. Optional<RuleDto> ruleDtoOptional = underTest.selectByUuid(rule.getUuid(), db.getSession());
  116. assertThat(ruleDtoOptional).isPresent();
  117. }
  118. @Test
  119. public void selectByUuids() {
  120. RuleDto rule1 = db.rules().insert();
  121. RuleDto rule2 = db.rules().insert();
  122. RuleDto removedRule = db.rules().insert(r -> r.setStatus(REMOVED));
  123. assertThat(underTest.selectByUuids(db.getSession(), singletonList(rule1.getUuid()))).hasSize(1);
  124. assertThat(underTest.selectByUuids(db.getSession(), asList(rule1.getUuid(), rule2.getUuid()))).hasSize(2);
  125. assertThat(underTest.selectByUuids(db.getSession(), asList(rule1.getUuid(), rule2.getUuid(), UNKNOWN_RULE_UUID))).hasSize(2);
  126. assertThat(underTest.selectByUuids(db.getSession(), asList(rule1.getUuid(), rule2.getUuid(), removedRule.getUuid()))).hasSize(3);
  127. assertThat(underTest.selectByUuids(db.getSession(), singletonList(UNKNOWN_RULE_UUID))).isEmpty();
  128. }
  129. @Test
  130. public void selectDefinitionByUuids() {
  131. RuleDto rule1 = db.rules().insert();
  132. RuleDto rule2 = db.rules().insert();
  133. assertThat(underTest.selectByUuids(db.getSession(), singletonList(rule1.getUuid()))).hasSize(1);
  134. assertThat(underTest.selectByUuids(db.getSession(), asList(rule1.getUuid(), rule2.getUuid()))).hasSize(2);
  135. assertThat(underTest.selectByUuids(db.getSession(), asList(rule1.getUuid(), rule2.getUuid(), UNKNOWN_RULE_UUID))).hasSize(2);
  136. assertThat(underTest.selectByUuids(db.getSession(), singletonList(UNKNOWN_RULE_UUID))).isEmpty();
  137. }
  138. @Test
  139. public void selectOrFailByKey() {
  140. RuleDto rule1 = db.rules().insert();
  141. db.rules().insert();
  142. RuleDto rule = underTest.selectOrFailByKey(db.getSession(), rule1.getKey());
  143. assertThat(rule.getUuid()).isEqualTo(rule1.getUuid());
  144. }
  145. @Test
  146. public void selectOrFailByKey_fails_if_rule_not_found() {
  147. DbSession session = db.getSession();
  148. RuleKey ruleKey = RuleKey.of("NOT", "FOUND");
  149. assertThatThrownBy(() -> underTest.selectOrFailByKey(session, ruleKey))
  150. .isInstanceOf(RowNotFoundException.class)
  151. .hasMessage("Rule with key 'NOT:FOUND' does not exist");
  152. }
  153. @Test
  154. public void selectByKeys() {
  155. RuleDto rule1 = db.rules().insert();
  156. db.rules().insert();
  157. assertThat(underTest.selectByKeys(db.getSession(), Collections.emptyList())).isEmpty();
  158. assertThat(underTest.selectByKeys(db.getSession(), singletonList(RuleKey.of("NOT", "FOUND")))).isEmpty();
  159. List<RuleDto> rules = underTest.selectByKeys(db.getSession(), asList(rule1.getKey(), RuleKey.of("java", "OTHER")));
  160. assertThat(rules).hasSize(1);
  161. assertThat(rules.get(0).getUuid()).isEqualTo(rule1.getUuid());
  162. }
  163. @Test
  164. public void selectDefinitionByKeys() {
  165. RuleDto rule = db.rules().insert();
  166. assertThat(underTest.selectByKeys(db.getSession(), Collections.emptyList())).isEmpty();
  167. assertThat(underTest.selectByKeys(db.getSession(), singletonList(RuleKey.of("NOT", "FOUND")))).isEmpty();
  168. List<RuleDto> rules = underTest.selectByKeys(db.getSession(), asList(rule.getKey(), RuleKey.of("java", "OTHER")));
  169. assertThat(rules).hasSize(1);
  170. assertThat(rules.get(0).getUuid()).isEqualTo(rule.getUuid());
  171. }
  172. @Test
  173. public void selectAll() {
  174. RuleDto rule1 = db.rules().insertRule(r -> r.addDefaultImpact(newRuleDefaultImpact(SECURITY, org.sonar.api.issue.impact.Severity.LOW)));
  175. RuleDto rule2 = db.rules().insertRule(r -> r.addDefaultImpact(newRuleDefaultImpact(RELIABILITY, org.sonar.api.issue.impact.Severity.MEDIUM)));
  176. RuleDto rule3 = db.rules().insertRule();
  177. List<RuleDto> ruleDtos = underTest.selectAll(db.getSession());
  178. assertThat(ruleDtos)
  179. .extracting(RuleDto::getUuid)
  180. .containsExactlyInAnyOrder(rule1.getUuid(), rule2.getUuid(), rule3.getUuid());
  181. assertThat(ruleDtos)
  182. .filteredOn(ruleDto -> ruleDto.getUuid().equals(rule1.getUuid()))
  183. .extracting(RuleDto::getDefaultImpacts)
  184. .flatMap(Function.identity())
  185. .extracting(ImpactDto::getSeverity, ImpactDto::getSoftwareQuality)
  186. .containsExactlyInAnyOrder(
  187. tuple(org.sonar.api.issue.impact.Severity.HIGH, MAINTAINABILITY),
  188. tuple(org.sonar.api.issue.impact.Severity.LOW, SECURITY));
  189. assertThat(ruleDtos)
  190. .filteredOn(ruleDto -> ruleDto.getUuid().equals(rule2.getUuid()))
  191. .extracting(RuleDto::getDefaultImpacts)
  192. .flatMap(Function.identity())
  193. .extracting(ImpactDto::getSeverity, ImpactDto::getSoftwareQuality)
  194. .containsExactlyInAnyOrder(
  195. tuple(org.sonar.api.issue.impact.Severity.HIGH, MAINTAINABILITY),
  196. tuple(org.sonar.api.issue.impact.Severity.MEDIUM, RELIABILITY));
  197. assertThat(ruleDtos)
  198. .filteredOn(ruleDto -> ruleDto.getUuid().equals(rule3.getUuid()))
  199. .extracting(RuleDto::getDefaultImpacts)
  200. .flatMap(Function.identity())
  201. .extracting(ImpactDto::getSeverity, ImpactDto::getSoftwareQuality)
  202. .containsExactlyInAnyOrder(
  203. tuple(org.sonar.api.issue.impact.Severity.HIGH, MAINTAINABILITY));
  204. }
  205. private void assertEquals(RuleDto actual, RuleDto expected) {
  206. assertThat(actual.getUuid()).isEqualTo(expected.getUuid());
  207. assertThat(actual.getRepositoryKey()).isEqualTo(expected.getRepositoryKey());
  208. assertThat(actual.getRuleKey()).isEqualTo(expected.getRuleKey());
  209. assertThat(actual.getKey()).isEqualTo(expected.getKey());
  210. assertThat(actual.getStatus()).isEqualTo(expected.getStatus());
  211. assertThat(actual.getName()).isEqualTo(expected.getName());
  212. assertThat(actual.getConfigKey()).isEqualTo(expected.getConfigKey());
  213. assertThat(actual.getSeverity()).isEqualTo(expected.getSeverity());
  214. assertThat(actual.getSeverityString()).isEqualTo(expected.getSeverityString());
  215. assertThat(actual.isExternal()).isEqualTo(expected.isExternal());
  216. assertThat(actual.isTemplate()).isEqualTo(expected.isTemplate());
  217. assertThat(actual.isCustomRule()).isEqualTo(expected.isCustomRule());
  218. assertThat(actual.getLanguage()).isEqualTo(expected.getLanguage());
  219. assertThat(actual.getTemplateUuid()).isEqualTo(expected.getTemplateUuid());
  220. assertThat(actual.getDefRemediationFunction()).isEqualTo(expected.getDefRemediationFunction());
  221. assertThat(actual.getDefRemediationGapMultiplier()).isEqualTo(expected.getDefRemediationGapMultiplier());
  222. assertThat(actual.getDefRemediationBaseEffort()).isEqualTo(expected.getDefRemediationBaseEffort());
  223. assertThat(actual.getGapDescription()).isEqualTo(expected.getGapDescription());
  224. assertThat(actual.getSystemTags()).isEqualTo(expected.getSystemTags());
  225. assertThat(actual.getSecurityStandards()).isEqualTo(expected.getSecurityStandards());
  226. assertThat(actual.getType()).isEqualTo(expected.getType());
  227. assertThat(actual.getDescriptionFormat()).isEqualTo(expected.getDescriptionFormat());
  228. assertThat(actual.getRemediationBaseEffort()).isEqualTo(expected.getRemediationBaseEffort());
  229. assertThat(actual.getRemediationFunction()).isEqualTo(expected.getRemediationFunction());
  230. assertThat(actual.getRemediationGapMultiplier()).isEqualTo(expected.getRemediationGapMultiplier());
  231. assertThat(actual.getTags()).isEqualTo(expected.getTags());
  232. assertThat(actual.getNoteData()).isEqualTo(expected.getNoteData());
  233. assertThat(actual.getNoteCreatedAt()).isEqualTo(expected.getNoteCreatedAt());
  234. assertThat(actual.getNoteUpdatedAt()).isEqualTo(expected.getNoteUpdatedAt());
  235. assertThat(actual.getAdHocName()).isEqualTo(expected.getAdHocName());
  236. assertThat(actual.getAdHocDescription()).isEqualTo(expected.getAdHocDescription());
  237. assertThat(actual.getAdHocSeverity()).isEqualTo(expected.getAdHocSeverity());
  238. assertThat(actual.getAdHocType()).isEqualTo(expected.getAdHocType());
  239. assertThat(actual.getRuleDescriptionSectionDtos()).usingRecursiveFieldByFieldElementComparator()
  240. .containsExactlyInAnyOrderElementsOf(expected.getRuleDescriptionSectionDtos());
  241. assertThat(actual.getEducationPrinciples()).isEqualTo(expected.getEducationPrinciples());
  242. assertThat(actual.getCleanCodeAttribute()).isEqualTo(expected.getCleanCodeAttribute());
  243. }
  244. @Test
  245. public void selectAllDefinitions() {
  246. RuleDto rule1 = db.rules().insert();
  247. RuleDto rule2 = db.rules().insert();
  248. RuleDto removedRule = db.rules().insert(r -> r.setStatus(REMOVED));
  249. List<RuleDto> ruleDtos = underTest.selectAll(db.getSession());
  250. assertThat(ruleDtos).extracting(RuleDto::getUuid).containsOnly(rule1.getUuid(), rule2.getUuid(), removedRule.getUuid());
  251. }
  252. @Test
  253. public void selectEnabled_with_ResultHandler() {
  254. RuleDto rule = db.rules().insert();
  255. db.rules().insert(r -> r.setStatus(REMOVED));
  256. List<RuleDto> rules = underTest.selectEnabled(db.getSession());
  257. assertThat(rules.size()).isOne();
  258. RuleDto ruleDto = rules.get(0);
  259. assertThat(ruleDto.getUuid()).isEqualTo(rule.getUuid());
  260. }
  261. @Test
  262. public void selectByTypeAndLanguages() {
  263. RuleDto rule1 = db.rules().insert(
  264. r -> r.setKey(RuleKey.of("java", "S001"))
  265. .setConfigKey("S1")
  266. .setType(RuleType.VULNERABILITY)
  267. .setLanguage("java"));
  268. RuleDto rule2 = db.rules().insert(
  269. r -> r.setKey(RuleKey.of("js", "S002"))
  270. .setType(RuleType.SECURITY_HOTSPOT)
  271. .setLanguage("js"));
  272. assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java")))
  273. .extracting(RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
  274. .containsExactly(tuple(rule1.getUuid(), "java", RuleType.VULNERABILITY.getDbConstant()));
  275. assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.SECURITY_HOTSPOT.getDbConstant()), singletonList("js")))
  276. .extracting(RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
  277. .containsExactly(tuple(rule2.getUuid(), "js", RuleType.SECURITY_HOTSPOT.getDbConstant()));
  278. assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.SECURITY_HOTSPOT.getDbConstant()), singletonList("java"))).isEmpty();
  279. assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("js"))).isEmpty();
  280. }
  281. @Test
  282. public void selectByLanguage() {
  283. RuleDto rule1 = db.rules().insert(
  284. r -> r.setKey(RuleKey.of("java", "S001"))
  285. .setType(RuleType.VULNERABILITY)
  286. .setLanguage("java"));
  287. RuleDto rule2 = db.rules().insert(
  288. r -> r.setKey(RuleKey.of("js", "S002"))
  289. .setType(RuleType.SECURITY_HOTSPOT)
  290. .setLanguage("js"));
  291. RuleDto rule3 = db.rules().insert(
  292. r -> r.setKey(RuleKey.of("java", "S003"))
  293. .setType(RuleType.BUG)
  294. .setLanguage("java"));
  295. assertThat(underTest.selectByLanguage(db.getSession(), "java")).hasSize(2);
  296. assertThat(underTest.selectByLanguage(db.getSession(), "java"))
  297. .extracting(RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
  298. .containsExactlyInAnyOrder(
  299. tuple(rule1.getUuid(), "java", RuleType.VULNERABILITY.getDbConstant()),
  300. tuple(rule3.getUuid(), "java", RuleType.BUG.getDbConstant()));
  301. assertThat(underTest.selectByLanguage(db.getSession(), "js")).hasSize(1);
  302. assertThat(underTest.selectByLanguage(db.getSession(), "js"))
  303. .extracting(RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
  304. .containsExactly(tuple(rule2.getUuid(), "js", RuleType.SECURITY_HOTSPOT.getDbConstant()));
  305. }
  306. @Test
  307. public void selectByTypeAndLanguages_return_nothing_when_no_rule_on_languages() {
  308. db.rules().insert(
  309. r -> r.setKey(RuleKey.of("java", "S001"))
  310. .setConfigKey("S1")
  311. .setType(RuleType.VULNERABILITY)
  312. .setLanguage("java"));
  313. db.rules().insert(
  314. r -> r.setKey(RuleKey.of("js", "S002"))
  315. .setType(RuleType.VULNERABILITY)
  316. .setLanguage("js"));
  317. assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("cpp")))
  318. .isEmpty();
  319. }
  320. @Test
  321. public void selectByTypeAndLanguages_return_nothing_when_no_rule_with_type() {
  322. db.rules().insert(
  323. r -> r.setKey(RuleKey.of("java", "S001"))
  324. .setConfigKey("S1")
  325. .setType(RuleType.VULNERABILITY)
  326. .setLanguage("java"));
  327. db.rules().insert(
  328. r -> r.setKey(RuleKey.of("java", "S002"))
  329. .setType(RuleType.SECURITY_HOTSPOT)
  330. .setLanguage("java"));
  331. db.rules().insert(
  332. r -> r.setKey(RuleKey.of("java", "S003"))
  333. .setType(RuleType.CODE_SMELL)
  334. .setLanguage("java"));
  335. assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.BUG.getDbConstant()), singletonList("java")))
  336. .isEmpty();
  337. }
  338. @Test
  339. public void selectByTypeAndLanguages_ignores_external_rules() {
  340. db.rules().insert(
  341. r -> r.setKey(RuleKey.of("java", "S001"))
  342. .setConfigKey("S1")
  343. .setType(RuleType.VULNERABILITY)
  344. .setIsExternal(true)
  345. .setLanguage("java"));
  346. assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java")))
  347. .extracting(RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
  348. .isEmpty();
  349. }
  350. @Test
  351. public void selectByTypeAndLanguages_ignores_template_rules() {
  352. db.rules().insert(
  353. r -> r.setKey(RuleKey.of("java", "S001"))
  354. .setConfigKey("S1")
  355. .setType(RuleType.VULNERABILITY)
  356. .setIsTemplate(true)
  357. .setLanguage("java"));
  358. assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java")))
  359. .extracting(RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
  360. .isEmpty();
  361. }
  362. @Test
  363. public void select_by_query() {
  364. db.rules().insert(r -> r.setKey(RuleKey.of("java", "S001")).setConfigKey("S1"));
  365. db.rules().insert(r -> r.setKey(RuleKey.of("java", "S002")));
  366. db.rules().insert(r -> r.setStatus(REMOVED));
  367. assertThat(underTest.selectByQuery(db.getSession(), RuleQuery.create())).hasSize(2);
  368. assertThat(underTest.selectByQuery(db.getSession(), RuleQuery.create().withKey("S001"))).hasSize(1);
  369. assertThat(underTest.selectByQuery(db.getSession(), RuleQuery.create().withConfigKey("S1"))).hasSize(1);
  370. assertThat(underTest.selectByQuery(db.getSession(), RuleQuery.create().withRepositoryKey("java"))).hasSize(2);
  371. assertThat(underTest.selectByQuery(db.getSession(),
  372. RuleQuery.create().withKey("S001").withConfigKey("S1").withRepositoryKey("java"))).hasSize(1);
  373. }
  374. @Test
  375. public void insert_rule_with_description_section_context() {
  376. RuleDto rule = db.rules().insert(r -> r
  377. .addRuleDescriptionSectionDto(createDescriptionSectionWithContext("how_to_fix", "spring", "Spring")));
  378. Optional<RuleDto> ruleDto = underTest.selectByUuid(rule.getUuid(), db.getSession());
  379. assertEquals(ruleDto.get(), rule);
  380. }
  381. @NotNull
  382. private static RuleDescriptionSectionDto createDescriptionSectionWithContext(String key, String contextKey, String contextDisplayName) {
  383. return RuleDescriptionSectionDto.builder()
  384. .uuid(UuidFactoryFast.getInstance().create())
  385. .content("content")
  386. .key(key)
  387. .context(RuleDescriptionSectionContextDto.of(contextKey, contextDisplayName))
  388. .build();
  389. }
  390. @Test
  391. public void insert_rule_with_different_section_context() {
  392. RuleDto rule = db.rules().insert(r -> r
  393. .addRuleDescriptionSectionDto(createDescriptionSectionWithContext("how_to_fix", "spring", "Spring"))
  394. .addRuleDescriptionSectionDto(createDescriptionSectionWithContext("how_to_fix", "myBatis", "My Batis")));
  395. Optional<RuleDto> ruleDto = underTest.selectByUuid(rule.getUuid(), db.getSession());
  396. assertEquals(ruleDto.get(), rule);
  397. }
  398. @Test
  399. public void insert() {
  400. RuleDescriptionSectionDto sectionDto = createDefaultRuleDescriptionSection();
  401. ImpactDto ruleDefaultImpactDto = newRuleDefaultImpact(SECURITY, org.sonar.api.issue.impact.Severity.HIGH);
  402. RuleDto newRule = new RuleDto()
  403. .setUuid("rule-uuid")
  404. .setRuleKey("NewRuleKey")
  405. .setRepositoryKey("plugin")
  406. .setName("new name")
  407. .setDescriptionFormat(RuleDto.Format.MARKDOWN)
  408. .addRuleDescriptionSectionDto(sectionDto)
  409. .addDefaultImpact(ruleDefaultImpactDto)
  410. .setStatus(DEPRECATED)
  411. .setConfigKey("NewConfigKey")
  412. .setSeverity(Severity.INFO)
  413. .setIsTemplate(true)
  414. .setIsExternal(true)
  415. .setIsAdHoc(true)
  416. .setLanguage("dart")
  417. .setTemplateUuid("uuid-3")
  418. .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.toString())
  419. .setDefRemediationGapMultiplier("5d")
  420. .setDefRemediationBaseEffort("10h")
  421. .setGapDescription("java.S115.effortToFix")
  422. .setSystemTags(newHashSet("systag1", "systag2"))
  423. .setSecurityStandards(newHashSet("owaspTop10:a1", "cwe:123"))
  424. .setType(RuleType.BUG)
  425. .setCleanCodeAttribute(CleanCodeAttribute.CLEAR)
  426. .setScope(Scope.ALL)
  427. .setCreatedAt(1_500_000_000_000L)
  428. .setUpdatedAt(2_000_000_000_000L);
  429. underTest.insert(db.getSession(), newRule);
  430. db.getSession().commit();
  431. RuleDto ruleDto = underTest.selectOrFailByKey(db.getSession(), RuleKey.of("plugin", "NewRuleKey"));
  432. assertThat(ruleDto.getUuid()).isNotNull();
  433. assertThat(ruleDto.getName()).isEqualTo("new name");
  434. assertThat(ruleDto.getStatus()).isEqualTo(DEPRECATED);
  435. assertThat(ruleDto.getRuleKey()).isEqualTo("NewRuleKey");
  436. assertThat(ruleDto.getRepositoryKey()).isEqualTo("plugin");
  437. assertThat(ruleDto.getConfigKey()).isEqualTo("NewConfigKey");
  438. assertThat(ruleDto.getSeverity()).isZero();
  439. assertThat(ruleDto.getLanguage()).isEqualTo("dart");
  440. assertThat(ruleDto.isTemplate()).isTrue();
  441. assertThat(ruleDto.isExternal()).isTrue();
  442. assertThat(ruleDto.isAdHoc()).isTrue();
  443. assertThat(ruleDto.getTemplateUuid()).isEqualTo("uuid-3");
  444. assertThat(ruleDto.getDefRemediationFunction()).isEqualTo("LINEAR_OFFSET");
  445. assertThat(ruleDto.getDefRemediationGapMultiplier()).isEqualTo("5d");
  446. assertThat(ruleDto.getDefRemediationBaseEffort()).isEqualTo("10h");
  447. assertThat(ruleDto.getGapDescription()).isEqualTo("java.S115.effortToFix");
  448. assertThat(ruleDto.getSystemTags()).containsOnly("systag1", "systag2");
  449. assertThat(ruleDto.getSecurityStandards()).containsOnly("owaspTop10:a1", "cwe:123");
  450. assertThat(ruleDto.getScope()).isEqualTo(Scope.ALL);
  451. assertThat(ruleDto.getType()).isEqualTo(RuleType.BUG.getDbConstant());
  452. assertThat(ruleDto.getCreatedAt()).isEqualTo(1_500_000_000_000L);
  453. assertThat(ruleDto.getUpdatedAt()).isEqualTo(2_000_000_000_000L);
  454. assertThat(ruleDto.getDescriptionFormat()).isEqualTo(RuleDto.Format.MARKDOWN);
  455. assertThat(ruleDto.getRuleDescriptionSectionDtos()).usingRecursiveFieldByFieldElementComparator()
  456. .containsOnly(sectionDto);
  457. assertCleanCodeInformation(ruleDto, CleanCodeAttribute.CLEAR, ruleDefaultImpactDto);
  458. }
  459. private static void assertCleanCodeInformation(RuleDto ruleDto, CleanCodeAttribute attribute, ImpactDto ruleDefaultImpactDto) {
  460. assertThat(ruleDto.getCleanCodeAttribute()).isEqualTo(attribute);
  461. assertThat(ruleDto.getDefaultImpacts()).usingRecursiveFieldByFieldElementComparator()
  462. .containsOnly(ruleDefaultImpactDto);
  463. }
  464. @Test
  465. public void update_RuleDefinitionDto() {
  466. RuleDto rule = db.rules().insert();
  467. RuleDescriptionSectionDto sectionDto = createDefaultRuleDescriptionSection();
  468. RuleDto ruleToUpdate = new RuleDto()
  469. .setUuid(rule.getUuid())
  470. .setRuleKey("NewRuleKey")
  471. .setRepositoryKey("plugin")
  472. .setName("new name")
  473. .setDescriptionFormat(RuleDto.Format.MARKDOWN)
  474. .addRuleDescriptionSectionDto(sectionDto)
  475. .setStatus(DEPRECATED)
  476. .setConfigKey("NewConfigKey")
  477. .setSeverity(Severity.INFO)
  478. .setIsTemplate(true)
  479. .setIsExternal(true)
  480. .setIsAdHoc(true)
  481. .setLanguage("dart")
  482. .setTemplateUuid("uuid-3")
  483. .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.toString())
  484. .setDefRemediationGapMultiplier("5d")
  485. .setDefRemediationBaseEffort("10h")
  486. .setGapDescription("java.S115.effortToFix")
  487. .setSystemTags(newHashSet("systag1", "systag2"))
  488. .setSecurityStandards(newHashSet("owaspTop10:a1", "cwe:123"))
  489. .setScope(Scope.ALL)
  490. .setType(RuleType.BUG)
  491. .setCleanCodeAttribute(CleanCodeAttribute.TRUSTWORTHY)
  492. .setUpdatedAt(2_000_000_000_000L);
  493. underTest.update(db.getSession(), ruleToUpdate);
  494. db.getSession().commit();
  495. RuleDto ruleDto = underTest.selectOrFailByKey(db.getSession(), RuleKey.of("plugin", "NewRuleKey"));
  496. assertThat(ruleDto.getName()).isEqualTo("new name");
  497. assertThat(ruleDto.getStatus()).isEqualTo(DEPRECATED);
  498. assertThat(ruleDto.getRuleKey()).isEqualTo("NewRuleKey");
  499. assertThat(ruleDto.getRepositoryKey()).isEqualTo("plugin");
  500. assertThat(ruleDto.getConfigKey()).isEqualTo("NewConfigKey");
  501. assertThat(ruleDto.getSeverity()).isZero();
  502. assertThat(ruleDto.getLanguage()).isEqualTo("dart");
  503. assertThat(ruleDto.isTemplate()).isTrue();
  504. assertThat(ruleDto.isExternal()).isTrue();
  505. assertThat(ruleDto.isAdHoc()).isTrue();
  506. assertThat(ruleDto.getTemplateUuid()).isEqualTo("uuid-3");
  507. assertThat(ruleDto.getDefRemediationFunction()).isEqualTo("LINEAR_OFFSET");
  508. assertThat(ruleDto.getDefRemediationGapMultiplier()).isEqualTo("5d");
  509. assertThat(ruleDto.getDefRemediationBaseEffort()).isEqualTo("10h");
  510. assertThat(ruleDto.getGapDescription()).isEqualTo("java.S115.effortToFix");
  511. assertThat(ruleDto.getSystemTags()).containsOnly("systag1", "systag2");
  512. assertThat(ruleDto.getSecurityStandards()).containsOnly("owaspTop10:a1", "cwe:123");
  513. assertThat(ruleDto.getScope()).isEqualTo(Scope.ALL);
  514. assertThat(ruleDto.getType()).isEqualTo(RuleType.BUG.getDbConstant());
  515. assertThat(ruleDto.getCreatedAt()).isEqualTo(rule.getCreatedAt());
  516. assertThat(ruleDto.getUpdatedAt()).isEqualTo(2_000_000_000_000L);
  517. assertThat(ruleDto.getDescriptionFormat()).isEqualTo(RuleDto.Format.MARKDOWN);
  518. assertThat(ruleDto.getRuleDescriptionSectionDtos()).usingRecursiveFieldByFieldElementComparator()
  519. .containsOnly(sectionDto);
  520. }
  521. @Test
  522. public void update_rule_sections_add_new_section() {
  523. RuleDto rule = db.rules().insert();
  524. RuleDescriptionSectionDto existingSection = rule.getRuleDescriptionSectionDtos().iterator().next();
  525. RuleDescriptionSectionDto newSection = RuleDescriptionSectionDto.builder()
  526. .uuid(randomAlphanumeric(20))
  527. .key("new_key")
  528. .content(randomAlphanumeric(1000))
  529. .build();
  530. rule.addRuleDescriptionSectionDto(newSection);
  531. underTest.update(db.getSession(), rule);
  532. db.getSession().commit();
  533. RuleDto ruleDto = underTest.selectOrFailByKey(db.getSession(), RuleKey.of(rule.getRepositoryKey(), rule.getRuleKey()));
  534. assertThat(ruleDto.getRuleDescriptionSectionDtos())
  535. .usingRecursiveFieldByFieldElementComparator()
  536. .containsExactlyInAnyOrder(newSection, existingSection);
  537. }
  538. @Test
  539. public void update_rule_sections_replaces_section() {
  540. RuleDto rule = db.rules().insert();
  541. Set<RuleDescriptionSectionDto> ruleDescriptionSectionDtos = rule.getRuleDescriptionSectionDtos();
  542. RuleDescriptionSectionDto existingSection = ruleDescriptionSectionDtos.iterator().next();
  543. RuleDescriptionSectionDto replacingSection = RuleDescriptionSectionDto.builder()
  544. .uuid(randomAlphanumeric(20))
  545. .key(existingSection.getKey())
  546. .content(randomAlphanumeric(1000))
  547. .build();
  548. rule.replaceRuleDescriptionSectionDtos(List.of(replacingSection));
  549. underTest.update(db.getSession(), rule);
  550. db.getSession().commit();
  551. RuleDto ruleDto = underTest.selectOrFailByKey(db.getSession(), RuleKey.of(rule.getRepositoryKey(), rule.getRuleKey()));
  552. assertThat(ruleDto.getRuleDescriptionSectionDtos())
  553. .usingRecursiveFieldByFieldElementComparator()
  554. .containsOnly(replacingSection);
  555. }
  556. @Test
  557. public void update_rule_sections_replaces_section_with_context() {
  558. RuleDto rule = db.rules().insert();
  559. Set<RuleDescriptionSectionDto> ruleDescriptionSectionDtos = rule.getRuleDescriptionSectionDtos();
  560. RuleDescriptionSectionDto existingSection = ruleDescriptionSectionDtos.iterator().next();
  561. RuleDescriptionSectionContextDto contextDto = RuleDescriptionSectionContextDto.of(randomAlphanumeric(10), randomAlphanumeric(10));
  562. RuleDescriptionSectionDto replacingSection = RuleDescriptionSectionDto.builder()
  563. .uuid(randomAlphanumeric(20))
  564. .key(existingSection.getKey())
  565. .content(randomAlphanumeric(1000))
  566. .context(contextDto)
  567. .build();
  568. rule.replaceRuleDescriptionSectionDtos(List.of(replacingSection));
  569. underTest.update(db.getSession(), rule);
  570. db.getSession().commit();
  571. RuleDto ruleDto = underTest.selectOrFailByKey(db.getSession(), RuleKey.of(rule.getRepositoryKey(), rule.getRuleKey()));
  572. assertThat(ruleDto.getRuleDescriptionSectionDtos())
  573. .usingRecursiveFieldByFieldElementComparator()
  574. .containsOnly(replacingSection);
  575. }
  576. @Test
  577. public void update_RuleMetadataDto_inserts_row_in_RULE_METADATA_if_not_exists_yet() {
  578. RuleDto rule = db.rules().insert();
  579. long createdAtBeforeUpdate = rule.getCreatedAt();
  580. rule.setNoteData("My note");
  581. rule.setNoteUserUuid("admin");
  582. rule.setNoteCreatedAt(DateUtils.parseDate("2013-12-19").getTime());
  583. rule.setNoteUpdatedAt(DateUtils.parseDate("2013-12-20").getTime());
  584. rule.setRemediationFunction(DebtRemediationFunction.Type.LINEAR.toString());
  585. rule.setRemediationGapMultiplier("1h");
  586. rule.setRemediationBaseEffort("5min");
  587. rule.setTags(newHashSet("tag1", "tag2"));
  588. rule.setAdHocName("ad hoc name");
  589. rule.setAdHocDescription("ad hoc desc");
  590. rule.setAdHocSeverity(Severity.BLOCKER);
  591. rule.setAdHocType(RuleType.CODE_SMELL);
  592. rule.setCreatedAt(3_500_000_000_000L);
  593. rule.setUpdatedAt(4_000_000_000_000L);
  594. underTest.update(db.getSession(), rule);
  595. db.getSession().commit();
  596. RuleDto ruleDto = underTest.selectOrFailByKey(db.getSession(), rule.getKey());
  597. assertThat(ruleDto.getNoteData()).isEqualTo("My note");
  598. assertThat(ruleDto.getNoteUserUuid()).isEqualTo("admin");
  599. assertThat(ruleDto.getNoteCreatedAt()).isNotNull();
  600. assertThat(ruleDto.getNoteUpdatedAt()).isNotNull();
  601. assertThat(ruleDto.getRemediationFunction()).isEqualTo("LINEAR");
  602. assertThat(ruleDto.getRemediationGapMultiplier()).isEqualTo("1h");
  603. assertThat(ruleDto.getRemediationBaseEffort()).isEqualTo("5min");
  604. assertThat(ruleDto.getTags()).containsOnly("tag1", "tag2");
  605. assertThat(ruleDto.getAdHocName()).isEqualTo("ad hoc name");
  606. assertThat(ruleDto.getAdHocDescription()).isEqualTo("ad hoc desc");
  607. assertThat(ruleDto.getAdHocSeverity()).isEqualTo(Severity.BLOCKER);
  608. assertThat(ruleDto.getAdHocType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant());
  609. assertThat(ruleDto.getSecurityStandards()).isEmpty();
  610. assertThat(ruleDto.getCreatedAt()).isEqualTo(createdAtBeforeUpdate);
  611. assertThat(ruleDto.getUpdatedAt()).isEqualTo(4_000_000_000_000L);
  612. // Info from rule definition
  613. assertThat(ruleDto.getDefRemediationFunction()).isEqualTo(rule.getDefRemediationFunction());
  614. assertThat(ruleDto.getDefRemediationGapMultiplier()).isEqualTo(rule.getDefRemediationGapMultiplier());
  615. assertThat(ruleDto.getDefRemediationBaseEffort()).isEqualTo(rule.getDefRemediationBaseEffort());
  616. assertThat(ruleDto.getGapDescription()).isEqualTo(rule.getGapDescription());
  617. assertThat(ruleDto.getSystemTags()).containsAll(rule.getSystemTags());
  618. assertThat(ruleDto.getType()).isEqualTo(rule.getType());
  619. }
  620. @Test
  621. public void update_RuleMetadataDto_updates_row_in_RULE_METADATA_if_already_exists() {
  622. RuleDto rule = db.rules().insert();
  623. rule.setAdHocDescription("ad-hoc-desc");
  624. rule.setCreatedAt(3_500_000_000_000L);
  625. rule.setUpdatedAt(4_000_000_000_000L);
  626. RuleDto ruleDtoBeforeUpdate = underTest.selectOrFailByKey(db.getSession(), rule.getKey());
  627. underTest.update(db.getSession(), rule);
  628. db.commit();
  629. RuleDto ruleDtoAfterUpdate = underTest.selectOrFailByKey(db.getSession(), rule.getKey());
  630. assertThat(ruleDtoAfterUpdate.getNoteData()).isEqualTo(rule.getNoteData());
  631. assertThat(ruleDtoAfterUpdate.getNoteUserUuid()).isEqualTo(rule.getNoteUserUuid());
  632. assertThat(ruleDtoAfterUpdate.getNoteCreatedAt()).isEqualTo(rule.getNoteCreatedAt());
  633. assertThat(ruleDtoAfterUpdate.getNoteUpdatedAt()).isEqualTo(rule.getNoteUpdatedAt());
  634. assertThat(ruleDtoAfterUpdate.getRemediationFunction()).isEqualTo(rule.getRemediationFunction());
  635. assertThat(ruleDtoAfterUpdate.getRemediationGapMultiplier()).isEqualTo(rule.getRemediationGapMultiplier());
  636. assertThat(ruleDtoAfterUpdate.getRemediationBaseEffort()).isEqualTo(rule.getRemediationBaseEffort());
  637. assertThat(ruleDtoAfterUpdate.getTags()).isEqualTo(rule.getTags());
  638. assertThat(ruleDtoAfterUpdate.getAdHocName()).isEqualTo(rule.getAdHocName());
  639. assertThat(ruleDtoAfterUpdate.getAdHocDescription()).isEqualTo("ad-hoc-desc");
  640. assertThat(ruleDtoAfterUpdate.getAdHocSeverity()).isEqualTo(rule.getAdHocSeverity());
  641. assertThat(ruleDtoAfterUpdate.getAdHocType()).isEqualTo(rule.getAdHocType());
  642. assertThat(ruleDtoAfterUpdate.getSecurityStandards()).isEqualTo(rule.getSecurityStandards());
  643. assertThat(ruleDtoAfterUpdate.getCreatedAt()).isEqualTo(ruleDtoBeforeUpdate.getCreatedAt());
  644. assertThat(ruleDtoAfterUpdate.getUpdatedAt()).isEqualTo(4_000_000_000_000L);
  645. rule.setNoteData("My note");
  646. rule.setNoteUserUuid("admin");
  647. rule.setNoteCreatedAt(DateUtils.parseDate("2013-12-19").getTime());
  648. rule.setNoteUpdatedAt(DateUtils.parseDate("2013-12-20").getTime());
  649. rule.setRemediationFunction(DebtRemediationFunction.Type.LINEAR.toString());
  650. rule.setRemediationGapMultiplier("1h");
  651. rule.setRemediationBaseEffort("5min");
  652. rule.setTags(newHashSet("tag1", "tag2"));
  653. rule.setAdHocName("ad hoc name");
  654. rule.setAdHocDescription("ad hoc desc");
  655. rule.setAdHocSeverity(Severity.BLOCKER);
  656. rule.setAdHocType(RuleType.CODE_SMELL);
  657. rule.setCreatedAt(6_500_000_000_000L);
  658. rule.setUpdatedAt(7_000_000_000_000L);
  659. underTest.update(db.getSession(), rule);
  660. db.commit();
  661. ruleDtoAfterUpdate = underTest.selectOrFailByKey(db.getSession(), rule.getKey());
  662. assertThat(ruleDtoAfterUpdate.getNoteData()).isEqualTo("My note");
  663. assertThat(ruleDtoAfterUpdate.getNoteUserUuid()).isEqualTo("admin");
  664. assertThat(ruleDtoAfterUpdate.getNoteCreatedAt()).isNotNull();
  665. assertThat(ruleDtoAfterUpdate.getNoteUpdatedAt()).isNotNull();
  666. assertThat(ruleDtoAfterUpdate.getRemediationFunction()).isEqualTo("LINEAR");
  667. assertThat(ruleDtoAfterUpdate.getRemediationGapMultiplier()).isEqualTo("1h");
  668. assertThat(ruleDtoAfterUpdate.getRemediationBaseEffort()).isEqualTo("5min");
  669. assertThat(ruleDtoAfterUpdate.getTags()).containsOnly("tag1", "tag2");
  670. assertThat(ruleDtoAfterUpdate.getAdHocName()).isEqualTo("ad hoc name");
  671. assertThat(ruleDtoAfterUpdate.getAdHocDescription()).isEqualTo("ad hoc desc");
  672. assertThat(ruleDtoAfterUpdate.getAdHocSeverity()).isEqualTo(Severity.BLOCKER);
  673. assertThat(ruleDtoAfterUpdate.getAdHocType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant());
  674. assertThat(ruleDtoAfterUpdate.getSecurityStandards()).isEmpty();
  675. assertThat(ruleDtoAfterUpdate.getCreatedAt()).isEqualTo(ruleDtoBeforeUpdate.getCreatedAt());
  676. assertThat(ruleDtoAfterUpdate.getUpdatedAt()).isEqualTo(7_000_000_000_000L);
  677. }
  678. @Test
  679. public void select_all_rule_params() {
  680. RuleDto rule1 = db.rules().insert();
  681. RuleParamDto ruleParam1 = db.rules().insertRuleParam(rule1);
  682. RuleParamDto ruleParam12 = db.rules().insertRuleParam(rule1);
  683. RuleDto rule2 = db.rules().insert();
  684. RuleParamDto ruleParam2 = db.rules().insertRuleParam(rule2);
  685. RuleDto rule3 = db.rules().insert();
  686. RuleParamDto ruleParam3 = db.rules().insertRuleParam(rule3);
  687. List<RuleParamDto> ruleDtos = underTest.selectAllRuleParams(db.getSession());
  688. assertThat(ruleDtos).hasSize(4);
  689. assertThat(ruleDtos).extracting(RuleParamDto::getUuid)
  690. .containsExactlyInAnyOrder(ruleParam1.getUuid(), ruleParam12.getUuid(),
  691. ruleParam2.getUuid(), ruleParam3.getUuid());
  692. }
  693. @Test
  694. public void select_parameters_by_rule_key() {
  695. RuleDto rule = db.rules().insert();
  696. RuleParamDto ruleParam = db.rules().insertRuleParam(rule);
  697. List<RuleParamDto> ruleDtos = underTest.selectRuleParamsByRuleKey(db.getSession(), rule.getKey());
  698. assertThat(ruleDtos.size()).isOne();
  699. RuleParamDto ruleDto = ruleDtos.get(0);
  700. assertThat(ruleDto.getUuid()).isEqualTo(ruleParam.getUuid());
  701. assertThat(ruleDto.getName()).isEqualTo(ruleParam.getName());
  702. assertThat(ruleDto.getDescription()).isEqualTo(ruleParam.getDescription());
  703. assertThat(ruleDto.getType()).isEqualTo(ruleParam.getType());
  704. assertThat(ruleDto.getRuleUuid()).isEqualTo(rule.getUuid());
  705. }
  706. @Test
  707. public void select_parameters_by_rule_keys() {
  708. RuleDto rule1 = db.rules().insert();
  709. db.rules().insertRuleParam(rule1);
  710. RuleDto rule2 = db.rules().insert();
  711. db.rules().insertRuleParam(rule2);
  712. assertThat(underTest.selectRuleParamsByRuleKeys(db.getSession(),
  713. Arrays.asList(rule1.getKey(), rule2.getKey()))).hasSize(2);
  714. assertThat(underTest.selectRuleParamsByRuleKeys(db.getSession(),
  715. singletonList(RuleKey.of("unknown", "Unknown")))).isEmpty();
  716. }
  717. @Test
  718. public void insert_parameter() {
  719. RuleDto ruleDefinitionDto = db.rules().insert();
  720. RuleParamDto orig = RuleParamDto.createFor(ruleDefinitionDto)
  721. .setName("max")
  722. .setType("INTEGER")
  723. .setDefaultValue("30")
  724. .setDescription("My Parameter");
  725. underTest.insertRuleParam(db.getSession(), ruleDefinitionDto, orig);
  726. List<RuleParamDto> ruleParamDtos = underTest.selectRuleParamsByRuleKey(db.getSession(), ruleDefinitionDto.getKey());
  727. assertThat(ruleParamDtos).hasSize(1);
  728. RuleParamDto loaded = ruleParamDtos.get(0);
  729. assertThat(loaded.getRuleUuid()).isEqualTo(orig.getRuleUuid());
  730. assertThat(loaded.getName()).isEqualTo(orig.getName());
  731. assertThat(loaded.getType()).isEqualTo(orig.getType());
  732. assertThat(loaded.getDefaultValue()).isEqualTo(orig.getDefaultValue());
  733. assertThat(loaded.getDescription()).isEqualTo(orig.getDescription());
  734. }
  735. @Test
  736. public void should_fail_to_insert_duplicate_parameter() {
  737. RuleDto ruleDefinitionDto = db.rules().insert();
  738. RuleParamDto param = RuleParamDto.createFor(ruleDefinitionDto)
  739. .setName("max")
  740. .setType("INTEGER")
  741. .setDefaultValue("30")
  742. .setDescription("My Parameter");
  743. DbSession session = db.getSession();
  744. underTest.insertRuleParam(session, ruleDefinitionDto, param);
  745. assertThatThrownBy(() -> underTest.insertRuleParam(session, ruleDefinitionDto, param))
  746. .isInstanceOf(PersistenceException.class);
  747. }
  748. @Test
  749. public void update_parameter() {
  750. RuleDto rule = db.rules().insert();
  751. RuleParamDto ruleParam = db.rules().insertRuleParam(rule);
  752. List<RuleParamDto> params = underTest.selectRuleParamsByRuleKey(db.getSession(), rule.getKey());
  753. assertThat(params).hasSize(1);
  754. RuleParamDto param = new RuleParamDto()
  755. .setUuid(ruleParam.getUuid())
  756. .setRuleUuid(rule.getUuid())
  757. // Name will not be updated
  758. .setName("format")
  759. .setType("STRING")
  760. .setDefaultValue("^[a-z]+(\\.[a-z][a-z0-9]*)*$")
  761. .setDescription("Regular expression used to check the package names against.");
  762. underTest.updateRuleParam(db.getSession(), rule, param);
  763. assertThat(underTest.selectRuleParamsByRuleKey(db.getSession(), rule.getKey()))
  764. .extracting(RuleParamDto::getName, RuleParamDto::getType, RuleParamDto::getDefaultValue, RuleParamDto::getDescription)
  765. .containsExactlyInAnyOrder(tuple(ruleParam.getName(), "STRING", "^[a-z]+(\\.[a-z][a-z0-9]*)*$", "Regular expression used to check the package names against."));
  766. }
  767. @Test
  768. public void delete_parameter() {
  769. RuleDto rule = db.rules().insert();
  770. RuleParamDto ruleParam = db.rules().insertRuleParam(rule);
  771. assertThat(underTest.selectRuleParamsByRuleKey(db.getSession(), rule.getKey())).hasSize(1);
  772. underTest.deleteRuleParam(db.getSession(), ruleParam.getUuid());
  773. assertThat(underTest.selectRuleParamsByRuleKey(db.getSession(), rule.getKey())).isEmpty();
  774. }
  775. @Test
  776. public void scrollIndexingRules_on_empty_table() {
  777. Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
  778. underTest.selectIndexingRules(db.getSession(), accumulator);
  779. assertThat(accumulator.list).isEmpty();
  780. }
  781. @Test
  782. public void scrollIndexingRules() {
  783. Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
  784. RuleDescriptionSectionDto ruleDescriptionSectionDto = RuleDescriptionSectionDto.builder()
  785. .key("DESC")
  786. .uuid("uuid")
  787. .content("my description")
  788. .context(RuleDescriptionSectionContextDto.of("context key", "context display name"))
  789. .build();
  790. RuleDto r1 = db.rules().insert(r -> {
  791. r.addRuleDescriptionSectionDto(ruleDescriptionSectionDto);
  792. });
  793. RuleDto r2 = db.rules().insert(r -> r.setIsExternal(true));
  794. underTest.selectIndexingRules(db.getSession(), accumulator);
  795. RuleForIndexingDto firstRule = findRuleForIndexingWithUuid(accumulator, r1.getUuid());
  796. RuleForIndexingDto secondRule = findRuleForIndexingWithUuid(accumulator, r2.getUuid());
  797. assertThat(Arrays.asList(firstRule, secondRule))
  798. .extracting(RuleForIndexingDto::getUuid, RuleForIndexingDto::getRuleKey)
  799. .containsExactlyInAnyOrder(tuple(r1.getUuid(), r1.getKey()), tuple(r2.getUuid(), r2.getKey()));
  800. Iterator<RuleForIndexingDto> it = accumulator.list.iterator();
  801. assertThat(firstRule.getRepository()).isEqualTo(r1.getRepositoryKey());
  802. assertThat(firstRule.getPluginRuleKey()).isEqualTo(r1.getRuleKey());
  803. assertThat(firstRule.getName()).isEqualTo(r1.getName());
  804. assertThat(firstRule.getRuleDescriptionSectionsDtos().stream()
  805. .filter(s -> s.getKey().equals(ruleDescriptionSectionDto.getKey()))
  806. .collect(MoreCollectors.onlyElement()))
  807. .usingRecursiveComparison()
  808. .isEqualTo(ruleDescriptionSectionDto);
  809. assertThat(firstRule.getDescriptionFormat()).isEqualTo(r1.getDescriptionFormat());
  810. assertThat(firstRule.getSeverity()).isEqualTo(r1.getSeverity());
  811. assertThat(firstRule.getStatus()).isEqualTo(r1.getStatus());
  812. assertThat(firstRule.isExternal()).isFalse();
  813. assertThat(firstRule.isTemplate()).isEqualTo(r1.isTemplate());
  814. assertThat(firstRule.getSystemTags()).isEqualTo(r1.getSystemTags());
  815. assertThat(firstRule.getSecurityStandards()).isEqualTo(r1.getSecurityStandards());
  816. assertThat(firstRule.getTemplateRuleKey()).isNull();
  817. assertThat(firstRule.getTemplateRepository()).isNull();
  818. assertThat(firstRule.getInternalKey()).isEqualTo(r1.getConfigKey());
  819. assertThat(firstRule.getLanguage()).isEqualTo(r1.getLanguage());
  820. assertThat(firstRule.getType()).isEqualTo(r1.getType());
  821. assertThat(firstRule.getCreatedAt()).isEqualTo(r1.getCreatedAt());
  822. assertThat(firstRule.getUpdatedAt()).isEqualTo(r1.getUpdatedAt());
  823. assertThat(secondRule.isExternal()).isTrue();
  824. }
  825. @Test
  826. public void scrollIndexingRules_maps_rule_definition_fields_for_regular_rule_and_template_rule() {
  827. Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
  828. RuleDto r1 = db.rules().insert();
  829. r1.setTags(Set.of("t1", "t2"));
  830. r1 = db.rules().update(r1);
  831. String r1Uuid = r1.getUuid();
  832. RuleDto r2 = db.rules().insert(rule -> rule.setTemplateUuid(r1Uuid));
  833. underTest.selectIndexingRules(db.getSession(), accumulator);
  834. assertThat(accumulator.list).hasSize(2);
  835. RuleForIndexingDto firstRule = findRuleForIndexingWithUuid(accumulator, r1.getUuid());
  836. RuleForIndexingDto secondRule = findRuleForIndexingWithUuid(accumulator, r2.getUuid());
  837. assertRuleDefinitionFieldsAreEquals(r1, firstRule);
  838. assertThat(r1.getTags()).isEqualTo(firstRule.getTags());
  839. assertThat(firstRule.getTemplateRuleKey()).isNull();
  840. assertThat(firstRule.getTemplateRepository()).isNull();
  841. assertRuleDefinitionFieldsAreEquals(r2, secondRule);
  842. assertThat(secondRule.getTemplateRuleKey()).isEqualTo(r1.getRuleKey());
  843. assertThat(secondRule.getTemplateRepository()).isEqualTo(r1.getRepositoryKey());
  844. }
  845. @NotNull
  846. private static RuleForIndexingDto findRuleForIndexingWithUuid(Accumulator<RuleForIndexingDto> accumulator, String uuid) {
  847. return accumulator.list.stream()
  848. .filter(rule -> rule.getUuid().equals(uuid))
  849. .findFirst().orElseThrow();
  850. }
  851. @Test
  852. public void scrollIndexingRulesByKeys() {
  853. Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
  854. RuleDto r1 = db.rules().insert();
  855. db.rules().insert();
  856. underTest.selectIndexingRulesByKeys(db.getSession(), singletonList(r1.getUuid()), accumulator);
  857. assertThat(accumulator.list)
  858. .extracting(RuleForIndexingDto::getUuid, RuleForIndexingDto::getRuleKey)
  859. .containsExactlyInAnyOrder(tuple(r1.getUuid(), r1.getKey()));
  860. }
  861. @Test
  862. public void scrollIndexingRulesByKeys_maps_rule_definition_fields_for_regular_rule_and_template_rule() {
  863. Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
  864. RuleDto r1 = db.rules().insert();
  865. RuleDto r2 = db.rules().insert(rule -> rule.setTemplateUuid(r1.getUuid()));
  866. underTest.selectIndexingRulesByKeys(db.getSession(), Arrays.asList(r1.getUuid(), r2.getUuid()), accumulator);
  867. assertThat(accumulator.list).hasSize(2);
  868. RuleForIndexingDto firstRule = accumulator.list.stream().filter(t -> t.getUuid().equals(r1.getUuid())).findFirst().get();
  869. RuleForIndexingDto secondRule = accumulator.list.stream().filter(t -> t.getUuid().equals(r2.getUuid())).findFirst().get();
  870. assertRuleDefinitionFieldsAreEquals(r1, firstRule);
  871. assertThat(firstRule.getTemplateRuleKey()).isNull();
  872. assertThat(firstRule.getTemplateRepository()).isNull();
  873. assertRuleDefinitionFieldsAreEquals(r2, secondRule);
  874. assertThat(secondRule.getTemplateRuleKey()).isEqualTo(r1.getRuleKey());
  875. assertThat(secondRule.getTemplateRepository()).isEqualTo(r1.getRepositoryKey());
  876. }
  877. private void assertRuleDefinitionFieldsAreEquals(RuleDto r1, RuleForIndexingDto ruleForIndexing) {
  878. assertThat(ruleForIndexing.getUuid()).isEqualTo(r1.getUuid());
  879. assertThat(ruleForIndexing.getRuleKey()).isEqualTo(r1.getKey());
  880. assertThat(ruleForIndexing.getRepository()).isEqualTo(r1.getRepositoryKey());
  881. assertThat(ruleForIndexing.getPluginRuleKey()).isEqualTo(r1.getRuleKey());
  882. assertThat(ruleForIndexing.getName()).isEqualTo(r1.getName());
  883. assertThat(ruleForIndexing.getRuleDescriptionSectionsDtos())
  884. .usingRecursiveComparison()
  885. .isEqualTo(r1.getRuleDescriptionSectionDtos());
  886. assertThat(ruleForIndexing.getDescriptionFormat()).isEqualTo(r1.getDescriptionFormat());
  887. assertThat(ruleForIndexing.getSeverity()).isEqualTo(r1.getSeverity());
  888. assertThat(ruleForIndexing.getSeverityAsString()).isEqualTo(SeverityUtil.getSeverityFromOrdinal(r1.getSeverity()));
  889. assertThat(ruleForIndexing.getStatus()).isEqualTo(r1.getStatus());
  890. assertThat(ruleForIndexing.isTemplate()).isEqualTo(r1.isTemplate());
  891. assertThat(ruleForIndexing.getSystemTags()).isEqualTo(r1.getSystemTags());
  892. assertThat(ruleForIndexing.getSecurityStandards()).isEqualTo(r1.getSecurityStandards());
  893. assertThat(ruleForIndexing.getInternalKey()).isEqualTo(r1.getConfigKey());
  894. assertThat(ruleForIndexing.getLanguage()).isEqualTo(r1.getLanguage());
  895. assertThat(ruleForIndexing.getType()).isEqualTo(r1.getType());
  896. assertThat(ruleForIndexing.getTypeAsRuleType()).isEqualTo(RuleType.valueOf(r1.getType()));
  897. assertThat(ruleForIndexing.getCreatedAt()).isEqualTo(r1.getCreatedAt());
  898. assertThat(ruleForIndexing.getUpdatedAt()).isEqualTo(r1.getUpdatedAt());
  899. }
  900. @Test
  901. public void scrollIndexingRulesByKeys_scrolls_nothing_if_key_does_not_exist() {
  902. Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
  903. db.rules().insert();
  904. String nonExistingRuleUuid = "non-existing-uuid";
  905. underTest.selectIndexingRulesByKeys(db.getSession(), singletonList(nonExistingRuleUuid), accumulator);
  906. assertThat(accumulator.list).isEmpty();
  907. }
  908. @Test
  909. public void scrollIndexingRuleExtensionsByIds() {
  910. Accumulator<RuleExtensionForIndexingDto> accumulator = new Accumulator<>();
  911. RuleDto r1 = db.rules().insert(ruleDto -> ruleDto.setTagsField("t1,t2"));
  912. db.rules().insert(ruleDto -> ruleDto.setTagsField("t1,t3"));
  913. underTest.scrollIndexingRuleExtensionsByIds(db.getSession(), singletonList(r1.getUuid()), accumulator);
  914. assertThat(accumulator.list)
  915. .extracting(RuleExtensionForIndexingDto::getRuleUuid, RuleExtensionForIndexingDto::getRuleKey, RuleExtensionForIndexingDto::getTags)
  916. .containsExactlyInAnyOrder(
  917. tuple(r1.getUuid(), r1.getKey(), r1.getTagsAsString()));
  918. }
  919. @Test
  920. public void selectAllDeprecatedRuleKeys() {
  921. RuleDto r1 = db.rules().insert();
  922. RuleDto r2 = db.rules().insert();
  923. db.rules().insertDeprecatedKey(r -> r.setRuleUuid(r1.getUuid()));
  924. db.rules().insertDeprecatedKey(r -> r.setRuleUuid(r2.getUuid()));
  925. db.getSession().commit();
  926. Set<DeprecatedRuleKeyDto> deprecatedRuleKeyDtos = underTest.selectAllDeprecatedRuleKeys(db.getSession());
  927. assertThat(deprecatedRuleKeyDtos).hasSize(2);
  928. }
  929. @Test
  930. public void selectDeprecatedRuleKeysByRuleUuids() {
  931. RuleDto r1 = db.rules().insert();
  932. RuleDto r2 = db.rules().insert();
  933. RuleDto r3 = db.rules().insert();
  934. RuleDto r4 = db.rules().insert();
  935. DeprecatedRuleKeyDto drk1 = db.rules().insertDeprecatedKey(r -> r.setRuleUuid(r1.getUuid()));
  936. DeprecatedRuleKeyDto drk2 = db.rules().insertDeprecatedKey(r -> r.setRuleUuid(r1.getUuid()));
  937. DeprecatedRuleKeyDto drk3 = db.rules().insertDeprecatedKey(r -> r.setRuleUuid(r2.getUuid()));
  938. db.rules().insertDeprecatedKey(r -> r.setRuleUuid(r4.getUuid()));
  939. db.getSession().commit();
  940. Set<DeprecatedRuleKeyDto> deprecatedRuleKeyDtos = underTest.selectDeprecatedRuleKeysByRuleUuids(
  941. db.getSession(), ImmutableSet.of(r1.getUuid(), r2.getUuid(), r3.getUuid()));
  942. assertThat(deprecatedRuleKeyDtos)
  943. .extracting(DeprecatedRuleKeyDto::getUuid)
  944. .containsExactlyInAnyOrder(drk1.getUuid(), drk2.getUuid(), drk3.getUuid());
  945. }
  946. @Test
  947. public void selectAllDeprecatedRuleKeys_return_values_even_if_there_is_no_rule() {
  948. db.rules().insertDeprecatedKey();
  949. db.rules().insertDeprecatedKey();
  950. Set<DeprecatedRuleKeyDto> deprecatedRuleKeyDtos = underTest.selectAllDeprecatedRuleKeys(db.getSession());
  951. assertThat(deprecatedRuleKeyDtos).hasSize(2);
  952. assertThat(deprecatedRuleKeyDtos)
  953. .extracting(DeprecatedRuleKeyDto::getNewRepositoryKey, DeprecatedRuleKeyDto::getNewRuleKey)
  954. .containsExactly(
  955. tuple(null, null),
  956. tuple(null, null));
  957. }
  958. @Test
  959. public void deleteDeprecatedRuleKeys_with_empty_list_has_no_effect() {
  960. db.rules().insertDeprecatedKey();
  961. db.rules().insertDeprecatedKey();
  962. assertThat(underTest.selectAllDeprecatedRuleKeys(db.getSession())).hasSize(2);
  963. underTest.deleteDeprecatedRuleKeys(db.getSession(), emptyList());
  964. assertThat(underTest.selectAllDeprecatedRuleKeys(db.getSession())).hasSize(2);
  965. }
  966. @Test
  967. public void deleteDeprecatedRuleKeys_with_non_existing_uuid_has_no_effect() {
  968. db.rules().insertDeprecatedKey(d -> d.setUuid("A1"));
  969. db.rules().insertDeprecatedKey(d -> d.setUuid("A2"));
  970. assertThat(underTest.selectAllDeprecatedRuleKeys(db.getSession())).hasSize(2);
  971. underTest.deleteDeprecatedRuleKeys(db.getSession(), asList("B1", "B2"));
  972. assertThat(underTest.selectAllDeprecatedRuleKeys(db.getSession())).hasSize(2);
  973. }
  974. @Test
  975. public void deleteDeprecatedRuleKeys() {
  976. DeprecatedRuleKeyDto deprecatedRuleKeyDto1 = db.rules().insertDeprecatedKey();
  977. db.rules().insertDeprecatedKey();
  978. assertThat(underTest.selectAllDeprecatedRuleKeys(db.getSession())).hasSize(2);
  979. underTest.deleteDeprecatedRuleKeys(db.getSession(), singletonList(deprecatedRuleKeyDto1.getUuid()));
  980. assertThat(underTest.selectAllDeprecatedRuleKeys(db.getSession())).hasSize(1);
  981. }
  982. @Test
  983. public void insertDeprecatedRuleKey() {
  984. RuleDto r1 = db.rules().insert();
  985. DeprecatedRuleKeyDto deprecatedRuleKeyDto = db.rules().insertDeprecatedKey(d -> d.setRuleUuid(r1.getUuid()));
  986. db.getSession().commit();
  987. Set<DeprecatedRuleKeyDto> deprecatedRuleKeyDtos = underTest.selectAllDeprecatedRuleKeys(db.getSession());
  988. assertThat(deprecatedRuleKeyDtos).hasSize(1);
  989. DeprecatedRuleKeyDto deprecatedRuleKeyDto1 = deprecatedRuleKeyDtos.iterator().next();
  990. assertThat(deprecatedRuleKeyDto1.getOldRepositoryKey()).isEqualTo(deprecatedRuleKeyDto.getOldRepositoryKey());
  991. assertThat(deprecatedRuleKeyDto1.getOldRuleKey()).isEqualTo(deprecatedRuleKeyDto.getOldRuleKey());
  992. assertThat(deprecatedRuleKeyDto1.getNewRepositoryKey()).isEqualTo(r1.getRepositoryKey());
  993. assertThat(deprecatedRuleKeyDto1.getNewRuleKey()).isEqualTo(r1.getRuleKey());
  994. assertThat(deprecatedRuleKeyDto1.getUuid()).isEqualTo(deprecatedRuleKeyDto.getUuid());
  995. assertThat(deprecatedRuleKeyDto1.getCreatedAt()).isEqualTo(deprecatedRuleKeyDto.getCreatedAt());
  996. assertThat(deprecatedRuleKeyDto1.getRuleUuid()).isEqualTo(r1.getUuid());
  997. }
  998. @Test
  999. public void countByLanguage_shouldReturnExpectedNumberOfRules() {
  1000. insertMultipleRules(10, "js", false, READY);
  1001. insertMultipleRules(5, "js", true, READY);
  1002. insertMultipleRules(3, "kotlin", false, DEPRECATED);
  1003. insertMultipleRules(3, "java", false, REMOVED);
  1004. db.getSession().commit();
  1005. assertThat(underTest.countByLanguage(db.getSession(), "js")).isEqualTo(10);
  1006. assertThat(underTest.countByLanguage(db.getSession(), "kotlin")).isEqualTo(3);
  1007. assertThat(underTest.countByLanguage(db.getSession(), "java")).isZero();
  1008. }
  1009. private void insertMultipleRules(int rulesCount, String language, boolean isExternal, RuleStatus ruleStatus) {
  1010. for (int i = 0; i < rulesCount; i++) {
  1011. db.rules().insert(rule -> rule.setLanguage(language).setIsExternal(isExternal).setStatus(ruleStatus));
  1012. }
  1013. }
  1014. @Test
  1015. public void insertDeprecatedRuleKey_with_same_RuleKey_should_fail() {
  1016. String repositoryKey = randomAlphanumeric(50);
  1017. String ruleKey = randomAlphanumeric(50);
  1018. RuleDbTester ruleTester = db.rules();
  1019. ruleTester.insertDeprecatedKey(d -> d.setOldRepositoryKey(repositoryKey)
  1020. .setOldRuleKey(ruleKey));
  1021. assertThatThrownBy(() -> {
  1022. ruleTester.insertDeprecatedKey(d -> d.setOldRepositoryKey(repositoryKey)
  1023. .setOldRuleKey(ruleKey));
  1024. })
  1025. .isInstanceOf(PersistenceException.class);
  1026. }
  1027. private static ImpactDto newRuleDefaultImpact(SoftwareQuality softwareQuality, org.sonar.api.issue.impact.Severity severity) {
  1028. return new ImpactDto()
  1029. .setUuid(UuidFactoryFast.getInstance().create())
  1030. .setSoftwareQuality(softwareQuality)
  1031. .setSeverity(severity);
  1032. }
  1033. private static RuleDescriptionSectionDto createDefaultRuleDescriptionSection() {
  1034. return RuleDescriptionSectionDto.createDefaultRuleDescriptionSection(UuidFactoryFast.getInstance().create(), RandomStringUtils.randomAlphanumeric(1000));
  1035. }
  1036. private static class Accumulator<T> implements Consumer<T> {
  1037. private final List<T> list = new ArrayList<>();
  1038. @Override
  1039. public void accept(T dto) {
  1040. list.add(dto);
  1041. }
  1042. }
  1043. }