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.

ActiveRuleDaoTest.java 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import java.util.Objects;
  25. import java.util.function.Consumer;
  26. import org.junit.Before;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.rules.ExpectedException;
  30. import org.sonar.api.rule.Severity;
  31. import org.sonar.api.rules.RuleType;
  32. import org.sonar.api.server.rule.RuleParamType;
  33. import org.sonar.api.utils.System2;
  34. import org.sonar.api.impl.utils.TestSystem2;
  35. import org.sonar.db.DbSession;
  36. import org.sonar.db.DbTester;
  37. import org.sonar.db.organization.OrganizationDto;
  38. import org.sonar.db.rule.RuleDefinitionDto;
  39. import org.sonar.db.rule.RuleParamDto;
  40. import static com.google.common.collect.Lists.newArrayList;
  41. import static java.util.Arrays.asList;
  42. import static java.util.Collections.emptyList;
  43. import static java.util.Collections.singletonList;
  44. import static org.assertj.core.api.Assertions.assertThat;
  45. import static org.assertj.core.api.Assertions.entry;
  46. import static org.assertj.core.api.Assertions.tuple;
  47. import static org.sonar.api.rule.RuleStatus.BETA;
  48. import static org.sonar.api.rule.RuleStatus.READY;
  49. import static org.sonar.api.rule.RuleStatus.REMOVED;
  50. import static org.sonar.api.rule.Severity.BLOCKER;
  51. import static org.sonar.api.rule.Severity.MAJOR;
  52. import static org.sonar.db.qualityprofile.ActiveRuleDto.INHERITED;
  53. import static org.sonar.db.qualityprofile.ActiveRuleDto.OVERRIDES;
  54. import static org.sonar.db.qualityprofile.ActiveRuleDto.createFor;
  55. public class ActiveRuleDaoTest {
  56. @Rule
  57. public ExpectedException thrown = ExpectedException.none();
  58. private static final long NOW = 10_000_000L;
  59. private OrganizationDto organization;
  60. private QProfileDto profile1;
  61. private QProfileDto profile2;
  62. private RuleDefinitionDto rule1;
  63. private RuleDefinitionDto rule2;
  64. private RuleDefinitionDto rule3;
  65. private RuleDefinitionDto removedRule;
  66. private RuleParamDto rule1Param1;
  67. private RuleParamDto rule1Param2;
  68. private RuleParamDto rule2Param1;
  69. private System2 system = new TestSystem2().setNow(NOW);
  70. @Rule
  71. public DbTester db = DbTester.create(system);
  72. private DbSession dbSession = db.getSession();
  73. private ActiveRuleDao underTest = db.getDbClient().activeRuleDao();
  74. @Before
  75. public void setUp() {
  76. organization = db.organizations().insert();
  77. profile1 = db.qualityProfiles().insert(organization);
  78. profile2 = db.qualityProfiles().insert(organization);
  79. rule1 = db.rules().insert();
  80. rule2 = db.rules().insert();
  81. rule3 = db.rules().insert();
  82. removedRule = db.rules().insert(r -> r.setStatus(REMOVED));
  83. rule1Param1 = new RuleParamDto()
  84. .setName("param1")
  85. .setDefaultValue("value1")
  86. .setType(RuleParamType.STRING.toString());
  87. rule1Param1 = db.rules().insertRuleParam(rule1);
  88. rule1Param2 = db.rules().insertRuleParam(rule1);
  89. rule2Param1 = db.rules().insertRuleParam(rule2);
  90. }
  91. @Test
  92. public void selectByKey() {
  93. ActiveRuleDto activeRule = createFor(profile1, rule1).setSeverity(BLOCKER);
  94. underTest.insert(dbSession, activeRule);
  95. assertThat(underTest.selectByKey(dbSession, activeRule.getKey())).isPresent();
  96. assertThat(underTest.selectByKey(dbSession, ActiveRuleKey.of(profile2, rule2.getKey()))).isEmpty();
  97. }
  98. @Test
  99. public void selectByRuleId() {
  100. ActiveRuleDto activeRule1 = createFor(profile1, rule1).setSeverity(BLOCKER);
  101. ActiveRuleDto activeRule2 = createFor(profile2, rule1).setSeverity(BLOCKER);
  102. underTest.insert(dbSession, activeRule1);
  103. underTest.insert(dbSession, activeRule2);
  104. dbSession.commit();
  105. assertThat(underTest.selectByRuleId(dbSession, organization, rule1.getId())).extracting("key").containsOnly(activeRule1.getKey(), activeRule2.getKey());
  106. assertThat(underTest.selectByRuleId(dbSession, organization, rule3.getId())).isEmpty();
  107. }
  108. @Test
  109. public void selectByRuleIds() {
  110. ActiveRuleDto activeRule1 = createFor(profile1, rule1).setSeverity(BLOCKER);
  111. ActiveRuleDto activeRule2 = createFor(profile1, rule2).setSeverity(BLOCKER);
  112. ActiveRuleDto activeRule3 = createFor(profile2, rule1).setSeverity(BLOCKER);
  113. underTest.insert(dbSession, activeRule1);
  114. underTest.insert(dbSession, activeRule2);
  115. underTest.insert(dbSession, activeRule3);
  116. dbSession.commit();
  117. assertThat(underTest.selectByRuleIds(dbSession, organization, asList(rule1.getId())))
  118. .extracting("key").containsOnly(activeRule1.getKey(), activeRule3.getKey());
  119. assertThat(underTest.selectByRuleIds(dbSession, organization, newArrayList(rule1.getId(), rule2.getId())))
  120. .extracting("key").containsOnly(activeRule1.getKey(), activeRule2.getKey(), activeRule3.getKey());
  121. }
  122. @Test
  123. public void selectByProfile() {
  124. ActiveRuleDto activeRule1 = createFor(profile1, rule1).setSeverity(BLOCKER);
  125. ActiveRuleDto activeRule2 = createFor(profile1, rule2).setSeverity(BLOCKER);
  126. underTest.insert(dbSession, activeRule1);
  127. underTest.insert(dbSession, activeRule2);
  128. List<OrgActiveRuleDto> result = underTest.selectByProfile(dbSession, profile1);
  129. assertThat(result)
  130. .hasSize(2)
  131. .extracting(OrgActiveRuleDto::getOrganizationUuid, OrgActiveRuleDto::getProfileUuid, OrgActiveRuleDto::getProfileId)
  132. .containsOnly(tuple(organization.getUuid(), profile1.getKee(), profile1.getId()));
  133. assertThat(underTest.selectByProfile(dbSession, profile2)).isEmpty();
  134. }
  135. @Test
  136. public void selectByProfileUuid_ignores_removed_rules() {
  137. ActiveRuleDto activeRule = createFor(profile1, removedRule).setSeverity(BLOCKER);
  138. underTest.insert(dbSession, activeRule);
  139. assertThat(underTest.selectByProfile(dbSession, profile1)).isEmpty();
  140. }
  141. @Test
  142. public void selectByTypeAndProfileUuids() {
  143. RuleDefinitionDto rule1 = db.rules().insert(r -> r.setType(RuleType.VULNERABILITY.getDbConstant()));
  144. ActiveRuleDto activeRule1 = createFor(profile1, rule1).setSeverity(BLOCKER);
  145. underTest.insert(dbSession, activeRule1);
  146. assertThat(underTest.selectByTypeAndProfileUuids(dbSession, singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList(profile1.getKee())))
  147. .extracting(OrgActiveRuleDto::getProfileUuid, OrgActiveRuleDto::getOrganizationUuid, OrgActiveRuleDto::getRuleId)
  148. .contains(tuple(profile1.getKee(), profile1.getOrganizationUuid(), rule1.getId()));
  149. }
  150. @Test
  151. public void selectByTypeAndProfileUuids_ignores_rules_in_other_profiles() {
  152. RuleDefinitionDto rule1 = db.rules().insert(r -> r.setType(RuleType.VULNERABILITY.getDbConstant()));
  153. ActiveRuleDto activeRule1 = createFor(profile2, rule1).setSeverity(BLOCKER);
  154. underTest.insert(dbSession, activeRule1);
  155. assertThat(underTest.selectByTypeAndProfileUuids(dbSession, singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList(profile1.getKee())))
  156. .isEmpty();
  157. }
  158. @Test
  159. public void selectByTypeAndProfileUuids_ignores_rules_with_another_rule_type() {
  160. RuleDefinitionDto rule1 = db.rules().insert(r -> r.setType(RuleType.VULNERABILITY.getDbConstant()));
  161. ActiveRuleDto activeRule1 = createFor(profile1, rule1).setSeverity(BLOCKER);
  162. underTest.insert(dbSession, activeRule1);
  163. assertThat(
  164. underTest.selectByTypeAndProfileUuids(dbSession,
  165. singletonList(RuleType.VULNERABILITY.getDbConstant()),
  166. singletonList(profile1.getKee())))
  167. .extracting(OrgActiveRuleDto::getProfileUuid, OrgActiveRuleDto::getOrganizationUuid, OrgActiveRuleDto::getRuleId)
  168. .contains(tuple(profile1.getKee(), profile1.getOrganizationUuid(), rule1.getId()));
  169. assertThat(
  170. underTest.selectByTypeAndProfileUuids(dbSession,
  171. asList(RuleType.CODE_SMELL.getDbConstant(), RuleType.SECURITY_HOTSPOT.getDbConstant(), RuleType.BUG.getDbConstant()),
  172. singletonList(profile1.getKee())))
  173. .isEmpty();
  174. }
  175. @Test
  176. public void selectByRuleProfile() {
  177. ActiveRuleDto activeRule1 = createFor(profile1, rule1).setSeverity(BLOCKER);
  178. ActiveRuleDto activeRule2 = createFor(profile1, rule2).setSeverity(MAJOR);
  179. underTest.insert(dbSession, activeRule1);
  180. underTest.insert(dbSession, activeRule2);
  181. List<ActiveRuleDto> result = underTest.selectByRuleProfile(dbSession, RulesProfileDto.from(profile1));
  182. assertThat(result)
  183. .hasSize(2)
  184. .extracting(ActiveRuleDto::getProfileId, ActiveRuleDto::getRuleKey, ActiveRuleDto::getSeverityString)
  185. .containsOnly(tuple(profile1.getId(), rule1.getKey(), BLOCKER), tuple(profile1.getId(), rule2.getKey(), MAJOR));
  186. assertThat(underTest.selectByProfile(dbSession, profile2)).isEmpty();
  187. }
  188. @Test
  189. public void selectByRulesAndRuleProfileUuids() {
  190. ActiveRuleDto rule1P1 = createFor(profile1, rule1).setSeverity(MAJOR);
  191. ActiveRuleDto rule2P1 = createFor(profile1, rule2).setSeverity(MAJOR);
  192. ActiveRuleDto rule1P2 = createFor(profile2, rule1).setSeverity(MAJOR);
  193. underTest.insert(dbSession, rule1P1);
  194. underTest.insert(dbSession, rule2P1);
  195. underTest.insert(dbSession, rule1P2);
  196. // empty rules
  197. Collection<ActiveRuleDto> result = underTest.selectByRulesAndRuleProfileUuids(dbSession, emptyList(), asList(profile1.getRulesProfileUuid()));
  198. assertThat(result).isEmpty();
  199. // empty profiles
  200. result = underTest.selectByRulesAndRuleProfileUuids(dbSession, asList(rule1.getId()), emptyList());
  201. assertThat(result).isEmpty();
  202. // match
  203. result = underTest.selectByRulesAndRuleProfileUuids(dbSession, asList(rule1.getId()), asList(profile1.getRulesProfileUuid(), profile2.getRulesProfileUuid()));
  204. assertThat(result)
  205. .extracting(ActiveRuleDto::getId)
  206. .containsExactlyInAnyOrder(rule1P1.getId(), rule1P2.getId());
  207. result = underTest.selectByRulesAndRuleProfileUuids(dbSession, asList(rule1.getId(), rule2.getId()), asList(profile1.getRulesProfileUuid(), profile2.getRulesProfileUuid()));
  208. assertThat(result)
  209. .extracting(ActiveRuleDto::getId)
  210. .containsExactlyInAnyOrder(rule1P1.getId(), rule1P2.getId(), rule2P1.getId());
  211. // do not match
  212. result = underTest.selectByRulesAndRuleProfileUuids(dbSession, asList(rule3.getId()), asList(profile1.getRulesProfileUuid(), profile2.getRulesProfileUuid()));
  213. assertThat(result).isEmpty();
  214. result = underTest.selectByRulesAndRuleProfileUuids(dbSession, asList(rule1.getId()), asList("unknown"));
  215. assertThat(result).isEmpty();
  216. }
  217. @Test
  218. public void insert() {
  219. ActiveRuleDto activeRule = createFor(profile1, rule1)
  220. .setSeverity(BLOCKER)
  221. .setInheritance(INHERITED)
  222. .setCreatedAt(1000L)
  223. .setUpdatedAt(2000L);
  224. underTest.insert(dbSession, activeRule);
  225. dbSession.commit();
  226. ActiveRuleDto result = underTest.selectByKey(dbSession, activeRule.getKey()).get();
  227. assertThat(result.getId()).isEqualTo(activeRule.getId());
  228. assertThat(result.getKey()).isEqualTo(ActiveRuleKey.of(profile1, rule1.getKey()));
  229. assertThat(result.getRuleId()).isEqualTo(rule1.getId());
  230. assertThat(result.getProfileId()).isEqualTo(profile1.getId());
  231. assertThat(result.getSeverityString()).isEqualTo(BLOCKER);
  232. assertThat(result.getInheritance()).isEqualTo(INHERITED);
  233. assertThat(result.getCreatedAt()).isEqualTo(1000L);
  234. assertThat(result.getUpdatedAt()).isEqualTo(2000L);
  235. }
  236. @Test
  237. public void fail_to_insert_when_profile_id_is_null() {
  238. thrown.expect(IllegalArgumentException.class);
  239. thrown.expectMessage("Quality profile is not persisted (missing id)");
  240. underTest.insert(dbSession, createFor(profile1, rule1).setProfileId(null));
  241. }
  242. @Test
  243. public void fail_to_insert_when_rule_id_is_null() {
  244. thrown.expect(IllegalArgumentException.class);
  245. thrown.expectMessage("Rule is not persisted");
  246. underTest.insert(dbSession, createFor(profile1, rule1).setRuleId(null));
  247. }
  248. @Test
  249. public void fail_to_insert_when_id_is_not_null() {
  250. thrown.expect(IllegalArgumentException.class);
  251. thrown.expectMessage("ActiveRule is already persisted");
  252. underTest.insert(dbSession, createFor(profile1, rule1).setId(100));
  253. }
  254. @Test
  255. public void update() {
  256. ActiveRuleDto activeRule = createFor(profile1, rule1)
  257. .setSeverity(BLOCKER)
  258. .setInheritance(INHERITED)
  259. .setCreatedAt(1000L)
  260. .setUpdatedAt(2000L);
  261. underTest.insert(dbSession, activeRule);
  262. dbSession.commit();
  263. ActiveRuleDto activeRuleUpdated = activeRule
  264. .setSeverity(MAJOR)
  265. .setInheritance(OVERRIDES)
  266. // created at should not be updated
  267. .setCreatedAt(3000L)
  268. .setUpdatedAt(4000L);
  269. underTest.update(dbSession, activeRuleUpdated);
  270. dbSession.commit();
  271. ActiveRuleDto result = underTest.selectByKey(dbSession, ActiveRuleKey.of(profile1, rule1.getKey())).get();
  272. assertThat(result.getId()).isEqualTo(activeRule.getId());
  273. assertThat(result.getKey()).isEqualTo(ActiveRuleKey.of(profile1, rule1.getKey()));
  274. assertThat(result.getRuleId()).isEqualTo(rule1.getId());
  275. assertThat(result.getProfileId()).isEqualTo(profile1.getId());
  276. assertThat(result.getSeverityString()).isEqualTo(MAJOR);
  277. assertThat(result.getInheritance()).isEqualTo(OVERRIDES);
  278. assertThat(result.getCreatedAt()).isEqualTo(1000L);
  279. assertThat(result.getUpdatedAt()).isEqualTo(4000L);
  280. }
  281. @Test
  282. public void fail_to_update_when_profile_id_is_null() {
  283. thrown.expect(IllegalArgumentException.class);
  284. thrown.expectMessage("Quality profile is not persisted (missing id)");
  285. underTest.update(dbSession, createFor(profile1, rule1).setId(100).setProfileId(null));
  286. }
  287. @Test
  288. public void fail_to_update_when_rule_id_is_null() {
  289. thrown.expect(IllegalArgumentException.class);
  290. thrown.expectMessage("Rule is not persisted");
  291. underTest.update(dbSession, createFor(profile1, rule1).setId(100).setRuleId(null));
  292. }
  293. @Test
  294. public void fail_to_update_when_id_is_null() {
  295. thrown.expect(IllegalArgumentException.class);
  296. thrown.expectMessage("ActiveRule is not persisted");
  297. underTest.update(dbSession, createFor(profile1, rule1).setId(null));
  298. }
  299. @Test
  300. public void delete() {
  301. ActiveRuleDto activeRule = createFor(profile1, rule1)
  302. .setSeverity(BLOCKER)
  303. .setInheritance(INHERITED)
  304. .setCreatedAt(1000L)
  305. .setUpdatedAt(2000L);
  306. underTest.insert(dbSession, activeRule);
  307. underTest.delete(dbSession, activeRule.getKey());
  308. assertThat(underTest.selectByKey(dbSession, ActiveRuleKey.of(profile1, rule1.getKey()))).isEmpty();
  309. }
  310. @Test
  311. public void delete_does_not_fail_when_active_rule_does_not_exist() {
  312. underTest.delete(dbSession, ActiveRuleKey.of(profile1, rule1.getKey()));
  313. }
  314. @Test
  315. public void deleteByRuleProfileUuids_deletes_rows_from_table() {
  316. underTest.insert(dbSession, newRow(profile1, rule1));
  317. underTest.insert(dbSession, newRow(profile1, rule2));
  318. underTest.insert(dbSession, newRow(profile2, rule1));
  319. underTest.deleteByRuleProfileUuids(dbSession, asList(profile1.getRulesProfileUuid()));
  320. assertThat(db.countRowsOfTable(dbSession, "active_rules")).isEqualTo(1);
  321. assertThat(underTest.selectByKey(dbSession, ActiveRuleKey.of(profile2, rule1.getKey()))).isPresent();
  322. }
  323. @Test
  324. public void deleteByRuleProfileUuids_does_not_fail_when_rules_profile_with_specified_key_does_not_exist() {
  325. underTest.insert(dbSession, newRow(profile1, rule1));
  326. underTest.deleteByRuleProfileUuids(dbSession, asList("does_not_exist"));
  327. assertThat(db.countRowsOfTable(dbSession, "active_rules")).isEqualTo(1);
  328. }
  329. @Test
  330. public void deleteByIds() {
  331. ActiveRuleDto ar1 = underTest.insert(dbSession, newRow(profile1, rule1));
  332. ActiveRuleDto ar2 = underTest.insert(dbSession, newRow(profile1, rule2));
  333. ActiveRuleDto ar3 = underTest.insert(dbSession, newRow(profile2, rule1));
  334. underTest.deleteByIds(dbSession, asList(ar1.getId(), ar3.getId()));
  335. assertThat(db.countRowsOfTable(dbSession, "active_rules")).isEqualTo(1);
  336. assertThat(underTest.selectByProfile(dbSession, profile1))
  337. .extracting(ActiveRuleDto::getId)
  338. .containsExactly(ar2.getId());
  339. }
  340. @Test
  341. public void deleteByIds_does_nothing_if_empty_list_of_ids() {
  342. underTest.insert(dbSession, newRow(profile1, rule1));
  343. underTest.deleteByIds(dbSession, emptyList());
  344. assertThat(db.countRowsOfTable(dbSession, "active_rules")).isEqualTo(1);
  345. }
  346. private static ActiveRuleDto newRow(QProfileDto profile, RuleDefinitionDto rule) {
  347. return createFor(profile, rule).setSeverity(BLOCKER);
  348. }
  349. @Test
  350. public void select_params_by_active_rule_id() {
  351. ActiveRuleDto activeRule = createFor(profile1, rule1).setSeverity(BLOCKER);
  352. underTest.insert(dbSession, activeRule);
  353. ActiveRuleParamDto activeRuleParam1 = ActiveRuleParamDto.createFor(rule1Param1);
  354. underTest.insertParam(dbSession, activeRule, activeRuleParam1);
  355. ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(rule1Param2);
  356. underTest.insertParam(dbSession, activeRule, activeRuleParam2);
  357. dbSession.commit();
  358. assertThat(underTest.selectParamsByActiveRuleId(dbSession, activeRule.getId())).hasSize(2);
  359. }
  360. @Test
  361. public void select_params_by_active_rule_ids() {
  362. ActiveRuleDto activeRule1 = createFor(profile1, rule1).setSeverity(BLOCKER);
  363. underTest.insert(dbSession, activeRule1);
  364. underTest.insertParam(dbSession, activeRule1, ActiveRuleParamDto.createFor(rule1Param1));
  365. underTest.insertParam(dbSession, activeRule1, ActiveRuleParamDto.createFor(rule1Param2));
  366. ActiveRuleDto activeRule2 = createFor(profile1, rule2).setSeverity(BLOCKER);
  367. underTest.insert(dbSession, activeRule2);
  368. underTest.insertParam(dbSession, activeRule2, ActiveRuleParamDto.createFor(rule2Param1));
  369. dbSession.commit();
  370. assertThat(underTest.selectParamsByActiveRuleIds(dbSession, asList(activeRule1.getId(), activeRule2.getId()))).hasSize(3);
  371. }
  372. @Test
  373. public void insertParam() {
  374. ActiveRuleDto activeRule = createFor(profile1, rule1).setSeverity(Severity.CRITICAL);
  375. underTest.insert(dbSession, activeRule);
  376. ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo");
  377. underTest.insertParam(dbSession, activeRule, activeRuleParam);
  378. List<ActiveRuleParamDto> reloaded = underTest.selectParamsByActiveRuleId(dbSession, activeRule.getId());
  379. assertThat(reloaded).hasSize(1);
  380. assertThat(reloaded.get(0))
  381. .matches(p -> Objects.equals(p.getId(), activeRuleParam.getId()))
  382. .matches(p -> p.getKey().equals(activeRuleParam.getKey()))
  383. .matches(p -> p.getActiveRuleId().equals(activeRule.getId()))
  384. .matches(p -> p.getRulesParameterId().equals(rule1Param1.getId()))
  385. .matches(p -> p.getValue().equals("foo"));
  386. }
  387. @Test
  388. public void insertParam_fails_when_active_rule_id_is_null() {
  389. thrown.expect(IllegalArgumentException.class);
  390. thrown.expectMessage("ActiveRule is not persisted");
  391. underTest.insertParam(dbSession,
  392. createFor(profile1, rule1).setId(null),
  393. ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1"));
  394. }
  395. @Test
  396. public void insertParam_fails_when_active_rule_param_id_is_null() {
  397. thrown.expect(IllegalArgumentException.class);
  398. thrown.expectMessage("ActiveRuleParam is already persisted");
  399. underTest.insertParam(dbSession,
  400. createFor(profile1, rule1).setId(100),
  401. ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1").setId(100));
  402. }
  403. @Test
  404. public void insertParam_fails_when_id_is_not_null() {
  405. thrown.expect(NullPointerException.class);
  406. thrown.expectMessage("Rule param is not persisted");
  407. underTest.insertParam(dbSession,
  408. createFor(profile1, rule1).setId(100),
  409. ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1").setRulesParameterId(null));
  410. }
  411. @Test
  412. public void updateParam() {
  413. ActiveRuleDto activeRule = createFor(profile1, rule1).setSeverity(Severity.CRITICAL);
  414. underTest.insert(dbSession, activeRule);
  415. ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo");
  416. underTest.insertParam(dbSession, activeRule, activeRuleParam);
  417. underTest.updateParam(dbSession, activeRuleParam.setValue("bar"));
  418. List<ActiveRuleParamDto> reloaded = underTest.selectParamsByActiveRuleId(dbSession, activeRule.getId());
  419. assertThat(reloaded).hasSize(1);
  420. assertThat(reloaded.get(0))
  421. .matches(p -> Objects.equals(p.getId(), activeRuleParam.getId()))
  422. .matches(p -> p.getKey().equals(activeRuleParam.getKey()))
  423. .matches(p -> p.getActiveRuleId().equals(activeRule.getId()))
  424. .matches(p -> p.getRulesParameterId().equals(rule1Param1.getId()))
  425. .matches(p -> p.getValue().equals("bar"));
  426. }
  427. @Test
  428. public void deleteParam_deletes_rows_by_id() {
  429. ActiveRuleDto activeRule = newRow(profile1, rule1);
  430. underTest.insert(dbSession, activeRule);
  431. ActiveRuleParamDto param = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo");
  432. underTest.insertParam(dbSession, activeRule, param);
  433. underTest.deleteParam(dbSession, param);
  434. assertThat(underTest.selectParamsByActiveRuleId(dbSession, activeRule.getId())).hasSize(0);
  435. }
  436. @Test
  437. public void deleteParametersByRuleProfileUuids_deletes_rows_by_rule_profile_uuids() {
  438. ActiveRuleDto activeRuleInProfile1 = newRow(profile1, rule1);
  439. underTest.insert(dbSession, activeRuleInProfile1);
  440. ActiveRuleParamDto param1 = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo");
  441. underTest.insertParam(dbSession, activeRuleInProfile1, param1);
  442. ActiveRuleDto activeRuleInProfile2 = newRow(profile2, rule1);
  443. underTest.insert(dbSession, activeRuleInProfile2);
  444. ActiveRuleParamDto param2 = ActiveRuleParamDto.createFor(rule1Param1).setValue("bar");
  445. underTest.insertParam(dbSession, activeRuleInProfile2, param2);
  446. underTest.deleteParametersByRuleProfileUuids(dbSession, asList(profile1.getRulesProfileUuid(), "does_not_exist"));
  447. assertThat(underTest.selectParamsByActiveRuleId(dbSession, activeRuleInProfile1.getId())).isEmpty();
  448. assertThat(underTest.selectParamsByActiveRuleId(dbSession, activeRuleInProfile2.getId()))
  449. .extracting(ActiveRuleParamDto::getKey, ActiveRuleParamDto::getValue)
  450. .containsExactly(tuple(rule1Param1.getName(), "bar"));
  451. }
  452. @Test
  453. public void deleteParametersByRuleProfileUuids_does_nothing_if_keys_are_empty() {
  454. ActiveRuleDto activeRuleInProfile1 = newRow(profile1, rule1);
  455. underTest.insert(dbSession, activeRuleInProfile1);
  456. ActiveRuleParamDto param1 = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo");
  457. underTest.insertParam(dbSession, activeRuleInProfile1, param1);
  458. underTest.deleteParametersByRuleProfileUuids(dbSession, emptyList());
  459. assertThat(underTest.selectParamsByActiveRuleId(dbSession, activeRuleInProfile1.getId()))
  460. .hasSize(1);
  461. }
  462. @Test
  463. public void deleteParamsByRuleParamOfAllOrganizations() {
  464. ActiveRuleDto activeRule1 = createFor(profile1, rule1).setSeverity(BLOCKER);
  465. underTest.insert(dbSession, activeRule1);
  466. ActiveRuleParamDto activeRuleParam1 = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo");
  467. underTest.insertParam(dbSession, activeRule1, activeRuleParam1);
  468. ActiveRuleDto activeRule2 = createFor(profile2, rule1).setSeverity(BLOCKER);
  469. underTest.insert(dbSession, activeRule2);
  470. ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(rule1Param1).setValue("bar");
  471. underTest.insertParam(dbSession, activeRule2, activeRuleParam2);
  472. List<Integer> activeRuleIds = asList(activeRule1.getId(), activeRule2.getId());
  473. assertThat(underTest.selectParamsByActiveRuleIds(dbSession, activeRuleIds)).hasSize(2);
  474. underTest.deleteParamsByRuleParamOfAllOrganizations(dbSession, rule1Param1);
  475. assertThat(underTest.selectParamsByActiveRuleIds(dbSession, activeRuleIds)).isEmpty();
  476. }
  477. @Test
  478. public void deleteParamsByActiveRuleIds() {
  479. ActiveRuleDto ar1 = underTest.insert(dbSession, newRow(profile1, rule1));
  480. ActiveRuleParamDto param = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo");
  481. underTest.insertParam(dbSession, ar1, param);
  482. ActiveRuleDto ar2 = underTest.insert(dbSession, newRow(profile1, rule2));
  483. ActiveRuleParamDto param2 = ActiveRuleParamDto.createFor(rule2Param1).setValue("bar");
  484. underTest.insertParam(dbSession, ar2, param2);
  485. underTest.deleteParamsByActiveRuleIds(dbSession, asList(ar1.getId()));
  486. assertThat(underTest.selectParamsByActiveRuleId(dbSession, ar1.getId())).hasSize(0);
  487. assertThat(underTest.selectParamsByActiveRuleId(dbSession, ar2.getId())).hasSize(1);
  488. }
  489. @Test
  490. public void countActiveRulesByQuery_filter_by_profiles() {
  491. db.qualityProfiles().activateRule(profile1, rule1);
  492. db.qualityProfiles().activateRule(profile1, rule2);
  493. db.qualityProfiles().activateRule(profile1, removedRule);
  494. db.qualityProfiles().activateRule(profile2, rule1);
  495. QProfileDto profileWithoutActiveRule = db.qualityProfiles().insert(organization);
  496. ActiveRuleCountQuery.Builder builder = ActiveRuleCountQuery.builder().setOrganization(organization);
  497. assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(asList(profile1, profile2)).build()))
  498. .containsOnly(entry(profile1.getKee(), 2L), entry(profile2.getKee(), 1L));
  499. assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(asList(profileWithoutActiveRule)).build())).isEmpty();
  500. assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(asList(profile1, profile2, profileWithoutActiveRule)).build())).containsOnly(
  501. entry(profile1.getKee(), 2L),
  502. entry(profile2.getKee(), 1L));
  503. assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(emptyList()).build())).isEmpty();
  504. }
  505. @Test
  506. public void countActiveRulesByQuery_filter_by_rule_status() {
  507. RuleDefinitionDto betaRule = db.rules().insert(r -> r.setStatus(BETA));
  508. db.qualityProfiles().activateRule(profile1, rule1);
  509. db.qualityProfiles().activateRule(profile1, rule2);
  510. db.qualityProfiles().activateRule(profile1, betaRule);
  511. db.qualityProfiles().activateRule(profile1, removedRule);
  512. db.qualityProfiles().activateRule(profile2, rule1);
  513. db.qualityProfiles().activateRule(profile2, betaRule);
  514. ActiveRuleCountQuery.Builder builder = ActiveRuleCountQuery.builder().setOrganization(organization);
  515. assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(asList(profile1, profile2)).setRuleStatus(BETA).build()))
  516. .containsOnly(entry(profile1.getKee(), 1L), entry(profile2.getKee(), 1L));
  517. assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(asList(profile1)).setRuleStatus(READY).build()))
  518. .containsOnly(entry(profile1.getKee(), 2L));
  519. assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(asList(profile1)).setRuleStatus(REMOVED).build()))
  520. .containsOnly(entry(profile1.getKee(), 1L));
  521. }
  522. @Test
  523. public void countActiveRulesByQuery_filter_by_inheritance() {
  524. db.qualityProfiles().activateRule(profile1, rule1);
  525. db.qualityProfiles().activateRule(profile1, rule2, ar -> ar.setInheritance(OVERRIDES));
  526. db.qualityProfiles().activateRule(profile1, removedRule, ar -> ar.setInheritance(OVERRIDES));
  527. db.qualityProfiles().activateRule(profile2, rule1, ar -> ar.setInheritance(OVERRIDES));
  528. db.qualityProfiles().activateRule(profile2, rule2, ar -> ar.setInheritance(INHERITED));
  529. ActiveRuleCountQuery.Builder builder = ActiveRuleCountQuery.builder().setOrganization(organization);
  530. assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(asList(profile1, profile2)).setInheritance(OVERRIDES).build()))
  531. .containsOnly(entry(profile1.getKee(), 1L), entry(profile2.getKee(), 1L));
  532. assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(asList(profile1, profile2)).setInheritance(INHERITED).build()))
  533. .containsOnly(entry(profile2.getKee(), 1L));
  534. }
  535. @Test
  536. public void countActiveRulesByQuery_filter_by_organization() {
  537. db.qualityProfiles().activateRule(profile1, rule1);
  538. OrganizationDto anotherOrganization = db.organizations().insert();
  539. QProfileDto profileOnAnotherOrganization = db.qualityProfiles().insert(anotherOrganization);
  540. db.qualityProfiles().activateRule(profileOnAnotherOrganization, rule1);
  541. assertThat(underTest.countActiveRulesByQuery(dbSession,
  542. ActiveRuleCountQuery.builder().setOrganization(organization).setProfiles(asList(profile1, profileOnAnotherOrganization)).build()))
  543. .containsOnly(entry(profile1.getKee(), 1L));
  544. }
  545. @Test
  546. public void scrollAllForIndexing_empty_table() {
  547. Accumulator accumulator = new Accumulator();
  548. underTest.scrollAllForIndexing(dbSession, accumulator);
  549. assertThat(accumulator.list).isEmpty();
  550. }
  551. @Test
  552. public void scrollAllForIndexing() {
  553. ActiveRuleDto ar1 = db.qualityProfiles().activateRule(profile1, rule1);
  554. ActiveRuleDto ar2 = db.qualityProfiles().activateRule(profile2, rule1);
  555. ActiveRuleDto ar3 = db.qualityProfiles().activateRule(profile2, rule2);
  556. Accumulator accumulator = new Accumulator();
  557. underTest.scrollAllForIndexing(dbSession, accumulator);
  558. assertThat(accumulator.list)
  559. .extracting(IndexedActiveRuleDto::getId,
  560. IndexedActiveRuleDto::getRuleId, IndexedActiveRuleDto::getRepository, IndexedActiveRuleDto::getKey,
  561. IndexedActiveRuleDto::getRuleProfileUuid,
  562. IndexedActiveRuleDto::getSeverity, IndexedActiveRuleDto::getInheritance)
  563. .containsExactlyInAnyOrder(
  564. tuple((long)ar1.getId(), rule1.getId(), ar1.getRuleKey().repository(), ar1.getRuleKey().rule(), profile1.getRulesProfileUuid(), ar1.getSeverity(), ar1.getInheritance()),
  565. tuple((long)ar2.getId(), rule1.getId(), ar2.getRuleKey().repository(), ar2.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar2.getSeverity(), ar2.getInheritance()),
  566. tuple((long)ar3.getId(), rule2.getId(), ar3.getRuleKey().repository(), ar3.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar3.getSeverity(), ar3.getInheritance()));
  567. }
  568. @Test
  569. public void scrollByIdsForIndexing() {
  570. ActiveRuleDto ar1 = db.qualityProfiles().activateRule(profile1, rule1);
  571. ActiveRuleDto ar2 = db.qualityProfiles().activateRule(profile2, rule1);
  572. ActiveRuleDto ar3 = db.qualityProfiles().activateRule(profile2, rule2);
  573. Accumulator accumulator = new Accumulator();
  574. underTest.scrollByIdsForIndexing(dbSession, asList((long) ar1.getId(), (long) ar2.getId()), accumulator);
  575. assertThat(accumulator.list)
  576. .extracting(IndexedActiveRuleDto::getId,
  577. IndexedActiveRuleDto::getRuleId, IndexedActiveRuleDto::getRepository, IndexedActiveRuleDto::getKey,
  578. IndexedActiveRuleDto::getRuleProfileUuid, IndexedActiveRuleDto::getSeverity)
  579. .containsExactlyInAnyOrder(
  580. tuple((long)ar1.getId(), rule1.getId(), ar1.getRuleKey().repository(), ar1.getRuleKey().rule(), profile1.getRulesProfileUuid(), ar1.getSeverity()),
  581. tuple((long)ar2.getId(), rule1.getId(), ar2.getRuleKey().repository(), ar2.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar2.getSeverity()));
  582. }
  583. @Test
  584. public void scrollByRuleProfileForIndexing() {
  585. ActiveRuleDto ar1 = db.qualityProfiles().activateRule(profile1, rule1);
  586. ActiveRuleDto ar2 = db.qualityProfiles().activateRule(profile2, rule1);
  587. ActiveRuleDto ar3 = db.qualityProfiles().activateRule(profile2, rule2);
  588. Accumulator accumulator = new Accumulator();
  589. underTest.scrollByRuleProfileForIndexing(dbSession, profile2.getRulesProfileUuid(), accumulator);
  590. assertThat(accumulator.list)
  591. .extracting(IndexedActiveRuleDto::getId, IndexedActiveRuleDto::getRepository, IndexedActiveRuleDto::getKey, IndexedActiveRuleDto::getRuleProfileUuid,
  592. IndexedActiveRuleDto::getSeverity)
  593. .containsExactlyInAnyOrder(
  594. tuple((long) ar2.getId(), ar2.getRuleKey().repository(), ar2.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar2.getSeverity()),
  595. tuple((long) ar3.getId(), ar3.getRuleKey().repository(), ar3.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar3.getSeverity()));
  596. }
  597. private static class Accumulator implements Consumer<IndexedActiveRuleDto> {
  598. private final List<IndexedActiveRuleDto> list = new ArrayList<>();
  599. @Override
  600. public void accept(IndexedActiveRuleDto dto) {
  601. list.add(dto);
  602. }
  603. }
  604. }