Search in sources :

Example 1 with DEPRECATED

use of org.sonar.api.rule.RuleStatus.DEPRECATED 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 2 with DEPRECATED

use of org.sonar.api.rule.RuleStatus.DEPRECATED 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 3 with DEPRECATED

use of org.sonar.api.rule.RuleStatus.DEPRECATED in project sonarqube by SonarSource.

the class SearchActionTest method json_example.

@Test
public void json_example() {
    // languages
    Language cs = newLanguage("cs", "C#");
    Language java = newLanguage("java", "Java");
    Language python = newLanguage("py", "Python");
    // profiles
    QProfileDto sonarWayCs = db.qualityProfiles().insert(p -> p.setName("Sonar way").setKee("AU-TpxcA-iU5OvuD2FL3").setIsBuiltIn(true).setLanguage(cs.getKey()));
    QProfileDto myCompanyProfile = db.qualityProfiles().insert(p -> p.setName("My Company Profile").setKee("iU5OvuD2FLz").setLanguage(java.getKey()));
    QProfileDto myBuProfile = db.qualityProfiles().insert(p -> p.setName("My BU Profile").setKee("AU-TpxcA-iU5OvuD2FL1").setParentKee(myCompanyProfile.getKee()).setLanguage(java.getKey()));
    QProfileDto sonarWayPython = db.qualityProfiles().insert(p -> p.setName("Sonar way").setKee("AU-TpxcB-iU5OvuD2FL7").setIsBuiltIn(true).setLanguage(python.getKey()));
    db.qualityProfiles().setAsDefault(sonarWayCs, myCompanyProfile, sonarWayPython);
    // rules
    List<RuleDefinitionDto> javaRules = range(0, 10).mapToObj(i -> db.rules().insertRule(r -> r.setLanguage(java.getKey())).getDefinition()).collect(MoreCollectors.toList());
    List<RuleDefinitionDto> deprecatedJavaRules = range(0, 5).mapToObj(i -> db.rules().insertRule(r -> r.setLanguage(java.getKey()).setStatus(DEPRECATED)).getDefinition()).collect(MoreCollectors.toList());
    range(0, 7).forEach(i -> db.qualityProfiles().activateRule(myCompanyProfile, javaRules.get(i)));
    range(0, 2).forEach(i -> db.qualityProfiles().activateRule(myCompanyProfile, deprecatedJavaRules.get(i)));
    range(0, 10).forEach(i -> db.qualityProfiles().activateRule(myBuProfile, javaRules.get(i)));
    range(0, 5).forEach(i -> db.qualityProfiles().activateRule(myBuProfile, deprecatedJavaRules.get(i)));
    range(0, 2).mapToObj(i -> db.rules().insertRule(r -> r.setLanguage(python.getKey())).getDefinition()).forEach(rule -> db.qualityProfiles().activateRule(sonarWayPython, rule));
    range(0, 3).mapToObj(i -> db.rules().insertRule(r -> r.setLanguage(cs.getKey())).getDefinition()).forEach(rule -> db.qualityProfiles().activateRule(sonarWayCs, rule));
    // project
    range(0, 7).mapToObj(i -> db.components().insertPrivateProjectDto()).forEach(project -> db.qualityProfiles().associateWithProject(project, myBuProfile));
    // User
    UserDto user = db.users().insertUser();
    db.qualityProfiles().addUserPermission(myCompanyProfile, user);
    db.qualityProfiles().addUserPermission(myBuProfile, user);
    userSession.logIn(user);
    underTest = new SearchAction(userSession, new Languages(cs, java, python), dbClient, new ComponentFinder(dbClient, null));
    ws = new WsActionTester(underTest);
    String result = ws.newRequest().execute().getInput();
    assertJson(result).ignoreFields("ruleUpdatedAt", "lastUsed", "userUpdatedAt").isSimilarTo(ws.getDef().responseExampleAsString());
}
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) ComponentFinder(org.sonar.server.component.ComponentFinder) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) Language(org.sonar.api.resources.Language) LanguageTesting.newLanguage(org.sonar.server.language.LanguageTesting.newLanguage) UserDto(org.sonar.db.user.UserDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Languages(org.sonar.api.resources.Languages) WsActionTester(org.sonar.server.ws.WsActionTester) Test(org.junit.Test)

Aggregations

IntStream.range (java.util.stream.IntStream.range)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)3 Rule (org.junit.Rule)3 Test (org.junit.Test)3 Language (org.sonar.api.resources.Language)3 Languages (org.sonar.api.resources.Languages)3 DEPRECATED (org.sonar.api.rule.RuleStatus.DEPRECATED)3 WebService (org.sonar.api.server.ws.WebService)3 DateUtils (org.sonar.api.utils.DateUtils)3 DateUtils.parseDateTime (org.sonar.api.utils.DateUtils.parseDateTime)3 System2 (org.sonar.api.utils.System2)3 DbTester (org.sonar.db.DbTester)3 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)3 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)3 UserDto (org.sonar.db.user.UserDto)3 NotFoundException (org.sonar.server.exceptions.NotFoundException)3 LanguageTesting.newLanguage (org.sonar.server.language.LanguageTesting.newLanguage)3 UserSessionRule (org.sonar.server.tester.UserSessionRule)3 TestRequest (org.sonar.server.ws.TestRequest)3