use of org.sonarsource.sonarlint.core.serverapi.ServerApiHelper in project sonarlint-core by SonarSource.
the class ConnectedSonarLintEngineImpl method getActiveRuleDetails.
@Override
public CompletableFuture<ConnectedRuleDetails> getActiveRuleDetails(EndpointParams endpoint, HttpClient client, String ruleKey, @Nullable String projectKey) {
var allRulesDefinitionsByKey = analysisContext.get().allRulesDefinitionsByKey;
var ruleDefFromPlugin = allRulesDefinitionsByKey.get(ruleKey);
if (ruleDefFromPlugin != null && (globalConfig.getExtraPluginsPathsByKey().containsKey(ruleDefFromPlugin.getLanguage().getPluginKey()) || projectKey == null)) {
// if no project key, or for rules from extra plugins there will be no rules metadata in the storage
return CompletableFuture.completedFuture(new DefaultRuleDetails(ruleKey, ruleDefFromPlugin.getName(), ruleDefFromPlugin.getHtmlDescription(), ruleDefFromPlugin.getSeverity(), ruleDefFromPlugin.getType(), ruleDefFromPlugin.getLanguage(), ""));
}
if (projectKey != null) {
var analyzerConfiguration = projectStorage.getAnalyzerConfiguration(projectKey);
var storageActiveRule = analyzerConfiguration.getRuleSetByLanguageKey().values().stream().flatMap(s -> s.getRules().stream()).filter(r -> r.getRuleKey().equals(ruleKey)).findFirst();
if (storageActiveRule.isPresent()) {
var activeRuleFromStorage = storageActiveRule.get();
var serverSeverity = activeRuleFromStorage.getSeverity();
if (StringUtils.isNotBlank(activeRuleFromStorage.getTemplateKey())) {
var templateRuleDefFromPlugin = Optional.ofNullable(allRulesDefinitionsByKey.get(activeRuleFromStorage.getTemplateKey())).orElseThrow(() -> new IllegalStateException("Unable to find rule definition for rule template " + activeRuleFromStorage.getTemplateKey()));
return new ServerApi(new ServerApiHelper(endpoint, client)).rules().getRule(ruleKey).thenApply(serverRule -> new DefaultRuleDetails(ruleKey, serverRule.getName(), serverRule.getHtmlDesc(), serverSeverity, templateRuleDefFromPlugin.getType(), templateRuleDefFromPlugin.getLanguage(), serverRule.getHtmlNote()));
} else {
if (ruleDefFromPlugin == null) {
throw new IllegalStateException("Unable to find rule definition for rule " + ruleKey);
}
return new ServerApi(new ServerApiHelper(endpoint, client)).rules().getRule(ruleKey).thenApply(serverRule -> new DefaultRuleDetails(ruleKey, ruleDefFromPlugin.getName(), ruleDefFromPlugin.getHtmlDescription(), serverSeverity != null ? serverSeverity : ruleDefFromPlugin.getSeverity(), ruleDefFromPlugin.getType(), ruleDefFromPlugin.getLanguage(), serverRule.getHtmlNote()));
}
}
}
throw new IllegalStateException("Unable to find rule details for '" + ruleKey + "'");
}
use of org.sonarsource.sonarlint.core.serverapi.ServerApiHelper in project sonarlint-core by SonarSource.
the class OrganizationApiTests method testListUserOrganizationWithMoreThan20Pages.
@Test
void testListUserOrganizationWithMoreThan20Pages() {
var underTest = new OrganizationApi(new ServerApiHelper(mockServer.endpointParams("myOrg"), MockWebServerExtension.httpClient()));
mockServer.addStringResponse("/api/system/status", "{\"id\": \"20160308094653\",\"version\": \"7.9\",\"status\": \"UP\"}");
for (var i = 0; i < 21; i++) {
mockOrganizationsPage(i + 1, 10500);
}
var orgs = underTest.listUserOrganizations(progressMonitor);
assertThat(orgs).hasSize(10500);
}
use of org.sonarsource.sonarlint.core.serverapi.ServerApiHelper in project sonarlint-core by SonarSource.
the class OrganizationApiTests method should_not_get_organization_if_server_is_too_old.
@Test
void should_not_get_organization_if_server_is_too_old() {
mockServer.addStringResponse("/api/system/status", "{" + "\"status\": \"UP\"," + "\"version\": \"7.8.0\"" + "}");
var underTest = new OrganizationApi(new ServerApiHelper(mockServer.endpointParams(), MockWebServerExtension.httpClient()));
var throwable = catchThrowable(() -> underTest.getOrganization("org:key", progressMonitor));
assertThat(throwable).isInstanceOf(UnsupportedServerException.class).hasMessageContaining("SonarQube server has version 7.8.0. Version should be greater or equal to");
}
use of org.sonarsource.sonarlint.core.serverapi.ServerApiHelper in project sonarlint-core by SonarSource.
the class ConnectionValidatorTests method testConnection_ok.
@Test
void testConnection_ok() throws ExecutionException, InterruptedException {
var underTest = new ConnectionValidator(new ServerApiHelper(mockServer.endpointParams(), MockWebServerExtension.httpClient()));
mockServer.addStringResponse("/api/system/status", "{\"id\": \"20160308094653\",\"version\": \"7.9\",\"status\": \"UP\"}");
mockServer.addStringResponse("/api/authentication/validate?format=json", "{\"valid\": true}");
var futureValidation = underTest.validateConnection();
var validation = futureValidation.get();
assertThat(validation.success()).isTrue();
assertThat(mockServer.takeRequest().getPath()).isEqualTo("/api/system/status");
assertThat(mockServer.takeRequest().getPath()).isEqualTo("/api/authentication/validate?format=json");
}
use of org.sonarsource.sonarlint.core.serverapi.ServerApiHelper in project sonarlint-core by SonarSource.
the class ConnectionValidatorTests method testClientError.
@Test
void testClientError() throws ExecutionException, InterruptedException {
var underTest = new ConnectionValidator(new ServerApiHelper(mockServer.endpointParams(), MockWebServerExtension.httpClient()));
var mockResponse = new MockResponse();
mockResponse.setResponseCode(400);
mockServer.addResponse("/api/system/status", mockResponse);
var futureValidation = underTest.validateConnection();
var validation = futureValidation.get();
assertThat(validation.success()).isFalse();
assertThat(validation.message()).isEqualTo("Error 400 on " + mockServer.endpointParams().getBaseUrl() + "api/system/status");
}
Aggregations