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.

UpdateEventActionIT.java 11KB

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