Search in sources :

Example 51 with WsResponse

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

the class DefaultGlobalSettingsLoaderTest method loadGlobalSettings.

@Test
public void loadGlobalSettings() throws IOException {
    WsResponse response = mock(WsResponse.class);
    PipedOutputStream out = new PipedOutputStream();
    PipedInputStream in = new PipedInputStream(out);
    Settings.ValuesWsResponse.newBuilder().addSettings(Settings.Setting.newBuilder().setKey("abc").setValue("def").build()).addSettings(Settings.Setting.newBuilder().setKey("123").setValue("456").build()).build().writeTo(out);
    out.close();
    when(response.contentStream()).thenReturn(in);
    when(wsClient.call(any())).thenReturn(response);
    Map<String, String> result = underTest.loadGlobalSettings();
    ArgumentCaptor<GetRequest> argumentCaptor = ArgumentCaptor.forClass(GetRequest.class);
    verify(wsClient, times(1)).call(argumentCaptor.capture());
    assertThat(argumentCaptor.getValue().getPath()).isEqualTo("api/settings/values.protobuf");
    assertThat(result).isNotNull().hasSize(2).containsEntry("abc", "def").containsEntry("123", "456");
}
Also used : GetRequest(org.sonarqube.ws.client.GetRequest) WsResponse(org.sonarqube.ws.client.WsResponse) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) Test(org.junit.Test)

Example 52 with WsResponse

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

the class ReportPublisherTest method send_pull_request_characteristic.

@Test
public void send_pull_request_characteristic() throws Exception {
    String branchName = "feature";
    String pullRequestId = "pr-123";
    when(branchConfiguration.branchName()).thenReturn(branchName);
    when(branchConfiguration.branchType()).thenReturn(PULL_REQUEST);
    when(branchConfiguration.pullRequestKey()).thenReturn(pullRequestId);
    WsResponse response = mock(WsResponse.class);
    PipedOutputStream out = new PipedOutputStream();
    PipedInputStream in = new PipedInputStream(out);
    Ce.SubmitResponse.newBuilder().build().writeTo(out);
    out.close();
    when(response.failIfNotSuccessful()).thenReturn(response);
    when(response.contentStream()).thenReturn(in);
    when(wsClient.call(any(WsRequest.class))).thenReturn(response);
    underTest.upload(reportTempFolder.newFile());
    ArgumentCaptor<WsRequest> capture = ArgumentCaptor.forClass(WsRequest.class);
    verify(wsClient).call(capture.capture());
    WsRequest wsRequest = capture.getValue();
    assertThat(wsRequest.getParameters().getKeys()).hasSize(2);
    assertThat(wsRequest.getParameters().getValues("projectKey")).containsExactly("org.sonarsource.sonarqube:sonarqube");
    assertThat(wsRequest.getParameters().getValues("characteristic")).containsExactlyInAnyOrder("pullRequest=" + pullRequestId);
}
Also used : WsRequest(org.sonarqube.ws.client.WsRequest) MockWsResponse(org.sonarqube.ws.client.MockWsResponse) WsResponse(org.sonarqube.ws.client.WsResponse) PipedOutputStream(java.io.PipedOutputStream) FileUtils.readFileToString(org.apache.commons.io.FileUtils.readFileToString) PipedInputStream(java.io.PipedInputStream) Test(org.junit.Test)

Example 53 with WsResponse

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

the class DefaultScannerWsClient method call.

/**
 * If an exception is not thrown, the response needs to be closed by either calling close() directly, or closing the
 * body content's stream/reader.
 *
 * @throws IllegalStateException if the request could not be executed due to a connectivity problem or timeout. Because networks can
 *                               fail during an exchange, it is possible that the remote server accepted the request before the failure
 * @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) {
    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);
    profiler.stopDebug(format("%s %d %s", request.getMethod(), response.code(), response.requestUrl()));
    failIfUnauthorized(response);
    return response;
}
Also used : Profiler(org.sonar.api.utils.log.Profiler) WsResponse(org.sonarqube.ws.client.WsResponse)

Example 54 with WsResponse

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

the class WsTestUtil method mockStream.

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);
}
Also used : WsResponse(org.sonarqube.ws.client.WsResponse)

Example 55 with WsResponse

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

the class WsTestUtil method mockStream.

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);
}
Also used : WsRequest(org.sonarqube.ws.client.WsRequest) WsResponse(org.sonarqube.ws.client.WsResponse)

Aggregations

WsResponse (org.sonarqube.ws.client.WsResponse)58 Test (org.junit.Test)34 GetRequest (org.sonarqube.ws.client.GetRequest)27 WsRequest (org.sonarqube.ws.client.WsRequest)20 MockWsResponse (org.sonarqube.ws.client.MockWsResponse)15 MessageException (org.sonar.api.utils.MessageException)6 HttpException (org.sonarqube.ws.client.HttpException)6 PipedInputStream (java.io.PipedInputStream)5 PipedOutputStream (java.io.PipedOutputStream)5 PostRequest (org.sonarqube.ws.client.PostRequest)3 WsClient (org.sonarqube.ws.client.WsClient)3 File (java.io.File)2 FileUtils.readFileToString (org.apache.commons.io.FileUtils.readFileToString)2 Profiler (org.sonar.api.utils.log.Profiler)2 ItUtils.newAdminWsClient (util.ItUtils.newAdminWsClient)2 SonarScanner (com.sonar.orchestrator.build.SonarScanner)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 CheckForNull (javax.annotation.CheckForNull)1