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.

DefaultProjectRepositoriesLoaderTest.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.repository;
  21. import com.google.common.io.Resources;
  22. import java.io.ByteArrayInputStream;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.net.HttpURLConnection;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  30. import org.sonar.api.utils.MessageException;
  31. import org.sonar.scanner.WsTestUtil;
  32. import org.sonar.scanner.http.DefaultScannerWsClient;
  33. import org.sonarqube.ws.Batch.WsProjectResponse;
  34. import org.sonarqube.ws.client.HttpException;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  37. import static org.mockito.ArgumentMatchers.any;
  38. import static org.mockito.ArgumentMatchers.anyInt;
  39. import static org.mockito.Mockito.mock;
  40. import static org.mockito.Mockito.when;
  41. public class DefaultProjectRepositoriesLoaderTest {
  42. private final static String PROJECT_KEY = "foo?";
  43. private DefaultProjectRepositoriesLoader loader;
  44. private DefaultScannerWsClient wsClient;
  45. @Before
  46. public void prepare() throws IOException {
  47. wsClient = mock(DefaultScannerWsClient.class);
  48. InputStream is = mockData();
  49. WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F", is);
  50. loader = new DefaultProjectRepositoriesLoader(wsClient);
  51. }
  52. @Test
  53. public void continueOnHttp404Exception() {
  54. when(wsClient.call(any())).thenThrow(new HttpException("/batch/project.protobuf?key=foo%3F", HttpURLConnection.HTTP_NOT_FOUND, ""));
  55. ProjectRepositories proj = loader.load(PROJECT_KEY, null);
  56. assertThat(proj.exists()).isFalse();
  57. }
  58. @Test(expected = IllegalStateException.class)
  59. public void failOnNonHttp404Exception() {
  60. when(wsClient.call(any())).thenThrow(IllegalStateException.class);
  61. ProjectRepositories proj = loader.load(PROJECT_KEY, null);
  62. assertThat(proj.exists()).isFalse();
  63. }
  64. @Test(expected = IllegalStateException.class)
  65. public void parsingError() throws IOException {
  66. InputStream is = mock(InputStream.class);
  67. when(is.read(any(byte[].class), anyInt(), anyInt())).thenThrow(IOException.class);
  68. WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F", is);
  69. loader.load(PROJECT_KEY, null);
  70. }
  71. @Test(expected = IllegalStateException.class)
  72. public void failFastHttpError() {
  73. HttpException http = new HttpException("url", 403, null);
  74. IllegalStateException e = new IllegalStateException("http error", http);
  75. WsTestUtil.mockException(wsClient, e);
  76. loader.load(PROJECT_KEY, null);
  77. }
  78. @Test
  79. public void failFastHttpErrorMessageException() {
  80. HttpException http = new HttpException("uri", 403, null);
  81. MessageException e = MessageException.of("http error", http);
  82. WsTestUtil.mockException(wsClient, e);
  83. assertThatThrownBy(() -> loader.load(PROJECT_KEY, null))
  84. .isInstanceOf(MessageException.class)
  85. .hasMessage("http error");
  86. }
  87. @Test
  88. public void deserializeResponse() {
  89. loader.load(PROJECT_KEY, null);
  90. }
  91. @Test
  92. public void passAndEncodeProjectKeyParameter() {
  93. loader.load(PROJECT_KEY, null);
  94. WsTestUtil.verifyCall(wsClient, "/batch/project.protobuf?key=foo%3F");
  95. }
  96. private InputStream mockData() throws IOException {
  97. ByteArrayOutputStream os = new ByteArrayOutputStream();
  98. WsProjectResponse.Builder projectResponseBuilder = WsProjectResponse.newBuilder();
  99. WsProjectResponse response = projectResponseBuilder.build();
  100. response.writeTo(os);
  101. return new ByteArrayInputStream(os.toByteArray());
  102. }
  103. @Test
  104. public void readRealResponse() throws IOException {
  105. InputStream is = getTestResource("project.protobuf");
  106. WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=org.sonarsource.github%3Asonar-github-plugin", is);
  107. DefaultInputFile file = mock(DefaultInputFile.class);
  108. when(file.getModuleRelativePath()).thenReturn("src/test/java/org/sonar/plugins/github/PullRequestIssuePostJobTest.java");
  109. ProjectRepositories proj = loader.load("org.sonarsource.github:sonar-github-plugin", null);
  110. FileData fd = proj.fileData("org.sonarsource.github:sonar-github-plugin", file);
  111. assertThat(fd.revision()).isEqualTo("27bf2c54633d05c5df402bbe09471fe43bd9e2e5");
  112. assertThat(fd.hash()).isEqualTo("edb6b3b9ab92d8dc53ba90ab86cd422e");
  113. }
  114. private InputStream getTestResource(String name) throws IOException {
  115. return Resources.asByteSource(this.getClass().getResource(this.getClass().getSimpleName() + "/" + name))
  116. .openBufferedStream();
  117. }
  118. }