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.

SetActionIT.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import java.util.Optional;
  25. import javax.annotation.Nullable;
  26. import org.junit.Before;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.runner.RunWith;
  30. import org.sonar.api.server.ws.WebService;
  31. import org.sonar.api.utils.System2;
  32. import org.sonar.api.web.UserRole;
  33. import org.sonar.core.documentation.DocumentationLinkGenerator;
  34. import org.sonar.core.platform.EditionProvider;
  35. import org.sonar.core.platform.PlatformEditionProvider;
  36. import org.sonar.core.util.UuidFactoryFast;
  37. import org.sonar.db.DbClient;
  38. import org.sonar.db.DbSession;
  39. import org.sonar.db.DbTester;
  40. import org.sonar.db.component.BranchDto;
  41. import org.sonar.db.component.ComponentDto;
  42. import org.sonar.db.component.ProjectData;
  43. import org.sonar.db.component.SnapshotDto;
  44. import org.sonar.db.newcodeperiod.NewCodePeriodDao;
  45. import org.sonar.db.newcodeperiod.NewCodePeriodDbTester;
  46. import org.sonar.db.newcodeperiod.NewCodePeriodDto;
  47. import org.sonar.db.newcodeperiod.NewCodePeriodType;
  48. import org.sonar.db.project.ProjectDto;
  49. import org.sonar.server.component.ComponentFinder;
  50. import org.sonar.server.component.TestComponentFinder;
  51. import org.sonar.server.exceptions.ForbiddenException;
  52. import org.sonar.server.exceptions.NotFoundException;
  53. import org.sonar.server.tester.UserSessionRule;
  54. import org.sonar.server.ws.TestRequest;
  55. import org.sonar.server.ws.WsActionTester;
  56. import static org.assertj.core.api.Assertions.assertThat;
  57. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  58. import static org.assertj.core.api.Assertions.entry;
  59. import static org.mockito.ArgumentMatchers.any;
  60. import static org.mockito.Mockito.mock;
  61. import static org.mockito.Mockito.when;
  62. import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
  63. @RunWith(DataProviderRunner.class)
  64. public class SetActionIT {
  65. @Rule
  66. public UserSessionRule userSession = UserSessionRule.standalone();
  67. @Rule
  68. public DbTester db = DbTester.create(System2.INSTANCE);
  69. private DbClient dbClient = db.getDbClient();
  70. private DbSession dbSession = db.getSession();
  71. private ComponentFinder componentFinder = TestComponentFinder.from(db);
  72. private PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
  73. private NewCodePeriodDao dao = new NewCodePeriodDao(System2.INSTANCE, UuidFactoryFast.getInstance());
  74. private DocumentationLinkGenerator documentationLinkGenerator = mock(DocumentationLinkGenerator.class);
  75. private WsActionTester ws;
  76. @Before
  77. public void setup() {
  78. when(documentationLinkGenerator.getDocumentationLink(any())).thenReturn("https://docs.sonarsource.com/sonarqube/9.9/project-administration/defining-new-code/");
  79. ws = new WsActionTester(new SetAction(dbClient, userSession, componentFinder, editionProvider, dao, documentationLinkGenerator));
  80. }
  81. @Test
  82. public void test_definition() {
  83. WebService.Action definition = ws.getDef();
  84. assertThat(definition.description()).contains("https://docs.sonarsource.com/sonarqube/9.9/project-administration/defining-new-code/");
  85. assertThat(definition.key()).isEqualTo("set");
  86. assertThat(definition.isInternal()).isFalse();
  87. assertThat(definition.since()).isEqualTo("8.0");
  88. assertThat(definition.isPost()).isTrue();
  89. assertThat(definition.params()).extracting(WebService.Param::key).containsOnly("value", "type", "project", "branch");
  90. assertThat(definition.param("value").isRequired()).isFalse();
  91. assertThat(definition.param("type").isRequired()).isTrue();
  92. assertThat(definition.param("project").isRequired()).isFalse();
  93. assertThat(definition.param("branch").isRequired()).isFalse();
  94. }
  95. // validation of type
  96. @Test
  97. public void throw_IAE_if_no_type_specified() {
  98. assertThatThrownBy(() -> ws.newRequest().execute())
  99. .isInstanceOf(IllegalArgumentException.class)
  100. .hasMessageContaining("The 'type' parameter is missing");
  101. }
  102. @Test
  103. public void throw_IAE_if_type_is_invalid() {
  104. assertThatThrownBy(() -> ws.newRequest().setParam("type", "unknown").execute())
  105. .isInstanceOf(IllegalArgumentException.class)
  106. .hasMessageContaining("Invalid type: unknown");
  107. }
  108. @Test
  109. public void throw_IAE_if_type_is_invalid_for_global() {
  110. assertThatThrownBy(() -> ws.newRequest().setParam("type", "specific_analysis").execute())
  111. .isInstanceOf(IllegalArgumentException.class)
  112. .hasMessageContaining("Invalid type 'SPECIFIC_ANALYSIS'. Overall setting can only be set with types: [PREVIOUS_VERSION, NUMBER_OF_DAYS]");
  113. }
  114. @Test
  115. public void throw_IAE_if_type_is_invalid_for_project() {
  116. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  117. logInAsProjectAdministrator(project);
  118. assertThatThrownBy(() -> ws.newRequest()
  119. .setParam("project", project.getKey())
  120. .setParam("type", "specific_analysis")
  121. .execute())
  122. .isInstanceOf(IllegalArgumentException.class)
  123. .hasMessageContaining("Invalid type 'SPECIFIC_ANALYSIS'. Projects can only be set with types: [PREVIOUS_VERSION, NUMBER_OF_DAYS, REFERENCE_BRANCH]");
  124. }
  125. @Test
  126. public void throw_IAE_if_no_value_for_days() {
  127. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  128. logInAsProjectAdministrator(project);
  129. assertThatThrownBy(() -> ws.newRequest()
  130. .setParam("project", project.getKey())
  131. .setParam("branch", DEFAULT_MAIN_BRANCH_NAME)
  132. .setParam("type", "number_of_days")
  133. .execute())
  134. .isInstanceOf(IllegalArgumentException.class)
  135. .hasMessageContaining("New code definition type 'NUMBER_OF_DAYS' requires a value");
  136. }
  137. @Test
  138. public void throw_IAE_if_no_value_for_analysis() {
  139. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  140. logInAsProjectAdministrator(project);
  141. assertThatThrownBy(() -> ws.newRequest()
  142. .setParam("project", project.getKey())
  143. .setParam("type", "specific_analysis")
  144. .setParam("branch", DEFAULT_MAIN_BRANCH_NAME)
  145. .execute())
  146. .isInstanceOf(IllegalArgumentException.class)
  147. .hasMessageContaining("New code definition type 'SPECIFIC_ANALYSIS' requires a value");
  148. }
  149. @Test
  150. public void throw_IAE_if_days_is_invalid() {
  151. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  152. logInAsProjectAdministrator(project);
  153. assertThatThrownBy(() -> ws.newRequest()
  154. .setParam("project", project.getKey())
  155. .setParam("type", "number_of_days")
  156. .setParam("branch", DEFAULT_MAIN_BRANCH_NAME)
  157. .setParam("value", "unknown")
  158. .execute())
  159. .isInstanceOf(IllegalArgumentException.class)
  160. .hasMessageContaining("Failed to parse number of days: unknown");
  161. }
  162. @Test
  163. public void throw_IAE_if_setting_is_not_cayc_compliant() {
  164. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  165. logInAsProjectAdministrator(project);
  166. TestRequest request = ws.newRequest()
  167. .setParam("project", project.getKey())
  168. .setParam("type", "number_of_days")
  169. .setParam("branch", DEFAULT_MAIN_BRANCH_NAME)
  170. .setParam("value", "92");
  171. assertThatThrownBy(() -> request
  172. .execute())
  173. .isInstanceOf(IllegalArgumentException.class)
  174. .hasMessageContaining("Failed to set the New Code Definition. The given value is not compatible with the Clean as You Code methodology. "
  175. + "Please refer to the documentation for compliant options.");
  176. }
  177. @Test
  178. public void no_error_if_setting_is_cayc_compliant() {
  179. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  180. logInAsProjectAdministrator(project);
  181. ws.newRequest()
  182. .setParam("project", project.getKey())
  183. .setParam("type", "number_of_days")
  184. .setParam("value", "90")
  185. .execute();
  186. assertTableContainsOnly(project.getUuid(), null, NewCodePeriodType.NUMBER_OF_DAYS, "90");
  187. }
  188. @Test
  189. public void throw_IAE_if_analysis_is_not_found() {
  190. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  191. logInAsProjectAdministrator(project);
  192. assertThatThrownBy(() -> ws.newRequest()
  193. .setParam("project", project.getKey())
  194. .setParam("type", "specific_analysis")
  195. .setParam("branch", DEFAULT_MAIN_BRANCH_NAME)
  196. .setParam("value", "unknown")
  197. .execute())
  198. .isInstanceOf(NotFoundException.class)
  199. .hasMessageContaining("Analysis 'unknown' is not found");
  200. }
  201. @Test
  202. public void throw_IAE_if_analysis_doesnt_belong_to_branch() {
  203. ProjectData projectData = db.components().insertPublicProject();
  204. ProjectDto project = projectData.getProjectDto();
  205. ComponentDto branch = db.components().insertProjectBranch(projectData.getMainBranchComponent(), b -> b.setKey("branch"));
  206. SnapshotDto analysisMaster = db.components().insertSnapshot(project);
  207. SnapshotDto analysisBranch = db.components().insertSnapshot(branch);
  208. logInAsProjectAdministrator(project);
  209. assertThatThrownBy(() -> ws.newRequest()
  210. .setParam("project", project.getKey())
  211. .setParam("type", "specific_analysis")
  212. .setParam("branch", DEFAULT_MAIN_BRANCH_NAME)
  213. .setParam("value", analysisBranch.getUuid())
  214. .execute())
  215. .isInstanceOf(IllegalArgumentException.class)
  216. .hasMessageContaining("Analysis '" + analysisBranch.getUuid() + "' does not belong to branch '" + DEFAULT_MAIN_BRANCH_NAME +
  217. "' of project '" + project.getKey() + "'");
  218. }
  219. // validation of project/branch
  220. @Test
  221. public void throw_IAE_if_branch_is_specified_without_project() {
  222. assertThatThrownBy(() -> ws.newRequest()
  223. .setParam("branch", "branch")
  224. .execute())
  225. .isInstanceOf(IllegalArgumentException.class)
  226. .hasMessageContaining("If branch key is specified, project key needs to be specified too");
  227. }
  228. @Test
  229. public void throw_NFE_if_project_not_found() {
  230. assertThatThrownBy(() -> ws.newRequest()
  231. .setParam("type", "previous_version")
  232. .setParam("project", "unknown")
  233. .execute())
  234. .isInstanceOf(NotFoundException.class)
  235. .hasMessageContaining("Project 'unknown' not found");
  236. }
  237. @Test
  238. public void throw_NFE_if_branch_not_found() {
  239. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  240. logInAsProjectAdministrator(project);
  241. assertThatThrownBy(() -> ws.newRequest()
  242. .setParam("project", project.getKey())
  243. .setParam("type", "previous_version")
  244. .setParam("branch", "unknown")
  245. .execute())
  246. .isInstanceOf(NotFoundException.class)
  247. .hasMessageContaining("Branch 'unknown' in project '" + project.getKey() + "' not found");
  248. }
  249. // permission
  250. @Test
  251. public void throw_NFE_if_no_project_permission() {
  252. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  253. assertThatThrownBy(() -> ws.newRequest()
  254. .setParam("project", project.getKey())
  255. .setParam("type", "previous_version")
  256. .execute())
  257. .isInstanceOf(ForbiddenException.class)
  258. .hasMessageContaining("Insufficient privileges");
  259. }
  260. @Test
  261. public void throw_NFE_if_no_system_permission() {
  262. assertThatThrownBy(() -> ws.newRequest()
  263. .setParam("type", "previous_version")
  264. .execute())
  265. .isInstanceOf(ForbiddenException.class)
  266. .hasMessageContaining("Insufficient privileges");
  267. }
  268. // success cases
  269. @Test
  270. public void set_global_period_to_previous_version() {
  271. logInAsSystemAdministrator();
  272. ws.newRequest()
  273. .setParam("type", "previous_version")
  274. .execute();
  275. assertTableContainsOnly(null, null, NewCodePeriodType.PREVIOUS_VERSION, null);
  276. }
  277. @Test
  278. public void set_project_period_to_number_of_days() {
  279. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  280. logInAsProjectAdministrator(project);
  281. ws.newRequest()
  282. .setParam("project", project.getKey())
  283. .setParam("type", "number_of_days")
  284. .setParam("value", "5")
  285. .execute();
  286. assertTableContainsOnly(project.getUuid(), null, NewCodePeriodType.NUMBER_OF_DAYS, "5");
  287. }
  288. @Test
  289. public void update_project_new_code_period() {
  290. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  291. logInAsProjectAdministrator(project);
  292. var currentTime = System.currentTimeMillis();
  293. db.newCodePeriods().insert(new NewCodePeriodDto()
  294. .setProjectUuid(project.getUuid())
  295. .setType(NewCodePeriodType.NUMBER_OF_DAYS)
  296. .setPreviousNonCompliantValue("100")
  297. .setUpdatedAt(currentTime)
  298. .setValue("90"));
  299. ws.newRequest()
  300. .setParam("project", project.getKey())
  301. .setParam("type", "number_of_days")
  302. .setParam("value", "30")
  303. .execute();
  304. var ncd = db.getDbClient().newCodePeriodDao().selectByProject(dbSession, project.getUuid());
  305. assertThat(ncd).isPresent();
  306. assertThat(ncd.get()).extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getPreviousNonCompliantValue)
  307. .containsExactly(NewCodePeriodType.NUMBER_OF_DAYS, "30", null);
  308. }
  309. @Test
  310. @UseDataProvider("provideNewCodePeriodTypeAndValue")
  311. public void never_set_project_value_in_community_edition(NewCodePeriodType type, @Nullable String value) {
  312. when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY));
  313. ProjectData projectData = db.components().insertPublicProject();
  314. ProjectDto project = projectData.getProjectDto();
  315. if (value != null && NewCodePeriodType.SPECIFIC_ANALYSIS.equals(type)) {
  316. db.components().insertSnapshot(project, snapshotDto -> snapshotDto.setUuid(value));
  317. }
  318. logInAsProjectAdministrator(project);
  319. TestRequest request = ws.newRequest()
  320. .setParam("project", project.getKey())
  321. .setParam("type", type.name());
  322. if (value != null) {
  323. request.setParam("value", value);
  324. }
  325. request.execute();
  326. assertTableContainsOnly(project.getUuid(), projectData.getMainBranchComponent().uuid(), type, value);
  327. }
  328. @DataProvider
  329. public static Object[][] provideNewCodePeriodTypeAndValue() {
  330. return new Object[][] {
  331. {NewCodePeriodType.NUMBER_OF_DAYS, "5"},
  332. {NewCodePeriodType.SPECIFIC_ANALYSIS, "analysis-uuid"},
  333. {NewCodePeriodType.PREVIOUS_VERSION, null},
  334. {NewCodePeriodType.REFERENCE_BRANCH, "master"}
  335. };
  336. }
  337. @Test
  338. public void set_project_twice_period_to_number_of_days() {
  339. ProjectDto project = db.components().insertPublicProject().getProjectDto();
  340. logInAsProjectAdministrator(project);
  341. ws.newRequest()
  342. .setParam("project", project.getKey())
  343. .setParam("type", "previous_version")
  344. .execute();
  345. assertTableContainsOnly(project.getUuid(), null, NewCodePeriodType.PREVIOUS_VERSION, null);
  346. ws.newRequest()
  347. .setParam("project", project.getKey())
  348. .setParam("type", "number_of_days")
  349. .setParam("value", "5")
  350. .execute();
  351. assertTableContainsOnly(project.getUuid(), null, NewCodePeriodType.NUMBER_OF_DAYS, "5");
  352. }
  353. @Test
  354. public void set_branch_period_to_analysis() {
  355. ProjectData projectData = db.components().insertPublicProject();
  356. ProjectDto project = projectData.getProjectDto();
  357. ComponentDto branch = db.components().insertProjectBranch(projectData.getMainBranchComponent(), b -> b.setKey("branch"));
  358. SnapshotDto analysisMaster = db.components().insertSnapshot(project);
  359. SnapshotDto analysisBranch = db.components().insertSnapshot(branch);
  360. logInAsProjectAdministrator(project);
  361. ws.newRequest()
  362. .setParam("project", project.getKey())
  363. .setParam("type", "specific_analysis")
  364. .setParam("branch", "branch")
  365. .setParam("value", analysisBranch.getUuid())
  366. .execute();
  367. assertTableContainsOnly(project.getUuid(), branch.uuid(), NewCodePeriodType.SPECIFIC_ANALYSIS, analysisBranch.getUuid());
  368. }
  369. @Test
  370. public void set_branch_period_twice_to_analysis() {
  371. ProjectData projectData = db.components().insertPublicProject();
  372. ProjectDto project = projectData.getProjectDto();
  373. BranchDto branch = db.components().insertProjectBranch(projectData.getProjectDto(), b -> b.setKey("branch"));
  374. SnapshotDto analysisMaster = db.components().insertSnapshot(project);
  375. SnapshotDto analysisBranch = db.components().insertSnapshot(branch);
  376. logInAsProjectAdministrator(project);
  377. ws.newRequest()
  378. .setParam("project", project.getKey())
  379. .setParam("type", "specific_analysis")
  380. .setParam("branch", "branch")
  381. .setParam("value", analysisBranch.getUuid())
  382. .execute();
  383. ws.newRequest()
  384. .setParam("project", project.getKey())
  385. .setParam("type", "previous_version")
  386. .setParam("branch", "branch")
  387. .execute();
  388. assertTableContainsOnly(project.getUuid(), branch.getUuid(), NewCodePeriodType.PREVIOUS_VERSION, null);
  389. }
  390. private void assertTableContainsOnly(@Nullable String projectUuid, @Nullable String branchUuid, NewCodePeriodType type, @Nullable String value) {
  391. assertThat(db.countRowsOfTable(dbSession, "new_code_periods")).isOne();
  392. assertThat(db.selectFirst(dbSession, "select project_uuid, branch_uuid, type, value from new_code_periods"))
  393. .containsOnly(entry("PROJECT_UUID", projectUuid), entry("BRANCH_UUID", branchUuid), entry("TYPE", type.name()), entry("VALUE", value));
  394. }
  395. private void logInAsProjectAdministrator(ProjectDto project) {
  396. userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
  397. }
  398. private void logInAsSystemAdministrator() {
  399. userSession.logIn().setSystemAdministrator();
  400. }
  401. }