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.

UnsetActionIT.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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.server.newcodeperiod.ws;
  21. import java.util.Optional;
  22. import javax.annotation.Nullable;
  23. import org.junit.Before;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.sonar.api.server.ws.WebService;
  27. import org.sonar.api.utils.System2;
  28. import org.sonar.api.web.UserRole;
  29. import org.sonar.core.documentation.DocumentationLinkGenerator;
  30. import org.sonar.core.platform.EditionProvider;
  31. import org.sonar.core.platform.PlatformEditionProvider;
  32. import org.sonar.core.util.UuidFactoryFast;
  33. import org.sonar.db.DbClient;
  34. import org.sonar.db.DbSession;
  35. import org.sonar.db.DbTester;
  36. import org.sonar.db.component.BranchDto;
  37. import org.sonar.db.component.ProjectData;
  38. import org.sonar.db.newcodeperiod.NewCodePeriodDao;
  39. import org.sonar.db.newcodeperiod.NewCodePeriodType;
  40. import org.sonar.db.project.ProjectDto;
  41. import org.sonar.server.component.ComponentFinder;
  42. import org.sonar.server.component.TestComponentFinder;
  43. import org.sonar.server.exceptions.ForbiddenException;
  44. import org.sonar.server.exceptions.NotFoundException;
  45. import org.sonar.server.tester.UserSessionRule;
  46. import org.sonar.server.ws.TestRequest;
  47. import org.sonar.server.ws.WsActionTester;
  48. import static org.assertj.core.api.Assertions.assertThat;
  49. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  50. import static org.assertj.core.api.Assertions.entry;
  51. import static org.mockito.ArgumentMatchers.any;
  52. import static org.mockito.Mockito.mock;
  53. import static org.mockito.Mockito.when;
  54. public class UnsetActionIT {
  55. @Rule
  56. public UserSessionRule userSession = UserSessionRule.standalone();
  57. @Rule
  58. public DbTester db = DbTester.create(System2.INSTANCE);
  59. private DbClient dbClient = db.getDbClient();
  60. private DbSession dbSession = db.getSession();
  61. private ComponentFinder componentFinder = TestComponentFinder.from(db);
  62. private NewCodePeriodDao dao = new NewCodePeriodDao(System2.INSTANCE, UuidFactoryFast.getInstance());
  63. private PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
  64. private DocumentationLinkGenerator documentationLinkGenerator = mock(DocumentationLinkGenerator.class);
  65. private WsActionTester ws;
  66. @Before
  67. public void setup(){
  68. when(documentationLinkGenerator.getDocumentationLink(any())).thenReturn("https://docs.sonarsource.com/sonarqube/9.9/project-administration/defining-new-code/");
  69. ws = new WsActionTester(new UnsetAction(dbClient, userSession, componentFinder, editionProvider, dao, documentationLinkGenerator));
  70. }
  71. @Test
  72. public void test_definition() {
  73. WebService.Action definition = ws.getDef();
  74. assertThat(definition.description()).contains("https://docs.sonarsource.com/sonarqube/9.9/project-administration/defining-new-code/");
  75. assertThat(definition.key()).isEqualTo("unset");
  76. assertThat(definition.isInternal()).isFalse();
  77. assertThat(definition.since()).isEqualTo("8.0");
  78. assertThat(definition.isPost()).isTrue();
  79. assertThat(definition.params()).extracting(WebService.Param::key).containsOnly("project", "branch");
  80. assertThat(definition.param("project").isRequired()).isFalse();
  81. assertThat(definition.param("branch").isRequired()).isFalse();
  82. }
  83. // validation of project/branch
  84. @Test
  85. public void throw_IAE_if_branch_is_specified_without_project() {
  86. TestRequest request = ws.newRequest()
  87. .setParam("branch", "branch");
  88. assertThatThrownBy(() -> request.execute())
  89. .isInstanceOf(IllegalArgumentException.class)
  90. .hasMessageContaining("If branch key is specified, project key needs to be specified too");
  91. }
  92. @Test
  93. public void throw_NFE_if_project_not_found() {
  94. assertThatThrownBy(() -> ws.newRequest()
  95. .setParam("type", "previous_version")
  96. .setParam("project", "unknown")
  97. .execute())
  98. .isInstanceOf(NotFoundException.class)
  99. .hasMessageContaining("Project 'unknown' not found");
  100. }
  101. @Test
  102. public void throw_NFE_if_branch_not_found() {
  103. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  104. logInAsProjectAdministrator(project);
  105. assertThatThrownBy(() -> ws.newRequest()
  106. .setParam("project", project.getKey())
  107. .setParam("type", "previous_version")
  108. .setParam("branch", "unknown")
  109. .execute())
  110. .isInstanceOf(NotFoundException.class)
  111. .hasMessageContaining("Branch 'unknown' in project '" + project.getKey() + "' not found");
  112. }
  113. // permission
  114. @Test
  115. public void throw_NFE_if_no_project_permission() {
  116. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  117. assertThatThrownBy(() -> ws.newRequest()
  118. .setParam("project", project.getKey())
  119. .setParam("type", "previous_version")
  120. .execute())
  121. .isInstanceOf(ForbiddenException.class)
  122. .hasMessageContaining("Insufficient privileges");
  123. }
  124. @Test
  125. public void throw_NFE_if_no_system_permission() {
  126. assertThatThrownBy(() -> ws.newRequest()
  127. .setParam("type", "previous_version")
  128. .execute())
  129. .isInstanceOf(ForbiddenException.class)
  130. .hasMessageContaining("Insufficient privileges");
  131. }
  132. // success cases
  133. @Test
  134. public void delete_global_period() {
  135. logInAsSystemAdministrator();
  136. ws.newRequest()
  137. .execute();
  138. assertTableEmpty();
  139. }
  140. @Test
  141. public void delete_project_period() {
  142. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  143. logInAsProjectAdministrator(project);
  144. ws.newRequest()
  145. .setParam("project", project.getKey())
  146. .execute();
  147. assertTableEmpty();
  148. }
  149. @Test
  150. public void delete_project_period_twice() {
  151. ProjectDto project1 = db.components().insertPublicProject().getProjectDto();
  152. ProjectDto project2 = db.components().insertPublicProject().getProjectDto();
  153. db.newCodePeriods().insert(project1.getUuid(), null, NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid1");
  154. db.newCodePeriods().insert(project2.getUuid(), null, NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid2");
  155. logInAsProjectAdministrator(project1);
  156. ws.newRequest()
  157. .setParam("project", project1.getKey())
  158. .execute();
  159. assertTableContainsOnly(project2.getUuid(), null, NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid2");
  160. ws.newRequest()
  161. .setParam("project", project1.getKey())
  162. .execute();
  163. assertTableContainsOnly(project2.getUuid(), null, NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid2");
  164. }
  165. @Test
  166. public void delete_branch_period() {
  167. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  168. BranchDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch"));
  169. db.newCodePeriods().insert(project.getUuid(), null, NewCodePeriodType.NUMBER_OF_DAYS, "20");
  170. db.newCodePeriods().insert(project.getUuid(), branch.getUuid(), NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid2");
  171. logInAsProjectAdministrator(project);
  172. ws.newRequest()
  173. .setParam("project", project.getKey())
  174. .setParam("branch", "branch")
  175. .execute();
  176. assertTableContainsOnly(project.getUuid(), null, NewCodePeriodType.NUMBER_OF_DAYS, "20");
  177. }
  178. @Test
  179. public void delete_branch_and_project_period_in_community_edition() {
  180. ProjectData projectData = db.components().insertPublicProject();
  181. ProjectDto project = projectData.getProjectDto();
  182. db.newCodePeriods().insert(project.getUuid(), null, NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid1");
  183. db.newCodePeriods().insert(project.getUuid(), projectData.getMainBranchComponent().uuid(), NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid2");
  184. when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY));
  185. logInAsProjectAdministrator(project);
  186. ws.newRequest()
  187. .setParam("project", project.getKey())
  188. .execute();
  189. assertTableEmpty();
  190. }
  191. @Test
  192. public void throw_IAE_if_unset_branch_NCD_and_project_NCD_not_compliant() {
  193. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  194. BranchDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch"));
  195. db.newCodePeriods().insert(project.getUuid(), null, NewCodePeriodType.NUMBER_OF_DAYS, "97");
  196. db.newCodePeriods().insert(project.getUuid(), branch.getUuid(), NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid");
  197. TestRequest request = ws.newRequest()
  198. .setParam("project", project.getKey())
  199. .setParam("branch", "branch");
  200. logInAsProjectAdministrator(project);
  201. assertThatThrownBy(() -> request.execute())
  202. .isInstanceOf(IllegalArgumentException.class)
  203. .hasMessageContaining("Failed to unset the New Code Definition. Your project " +
  204. "New Code Definition is not compatible with the Clean as You Code methodology. Please update your project New Code Definition");
  205. }
  206. @Test
  207. public void throw_IAE_if_unset_branch_NCD_and_no_project_NCD_and_instance_NCD_not_compliant() {
  208. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  209. BranchDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch"));
  210. db.newCodePeriods().insert(null, null, NewCodePeriodType.NUMBER_OF_DAYS, "97");
  211. db.newCodePeriods().insert(project.getUuid(), branch.getUuid(), NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid");
  212. TestRequest request = ws.newRequest()
  213. .setParam("project", project.getKey())
  214. .setParam("branch", "branch");
  215. logInAsProjectAdministrator(project);
  216. assertThatThrownBy(() -> request.execute())
  217. .isInstanceOf(IllegalArgumentException.class)
  218. .hasMessageContaining("Failed to unset the New Code Definition. Your instance " +
  219. "New Code Definition is not compatible with the Clean as You Code methodology. Please update your instance New Code Definition");
  220. }
  221. @Test
  222. public void throw_IAE_if_unset_project_NCD_and_instance_NCD_not_compliant() {
  223. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  224. db.newCodePeriods().insert(null, null, NewCodePeriodType.NUMBER_OF_DAYS, "97");
  225. db.newCodePeriods().insert(project.getUuid(), null, NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid");
  226. logInAsProjectAdministrator(project);
  227. TestRequest request = ws.newRequest()
  228. .setParam("project", project.getKey());
  229. assertThatThrownBy(() -> request.execute())
  230. .isInstanceOf(IllegalArgumentException.class)
  231. .hasMessageContaining("Failed to unset the New Code Definition. Your instance " +
  232. "New Code Definition is not compatible with the Clean as You Code methodology. Please update your instance New Code Definition");
  233. }
  234. @Test
  235. public void do_not_throw_IAE_if_unset_project_NCD_and_no_instance_NCD() {
  236. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  237. db.newCodePeriods().insert(project.getUuid(), null, NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid");
  238. logInAsProjectAdministrator(project);
  239. ws.newRequest()
  240. .setParam("project", project.getKey())
  241. .execute();
  242. assertTableEmpty();
  243. }
  244. @Test
  245. public void do_not_throw_IAE_if_unset_branch_NCD_and_project_NCD_compliant() {
  246. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  247. BranchDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch"));
  248. db.newCodePeriods().insert(project.getUuid(), branch.getUuid(), NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid");
  249. db.newCodePeriods().insert(project.getUuid(), null, NewCodePeriodType.PREVIOUS_VERSION, null);
  250. logInAsProjectAdministrator(project);
  251. ws.newRequest()
  252. .setParam("project", project.getKey())
  253. .setParam("branch", "branch")
  254. .execute();
  255. assertTableContainsOnly(project.getUuid(), null, NewCodePeriodType.PREVIOUS_VERSION, null);
  256. }
  257. @Test
  258. public void do_not_throw_IAE_if_unset_branch_NCD_and_project_NCD_not_compliant_and_no_branch_NCD() {
  259. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  260. db.components().insertProjectBranch(project, b -> b.setKey("branch"));
  261. db.newCodePeriods().insert(project.getUuid(), null, NewCodePeriodType.NUMBER_OF_DAYS, "93");
  262. logInAsProjectAdministrator(project);
  263. ws.newRequest()
  264. .setParam("project", project.getKey())
  265. .setParam("branch", "branch")
  266. .execute();
  267. assertTableContainsOnly(project.getUuid(), null, NewCodePeriodType.NUMBER_OF_DAYS, "93");
  268. }
  269. @Test
  270. public void do_not_throw_IAE_if_unset_branch_NCD_and_no_project_NCD_and_instance_NCD_compliant() {
  271. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  272. BranchDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch"));
  273. db.newCodePeriods().insert(project.getUuid(), branch.getUuid(), NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid");
  274. db.newCodePeriods().insert(null, null, NewCodePeriodType.PREVIOUS_VERSION, null);
  275. logInAsProjectAdministrator(project);
  276. ws.newRequest()
  277. .setParam("project", project.getKey())
  278. .setParam("branch", "branch")
  279. .execute();
  280. assertTableContainsOnly(null, null, NewCodePeriodType.PREVIOUS_VERSION, null);
  281. }
  282. @Test
  283. public void do_not_throw_IAE_if_unset_branch_NCD_and_no_project_NCD_and_no_instance() {
  284. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  285. BranchDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch"));
  286. db.newCodePeriods().insert(project.getUuid(), branch.getUuid(), NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid");
  287. logInAsProjectAdministrator(project);
  288. ws.newRequest()
  289. .setParam("project", project.getKey())
  290. .setParam("branch", "branch")
  291. .execute();
  292. assertTableEmpty();
  293. }
  294. @Test
  295. public void do_not_throw_IAE_if_unset_project_NCD_and_instance_NCD_compliant() {
  296. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  297. BranchDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch"));
  298. db.newCodePeriods().insert(null, null, NewCodePeriodType.PREVIOUS_VERSION, null);
  299. db.newCodePeriods().insert(project.getUuid(), null, NewCodePeriodType.PREVIOUS_VERSION, null);
  300. logInAsProjectAdministrator(project);
  301. ws.newRequest()
  302. .setParam("project", project.getKey())
  303. .execute();
  304. assertTableContainsOnly(null, null, NewCodePeriodType.PREVIOUS_VERSION, null);
  305. }
  306. @Test
  307. public void do_not_throw_IAE_if_unset_project_NCD_and_instance_NCD_not_compliant_and_no_project_NCD() {
  308. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  309. BranchDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch"));
  310. db.newCodePeriods().insert(null, null, NewCodePeriodType.NUMBER_OF_DAYS, "93");
  311. logInAsProjectAdministrator(project);
  312. ws.newRequest()
  313. .setParam("project", project.getKey())
  314. .execute();
  315. assertTableContainsOnly(null, null, NewCodePeriodType.NUMBER_OF_DAYS, "93");
  316. }
  317. private void assertTableEmpty() {
  318. assertThat(db.countRowsOfTable(dbSession, "new_code_periods")).isZero();
  319. }
  320. private void assertTableContainsOnly(@Nullable String projectUuid, @Nullable String branchUuid, NewCodePeriodType type, @Nullable String value) {
  321. assertThat(db.countRowsOfTable(dbSession, "new_code_periods")).isOne();
  322. assertThat(db.selectFirst(dbSession, "select project_uuid, branch_uuid, type, value from new_code_periods"))
  323. .containsOnly(entry("PROJECT_UUID", projectUuid), entry("BRANCH_UUID", branchUuid), entry("TYPE", type.name()), entry("VALUE", value));
  324. }
  325. private void logInAsProjectAdministrator(ProjectDto project) {
  326. userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
  327. }
  328. private void logInAsSystemAdministrator() {
  329. userSession.logIn().setSystemAdministrator();
  330. }
  331. }