Search in sources :

Example 31 with WsResponse

use of org.sonarqube.ws.client.WsResponse in project sonarqube by SonarSource.

the class ReportPublisher method upload.

/**
   * Uploads the report file to server and returns the generated task id
   */
@VisibleForTesting
String upload(File report) {
    LOG.debug("Upload report");
    long startTime = System.currentTimeMillis();
    ProjectDefinition projectDefinition = projectReactor.getRoot();
    PostRequest.Part filePart = new PostRequest.Part(MediaTypes.ZIP, report);
    PostRequest post = new PostRequest("api/ce/submit").setMediaType(MediaTypes.PROTOBUF).setParam("organization", settings.getString(CoreProperties.PROJECT_ORGANIZATION_PROPERTY)).setParam("projectKey", projectDefinition.getKey()).setParam("projectName", projectDefinition.getOriginalName()).setParam("projectBranch", projectDefinition.getBranch()).setPart("report", filePart);
    WsResponse response;
    try {
        response = wsClient.call(post).failIfNotSuccessful();
    } catch (HttpException e) {
        throw MessageException.of(String.format("Failed to upload report - %d: %s", e.code(), ScannerWsClient.tryParseAsJsonError(e.content())));
    }
    try (InputStream protobuf = response.contentStream()) {
        return WsCe.SubmitResponse.parser().parseFrom(protobuf).getTaskId();
    } catch (Exception e) {
        throw Throwables.propagate(e);
    } finally {
        long stopTime = System.currentTimeMillis();
        LOG.info("Analysis report uploaded in " + (stopTime - startTime) + "ms");
    }
}
Also used : PostRequest(org.sonarqube.ws.client.PostRequest) InputStream(java.io.InputStream) WsResponse(org.sonarqube.ws.client.WsResponse) HttpException(org.sonarqube.ws.client.HttpException) MessageException(org.sonar.api.utils.MessageException) HttpException(org.sonarqube.ws.client.HttpException) IOException(java.io.IOException) ProjectDefinition(org.sonar.api.batch.bootstrap.ProjectDefinition) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 32 with WsResponse

use of org.sonarqube.ws.client.WsResponse in project sonarqube by SonarSource.

the class ScannerWsClientTest method fail_if_requires_permission.

@Test
public void fail_if_requires_permission() throws Exception {
    expectedException.expect(MessageException.class);
    expectedException.expectMessage("missing scan permission, missing another permission");
    WsRequest request = newRequest();
    WsResponse response = newResponse().setCode(403).setContent("{\"errors\":[{\"msg\":\"missing scan permission\"}, {\"msg\":\"missing another permission\"}]}");
    when(wsClient.wsConnector().call(request)).thenReturn(response);
    new ScannerWsClient(wsClient, true, new GlobalMode(new GlobalProperties(Collections.emptyMap()))).call(request);
}
Also used : WsRequest(org.sonarqube.ws.client.WsRequest) WsResponse(org.sonarqube.ws.client.WsResponse) MockWsResponse(org.sonarqube.ws.client.MockWsResponse) Test(org.junit.Test)

Example 33 with WsResponse

use of org.sonarqube.ws.client.WsResponse in project sonarqube by SonarSource.

the class ScannerWsClientTest method log_and_profile_request_if_debug_level.

@Test
public void log_and_profile_request_if_debug_level() throws Exception {
    WsRequest request = newRequest();
    WsResponse response = newResponse().setRequestUrl("https://local/api/issues/search");
    when(wsClient.wsConnector().call(request)).thenReturn(response);
    logTester.setLevel(LoggerLevel.DEBUG);
    ScannerWsClient underTest = new ScannerWsClient(wsClient, false, new GlobalMode(new GlobalProperties(Collections.emptyMap())));
    WsResponse result = underTest.call(request);
    // do not fail the execution -> interceptor returns the response
    assertThat(result).isSameAs(response);
    // check logs
    List<String> debugLogs = logTester.logs(LoggerLevel.DEBUG);
    assertThat(debugLogs).hasSize(1);
    assertThat(debugLogs.get(0)).contains("GET 200 https://local/api/issues/search | time=");
}
Also used : WsRequest(org.sonarqube.ws.client.WsRequest) WsResponse(org.sonarqube.ws.client.WsResponse) MockWsResponse(org.sonarqube.ws.client.MockWsResponse) Test(org.junit.Test)

Example 34 with WsResponse

use of org.sonarqube.ws.client.WsResponse in project sonarqube by SonarSource.

the class ScannerWsClientTest method fail_if_credentials_are_not_valid.

@Test
public void fail_if_credentials_are_not_valid() throws Exception {
    expectedException.expect(MessageException.class);
    expectedException.expectMessage("Not authorized. Please check the properties sonar.login and sonar.password.");
    WsRequest request = newRequest();
    WsResponse response = newResponse().setCode(401);
    when(wsClient.wsConnector().call(request)).thenReturn(response);
    new ScannerWsClient(wsClient, /* credentials are configured */
    true, new GlobalMode(new GlobalProperties(Collections.emptyMap()))).call(request);
}
Also used : WsRequest(org.sonarqube.ws.client.WsRequest) WsResponse(org.sonarqube.ws.client.WsResponse) MockWsResponse(org.sonarqube.ws.client.MockWsResponse) Test(org.junit.Test)

Example 35 with WsResponse

use of org.sonarqube.ws.client.WsResponse in project sonarqube by SonarSource.

the class WsTestUtil method mockStream.

public static void mockStream(ScannerWsClient mock, String path, InputStream is) {
    WsResponse response = mock(WsResponse.class);
    when(response.contentStream()).thenReturn(is);
    when(mock.call(argThat(new RequestMatcher(path)))).thenReturn(response);
}
Also used : WsResponse(org.sonarqube.ws.client.WsResponse)

Aggregations

WsResponse (org.sonarqube.ws.client.WsResponse)38 Test (org.junit.Test)23 GetRequest (org.sonarqube.ws.client.GetRequest)21 WsRequest (org.sonarqube.ws.client.WsRequest)9 MockWsResponse (org.sonarqube.ws.client.MockWsResponse)5 WsClient (org.sonarqube.ws.client.WsClient)3 TempFolder (org.sonar.api.utils.TempFolder)2 HttpException (org.sonarqube.ws.client.HttpException)2 PostRequest (org.sonarqube.ws.client.PostRequest)2 ItUtils.newAdminWsClient (util.ItUtils.newAdminWsClient)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 SonarScanner (com.sonar.orchestrator.build.SonarScanner)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 CheckForNull (javax.annotation.CheckForNull)1 ProjectDefinition (org.sonar.api.batch.bootstrap.ProjectDefinition)1 MessageException (org.sonar.api.utils.MessageException)1