]> source.dussan.org Git - sonarqube.git/blob
d29a25e84c25278df9064d21219fc31ffe1959a1
[sonarqube.git] /
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.projectanalysis.ws;
21
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;
41
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;
49
50 public class DeleteEventActionIT {
51   @Rule
52   public UserSessionRule userSession = UserSessionRule.standalone();
53   @Rule
54   public DbTester db = DbTester.create(System2.INSTANCE);
55   private DbClient dbClient = db.getDbClient();
56   private DbSession dbSession = db.getSession();
57
58   private WsActionTester ws = new WsActionTester(new DeleteEventAction(db.getDbClient(), userSession));
59
60   @Test
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);
67
68     call("E2");
69
70     List<EventDto> events = db.getDbClient().eventDao().selectByAnalysisUuid(db.getSession(), analysis.getUuid());
71     assertThat(events).extracting(EventDto::getUuid).containsExactly("E1");
72   }
73
74   @Test
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);
80
81     call("E1");
82
83     SnapshotDto newAnalysis = dbClient.snapshotDao().selectByUuid(dbSession, analysis.getUuid()).get();
84     assertThat(newAnalysis.getProjectVersion()).isNull();
85   }
86
87   @Test
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);
93
94     assertThatThrownBy(() -> call("E1"))
95       .isInstanceOf(IllegalArgumentException.class)
96       .hasMessageContaining("Cannot delete the version event of last analysis");
97   }
98
99   @Test
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);
105
106     assertThatThrownBy(() -> call("E1"))
107       .isInstanceOf(IllegalArgumentException.class)
108       .hasMessageContaining("Event of category 'QUALITY_PROFILE' cannot be modified.");
109   }
110
111   @Test
112   public void fail_if_event_does_not_exist() {
113     assertThatThrownBy(() -> call("E42"))
114       .isInstanceOf(NotFoundException.class)
115       .hasMessageContaining("E42' not found");
116   }
117
118   @Test
119   public void fail_if_not_enough_permission() {
120     SnapshotDto analysis = db.components().insertProjectAndSnapshot(ComponentTesting.newPrivateProjectDto());
121     db.events().insertEvent(newEvent(analysis).setUuid("E1"));
122     userSession.logIn();
123
124     assertThatThrownBy(() -> call("E1"))
125       .isInstanceOf(ForbiddenException.class);
126   }
127
128   @Test
129   public void fail_if_event_not_provided() {
130     assertThatThrownBy(() -> call(null))
131       .isInstanceOf(IllegalArgumentException.class);
132   }
133
134   @Test
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();
141   }
142
143   private void call(@Nullable String event) {
144     TestRequest request = ws.newRequest();
145     if (event != null) {
146       request.setParam(PARAM_EVENT, event);
147     }
148
149     request.execute();
150   }
151
152   private void logInAsProjectAdministrator(ComponentDto project) {
153     userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
154   }
155 }