Search in sources :

Example 6 with SearchWsResponse

use of org.sonarqube.ws.Qualityprofiles.SearchWsResponse in project sonarqube by SonarSource.

the class SearchActionTest method statistics_on_active_rules.

@Test
public void statistics_on_active_rules() {
    QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO1.getKey()));
    RuleDefinitionDto rule = db.rules().insertRule(r -> r.setLanguage(XOO1.getKey())).getDefinition();
    RuleDefinitionDto deprecatedRule1 = db.rules().insertRule(r -> r.setStatus(DEPRECATED)).getDefinition();
    RuleDefinitionDto deprecatedRule2 = db.rules().insertRule(r -> r.setStatus(DEPRECATED)).getDefinition();
    RuleDefinitionDto inactiveRule = db.rules().insertRule(r -> r.setLanguage(XOO1.getKey())).getDefinition();
    db.qualityProfiles().activateRule(profile, rule);
    db.qualityProfiles().activateRule(profile, deprecatedRule1);
    db.qualityProfiles().activateRule(profile, deprecatedRule2);
    SearchWsResponse result = call(ws.newRequest());
    assertThat(result.getProfilesList()).extracting(QualityProfile::getActiveRuleCount, QualityProfile::getActiveDeprecatedRuleCount).containsExactlyInAnyOrder(tuple(3L, 2L));
}
Also used : ComponentFinder(org.sonar.server.component.ComponentFinder) SearchWsResponse(org.sonarqube.ws.Qualityprofiles.SearchWsResponse) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) IntStream.range(java.util.stream.IntStream.range) GlobalPermission(org.sonar.db.permission.GlobalPermission) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Languages(org.sonar.api.resources.Languages) QualityProfileDbTester(org.sonar.db.qualityprofile.QualityProfileDbTester) PARAM_PROJECT(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT) WebService(org.sonar.api.server.ws.WebService) DateUtils(org.sonar.api.utils.DateUtils) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) PARAM_LANGUAGE(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE) PARAM_DEFAULTS(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_DEFAULTS) MediaTypes(org.sonarqube.ws.MediaTypes) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Language(org.sonar.api.resources.Language) PARAM_QUALITY_PROFILE(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE) PARAM_PROJECT_KEY(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT_KEY) GroupDto(org.sonar.db.user.GroupDto) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) QualityProfile(org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile) System2(org.sonar.api.utils.System2) Assertions.tuple(org.assertj.core.api.Assertions.tuple) QualityProfileTesting.newQualityProfileDto(org.sonar.db.qualityprofile.QualityProfileTesting.newQualityProfileDto) LanguageTesting.newLanguage(org.sonar.server.language.LanguageTesting.newLanguage) TestRequest(org.sonar.server.ws.TestRequest) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) Test(org.junit.Test) WsActionTester(org.sonar.server.ws.WsActionTester) DEPRECATED(org.sonar.api.rule.RuleStatus.DEPRECATED) NotFoundException(org.sonar.server.exceptions.NotFoundException) DbClient(org.sonar.db.DbClient) List(java.util.List) Rule(org.junit.Rule) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) ProjectDto(org.sonar.db.project.ProjectDto) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) Change(org.sonar.api.server.ws.Change) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) SearchWsResponse(org.sonarqube.ws.Qualityprofiles.SearchWsResponse) Test(org.junit.Test)

Example 7 with SearchWsResponse

use of org.sonarqube.ws.Qualityprofiles.SearchWsResponse 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 8 with SearchWsResponse

use of org.sonarqube.ws.Qualityprofiles.SearchWsResponse in project sonarqube by SonarSource.

the class SearchActionTest method search_map_dates.

@Test
public void search_map_dates() {
    long time = DateUtils.parseDateTime("2016-12-22T19:10:03+0100").getTime();
    qualityProfileDb.insertQualityProfiles(newQualityProfileDto().setLanguage(xoo1.getKey()).setRulesUpdatedAt("2016-12-21T19:10:03+0100").setLastUsed(time).setUserUpdatedAt(time));
    SearchWsResponse result = call(ws.newRequest());
    assertThat(result.getProfilesCount()).isEqualTo(1);
    assertThat(result.getProfiles(0).getRulesUpdatedAt()).isEqualTo("2016-12-21T19:10:03+0100");
    assertThat(parseDateTime(result.getProfiles(0).getLastUsed()).getTime()).isEqualTo(time);
    assertThat(parseDateTime(result.getProfiles(0).getUserUpdatedAt()).getTime()).isEqualTo(time);
}
Also used : SearchWsResponse(org.sonarqube.ws.QualityProfiles.SearchWsResponse) Test(org.junit.Test)

Example 9 with SearchWsResponse

use of org.sonarqube.ws.Qualityprofiles.SearchWsResponse in project sonarqube by SonarSource.

the class SearchActionTest method search_for_project_qp.

@Test
public void search_for_project_qp() {
    long time = DateUtils.parseDateTime("2016-12-22T19:10:03+0100").getTime();
    OrganizationDto org = db.organizations().insert();
    QualityProfileDto qualityProfileOnXoo1 = QualityProfileDto.createFor("sonar-way-xoo1-12345").setOrganizationUuid(org.getUuid()).setLanguage(xoo1.getKey()).setRulesUpdatedAt("2016-12-21T19:10:03+0100").setLastUsed(time).setName("Sonar way");
    QualityProfileDto qualityProfileOnXoo2 = QualityProfileDto.createFor("sonar-way-xoo2-12345").setOrganizationUuid(org.getUuid()).setLanguage(xoo2.getKey()).setRulesUpdatedAt("2016-12-21T19:10:03+0100").setLastUsed(time).setName("Sonar way");
    QualityProfileDto anotherQualityProfileOnXoo1 = QualityProfileDto.createFor("sonar-way-xoo1-45678").setOrganizationUuid(org.getUuid()).setLanguage(xoo1.getKey()).setRulesUpdatedAt("2016-12-21T19:10:03+0100").setLastUsed(time).setName("Another way");
    ComponentDto project = newProjectDto(org, "project-uuid");
    qualityProfileDb.insertQualityProfiles(qualityProfileOnXoo1, qualityProfileOnXoo2, anotherQualityProfileOnXoo1);
    qualityProfileDb.insertProjectWithQualityProfileAssociations(project, qualityProfileOnXoo1, qualityProfileOnXoo2);
    SearchWsResponse result = call(ws.newRequest().setParam(PARAM_PROJECT_KEY, project.key()));
    assertThat(result.getProfilesList()).hasSize(2).extracting(QualityProfile::getKey).contains("sonar-way-xoo1-12345", "sonar-way-xoo2-12345").doesNotContain("sonar-way-xoo1-45678");
    assertThat(result.getProfilesList()).extracting(QualityProfile::getRulesUpdatedAt, QualityProfile::getLastUsed).contains(tuple("2016-12-21T19:10:03+0100", formatDateTime(time)));
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) QualityProfile(org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile) SearchWsResponse(org.sonarqube.ws.QualityProfiles.SearchWsResponse) OrganizationDto(org.sonar.db.organization.OrganizationDto) QualityProfileTesting.newQualityProfileDto(org.sonar.db.qualityprofile.QualityProfileTesting.newQualityProfileDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 10 with SearchWsResponse

use of org.sonarqube.ws.Qualityprofiles.SearchWsResponse in project sonarlint-core by SonarSource.

the class ModuleQualityProfilesDownloader method fetchModuleQualityProfiles.

public List<QualityProfile> fetchModuleQualityProfiles(String moduleKey, String serverVersion) {
    SearchWsResponse qpResponse;
    String param;
    if (Version.create(serverVersion).compareToIgnoreQualifier(Version.create("6.5")) >= 0) {
        param = "project";
    } else {
        param = "projectKey";
    }
    String baseUrl = "/api/qualityprofiles/search.protobuf?" + param + "=" + StringUtils.urlEncode(moduleKey);
    String organizationKey = wsClient.getOrganizationKey();
    if (organizationKey != null) {
        baseUrl += "&organization=" + StringUtils.urlEncode(organizationKey);
    }
    try (InputStream contentStream = wsClient.get(baseUrl).contentStream()) {
        qpResponse = QualityProfiles.SearchWsResponse.parseFrom(contentStream);
    } catch (NotFoundException e) {
        throw new ProjectNotFoundException(moduleKey, organizationKey);
    } catch (IOException e) {
        throw new IllegalStateException("Failed to load module quality profiles", e);
    }
    return qpResponse.getProfilesList();
}
Also used : ProjectNotFoundException(org.sonarsource.sonarlint.core.client.api.exceptions.ProjectNotFoundException) InputStream(java.io.InputStream) NotFoundException(org.sonarsource.sonarlint.core.container.connected.exceptions.NotFoundException) ProjectNotFoundException(org.sonarsource.sonarlint.core.client.api.exceptions.ProjectNotFoundException) SearchWsResponse(org.sonarqube.ws.QualityProfiles.SearchWsResponse) IOException(java.io.IOException)

Aggregations

Test (org.junit.Test)21 SearchWsResponse (org.sonarqube.ws.Qualityprofiles.SearchWsResponse)21 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)16 QualityProfile (org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile)14 ProjectDto (org.sonar.db.project.ProjectDto)9 Languages (org.sonar.api.resources.Languages)6 ComponentFinder (org.sonar.server.component.ComponentFinder)6 WsActionTester (org.sonar.server.ws.WsActionTester)6 SearchWsResponse (org.sonarqube.ws.QualityProfiles.SearchWsResponse)6 List (java.util.List)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Assertions.tuple (org.assertj.core.api.Assertions.tuple)5 Rule (org.junit.Rule)5 QualityProfileTesting.newQualityProfileDto (org.sonar.db.qualityprofile.QualityProfileTesting.newQualityProfileDto)5 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 IntStream.range (java.util.stream.IntStream.range)4 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)4 Language (org.sonar.api.resources.Language)4 DEPRECATED (org.sonar.api.rule.RuleStatus.DEPRECATED)4