3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.projectanalysis.ws;
22 import java.util.List;
23 import javax.annotation.Nullable;
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.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;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.assertj.core.api.Assertions.assertThatThrownBy;
44 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
45 import static org.sonar.db.component.SnapshotTesting.newAnalysis;
46 import static org.sonar.db.event.EventTesting.newEvent;
47 import static org.sonar.server.projectanalysis.ws.EventCategory.VERSION;
48 import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_EVENT;
50 public class DeleteEventActionIT {
52 public UserSessionRule userSession = UserSessionRule.standalone();
54 public DbTester db = DbTester.create(System2.INSTANCE);
55 private DbClient dbClient = db.getDbClient();
56 private DbSession dbSession = db.getSession();
58 private WsActionTester ws = new WsActionTester(new DeleteEventAction(db.getDbClient(), userSession));
61 public void delete_event() {
62 ComponentDto project = ComponentTesting.newPrivateProjectDto();
63 SnapshotDto analysis = db.components().insertProjectAndSnapshot(project);
64 db.events().insertEvent(newEvent(analysis).setUuid("E1"));
65 db.events().insertEvent(newEvent(analysis).setUuid("E2"));
66 logInAsProjectAdministrator(project);
70 List<EventDto> events = db.getDbClient().eventDao().selectByAnalysisUuid(db.getSession(), analysis.getUuid());
71 assertThat(events).extracting(EventDto::getUuid).containsExactly("E1");
75 public void delete_version_event() {
76 ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
77 SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(project).setProjectVersion("5.6.3").setLast(false));
78 db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(VERSION.getLabel()));
79 logInAsProjectAdministrator(project);
83 SnapshotDto newAnalysis = dbClient.snapshotDao().selectByUuid(dbSession, analysis.getUuid()).get();
84 assertThat(newAnalysis.getProjectVersion()).isNull();
88 public void fail_if_version_for_last_analysis() {
89 ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
90 SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(project).setProjectVersion("5.6.3").setLast(true));
91 db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(VERSION.getLabel()));
92 logInAsProjectAdministrator(project);
94 assertThatThrownBy(() -> call("E1"))
95 .isInstanceOf(IllegalArgumentException.class)
96 .hasMessageContaining("Cannot delete the version event of last analysis");
100 public void fail_if_category_different_than_other_and_version() {
101 ComponentDto project = newPrivateProjectDto("P1");
102 SnapshotDto analysis = db.components().insertProjectAndSnapshot(project);
103 db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory("Profile"));
104 logInAsProjectAdministrator(project);
106 assertThatThrownBy(() -> call("E1"))
107 .isInstanceOf(IllegalArgumentException.class)
108 .hasMessageContaining("Event of category 'QUALITY_PROFILE' cannot be modified.");
112 public void fail_if_event_does_not_exist() {
113 assertThatThrownBy(() -> call("E42"))
114 .isInstanceOf(NotFoundException.class)
115 .hasMessageContaining("E42' not found");
119 public void fail_if_not_enough_permission() {
120 SnapshotDto analysis = db.components().insertProjectAndSnapshot(ComponentTesting.newPrivateProjectDto());
121 db.events().insertEvent(newEvent(analysis).setUuid("E1"));
124 assertThatThrownBy(() -> call("E1"))
125 .isInstanceOf(ForbiddenException.class);
129 public void fail_if_event_not_provided() {
130 assertThatThrownBy(() -> call(null))
131 .isInstanceOf(IllegalArgumentException.class);
135 public void ws_definition() {
136 WebService.Action definition = ws.getDef();
137 assertThat(definition.key()).isEqualTo("delete_event");
138 assertThat(definition.isPost()).isTrue();
139 assertThat(definition.isInternal()).isFalse();
140 assertThat(definition.param(PARAM_EVENT).isRequired()).isTrue();
143 private void call(@Nullable String event) {
144 TestRequest request = ws.newRequest();
146 request.setParam(PARAM_EVENT, event);
152 private void logInAsProjectAdministrator(ComponentDto project) {
153 userSession.logIn().addProjectPermission(UserRole.ADMIN, project);