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