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.

QProfileFactoryImplTest.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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.server.qualityprofile;
  21. import java.util.Collections;
  22. import java.util.Set;
  23. import org.assertj.core.api.AbstractObjectAssert;
  24. import org.junit.Before;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.junit.rules.ExpectedException;
  28. import org.mockito.ArgumentCaptor;
  29. import org.sonar.api.rule.Severity;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.api.utils.internal.AlwaysIncreasingSystem2;
  32. import org.sonar.core.util.SequenceUuidFactory;
  33. import org.sonar.core.util.Uuids;
  34. import org.sonar.db.DbSession;
  35. import org.sonar.db.DbTester;
  36. import org.sonar.db.component.ComponentDto;
  37. import org.sonar.db.organization.OrganizationDto;
  38. import org.sonar.db.qualityprofile.ActiveRuleDto;
  39. import org.sonar.db.qualityprofile.ActiveRuleParamDto;
  40. import org.sonar.db.qualityprofile.OrgQProfileDto;
  41. import org.sonar.db.qualityprofile.QProfileChangeDto;
  42. import org.sonar.db.qualityprofile.QProfileDto;
  43. import org.sonar.db.qualityprofile.RulesProfileDto;
  44. import org.sonar.db.rule.RuleDefinitionDto;
  45. import org.sonar.db.rule.RuleParamDto;
  46. import org.sonar.db.user.GroupDto;
  47. import org.sonar.db.user.UserDto;
  48. import org.sonar.server.exceptions.BadRequestException;
  49. import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
  50. import static java.util.Arrays.asList;
  51. import static org.assertj.core.api.Assertions.assertThat;
  52. import static org.mockito.ArgumentMatchers.any;
  53. import static org.mockito.ArgumentMatchers.anyCollection;
  54. import static org.mockito.Mockito.mock;
  55. import static org.mockito.Mockito.never;
  56. import static org.mockito.Mockito.verify;
  57. import static org.mockito.Mockito.verifyZeroInteractions;
  58. public class QProfileFactoryImplTest {
  59. private System2 system2 = new AlwaysIncreasingSystem2();
  60. @Rule
  61. public ExpectedException expectedException = ExpectedException.none();
  62. @Rule
  63. public DbTester db = DbTester.create(system2);
  64. private DbSession dbSession = db.getSession();
  65. private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class);
  66. private QProfileFactory underTest = new QProfileFactoryImpl(db.getDbClient(), new SequenceUuidFactory(), system2, activeRuleIndexer);
  67. private RuleDefinitionDto rule;
  68. private RuleParamDto ruleParam;
  69. @Before
  70. public void setUp() {
  71. rule = db.rules().insert();
  72. ruleParam = db.rules().insertRuleParam(rule);
  73. }
  74. @Test
  75. public void checkAndCreateCustom() {
  76. OrganizationDto organization = db.organizations().insert();
  77. QProfileDto profile = underTest.checkAndCreateCustom(dbSession, organization, new QProfileName("xoo", "P1"));
  78. assertThat(profile.getOrganizationUuid()).isEqualTo(organization.getUuid());
  79. assertThat(profile.getKee()).isNotEmpty();
  80. assertThat(profile.getName()).isEqualTo("P1");
  81. assertThat(profile.getLanguage()).isEqualTo("xoo");
  82. assertThat(profile.getId()).isNotNull();
  83. assertThat(profile.isBuiltIn()).isFalse();
  84. QProfileDto reloaded = db.getDbClient().qualityProfileDao().selectByNameAndLanguage(dbSession, organization, profile.getName(), profile.getLanguage());
  85. assertEqual(profile, reloaded);
  86. assertThat(db.getDbClient().qualityProfileDao().selectOrderedByOrganizationUuid(dbSession, organization)).extracting(QProfileDto::getKee).containsExactly(profile.getKee());
  87. }
  88. @Test
  89. public void checkAndCreateCustom_throws_BadRequestException_if_name_null() {
  90. QProfileName name = new QProfileName("xoo", null);
  91. OrganizationDto organization = db.organizations().insert();
  92. expectBadRequestException("quality_profiles.profile_name_cant_be_blank");
  93. underTest.checkAndCreateCustom(dbSession, organization, name);
  94. }
  95. @Test
  96. public void checkAndCreateCustom_throws_BadRequestException_if_name_empty() {
  97. QProfileName name = new QProfileName("xoo", "");
  98. OrganizationDto organization = db.organizations().insert();
  99. expectBadRequestException("quality_profiles.profile_name_cant_be_blank");
  100. underTest.checkAndCreateCustom(dbSession, organization, name);
  101. }
  102. @Test
  103. public void checkAndCreateCustom_throws_BadRequestException_if_already_exists() {
  104. QProfileName name = new QProfileName("xoo", "P1");
  105. OrganizationDto organization = db.organizations().insert();
  106. underTest.checkAndCreateCustom(dbSession, organization, name);
  107. dbSession.commit();
  108. expectBadRequestException("Quality profile already exists: xoo/P1");
  109. underTest.checkAndCreateCustom(dbSession, organization, name);
  110. }
  111. @Test
  112. public void delete_custom_profiles() {
  113. OrganizationDto org = db.organizations().insert();
  114. QProfileDto profile1 = createCustomProfile(org);
  115. QProfileDto profile2 = createCustomProfile(org);
  116. QProfileDto profile3 = createCustomProfile(org);
  117. underTest.delete(dbSession, asList(profile1, profile2));
  118. verifyCallActiveRuleIndexerDelete(profile1.getKee(), profile2.getKee());
  119. assertThatCustomProfileDoesNotExist(profile1);
  120. assertThatCustomProfileDoesNotExist(profile2);
  121. assertThatCustomProfileExists(profile3);
  122. }
  123. @Test
  124. public void delete_removes_custom_profile_marked_as_default() {
  125. OrganizationDto org = db.organizations().insert();
  126. QProfileDto profile = createCustomProfile(org);
  127. db.qualityProfiles().setAsDefault(profile);
  128. underTest.delete(dbSession, asList(profile));
  129. assertThatCustomProfileDoesNotExist(profile);
  130. }
  131. @Test
  132. public void delete_removes_custom_profile_from_project_associations() {
  133. OrganizationDto org = db.organizations().insert();
  134. QProfileDto profile = createCustomProfile(org);
  135. ComponentDto project = db.components().insertPrivateProject(org);
  136. db.qualityProfiles().associateWithProject(project, profile);
  137. underTest.delete(dbSession, asList(profile));
  138. assertThatCustomProfileDoesNotExist(profile);
  139. }
  140. @Test
  141. public void delete_builtin_profile() {
  142. RulesProfileDto builtInProfile = createBuiltInProfile();
  143. OrganizationDto org = db.organizations().insert();
  144. QProfileDto profile = associateBuiltInProfileToOrganization(builtInProfile, org);
  145. underTest.delete(dbSession, asList(profile));
  146. verifyNoCallsActiveRuleIndexerDelete();
  147. // remove only from org_qprofiles
  148. assertThat(db.getDbClient().qualityProfileDao().selectOrderedByOrganizationUuid(dbSession, org)).isEmpty();
  149. assertThatRulesProfileExists(builtInProfile);
  150. }
  151. @Test
  152. public void delete_builtin_profile_associated_to_project() {
  153. RulesProfileDto builtInProfile = createBuiltInProfile();
  154. OrganizationDto org = db.organizations().insert();
  155. ComponentDto project = db.components().insertPrivateProject(org);
  156. QProfileDto profile = associateBuiltInProfileToOrganization(builtInProfile, org);
  157. db.qualityProfiles().associateWithProject(project, profile);
  158. assertThat(db.getDbClient().qualityProfileDao().selectAssociatedToProjectAndLanguage(dbSession, project, profile.getLanguage())).isNotNull();
  159. underTest.delete(dbSession, asList(profile));
  160. verifyNoCallsActiveRuleIndexerDelete();
  161. // remove only from org_qprofiles and project_qprofiles
  162. assertThat(db.getDbClient().qualityProfileDao().selectOrderedByOrganizationUuid(dbSession, org)).isEmpty();
  163. assertThat(db.getDbClient().qualityProfileDao().selectAssociatedToProjectAndLanguage(dbSession, project, profile.getLanguage())).isNull();
  164. assertThatRulesProfileExists(builtInProfile);
  165. }
  166. @Test
  167. public void delete_builtin_profile_marked_as_default_on_organization() {
  168. RulesProfileDto builtInProfile = createBuiltInProfile();
  169. OrganizationDto org = db.organizations().insert();
  170. QProfileDto profile = associateBuiltInProfileToOrganization(builtInProfile, org);
  171. db.qualityProfiles().setAsDefault(profile);
  172. underTest.delete(dbSession, asList(profile));
  173. verifyNoCallsActiveRuleIndexerDelete();
  174. // remove only from org_qprofiles and default_qprofiles
  175. assertThat(db.getDbClient().qualityProfileDao().selectOrderedByOrganizationUuid(dbSession, org)).isEmpty();
  176. assertThat(db.getDbClient().qualityProfileDao().selectDefaultProfile(dbSession, org, profile.getLanguage())).isNull();
  177. assertThatRulesProfileExists(builtInProfile);
  178. }
  179. @Test
  180. public void delete_accepts_empty_list_of_keys() {
  181. OrganizationDto org = db.organizations().insert();
  182. QProfileDto profile = createCustomProfile(org);
  183. underTest.delete(dbSession, Collections.emptyList());
  184. verifyZeroInteractions(activeRuleIndexer);
  185. assertQualityProfileFromDb(profile).isNotNull();
  186. }
  187. @Test
  188. public void delete_removes_qprofile_edit_permissions() {
  189. OrganizationDto organization = db.organizations().insert();
  190. QProfileDto profile = db.qualityProfiles().insert(organization);
  191. UserDto user = db.users().insertUser();
  192. db.qualityProfiles().addUserPermission(profile, user);
  193. GroupDto group = db.users().insertGroup(organization);
  194. db.qualityProfiles().addGroupPermission(profile, group);
  195. underTest.delete(dbSession, asList(profile));
  196. assertThat(db.countRowsOfTable(dbSession, "qprofile_edit_users")).isZero();
  197. assertThat(db.countRowsOfTable(dbSession, "qprofile_edit_groups")).isZero();
  198. }
  199. private QProfileDto createCustomProfile(OrganizationDto org) {
  200. QProfileDto profile = db.qualityProfiles().insert(org, p -> p.setLanguage("xoo").setIsBuiltIn(false));
  201. ActiveRuleDto activeRuleDto = db.qualityProfiles().activateRule(profile, rule);
  202. ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto()
  203. .setRulesParameterId(ruleParam.getId())
  204. .setKey("foo")
  205. .setValue("bar");
  206. db.getDbClient().activeRuleDao().insertParam(dbSession, activeRuleDto, activeRuleParam);
  207. db.getDbClient().qProfileChangeDao().insert(dbSession, new QProfileChangeDto()
  208. .setChangeType(ActiveRuleChange.Type.ACTIVATED.name())
  209. .setRulesProfileUuid(profile.getRulesProfileUuid()));
  210. db.commit();
  211. return profile;
  212. }
  213. private RulesProfileDto createBuiltInProfile() {
  214. RulesProfileDto rulesProfileDto = new RulesProfileDto()
  215. .setIsBuiltIn(true)
  216. .setKee(Uuids.createFast())
  217. .setLanguage("xoo")
  218. .setName("Sonar way");
  219. db.getDbClient().qualityProfileDao().insert(dbSession, rulesProfileDto);
  220. ActiveRuleDto activeRuleDto = new ActiveRuleDto()
  221. .setProfileId(rulesProfileDto.getId())
  222. .setRuleId(rule.getId())
  223. .setSeverity(Severity.BLOCKER);
  224. db.getDbClient().activeRuleDao().insert(dbSession, activeRuleDto);
  225. ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto()
  226. .setRulesParameterId(ruleParam.getId())
  227. .setKey("foo")
  228. .setValue("bar");
  229. db.getDbClient().activeRuleDao().insertParam(dbSession, activeRuleDto, activeRuleParam);
  230. db.getDbClient().qProfileChangeDao().insert(dbSession, new QProfileChangeDto()
  231. .setChangeType(ActiveRuleChange.Type.ACTIVATED.name())
  232. .setRulesProfileUuid(rulesProfileDto.getKee()));
  233. db.commit();
  234. return rulesProfileDto;
  235. }
  236. private QProfileDto associateBuiltInProfileToOrganization(RulesProfileDto rulesProfile, OrganizationDto organization) {
  237. OrgQProfileDto orgQProfileDto = new OrgQProfileDto()
  238. .setUuid(Uuids.createFast())
  239. .setRulesProfileUuid(rulesProfile.getKee())
  240. .setOrganizationUuid(organization.getUuid());
  241. db.getDbClient().qualityProfileDao().insert(dbSession, orgQProfileDto);
  242. db.commit();
  243. return QProfileDto.from(orgQProfileDto, rulesProfile);
  244. }
  245. private AbstractObjectAssert<?, QProfileDto> assertQualityProfileFromDb(QProfileDto profile) {
  246. return assertThat(db.getDbClient().qualityProfileDao().selectByUuid(dbSession, profile.getKee()));
  247. }
  248. private void verifyNoCallsActiveRuleIndexerDelete() {
  249. verify(activeRuleIndexer, never()).commitDeletionOfProfiles(any(DbSession.class), anyCollection());
  250. }
  251. private void verifyCallActiveRuleIndexerDelete(String... expectedRuleProfileUuids) {
  252. Class<Set<QProfileDto>> setClass = (Class<Set<QProfileDto>>) (Class) Set.class;
  253. ArgumentCaptor<Set<QProfileDto>> setCaptor = ArgumentCaptor.forClass(setClass);
  254. verify(activeRuleIndexer).commitDeletionOfProfiles(any(DbSession.class), setCaptor.capture());
  255. assertThat(setCaptor.getValue())
  256. .extracting(QProfileDto::getKee)
  257. .containsExactlyInAnyOrder(expectedRuleProfileUuids);
  258. }
  259. private void assertThatRulesProfileExists(RulesProfileDto rulesProfile) {
  260. assertThat(db.getDbClient().qualityProfileDao().selectBuiltInRuleProfiles(dbSession))
  261. .extracting(RulesProfileDto::getKee)
  262. .containsExactly(rulesProfile.getKee());
  263. assertThat(db.countRowsOfTable(dbSession, "active_rules")).isGreaterThan(0);
  264. assertThat(db.countRowsOfTable(dbSession, "active_rule_parameters")).isGreaterThan(0);
  265. assertThat(db.countRowsOfTable(dbSession, "qprofile_changes")).isGreaterThan(0);
  266. }
  267. private void assertThatCustomProfileDoesNotExist(QProfileDto profile) {
  268. assertThat(db.countSql(dbSession, "select count(*) from org_qprofiles where uuid = '" + profile.getKee() + "'")).isEqualTo(0);
  269. assertThat(db.countSql(dbSession, "select count(*) from project_qprofiles where profile_key = '" + profile.getKee() + "'")).isEqualTo(0);
  270. assertThat(db.countSql(dbSession, "select count(*) from default_qprofiles where qprofile_uuid = '" + profile.getKee() + "'")).isEqualTo(0);
  271. assertThat(db.countSql(dbSession, "select count(*) from rules_profiles where kee = '" + profile.getRulesProfileUuid() + "'")).isEqualTo(0);
  272. assertThat(db.countSql(dbSession, "select count(*) from active_rules where profile_id = " + profile.getId())).isEqualTo(0);
  273. assertThat(db.countSql(dbSession, "select count(*) from qprofile_changes where rules_profile_uuid = '" + profile.getRulesProfileUuid() + "'")).isEqualTo(0);
  274. // TODO active_rule_parameters
  275. }
  276. private void assertThatCustomProfileExists(QProfileDto profile) {
  277. assertThat(db.countSql(dbSession, "select count(*) from org_qprofiles where uuid = '" + profile.getKee() + "'")).isGreaterThan(0);
  278. //assertThat(db.countSql(dbSession, "select count(*) from project_qprofiles where profile_key = '" + profile.getKee() + "'")).isGreaterThan(0);
  279. //assertThat(db.countSql(dbSession, "select count(*) from default_qprofiles where qprofile_uuid = '" + profile.getKee() + "'")).isGreaterThan(0);
  280. assertThat(db.countSql(dbSession, "select count(*) from rules_profiles where kee = '" + profile.getRulesProfileUuid() + "'")).isEqualTo(1);
  281. assertThat(db.countSql(dbSession, "select count(*) from active_rules where profile_id = " + profile.getId())).isGreaterThan(0);
  282. assertThat(db.countSql(dbSession, "select count(*) from qprofile_changes where rules_profile_uuid = '" + profile.getRulesProfileUuid() + "'")).isGreaterThan(0);
  283. // TODO active_rule_parameters
  284. }
  285. private static void assertEqual(QProfileDto p1, QProfileDto p2) {
  286. assertThat(p2.getOrganizationUuid()).isEqualTo(p1.getOrganizationUuid());
  287. assertThat(p2.getName()).isEqualTo(p1.getName());
  288. assertThat(p2.getKee()).startsWith(p1.getKee());
  289. assertThat(p2.getLanguage()).isEqualTo(p1.getLanguage());
  290. assertThat(p2.getId()).isEqualTo(p1.getId());
  291. assertThat(p2.getParentKee()).isEqualTo(p1.getParentKee());
  292. }
  293. private void expectBadRequestException(String message) {
  294. expectedException.expect(BadRequestException.class);
  295. expectedException.expectMessage(message);
  296. }
  297. }