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.

ActivityStatusActionTest.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.ce.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.core.util.Uuids;
  29. import org.sonar.db.DbClient;
  30. import org.sonar.db.DbSession;
  31. import org.sonar.db.DbTester;
  32. import org.sonar.db.ce.CeActivityDto;
  33. import org.sonar.db.ce.CeQueueDto;
  34. import org.sonar.db.component.ComponentDto;
  35. import org.sonar.db.component.ComponentTesting;
  36. import org.sonar.db.organization.OrganizationDto;
  37. import org.sonar.server.component.TestComponentFinder;
  38. import org.sonar.server.exceptions.ForbiddenException;
  39. import org.sonar.server.exceptions.NotFoundException;
  40. import org.sonar.server.tester.UserSessionRule;
  41. import org.sonar.server.ws.TestRequest;
  42. import org.sonar.server.ws.WsActionTester;
  43. import org.sonarqube.ws.Ce;
  44. import static org.assertj.core.api.Assertions.assertThat;
  45. import static org.mockito.Mockito.mock;
  46. import static org.mockito.Mockito.when;
  47. import static org.sonar.db.ce.CeQueueTesting.newCeQueueDto;
  48. import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
  49. import static org.sonar.server.ce.ws.CeWsParameters.DEPRECATED_PARAM_COMPONENT_KEY;
  50. import static org.sonar.server.ce.ws.CeWsParameters.PARAM_COMPONENT_ID;
  51. import static org.sonar.test.JsonAssert.assertJson;
  52. public class ActivityStatusActionTest {
  53. @Rule
  54. public ExpectedException expectedException = ExpectedException.none();
  55. @Rule
  56. public UserSessionRule userSession = UserSessionRule.standalone().logIn().setSystemAdministrator();
  57. @Rule
  58. public DbTester db = DbTester.create(System2.INSTANCE);
  59. private System2 system2 = mock(System2.class);
  60. private DbClient dbClient = db.getDbClient();
  61. private DbSession dbSession = db.getSession();
  62. private WsActionTester ws = new WsActionTester(new ActivityStatusAction(userSession, dbClient, TestComponentFinder.from(db), system2));
  63. @Test
  64. public void test_definition() {
  65. WebService.Action def = ws.getDef();
  66. assertThat(def.key()).isEqualTo("activity_status");
  67. assertThat(def.isInternal()).isFalse();
  68. assertThat(def.isPost()).isFalse();
  69. assertThat(def.params()).extracting(WebService.Param::key).containsOnly("componentId", "componentKey");
  70. }
  71. @Test
  72. public void json_example() {
  73. when(system2.now()).thenReturn(200123L);
  74. dbClient.ceQueueDao().insert(dbSession, newCeQueueDto("ce-queue-uuid-1").setStatus(CeQueueDto.Status.PENDING).setCreatedAt(100000));
  75. dbClient.ceQueueDao().insert(dbSession, newCeQueueDto("ce-queue-uuid-2").setStatus(CeQueueDto.Status.PENDING));
  76. dbClient.ceQueueDao().insert(dbSession, newCeQueueDto("ce-queue-uuid-3").setStatus(CeQueueDto.Status.IN_PROGRESS));
  77. for (int i = 0; i < 5; i++) {
  78. dbClient.ceActivityDao().insert(dbSession, new CeActivityDto(newCeQueueDto("ce-activity-uuid-" + i))
  79. .setStatus(CeActivityDto.Status.FAILED));
  80. }
  81. db.commit();
  82. String result = ws.newRequest().execute().getInput();
  83. assertJson(result).isSimilarTo(getClass().getResource("activity_status-example.json"));
  84. }
  85. @Test
  86. public void status_for_a_project_as_project_admin() {
  87. String projectUuid = "project-uuid";
  88. String anotherProjectUuid = "another-project-uuid";
  89. OrganizationDto organizationDto = db.organizations().insert();
  90. ComponentDto project = newPrivateProjectDto(organizationDto, projectUuid);
  91. ComponentDto anotherProject = newPrivateProjectDto(organizationDto, anotherProjectUuid);
  92. db.components().insertComponent(project);
  93. db.components().insertComponent(newPrivateProjectDto(organizationDto, anotherProjectUuid));
  94. userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
  95. // pending tasks returned
  96. insertInQueue(CeQueueDto.Status.PENDING, project);
  97. insertInQueue(CeQueueDto.Status.PENDING, project);
  98. // other tasks not returned
  99. insertInQueue(CeQueueDto.Status.IN_PROGRESS, project);
  100. insertInQueue(CeQueueDto.Status.PENDING, anotherProject);
  101. insertInQueue(CeQueueDto.Status.PENDING, null);
  102. // only one last activity for a given project
  103. insertActivity(CeActivityDto.Status.SUCCESS, project);
  104. insertActivity(CeActivityDto.Status.CANCELED, project);
  105. insertActivity(CeActivityDto.Status.FAILED, project);
  106. insertActivity(CeActivityDto.Status.FAILED, project);
  107. insertActivity(CeActivityDto.Status.FAILED, anotherProject);
  108. Ce.ActivityStatusWsResponse result = call(projectUuid);
  109. assertThat(result.getPending()).isEqualTo(2);
  110. assertThat(result.getFailing()).isEqualTo(1);
  111. }
  112. @Test
  113. public void add_pending_time() {
  114. String projectUuid = "project-uuid";
  115. OrganizationDto organizationDto = db.organizations().insert();
  116. ComponentDto project = newPrivateProjectDto(organizationDto, projectUuid);
  117. db.components().insertComponent(project);
  118. userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
  119. when(system2.now()).thenReturn(2000L);
  120. insertInQueue(CeQueueDto.Status.PENDING, project, 1000L);
  121. Ce.ActivityStatusWsResponse result = call(projectUuid);
  122. assertThat(result).extracting(Ce.ActivityStatusWsResponse::getPending, Ce.ActivityStatusWsResponse::getFailing,
  123. Ce.ActivityStatusWsResponse::getInProgress, Ce.ActivityStatusWsResponse::getPendingTime)
  124. .containsOnly(1, 0, 0, 1000L);
  125. }
  126. @Test
  127. public void empty_status() {
  128. Ce.ActivityStatusWsResponse result = call();
  129. assertThat(result.getPending()).isEqualTo(0);
  130. assertThat(result.getFailing()).isEqualTo(0);
  131. }
  132. @Test
  133. public void fail_if_component_uuid_and_key_are_provided() {
  134. ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
  135. db.components().insertComponent(project);
  136. expectedException.expect(IllegalArgumentException.class);
  137. callByComponentUuidOrComponentKey(project.uuid(), project.getDbKey());
  138. }
  139. @Test
  140. public void fail_if_component_uuid_is_unknown() {
  141. expectedException.expect(NotFoundException.class);
  142. call("unknown-uuid");
  143. }
  144. @Test
  145. public void fail_if_component_key_is_unknown() {
  146. expectedException.expect(NotFoundException.class);
  147. callByComponentKey("unknown-key");
  148. }
  149. @Test
  150. public void throw_ForbiddenException_if_not_root() {
  151. userSession.logIn();
  152. expectedException.expect(ForbiddenException.class);
  153. expectedException.expectMessage("Insufficient privileges");
  154. call();
  155. }
  156. @Test
  157. public void throw_ForbiddenException_if_not_administrator_of_requested_project() {
  158. userSession.logIn();
  159. ComponentDto project = db.components().insertPrivateProject();
  160. expectedException.expect(ForbiddenException.class);
  161. expectedException.expectMessage("Insufficient privileges");
  162. callByComponentKey(project.getDbKey());
  163. }
  164. private void insertInQueue(CeQueueDto.Status status, @Nullable ComponentDto componentDto) {
  165. insertInQueue(status, componentDto, null);
  166. }
  167. private void insertInQueue(CeQueueDto.Status status, @Nullable ComponentDto componentDto, @Nullable Long createdAt) {
  168. CeQueueDto ceQueueDto = newCeQueueDto(Uuids.createFast())
  169. .setStatus(status)
  170. .setComponent(componentDto);
  171. if (createdAt != null) {
  172. ceQueueDto.setCreatedAt(createdAt);
  173. }
  174. dbClient.ceQueueDao().insert(dbSession, ceQueueDto);
  175. db.commit();
  176. }
  177. private void insertActivity(CeActivityDto.Status status, @Nullable ComponentDto dto) {
  178. CeQueueDto ceQueueDto = newCeQueueDto(Uuids.createFast());
  179. ceQueueDto.setComponent(dto);
  180. dbClient.ceActivityDao().insert(dbSession, new CeActivityDto(ceQueueDto)
  181. .setStatus(status));
  182. db.commit();
  183. }
  184. private Ce.ActivityStatusWsResponse call() {
  185. return callByComponentUuidOrComponentKey(null, null);
  186. }
  187. private Ce.ActivityStatusWsResponse call(String componentUuid) {
  188. return callByComponentUuidOrComponentKey(componentUuid, null);
  189. }
  190. private Ce.ActivityStatusWsResponse callByComponentKey(String componentKey) {
  191. return callByComponentUuidOrComponentKey(null, componentKey);
  192. }
  193. private Ce.ActivityStatusWsResponse callByComponentUuidOrComponentKey(@Nullable String componentUuid, @Nullable String componentKey) {
  194. TestRequest request = ws.newRequest();
  195. if (componentUuid != null) {
  196. request.setParam(PARAM_COMPONENT_ID, componentUuid);
  197. }
  198. if (componentKey != null) {
  199. request.setParam(DEPRECATED_PARAM_COMPONENT_KEY, componentKey);
  200. }
  201. return request.executeProtobuf(Ce.ActivityStatusWsResponse.class);
  202. }
  203. }