Search in sources :

Example 56 with QualityProfileDto

use of org.sonar.db.qualityprofile.QualityProfileDto in project sonarqube by SonarSource.

the class RuleUpdaterMediumTest method update_active_rule_parameters_when_updating_custom_rule.

@Test
public void update_active_rule_parameters_when_updating_custom_rule() {
    // Create template rule with 3 parameters
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001")).setLanguage("xoo");
    ruleDao.insert(dbSession, templateRule);
    RuleParamDto templateRuleParam1 = RuleParamDto.createFor(templateRule).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam1);
    RuleParamDto templateRuleParam2 = RuleParamDto.createFor(templateRule).setName("format").setType("STRING").setDescription("format").setDefaultValue("csv");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam2);
    RuleParamDto templateRuleParam3 = RuleParamDto.createFor(templateRule).setName("message").setType("STRING").setDescription("message");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam3);
    // Create custom rule
    RuleDto customRule = RuleTesting.newCustomRule(templateRule).setSeverity(Severity.MAJOR).setLanguage("xoo");
    ruleDao.insert(dbSession, customRule);
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam1.setDefaultValue("a.*"));
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam2.setDefaultValue("txt"));
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam3);
    // Create a quality profile
    QualityProfileDto profileDto = QProfileTesting.newXooP1("org-123");
    db.qualityProfileDao().insert(dbSession, profileDto);
    dbSession.commit();
    // Activate the custom rule
    RuleActivation activation = new RuleActivation(customRule.getKey()).setSeverity(Severity.BLOCKER);
    tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_NAME);
    dbSession.commit();
    dbSession.clearCache();
    // Update custom rule parameter 'regex', add 'message' and remove 'format'
    RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey()).setParameters(ImmutableMap.of("regex", "b.*", "message", "a message"));
    underTest.update(update, userSessionRule);
    dbSession.clearCache();
    // Verify custom rule parameters has been updated
    List<RuleParamDto> params = ruleDao.selectRuleParamsByRuleKey(dbSession, customRule.getKey());
    assertThat(params).hasSize(3);
    Map<String, RuleParamDto> paramsByKey = paramsByKey(params);
    assertThat(paramsByKey.get("regex")).isNotNull();
    assertThat(paramsByKey.get("regex").getDefaultValue()).isEqualTo("b.*");
    assertThat(paramsByKey.get("message")).isNotNull();
    assertThat(paramsByKey.get("message").getDefaultValue()).isEqualTo("a message");
    assertThat(paramsByKey.get("format")).isNotNull();
    assertThat(paramsByKey.get("format").getDefaultValue()).isNull();
    // Verify that severity has not changed
    ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, ActiveRuleKey.of(profileDto.getKey(), customRule.getKey()));
    assertThat(activeRuleDto.getSeverityString()).isEqualTo(Severity.BLOCKER);
    // Verify active rule parameters has been updated
    List<ActiveRuleParamDto> activeRuleParams = db.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
    assertThat(activeRuleParams).hasSize(2);
    Map<String, ActiveRuleParamDto> activeRuleParamsByKey = ActiveRuleParamDto.groupByKey(activeRuleParams);
    assertThat(activeRuleParamsByKey.get("regex").getValue()).isEqualTo("b.*");
    assertThat(activeRuleParamsByKey.get("message").getValue()).isEqualTo("a message");
    assertThat(activeRuleParamsByKey.get("format")).isNull();
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) RuleActivation(org.sonar.server.qualityprofile.RuleActivation) RuleActivator(org.sonar.server.qualityprofile.RuleActivator) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 57 with QualityProfileDto

use of org.sonar.db.qualityprofile.QualityProfileDto in project sonarqube by SonarSource.

the class QProfilesWsMediumTest method reset.

@Test
public void reset() throws Exception {
    QualityProfileDto profile = QProfileTesting.newXooP1("org-123");
    QualityProfileDto subProfile = QProfileTesting.newXooP2("org-123").setParentKee(QProfileTesting.XOO_P1_KEY);
    db.qualityProfileDao().insert(session, profile, subProfile);
    RuleDto rule = createRule(profile.getLanguage(), "rule");
    ActiveRuleDto active1 = ActiveRuleDto.createFor(profile, rule).setSeverity(rule.getSeverityString());
    ActiveRuleDto active2 = ActiveRuleDto.createFor(subProfile, rule).setSeverity("MINOR");
    db.activeRuleDao().insert(session, active1);
    db.activeRuleDao().insert(session, active2);
    session.commit();
    ruIndexer.index();
    activeRuIndexer.index();
    // 0. assert rule child rule is minor
    assertThat(db.activeRuleDao().selectOrFailByKey(session, active2.getKey()).getSeverityString()).isEqualTo("MINOR");
    // 1. reset child rule
    WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, RuleActivationActions.ACTIVATE_ACTION);
    request.setParam("profile_key", subProfile.getKey());
    request.setParam("rule_key", rule.getKey().toString());
    request.setParam("reset", "true");
    request.execute();
    session.clearCache();
    // 2. assert rule child rule is NOT minor
    assertThat(db.activeRuleDao().selectOrFailByKey(session, active2.getKey()).getSeverityString()).isNotEqualTo("MINOR");
}
Also used : WsTester(org.sonar.server.ws.WsTester) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 58 with QualityProfileDto

use of org.sonar.db.qualityprofile.QualityProfileDto in project sonarqube by SonarSource.

the class QProfilesWsMediumTest method activate_rule.

@Test
public void activate_rule() throws Exception {
    QualityProfileDto profile = createProfile("java");
    RuleDto rule = createRule(profile.getLanguage(), "toto");
    session.commit();
    ruIndexer.index();
    // 0. Assert No Active Rule for profile
    assertThat(db.activeRuleDao().selectByProfileKey(session, profile.getKey())).isEmpty();
    // 1. Activate Rule
    WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, RuleActivationActions.ACTIVATE_ACTION);
    request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey());
    request.setParam(RuleActivationActions.RULE_KEY, rule.getKey().toString());
    WsTester.Result result = request.execute();
    session.clearCache();
    // 2. Assert ActiveRule in DAO
    assertThat(db.activeRuleDao().selectByProfileKey(session, profile.getKey())).hasSize(1);
}
Also used : WsTester(org.sonar.server.ws.WsTester) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 59 with QualityProfileDto

use of org.sonar.db.qualityprofile.QualityProfileDto in project sonarqube by SonarSource.

the class QProfilesWsMediumTest method bulk_deactivate_rule.

@Test
public void bulk_deactivate_rule() throws Exception {
    QualityProfileDto profile = createProfile("java");
    RuleDto rule0 = createRule(profile.getLanguage(), "toto1");
    RuleDto rule1 = createRule(profile.getLanguage(), "toto2");
    RuleDto rule2 = createRule(profile.getLanguage(), "toto3");
    RuleDto rule3 = createRule(profile.getLanguage(), "toto4");
    createActiveRule(rule0, profile);
    createActiveRule(rule2, profile);
    createActiveRule(rule3, profile);
    createActiveRule(rule1, profile);
    session.commit();
    ruIndexer.index();
    activeRuIndexer.index();
    // 0. Assert No Active Rule for profile
    assertThat(db.activeRuleDao().selectByProfileKey(session, profile.getKey())).hasSize(4);
    // 1. Deactivate Rule
    WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, BulkRuleActivationActions.BULK_DEACTIVATE_ACTION);
    request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey());
    WsTester.Result result = request.execute();
    session.clearCache();
    // 2. Assert ActiveRule in DAO
    assertThat(db.activeRuleDao().selectByProfileKey(session, profile.getKey())).isEmpty();
}
Also used : WsTester(org.sonar.server.ws.WsTester) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 60 with QualityProfileDto

use of org.sonar.db.qualityprofile.QualityProfileDto in project sonarqube by SonarSource.

the class QProfilesWsMediumTest method add_project_with_key_and_uuid.

@Test
public void add_project_with_key_and_uuid() throws Exception {
    OrganizationDto organizationDto = OrganizationTesting.newOrganizationDto();
    db.organizationDao().insert(session, organizationDto);
    ComponentDto project = ComponentTesting.newProjectDto(organizationDto, "ABCD").setId(1L);
    db.componentDao().insert(session, project);
    QualityProfileDto profile = QProfileTesting.newXooP1("org-123");
    db.qualityProfileDao().insert(session, profile);
    session.commit();
    wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "add_project").setParam("profileKey", profile.getKee()).setParam("projectUuid", project.uuid()).execute().assertNoContent();
    assertThat(tester.get(QProfileFactory.class).getByProjectAndLanguage(session, project.getKey(), "xoo").getKee()).isEqualTo(profile.getKee());
    // Second call must not fail, do nothing
    wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "add_project").setParam("profileKey", profile.getKee()).setParam("projectUuid", project.uuid()).execute().assertNoContent();
    assertThat(tester.get(QProfileFactory.class).getByProjectAndLanguage(session, project.getKey(), "xoo").getKee()).isEqualTo(profile.getKee());
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) OrganizationDto(org.sonar.db.organization.OrganizationDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Aggregations

QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)126 Test (org.junit.Test)76 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)37 RuleDto (org.sonar.db.rule.RuleDto)29 WsTester (org.sonar.server.ws.WsTester)21 RuleQuery (org.sonar.server.rule.index.RuleQuery)14 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)13 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)12 DbSession (org.sonar.db.DbSession)11 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)9 SearchOptions (org.sonar.server.es.SearchOptions)9 ComponentDto (org.sonar.db.component.ComponentDto)8 OrganizationDto (org.sonar.db.organization.OrganizationDto)7 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)6 Date (java.util.Date)5 QualityProfileTesting.newQualityProfileDto (org.sonar.db.qualityprofile.QualityProfileTesting.newQualityProfileDto)5 RuleParamDto (org.sonar.db.rule.RuleParamDto)5 RulesProfile (org.sonar.api.profiles.RulesProfile)4 Language (org.sonar.api.resources.Language)4 RuleKey (org.sonar.api.rule.RuleKey)4