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.

DeleteActionTest.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.projectanalysis.ws;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.junit.rules.ExpectedException;
  24. import org.sonar.api.server.ws.WebService;
  25. import org.sonar.api.web.UserRole;
  26. import org.sonar.db.DbClient;
  27. import org.sonar.db.DbSession;
  28. import org.sonar.db.DbTester;
  29. import org.sonar.db.component.ComponentDto;
  30. import org.sonar.db.component.SnapshotDto;
  31. import org.sonar.server.exceptions.ForbiddenException;
  32. import org.sonar.server.exceptions.NotFoundException;
  33. import org.sonar.server.tester.UserSessionRule;
  34. import org.sonar.server.ws.WsActionTester;
  35. import static com.google.common.collect.Lists.newArrayList;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. import static org.assertj.core.api.Assertions.tuple;
  38. import static org.sonar.db.component.SnapshotDto.STATUS_PROCESSED;
  39. import static org.sonar.db.component.SnapshotDto.STATUS_UNPROCESSED;
  40. import static org.sonar.db.component.SnapshotTesting.newAnalysis;
  41. import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_ANALYSIS;
  42. public class DeleteActionTest {
  43. @Rule
  44. public ExpectedException expectedException = ExpectedException.none();
  45. @Rule
  46. public UserSessionRule userSession = UserSessionRule.standalone();
  47. @Rule
  48. public DbTester db = DbTester.create();
  49. private DbClient dbClient = db.getDbClient();
  50. private DbSession dbSession = db.getSession();
  51. private WsActionTester ws = new WsActionTester(new DeleteAction(dbClient, userSession));
  52. @Test
  53. public void project_administrator_deletes_analysis() {
  54. ComponentDto project = db.components().insertPrivateProject();
  55. db.components().insertSnapshot(newAnalysis(project).setUuid("A1").setLast(false).setStatus(STATUS_PROCESSED));
  56. db.components().insertSnapshot(newAnalysis(project).setUuid("A2").setLast(true).setStatus(STATUS_PROCESSED));
  57. logInAsProjectAdministrator(project);
  58. call("A1");
  59. db.commit();
  60. assertThat(dbClient.snapshotDao().selectByUuids(dbSession, newArrayList("A1", "A2"))).extracting(SnapshotDto::getUuid, SnapshotDto::getStatus).containsExactly(
  61. tuple("A1", STATUS_UNPROCESSED),
  62. tuple("A2", STATUS_PROCESSED));
  63. }
  64. @Test
  65. public void definition() {
  66. WebService.Action definition = ws.getDef();
  67. assertThat(definition.key()).isEqualTo("delete");
  68. assertThat(definition.isPost()).isTrue();
  69. assertThat(definition.param("analysis").isRequired()).isTrue();
  70. }
  71. @Test
  72. public void last_analysis_cannot_be_deleted() {
  73. ComponentDto project = db.components().insertPrivateProject();
  74. db.components().insertSnapshot(newAnalysis(project).setUuid("A1").setLast(true));
  75. logInAsProjectAdministrator(project);
  76. expectedException.expect(IllegalArgumentException.class);
  77. expectedException.expectMessage("The last analysis 'A1' cannot be deleted");
  78. call("A1");
  79. }
  80. @Test
  81. public void fail_when_analysis_not_found() {
  82. userSession.logIn().setRoot();
  83. expectedException.expect(NotFoundException.class);
  84. expectedException.expectMessage("Analysis 'A42' not found");
  85. call("A42");
  86. }
  87. @Test
  88. public void fail_when_analysis_is_unprocessed() {
  89. ComponentDto project = db.components().insertPrivateProject();
  90. db.components().insertSnapshot(newAnalysis(project).setUuid("A1").setLast(false).setStatus(STATUS_UNPROCESSED));
  91. logInAsProjectAdministrator(project);
  92. expectedException.expect(NotFoundException.class);
  93. expectedException.expectMessage("Analysis 'A1' not found");
  94. call("A1");
  95. }
  96. @Test
  97. public void fail_when_not_enough_permission() {
  98. ComponentDto project = db.components().insertPrivateProject();
  99. db.components().insertSnapshot(newAnalysis(project).setUuid("A1").setLast(false));
  100. userSession.logIn();
  101. expectedException.expect(ForbiddenException.class);
  102. call("A1");
  103. }
  104. private void call(String analysis) {
  105. ws.newRequest()
  106. .setParam(PARAM_ANALYSIS, analysis)
  107. .execute();
  108. }
  109. private void logInAsProjectAdministrator(ComponentDto project) {
  110. userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
  111. }
  112. }