Search in sources :

Example 1 with PARAM_KEY

use of org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY in project sonarqube by SonarSource.

the class ProjectsAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String profileKey = request.mandatoryParam(PARAM_KEY);
    try (DbSession session = dbClient.openSession(false)) {
        String selected = request.param(Param.SELECTED);
        String query = request.param(Param.TEXT_QUERY);
        int page = request.mandatoryParamAsInt(Param.PAGE);
        int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE);
        List<ProjectQprofileAssociationDto> projects = loadAllProjects(profileKey, session, selected, query).stream().sorted(comparing(ProjectQprofileAssociationDto::getProjectName).thenComparing(ProjectQprofileAssociationDto::getProjectUuid)).collect(MoreCollectors.toList());
        Collection<String> projectUuids = projects.stream().map(ProjectQprofileAssociationDto::getProjectUuid).collect(MoreCollectors.toSet());
        Set<String> authorizedProjectUuids = dbClient.authorizationDao().keepAuthorizedProjectUuids(session, projectUuids, userSession.getUuid(), UserRole.USER);
        Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(authorizedProjectUuids.size());
        List<ProjectQprofileAssociationDto> authorizedProjects = projects.stream().filter(input -> authorizedProjectUuids.contains(input.getProjectUuid())).skip(paging.offset()).limit(paging.pageSize()).collect(MoreCollectors.toList());
        writeProjects(response, authorizedProjects, paging);
    }
}
Also used : NewController(org.sonar.api.server.ws.WebService.NewController) ProjectQprofileAssociationDto(org.sonar.db.qualityprofile.ProjectQprofileAssociationDto) Collection(java.util.Collection) Set(java.util.Set) NewAction(org.sonar.api.server.ws.WebService.NewAction) DbSession(org.sonar.db.DbSession) NotFoundException(org.sonar.server.exceptions.NotFoundException) JsonWriter(org.sonar.api.utils.text.JsonWriter) Request(org.sonar.api.server.ws.Request) DbClient(org.sonar.db.DbClient) List(java.util.List) Param(org.sonar.api.server.ws.WebService.Param) UserRole(org.sonar.api.web.UserRole) UUID_EXAMPLE_01(org.sonar.core.util.Uuids.UUID_EXAMPLE_01) PARAM_KEY(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY) Response(org.sonar.api.server.ws.Response) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) Change(org.sonar.api.server.ws.Change) Paging.forPageIndex(org.sonar.api.utils.Paging.forPageIndex) Paging(org.sonar.api.utils.Paging) UserSession(org.sonar.server.user.UserSession) Comparator.comparing(java.util.Comparator.comparing) SelectionMode(org.sonar.api.server.ws.WebService.SelectionMode) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) DbSession(org.sonar.db.DbSession) Paging(org.sonar.api.utils.Paging) ProjectQprofileAssociationDto(org.sonar.db.qualityprofile.ProjectQprofileAssociationDto)

Example 2 with PARAM_KEY

use of org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY in project sonarqube by SonarSource.

the class ShowActionTest method statistics.

@Test
public void statistics() {
    QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO1.getKey()));
    // Active rules
    range(0, 10).mapToObj(i -> db.rules().insertRule(r -> r.setLanguage(XOO1.getKey())).getDefinition()).forEach(r -> db.qualityProfiles().activateRule(profile, r));
    // Deprecated rules
    range(0, 3).mapToObj(i -> db.rules().insertRule(r -> r.setLanguage(XOO1.getKey()).setStatus(DEPRECATED)).getDefinition()).forEach(r -> db.qualityProfiles().activateRule(profile, r));
    // Projects
    range(0, 7).mapToObj(i -> db.components().insertPrivateProjectDto()).forEach(project -> db.qualityProfiles().associateWithProject(project, profile));
    ShowResponse result = call(ws.newRequest().setParam(PARAM_KEY, profile.getKee()));
    assertThat(result.getProfile()).extracting(QualityProfile::getActiveRuleCount, QualityProfile::getActiveDeprecatedRuleCount, QualityProfile::getProjectCount).containsExactly(13L, 3L, 7L);
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) IntStream.range(java.util.stream.IntStream.range) EsTester(org.sonar.server.es.EsTester) ActiveRuleIndexer(org.sonar.server.qualityprofile.index.ActiveRuleIndexer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Languages(org.sonar.api.resources.Languages) WebService(org.sonar.api.server.ws.WebService) DateUtils(org.sonar.api.utils.DateUtils) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) PARAM_COMPARE_TO_SONAR_WAY(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_COMPARE_TO_SONAR_WAY) ShowResponse(org.sonarqube.ws.Qualityprofiles.ShowResponse) MediaTypes(org.sonarqube.ws.MediaTypes) RuleIndexer(org.sonar.server.rule.index.RuleIndexer) Language(org.sonar.api.resources.Language) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) QualityProfile(org.sonarqube.ws.Qualityprofiles.ShowResponse.QualityProfile) System2(org.sonar.api.utils.System2) 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) RuleIndex(org.sonar.server.rule.index.RuleIndex) WsActionTester(org.sonar.server.ws.WsActionTester) DEPRECATED(org.sonar.api.rule.RuleStatus.DEPRECATED) NotFoundException(org.sonar.server.exceptions.NotFoundException) CompareToSonarWay(org.sonarqube.ws.Qualityprofiles.ShowResponse.CompareToSonarWay) Rule(org.junit.Rule) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) PARAM_KEY(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) ShowResponse(org.sonarqube.ws.Qualityprofiles.ShowResponse) Test(org.junit.Test)

Example 3 with PARAM_KEY

use of org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY in project sonarqube by SonarSource.

the class ShowActionTest method compare_to_sonar_way_profile_when_same_active_rules.

@Test
public void compare_to_sonar_way_profile_when_same_active_rules() {
    QProfileDto sonarWayProfile = db.qualityProfiles().insert(p -> p.setIsBuiltIn(true).setName("Sonar way").setLanguage(XOO1.getKey()));
    QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO1.getKey()));
    RuleDefinitionDto commonRule = db.rules().insertRule(r -> r.setLanguage(XOO1.getKey())).getDefinition();
    db.qualityProfiles().activateRule(profile, commonRule);
    db.qualityProfiles().activateRule(sonarWayProfile, commonRule);
    ruleIndexer.indexAll();
    activeRuleIndexer.indexAll();
    CompareToSonarWay result = call(ws.newRequest().setParam(PARAM_KEY, profile.getKee()).setParam(PARAM_COMPARE_TO_SONAR_WAY, "true")).getCompareToSonarWay();
    assertThat(result).extracting(CompareToSonarWay::getProfile, CompareToSonarWay::getProfileName, CompareToSonarWay::getMissingRuleCount).containsExactly(sonarWayProfile.getKee(), sonarWayProfile.getName(), 0L);
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) IntStream.range(java.util.stream.IntStream.range) EsTester(org.sonar.server.es.EsTester) ActiveRuleIndexer(org.sonar.server.qualityprofile.index.ActiveRuleIndexer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Languages(org.sonar.api.resources.Languages) WebService(org.sonar.api.server.ws.WebService) DateUtils(org.sonar.api.utils.DateUtils) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) PARAM_COMPARE_TO_SONAR_WAY(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_COMPARE_TO_SONAR_WAY) ShowResponse(org.sonarqube.ws.Qualityprofiles.ShowResponse) MediaTypes(org.sonarqube.ws.MediaTypes) RuleIndexer(org.sonar.server.rule.index.RuleIndexer) Language(org.sonar.api.resources.Language) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) QualityProfile(org.sonarqube.ws.Qualityprofiles.ShowResponse.QualityProfile) System2(org.sonar.api.utils.System2) 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) RuleIndex(org.sonar.server.rule.index.RuleIndex) WsActionTester(org.sonar.server.ws.WsActionTester) DEPRECATED(org.sonar.api.rule.RuleStatus.DEPRECATED) NotFoundException(org.sonar.server.exceptions.NotFoundException) CompareToSonarWay(org.sonarqube.ws.Qualityprofiles.ShowResponse.CompareToSonarWay) Rule(org.junit.Rule) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) PARAM_KEY(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) CompareToSonarWay(org.sonarqube.ws.Qualityprofiles.ShowResponse.CompareToSonarWay) Test(org.junit.Test)

Example 4 with PARAM_KEY

use of org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY in project sonarqube by SonarSource.

the class ShowActionTest method json_example.

@Test
public void json_example() {
    Language cs = newLanguage("cs", "C#");
    QProfileDto parentProfile = db.qualityProfiles().insert(p -> p.setKee("AU-TpxcA-iU5OvuD2FL1").setName("Parent Company Profile").setLanguage(cs.getKey()));
    QProfileDto profile = db.qualityProfiles().insert(p -> p.setKee("AU-TpxcA-iU5OvuD2FL3").setName("My Company Profile").setLanguage(cs.getKey()).setIsBuiltIn(false).setRulesUpdatedAt("2016-12-22T19:10:03+0100").setParentKee(parentProfile.getKee()));
    // Active rules
    range(0, 10).mapToObj(i -> db.rules().insertRule(r -> r.setLanguage(cs.getKey())).getDefinition()).forEach(r -> db.qualityProfiles().activateRule(profile, r));
    // Projects
    range(0, 7).mapToObj(i -> db.components().insertPrivateProjectDto()).forEach(project -> db.qualityProfiles().associateWithProject(project, profile));
    ws = new WsActionTester(new ShowAction(db.getDbClient(), new QProfileWsSupport(db.getDbClient(), userSession), new Languages(cs), ruleIndex));
    String result = ws.newRequest().setParam(PARAM_KEY, profile.getKee()).execute().getInput();
    assertJson(result).ignoreFields("rulesUpdatedAt", "lastUsed", "userUpdatedAt").isSimilarTo(ws.getDef().responseExampleAsString());
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) IntStream.range(java.util.stream.IntStream.range) EsTester(org.sonar.server.es.EsTester) ActiveRuleIndexer(org.sonar.server.qualityprofile.index.ActiveRuleIndexer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Languages(org.sonar.api.resources.Languages) WebService(org.sonar.api.server.ws.WebService) DateUtils(org.sonar.api.utils.DateUtils) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) PARAM_COMPARE_TO_SONAR_WAY(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_COMPARE_TO_SONAR_WAY) ShowResponse(org.sonarqube.ws.Qualityprofiles.ShowResponse) MediaTypes(org.sonarqube.ws.MediaTypes) RuleIndexer(org.sonar.server.rule.index.RuleIndexer) Language(org.sonar.api.resources.Language) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) QualityProfile(org.sonarqube.ws.Qualityprofiles.ShowResponse.QualityProfile) System2(org.sonar.api.utils.System2) 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) RuleIndex(org.sonar.server.rule.index.RuleIndex) WsActionTester(org.sonar.server.ws.WsActionTester) DEPRECATED(org.sonar.api.rule.RuleStatus.DEPRECATED) NotFoundException(org.sonar.server.exceptions.NotFoundException) CompareToSonarWay(org.sonarqube.ws.Qualityprofiles.ShowResponse.CompareToSonarWay) Rule(org.junit.Rule) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) PARAM_KEY(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) Language(org.sonar.api.resources.Language) LanguageTesting.newLanguage(org.sonar.server.language.LanguageTesting.newLanguage) Languages(org.sonar.api.resources.Languages) WsActionTester(org.sonar.server.ws.WsActionTester) Test(org.junit.Test)

Example 5 with PARAM_KEY

use of org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY in project sonarqube by SonarSource.

the class ShowActionTest method compare_to_sonar_way_profile.

@Test
public void compare_to_sonar_way_profile() {
    QProfileDto sonarWayProfile = db.qualityProfiles().insert(p -> p.setIsBuiltIn(true).setName("Sonar way").setLanguage(XOO1.getKey()));
    QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO1.getKey()));
    RuleDefinitionDto commonRule = db.rules().insertRule(r -> r.setLanguage(XOO1.getKey())).getDefinition();
    RuleDefinitionDto sonarWayRule1 = db.rules().insertRule(r -> r.setLanguage(XOO1.getKey())).getDefinition();
    RuleDefinitionDto sonarWayRule2 = db.rules().insertRule(r -> r.setLanguage(XOO1.getKey())).getDefinition();
    RuleDefinitionDto profileRule1 = db.rules().insertRule(r -> r.setLanguage(XOO1.getKey())).getDefinition();
    RuleDefinitionDto profileRule2 = db.rules().insertRule(r -> r.setLanguage(XOO1.getKey())).getDefinition();
    RuleDefinitionDto profileRule3 = db.rules().insertRule(r -> r.setLanguage(XOO1.getKey())).getDefinition();
    db.qualityProfiles().activateRule(profile, commonRule);
    db.qualityProfiles().activateRule(profile, profileRule1);
    db.qualityProfiles().activateRule(profile, profileRule2);
    db.qualityProfiles().activateRule(profile, profileRule3);
    db.qualityProfiles().activateRule(sonarWayProfile, commonRule);
    db.qualityProfiles().activateRule(sonarWayProfile, sonarWayRule1);
    db.qualityProfiles().activateRule(sonarWayProfile, sonarWayRule2);
    ruleIndexer.indexAll();
    activeRuleIndexer.indexAll();
    CompareToSonarWay result = call(ws.newRequest().setParam(PARAM_KEY, profile.getKee()).setParam(PARAM_COMPARE_TO_SONAR_WAY, "true")).getCompareToSonarWay();
    assertThat(result).extracting(CompareToSonarWay::getProfile, CompareToSonarWay::getProfileName, CompareToSonarWay::getMissingRuleCount).containsExactly(sonarWayProfile.getKee(), sonarWayProfile.getName(), 2L);
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) IntStream.range(java.util.stream.IntStream.range) EsTester(org.sonar.server.es.EsTester) ActiveRuleIndexer(org.sonar.server.qualityprofile.index.ActiveRuleIndexer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Languages(org.sonar.api.resources.Languages) WebService(org.sonar.api.server.ws.WebService) DateUtils(org.sonar.api.utils.DateUtils) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) PARAM_COMPARE_TO_SONAR_WAY(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_COMPARE_TO_SONAR_WAY) ShowResponse(org.sonarqube.ws.Qualityprofiles.ShowResponse) MediaTypes(org.sonarqube.ws.MediaTypes) RuleIndexer(org.sonar.server.rule.index.RuleIndexer) Language(org.sonar.api.resources.Language) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) QualityProfile(org.sonarqube.ws.Qualityprofiles.ShowResponse.QualityProfile) System2(org.sonar.api.utils.System2) 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) RuleIndex(org.sonar.server.rule.index.RuleIndex) WsActionTester(org.sonar.server.ws.WsActionTester) DEPRECATED(org.sonar.api.rule.RuleStatus.DEPRECATED) NotFoundException(org.sonar.server.exceptions.NotFoundException) CompareToSonarWay(org.sonarqube.ws.Qualityprofiles.ShowResponse.CompareToSonarWay) Rule(org.junit.Rule) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) PARAM_KEY(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) CompareToSonarWay(org.sonarqube.ws.Qualityprofiles.ShowResponse.CompareToSonarWay) Test(org.junit.Test)

Aggregations

QProfileDto (org.sonar.db.qualityprofile.QProfileDto)5 NotFoundException (org.sonar.server.exceptions.NotFoundException)5 PARAM_KEY (org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY)5 IntStream.range (java.util.stream.IntStream.range)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)4 Rule (org.junit.Rule)4 Test (org.junit.Test)4 Language (org.sonar.api.resources.Language)4 Languages (org.sonar.api.resources.Languages)4 DEPRECATED (org.sonar.api.rule.RuleStatus.DEPRECATED)4 WebService (org.sonar.api.server.ws.WebService)4 DateUtils (org.sonar.api.utils.DateUtils)4 DateUtils.parseDateTime (org.sonar.api.utils.DateUtils.parseDateTime)4 System2 (org.sonar.api.utils.System2)4 DbTester (org.sonar.db.DbTester)4 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)4 UserDto (org.sonar.db.user.UserDto)4 EsTester (org.sonar.server.es.EsTester)4 LanguageTesting.newLanguage (org.sonar.server.language.LanguageTesting.newLanguage)4