diff options
author | Lukasz Jarocki <lukasz.jarocki@sonarsource.com> | 2022-04-19 11:36:02 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-04-29 20:03:18 +0000 |
commit | 20537fc4569b2b47d04ae52ee12ed8e825285884 (patch) | |
tree | 92e662e2c45a06ba4ccc6bd1244f217972e787af /sonar-scanner-engine | |
parent | 9c4c096067a9f2ba3f4b4fcf6af61957d369e570 (diff) | |
download | sonarqube-20537fc4569b2b47d04ae52ee12ed8e825285884.tar.gz sonarqube-20537fc4569b2b47d04ae52ee12ed8e825285884.zip |
SONAR-16260 scanner passing new context header - projectKey
Diffstat (limited to 'sonar-scanner-engine')
13 files changed, 114 insertions, 61 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/DefaultScannerWsClient.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/DefaultScannerWsClient.java index 8dc5dc3a39f..a554aa5c238 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/DefaultScannerWsClient.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/DefaultScannerWsClient.java @@ -32,7 +32,9 @@ import org.sonar.api.utils.MessageException; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; +import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.HttpException; +import org.sonarqube.ws.client.PostRequest; import org.sonarqube.ws.client.WsClient; import org.sonarqube.ws.client.WsConnector; import org.sonarqube.ws.client.WsRequest; @@ -48,14 +50,18 @@ public class DefaultScannerWsClient implements ScannerWsClient { private static final int MAX_ERROR_MSG_LEN = 128; private static final Logger LOG = Loggers.get(DefaultScannerWsClient.class); + private static final String PROJECT_KEY_CONTEXT_HEADER = "PROJECT_KEY"; + private final WsClient target; private final boolean hasCredentials; private final GlobalAnalysisMode globalMode; + private final ScannerProperties scannerProperties; - public DefaultScannerWsClient(WsClient target, boolean hasCredentials, GlobalAnalysisMode globalMode) { + public DefaultScannerWsClient(WsClient target, boolean hasCredentials, GlobalAnalysisMode globalMode, ScannerProperties scannerProperties) { this.target = target; this.hasCredentials = hasCredentials; this.globalMode = globalMode; + this.scannerProperties = scannerProperties; } /** @@ -67,7 +73,7 @@ public class DefaultScannerWsClient implements ScannerWsClient { * @throws MessageException if there was a problem with authentication or if a error message was parsed from the response. * @throws HttpException if the response code is not in range [200..300). Consider using {@link #createErrorMessage(HttpException)} to create more relevant messages for the users. */ - public WsResponse call(WsRequest request) { + private WsResponse getResponse(WsRequest request) { checkState(!globalMode.isMediumTest(), "No WS call should be made in medium test mode"); Profiler profiler = Profiler.createIfDebug(LOG).start(); WsResponse response = target.wsConnector().call(request); @@ -76,6 +82,16 @@ public class DefaultScannerWsClient implements ScannerWsClient { return response; } + public WsResponse call(GetRequest getRequest) { + getRequest.setHeader(PROJECT_KEY_CONTEXT_HEADER, scannerProperties.getProjectKey()); + return getResponse(getRequest); + } + + public WsResponse call(PostRequest postRequest) { + postRequest.setHeader(PROJECT_KEY_CONTEXT_HEADER, scannerProperties.getProjectKey()); + return getResponse(postRequest); + } + public String baseUrl() { return target.wsConnector().baseUrl(); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java index ce09309cab1..bd8452f4ad1 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java @@ -19,11 +19,14 @@ */ package org.sonar.scanner.bootstrap; -import org.sonarqube.ws.client.WsRequest; +import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.PostRequest; import org.sonarqube.ws.client.WsResponse; public interface ScannerWsClient { - WsResponse call(WsRequest request); + WsResponse call(GetRequest request); + + WsResponse call(PostRequest request); String baseUrl(); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClientProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClientProvider.java index 678b26f17fd..606785aaf8a 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClientProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClientProvider.java @@ -36,7 +36,8 @@ public class ScannerWsClientProvider { static final int DEFAULT_READ_TIMEOUT_SEC = 60; @Bean("DefaultScannerWsClient") - public DefaultScannerWsClient provide(ScannerProperties scannerProps, EnvironmentInformation env, GlobalAnalysisMode globalMode, System2 system) { + public DefaultScannerWsClient provide(ScannerProperties scannerProps, EnvironmentInformation env, GlobalAnalysisMode globalMode, + System2 system) { String url = defaultIfBlank(scannerProps.property("sonar.host.url"), "http://localhost:9000"); HttpConnector.Builder connectorBuilder = HttpConnector.newBuilder(); @@ -56,6 +57,7 @@ public class ScannerWsClientProvider { connectorBuilder.proxyCredentials(proxyUser, System.getProperty("http.proxyPassword")); } - return new DefaultScannerWsClient(WsClientFactories.getDefault().newClient(connectorBuilder.build()), login != null, globalMode); + return new DefaultScannerWsClient(WsClientFactories.getDefault().newClient(connectorBuilder.build()), login != null, + globalMode, scannerProps); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/WsTestUtil.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/WsTestUtil.java index 45905619f03..157c0745b5e 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/WsTestUtil.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/WsTestUtil.java @@ -25,6 +25,7 @@ import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; import org.mockito.ArgumentMatcher; import org.sonar.scanner.bootstrap.DefaultScannerWsClient; +import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.WsRequest; import org.sonarqube.ws.client.WsResponse; @@ -38,19 +39,19 @@ public class WsTestUtil { public static void mockStream(DefaultScannerWsClient mock, String path, InputStream is) { WsResponse response = mock(WsResponse.class); when(response.contentStream()).thenReturn(is); - when(mock.call(argThat(new RequestMatcher(path)))).thenReturn(response); + when(mock.call((GetRequest) argThat(new RequestMatcher(path)))).thenReturn(response); } public static void mockStream(DefaultScannerWsClient mock, InputStream is) { WsResponse response = mock(WsResponse.class); when(response.contentStream()).thenReturn(is); - when(mock.call(any(WsRequest.class))).thenReturn(response); + when(mock.call(any(GetRequest.class))).thenReturn(response); } public static void mockReader(DefaultScannerWsClient mock, Reader reader) { WsResponse response = mock(WsResponse.class); when(response.contentReader()).thenReturn(reader); - when(mock.call(any(WsRequest.class))).thenReturn(response); + when(mock.call(any(GetRequest.class))).thenReturn(response); } public static void mockReader(DefaultScannerWsClient mock, String path, Reader reader, Reader... others) { @@ -63,19 +64,19 @@ public class WsTestUtil { otherResponses[i] = otherResponse; } - when(mock.call(argThat(new RequestMatcher(path)))).thenReturn(response, otherResponses); + when(mock.call((GetRequest) argThat(new RequestMatcher(path)))).thenReturn(response, otherResponses); } public static void mockException(DefaultScannerWsClient mock, Exception e) { - when(mock.call(any(WsRequest.class))).thenThrow(e); + when(mock.call(any(GetRequest.class))).thenThrow(e); } public static void mockException(DefaultScannerWsClient mock, String path, Exception e) { - when(mock.call(argThat(new RequestMatcher(path)))).thenThrow(e); + when(mock.call((GetRequest) argThat(new RequestMatcher(path)))).thenThrow(e); } public static void verifyCall(DefaultScannerWsClient mock, String path) { - verify(mock).call(argThat(new RequestMatcher(path))); + verify(mock).call((GetRequest) argThat(new RequestMatcher(path))); } private static class RequestMatcher implements ArgumentMatcher<WsRequest> { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/DefaultScannerWsClientTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/DefaultScannerWsClientTest.java index 9e11d10290e..20dc164f075 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/DefaultScannerWsClientTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/DefaultScannerWsClientTest.java @@ -19,9 +19,9 @@ */ package org.sonar.scanner.bootstrap; -import java.util.Collections; import java.util.List; import org.apache.commons.lang.StringUtils; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.mockito.Mockito; @@ -31,8 +31,8 @@ import org.sonar.api.utils.log.LoggerLevel; import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.HttpException; import org.sonarqube.ws.client.MockWsResponse; +import org.sonarqube.ws.client.PostRequest; import org.sonarqube.ws.client.WsClient; -import org.sonarqube.ws.client.WsRequest; import org.sonarqube.ws.client.WsResponse; import static org.assertj.core.api.Assertions.assertThat; @@ -45,17 +45,24 @@ public class DefaultScannerWsClientTest { @Rule public LogTester logTester = new LogTester(); - WsClient wsClient = mock(WsClient.class, Mockito.RETURNS_DEEP_STUBS); + private final WsClient wsClient = mock(WsClient.class, Mockito.RETURNS_DEEP_STUBS); + + private final ScannerProperties scannerProperties = mock(ScannerProperties.class); + + @Before + public void before() { + when(scannerProperties.getProjectKey()).thenReturn("projectKey"); + } @Test public void log_and_profile_request_if_debug_level() { - WsRequest request = newRequest(); + GetRequest request = newGetRequest(); WsResponse response = newResponse().setRequestUrl("https://local/api/issues/search"); when(wsClient.wsConnector().call(request)).thenReturn(response); logTester.setLevel(LoggerLevel.DEBUG); - DefaultScannerWsClient underTest = new DefaultScannerWsClient(wsClient, false, new GlobalAnalysisMode( - new ScannerProperties(Collections.emptyMap()))); + DefaultScannerWsClient underTest = new DefaultScannerWsClient(wsClient, false, new GlobalAnalysisMode(scannerProperties), + scannerProperties); WsResponse result = underTest.call(request); @@ -69,6 +76,21 @@ public class DefaultScannerWsClientTest { } @Test + public void call_alwaysAddContextHeaders() { + GetRequest getRequest = newGetRequest(); + PostRequest postRequest = newPostRequest(); + + DefaultScannerWsClient underTest = new DefaultScannerWsClient(wsClient, false, new GlobalAnalysisMode(scannerProperties), + scannerProperties); + + underTest.call(getRequest); + underTest.call(postRequest); + + assertThat(getRequest.getHeaders().getValue("PROJECT_KEY")).contains("projectKey"); + assertThat(postRequest.getHeaders().getValue("PROJECT_KEY")).contains("projectKey"); + } + + @Test public void create_error_msg_from_json() { String content = "{\"errors\":[{\"msg\":\"missing scan permission\"}, {\"msg\":\"missing another permission\"}]}"; assertThat(DefaultScannerWsClient.createErrorMessage(new HttpException("url", 400, content))).isEqualTo("missing scan permission, missing another permission"); @@ -88,12 +110,12 @@ public class DefaultScannerWsClientTest { @Test public void fail_if_requires_credentials() { - WsRequest request = newRequest(); + GetRequest request = newGetRequest(); WsResponse response = newResponse().setCode(401); when(wsClient.wsConnector().call(request)).thenReturn(response); assertThatThrownBy(() -> new DefaultScannerWsClient(wsClient, false, - new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))).call(request)) + new GlobalAnalysisMode(scannerProperties), scannerProperties).call(request)) .isInstanceOf(MessageException.class) .hasMessage("Not authorized. Analyzing this project requires authentication. Please provide a user token in sonar.login or other " + "credentials in sonar.login and sonar.password."); @@ -101,39 +123,39 @@ public class DefaultScannerWsClientTest { @Test public void fail_if_credentials_are_not_valid() { - WsRequest request = newRequest(); + GetRequest request = newGetRequest(); WsResponse response = newResponse().setCode(401); when(wsClient.wsConnector().call(request)).thenReturn(response); assertThatThrownBy(() -> new DefaultScannerWsClient(wsClient, /* credentials are configured */true, - new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))).call(request)) + new GlobalAnalysisMode(scannerProperties), scannerProperties).call(request)) .isInstanceOf(MessageException.class) .hasMessage("Not authorized. Please check the properties sonar.login and sonar.password."); } @Test public void fail_if_requires_permission() { - WsRequest request = newRequest(); + GetRequest request = newGetRequest(); WsResponse response = newResponse() .setCode(403); when(wsClient.wsConnector().call(request)).thenReturn(response); assertThatThrownBy(() -> new DefaultScannerWsClient(wsClient, true, - new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))).call(request)) + new GlobalAnalysisMode(scannerProperties), scannerProperties).call(request)) .isInstanceOf(MessageException.class) .hasMessage("You're not authorized to run analysis. Please contact the project administrator."); } @Test public void fail_if_bad_request() { - WsRequest request = newRequest(); + GetRequest request = newGetRequest(); WsResponse response = newResponse() .setCode(400) .setContent("{\"errors\":[{\"msg\":\"Boo! bad request! bad!\"}]}"); when(wsClient.wsConnector().call(request)).thenReturn(response); assertThatThrownBy(() -> new DefaultScannerWsClient(wsClient, true, - new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))).call(request)) + new GlobalAnalysisMode(scannerProperties), scannerProperties).call(request)) .isInstanceOf(MessageException.class) .hasMessage("Boo! bad request! bad!"); } @@ -142,7 +164,11 @@ public class DefaultScannerWsClientTest { return new MockWsResponse().setRequestUrl("https://local/api/issues/search"); } - private WsRequest newRequest() { + private GetRequest newGetRequest() { return new GetRequest("api/issues/search"); } + + private PostRequest newPostRequest() { + return new PostRequest("api/ce/task"); + } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/PluginFilesTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/PluginFilesTest.java index 2d42f7bded5..0c726b4035a 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/PluginFilesTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/PluginFilesTest.java @@ -47,6 +47,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.fs.internal.DefaultInputProject; import org.sonar.api.config.internal.MapSettings; import org.sonar.scanner.bootstrap.ScannerPluginInstaller.InstalledPlugin; import org.sonarqube.ws.client.HttpConnector; @@ -56,6 +57,7 @@ import static org.apache.commons.io.FileUtils.moveFile; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.ThrowableAssert.ThrowingCallable; +import static org.mockito.Mockito.mock; public class PluginFilesTest { @@ -71,7 +73,8 @@ public class PluginFilesTest { public void setUp() throws Exception { HttpConnector connector = HttpConnector.newBuilder().url(server.url("/").toString()).build(); GlobalAnalysisMode analysisMode = new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap())); - DefaultScannerWsClient wsClient = new DefaultScannerWsClient(WsClientFactories.getDefault().newClient(connector), false, analysisMode); + DefaultScannerWsClient wsClient = new DefaultScannerWsClient(WsClientFactories.getDefault().newClient(connector), false, + analysisMode, mock(ScannerProperties.class)); userHome = temp.newFolder(); MapSettings settings = new MapSettings(); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerWsClientProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerWsClientProviderTest.java index 4fe6fd6dad8..e6e6d3ba627 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerWsClientProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerWsClientProviderTest.java @@ -39,7 +39,8 @@ public class ScannerWsClientProviderTest { public void provide_client_with_default_settings() { ScannerProperties settings = new ScannerProperties(new HashMap<>()); - DefaultScannerWsClient client = underTest.provide(settings, env, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap())), mock(System2.class)); + DefaultScannerWsClient client = underTest.provide(settings, env, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap())), + mock(System2.class)); assertThat(client).isNotNull(); assertThat(client.baseUrl()).isEqualTo("http://localhost:9000/"); @@ -59,7 +60,8 @@ public class ScannerWsClientProviderTest { props.put("sonar.ws.timeout", "42"); ScannerProperties settings = new ScannerProperties(props); - DefaultScannerWsClient client = underTest.provide(settings, env, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap())), mock(System2.class)); + DefaultScannerWsClient client = underTest.provide(settings, env, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap())), + mock(System2.class)); assertThat(client).isNotNull(); HttpConnector httpConnector = (HttpConnector) client.wsConnector(); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheLoaderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheLoaderTest.java index 52eb5b484df..393cd5e3e89 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheLoaderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheLoaderTest.java @@ -35,9 +35,8 @@ import org.sonar.api.utils.MessageException; import org.sonar.scanner.bootstrap.DefaultScannerWsClient; import org.sonar.scanner.protocol.internal.ScannerInternal.AnalysisCacheMsg; import org.sonar.scanner.scan.branch.BranchConfiguration; -import org.sonar.scanner.scan.branch.BranchType; +import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.HttpException; -import org.sonarqube.ws.client.WsRequest; import org.sonarqube.ws.client.WsResponse; import static org.assertj.core.api.Assertions.assertThat; @@ -61,7 +60,7 @@ public class AnalysisCacheLoaderTest { @Before public void before() { when(project.key()).thenReturn("myproject"); - when(wsClient.call(any())).thenReturn(response); + when(wsClient.call(any(GetRequest.class))).thenReturn(response); } @Test @@ -92,13 +91,13 @@ public class AnalysisCacheLoaderTest { @Test public void returns_empty_if_404() { - when(wsClient.call(any())).thenThrow(new HttpException("url", 404, "content")); + when(wsClient.call(any(GetRequest.class))).thenThrow(new HttpException("url", 404, "content")); assertThat(loader.load()).isEmpty(); } @Test public void throw_error_if_http_exception_not_404() { - when(wsClient.call(any())).thenThrow(new HttpException("url", 401, "content")); + when(wsClient.call(any(GetRequest.class))).thenThrow(new HttpException("url", 401, "content")); assertThatThrownBy(loader::load) .isInstanceOf(MessageException.class) .hasMessage("Failed to download analysis cache: HTTP code 401: content"); @@ -113,7 +112,7 @@ public class AnalysisCacheLoaderTest { } private void assertRequestPath(String expectedPath) { - ArgumentCaptor<WsRequest> requestCaptor = ArgumentCaptor.forClass(WsRequest.class); + ArgumentCaptor<GetRequest> requestCaptor = ArgumentCaptor.forClass(GetRequest.class); verify(wsClient).call(requestCaptor.capture()); assertThat(requestCaptor.getValue().getPath()).isEqualTo(expectedPath); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/qualitygate/QualityGateCheckTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/qualitygate/QualityGateCheckTest.java index bd3803a7eb1..89613f6bba0 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/qualitygate/QualityGateCheckTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/qualitygate/QualityGateCheckTest.java @@ -38,9 +38,9 @@ import org.sonarqube.ws.Ce; import org.sonarqube.ws.Ce.TaskStatus; import org.sonarqube.ws.Qualitygates; import org.sonarqube.ws.Qualitygates.ProjectStatusResponse.Status; +import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.HttpException; import org.sonarqube.ws.client.MockWsResponse; -import org.sonarqube.ws.client.WsRequest; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -266,8 +266,8 @@ public class QualityGateCheckTest { .hasMessageContaining("CE Task finished abnormally with status: " + taskStatus.name()); } - private WsRequest newGetCeTaskRequest() { - return argThat(new WsRequestPathMatcher("api/ce/task")); + private GetRequest newGetCeTaskRequest() { + return argThat(new GetRequestPathMatcher("api/ce/task")); } private MockWsResponse getCeTaskWsResponse(TaskStatus status) { @@ -302,8 +302,8 @@ public class QualityGateCheckTest { .isInstanceOf(IllegalStateException.class); } - private WsRequest newGetQualityGateRequest() { - return argThat(new WsRequestPathMatcher("api/qualitygates/project_status")); + private GetRequest newGetQualityGateRequest() { + return argThat(new GetRequestPathMatcher("api/qualitygates/project_status")); } private MockWsResponse getQualityGateWsResponse(Status status) { @@ -325,15 +325,15 @@ public class QualityGateCheckTest { }; } - private static class WsRequestPathMatcher implements ArgumentMatcher<WsRequest> { + private static class GetRequestPathMatcher implements ArgumentMatcher<GetRequest> { String path; - WsRequestPathMatcher(String path) { + GetRequestPathMatcher(String path) { this.path = path; } @Override - public boolean matches(WsRequest right) { + public boolean matches(GetRequest right) { return path.equals(right.getPath()); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java index e30e630a99c..55d6772e37e 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java @@ -45,6 +45,7 @@ import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonarqube.ws.Ce; import org.sonarqube.ws.client.HttpException; import org.sonarqube.ws.client.MockWsResponse; +import org.sonarqube.ws.client.PostRequest; import org.sonarqube.ws.client.WsRequest; import org.sonarqube.ws.client.WsResponse; @@ -96,12 +97,12 @@ public class ReportPublisherTest { public void use_30s_write_timeout() { MockWsResponse submitMockResponse = new MockWsResponse(); submitMockResponse.setContent(Ce.SubmitResponse.newBuilder().setTaskId("task-1234").build().toByteArray()); - when(wsClient.call(any())).thenReturn(submitMockResponse); + when(wsClient.call(any(PostRequest.class))).thenReturn(submitMockResponse); underTest.start(); underTest.execute(); - verify(wsClient).call(argThat(req -> req.getWriteTimeOutInMs().orElse(0) == 30_000)); + verify(wsClient).call((PostRequest) argThat(req -> ((PostRequest) req).getWriteTimeOutInMs().orElse(0) == 30_000)); } @Test @@ -122,7 +123,7 @@ public class ReportPublisherTest { HttpException ex = new HttpException("url", 404, "{\"errors\":[{\"msg\":\"Organization with key 'MyOrg' does not exist\"}]}"); WsResponse response = mock(WsResponse.class); when(response.failIfNotSuccessful()).thenThrow(ex); - when(wsClient.call(any(WsRequest.class))).thenThrow(new IllegalStateException("timeout")); + when(wsClient.call(any(PostRequest.class))).thenThrow(new IllegalStateException("timeout")); assertThatThrownBy(() -> underTest.upload(reportTempFolder.newFile())) .isInstanceOf(IllegalStateException.class) @@ -134,7 +135,7 @@ public class ReportPublisherTest { HttpException ex = new HttpException("url", 404, "{\"errors\":[{\"msg\":\"Organization with key 'MyOrg' does not exist\"}]}"); WsResponse response = mock(WsResponse.class); when(response.failIfNotSuccessful()).thenThrow(ex); - when(wsClient.call(any(WsRequest.class))).thenReturn(response); + when(wsClient.call(any(PostRequest.class))).thenReturn(response); assertThatThrownBy(() -> underTest.upload(reportTempFolder.newFile())) .isInstanceOf(MessageException.class) @@ -225,7 +226,7 @@ public class ReportPublisherTest { MockWsResponse submitMockResponse = new MockWsResponse(); submitMockResponse.setContent(Ce.SubmitResponse.newBuilder().setTaskId("task-1234").build().toByteArray()); - when(wsClient.call(any())).thenReturn(submitMockResponse); + when(wsClient.call(any(PostRequest.class))).thenReturn(submitMockResponse); underTest.start(); underTest.execute(); @@ -276,10 +277,10 @@ public class ReportPublisherTest { when(response.failIfNotSuccessful()).thenReturn(response); when(response.contentStream()).thenReturn(in); - when(wsClient.call(any(WsRequest.class))).thenReturn(response); + when(wsClient.call(any(PostRequest.class))).thenReturn(response); underTest.upload(reportTempFolder.newFile()); - ArgumentCaptor<WsRequest> capture = ArgumentCaptor.forClass(WsRequest.class); + ArgumentCaptor<PostRequest> capture = ArgumentCaptor.forClass(PostRequest.class); verify(wsClient).call(capture.capture()); WsRequest wsRequest = capture.getValue(); @@ -303,10 +304,10 @@ public class ReportPublisherTest { when(response.failIfNotSuccessful()).thenReturn(response); when(response.contentStream()).thenReturn(in); - when(wsClient.call(any(WsRequest.class))).thenReturn(response); + when(wsClient.call(any(PostRequest.class))).thenReturn(response); underTest.upload(reportTempFolder.newFile()); - ArgumentCaptor<WsRequest> capture = ArgumentCaptor.forClass(WsRequest.class); + ArgumentCaptor<PostRequest> capture = ArgumentCaptor.forClass(PostRequest.class); verify(wsClient).call(capture.capture()); WsRequest wsRequest = capture.getValue(); @@ -334,10 +335,10 @@ public class ReportPublisherTest { when(response.failIfNotSuccessful()).thenReturn(response); when(response.contentStream()).thenReturn(in); - when(wsClient.call(any(WsRequest.class))).thenReturn(response); + when(wsClient.call(any(PostRequest.class))).thenReturn(response); underTest.upload(reportTempFolder.newFile()); - ArgumentCaptor<WsRequest> capture = ArgumentCaptor.forClass(WsRequest.class); + ArgumentCaptor<PostRequest> capture = ArgumentCaptor.forClass(PostRequest.class); verify(wsClient).call(capture.capture()); WsRequest wsRequest = capture.getValue(); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java index d6c7500f6fd..ca09e2bd359 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java @@ -32,8 +32,8 @@ import org.sonar.api.utils.MessageException; import org.sonar.scanner.WsTestUtil; import org.sonar.scanner.bootstrap.DefaultScannerWsClient; import org.sonarqube.ws.Batch.WsProjectResponse; +import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.HttpException; -import org.sonarqube.ws.client.WsRequest; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -58,14 +58,14 @@ public class DefaultProjectRepositoriesLoaderTest { @Test public void continueOnHttp404Exception() { - when(wsClient.call(any(WsRequest.class))).thenThrow(new HttpException("/batch/project.protobuf?key=foo%3F", HttpURLConnection.HTTP_NOT_FOUND, "")); + when(wsClient.call(any(GetRequest.class))).thenThrow(new HttpException("/batch/project.protobuf?key=foo%3F", HttpURLConnection.HTTP_NOT_FOUND, "")); ProjectRepositories proj = loader.load(PROJECT_KEY, null); assertThat(proj.exists()).isFalse(); } @Test(expected = IllegalStateException.class) public void failOnNonHttp404Exception() { - when(wsClient.call(any(WsRequest.class))).thenThrow(IllegalStateException.class); + when(wsClient.call(any(GetRequest.class))).thenThrow(IllegalStateException.class); ProjectRepositories proj = loader.load(PROJECT_KEY, null); assertThat(proj.exists()).isFalse(); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/settings/DefaultGlobalSettingsLoaderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/settings/DefaultGlobalSettingsLoaderTest.java index 069d7a85fb2..daae92c11c2 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/settings/DefaultGlobalSettingsLoaderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/settings/DefaultGlobalSettingsLoaderTest.java @@ -58,7 +58,7 @@ public class DefaultGlobalSettingsLoaderTest { .writeTo(out); out.close(); when(response.contentStream()).thenReturn(in); - when(wsClient.call(any())).thenReturn(response); + when(wsClient.call(any(GetRequest.class))).thenReturn(response); Map<String, String> result = underTest.loadGlobalSettings(); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/settings/DefaultProjectSettingsLoaderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/settings/DefaultProjectSettingsLoaderTest.java index 898596de087..9699b433258 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/settings/DefaultProjectSettingsLoaderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/settings/DefaultProjectSettingsLoaderTest.java @@ -60,7 +60,7 @@ public class DefaultProjectSettingsLoaderTest { .writeTo(out); out.close(); when(response.contentStream()).thenReturn(in); - when(wsClient.call(any())).thenReturn(response); + when(wsClient.call(any(GetRequest.class))).thenReturn(response); when(properties.getProjectKey()).thenReturn("project_key"); Map<String, String> result = underTest.loadProjectSettings(); |