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.

IndexationStatusActionIT.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.ce.ws;
  21. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.runner.RunWith;
  25. import org.sonar.api.server.ws.WebService;
  26. import org.sonar.api.utils.System2;
  27. import org.sonar.db.DbTester;
  28. import org.sonar.server.issue.index.IssueIndexSyncProgressChecker;
  29. import org.sonar.server.issue.index.IssueSyncProgress;
  30. import org.sonar.server.tester.UserSessionRule;
  31. import org.sonar.server.ws.WsActionTester;
  32. import org.sonarqube.ws.Ce.IndexationStatusWsResponse;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. import static org.mockito.ArgumentMatchers.any;
  35. import static org.mockito.Mockito.mock;
  36. import static org.mockito.Mockito.when;
  37. @RunWith(DataProviderRunner.class)
  38. public class IndexationStatusActionIT {
  39. @Rule
  40. public UserSessionRule userSession = UserSessionRule.standalone();
  41. public IssueIndexSyncProgressChecker issueIndexSyncProgressCheckerMock = mock(IssueIndexSyncProgressChecker.class);
  42. @Rule
  43. public DbTester db = DbTester.create(System2.INSTANCE);
  44. private final WsActionTester ws = new WsActionTester(new IndexationStatusAction(db.getDbClient(), issueIndexSyncProgressCheckerMock));
  45. @Test
  46. public void definition() {
  47. WebService.Action def = ws.getDef();
  48. assertThat(def.key()).isEqualTo("indexation_status");
  49. assertThat(def.isInternal()).isTrue();
  50. assertThat(def.isPost()).isFalse();
  51. assertThat(def.params()).isEmpty();
  52. }
  53. @Test
  54. public void verify_example_of_response() {
  55. when(issueIndexSyncProgressCheckerMock.getIssueSyncProgress(any())).thenReturn(new IssueSyncProgress(false, 22, 38, false));
  56. ws.newRequest().execute().assertJson(ws.getDef().responseExampleAsString());
  57. }
  58. @Test
  59. public void call_whenNoTasksLeft_shouldReturnCompleted() {
  60. when(issueIndexSyncProgressCheckerMock.getIssueSyncProgress(any())).thenReturn(new IssueSyncProgress(true, 10, 10, false));
  61. IndexationStatusWsResponse response = ws.newRequest()
  62. .executeProtobuf(IndexationStatusWsResponse.class);
  63. assertThat(response.getCompletedCount()).isEqualTo(10);
  64. assertThat(response.getTotal()).isEqualTo(10);
  65. assertThat(response.getIsCompleted()).isTrue();
  66. assertThat(response.getHasFailures()).isFalse();
  67. }
  68. @Test
  69. public void call_whenBranchesNeedIssueSync_shouldReturnNotCompleted() {
  70. when(issueIndexSyncProgressCheckerMock.getIssueSyncProgress(any())).thenReturn(new IssueSyncProgress(false, 0, 10, false));
  71. IndexationStatusWsResponse response = ws.newRequest()
  72. .executeProtobuf(IndexationStatusWsResponse.class);
  73. assertThat(response.getCompletedCount()).isZero();
  74. assertThat(response.getTotal()).isEqualTo(10);
  75. assertThat(response.getIsCompleted()).isFalse();
  76. assertThat(response.getHasFailures()).isFalse();
  77. }
  78. }