]> source.dussan.org Git - sonarqube.git/blob
465a337a614a563cf56bb012704088131ecf8890
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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 javax.annotation.Nullable;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.api.utils.System2;
27 import org.sonar.api.web.UserRole;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.component.ProjectData;
32 import org.sonar.db.component.SnapshotDto;
33 import org.sonar.db.event.EventDto;
34 import org.sonar.server.exceptions.ForbiddenException;
35 import org.sonar.server.exceptions.NotFoundException;
36 import org.sonar.server.tester.UserSessionRule;
37 import org.sonar.server.ws.TestRequest;
38 import org.sonar.server.ws.WsActionTester;
39 import org.sonarqube.ws.ProjectAnalyses;
40 import org.sonarqube.ws.ProjectAnalyses.UpdateEventResponse;
41
42 import static java.util.Optional.ofNullable;
43 import static org.apache.commons.lang.StringUtils.repeat;
44 import static org.assertj.core.api.Assertions.assertThat;
45 import static org.assertj.core.api.Assertions.assertThatThrownBy;
46 import static org.sonar.db.component.SnapshotTesting.newAnalysis;
47 import static org.sonar.db.event.EventTesting.newEvent;
48 import static org.sonar.server.projectanalysis.ws.EventCategory.OTHER;
49 import static org.sonar.server.projectanalysis.ws.EventCategory.VERSION;
50 import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_EVENT;
51 import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_NAME;
52 import static org.sonar.test.JsonAssert.assertJson;
53 import static org.sonarqube.ws.client.WsRequest.Method.POST;
54
55 public class UpdateEventActionIT {
56   @Rule
57   public UserSessionRule userSession = UserSessionRule.standalone();
58   @Rule
59   public DbTester db = DbTester.create(System2.INSTANCE);
60
61   private final DbClient dbClient = db.getDbClient();
62   private final DbSession dbSession = db.getSession();
63   private final WsActionTester ws = new WsActionTester(new UpdateEventAction(dbClient, userSession));
64
65   @Test
66   public void json_example() {
67     ProjectData project = db.components().insertPrivateProject();
68     SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(project.getMainBranchDto()).setUuid("A2"));
69     db.events().insertEvent(newEvent(analysis)
70       .setUuid("E1")
71       .setCategory(OTHER.getLabel())
72       .setName("Original Name")
73       .setDescription("Original Description"));
74     logInAsProjectAdministrator(project);
75
76     String result = ws.newRequest()
77       .setParam(PARAM_EVENT, "E1")
78       .setParam(PARAM_NAME, "My Custom Event")
79       .execute().getInput();
80
81     assertJson(result).isSimilarTo(getClass().getResource("update_event-example.json"));
82   }
83
84   @Test
85   public void update_name_in_db() {
86     SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6");
87     EventDto originalEvent = db.events().insertEvent(newEvent(analysis).setUuid("E1").setName("Original Name"));
88
89     call("E1", "name");
90
91     EventDto newEvent = dbClient.eventDao().selectByUuid(dbSession, "E1").get();
92     assertThat(newEvent.getName()).isEqualTo("name");
93     assertThat(newEvent.getDescription()).isNull();
94     assertThat(newEvent.getCategory()).isEqualTo(originalEvent.getCategory());
95     assertThat(newEvent.getDate()).isEqualTo(originalEvent.getDate());
96     assertThat(newEvent.getCreatedAt()).isEqualTo(originalEvent.getCreatedAt());
97   }
98
99   @Test
100   public void ws_response_with_updated_name() {
101     SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6");
102     EventDto originalEvent = db.events().insertEvent(newEvent(analysis).setUuid("E1").setName("Original Name"));
103
104     ProjectAnalyses.Event result = call("E1", "name").getEvent();
105
106     assertThat(result.getName()).isEqualTo("name");
107     assertThat(result.hasDescription()).isFalse();
108     assertThat(result.getCategory()).isEqualTo(OTHER.name());
109     assertThat(result.getAnalysis()).isEqualTo(originalEvent.getAnalysisUuid());
110     assertThat(result.getKey()).isEqualTo("E1");
111   }
112
113   @Test
114   public void update_VERSION_event_update_analysis_version() {
115     SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6");
116     db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(VERSION.getLabel()));
117
118     call("E1", "6.3");
119
120     SnapshotDto updatedAnalysis = dbClient.snapshotDao().selectByUuid(dbSession, analysis.getUuid()).get();
121     assertThat(updatedAnalysis.getProjectVersion()).isEqualTo("6.3");
122   }
123
124   @Test
125   public void update_OTHER_event_does_not_update_analysis_version() {
126     SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6");
127     db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(OTHER.getLabel()));
128
129     call("E1", "6.3");
130
131     SnapshotDto updatedAnalysis = dbClient.snapshotDao().selectByUuid(dbSession, analysis.getUuid()).get();
132     assertThat(updatedAnalysis.getProjectVersion()).isEqualTo("5.6");
133   }
134
135   @Test
136   public void update_name_only_in_db() {
137     SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6");
138     EventDto originalEvent = db.events().insertEvent(newEvent(analysis).setUuid("E1").setName("Original Name").setDescription("Original Description"));
139
140     call("E1", "name");
141
142     EventDto newEvent = dbClient.eventDao().selectByUuid(dbSession, "E1").get();
143     assertThat(newEvent.getName()).isEqualTo("name");
144     assertThat(newEvent.getDescription()).isEqualTo(originalEvent.getDescription());
145   }
146
147   @Test
148   public void test_ws_definition() {
149     WebService.Action definition = ws.getDef();
150
151     assertThat(definition.key()).isEqualTo("update_event");
152     assertThat(definition.responseExampleAsString()).isNotEmpty();
153     assertThat(definition.isPost()).isTrue();
154     assertThat(definition.since()).isEqualTo("6.3");
155     assertThat(definition.param(PARAM_EVENT).isRequired()).isTrue();
156     assertThat(definition.param(PARAM_NAME).isRequired()).isTrue();
157   }
158
159   @Test
160   public void throw_ForbiddenException_if_not_project_administrator() {
161     ProjectData project = db.components().insertPrivateProject();
162     SnapshotDto analysis = db.components().insertSnapshot(project.getMainBranchDto());
163     db.events().insertEvent(newEvent(analysis).setUuid("E1"));
164     userSession.logIn().addProjectPermission(UserRole.USER, project.getProjectDto());
165
166     assertThatThrownBy(() -> call("E1", "name"))
167       .isInstanceOf(ForbiddenException.class);
168   }
169
170   @Test
171   public void fail_if_event_is_not_found() {
172     userSession.logIn().setSystemAdministrator();
173
174     assertThatThrownBy(() -> call("E42", "name"))
175       .isInstanceOf(NotFoundException.class)
176       .hasMessageContaining("Event 'E42' not found");
177   }
178
179   @Test
180   public void fail_if_no_name() {
181     SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6");
182     db.events().insertEvent(newEvent(analysis).setUuid("E1"));
183
184     assertThatThrownBy(() -> call("E1", null))
185       .isInstanceOf(IllegalArgumentException.class)
186       .hasMessageContaining("The 'name' parameter is missing");
187   }
188
189   @Test
190   public void fail_if_blank_name() {
191     SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6");
192     db.events().insertEvent(newEvent(analysis).setUuid("E1"));
193
194     assertThatThrownBy(() -> call("E1", "     "))
195       .isInstanceOf(IllegalArgumentException.class)
196       .hasMessageContaining("A non empty name is required");
197   }
198
199   @Test
200   public void fail_if_category_other_than_other_or_version() {
201     SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6");
202     db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory("Profile"));
203
204     assertThatThrownBy(() -> call("E1", "name"))
205       .isInstanceOf(IllegalArgumentException.class)
206       .hasMessageContaining("Event of category 'QUALITY_PROFILE' cannot be modified.");
207   }
208
209   @Test
210   public void fail_if_other_event_with_same_name_on_same_analysis() {
211     SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6");
212     db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(OTHER.getLabel()).setName("E1 name"));
213     db.events().insertEvent(newEvent(analysis).setUuid("E2").setCategory(OTHER.getLabel()).setName("E2 name"));
214
215     assertThatThrownBy(() -> call("E2", "E1 name"))
216       .isInstanceOf(IllegalArgumentException.class)
217       .hasMessageContaining("An 'Other' event with the same name already exists on analysis '" + analysis.getUuid() + "'");
218   }
219
220   @Test
221   public void limit_version_name_length_to_100_for_analysis_events() {
222     SnapshotDto analysis = createAnalysisAndLogInAsProjectAdministrator("5.6");
223     db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(OTHER.getLabel()).setName("E1 name"));
224     db.events().insertEvent(newEvent(analysis).setUuid("E2").setCategory(VERSION.getLabel()).setName("E2 name"));
225
226     call("E1", repeat("a", 100));
227     call("E1", repeat("a", 101));
228     call("E2", repeat("a", 100));
229
230
231     assertThatThrownBy(() -> call("E2", repeat("a", 101)))
232       .isInstanceOf(IllegalArgumentException.class)
233       .hasMessageContaining("Event name length (101) is longer than the maximum authorized (100). " +
234         "'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided");
235   }
236
237   private UpdateEventResponse call(@Nullable String eventUuid, @Nullable String name) {
238     TestRequest request = ws.newRequest()
239       .setMethod(POST.name());
240     ofNullable(eventUuid).ifPresent(e -> request.setParam(PARAM_EVENT, e));
241     ofNullable(name).ifPresent(n -> request.setParam(PARAM_NAME, n));
242
243     return request.executeProtobuf(UpdateEventResponse.class);
244   }
245
246   private void logInAsProjectAdministrator(ProjectData project) {
247     userSession.logIn().addProjectPermission(UserRole.ADMIN, project.getProjectDto())
248       .registerBranches(project.getMainBranchDto());
249   }
250
251   private SnapshotDto createAnalysisAndLogInAsProjectAdministrator(String version) {
252     ProjectData project = db.components().insertPrivateProject();
253     SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(project.getMainBranchDto()).setProjectVersion(version));
254     logInAsProjectAdministrator(project);
255     return analysis;
256   }
257 }