Search in sources :

Example 26 with GetRequest

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

the class DefaultProjectSettingsLoaderTest method loadProjectSettings.

@Test
public void loadProjectSettings() 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);
    when(properties.getProjectKey()).thenReturn("project_key");
    Map<String, String> result = underTest.loadProjectSettings();
    ArgumentCaptor<GetRequest> argumentCaptor = ArgumentCaptor.forClass(GetRequest.class);
    verify(wsClient, times(1)).call(argumentCaptor.capture());
    assertThat(argumentCaptor.getValue().getPath()).isEqualTo("api/settings/values.protobuf?component=project_key");
    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 27 with GetRequest

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

the class DefaultQualityProfileLoader method call.

private Map<String, QualityProfile> call(String url) throws IOException {
    GetRequest getRequest = new GetRequest(url);
    try (InputStream is = wsClient.call(getRequest).contentStream()) {
        SearchWsResponse profiles = SearchWsResponse.parseFrom(is);
        List<QualityProfile> profilesList = profiles.getProfilesList();
        return profilesList.stream().collect(toMap(QualityProfile::getLanguage, identity(), throwingMerger(), LinkedHashMap::new));
    }
}
Also used : InputStream(java.io.InputStream) GetRequest(org.sonarqube.ws.client.GetRequest) QualityProfile(org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile) SearchWsResponse(org.sonarqube.ws.Qualityprofiles.SearchWsResponse)

Example 28 with GetRequest

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

the class DefaultRulesLoader method load.

@Override
public List<Rule> load() {
    GetRequest getRequest = new GetRequest(RULES_SEARCH_URL);
    ListResponse list = loadFromStream(wsClient.call(getRequest).contentStream());
    return list.getRulesList();
}
Also used : ListResponse(org.sonarqube.ws.Rules.ListResponse) GetRequest(org.sonarqube.ws.client.GetRequest)

Example 29 with GetRequest

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

the class DefaultActiveRulesLoader method load.

@Override
public List<LoadedActiveRule> load(String qualityProfileKey) {
    List<LoadedActiveRule> ruleList = new LinkedList<>();
    int page = 1;
    int pageSize = 500;
    long loaded = 0;
    while (true) {
        GetRequest getRequest = new GetRequest(getUrl(qualityProfileKey, page, pageSize));
        SearchResponse response = loadFromStream(wsClient.call(getRequest).contentStream());
        List<LoadedActiveRule> pageRules = readPage(response);
        ruleList.addAll(pageRules);
        loaded += response.getPs();
        if (response.getTotal() <= loaded) {
            break;
        }
        page++;
    }
    return ruleList;
}
Also used : GetRequest(org.sonarqube.ws.client.GetRequest) LoadedActiveRule(org.sonar.api.batch.rule.LoadedActiveRule) LinkedList(java.util.LinkedList) SearchResponse(org.sonarqube.ws.Rules.SearchResponse)

Example 30 with GetRequest

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

the class AbstractSettingsLoader method load.

Map<String, String> load(@Nullable String componentKey) {
    String url = "api/settings/values.protobuf";
    Profiler profiler = Profiler.create(LOG);
    if (componentKey != null) {
        url += "?component=" + ScannerUtils.encodeForUrl(componentKey);
        profiler.startInfo(String.format("Load project settings for component key: '%s'", componentKey));
    } else {
        profiler.startInfo("Load global settings");
    }
    try (InputStream is = wsClient.call(new GetRequest(url)).contentStream()) {
        Settings.ValuesWsResponse values = Settings.ValuesWsResponse.parseFrom(is);
        profiler.stopInfo();
        return toMap(values.getSettingsList());
    } catch (HttpException e) {
        if (e.code() == HttpURLConnection.HTTP_NOT_FOUND) {
            return Collections.emptyMap();
        }
        throw e;
    } catch (IOException e) {
        throw new IllegalStateException("Unable to load settings", e);
    }
}
Also used : Profiler(org.sonar.api.utils.log.Profiler) InputStream(java.io.InputStream) GetRequest(org.sonarqube.ws.client.GetRequest) HttpException(org.sonarqube.ws.client.HttpException) IOException(java.io.IOException) Settings(org.sonarqube.ws.Settings)

Aggregations

GetRequest (org.sonarqube.ws.client.GetRequest)59 Test (org.junit.Test)34 WsResponse (org.sonarqube.ws.client.WsResponse)27 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 Profiler (org.sonar.api.utils.log.Profiler)5 Reader (java.io.Reader)4 HttpException (org.sonarqube.ws.client.HttpException)4 WsClient (org.sonarqube.ws.client.WsClient)4 ItUtils.newAdminWsClient (util.ItUtils.newAdminWsClient)3 File (java.io.File)2 PipedInputStream (java.io.PipedInputStream)2 PipedOutputStream (java.io.PipedOutputStream)2 ShowWsResponse (org.sonarqube.ws.ServerId.ShowWsResponse)2 ItUtils.newWsClient (util.ItUtils.newWsClient)2 Gson (com.google.gson.Gson)1 LinkedList (java.util.LinkedList)1 CheckForNull (javax.annotation.CheckForNull)1 LoadedActiveRule (org.sonar.api.batch.rule.LoadedActiveRule)1 Metric (org.sonar.api.measures.Metric)1