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.

DeleteEventActionTest.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 java.util.List;
  22. import javax.annotation.Nullable;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.ExpectedException;
  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.db.DbClient;
  30. import org.sonar.db.DbSession;
  31. import org.sonar.db.DbTester;
  32. import org.sonar.db.component.ComponentDto;
  33. import org.sonar.db.component.ComponentTesting;
  34. import org.sonar.db.component.SnapshotDto;
  35. import org.sonar.db.event.EventDto;
  36. import org.sonar.server.exceptions.ForbiddenException;
  37. import org.sonar.server.exceptions.NotFoundException;
  38. import org.sonar.server.tester.UserSessionRule;
  39. import org.sonar.server.ws.TestRequest;
  40. import org.sonar.server.ws.WsActionTester;
  41. import static org.assertj.core.api.Assertions.assertThat;
  42. import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
  43. import static org.sonar.db.component.SnapshotTesting.newAnalysis;
  44. import static org.sonar.db.event.EventTesting.newEvent;
  45. import static org.sonar.server.projectanalysis.ws.EventCategory.VERSION;
  46. import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_EVENT;
  47. public class DeleteEventActionTest {
  48. @Rule
  49. public ExpectedException expectedException = ExpectedException.none();
  50. @Rule
  51. public UserSessionRule userSession = UserSessionRule.standalone();
  52. @Rule
  53. public DbTester db = DbTester.create(System2.INSTANCE);
  54. private DbClient dbClient = db.getDbClient();
  55. private DbSession dbSession = db.getSession();
  56. private WsActionTester ws = new WsActionTester(new DeleteEventAction(db.getDbClient(), userSession));
  57. @Test
  58. public void delete_event() {
  59. ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
  60. SnapshotDto analysis = db.components().insertProjectAndSnapshot(project);
  61. db.events().insertEvent(newEvent(analysis).setUuid("E1"));
  62. db.events().insertEvent(newEvent(analysis).setUuid("E2"));
  63. logInAsProjectAdministrator(project);
  64. call("E2");
  65. List<EventDto> events = db.getDbClient().eventDao().selectByAnalysisUuid(db.getSession(), analysis.getUuid());
  66. assertThat(events).extracting(EventDto::getUuid).containsExactly("E1");
  67. }
  68. @Test
  69. public void delete_version_event() {
  70. ComponentDto project = db.components().insertPrivateProject();
  71. SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(project).setCodePeriodVersion("5.6.3").setLast(false));
  72. db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(VERSION.getLabel()));
  73. logInAsProjectAdministrator(project);
  74. call("E1");
  75. SnapshotDto newAnalysis = dbClient.snapshotDao().selectByUuid(dbSession, analysis.getUuid()).get();
  76. assertThat(newAnalysis.getCodePeriodVersion()).isNull();
  77. }
  78. @Test
  79. public void fail_if_version_for_last_analysis() {
  80. ComponentDto project = db.components().insertPrivateProject();
  81. SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(project).setCodePeriodVersion("5.6.3").setLast(true));
  82. db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(VERSION.getLabel()));
  83. logInAsProjectAdministrator(project);
  84. expectedException.expect(IllegalArgumentException.class);
  85. expectedException.expectMessage("Cannot delete the version event of last analysis");
  86. call("E1");
  87. }
  88. @Test
  89. public void fail_if_category_different_than_other_and_version() {
  90. ComponentDto project = newPrivateProjectDto(db.organizations().insert(), "P1");
  91. SnapshotDto analysis = db.components().insertProjectAndSnapshot(project);
  92. db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory("Profile"));
  93. logInAsProjectAdministrator(project);
  94. expectedException.expect(IllegalArgumentException.class);
  95. expectedException.expectMessage("Event of category 'QUALITY_PROFILE' cannot be modified. Authorized categories: VERSION, OTHER");
  96. call("E1");
  97. }
  98. @Test
  99. public void fail_if_event_does_not_exist() {
  100. expectedException.expect(NotFoundException.class);
  101. expectedException.expectMessage("E42' not found");
  102. call("E42");
  103. }
  104. @Test
  105. public void fail_if_not_enough_permission() {
  106. SnapshotDto analysis = db.components().insertProjectAndSnapshot(ComponentTesting.newPrivateProjectDto(db.organizations().insert()));
  107. db.events().insertEvent(newEvent(analysis).setUuid("E1"));
  108. userSession.logIn();
  109. expectedException.expect(ForbiddenException.class);
  110. call("E1");
  111. }
  112. @Test
  113. public void fail_if_event_not_provided() {
  114. expectedException.expect(IllegalArgumentException.class);
  115. call(null);
  116. }
  117. @Test
  118. public void ws_definition() {
  119. WebService.Action definition = ws.getDef();
  120. assertThat(definition.key()).isEqualTo("delete_event");
  121. assertThat(definition.isPost()).isTrue();
  122. assertThat(definition.isInternal()).isFalse();
  123. assertThat(definition.param(PARAM_EVENT).isRequired()).isTrue();
  124. }
  125. private void call(@Nullable String event) {
  126. TestRequest request = ws.newRequest();
  127. if (event != null) {
  128. request.setParam(PARAM_EVENT, event);
  129. }
  130. request.execute();
  131. }
  132. private void logInAsProjectAdministrator(ComponentDto project) {
  133. userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
  134. }
  135. }