Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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.scanner;
  21. import java.io.InputStream;
  22. import java.io.Reader;
  23. import javax.annotation.Nullable;
  24. import org.apache.commons.lang.StringUtils;
  25. import org.mockito.ArgumentMatcher;
  26. import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
  27. import org.sonarqube.ws.client.GetRequest;
  28. import org.sonarqube.ws.client.WsRequest;
  29. import org.sonarqube.ws.client.WsResponse;
  30. import static org.mockito.ArgumentMatchers.any;
  31. import static org.mockito.ArgumentMatchers.argThat;
  32. import static org.mockito.Mockito.mock;
  33. import static org.mockito.Mockito.verify;
  34. import static org.mockito.Mockito.when;
  35. public class WsTestUtil {
  36. public static void mockStream(DefaultScannerWsClient mock, String path, InputStream is) {
  37. WsResponse response = mock(WsResponse.class);
  38. when(response.contentStream()).thenReturn(is);
  39. when(mock.call((GetRequest) argThat(new RequestMatcher(path)))).thenReturn(response);
  40. }
  41. public static void mockStream(DefaultScannerWsClient mock, InputStream is) {
  42. WsResponse response = mock(WsResponse.class);
  43. when(response.contentStream()).thenReturn(is);
  44. when(mock.call(any(GetRequest.class))).thenReturn(response);
  45. }
  46. public static void mockReader(DefaultScannerWsClient mock, Reader reader) {
  47. WsResponse response = mock(WsResponse.class);
  48. when(response.contentReader()).thenReturn(reader);
  49. when(mock.call(any(GetRequest.class))).thenReturn(response);
  50. }
  51. public static void mockReader(DefaultScannerWsClient mock, String path, Reader reader, Reader... others) {
  52. WsResponse response = mock(WsResponse.class);
  53. when(response.contentReader()).thenReturn(reader);
  54. WsResponse[] otherResponses = new WsResponse[others.length];
  55. for (int i = 0; i < others.length; i++) {
  56. WsResponse otherResponse = mock(WsResponse.class);
  57. when(otherResponse.contentReader()).thenReturn(others[i]);
  58. otherResponses[i] = otherResponse;
  59. }
  60. when(mock.call((GetRequest) argThat(new RequestMatcher(path)))).thenReturn(response, otherResponses);
  61. }
  62. public static void mockException(DefaultScannerWsClient mock, Exception e) {
  63. when(mock.call(any(GetRequest.class))).thenThrow(e);
  64. }
  65. public static void mockException(DefaultScannerWsClient mock, String path, Exception e) {
  66. when(mock.call((GetRequest) argThat(new RequestMatcher(path)))).thenThrow(e);
  67. }
  68. public static void verifyCall(DefaultScannerWsClient mock, String path) {
  69. verify(mock).call((GetRequest) argThat(new RequestMatcher(path)));
  70. }
  71. private static class RequestMatcher implements ArgumentMatcher<WsRequest> {
  72. private String path;
  73. public RequestMatcher(String path) {
  74. this.path = path;
  75. }
  76. @Override
  77. public boolean matches(@Nullable WsRequest item) {
  78. if (item == null) {
  79. return false;
  80. }
  81. return StringUtils.equals(item.getPath(), path);
  82. }
  83. @Override
  84. public String toString() {
  85. return "Request with path: " + path;
  86. }
  87. }
  88. }