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.

UpdateEventActionTest.java 11KB

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